2001-01-11 10:52:33 +01:00
|
|
|
<?php
|
2001-01-13 11:18:50 +01:00
|
|
|
/**************************************************************************\
|
2001-01-16 14:52:32 +01:00
|
|
|
* phpGroupWare API - MS SQL Server support *
|
2001-01-13 11:18:50 +01:00
|
|
|
* (C) Copyright 1998 Cameron Taggart (cameront@wolfenet.com) *
|
|
|
|
* Modified by Guarneri carmelo (carmelo@melting-soft.com) *
|
2001-01-16 14:52:32 +01:00
|
|
|
* Modified by Cameron Just (C.Just@its.uq.edu.au) *
|
2001-01-14 16:37:07 +01:00
|
|
|
* ------------------------------------------------------------------------ *
|
2001-01-16 14:52:32 +01:00
|
|
|
* This is not part of phpGroupWare, but is used by phpGroupWare. *
|
|
|
|
* http://www.phpgroupware.org/ *
|
2001-01-14 16:37:07 +01:00
|
|
|
* ------------------------------------------------------------------------ *
|
2001-01-16 14:52:32 +01:00
|
|
|
* 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:54:40 +02:00
|
|
|
/* echo "<BR>This is using the MSSQL class<BR>"; */
|
2001-01-11 10:52:33 +01:00
|
|
|
|
2001-07-26 12:54:40 +02:00
|
|
|
class db
|
2001-07-01 21:43:04 +02:00
|
|
|
{
|
2001-07-26 12:54:40 +02:00
|
|
|
var $Host = '';
|
|
|
|
var $Database = '';
|
|
|
|
var $User = '';
|
|
|
|
var $Password = '';
|
|
|
|
|
|
|
|
var $Link_ID = 0;
|
|
|
|
var $Query_ID = 0;
|
|
|
|
var $Record = array();
|
|
|
|
var $Row = 0;
|
|
|
|
var $VEOF = -1;
|
|
|
|
|
|
|
|
var $Errno = 0;
|
|
|
|
var $Error = '';
|
|
|
|
var $use_pconnect = True;
|
|
|
|
var $Auto_Free = 0; ## set this to 1 to automatically free results
|
|
|
|
|
|
|
|
function connect()
|
2001-07-01 21:43:04 +02:00
|
|
|
{
|
2001-07-26 12:54:40 +02:00
|
|
|
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)
|
|
|
|
{
|
|
|
|
$this->halt("Link-ID == false, mssql_pconnect failed");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mssql_select_db($this->Database, $this->Link_ID);
|
|
|
|
}
|
|
|
|
}
|
2001-06-26 03:47:36 +02:00
|
|
|
}
|
2001-07-01 21:43:04 +02:00
|
|
|
|
2001-07-26 12:54:40 +02:00
|
|
|
function free_result()
|
2001-07-02 21:17:04 +02:00
|
|
|
{
|
2001-07-26 12:54:40 +02:00
|
|
|
mssql_free_result($this->Query_ID);
|
|
|
|
$this->Query_ID = 0;
|
|
|
|
$this->VEOF = -1;
|
2001-07-02 21:17:04 +02:00
|
|
|
}
|
2001-06-26 03:47:36 +02:00
|
|
|
|
2001-07-26 12:54:40 +02:00
|
|
|
function query($Query_String, $line = '', $file = '')
|
2001-07-02 21:17:04 +02:00
|
|
|
{
|
2001-07-26 12:54:40 +02:00
|
|
|
$this->VEOF = -1;
|
|
|
|
|
|
|
|
if (!$this->Link_ID)
|
|
|
|
{
|
|
|
|
$this->connect();
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->Query_ID = mssql_query($Query_String, $this->Link_ID);
|
|
|
|
$this->Row = 0;
|
|
|
|
if (!$this->Query_ID)
|
|
|
|
{
|
|
|
|
$this->halt("Invalid SQL: " . $Query_String, $line, $file);
|
|
|
|
}
|
|
|
|
return $this->Query_ID;
|
2001-07-02 21:17:04 +02:00
|
|
|
}
|
2001-07-01 21:43:04 +02:00
|
|
|
|
2001-07-26 12:54:40 +02:00
|
|
|
// public: perform a query with limited result set
|
|
|
|
function limit_query($Query_String, $offset, $line = '', $file = '', $num_rows = '')
|
2001-07-01 21:43:04 +02:00
|
|
|
{
|
2001-07-26 12:54:40 +02:00
|
|
|
global $phpgw_info;
|
2001-07-01 21:43:04 +02:00
|
|
|
|
2001-07-26 12:54:40 +02:00
|
|
|
if (! $num_rows)
|
|
|
|
{
|
|
|
|
$num_rows = $phpgw_info['user']['preferences']['common']['maxmatchs'];
|
|
|
|
}
|
2001-07-01 21:43:04 +02:00
|
|
|
|
2001-07-26 12:54:40 +02:00
|
|
|
if ($this->Debug)
|
|
|
|
{
|
|
|
|
printf("Debug: limit_query = %s<br>offset=%d, num_rows=%d<br>\n", $Query_String, $offset, $num_rows);
|
|
|
|
}
|
2001-07-01 21:43:04 +02:00
|
|
|
|
2001-07-26 12:54:40 +02:00
|
|
|
$this->query($Query_String, $line, $file);
|
|
|
|
if ($this->Query_ID)
|
|
|
|
{
|
|
|
|
$this->Row = $offset;
|
|
|
|
// Push cursor to appropriate row in case next_record() is used
|
|
|
|
if ($offset > 0)
|
|
|
|
{
|
|
|
|
@mssql_data_seek($this->Query_ID, $offset);
|
|
|
|
}
|
|
|
|
$this->VEOF = $offset + $num_rows - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->Query_ID;
|
2001-07-01 21:43:04 +02:00
|
|
|
}
|
|
|
|
|
2001-07-26 12:54:40 +02:00
|
|
|
function next_record()
|
2001-07-01 21:43:04 +02:00
|
|
|
{
|
2001-07-26 12:54:40 +02:00
|
|
|
if (!$this->Query_ID)
|
|
|
|
{
|
|
|
|
$this->halt("next_record called with no query pending.");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->VEOF == -1 || ($this->Row++ <= $this->VEOF))
|
2001-07-01 21:43:04 +02:00
|
|
|
{
|
2001-07-26 12:54:40 +02:00
|
|
|
// 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
|
2001-07-01 21:43:04 +02:00
|
|
|
{
|
2001-07-26 12:54:40 +02:00
|
|
|
$this->Record = NULL;
|
2001-07-01 21:43:04 +02:00
|
|
|
}
|
2001-06-26 03:47:36 +02:00
|
|
|
}
|
2001-07-01 21:43:04 +02:00
|
|
|
else
|
2001-07-26 12:54:40 +02:00
|
|
|
{
|
2001-07-01 21:43:04 +02:00
|
|
|
$this->Record = NULL;
|
2001-07-26 12:54:40 +02:00
|
|
|
}
|
2001-07-01 21:43:04 +02:00
|
|
|
|
2001-07-26 12:54:40 +02:00
|
|
|
$stat = is_array($this->Record);
|
|
|
|
if (!$stat && $this->Auto_Free)
|
|
|
|
{
|
|
|
|
$this->free();
|
|
|
|
}
|
2001-07-01 21:43:04 +02:00
|
|
|
|
2001-07-26 12:54:40 +02:00
|
|
|
return $stat;
|
|
|
|
}
|
2001-06-26 03:47:36 +02:00
|
|
|
|
2001-07-26 12:54:40 +02:00
|
|
|
function transaction_begin()
|
|
|
|
{
|
|
|
|
return $this->query('BEGIN TRAN');
|
|
|
|
}
|
2001-06-26 03:47:36 +02:00
|
|
|
|
2001-07-26 12:54:40 +02:00
|
|
|
function transaction_commit()
|
|
|
|
{
|
|
|
|
if (!$this->Errno)
|
|
|
|
{
|
|
|
|
return $this->query('COMMIT TRAN');
|
|
|
|
}
|
2001-06-26 03:47:36 +02:00
|
|
|
|
2001-07-26 12:54:40 +02:00
|
|
|
return False;
|
|
|
|
}
|
2001-06-26 03:47:36 +02:00
|
|
|
|
2001-07-26 12:54:40 +02:00
|
|
|
function transaction_abort()
|
|
|
|
{
|
|
|
|
return $this->query('ROLLBACK TRAN');
|
|
|
|
}
|
2001-06-26 03:47:36 +02:00
|
|
|
|
2001-07-26 12:54:40 +02:00
|
|
|
function seek($pos)
|
|
|
|
{
|
|
|
|
mssql_data_seek($this->Query_ID,$pos);
|
|
|
|
$this->Row = $pos;
|
|
|
|
}
|
2001-06-26 03:47:36 +02:00
|
|
|
|
2001-07-26 12:54:40 +02:00
|
|
|
function metadata($table)
|
|
|
|
{
|
|
|
|
$count = 0;
|
|
|
|
$id = 0;
|
|
|
|
$res = array();
|
2001-06-26 03:47:36 +02:00
|
|
|
|
2001-07-26 12:54:40 +02:00
|
|
|
$this->connect();
|
|
|
|
$id = mssql_query("select * from $table", $this->Link_ID);
|
|
|
|
if (!$id)
|
|
|
|
{
|
|
|
|
$this->halt("Metadata query failed.");
|
|
|
|
}
|
2001-07-01 21:43:04 +02:00
|
|
|
|
2001-07-26 12:54:40 +02:00
|
|
|
$count = mssql_num_fields($id);
|
2001-06-26 03:47:36 +02:00
|
|
|
|
2001-07-26 12:54:40 +02:00
|
|
|
for ($i=0; $i<$count; $i++)
|
|
|
|
{
|
|
|
|
$info = mssql_fetch_field($id, $i);
|
|
|
|
$res[$i]["table"] = $table;
|
|
|
|
$res[$i]["name"] = $info["name"];
|
|
|
|
$res[$i]["len"] = $info["max_length"];
|
|
|
|
$res[$i]["flags"] = $info["numeric"];
|
|
|
|
}
|
|
|
|
$this->free_result();
|
|
|
|
return $res;
|
2001-06-26 03:47:36 +02:00
|
|
|
}
|
|
|
|
|
2001-07-26 12:54:40 +02:00
|
|
|
function affected_rows()
|
|
|
|
{
|
|
|
|
return mssql_affected_rows($this->Query_ID);
|
|
|
|
}
|
2001-06-26 03:47:36 +02:00
|
|
|
|
2001-07-26 12:54:40 +02:00
|
|
|
function num_rows()
|
|
|
|
{
|
|
|
|
return mssql_num_rows($this->Query_ID);
|
|
|
|
}
|
2001-06-26 03:47:36 +02:00
|
|
|
|
2001-07-26 12:54:40 +02:00
|
|
|
function num_fields()
|
|
|
|
{
|
|
|
|
return mssql_num_fields($this->Query_ID);
|
|
|
|
}
|
2001-06-26 03:47:36 +02:00
|
|
|
|
2001-07-26 12:54:40 +02:00
|
|
|
function nf()
|
|
|
|
{
|
|
|
|
return $this->num_rows();
|
|
|
|
}
|
2001-06-26 03:47:36 +02:00
|
|
|
|
2001-07-26 12:54:40 +02:00
|
|
|
function np()
|
|
|
|
{
|
|
|
|
print $this->num_rows();
|
|
|
|
}
|
2001-06-26 03:47:36 +02:00
|
|
|
|
2001-07-26 12:54:40 +02:00
|
|
|
function f($Field_Name)
|
|
|
|
{
|
|
|
|
return $this->Record[strtolower($Field_Name)];
|
|
|
|
}
|
2001-06-26 03:47:36 +02:00
|
|
|
|
2001-07-26 12:54:40 +02:00
|
|
|
function p($Field_Name)
|
|
|
|
{
|
|
|
|
print $this->f($Field_Name);
|
|
|
|
}
|
2001-06-26 03:47:36 +02:00
|
|
|
|
2001-07-26 12:54:40 +02:00
|
|
|
/* public: table locking */
|
|
|
|
function lock($table, $mode="write")
|
|
|
|
{
|
|
|
|
return 1; // FIXME: fill it in!
|
|
|
|
}
|
2001-07-01 21:43:04 +02:00
|
|
|
|
2001-07-26 12:54:40 +02:00
|
|
|
function unlock()
|
|
|
|
{
|
|
|
|
return 1; // FIXME: fill it in!
|
|
|
|
}
|
2001-07-01 21:43:04 +02:00
|
|
|
|
2001-07-26 12:54:40 +02:00
|
|
|
/* private: error handling */
|
|
|
|
function halt($msg, $line = '', $file = '')
|
|
|
|
{
|
|
|
|
global $phpgw;
|
|
|
|
$this->unlock();
|
2001-07-01 21:43:04 +02:00
|
|
|
|
2001-07-26 12:54:40 +02:00
|
|
|
$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).";
|
|
|
|
}
|
2001-07-01 21:43:04 +02:00
|
|
|
|
2001-07-26 12:54:40 +02:00
|
|
|
if ($this->Halt_On_Error == "no")
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2001-07-01 21:43:04 +02:00
|
|
|
|
2001-07-26 12:54:40 +02:00
|
|
|
$this->haltmsg($msg);
|
2001-07-01 21:43:04 +02:00
|
|
|
|
2001-07-26 12:54:40 +02:00
|
|
|
if ($file)
|
|
|
|
{
|
|
|
|
printf("<br><b>File:</b> %s",$file);
|
|
|
|
}
|
2001-07-01 21:43:04 +02:00
|
|
|
|
2001-07-26 12:54:40 +02:00
|
|
|
if ($line)
|
|
|
|
{
|
|
|
|
printf("<br><b>Line:</b> %s",$line);
|
|
|
|
}
|
2001-07-01 21:43:04 +02:00
|
|
|
|
2001-07-26 12:54:40 +02:00
|
|
|
if ($this->Halt_On_Error != "report")
|
|
|
|
{
|
|
|
|
echo "<p><b>Session halted.</b>";
|
|
|
|
$phpgw->common->phpgw_exit(True);
|
|
|
|
}
|
2001-07-01 21:43:04 +02:00
|
|
|
}
|
|
|
|
|
2001-07-26 12:54:40 +02:00
|
|
|
function haltmsg($msg)
|
|
|
|
{
|
|
|
|
printf("<b>Database error:</b> %s<br>\n", $msg);
|
|
|
|
if ($this->Errno != "0" && $this->Error != "()")
|
|
|
|
{
|
|
|
|
printf("<b>MS-SQL Error</b>: %s (%s)<br>\n", $this->Errno, $this->Error);
|
|
|
|
}
|
2001-07-01 21:43:04 +02:00
|
|
|
}
|
2001-06-26 03:47:36 +02:00
|
|
|
|
2001-07-26 12:54:40 +02:00
|
|
|
function table_names()
|
2001-06-26 03:47:36 +02:00
|
|
|
{
|
2001-07-26 12:54:40 +02:00
|
|
|
$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;
|
2001-06-26 03:47:36 +02:00
|
|
|
}
|
2001-07-26 12:54:40 +02:00
|
|
|
}
|
2001-01-11 10:52:33 +01:00
|
|
|
?>
|