Formatting

This commit is contained in:
Miles Lott 2001-07-26 10:16:42 +00:00
parent aaa79c4767
commit 260e5705bf

View File

@ -15,52 +15,63 @@
/* $Id$ */ /* $Id$ */
class db { class db
var $Host = ""; {
var $Database = ""; var $Host = '';
var $User = ""; var $Database = '';
var $Password = ""; var $User = '';
var $Password = '';
var $use_pconnect = False; var $use_pconnect = False;
var $auto_stripslashes = False; var $auto_stripslashes = False;
var $Halt_On_Error = "yes"; ## "yes" (halt with message), "no" (ignore errors quietly), "report" (ignore errror, but spit a warning) /* "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 $Link_ID = 0;
var $Query_ID = 0; var $Query_ID = 0;
var $Record = array(); var $Record = array();
var $Row = 0; var $Row = 0;
var $Seq_Table = "db_sequence"; var $Seq_Table = 'db_sequence';
var $Errno = 0; var $Errno = 0;
var $Error = ""; var $Error = '';
var $Auto_Free = 0; # Set this to 1 for automatic pg_freeresult on /* Set this to 1 for automatic pg_freeresult on last record. */
# last record. var $Auto_Free = 0;
function ifadd($add, $me) { function ifadd($add, $me)
if("" != $add) return " ".$me.$add; {
if('' != $add) return ' ' . $me . $add;
} }
/* public: constructor */ /* public: constructor */
function db($query = "") { function db($query = '')
{
$this->query($query); $this->query($query);
} }
function connect() { function connect()
if ( 0 == $this->Link_ID ) { {
$cstr = "dbname=".$this->Database. if ( 0 == $this->Link_ID )
$this->ifadd($this->Host, "host="). {
$this->ifadd($this->Port, "port="). $cstr = 'dbname=' . $this->Database
$this->ifadd($this->User, "user="). . $this->ifadd($this->Host, 'host=')
$this->ifadd($this->Password, "password="); . $this->ifadd($this->Port, 'port=')
. $this->ifadd($this->User, 'user=')
. $this->ifadd($this->Password, 'password=');
if ($this->use_pconnect) if ($this->use_pconnect)
{
$this->Link_ID=pg_pconnect($cstr); $this->Link_ID=pg_pconnect($cstr);
}
else else
{
$this->Link_ID=pg_connect($cstr); $this->Link_ID=pg_connect($cstr);
if (!$this->Link_ID) { }
$this->halt("Link-ID == false, pconnect failed"); if (!$this->Link_ID)
{
$this->halt('Link-ID == false, pconnect failed');
} }
} }
} }
@ -71,47 +82,54 @@ class db {
echo '<b>Warning: limit() is no longer used, use limit_query()</b>'; echo '<b>Warning: limit() is no longer used, use limit_query()</b>';
if ($start == 0) { if ($start == 0)
$s = "limit " . $phpgw_info["user"]["preferences"]["common"]["maxmatchs"]; {
} else { $s = 'limit ' . $phpgw_info['user']['preferences']['common']['maxmatchs'];
$s = "limit " . $phpgw_info["user"]["preferences"]["common"]["maxmatchs"] . ",$start"; }
else
{
$s = 'limit ' . $phpgw_info['user']['preferences']['common']['maxmatchs'] . ',' . $start;
} }
return $s; return $s;
} }
// This only affects systems not using persistant connections /* This only affects systems not using persistant connections */
function disconnect() function disconnect()
{ {
return @pg_close($this->Link_ID); return @pg_close($this->Link_ID);
} }
// I added the line and file section so we can have better error reporting. (jengo) /* I added the line and file section so we can have better error reporting. (jengo) */
function query($Query_String, $line = "", $file = "") { function query($Query_String, $line = '', $file = '')
{
/* No empty queries, please, since PHP4 chokes on them. */ /* No empty queries, please, since PHP4 chokes on them. */
if ($Query_String == "")
/* The empty query string is passed on from the constructor, /* The empty query string is passed on from the constructor,
* when calling the class without a query, e.g. in situations * when calling the class without a query, e.g. in situations
* like these: '$db = new db_Subclass;' * like these: '$db = new db_Subclass;'
*/ */
if ($Query_String == '')
{
return 0; return 0;
}
$this->connect(); $this->connect();
# printf("<br>Debug: query = %s<br>\n", $Query_String); /* printf("<br>Debug: query = %s<br>\n", $Query_String); */
$this->Query_ID = pg_Exec($this->Link_ID, $Query_String); $this->Query_ID = pg_Exec($this->Link_ID, $Query_String);
$this->Row = 0; $this->Row = 0;
$this->Error = pg_ErrorMessage($this->Link_ID); $this->Error = pg_ErrorMessage($this->Link_ID);
$this->Errno = ($this->Error == "")?0:1; $this->Errno = ($this->Error == '') ? 0 : 1;
if (! $this->Query_ID) { if (! $this->Query_ID)
$this->halt("Invalid SQL: ".$Query_String, $line, $file); {
$this->halt('Invalid SQL: ' . $Query_String, $line, $file);
} }
return $this->Query_ID; return $this->Query_ID;
} }
// public: perform a query with limited result set /* public: perform a query with limited result set */
function limit_query($Query_String, $offset, $line = '', $file = '', $num_rows = '') function limit_query($Query_String, $offset, $line = '', $file = '', $num_rows = '')
{ {
global $phpgw_info; global $phpgw_info;
@ -139,19 +157,22 @@ class db {
} }
// public: discard the query result // public: discard the query result
function free() { function free()
{
@pg_freeresult($this->Query_ID); @pg_freeresult($this->Query_ID);
$this->Query_ID = 0; $this->Query_ID = 0;
} }
function next_record() { function next_record()
{
$this->Record = @pg_fetch_array($this->Query_ID, $this->Row++); $this->Record = @pg_fetch_array($this->Query_ID, $this->Row++);
$this->Error = pg_ErrorMessage($this->Link_ID); $this->Error = pg_ErrorMessage($this->Link_ID);
$this->Errno = ($this->Error == "")?0:1; $this->Errno = ($this->Error == '') ? 0 : 1;
$stat = is_array($this->Record); $stat = is_array($this->Record);
if (!$stat && $this->Auto_Free) { if (!$stat && $this->Auto_Free)
{
pg_freeresult($this->Query_ID); pg_freeresult($this->Query_ID);
$this->Query_ID = 0; $this->Query_ID = 0;
} }
@ -185,7 +206,6 @@ class db {
return pg_Exec($this->Link_ID,'rollback'); return pg_Exec($this->Link_ID,'rollback');
} }
function lock($table, $mode = 'write') function lock($table, $mode = 'write')
{ {
$result = $this->transaction_begin(); $result = $this->transaction_begin();
@ -201,7 +221,7 @@ class db {
} }
else else
{ {
$result = pg_Exec($this->Link_ID, "lock table $table in share mode"); $result = pg_Exec($this->Link_ID, 'lock table ' . $table . ' in share mode');
} }
} }
else else
@ -219,10 +239,12 @@ class db {
/* public: sequence numbers */ /* public: sequence numbers */
function nextid($seq_name) { function nextid($seq_name)
{
$this->connect(); $this->connect();
if ($this->lock($this->Seq_Table)) { if ($this->lock($this->Seq_Table))
{
/* get sequence number (locked) and increment */ /* get sequence number (locked) and increment */
$q = sprintf("select nextid from %s where seq_name = '%s'", $q = sprintf("select nextid from %s where seq_name = '%s'",
$this->Seq_Table, $this->Seq_Table,
@ -231,15 +253,18 @@ class db {
$res = @pg_Fetch_Array($id, 0); $res = @pg_Fetch_Array($id, 0);
/* No current value, make one */ /* No current value, make one */
if (!is_array($res)) { if (!is_array($res))
{
$currentid = 0; $currentid = 0;
$q = sprintf("insert into %s values('%s', %s)", $q = sprintf("insert into %s values('%s', %s)",
$this->Seq_Table, $this->Seq_Table,
$seq_name, $seq_name,
$currentid); $currentid);
$id = @pg_Exec($this->Link_ID, $q); $id = @pg_Exec($this->Link_ID, $q);
} else { }
$currentid = $res["nextid"]; else
{
$currentid = $res['nextid'];
} }
$nextid = $currentid + 1; $nextid = $currentid + 1;
$q = sprintf("update %s set nextid = '%s' where seq_name = '%s'", $q = sprintf("update %s set nextid = '%s' where seq_name = '%s'",
@ -248,71 +273,83 @@ class db {
$seq_name); $seq_name);
$id = @pg_Exec($this->Link_ID, $q); $id = @pg_Exec($this->Link_ID, $q);
$this->unlock(); $this->unlock();
} else { }
$this->halt("cannot lock ".$this->Seq_Table." - has it been created?"); else
{
$this->halt('cannot lock ' . $this->Seq_Table . ' - has it been created?');
return 0; return 0;
} }
return $nextid; return $nextid;
} }
function metadata($table)
{
function metadata($table) {
$count = 0; $count = 0;
$id = 0; $id = 0;
$res = array(); $res = array();
$this->connect(); $this->connect();
$id = pg_exec($this->Link_ID, "select * from $table"); $id = pg_exec($this->Link_ID, "select * from $table");
if ($id < 0) { if ($id < 0)
{
$this->Error = pg_ErrorMessage($id); $this->Error = pg_ErrorMessage($id);
$this->Errno = 1; $this->Errno = 1;
$this->halt("Metadata query failed."); $this->halt('Metadata query failed.');
} }
$count = pg_NumFields($id); $count = pg_NumFields($id);
for ($i=0; $i<$count; $i++) { for ($i=0; $i<$count; $i++)
$res[$i]["table"] = $table; {
$res[$i]["name"] = pg_FieldName ($id, $i); $res[$i]['table'] = $table;
$res[$i]["type"] = pg_FieldType ($id, $i); $res[$i]['name'] = pg_FieldName ($id, $i);
$res[$i]["len"] = pg_FieldSize ($id, $i); $res[$i]['type'] = pg_FieldType ($id, $i);
$res[$i]["flags"] = ""; $res[$i]['len'] = pg_FieldSize ($id, $i);
$res[$i]['flags'] = '';
} }
pg_FreeResult($id); pg_FreeResult($id);
return $res; return $res;
} }
function affected_rows() { function affected_rows()
{
return pg_cmdtuples($this->Query_ID); return pg_cmdtuples($this->Query_ID);
} }
function num_rows() { function num_rows()
{
return pg_numrows($this->Query_ID); return pg_numrows($this->Query_ID);
} }
function num_fields() { function num_fields()
{
return pg_numfields($this->Query_ID); return pg_numfields($this->Query_ID);
} }
function nf() { function nf()
{
return $this->num_rows(); return $this->num_rows();
} }
function np() { function np()
{
print $this->num_rows(); print $this->num_rows();
} }
function f($Name,$strip_slashes = "") function f($Name,$strip_slashes = '')
{
if ($strip_slashes || ($this->auto_stripslashes && ! $strip_slashes))
{ {
if ($strip_slashes || ($this->auto_stripslashes && ! $strip_slashes)) {
return stripslashes($this->Record[$Name]); return stripslashes($this->Record[$Name]);
} else { }
else
{
return $this->Record[$Name]; return $this->Record[$Name];
} }
} }
function p($Name) { function p($Name)
{
print $this->Record[$Name]; print $this->Record[$Name];
} }
@ -320,42 +357,49 @@ class db {
{ {
global $phpgw; global $phpgw;
if($this->Halt_On_Error == "no") { if($this->Halt_On_Error == "no")
{
return; return;
} }
$this->unlock(); // Just in case there is a table currently locked /* Just in case there is a table currently locked */
$this->unlock();
printf("<b>Database error:</b> %s<br>\n", $msg); printf("<b>Database error:</b> %s<br>\n", $msg);
printf("<b>PostgreSQL Error</b>: %s (%s)<br>\n", printf("<b>PostgreSQL Error</b>: %s (%s)<br>\n",
$this->Errno, $this->Errno,
$this->Error); $this->Error);
if ($file) { if ($file)
{
printf("<br><b>File:</b> %s",$file); printf("<br><b>File:</b> %s",$file);
} }
if ($line) { if ($line)
{
printf("<br><b>Line:</b> %s",$line); printf("<br><b>Line:</b> %s",$line);
} }
if ($this->Halt_On_Error == "yes") { if ($this->Halt_On_Error == 'yes')
echo "<p><b>Session halted.</b>"; {
echo '<p><b>Session halted.</b>';
$phpgw->common->phpgw_exit(True); $phpgw->common->phpgw_exit(True);
} }
} }
function table_names() { function table_names()
{
$this->query("select relname from pg_class where relkind = 'r' and not relname like 'pg_%'"); $this->query("select relname from pg_class where relkind = 'r' and not relname like 'pg_%'");
$i=0; $i=0;
while ($this->next_record()) while ($this->next_record())
{ {
$return[$i]["table_name"]= $this->f(0); $return[$i]['table_name']= $this->f(0);
$return[$i]["tablespace_name"]=$this->Database; $return[$i]['tablespace_name']=$this->Database;
$return[$i]["database"]=$this->Database; $return[$i]['database']=$this->Database;
$i++; $i++;
} }
return $return; return $return;
} }
function create_database($adminname = "", $adminpasswd = "") { function create_database($adminname = '', $adminpasswd = '')
{
$currentUser = $this->User; $currentUser = $this->User;
$currentPassword = $this->Password; $currentPassword = $this->Password;
$currentDatabase = $this->Database; $currentDatabase = $this->Database;
@ -378,7 +422,7 @@ class db {
if($outval != 0) if($outval != 0)
{ {
/* either the rights r not available or the postmaster is not running .... */ /* either the rights r not available or the postmaster is not running .... */
echo 'database creation faliure <BR>'; echo 'database creation failure <BR>';
echo 'please setup the postreSQL database manually<BR>'; echo 'please setup the postreSQL database manually<BR>';
} }
@ -389,3 +433,4 @@ class db {
//return $return; //return $return;
} }
} }