egroupware/phpgwapi/inc/class.db_sybase.inc.php

167 lines
4.1 KiB
PHP
Raw Normal View History

<?php
2001-01-13 11:18:50 +01:00
/**************************************************************************\
* phpGroupWare API - Sybase DB support *
* Copyright (c) 1998,1999 SH Online Dienst GmbH Boris Erdmann, *
* Kristian Koehntopp *
* Adapted from db_mysql.inc by Sascha Schumann <sascha@schumann.cx> *
* metadata() contributed by Adelino Monteiro <adelino@infologia.pt> *
* ------------------------------------------------------------------------ *
* This is not part of phpGroupWare, but is used by phpGroupWare. *
* http://www.phpgroupware.org/ *
* ------------------------------------------------------------------------ *
* 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-09-29 02:33:05 +02:00
class db
{
var $Host = '';
var $Database = '';
var $User = '';
var $Password = '';
2001-09-29 02:33:05 +02:00
var $Link_ID = 0;
var $Query_ID = 0;
var $Record = array();
var $Row;
2002-02-10 13:04:10 +01:00
var $Auto_Free = 0; /* Set this to 1 for automatic sybase_free_result() */
function connect()
{
2002-02-10 13:04:10 +01:00
if(0 == $this->Link_ID)
{
2002-02-10 13:04:10 +01:00
if($GLOBALS['phpgw_info']['server']['db_persistent'])
{
2002-02-10 13:04:10 +01:00
$this->Link_ID = sybase_pconnect($this->Host,$this->User,$this->Password);
}
else
{
2002-02-10 13:04:10 +01:00
$this->Link_ID = sybase_connect($this->Host,$this->User,$this->Password);
}
}
2002-02-10 13:04:10 +01:00
if(!$this->Link_ID)
{
2001-09-29 02:33:05 +02:00
$this->halt('Link-ID == false, '.($GLOBALS['phpgw_info']['server']['db_persistent']?'p':'')..'connect failed');
}
if(!sybase_select_db($this->Database, $this->Link_ID))
{
2001-09-29 02:33:05 +02:00
$this->halt('cannot use database '.$this->Database);
}
}
2001-09-29 02:33:05 +02:00
function query($Query_String)
{
$this->connect();
2002-02-10 13:04:10 +01:00
/* printf("Debug: query = %s<br>\n", $Query_String); */
2001-09-29 02:33:05 +02:00
$this->Query_ID = sybase_query($Query_String,$this->Link_ID);
$this->Row = 0;
2002-02-10 13:04:10 +01:00
if(!$this->Query_ID)
2001-09-29 02:33:05 +02:00
{
$this->halt('Invalid SQL: '.$Query_String);
}
return $this->Query_ID;
}
function next_record()
{
$this->Record = sybase_fetch_array($this->Query_ID);
$this->Row += 1;
$stat = is_array($this->Record);
2002-02-10 13:04:10 +01:00
if(!$stat && $this->Auto_Free)
2001-09-29 02:33:05 +02:00
{
sybase_free_result($this->Query_ID);
$this->Query_ID = 0;
}
return $stat;
}
function seek($pos)
{
$status = sybase_data_seek($this->Query_ID, $pos);
2002-02-10 13:04:10 +01:00
if($status)
2001-09-29 02:33:05 +02:00
{
$this->Row = $pos;
}
return;
}
function metadata($table)
{
$count = 0;
$id = 0;
$res = array();
$this->connect();
$result = $this->query("exec sp_columns $table");
2002-02-10 13:04:10 +01:00
if($result < 0)
2001-09-29 02:33:05 +02:00
{
$this->Errno = 1;
$this->Error = 'Metadata query failed';
$this->halt('Metadata query failed.');
}
$count = sybase_num_rows($result);
2002-02-10 13:04:10 +01:00
for($i=0; $i<$count; $i++)
2001-09-29 02:33:05 +02:00
{
$res[$i]['table'] = $table ;
$res[$i]['name'] = sybase_result ($result, $i, 'COLUMN_NAME');
$res[$i]['type'] = sybase_result ($result, $i, 'TYPE_NAME');
$res[$i]['len'] = sybase_result ($result, $i, 'LENGTH');
$res[$i]['position'] = sybase_result ($result, $i, 'ORDINAL_POSITION');
$res[$i]['flags'] = sybase_result ($result, $i, 'REMARKS');
}
}
function affected_rows()
{
return sybase_affected_rows($this->Query_ID);
}
function num_rows()
{
return sybase_num_rows($this->Query_ID);
}
function num_fields()
{
return sybase_num_fields($this->Query_ID);
}
function nf()
{
return $this->num_rows();
}
function np()
{
print $this->num_rows();
}
function f($Name)
{
return $this->Record[$Name];
}
function p($Name)
{
print $this->Record[$Name];
}
function halt($msg)
{
printf("<b>Database error:</b> %s<br>\n", $msg);
printf("<b>Sybase Error</b><br>\n");
die("Session halted.");
}
}
?>