Evolved the class to be more current. Untested and incomplete (table locking not implemented). I *hate* locks, anyway...

This commit is contained in:
mdean 2001-07-01 19:43:04 +00:00
parent 50837a1db7
commit 77d13d24d9

View File

@ -19,69 +19,116 @@
# echo "<BR>This is using the MSSQL class<BR>"; # echo "<BR>This is using the MSSQL class<BR>";
class db { class db {
var $Host = ""; var $Host = '';
var $Database = ""; var $Database = '';
var $User = ""; var $User = '';
var $Password = ""; var $Password = '';
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 $VEOF = -1;
var $Errno = 0; var $Errno = 0;
var $Error = ""; var $Error = '';
var $use_pconnect = True;
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 ) { {
$this->Link_ID=mssql_pconnect($this->Host, $this->User, $this->Password); if ( 0 == $this->Link_ID )
{
if ($this->use_pconnect)
$this->Link_ID=mssql_pconnect($this->Host, $this->User, $this->Password);
else
$this->Link_ID=mssql_connect($this->Host, $this->User, $this->Password);
if (!$this->Link_ID) if (!$this->Link_ID)
$this->halt("Link-ID == false, mssql_pconnect failed"); $this->halt("Link-ID == false, mssql_pconnect failed");
else else
mssql_select_db($this->Database, $this->Link_ID); mssql_select_db($this->Database, $this->Link_ID);
} }
} }
function free_result(){
function free_result()
{
mssql_free_result($this->Query_ID); mssql_free_result($this->Query_ID);
$this->Query_ID = 0; $this->Query_ID = 0;
$this->VEOF = -1;
} }
function query($Query_String) { function query($Query_String, $line = '', $file = '')
{
$this->VEOF = -1;
if (!$this->Link_ID) if (!$this->Link_ID)
$this->connect(); $this->connect();
# printf("<br>Debug: query = %s<br>\n", $Query_String);
$this->Query_ID = mssql_query($Query_String, $this->Link_ID); $this->Query_ID = mssql_query($Query_String, $this->Link_ID);
$this->Row = 0; $this->Row = 0;
if (!$this->Query_ID) { if (!$this->Query_ID) {
$this->Errno = 1; $this->halt("Invalid SQL: " . $Query_String, $line, $file);
$this->Error = "General Error (The MSSQL interface cannot return detailed error messages).";
$this->halt("Invalid SQL: ".$Query_String);
} }
return $this->Query_ID; return $this->Query_ID;
} }
function next_record() { function limit_query($Query_String, $offset, $num_rows, $line = '', $file = '')
{
global $phpgw_info;
if ($this->Record = mssql_fetch_row($this->Query_ID)) { if ($this->Debug)
// add to Record[<key>] printf("Debug: limit_query = %s<br>offset=%d, num_rows=%d<br>\n", $Query_String, $offset, $num_rows);
$count = mssql_num_fields($this->Query_ID);
for ($i=0; $i<$count; $i++){ if (!IsSet($num_rows) || $num_rows < 1)
$fieldinfo = mssql_fetch_field($this->Query_ID,$i); $num_rows = $phpgw_info['user']['preferences']['common']['maxmatchs'];
$this->Record[strtolower($fieldinfo->name)] = $this->Record[$i];
} $this->query($Query_String, $line, $file);
$this->Row += 1; if ($this->Query_ID)
$stat = 1; {
} else { $this->Row = $offset;
if ($this->Auto_Free) { // Push cursor to appropriate row in case next_record() is used
$this->free_result(); if ($offset > 0)
} @mssql_data_seek($this->Query_ID, $offset);
$stat = 0;
$this->VEOF = $offset + $num_rows - 1;
} }
return $this->Query_ID;
}
function next_record()
{
if (!$this->Query_ID)
{
$this->halt("next_record called with no query pending.");
return 0;
}
if ($this->VEOF == -1 || ($this->Row++ <= $this->VEOF))
{
// Work around for buggy mssql_fetch_array
$rec = @mssql_fetch_row($this->Query_ID);
if ($rec)
{
$this->Record = array();
for (var $i = 0; $i < count($rec); $i++)
{
$this->Record[$i] = $rec[$i];
$o = mssql_fetch_field($i, $this->Query_ID);
$this->Record[$o->name] = $rec[$i];
}
}
else
$this->Record = NULL;
}
else
$this->Record = NULL;
$stat = is_array($this->Record);
if (!$stat && $this->Auto_Free)
$this->free();
return $stat; return $stat;
} }
@ -115,11 +162,9 @@ class db {
$this->connect(); $this->connect();
$id = mssql_query("select * from $table", $this->Link_ID); $id = mssql_query("select * from $table", $this->Link_ID);
if (!$id) { if (!$id)
$this->Errno = 1;
$this->Error = "General Error (The MSSQL interface cannot return detailed error messages).";
$this->halt("Metadata query failed."); $this->halt("Metadata query failed.");
}
$count = mssql_num_fields($id); $count = mssql_num_fields($id);
for ($i=0; $i<$count; $i++) { for ($i=0; $i<$count; $i++) {
@ -161,12 +206,52 @@ class db {
print $this->f($Field_Name); print $this->f($Field_Name);
} }
function halt($msg) { /* public: table locking */
function lock($table, $mode="write")
{
return 1; // FIXME: fill it in!
}
function unlock()
{
return 1; // FIXME: fill it in!
}
/* private: error handling */
function halt($msg, $line = '', $file = '')
{
global $phpgw;
$this->unlock();
$this->Errno = 1;
$this->Error = mssql_get_last_message();
if ($this->Error == '')
$this->Error = "General Error (The MS-SQL interface did not return a detailed error message).";
if ($this->Halt_On_Error == "no")
return;
$this->haltmsg($msg);
if ($file)
printf("<br><b>File:</b> %s",$file);
if ($line)
printf("<br><b>Line:</b> %s",$line);
if ($this->Halt_On_Error != "report")
{
echo "<p><b>Session halted.</b>";
$phpgw->common->phpgw_exit(True);
}
}
function haltmsg($msg)
{
printf("<b>Database error:</b> %s<br>\n", $msg); printf("<b>Database error:</b> %s<br>\n", $msg);
printf("<b>MSSQL Error</b>: %s (%s)<br>\n", if ($this->Errno != "0" && $this->Error != "()") {
$this->Errno, printf("<b>MS-SQL Error</b>: %s (%s)<br>\n", $this->Errno, $this->Error);
$this->Error); }
die("Session halted.");
} }
function table_names() function table_names()