egroupware/phpgwapi/inc/class.db_pgsql.inc.php

611 lines
13 KiB
PHP
Raw Normal View History

<?php
2001-01-13 11:18:50 +01:00
/**************************************************************************\
* phpGroupWare API - *
* Copyright (c) 1998,1999 SH Online Dienst GmbH Boris Erdmann, *
* Kristian Koehntopp *
* ------------------------------------------------------------------------ *
* This is not part of phpGroupWare, but is used by phpGroupWare. *
* http://www.phpgroupware.org/ *
* ------------------------------------------------------------------------ *
* 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-07-26 12:16:42 +02:00
class db
{
2001-07-26 12:16:42 +02:00
var $Host = '';
var $Database = '';
var $User = '';
var $Password = '';
2001-07-26 12:16:42 +02:00
var $auto_stripslashes = False;
2002-02-10 13:04:10 +01:00
/* 'yes' (halt with message), 'no' (ignore errors quietly), 'report' (ignore errror, but spit a warning) */
2001-07-26 12:16:42 +02:00
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;
2002-02-10 13:04:10 +01:00
/* PostgreSQL changed some things from 6.x -> 7.x */
var $db_version;
2002-02-10 13:04:10 +01:00
/* For our error handling */
var $xmlrpc = False;
var $soap = False;
2001-07-26 12:16:42 +02:00
function ifadd($add, $me)
{
if('' != $add)
{
return ' ' . $me . $add;
}
}
2001-07-26 12:16:42 +02:00
/* public: constructor */
function db($query = '')
{
2001-07-26 12:16:42 +02:00
$this->query($query);
2002-02-10 13:04:10 +01:00
if(ereg('xmlrpc.php',$GLOBALS['PHP_SELF']))
{
$this->xmlrpc = True;
}
2002-02-10 13:04:10 +01:00
if(ereg('soap.php',$GLOBALS['PHP_SELF']))
{
$this->soap = True;
}
2001-07-26 12:16:42 +02:00
}
function connect()
{
2002-02-10 13:04:10 +01:00
if(0 == $this->Link_ID)
2001-07-26 12:16:42 +02:00
{
$cstr = 'dbname=' . $this->Database
. $this->ifadd($this->Host, 'host=')
. $this->ifadd($this->Port, 'port=')
. $this->ifadd($this->User, 'user=')
. $this->ifadd("'".$this->Password."'", 'password=');
2002-02-10 13:04:10 +01:00
if($GLOBALS['phpgw_info']['server']['db_persistent'])
2001-07-26 12:16:42 +02:00
{
$this->Link_ID=pg_pconnect($cstr);
}
else
{
$this->Link_ID=pg_connect($cstr);
}
2002-02-10 13:04:10 +01:00
if(!$this->Link_ID)
2001-07-26 12:16:42 +02:00
{
$this->halt('Link-ID == false, '.($GLOBALS['phpgw_info']['server']['db_persistent']?'p':'').'connect failed');
2001-07-26 12:16:42 +02:00
}
else
{
2002-02-10 13:04:10 +01:00
$this->query('SELECT version()',__LINE__,__FILE__);
$this->next_record();
$version = $this->f('version');
$parts = explode(' ',$version);
$this->db_version = $parts[1];
}
}
}
function to_timestamp($epoch)
{
$db_version = $this->db_version;
2002-02-10 13:04:10 +01:00
if(floor($db_version) == 6)
{
return $this->to_timestamp_6($epoch);
}
else
{
return $this->to_timestamp_7($epoch);
}
}
function from_timestamp($timestamp)
{
2002-02-10 13:04:10 +01:00
if(floor($this->db_version) == 6)
{
return $this->from_timestamp_6($timestamp);
2001-07-26 12:16:42 +02:00
}
else
{
return $this->from_timestamp_7($timestamp);
}
}
2002-02-10 13:04:10 +01:00
/* For PostgreSQL 6.x */
function to_timestamp_6($epoch)
{
}
2001-07-26 12:16:42 +02:00
2002-02-10 13:04:10 +01:00
/* For PostgreSQL 6.x */
function from_timestamp_6($timestamp)
{
}
2002-02-10 13:04:10 +01:00
/* For PostgreSQL 7.x */
function to_timestamp_7($epoch)
{
2002-02-10 13:04:10 +01:00
/* This needs the GMT offset! */
return date('Y-m-d H:i:s-00',$epoch);
}
2002-02-10 13:04:10 +01:00
/* For PostgreSQL 7.x */
function from_timestamp_7($timestamp)
{
ereg('([0-9]{4})-([0-9]{2})-([0-9]{2}) ([0-9]{2}):([0-9]{2}):([0-9]{2})',$timestamp,$parts);
2002-02-10 13:04:10 +01:00
return mktime($parts[4],$parts[5],$parts[6],$parts[2],$parts[3],$parts[1]);
}
2001-07-26 12:16:42 +02:00
function limit($start)
{
2001-07-26 12:16:42 +02:00
echo '<b>Warning: limit() is no longer used, use limit_query()</b>';
if ($start == 0)
{
2002-02-10 13:04:10 +01:00
$s = 'LIMIT ' . $GLOBALS['phpgw_info']['user']['preferences']['common']['maxmatchs'];
2001-07-26 12:16:42 +02:00
}
else
{
2002-02-10 13:04:10 +01:00
$s = 'LIMIT ' . $GLOBALS['phpgw_info']['user']['preferences']['common']['maxmatchs'] . ',' . $start;
2001-07-26 12:16:42 +02:00
}
return $s;
}
2001-07-26 12:16:42 +02:00
/* This only affects systems not using persistant connections */
function disconnect()
{
2001-07-26 12:16:42 +02:00
return @pg_close($this->Link_ID);
}
2001-09-17 02:29:44 +02:00
function db_addslashes($str)
{
2002-02-10 13:04:10 +01:00
if(!isset($str) || $str == '')
{
2001-09-17 02:29:44 +02:00
return '';
}
2001-09-17 02:29:44 +02:00
return addslashes($str);
}
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;'
*/
2002-02-10 13:04:10 +01:00
if($Query_String == '')
2001-07-26 12:16:42 +02:00
{
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
$this->Query_ID = @pg_Exec($this->Link_ID, $Query_String);
2001-07-26 12:16:42 +02:00
$this->Row = 0;
$this->Error = pg_ErrorMessage($this->Link_ID);
$this->Errno = ($this->Error == '') ? 0 : 1;
2002-02-10 13:04:10 +01:00
if(!$this->Query_ID)
2001-07-26 12:16:42 +02:00
{
$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
{
2002-02-10 13:04:10 +01:00
if(!$num_rows)
2001-07-26 12:16:42 +02:00
{
2001-09-02 02:26:16 +02:00
$num_rows = $GLOBALS['phpgw_info']['user']['preferences']['common']['maxmatchs'];
2001-07-26 12:16:42 +02:00
}
2002-02-10 13:04:10 +01:00
if($offset == 0)
2001-07-26 12:16:42 +02:00
{
$Query_String .= ' LIMIT ' . $num_rows;
}
else
{
$Query_String .= ' LIMIT ' . $num_rows . ' OFFSET ' . $offset;
2001-07-26 12:16:42 +02:00
}
2002-02-10 13:04:10 +01:00
if($this->Debug)
2001-07-26 12:16:42 +02:00
{
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
2002-02-10 13:04:10 +01:00
/* public: discard the query result */
2001-07-26 12:16:42 +02:00
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);
2002-02-10 13:04:10 +01:00
if(!$stat && $this->Auto_Free)
2001-07-26 12:16:42 +02:00
{
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()
{
2002-02-10 13:04:10 +01:00
if(!$this->Errno)
2001-07-26 12:16:42 +02:00
{
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 get_last_insert_id($table, $field)
{
/* This will get the last insert ID created on the current connection. Should only be called
* after an insert query is run on a table that has an auto incrementing field. Of note, table
* and field are required because pgsql returns the last inserted OID, which is unique across
* an entire installation. These params allow us to retrieve the sequenced field without adding
* conditional code to the apps.
*/
2002-02-10 13:04:10 +01:00
if(!isset($table) || $table == '' || !isset($field) || $field == '')
{
return -1;
}
$oid = pg_getlastoid($this->Query_ID);
2002-02-10 13:04:10 +01:00
if($oid == -1)
{
return -1;
}
2002-02-10 13:04:10 +01:00
$result = @pg_Exec($this->Link_ID, "SELECT $field FROM $table WHERE oid=$oid");
if(!$result)
{
return -1;
}
$Record = @pg_fetch_array($result, 0);
@pg_freeresult($result);
2002-02-10 13:04:10 +01:00
if(!is_array($Record)) /* OID not found? */
{
return -1;
}
return $Record[0];
}
2002-02-10 13:04:10 +01:00
function lock($table, $mode='write')
2001-07-26 12:16:42 +02:00
{
$result = $this->transaction_begin();
2002-02-10 13:04:10 +01:00
if($mode == 'write')
2001-05-24 09:54:33 +02:00
{
2002-02-10 13:04:10 +01:00
if(is_array($table))
2001-05-24 09:54:33 +02:00
{
2002-02-10 13:04:10 +01:00
while($t = each($table))
2001-07-26 12:16:42 +02:00
{
$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-07-26 12:16:42 +02:00
/* public: sequence numbers */
function nextid($seq_name)
{
$this->connect();
2002-02-10 13:04:10 +01:00
if($this->lock($this->Seq_Table))
2001-07-26 12:16:42 +02:00
{
/* get sequence number (locked) and increment */
2002-02-10 13:04:10 +01:00
$q = sprintf("SELECT nextid FROM %s WHERE seq_name = '%s'",
2001-07-26 12:16:42 +02:00
$this->Seq_Table,
$seq_name);
$id = @pg_Exec($this->Link_ID, $q);
$res = @pg_Fetch_Array($id, 0);
/* No current value, make one */
2002-02-10 13:04:10 +01:00
if(!is_array($res))
2001-07-26 12:16:42 +02:00
{
$currentid = 0;
2002-02-10 13:04:10 +01:00
$q = sprintf("INSERT INTO %s VALUES('%s', %s)",
2001-07-26 12:16:42 +02:00
$this->Seq_Table,
$seq_name,
$currentid);
$id = @pg_Exec($this->Link_ID, $q);
}
else
{
$currentid = $res['nextid'];
}
$nextid = $currentid + 1;
2002-02-10 13:04:10 +01:00
$q = sprintf("UPDATE %s SET nextid = '%s' WHERE seq_name = '%s'",
2001-07-26 12:16:42 +02:00
$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();
2002-02-10 13:04:10 +01:00
$id = pg_exec($this->Link_ID, "SELECT * FROM $table");
if($id < 0)
2001-07-26 12:16:42 +02:00
{
$this->Error = pg_ErrorMessage($id);
$this->Errno = 1;
$this->halt('Metadata query failed.');
}
$count = pg_NumFields($id);
2002-02-10 13:04:10 +01:00
for($i=0; $i<$count; $i++)
2001-07-26 12:16:42 +02:00
{
$res[$i]['table'] = $table;
2002-02-10 13:04:10 +01:00
$res[$i]['name'] = pg_FieldName($id, $i);
$res[$i]['type'] = pg_FieldType($id, $i);
$res[$i]['len'] = pg_FieldSize($id, $i);
2001-07-26 12:16:42 +02:00
$res[$i]['flags'] = '';
}
pg_FreeResult($id);
return $res;
}
function affected_rows()
{
return pg_cmdtuples($this->Query_ID);
}
2001-07-26 12:16:42 +02:00
function num_rows()
{
2001-07-26 12:16:42 +02:00
return pg_numrows($this->Query_ID);
}
2001-07-26 12:16:42 +02:00
function num_fields()
{
2001-07-26 12:16:42 +02:00
return pg_numfields($this->Query_ID);
}
2001-07-26 12:16:42 +02:00
function nf()
{
2001-07-26 12:16:42 +02:00
return $this->num_rows();
}
2001-07-26 12:16:42 +02:00
function np()
{
2001-07-26 12:16:42 +02:00
print $this->num_rows();
}
2001-07-26 12:16:42 +02:00
function f($Name,$strip_slashes = '')
{
2002-02-10 13:04:10 +01:00
if($strip_slashes || ($this->auto_stripslashes && ! $strip_slashes))
2001-07-26 12:16:42 +02:00
{
return stripslashes($this->Record[$Name]);
}
else
{
return $this->Record[$Name];
}
}
function p($Name)
{
print $this->Record[$Name];
}
function halt($msg, $line = '', $file = '')
2001-07-26 12:16:42 +02:00
{
2002-02-10 13:04:10 +01:00
if($this->Halt_On_Error == 'no')
2001-07-26 12:16:42 +02:00
{
return;
}
2001-07-26 12:16:42 +02:00
/* Just in case there is a table currently locked */
$this->transaction_abort();
2002-02-10 13:04:10 +01:00
if($this->xmlrpc || $this->soap)
{
$s = sprintf("Database error: %s\n", $msg);
$s .= sprintf("PostgreSQL Error: %s\n\n (%s)\n\n",$this->Errno,$this->Error);
}
else
{
$s = sprintf("<b>Database error:</b> %s<br>\n", $msg);
$s .= sprintf("<b>PostgreSQL Error</b>: %s (%s)<br>\n",$this->Errno,$this->Error);
}
2001-07-26 12:16:42 +02:00
2002-02-10 13:04:10 +01:00
if($file)
2001-07-26 12:16:42 +02:00
{
2002-02-10 13:04:10 +01:00
if($this->xmlrpc || $this->soap)
{
2002-02-10 13:04:10 +01:00
$s .= sprintf("File: %s\n",$file);
}
else
{
2002-02-10 13:04:10 +01:00
$s .= sprintf('<br><b>File:</b> %s',$file);
}
2001-07-26 12:16:42 +02:00
}
2002-02-10 13:04:10 +01:00
if($line)
2001-07-26 12:16:42 +02:00
{
2002-02-10 13:04:10 +01:00
if($this->xmlrpc || $this->soap)
{
2002-02-10 13:04:10 +01:00
$s .= sprintf("Line: %s\n",$line);
}
else
{
2002-02-10 13:04:10 +01:00
$s .= sprintf("<br><b>Line:</b> %s",$line);
}
2001-07-26 12:16:42 +02:00
}
2002-02-10 13:04:10 +01:00
if($this->Halt_On_Error == 'yes')
2001-07-26 12:16:42 +02:00
{
2002-02-10 13:04:10 +01:00
if(!$this->xmlrpc && !$this->soap)
{
$s .= '<p><b>Session halted.</b>';
}
}
2002-02-10 13:04:10 +01:00
if($this->xmlrpc)
{
xmlrpcfault($s);
}
2002-02-10 13:04:10 +01:00
elseif($this->soap)
{
}
else
{
echo $s;
exit;
2001-07-26 12:16:42 +02:00
}
}
function table_names()
{
2002-02-10 13:04:10 +01:00
$this->query("SELECT relname FROM pg_class WHERE relkind = 'r' AND NOT relname LIKE 'pg_%'");
2001-07-26 12:16:42 +02:00
$i=0;
2002-02-10 13:04:10 +01:00
while($this->next_record())
2001-07-26 12:16:42 +02:00
{
$return[$i]['table_name']= $this->f(0);
$return[$i]['tablespace_name']=$this->Database;
$return[$i]['database']=$this->Database;
$i++;
}
return $return;
}
function index_names()
{
$this->query("SELECT relname FROM pg_class WHERE NOT relname ~ 'pg_.*' AND relkind ='i' ORDER BY relname");
$i=0;
2002-02-10 13:04:10 +01:00
while($this->next_record())
{
$return[$i]['index_name']= $this->f(0);
$return[$i]['tablespace_name']=$this->Database;
$return[$i]['database']=$this->Database;
$i++;
}
return $return;
}
2001-07-26 12:16:42 +02:00
function create_database($adminname = '', $adminpasswd = '')
{
$currentUser = $this->User;
$currentPassword = $this->Password;
$currentDatabase = $this->Database;
2002-02-10 13:04:10 +01:00
if($adminname != '')
2001-07-26 12:16:42 +02:00
{
$this->User = $adminname;
$this->Password = $adminpasswd;
}
2002-02-10 13:04:10 +01:00
if(!$this->Host)
2001-07-26 12:16:42 +02:00
{
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 .... */
2002-02-10 13:04:10 +01:00
echo 'Database creation failure <BR>';
echo 'Please setup the postreSQL database manually<BR>';
2001-07-26 12:16:42 +02:00
}
$this->User = $currentUser;
$this->Password = $currentPassword;
$this->Database = $currentDatabase;
$this->connect();
//return $return;
}
}