Source for file sybase.php
Documentation is available at sybase.php
/* vim: set expandtab tabstop=4 shiftwidth=4 foldmethod=marker: */
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2004 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.02 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors: Sterling Hughes <sterling@php.net> |
// | Maintainer: Daniel Convissor <danielc@php.net> |
// +----------------------------------------------------------------------+
// $Id: sybase.php,v 1.50 2004/03/05 01:46:53 danielc Exp $
// - This driver may fail with multiple connections under the same
// user/pass/host and different databases
require_once 'DB/common.php';
* Database independent query interface definition for PHP's Sybase
* @version $Id: sybase.php,v 1.50 2004/03/05 01:46:53 danielc Exp $
* @author Sterling Hughes <sterling@php.net>
* Connect to a database and log in as the specified user.
* @param $dsn the data source name (see DB::parseDSN for syntax)
* @param $persistent (optional) whether the connection should
* @return int DB_OK on success, a DB error on failure
function connect($dsninfo, $persistent = false )
$interface = $dsninfo['hostspec'] ? $dsninfo['hostspec'] : 'localhost';
$connect_function = $persistent ? 'sybase_pconnect' : 'sybase_connect';
if ($interface && $dsninfo['username'] && $dsninfo['password']) {
$conn = @$connect_function($interface, $dsninfo['username'],
} elseif ($interface && $dsninfo['username']) {
* Using false for pw as a workaround to avoid segfault.
$conn = @$connect_function($interface, $dsninfo['username'],
if ($dsninfo['database']) {
if (!@sybase_select_db ($dsninfo['database'], $conn)) {
null , null , @sybase_get_last_message ());
$this->_db = $dsninfo['database'];
* Log out and disconnect from the database.
* @return bool true on success, false if not connected.
* Get the last server error messge (if any)
* @return string sybase last error message
return @sybase_get_last_message ();
* Determine PEAR::DB error code from the database's text error message.
* @param string $errormsg error message returned from the database
* @return integer an error number from a DB error constant
if (!isset ($error_regexps)) {
'/Incorrect syntax near/'
'/^Unclosed quote before the character string [\"\'].*[\"\']\./'
'/Implicit conversion from datatype [\"\'].+[\"\'] to [\"\'].+[\"\'] is not allowed\./'
'/Cannot drop the table [\"\'].+[\"\'], because it doesn\'t exist in the system catalogs\./'
'/Only the owner of object [\"\'].+[\"\'] or a user with System Administrator \(SA\) role can run this command\./'
'/^.+ permission denied on object .+, database .+, owner .+/'
'/^.* permission denied, database .+, owner .+/'
'/There is already an object named/'
'/does not allow null values/'
'/Command has been aborted/'
foreach ($error_regexps as $regexp => $code) {
// {{{ sybaseRaiseError()
* Gather information about an error, then use that info to create a
* DB error object and finally return that object.
* @param integer $errno PEAR error number (usually a DB constant) if
* manually raising an error
* @return object DB error object
* @see DB_common::raiseError()
return $this->raiseError($errno, null , null , null , $native);
* Send a query to Sybase and return the results as a Sybase resource
* @return mixed returns a valid Sybase result for successful SELECT
* queries, DB_OK for other successful queries. A DB error is
if (!@sybase_select_db ($this->_db, $this->connection)) {
$query = $this->modifyQuery ($query);
$result = @sybase_query ('BEGIN TRANSACTION', $this->connection);
$result = @sybase_query ($query, $this->connection);
$numrows = $this->numRows($result);
$this->num_rows[(int) $result] = $numrows;
// Determine which queries that should return data, and which
// should return an error code only.
return $ismanip ? DB_OK : $result;
* Move the internal sybase result pointer to the next available result
* @param a valid sybase result resource
* @return true if a result is available otherwise return false
* Fetch a row and insert the data into an existing array.
* Formating of the array and the data therein are configurable.
* See DB_result::fetchInto() for more information.
* @param resource $result query result identifier
* @param array $arr (reference) array where data from the row
* @param int $fetchmode how the resulting array should be indexed
* @param int $rownum the row number to fetch
* @return mixed DB_OK on success, null when end of result set is
* @see DB_result::fetchInto()
function fetchInto ($result, &$arr, $fetchmode, $rownum=null )
if (!@sybase_data_seek ($result, $rownum)) {
$arr = @sybase_fetch_assoc ($result);
if ($arr = @sybase_fetch_array ($result)) {
foreach ($arr as $key => $value) {
$arr = @sybase_fetch_row ($result);
// reported not work as seems that sybase_get_last_message()
// always return a message here
//if ($errmsg = @sybase_get_last_message()) {
// return $this->sybaseRaiseError($errmsg);
$this->_rtrimArrayValues ($arr);
$this->_convertNullArrayValuesToEmpty ($arr);
* Free the internal resources associated with $result.
* @param $result Sybase result identifier
* @return bool true on success, false if $result is invalid
unset ($this->num_rows[(int) $result]);
return @sybase_free_result ($result);
* Get the number of columns in a result set.
* @param $result Sybase result identifier
* @return int the number of columns per row in $result
$cols = @sybase_num_fields ($result);
* Get the number of rows in a result set.
* @param $result Sybase result identifier
* @return int the number of rows in $result
$rows = @sybase_num_rows ($result);
* Gets the number of rows affected by the data manipulation
* query. For other queries, this function returns 0.
* @return number of rows affected by the last query
$result = @sybase_affected_rows ($this->connection);
* Returns the next free id in a sequence
* @param string $seq_name name of the sequence
* @param boolean $ondemand when true, the seqence is automatically
* created if it does not exist
* @return int the next id number in the sequence. DB_Error if problem.
* @see DB_common::nextID()
function nextId($seq_name, $ondemand = true )
$seqname = $this->getSequenceName ($seq_name);
if (!@sybase_select_db ($this->_db, $this->connection)) {
$this->pushErrorHandling (PEAR_ERROR_RETURN );
$result = $this->query(" INSERT INTO $seqname (vapor) VALUES (0)" );
$this->popErrorHandling ();
$result = & $this->query(" SELECT @@IDENTITY FROM $seqname" );
* @param string $seq_name name of the new sequence
* @return int DB_OK on success. A DB_Error object is returned if
* @see DB_common::createSequence()
$seqname = $this->getSequenceName ($seq_name);
return $this->query(" CREATE TABLE $seqname ".
'(id numeric(10,0) IDENTITY NOT NULL ,' .
* @param string $seq_name name of the sequence to be deleted
* @return int DB_OK on success. DB_Error if problems.
* @see DB_common::dropSequence()
$seqname = $this->getSequenceName ($seq_name);
return $this->query(" DROP TABLE $seqname" );
* Returns the query needed to get some backend info
* @param string $type What kind of info you want to retrieve
* @return string The SQL query string
return "select name from sysobjects where type = 'U' order by name";
return "select name from sysobjects where type = 'V'";
* Enable/disable automatic commits
// XXX if $this->transaction_opcount > 0, we should probably
* Commit the current transaction.
if (!@sybase_select_db ($this->_db, $this->connection)) {
$result = @sybase_query ('COMMIT', $this->connection);
* Roll back (undo) the current transaction.
if (!@sybase_select_db ($this->_db, $this->connection)) {
$result = @sybase_query ('ROLLBACK', $this->connection);
* Returns information about a table or a result set.
* NOTE: only supports 'table' and 'flags' if <var>$result</var>
* @param object|string $result DB_result object from a query or a
* string containing the name of a table
* @param int $mode a valid tableInfo mode
* @return array an associative array with the information requested
* or an error object if something is wrong
* @see DB_common::tableInfo()
if (isset ($result->result )) {
* Probably received a result object.
* Extract the result resource identifier.
* Probably received a table name.
* Create a result resource identifier.
if (!@sybase_select_db ($this->_db, $this->connection)) {
$id = @sybase_query (" SELECT * FROM $result WHERE 1=0" ,
* Probably received a result resource identifier.
* Depricated. Here for compatibility only.
$case_func = 'strtolower';
$count = @sybase_num_fields ($id);
// made this IF due to performance (one if is faster than $count if's)
for ($i=0; $i< $count; $i++ ) {
$f = @sybase_fetch_field ($id, $i);
// column_source is often blank
$res[$i]['table'] = $case_func($result);
$res[$i]['table'] = $case_func($f->column_source );
$res[$i]['name'] = $case_func($f->name );
$res[$i]['type'] = $f->type;
$res[$i]['len'] = $f->max_length;
$res[$i]['flags'] = $this->_sybase_field_flags (
$res[$i]['table'], $res[$i]['name']);
$res['num_fields'] = $count;
for ($i=0; $i< $count; $i++ ) {
$f = @sybase_fetch_field ($id, $i);
// column_source is often blank
$res[$i]['table'] = $case_func($result);
$res[$i]['table'] = $case_func($f->column_source );
$res[$i]['name'] = $case_func($f->name );
$res[$i]['type'] = $f->type;
$res[$i]['len'] = $f->max_length;
$res[$i]['flags'] = $this->_sybase_field_flags (
$res[$i]['table'], $res[$i]['name']);
$res['order'][$res[$i]['name']] = $i;
$res['ordertable'][$res[$i]['table']][$res[$i]['name']] = $i;
// free the result only if we were called on a table
@sybase_free_result ($id);
// {{{ _sybase_field_flags()
* Get the flags for a field.
* + <samp>unique_key</samp> (unique index, unique check or primary_key)
* + <samp>multiple_key</samp> (multi-key index)
* @param string $table table name
* @param string $column field name
* @return string space delimited string of flags. Empty string if none.
function _sybase_field_flags ($table, $column)
static $tableName = null;
if ($table != $tableName) {
// get unique/primary keys
$res = $this->getAll (" sp_helpindex $table" , DB_FETCHMODE_ASSOC );
if (!isset ($res[0 ]['index_description'])) {
foreach ($keys as $key) {
$this->_add_flag ($flags[$key], 'multiple_key');
if (strpos($val['index_description'], 'unique')) {
foreach ($keys as $key) {
$this->_add_flag ($flags[$key], 'unique_key');
if (array_key_exists ($column, $flags)) {
return(implode (' ', $flags[$column]));
* Adds a string to the flags array if the flag is not yet in there
* - if there is no flag present the array is created.
* @param array $array reference of flags array to add a value to
* @param mixed $value value to add to the flag array
function _add_flag (&$array, $value)
* Quote a string so it can be safely used as a table / column name
* Quoting style depends on which database driver is being used.
* @param string $str identifier name to be quoted
* @return string quoted identifier string
Documentation generated on Mon, 11 Mar 2019 10:14:55 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|