formatting

This commit is contained in:
Miles Lott 2001-09-29 00:33:05 +00:00
parent c016448915
commit 4d929cadc2
3 changed files with 425 additions and 348 deletions

View File

@ -15,22 +15,25 @@
/* $Id$ */ /* $Id$ */
class db { class db
var $Host = ""; {
var $Database = ""; var $Host = '';
var $Database = '';
var $Link_ID = 0; var $Link_ID = 0;
var $Query_ID = 0; var $Query_ID = 0;
var $Record = array(); var $Record = array();
var $Row; var $Row;
var $Error = ""; var $Error = '';
var $Auto_Free = 0; ## Set this to 1 for automatic msql_free_result() var $Auto_Free = 0; ## Set this to 1 for automatic msql_free_result()
function connect() { function connect()
{
// Not connected? Then connect? // Not connected? Then connect?
if ( 0 == $this->Link_ID ) { if ( 0 == $this->Link_ID )
{
// Check for local connect // Check for local connect
$this->Link_ID = empty($this->Host)? $this->Link_ID = empty($this->Host)?
$this->Link_ID=msql_pconnect(): $this->Link_ID=msql_pconnect():
@ -38,17 +41,20 @@ class db {
} }
// Still not connected? Raise error. // Still not connected? Raise error.
if ( 0 == $this->Link_ID ) { if ( 0 == $this->Link_ID )
$this->halt("Link-ID == false, pconnect failed"); {
$this->halt('Link-ID == false, pconnect failed');
} }
// Select current database // Select current database
if (!msql_select_db($this->Database, $this->Link_ID)) { if (!msql_select_db($this->Database, $this->Link_ID))
$this->halt("cannot use database ".$this->Database); {
$this->halt('cannot use database '.$this->Database);
} }
} }
function query($Query_String) { function query($Query_String)
{
$this->connect(); $this->connect();
# printf("Debug: query = %s<br>\n", $Query_String); # printf("Debug: query = %s<br>\n", $Query_String);
@ -56,92 +62,108 @@ class db {
$this->Query_ID = msql_query($Query_String,$this->Link_ID); $this->Query_ID = msql_query($Query_String,$this->Link_ID);
$this->Row = 0; $this->Row = 0;
$this->Error = msql_error(); $this->Error = msql_error();
if (!$this->Query_ID) { if (!$this->Query_ID)
$this->halt("Invalid SQL: ".$Query_String); {
$this->halt('Invalid SQL: '.$Query_String);
} }
return $this->Query_ID; return $this->Query_ID;
} }
function next_record() { function next_record()
{
$this->Record = msql_fetch_array($this->Query_ID); $this->Record = msql_fetch_array($this->Query_ID);
$this->Row += 1; $this->Row += 1;
$this->Error = msql_error(); $this->Error = msql_error();
$stat = is_array($this->Record); $stat = is_array($this->Record);
if (!$stat && $this->Auto_Free) { if (!$stat && $this->Auto_Free)
{
msql_free_result($this->Query_ID); msql_free_result($this->Query_ID);
$this->Query_ID = 0; $this->Query_ID = 0;
} }
return $stat; return $stat;
} }
function seek($pos) { function seek($pos)
{
$status = msql_data_seek($this->Query_ID, $pos); $status = msql_data_seek($this->Query_ID, $pos);
if ($status) if ($status)
{
$this->Row = $pos; $this->Row = $pos;
}
return; return;
} }
function metadata($table) { function metadata($table)
{
$count = 0; $count = 0;
$id = 0; $id = 0;
$res = array(); $res = array();
$this->connect(); $this->connect();
$id = @msql_list_fields($this->Database, $table); $id = @msql_list_fields($this->Database, $table);
if ($id < 0) { if ($id < 0)
{
$this->Error = msql_error(); $this->Error = msql_error();
$this->halt("Metadata query failed."); $this->halt('Metadata query failed.');
} }
$count = msql_num_fields($id); $count = msql_num_fields($id);
for ($i=0; $i<$count; $i++) { for ($i=0; $i<$count; $i++)
$res[$i]["table"] = msql_fieldtable ($id, $i); {
$res[$i]["name"] = msql_fieldname ($id, $i); $res[$i]['table'] = msql_fieldtable ($id, $i);
$res[$i]["type"] = msql_fieldtype ($id, $i); $res[$i]['name'] = msql_fieldname ($id, $i);
$res[$i]["len"] = msql_fieldlen ($id, $i); $res[$i]['type'] = msql_fieldtype ($id, $i);
$res[$i]["flags"] = msql_fieldflags ($id, $i); $res[$i]['len'] = msql_fieldlen ($id, $i);
$res["meta"][$res[$i]["name"]] = $i; $res[$i]['flags'] = msql_fieldflags ($id, $i);
$res["num_fields"]= $count; $res['meta'][$res[$i]['name']] = $i;
$res['num_fields']= $count;
} }
msql_free_result($id); msql_free_result($id);
return $res; return $res;
} }
function affected_rows() { function affected_rows()
{
return msql_affected_rows($this->Query_ID); return msql_affected_rows($this->Query_ID);
} }
function num_rows() { function num_rows()
{
return msql_num_rows($this->Query_ID); return msql_num_rows($this->Query_ID);
} }
function num_fields() { function num_fields()
{
return msql_num_fields($this->Query_ID); return msql_num_fields($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) { function f($Name)
{
return $this->Record[$Name]; return $this->Record[$Name];
} }
function p($Name) { function p($Name)
{
print $this->Record[$Name]; print $this->Record[$Name];
} }
function halt($msg) { function halt($msg)
{
printf("<b>Database error:</b> %s<br>\n", $msg); printf("<b>Database error:</b> %s<br>\n", $msg);
printf("<b>MSQL Error</b>: %s<br>\n", $this->Error); printf("<b>MSQL Error</b>: %s<br>\n", $this->Error);
die("Session halted."); die('Session halted.');
} }
} }
?> ?>

View File

@ -14,11 +14,12 @@
\**************************************************************************/ \**************************************************************************/
/* $Id$ */ /* $Id$ */
class db { class db
var $Host = ""; {
var $Database = ""; var $Host = '';
var $User = ""; var $Database = '';
var $Password = ""; var $User = '';
var $Password = '';
var $UseODBCCursor = 0; var $UseODBCCursor = 0;
var $Link_ID = 0; var $Link_ID = 0;
@ -27,20 +28,24 @@ class db {
var $Row = 0; var $Row = 0;
var $Errno = 0; var $Errno = 0;
var $Error = ""; var $Error = '';
var $Auto_Free = 0; ## set this to 1 to automatically free results var $Auto_Free = 0; ## set this to 1 to automatically free results
function connect() { function connect()
if ( 0 == $this->Link_ID ) { {
if ( 0 == $this->Link_ID )
{
$this->Link_ID=odbc_pconnect($this->Database, $this->User, $this->Password, $this->UseODBCCursor); $this->Link_ID=odbc_pconnect($this->Database, $this->User, $this->Password, $this->UseODBCCursor);
if (!$this->Link_ID) { if (!$this->Link_ID)
$this->halt("Link-ID == false, odbc_pconnect failed"); {
$this->halt('Link-ID == false, odbc_pconnect failed');
} }
} }
} }
function query($Query_String) { function query($Query_String)
{
$this->connect(); $this->connect();
# printf("<br>Debug: query = %s<br>\n", $Query_String); # printf("<br>Debug: query = %s<br>\n", $Query_String);
@ -55,124 +60,155 @@ class db {
odbc_binmode($this->Query_ID, 1); odbc_binmode($this->Query_ID, 1);
odbc_longreadlen($this->Query_ID, 4096); odbc_longreadlen($this->Query_ID, 4096);
if (!$this->Query_ID) { if (!$this->Query_ID)
{
$this->Errno = 1; $this->Errno = 1;
$this->Error = "General Error (The ODBC interface cannot return detailed error messages)."; $this->Error = 'General Error (The ODBC interface cannot return detailed error messages).';
$this->halt("Invalid SQL: ".$Query_String); $this->halt('Invalid SQL: '.$Query_String);
} }
return $this->Query_ID; return $this->Query_ID;
} }
function next_record() { function next_record()
{
$this->Record = array(); $this->Record = array();
$stat = odbc_fetch_into($this->Query_ID, ++$this->Row, &$this->Record); $stat = odbc_fetch_into($this->Query_ID, ++$this->Row, &$this->Record);
if (!$stat) { if (!$stat)
if ($this->Auto_Free) { {
if ($this->Auto_Free)
{
odbc_free_result($this->Query_ID); odbc_free_result($this->Query_ID);
$this->Query_ID = 0; $this->Query_ID = 0;
}; };
} else { }
else
{
// add to Record[<key>] // add to Record[<key>]
$count = odbc_num_fields($this->Query_ID); $count = odbc_num_fields($this->Query_ID);
for ($i=1; $i<=$count; $i++) for ($i=1; $i<=$count; $i++)
{
$this->Record[strtolower(odbc_field_name ($this->Query_ID, $i)) ] = $this->Record[ $i - 1 ]; $this->Record[strtolower(odbc_field_name ($this->Query_ID, $i)) ] = $this->Record[ $i - 1 ];
} }
}
return $stat; return $stat;
} }
function seek($pos) { function seek($pos)
{
$this->Row = $pos; $this->Row = $pos;
} }
function metadata($table) { function metadata($table)
{
$count = 0; $count = 0;
$id = 0; $id = 0;
$res = array(); $res = array();
$this->connect(); $this->connect();
$id = odbc_exec($this->Link_ID, "select * from $table"); $id = odbc_exec($this->Link_ID, "select * from $table");
if (!$id) { if (!$id)
{
$this->Errno = 1; $this->Errno = 1;
$this->Error = "General Error (The ODBC interface cannot return detailed error messages)."; $this->Error = 'General Error (The ODBC interface cannot return detailed error messages).';
$this->halt("Metadata query failed."); $this->halt('Metadata query failed.');
} }
$count = odbc_num_fields($id); $count = odbc_num_fields($id);
for ($i=1; $i<=$count; $i++) { for ($i=1; $i<=$count; $i++)
$res[$i]["table"] = $table; {
$res[$i]['table'] = $table;
$name = odbc_field_name ($id, $i); $name = odbc_field_name ($id, $i);
$res[$i]["name"] = $name; $res[$i]['name'] = $name;
$res[$i]["type"] = odbc_field_type ($id, $name); $res[$i]['type'] = odbc_field_type ($id, $name);
$res[$i]["len"] = 0; // can we determine the width of this column? $res[$i]['len'] = 0; // can we determine the width of this column?
$res[$i]["flags"] = ""; // any optional flags to report? $res[$i]['flags'] = ''; // any optional flags to report?
} }
odbc_free_result($id); odbc_free_result($id);
return $res; return $res;
} }
function affected_rows() { function affected_rows()
{
return odbc_num_rows($this->Query_ID); return odbc_num_rows($this->Query_ID);
} }
function num_rows() { function num_rows()
{
# Many ODBC drivers don't support odbc_num_rows() on SELECT statements. # Many ODBC drivers don't support odbc_num_rows() on SELECT statements.
$num_rows = odbc_num_rows($this->Query_ID); $num_rows = odbc_num_rows($this->Query_ID);
//printf ($num_rows."<br>"); //printf ($num_rows."<br>");
# This is a workaround. It is intended to be ugly. # This is a workaround. It is intended to be ugly.
if ($num_rows < 0) { if ($num_rows < 0)
{
$i=10; $i=10;
while (odbc_fetch_row($this->Query_ID, $i)) while (odbc_fetch_row($this->Query_ID, $i))
{
$i*=10; $i*=10;
$j=0;
while ($i!=$j) {
$k= $j+intval(($i-$j)/2);
if (odbc_fetch_row($this->Query_ID, $k))
$j=$k;
else
$i=$k;
if (($i-$j)==1) {
if (odbc_fetch_row($this->Query_ID, $i))
$j=$i;
else
$i=$j;
};
//printf("$i $j $k <br>");
};
$num_rows=$i;
} }
$j=0;
while ($i!=$j)
{
$k= $j+intval(($i-$j)/2);
if (odbc_fetch_row($this->Query_ID, $k))
{
$j=$k;
}
else
{
$i=$k;
}
if (($i-$j)==1)
{
if (odbc_fetch_row($this->Query_ID, $i))
{
$j=$i;
}
else
{
$i=$j;
}
}
//printf("$i $j $k <br>");
}
$num_rows=$i;
}
return $num_rows; return $num_rows;
} }
function num_fields() { function num_fields()
{
return count($this->Record)/2; return count($this->Record)/2;
} }
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($Field_Name) { function f($Field_Name)
{
return $this->Record[strtolower($Field_Name)]; return $this->Record[strtolower($Field_Name)];
} }
function p($Field_Name) { function p($Field_Name)
{
print $this->f($Field_Name); print $this->f($Field_Name);
} }
function halt($msg) { function halt($msg)
{
printf("<b>Database error:</b> %s<br>\n", $msg); printf("<b>Database error:</b> %s<br>\n", $msg);
printf("<b>ODBC Error</b>: %s (%s)<br>\n", printf("<b>ODBC Error</b>: %s (%s)<br>\n",
$this->Errno, $this->Errno,
$this->Error); $this->Error);
die("Session halted."); die('Session halted.');
} }
} }
?> ?>

View File

@ -17,11 +17,12 @@
/* $Id$ */ /* $Id$ */
class db { class db
var $Host = ""; {
var $Database = ""; var $Host = '';
var $User = ""; var $Database = '';
var $Password = ""; var $User = '';
var $Password = '';
var $Link_ID = 0; var $Link_ID = 0;
var $Query_ID = 0; var $Query_ID = 0;
@ -45,101 +46,119 @@ class db {
} }
if (!$this->Link_ID) if (!$this->Link_ID)
{ {
$this->halt('Link-ID == false, '.($GLOBALS['phpgw_info']['server']['db_persistent']?'p':'')..'connect failed"); $this->halt('Link-ID == false, '.($GLOBALS['phpgw_info']['server']['db_persistent']?'p':'')..'connect failed');
} }
if(!sybase_select_db($this->Database, $this->Link_ID)) if(!sybase_select_db($this->Database, $this->Link_ID))
{ {
$this->halt("cannot use database ".$this->Database); $this->halt('cannot use database '.$this->Database);
} }
} }
function query($Query_String) { function query($Query_String)
{
$this->connect(); $this->connect();
# printf("Debug: query = %s<br>\n", $Query_String); # printf("Debug: query = %s<br>\n", $Query_String);
$this->Query_ID = sybase_query($Query_String,$this->Link_ID); $this->Query_ID = sybase_query($Query_String,$this->Link_ID);
$this->Row = 0; $this->Row = 0;
if (!$this->Query_ID) { if (!$this->Query_ID)
$this->halt("Invalid SQL: ".$Query_String); {
$this->halt('Invalid SQL: '.$Query_String);
} }
return $this->Query_ID; return $this->Query_ID;
} }
function next_record() { function next_record()
{
$this->Record = sybase_fetch_array($this->Query_ID); $this->Record = sybase_fetch_array($this->Query_ID);
$this->Row += 1; $this->Row += 1;
$stat = is_array($this->Record); $stat = is_array($this->Record);
if (!$stat && $this->Auto_Free) { if (!$stat && $this->Auto_Free)
{
sybase_free_result($this->Query_ID); sybase_free_result($this->Query_ID);
$this->Query_ID = 0; $this->Query_ID = 0;
} }
return $stat; return $stat;
} }
function seek($pos) { function seek($pos)
{
$status = sybase_data_seek($this->Query_ID, $pos); $status = sybase_data_seek($this->Query_ID, $pos);
if ($status) if ($status)
{
$this->Row = $pos; $this->Row = $pos;
}
return; return;
} }
function metadata($table) { function metadata($table)
{
$count = 0; $count = 0;
$id = 0; $id = 0;
$res = array(); $res = array();
$this->connect(); $this->connect();
$result = $this->query("exec sp_columns $table"); $result = $this->query("exec sp_columns $table");
if ($result < 0) { if ($result < 0)
{
$this->Errno = 1; $this->Errno = 1;
$this->Error = "Metadata query failed"; $this->Error = 'Metadata query failed';
$this->halt("Metadata query failed."); $this->halt('Metadata query failed.');
} }
$count = sybase_num_rows($result); $count = sybase_num_rows($result);
for ($i=0; $i<$count; $i++) { for ($i=0; $i<$count; $i++)
$res[$i]["table"] = $table ; {
$res[$i]["name"] = sybase_result ($result, $i, "COLUMN_NAME"); $res[$i]['table'] = $table ;
$res[$i]["type"] = sybase_result ($result, $i, "TYPE_NAME"); $res[$i]['name'] = sybase_result ($result, $i, 'COLUMN_NAME');
$res[$i]["len"] = sybase_result ($result, $i, "LENGTH"); $res[$i]['type'] = sybase_result ($result, $i, 'TYPE_NAME');
$res[$i]["position"] = sybase_result ($result, $i, "ORDINAL_POSITION"); $res[$i]['len'] = sybase_result ($result, $i, 'LENGTH');
$res[$i]["flags"] = sybase_result ($result, $i, "REMARKS"); $res[$i]['position'] = sybase_result ($result, $i, 'ORDINAL_POSITION');
$res[$i]['flags'] = sybase_result ($result, $i, 'REMARKS');
} }
} }
function affected_rows() { function affected_rows()
{
return sybase_affected_rows($this->Query_ID); return sybase_affected_rows($this->Query_ID);
} }
function num_rows() { function num_rows()
{
return sybase_num_rows($this->Query_ID); return sybase_num_rows($this->Query_ID);
} }
function num_fields() { function num_fields()
{
return sybase_num_fields($this->Query_ID); return sybase_num_fields($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) { function f($Name)
{
return $this->Record[$Name]; return $this->Record[$Name];
} }
function p($Name) { function p($Name)
{
print $this->Record[$Name]; print $this->Record[$Name];
} }
function halt($msg) { function halt($msg)
{
printf("<b>Database error:</b> %s<br>\n", $msg); printf("<b>Database error:</b> %s<br>\n", $msg);
printf("<b>Sybase Error</b><br>\n"); printf("<b>Sybase Error</b><br>\n");
die("Session halted."); die("Session halted.");