Sources for file plugins/database/sybase.php in version 2.0 Release Candidate 1
Click on a comment to hide it. Click here to show all comments.
<?PHP
/*
* Project: Onyx: PHP Application Backend
* File: plugins/database/sybase.php
* Database Plugin - Sybase
*
* Version: 2.0rc1
* CVS tag: $Id: sybase.php,v 1.2 2002/11/29 07:22:12 bok Exp $
* Author: Robert Amos <bok[at]ausmac.net>
* Andrew Wellington <proton[at]wiretapped.net>
* Copyright: 2001,2002 odynia.org.
*/
class db_plugin {
// initial variables
var $_connections = array(); // database connections
var $_resources = array(); // misc resources
/***************************************************************\
* Method: Connect *
* Description: Connect to the Sybase server *
* Syntax: $db->_connect( string dbname [, string server ****
* [, int port [, string user [, string password]]]]);*
* Returns: resource, or false ****
\***************************************************************/
function _connect ($dbname, $server=NULL, $port=NULL, $user=NULL, $passwd=NULL) {
// check first
if (empty($dbname))
return false;
// do we have a connection to this db open already?
if (isset($this->_connections[$dbname]))
return $this->_connections[$dbname];
// checkering
if (!is_null($port))
$host .= ':'.$port;
// Open connection
if ($onyx_parent->use_persistent_db_conns)
$conn = @sybase_pconnect($host, $user, $passwd);
else
$conn = @sybase_connect($host, $user, $passwd);
// bad connection, set error
if (!$conn) {
trigger_error('Unable to establish connection to Sybase Server: '.$host, WARNING);
return false;
}
if (!sybase_select_db($dbname, $conn)) {
trigger_error('Unable to select specified database ('.$dbname.') in connection to Sybase server
'.$host, WARNING);
return false;
}
// set the resource id into the database for later use
$this->_connections[$dbname] = $conn;
// return it so the calling function can use it
return $this->_connections[$dbname];
}
/***************************************************************\
* Method: Select Database Name *
* Description: Select the appropriate database to connect to *
* Syntax: $db->select ( string database ); *
* Returns: resource, or false *
\***************************************************************/
function select ($dbname) {
// check
if (empty($dbname))
return false;
// already using warez?
if (isset($this->_connections[$dbname])) {
$this->_resources['db_select'] = $this->_connections[$dbname];
return $this->_connections[$dbname];
}
// mummy!
global $onyx_parent;
// set variable stuffs
$server = ( empty($onyx_parent->db_host) ? NULL : $onyx_parent->db_host );
$port = ( empty($onyx_parent->db_port) ? NULL : $onyx_parent->db_port );
$user = ( empty($onyx_parent->db_user) ? NULL : $onyx_parent->db_user );
$passwd = ( empty($onyx_parent->db_passwd) ? NULL : $onyx_parent->db_passwd );
// connect!
if (!$this->_connect($dbname, $server, $port, $user, $passwd))
return false;
// set this as the latest resource
$this->_resources["db_select"] = $this->_connections[$dbname];
// done!
return $this->_connections[$dbname];
}
/***************************************************************\
* Method: Execute given query *
* Description: Execute given SQL query against the database *
* Syntax: $db->exec (string sql [, resource db_select]); *
* Returns: resource, or false *
\***************************************************************/
function exec ($sql, $res=NULL) {
// they better give us an SQL query or there'll be trouble!
if (empty($sql))
return false;
// check magical juarez
if (is_null($res))
$res = $this->_resources["db_select"];
if (empty($res))
return false;
// exec the query i guess, not much else to do
$result = sybase_query($sql, $res);
// problems?
if (!$result)
trigger_error('SQL Query failed. Sybase returned: '.sybase_get_last_message(), WARNING);
// more magic
$this->_resources["db_exec"] = $result;
// return the result index
return $result;
}
/***************************************************************\
* Method: Execute given query *
* Description: Execute given SQL query against the database *
* Syntax: $db->query (string sql [, resource db_select]); *
* Returns: resource, or false *
* Note: This method is simply a wrapper for $db->exec();*
\***************************************************************/
function query ($sql, $res=NULL) {
return $this->exec($sql, $res);
}
/***************************************************************\
* Method: Fetch results! *
* Description: Fetch the results of a $db->exec(); *
* Syntax: $db->fetch ( [ int row [, string column *
* [, resource db_exec ]]] ); *
* Returns: mixed, or false *
\***************************************************************/
function fetch ($row=NULL, $column=NULL, $res=NULL) {
// do checking stuffs
if (!is_null($row) && !is_null($column))
return $this->fetch_result($row, $column, $res);
if (!is_null($row) && is_null($column))
return $this->fetch_array($row, $res);
if (is_null($row) && !is_null($column))
return $this->fetch_column($column, $res);
return $this->fetch_all_array($res);
// wow that was hard, huh?
}
/***************************************************************\
* Method: Fetch all results! *
* Description: Fetch the results of an execas an arrayofarrays *
* Syntax: $db->fetch_all_array ( [ resource db_exec ] ); *
* Returns: array, or false *
* Note: It's remarkable how similar this is to *
* $db->fetch(); is it? *
\***************************************************************/
function fetch_all_array ($res=NULL) {
// juarez?
if (is_null($res))
$res = $this->_resources["db_exec"];
if (empty($res))
return false;
// loopy? crazy? damn these comments are boring
// loop through the rows, grab them all
$num = $this->num_rows($res);
sybase_data_seek($res, 0);
for ($i = 0;$i < $num;$i++)
$result[] = sybase_fetch_array($res);
// we warezish arent we? :P
return $result;
}
/***************************************************************\
* Method: Fetch all results! *
* Description: Fetch the results of an execas an array of objs *
* Syntax: $db->fetch_all_objects ( [ resource db_exec ]); *
* Returns: array, or false *
\***************************************************************/
function fetch_all_objects ($res=NULL) {
// juarez?
if (is_null($res))
$res = $this->_resources["db_exec"];
if (empty($res))
return false;
// loopy? crazy? damn these comments are boring
// loop through the rows, grab them all
$num = $this->num_rows($res);
sybase_data_seek($res, 0);
for ($i = 0;$i < $num;$i++)
$result[] = sybase_fetch_object($res);
// we warezish arent we? :P
return $result;
}
/***************************************************************\
* Method: Fetch all results! *
* Description: Fetch the results of an execas an array of objs *
* Syntax: $db->fetch_all ( [ resource db_exec ] ); *
* Returns: array, or false *
\***************************************************************/
function fetch_all ($res=NULL) {
return $this->fetch_all_array($res);
}
/***************************************************************\
* Method: Fetch a row! *
* Description: Fetch the specified row as an object. *
* Syntax: $db->fetch_object(int row [, resource db_exec ])*
* Returns: array, or false *
* Note: These functions all look the same to me. *
\***************************************************************/
function fetch_object ($row, $res=NULL) {
// juarez?
if (is_null($res))
$res = $this->_resources["db_exec"];
if (empty($res))
return false;
// wow, harsh
if (!sybase_data_seek($res, $row)) {
trigger_error('Unable to jump to row '.$row.' on connection '.$res, WARNING);
return false;
}
$result = sybase_fetch_object($res);
// we warezish arent we? :P
return $result;
}
/***************************************************************\
* Method: Fetch a row! *
* Description: Fetch the specified row as an array. *
* Syntax: $db->fetch_array (int row [, resource db_exec ])*
* Returns: array, or false *
* Note: These functions all look the same to me. *
\***************************************************************/
function fetch_array ($row, $res=NULL) {
// juarez?
if (is_null($res))
$res = $this->_resources["db_exec"];
if (empty($res))
return false;
// wow, harsh
if (!sybase_data_seek($res, $row)) {
trigger_error('Unable to jump to row '.$row.' on connection '.$res, WARNING);
return false;
}
$result = sybase_fetch_array($res);
// we warezish arent we? :P
return $result;
}
/***************************************************************\
* Method: Fetch a column *
* Description: Fetch the specified column as an array. *
* Syntax: $db->fetch_column(string column *
* [, resource db_exec ]); *
* Returns: array, or false *
* Note: These functions look the same. Its true!! *
\***************************************************************/
function fetch_column ($column, $res=NULL) {
if (empty($column))
return false;
// juarez?
if (is_null($res))
$res = $this->_resources["db_exec"];
if (empty($res))
return false;
// loopy? crazy? damn these comments are boring
// loop through the rows, grab them all
$num = $this->num_rows($res);
sybase_data_seek($res, 0);
for ($i = 0;$i < $num;$i++) {
$t = sybase_fetch_array($res);
$result[] = $t[$column];
}
// we warezish arent we? :P
return $result;
}
/***************************************************************\
* Method: Fetch a result *
* Description: Fetch the specified result as a mixed!. *
* Syntax: $db->fetch_result(int row, string column *
* [, resource db_exec ]); *
* Returns: mixed, or false *
* Note: These functions look the same. Its true!! *
\***************************************************************/
function fetch_result ($row, $column, $res=NULL) {
if (empty($column) || !is_int($row))
return false;
// juarez?
if (is_null($res))
$res = $this->_resources["db_exec"];
if (empty($res))
return false;
// ouch, how did i ever come up with this function?
$result = sybase_result($res, $row, $column);
// we warezish arent we? :P
return $result;
}
/***************************************************************\
* Method: num_rows *
* Description: Get the number of rows returned in a select *
* Syntax: $db->num_rows ( [ resource db_exec ] ); *
* Returns: integer, or false *
\***************************************************************/
function num_rows ($res=NULL) {
// phwoar
if (is_null($res))
$res = $this->_resources["db_exec"];
if (empty($res))
return false;
// I don't know how I do it, I'm just so amazing
$result = sybase_num_rows($res);
return $result;
}
/***************************************************************\
* Method: affected_rows *
* Description: Get the number of rows affected by a query *
* Syntax: $db->affected_rows ( [ resource db_exec ] ); *
* Returns: integer, or false *
\***************************************************************/
function affected_rows ($res=NULL) {
// phwoar || dude!
if (is_null($res))
$res = $this->_resources["db_exec"];
if (empty($res))
return false;
// I don't know how I do it, I'm just so amazing || I fear j00.
$result = sybase_affected_rows($res);
return $result;
}
/***************************************************************\
* Method: spec *
* Description: load a database spec file and hand it back *
* Syntax: $db->spec ( string table name ); *
* Returns: object containing the table specs *
\***************************************************************/
function spec ($table) {
global $onyx_parent;
// load the juarez, d00d!
return $onyx_parent->load_plugin($table, 'dbspec');
}
}
?>
