2001-01-11 10:52:33 +01:00
|
|
|
<?php
|
2001-01-13 11:18:50 +01:00
|
|
|
/**************************************************************************\
|
|
|
|
* phpGroupWare API - *
|
|
|
|
* Copyright (c) 1998,1999 SH Online Dienst GmbH Boris Erdmann, *
|
|
|
|
* Kristian Koehntopp *
|
2001-01-14 16:37:07 +01:00
|
|
|
* ------------------------------------------------------------------------ *
|
2001-01-16 14:52:32 +01:00
|
|
|
* This is not part of phpGroupWare, but is used by phpGroupWare. *
|
|
|
|
* http://www.phpgroupware.org/ *
|
2001-01-14 16:37:07 +01:00
|
|
|
* ------------------------------------------------------------------------ *
|
2001-01-16 14:52:32 +01:00
|
|
|
* This program is free software; you can redistribute it and/or modify it *
|
|
|
|
* under the terms of the GNU Lesser General Public License as published *
|
|
|
|
* by the Free Software Foundation; either version 2.1 of the License, or *
|
|
|
|
* any later version. *
|
2001-01-13 11:18:50 +01:00
|
|
|
\**************************************************************************/
|
|
|
|
|
|
|
|
/* $Id$ */
|
2001-01-11 10:52:33 +01:00
|
|
|
|
2001-07-26 12:16:42 +02:00
|
|
|
class db
|
2001-07-02 21:16:20 +02:00
|
|
|
{
|
2001-07-26 12:16:42 +02:00
|
|
|
var $Host = '';
|
|
|
|
var $Database = '';
|
|
|
|
var $User = '';
|
|
|
|
var $Password = '';
|
2001-07-02 21:16:20 +02:00
|
|
|
|
2001-07-26 12:16:42 +02:00
|
|
|
var $use_pconnect = False;
|
|
|
|
var $auto_stripslashes = False;
|
|
|
|
|
|
|
|
/* "yes" (halt with message), "no" (ignore errors quietly), "report" (ignore errror, but spit a warning) */
|
|
|
|
var $Halt_On_Error = 'yes';
|
|
|
|
|
|
|
|
var $Link_ID = 0;
|
|
|
|
var $Query_ID = 0;
|
|
|
|
var $Record = array();
|
|
|
|
var $Row = 0;
|
|
|
|
|
|
|
|
var $Seq_Table = 'db_sequence';
|
|
|
|
|
|
|
|
var $Errno = 0;
|
|
|
|
var $Error = '';
|
|
|
|
|
|
|
|
/* Set this to 1 for automatic pg_freeresult on last record. */
|
|
|
|
var $Auto_Free = 0;
|
|
|
|
|
|
|
|
function ifadd($add, $me)
|
2001-07-02 21:16:20 +02:00
|
|
|
{
|
2001-07-26 12:16:42 +02:00
|
|
|
if('' != $add) return ' ' . $me . $add;
|
2001-07-02 21:16:20 +02:00
|
|
|
}
|
|
|
|
|
2001-07-26 12:16:42 +02:00
|
|
|
/* public: constructor */
|
|
|
|
function db($query = '')
|
2001-07-02 21:16:20 +02:00
|
|
|
{
|
2001-07-26 12:16:42 +02:00
|
|
|
$this->query($query);
|
|
|
|
}
|
|
|
|
|
|
|
|
function connect()
|
|
|
|
{
|
|
|
|
if ( 0 == $this->Link_ID )
|
|
|
|
{
|
|
|
|
$cstr = 'dbname=' . $this->Database
|
|
|
|
. $this->ifadd($this->Host, 'host=')
|
|
|
|
. $this->ifadd($this->Port, 'port=')
|
|
|
|
. $this->ifadd($this->User, 'user=')
|
|
|
|
. $this->ifadd($this->Password, 'password=');
|
|
|
|
if ($this->use_pconnect)
|
|
|
|
{
|
|
|
|
$this->Link_ID=pg_pconnect($cstr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$this->Link_ID=pg_connect($cstr);
|
|
|
|
}
|
|
|
|
if (!$this->Link_ID)
|
|
|
|
{
|
|
|
|
$this->halt('Link-ID == false, pconnect failed');
|
|
|
|
}
|
|
|
|
}
|
2001-07-02 21:16:20 +02:00
|
|
|
}
|
2001-07-26 12:16:42 +02:00
|
|
|
|
|
|
|
function limit($start)
|
2001-07-02 21:16:20 +02:00
|
|
|
{
|
2001-07-26 12:16:42 +02:00
|
|
|
global $phpgw_info;
|
|
|
|
|
|
|
|
echo '<b>Warning: limit() is no longer used, use limit_query()</b>';
|
|
|
|
|
|
|
|
if ($start == 0)
|
|
|
|
{
|
|
|
|
$s = 'limit ' . $phpgw_info['user']['preferences']['common']['maxmatchs'];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$s = 'limit ' . $phpgw_info['user']['preferences']['common']['maxmatchs'] . ',' . $start;
|
|
|
|
}
|
|
|
|
return $s;
|
2001-07-02 21:16:20 +02:00
|
|
|
}
|
|
|
|
|
2001-07-26 12:16:42 +02:00
|
|
|
/* This only affects systems not using persistant connections */
|
|
|
|
function disconnect()
|
2001-07-02 21:16:20 +02:00
|
|
|
{
|
2001-07-26 12:16:42 +02:00
|
|
|
return @pg_close($this->Link_ID);
|
2001-07-02 21:16:20 +02:00
|
|
|
}
|
|
|
|
|
2001-07-26 12:16:42 +02:00
|
|
|
/* I added the line and file section so we can have better error reporting. (jengo) */
|
|
|
|
function query($Query_String, $line = '', $file = '')
|
|
|
|
{
|
|
|
|
/* No empty queries, please, since PHP4 chokes on them. */
|
|
|
|
/* The empty query string is passed on from the constructor,
|
|
|
|
* when calling the class without a query, e.g. in situations
|
|
|
|
* like these: '$db = new db_Subclass;'
|
|
|
|
*/
|
|
|
|
if ($Query_String == '')
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2001-07-01 19:04:42 +02:00
|
|
|
|
2001-07-26 12:16:42 +02:00
|
|
|
$this->connect();
|
2001-05-24 09:54:33 +02:00
|
|
|
|
2001-07-26 12:16:42 +02:00
|
|
|
/* printf("<br>Debug: query = %s<br>\n", $Query_String); */
|
2001-05-24 09:54:33 +02:00
|
|
|
|
2001-07-26 12:16:42 +02:00
|
|
|
$this->Query_ID = pg_Exec($this->Link_ID, $Query_String);
|
|
|
|
$this->Row = 0;
|
|
|
|
|
|
|
|
$this->Error = pg_ErrorMessage($this->Link_ID);
|
|
|
|
$this->Errno = ($this->Error == '') ? 0 : 1;
|
|
|
|
if (! $this->Query_ID)
|
|
|
|
{
|
|
|
|
$this->halt('Invalid SQL: ' . $Query_String, $line, $file);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->Query_ID;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* public: perform a query with limited result set */
|
|
|
|
function limit_query($Query_String, $offset, $line = '', $file = '', $num_rows = '')
|
2001-05-24 09:54:33 +02:00
|
|
|
{
|
2001-07-26 12:16:42 +02:00
|
|
|
global $phpgw_info;
|
|
|
|
|
|
|
|
if (! $num_rows)
|
|
|
|
{
|
|
|
|
$num_rows = $phpgw_info['user']['preferences']['common']['maxmatchs'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($offset == 0)
|
|
|
|
{
|
|
|
|
$Query_String .= ' LIMIT ' . $num_rows;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$Query_String .= ' LIMIT ' . $num_rows . ',' . $offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->Debug)
|
|
|
|
{
|
|
|
|
printf("Debug: limit_query = %s<br>offset=%d, num_rows=%d<br>\n", $Query_String, $offset, $num_rows);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->query($Query_String, $line, $file);
|
2001-05-24 09:54:33 +02:00
|
|
|
}
|
2001-07-26 12:16:42 +02:00
|
|
|
|
|
|
|
// public: discard the query result
|
|
|
|
function free()
|
2001-05-24 09:54:33 +02:00
|
|
|
{
|
2001-07-26 12:16:42 +02:00
|
|
|
@pg_freeresult($this->Query_ID);
|
|
|
|
$this->Query_ID = 0;
|
2001-05-24 09:54:33 +02:00
|
|
|
}
|
|
|
|
|
2001-07-26 12:16:42 +02:00
|
|
|
function next_record()
|
|
|
|
{
|
|
|
|
$this->Record = @pg_fetch_array($this->Query_ID, $this->Row++);
|
2001-05-24 09:54:33 +02:00
|
|
|
|
2001-07-26 12:16:42 +02:00
|
|
|
$this->Error = pg_ErrorMessage($this->Link_ID);
|
|
|
|
$this->Errno = ($this->Error == '') ? 0 : 1;
|
2001-05-24 09:54:33 +02:00
|
|
|
|
2001-07-26 12:16:42 +02:00
|
|
|
$stat = is_array($this->Record);
|
|
|
|
if (!$stat && $this->Auto_Free)
|
|
|
|
{
|
|
|
|
pg_freeresult($this->Query_ID);
|
|
|
|
$this->Query_ID = 0;
|
|
|
|
}
|
|
|
|
return $stat;
|
|
|
|
}
|
|
|
|
|
|
|
|
function seek($pos)
|
|
|
|
{
|
|
|
|
$this->Row = $pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
function transaction_begin()
|
|
|
|
{
|
|
|
|
return $this->query('begin');
|
|
|
|
}
|
|
|
|
|
|
|
|
function transaction_commit()
|
|
|
|
{
|
|
|
|
if (! $this->Errno)
|
|
|
|
{
|
|
|
|
return pg_Exec($this->Link_ID,'commit');
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return False;
|
|
|
|
}
|
|
|
|
}
|
2001-05-24 09:54:33 +02:00
|
|
|
|
2001-07-26 12:16:42 +02:00
|
|
|
function transaction_abort()
|
2001-05-24 09:54:33 +02:00
|
|
|
{
|
2001-07-26 12:16:42 +02:00
|
|
|
return pg_Exec($this->Link_ID,'rollback');
|
|
|
|
}
|
|
|
|
|
|
|
|
function lock($table, $mode = 'write')
|
|
|
|
{
|
|
|
|
$result = $this->transaction_begin();
|
|
|
|
|
|
|
|
if ($mode == 'write')
|
2001-05-24 09:54:33 +02:00
|
|
|
{
|
2001-07-26 12:16:42 +02:00
|
|
|
if (is_array($table))
|
2001-05-24 09:54:33 +02:00
|
|
|
{
|
2001-07-26 12:16:42 +02:00
|
|
|
while ($t = each($table))
|
|
|
|
{
|
|
|
|
$result = pg_Exec($this->Link_ID,'lock table ' . $t[1] . ' in share mode');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$result = pg_Exec($this->Link_ID, 'lock table ' . $table . ' in share mode');
|
2001-05-24 09:54:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2001-07-26 12:16:42 +02:00
|
|
|
$result = 1;
|
2001-05-24 09:54:33 +02:00
|
|
|
}
|
2001-07-26 12:16:42 +02:00
|
|
|
|
|
|
|
return $result;
|
2001-05-24 09:54:33 +02:00
|
|
|
}
|
2001-07-26 12:16:42 +02:00
|
|
|
|
|
|
|
function unlock()
|
2001-05-24 09:54:33 +02:00
|
|
|
{
|
2001-07-26 12:16:42 +02:00
|
|
|
return $this->transaction_commit();
|
2001-05-24 09:54:33 +02:00
|
|
|
}
|
|
|
|
|
2001-01-11 10:52:33 +01:00
|
|
|
|
2001-07-26 12:16:42 +02:00
|
|
|
/* public: sequence numbers */
|
|
|
|
function nextid($seq_name)
|
|
|
|
{
|
|
|
|
$this->connect();
|
|
|
|
|
|
|
|
if ($this->lock($this->Seq_Table))
|
|
|
|
{
|
|
|
|
/* get sequence number (locked) and increment */
|
|
|
|
$q = sprintf("select nextid from %s where seq_name = '%s'",
|
|
|
|
$this->Seq_Table,
|
|
|
|
$seq_name);
|
|
|
|
$id = @pg_Exec($this->Link_ID, $q);
|
|
|
|
$res = @pg_Fetch_Array($id, 0);
|
|
|
|
|
|
|
|
/* No current value, make one */
|
|
|
|
if (!is_array($res))
|
|
|
|
{
|
|
|
|
$currentid = 0;
|
|
|
|
$q = sprintf("insert into %s values('%s', %s)",
|
|
|
|
$this->Seq_Table,
|
|
|
|
$seq_name,
|
|
|
|
$currentid);
|
|
|
|
$id = @pg_Exec($this->Link_ID, $q);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$currentid = $res['nextid'];
|
|
|
|
}
|
|
|
|
$nextid = $currentid + 1;
|
|
|
|
$q = sprintf("update %s set nextid = '%s' where seq_name = '%s'",
|
|
|
|
$this->Seq_Table,
|
|
|
|
$nextid,
|
|
|
|
$seq_name);
|
|
|
|
$id = @pg_Exec($this->Link_ID, $q);
|
|
|
|
$this->unlock();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$this->halt('cannot lock ' . $this->Seq_Table . ' - has it been created?');
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return $nextid;
|
|
|
|
}
|
|
|
|
|
|
|
|
function metadata($table)
|
|
|
|
{
|
|
|
|
$count = 0;
|
|
|
|
$id = 0;
|
|
|
|
$res = array();
|
|
|
|
|
|
|
|
$this->connect();
|
|
|
|
$id = pg_exec($this->Link_ID, "select * from $table");
|
|
|
|
if ($id < 0)
|
|
|
|
{
|
|
|
|
$this->Error = pg_ErrorMessage($id);
|
|
|
|
$this->Errno = 1;
|
|
|
|
$this->halt('Metadata query failed.');
|
|
|
|
}
|
|
|
|
$count = pg_NumFields($id);
|
|
|
|
|
|
|
|
for ($i=0; $i<$count; $i++)
|
|
|
|
{
|
|
|
|
$res[$i]['table'] = $table;
|
|
|
|
$res[$i]['name'] = pg_FieldName ($id, $i);
|
|
|
|
$res[$i]['type'] = pg_FieldType ($id, $i);
|
|
|
|
$res[$i]['len'] = pg_FieldSize ($id, $i);
|
|
|
|
$res[$i]['flags'] = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
pg_FreeResult($id);
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
|
|
|
function affected_rows()
|
|
|
|
{
|
|
|
|
return pg_cmdtuples($this->Query_ID);
|
|
|
|
}
|
2001-01-11 10:52:33 +01:00
|
|
|
|
2001-07-26 12:16:42 +02:00
|
|
|
function num_rows()
|
2001-06-01 10:57:52 +02:00
|
|
|
{
|
2001-07-26 12:16:42 +02:00
|
|
|
return pg_numrows($this->Query_ID);
|
2001-06-01 10:57:52 +02:00
|
|
|
}
|
2001-06-01 11:53:42 +02:00
|
|
|
|
2001-07-26 12:16:42 +02:00
|
|
|
function num_fields()
|
2001-06-04 01:50:50 +02:00
|
|
|
{
|
2001-07-26 12:16:42 +02:00
|
|
|
return pg_numfields($this->Query_ID);
|
2001-06-04 01:50:50 +02:00
|
|
|
}
|
2001-07-26 12:16:42 +02:00
|
|
|
|
|
|
|
function nf()
|
2001-06-04 01:50:50 +02:00
|
|
|
{
|
2001-07-26 12:16:42 +02:00
|
|
|
return $this->num_rows();
|
2001-06-04 01:50:50 +02:00
|
|
|
}
|
|
|
|
|
2001-07-26 12:16:42 +02:00
|
|
|
function np()
|
2001-06-01 11:53:42 +02:00
|
|
|
{
|
2001-07-26 12:16:42 +02:00
|
|
|
print $this->num_rows();
|
2001-06-01 11:53:42 +02:00
|
|
|
}
|
2001-06-01 10:57:52 +02:00
|
|
|
|
2001-07-26 12:16:42 +02:00
|
|
|
function f($Name,$strip_slashes = '')
|
|
|
|
{
|
|
|
|
if ($strip_slashes || ($this->auto_stripslashes && ! $strip_slashes))
|
|
|
|
{
|
|
|
|
return stripslashes($this->Record[$Name]);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return $this->Record[$Name];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function p($Name)
|
|
|
|
{
|
|
|
|
print $this->Record[$Name];
|
|
|
|
}
|
|
|
|
|
|
|
|
function halt($msg, $line = "", $file = "")
|
|
|
|
{
|
|
|
|
global $phpgw;
|
|
|
|
|
|
|
|
if($this->Halt_On_Error == "no")
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* Just in case there is a table currently locked */
|
|
|
|
$this->unlock();
|
|
|
|
|
|
|
|
printf("<b>Database error:</b> %s<br>\n", $msg);
|
|
|
|
printf("<b>PostgreSQL Error</b>: %s (%s)<br>\n",
|
|
|
|
$this->Errno,
|
|
|
|
$this->Error);
|
|
|
|
if ($file)
|
|
|
|
{
|
|
|
|
printf("<br><b>File:</b> %s",$file);
|
|
|
|
}
|
|
|
|
if ($line)
|
|
|
|
{
|
|
|
|
printf("<br><b>Line:</b> %s",$line);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->Halt_On_Error == 'yes')
|
|
|
|
{
|
|
|
|
echo '<p><b>Session halted.</b>';
|
|
|
|
$phpgw->common->phpgw_exit(True);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function table_names()
|
|
|
|
{
|
|
|
|
$this->query("select relname from pg_class where relkind = 'r' and not relname like 'pg_%'");
|
|
|
|
$i=0;
|
|
|
|
while ($this->next_record())
|
|
|
|
{
|
|
|
|
$return[$i]['table_name']= $this->f(0);
|
|
|
|
$return[$i]['tablespace_name']=$this->Database;
|
|
|
|
$return[$i]['database']=$this->Database;
|
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
|
|
|
function create_database($adminname = '', $adminpasswd = '')
|
|
|
|
{
|
|
|
|
$currentUser = $this->User;
|
|
|
|
$currentPassword = $this->Password;
|
|
|
|
$currentDatabase = $this->Database;
|
|
|
|
|
|
|
|
if ($adminname != "")
|
|
|
|
{
|
|
|
|
$this->User = $adminname;
|
|
|
|
$this->Password = $adminpasswd;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! $this->Host)
|
|
|
|
{
|
|
|
|
system('createdb ' . $currentDatabase, $outval);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
system('createdb -h ' . $this->Host . ' ' . $currentDatabase, $outval);
|
|
|
|
}
|
|
|
|
|
|
|
|
if($outval != 0)
|
|
|
|
{
|
|
|
|
/* either the rights r not available or the postmaster is not running .... */
|
|
|
|
echo 'database creation failure <BR>';
|
|
|
|
echo 'please setup the postreSQL database manually<BR>';
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->User = $currentUser;
|
|
|
|
$this->Password = $currentPassword;
|
|
|
|
$this->Database = $currentDatabase;
|
|
|
|
$this->connect();
|
|
|
|
//return $return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|