Added some missing functionality.

This commit is contained in:
mdean 2001-06-26 01:47:36 +00:00
parent 2fe4597371
commit 40ec10583d

View File

@ -19,136 +19,168 @@
# 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 $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=mssql_pconnect($this->Host, $this->User, $this->Password); $this->Link_ID=mssql_pconnect($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;
} }
function query($Query_String) { function query($Query_String) {
if (!$this->Link_ID) if (!$this->Link_ID)
$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 = 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->Errno = 1;
$this->Error = "General Error (The MSSQL interface cannot return detailed error messages)."; $this->Error = "General Error (The MSSQL 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() {
if ($this->Record = mssql_fetch_row($this->Query_ID)) { if ($this->Record = mssql_fetch_row($this->Query_ID)) {
// add to Record[<key>] // add to Record[<key>]
$count = mssql_num_fields($this->Query_ID); $count = mssql_num_fields($this->Query_ID);
for ($i=0; $i<$count; $i++){ for ($i=0; $i<$count; $i++){
$fieldinfo = mssql_fetch_field($this->Query_ID,$i); $fieldinfo = mssql_fetch_field($this->Query_ID,$i);
$this->Record[strtolower($fieldinfo->name)] = $this->Record[$i]; $this->Record[strtolower($fieldinfo->name)] = $this->Record[$i];
} }
$this->Row += 1; $this->Row += 1;
$stat = 1; $stat = 1;
} else { } else {
if ($this->Auto_Free) { if ($this->Auto_Free) {
$this->free_result(); $this->free_result();
} }
$stat = 0; $stat = 0;
} }
return $stat; return $stat;
} }
function seek($pos) { function transaction_begin()
{
return $this->query('BEGIN TRAN');
}
function transaction_commit()
{
if (!$this->Errno)
return $this->query('COMMIT TRAN');
return False;
}
function transaction_abort()
{
return $this->query('ROLLBACK TRAN');
}
function seek($pos) {
mssql_data_seek($this->Query_ID,$pos); mssql_data_seek($this->Query_ID,$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 = 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->Errno = 1;
$this->Error = "General Error (The MSSQL interface cannot return detailed error messages)."; $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++) {
$info = mssql_fetch_field($id, $i); $info = mssql_fetch_field($id, $i);
$res[$i]["table"] = $table; $res[$i]["table"] = $table;
$res[$i]["name"] = $info["name"]; $res[$i]["name"] = $info["name"];
$res[$i]["len"] = $info["max_length"]; $res[$i]["len"] = $info["max_length"];
$res[$i]["flags"] = $info["numeric"]; $res[$i]["flags"] = $info["numeric"];
} }
$this->free_result(); $this->free_result();
return $res; return $res;
} }
function affected_rows() { function affected_rows() {
return mssql_affected_rows($this->Query_ID); return mssql_affected_rows($this->Query_ID);
} }
function num_rows() { function num_rows() {
return mssql_num_rows($this->Query_ID); return mssql_num_rows($this->Query_ID);
} }
function num_fields() { function num_fields() {
return mssql_num_fields($this->Query_ID); return mssql_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($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>MSSQL Error</b>: %s (%s)<br>\n", printf("<b>MSSQL Error</b>: %s (%s)<br>\n",
$this->Errno, $this->Errno,
$this->Error); $this->Error);
die("Session halted."); die("Session halted.");
} }
function table_names()
{
$this->query("select name from sysobjects where type='u' and name != 'dtproperties'");
$i = 0;
while ($info = @mssql_fetch_row($this->Query_ID))
{
$return[$i]['table_name'] = $info[0];
$return[$i]['tablespace_name'] = $this->Database;
$return[$i]['database'] = $this->Database;
$i++;
}
return $return;
}
} }
?> ?>