|
From: Benjamin C. <bc...@us...> - 2002-09-13 18:07:55
|
Update of /cvsroot/phpbt/phpbt/inc/pear/DB In directory usw-pr-cvs1:/tmp/cvs-serv10534/inc/pear/DB Added Files: common.php dbase.php fbsql.php ibase.php ifx.php msql.php mssql.php mysql.php oci8.php odbc.php pgsql.php storage.php sybase.php Log Message: Adding pear from php 4.2.3 distribution. This will help solve one of the primary installation problems of not being able to load pear. --- NEW FILE: common.php --- <?php // // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2002 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 | // | li...@ph... so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Author: Stig Bakken <ss...@fa...> | // +----------------------------------------------------------------------+ // // $Id: common.php,v 1.1 2002/09/13 18:07:51 bcurtis Exp $ [...1243 lines suppressed...] } // }}} } // Used by many drivers if (!function_exists('array_change_key_case')) { define('CASE_UPPER', 1); define('CASE_LOWER', 0); function &array_change_key_case(&$array, $case) { $casefunc = ($case == CASE_LOWER) ? 'strtolower' : 'strtoupper'; $ret = array(); foreach ($array as $key => $value) { $ret[$casefunc($key)] = $value; } return $ret; } } ?> --- NEW FILE: dbase.php --- <?php // // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2002 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 | // | li...@ph... so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Author: Tomas V.V.Cox <co...@id...> | // +----------------------------------------------------------------------+ // // $Id: dbase.php,v 1.1 2002/09/13 18:07:51 bcurtis Exp $ // // Database independent query interface definition for PHP's dbase // extension. // // XXX legend: // You have to compile your PHP with the --enable-dbase option // require_once "DB/common.php"; class DB_dbase extends DB_common { // {{{ properties var $connection; var $phptype, $dbsyntax; var $prepare_tokens = array(); var $prepare_types = array(); var $transaction_opcount = 0; var $res_row = array(); var $result = 0; // }}} // {{{ constructor /** * DB_mysql constructor. * * @access public */ function DB_dbase() { $this->DB_common(); $this->phptype = 'dbase'; $this->dbsyntax = 'dbase'; $this->features = array( 'prepare' => false, 'pconnect' => false, 'transactions' => false, 'limit' => false ); $this->errorcode_map = array(); } function connect($dsninfo, $persistent = false) { if (!DB::assertExtension('dbase')) { return $this->raiseError(DB_ERROR_EXTENSION_NOT_FOUND); } $this->dsn = $dsninfo; ob_start(); $conn = dbase_open($dsninfo['database'], 0); $error = ob_get_contents(); ob_end_clean(); if (!$conn) { return $this->raiseError(DB_ERROR_CONNECT_FAILED, null, null, null, strip_tags($error)); } $this->connection = $conn; return DB_OK; } function disconnect() { $ret = dbase_close($this->connection); $this->connection = null; return $ret; } function &query($query = null) { // emulate result resources $this->res_row[$this->result] = 0; return new DB_result($this, $this->result++); } function fetchInto($res, &$row, $fetchmode, $rownum = null) { if ($rownum === null) { $rownum = $this->res_row[$res]++; } if ($fetchmode & DB_FETCHMODE_ASSOC) { $row = @dbase_get_record_with_names($this->connection, $rownum); } else { $row = @dbase_get_record($this->connection, $rownum); } if (!$row) { return null; } return DB_OK; } function numCols($foo) { return dbase_numfields($this->connection); } function numRows($foo) { return dbase_numrecords($this->connection); } } ?> --- NEW FILE: fbsql.php --- <?php // // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2002 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 | // | li...@ph... so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Author: Frank M. Kromann <fr...@fr...> | // +----------------------------------------------------------------------+ // // $Id: fbsql.php,v 1.1 2002/09/13 18:07:51 bcurtis Exp $ // // Database independent query interface definition for PHP's FrontBase // extension. // // // XXX legend: // // XXX ERRORMSG: The error message from the fbsql function should // be registered here. // require_once "DB/common.php"; class DB_fbsql extends DB_common { // {{{ properties var $connection; var $phptype, $dbsyntax; var $prepare_tokens = array(); var $prepare_types = array(); var $num_rows = array(); var $fetchmode = DB_FETCHMODE_ORDERED; /* Default fetch mode */ // }}} // {{{ constructor /** * DB_fbsql constructor. * * @access public */ function DB_fbsql() { $this->DB_common(); $this->phptype = 'fbsql'; $this->dbsyntax = 'fbsql'; $this->features = array( 'prepare' => false, 'pconnect' => true, 'transactions' => true, 'limit' => 'emulate' ); $this->errorcode_map = array( 1004 => DB_ERROR_CANNOT_CREATE, 1005 => DB_ERROR_CANNOT_CREATE, 1006 => DB_ERROR_CANNOT_CREATE, 1007 => DB_ERROR_ALREADY_EXISTS, 1008 => DB_ERROR_CANNOT_DROP, 1046 => DB_ERROR_NODBSELECTED, 1050 => DB_ERROR_ALREADY_EXISTS, 1051 => DB_ERROR_NOSUCHTABLE, 1054 => DB_ERROR_NOSUCHFIELD, 1062 => DB_ERROR_ALREADY_EXISTS, 1064 => DB_ERROR_SYNTAX, 1100 => DB_ERROR_NOT_LOCKED, 1136 => DB_ERROR_VALUE_COUNT_ON_ROW, 1146 => DB_ERROR_NOSUCHTABLE, ); } // }}} // {{{ connect() /** * 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 * be persistent * @access public * @return int DB_OK on success, a DB error on failure */ function connect($dsninfo, $persistent = false) { if (!DB::assertExtension('fbsql')) return $this->raiseError(DB_ERROR_EXTENSION_NOT_FOUND); $this->dsn = $dsninfo; $dbhost = $dsninfo['hostspec'] ? $dsninfo['hostspec'] : 'localhost'; $user = $dsninfo['username']; $pw = $dsninfo['password']; $connect_function = $persistent ? 'fbsql_pconnect' : 'fbsql_connect'; ini_set('track_errors', true); if ($dbhost && $user && $pw) { $conn = @$connect_function($dbhost, $user, $pw); } elseif ($dbhost && $user) { $conn = @$connect_function($dbhost, $user); } elseif ($dbhost) { $conn = @$connect_function($dbhost); } else { $conn = false; } ini_restore("track_errors"); if (empty($conn)) { if (empty($php_errormsg)) { return $this->raiseError(DB_ERROR_CONNECT_FAILED); } else { return $this->raiseError(DB_ERROR_CONNECT_FAILED, null, null, null, $php_errormsg); } } if ($dsninfo['database']) { if (!fbsql_select_db($dsninfo['database'], $conn)) { return $this->fbsqlRaiseError(); } } $this->connection = $conn; return DB_OK; } // }}} // {{{ disconnect() /** * Log out and disconnect from the database. * * @access public * * @return bool TRUE on success, FALSE if not connected. */ function disconnect() { $ret = fbsql_close($this->connection); $this->connection = null; return $ret; } // }}} // {{{ simpleQuery() /** * Send a query to fbsql and return the results as a fbsql resource * identifier. * * @param the SQL query * * @access public * * @return mixed returns a valid fbsql result for successful SELECT * queries, DB_OK for other successful queries. A DB error is * returned on failure. */ function simpleQuery($query) { $this->last_query = $query; $query = $this->modifyQuery($query); $result = @fbsql_query("$query;", $this->connection); if (!$result) { return $this->fbsqlRaiseError(); } // Determine which queries that should return data, and which // should return an error code only. if (DB::isManip($query)) { return DB_OK; } $numrows = $this->numrows($result); if (is_object($numrows)) { return $numrows; } $this->num_rows[$result] = $numrows; return $result; } // }}} // {{{ nextResult() /** * Move the internal fbsql result pointer to the next available result * * @param a valid fbsql result resource * * @access public * * @return true if a result is available otherwise return false */ function nextResult($result) { return @fbsql_next_result($result); } // }}} // {{{ fetchRow() /** * Fetch and return a row of data (it uses fetchInto for that) * @param $result fbsql result identifier * @param $fetchmode format of fetched row array * @param $rownum the absolute row number to fetch * * @return array a row of data, or false on error */ function fetchRow($result, $fetchmode = DB_FETCHMODE_DEFAULT, $rownum=null) { if ($fetchmode == DB_FETCHMODE_DEFAULT) { $fetchmode = $this->fetchmode; } $res = $this->fetchInto ($result, $arr, $fetchmode, $rownum); if ($res !== DB_OK) { return $res; } return $arr; } // }}} // {{{ fetchInto() /** * Fetch a row and insert the data into an existing array. * * @param $result fbsql result identifier * @param $arr (reference) array where data from the row is stored * @param $fetchmode how the array data should be indexed * @param $rownum the row number to fetch * @access public * * @return int DB_OK on success, a DB error on failure */ function fetchInto($result, &$arr, $fetchmode, $rownum=null) { if ($rownum !== null) { if (!@fbsql_data_seek($result, $rownum)) { return null; } } if ($fetchmode & DB_FETCHMODE_ASSOC) { $arr = @fbsql_fetch_array($result, FBSQL_ASSOC); } else { $arr = @fbsql_fetch_row($result); } if (!$arr) { $errno = @fbsql_errno($this->connection); if (!$errno) { return NULL; } return $this->fbsqlRaiseError($errno); } return DB_OK; } // }}} // {{{ freeResult() /** * Free the internal resources associated with $result. * * @param $result fbsql result identifier or DB statement identifier * * @access public * * @return bool TRUE on success, FALSE if $result is invalid */ function freeResult($result) { if (is_resource($result)) { return fbsql_free_result($result); } if (!isset($this->prepare_tokens[(int)$result])) { return false; } unset($this->prepare_tokens[(int)$result]); unset($this->prepare_types[(int)$result]); return true; } // }}} // {{{ autoCommit() function autoCommit($onoff=false) { if ($onoff) { $this->query("SET COMMIT TRUE"); } else { $this->query("SET COMMIT FALSE"); } } // }}} // {{{ commit() function commit() { fbsql_commit(); } // }}} // {{{ rollback() function rollback() { fbsql_rollback(); } // }}} // {{{ numCols() /** * Get the number of columns in a result set. * * @param $result fbsql result identifier * * @access public * * @return int the number of columns per row in $result */ function numCols($result) { $cols = @fbsql_num_fields($result); if (!$cols) { return $this->fbsqlRaiseError(); } return $cols; } // }}} // {{{ numRows() /** * Get the number of rows in a result set. * * @param $result fbsql result identifier * * @access public * * @return int the number of rows in $result */ function numRows($result) { $rows = @fbsql_num_rows($result); if ($rows === null) { return $this->fbsqlRaiseError(); } return $rows; } // }}} // {{{ affectedRows() /** * 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 */ function affectedRows() { if (DB::isManip($this->last_query)) { $result = @fbsql_affected_rows($this->connection); } else { $result = 0; } return $result; } // }}} // {{{ errorNative() /** * Get the native error code of the last error (if any) that * occured on the current connection. * * @access public * * @return int native fbsql error code */ function errorNative() { return fbsql_errno($this->connection); } // }}} // {{{ nextId() /** * Get the next value in a sequence. We emulate sequences * for fbsql. Will create the sequence if it does not exist. * * @access public * * @param $seq_name the name of the sequence * * @param $ondemand whether to create the sequence table on demand * (default is true) * * @return a sequence integer, or a DB error */ function nextId($seq_name, $ondemand = true) { $sqn = preg_replace('/[^a-z0-9_]/i', '_', $seq_name); $repeat = 0; do { $seqname = sprintf($this->getOption("seqname_format"), $sqn); $result = $this->query("INSERT INTO ${seqname} VALUES(NULL)"); if ($ondemand && DB::isError($result) && $result->getCode() == DB_ERROR_NOSUCHTABLE) { $repeat = 1; $result = $this->createSequence($seq_name); if (DB::isError($result)) { return $result; } } else { $repeat = 0; } } while ($repeat); if (DB::isError($result)) { return $result; } return fbsql_insert_id($this->connection); } // }}} // {{{ createSequence() function createSequence($seq_name) { $sqn = preg_replace('/[^a-z0-9_]/i', '_', $seq_name); $seqname = sprintf($this->getOption("seqname_format"), $sqn); return $this->query("CREATE TABLE ${seqname} ". '(id INTEGER UNSIGNED AUTO_INCREMENT NOT NULL,'. ' PRIMARY KEY(id))'); } // }}} // {{{ dropSequence() function dropSequence($seq_name) { $sqn = preg_replace('/[^a-z0-9_]/i', '_', $seq_name); $seqname = sprintf($this->getOption("seqname_format"), $sqn); return $this->query("DROP TABLE ${seqname} RESTRICT"); } // }}} // {{{ modifyQuery() function modifyQuery($query) { if ($this->options['optimize'] == 'portability') { // "DELETE FROM table" gives 0 affected rows in fbsql. // This little hack lets you know how many rows were deleted. if (preg_match('/^\s*DELETE\s+FROM\s+(\S+)\s*$/i', $query)) { $query = preg_replace('/^\s*DELETE\s+FROM\s+(\S+)\s*$/', 'DELETE FROM \1 WHERE 1=1', $query); } } return $query; } // }}} // {{{ fbsqlRaiseError() function fbsqlRaiseError($errno = null) { if ($errno === null) { $errno = $this->errorCode(fbsql_errno($this->connection)); } return $this->raiseError($errno, null, null, null, fbsql_error($this->connection)); } // }}} // {{{ tableInfo() function tableInfo($result, $mode = null) { $count = 0; $id = 0; $res = array(); /* * depending on $mode, metadata returns the following values: * * - mode is false (default): * $result[]: * [0]["table"] table name * [0]["name"] field name * [0]["type"] field type * [0]["len"] field length * [0]["flags"] field flags * * - mode is DB_TABLEINFO_ORDER * $result[]: * ["num_fields"] number of metadata records * [0]["table"] table name * [0]["name"] field name * [0]["type"] field type * [0]["len"] field length * [0]["flags"] field flags * ["order"][field name] index of field named "field name" * The last one is used, if you have a field name, but no index. * Test: if (isset($result['meta']['myfield'])) { ... * * - mode is DB_TABLEINFO_ORDERTABLE * the same as above. but additionally * ["ordertable"][table name][field name] index of field * named "field name" * * this is, because if you have fields from different * tables with the same field name * they override each * other with DB_TABLEINFO_ORDER * * you can combine DB_TABLEINFO_ORDER and * DB_TABLEINFO_ORDERTABLE with DB_TABLEINFO_ORDER | * DB_TABLEINFO_ORDERTABLE * or with DB_TABLEINFO_FULL */ // if $result is a string, then we want information about a // table without a resultset if (is_string($result)) { $id = @fbsql_list_fields($this->dsn['database'], $result, $this->connection); if (empty($id)) { return $this->fbsqlRaiseError(); } } else { // else we want information about a resultset $id = $result; if (empty($id)) { return $this->fbsqlRaiseError(); } } $count = @fbsql_num_fields($id); // made this IF due to performance (one if is faster than $count if's) if (empty($mode)) { for ($i=0; $i<$count; $i++) { $res[$i]['table'] = @fbsql_field_table ($id, $i); $res[$i]['name'] = @fbsql_field_name ($id, $i); $res[$i]['type'] = @fbsql_field_type ($id, $i); $res[$i]['len'] = @fbsql_field_len ($id, $i); $res[$i]['flags'] = @fbsql_field_flags ($id, $i); } } else { // full $res["num_fields"]= $count; for ($i=0; $i<$count; $i++) { $res[$i]['table'] = @fbsql_field_table ($id, $i); $res[$i]['name'] = @fbsql_field_name ($id, $i); $res[$i]['type'] = @fbsql_field_type ($id, $i); $res[$i]['len'] = @fbsql_field_len ($id, $i); $res[$i]['flags'] = @fbsql_field_flags ($id, $i); if ($mode & DB_TABLEINFO_ORDER) { $res['order'][$res[$i]['name']] = $i; } if ($mode & DB_TABLEINFO_ORDERTABLE) { $res['ordertable'][$res[$i]['table']][$res[$i]['name']] = $i; } } } // free the result only if we were called on a table if (is_string($result)) { @fbsql_free_result($id); } return $res; } // }}} // {{{ getSpecialQuery() /** * 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 */ function getSpecialQuery($type) { switch ($type) { case 'tables': $sql = 'select "table_name" from information_schema.tables'; break; default: return null; } return $sql; } // }}} } // TODO/wishlist: // longReadlen // binmode ?> --- NEW FILE: ibase.php --- <?php // // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2002 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 | // | li...@ph... so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Author: Sterling Hughes <ste...@ph...> | // +----------------------------------------------------------------------+ // // $Id: ibase.php,v 1.1 2002/09/13 18:07:51 bcurtis Exp $ // // Database independent query interface definition for PHP's Interbase // extension. // require_once 'DB/common.php'; class DB_ibase extends DB_common { var $connection; var $phptype, $dbsyntax; var $autocommit = 1; var $manip_query = array(); function DB_ibase() { $this->DB_common(); $this->phptype = 'ibase'; $this->dbsyntax = 'ibase'; $this->features = array( 'prepare' => true, 'pconnect' => true, 'transactions' => true, 'limit' => false ); // just a few of the tons of Interbase error codes listed in the // Language Reference section of the Interbase manual $this->errorcode_map = array( -104 => DB_ERROR_SYNTAX, -150 => DB_ERROR_ACCESS_VIOLATION, -151 => DB_ERROR_ACCESS_VIOLATION, -155 => DB_ERROR_NOSUCHTABLE, -157 => DB_ERROR_NOSUCHFIELD, -158 => DB_ERROR_VALUE_COUNT_ON_ROW, -170 => DB_ERROR_MISMATCH, -171 => DB_ERROR_MISMATCH, -172 => DB_ERROR_INVALID, -204 => DB_ERROR_INVALID, -205 => DB_ERROR_NOSUCHFIELD, -206 => DB_ERROR_NOSUCHFIELD, -208 => DB_ERROR_INVALID, -219 => DB_ERROR_NOSUCHTABLE, -297 => DB_ERROR_CONSTRAINT, -530 => DB_ERROR_CONSTRAINT, -803 => DB_ERROR_CONSTRAINT, -551 => DB_ERROR_ACCESS_VIOLATION, -552 => DB_ERROR_ACCESS_VIOLATION, -922 => DB_ERROR_NOSUCHDB, -923 => DB_ERROR_CONNECT_FAILED, -924 => DB_ERROR_CONNECT_FAILED ); } function connect($dsninfo, $persistent = false) { if (!DB::assertExtension('interbase')) { return $this->raiseError(DB_ERROR_EXTENSION_NOT_FOUND); } $this->dsn = $dsninfo; $user = $dsninfo['username']; $pw = $dsninfo['password']; $dbhost = $dsninfo['hostspec'] ? ($dsninfo['hostspec'] . ':/' . $dsninfo['database']) : $dsninfo['database']; $connect_function = $persistent ? 'ibase_pconnect' : 'ibase_connect'; $params = array(); $params[] = $dbhost; $params[] = !empty($user) ? $user : null; $params[] = !empty($pw) ? $pw : null; $params[] = isset($dsninfo['charset']) ? $dsninfo['charset'] : null; $params[] = isset($dsninfo['buffers']) ? $dsninfo['buffers'] : null; $params[] = isset($dsninfo['dialect']) ? $dsninfo['dialect'] : null; $params[] = isset($dsninfo['role']) ? $dsninfo['role'] : null; /* if ($dbhost && $user && $pw) { $conn = $connect_function($dbhost, $user, $pw); } elseif ($dbhost && $user) { $conn = $connect_function($dbhost, $user); } elseif ($dbhost) { $conn = $connect_function($dbhost); } else { return $this->raiseError("no host, user or password"); } */ $conn = @call_user_func_array($connect_function, $params); if (!$conn) { return $this->ibaseRaiseError(DB_ERROR_CONNECT_FAILED); } $this->connection = $conn; return DB_OK; } function disconnect() { $ret = @ibase_close($this->connection); $this->connection = null; return $ret; } function simpleQuery($query) { $ismanip = DB::isManip($query); $this->last_query = $query; $query = $this->modifyQuery($query); $result = @ibase_query($this->connection, $query); if (!$result) { return $this->ibaseRaiseError(); } if ($this->autocommit && $ismanip) { ibase_commit($this->connection); } // Determine which queries that should return data, and which // should return an error code only. return DB::isManip($query) ? DB_OK : $result; } // {{{ modifyLimitQuery() /** * This method is used by backends to alter limited queries * Uses the new FIRST n SKIP n Firebird 1.0 syntax, so it is * only compatible with Firebird 1.x * * @param string $query query to modify * @param integer $from the row to start to fetching * @param integer $count the numbers of rows to fetch * * @return the new (modified) query * @author Ludovico Magnocavallo <lu...@su...> * @access private */ function modifyLimitQuery($query, $from, $count) { if ($this->dsn['dbsyntax'] == 'firebird') { $from++; // SKIP starts from 1, ie SKIP 1 starts from the first record $query = preg_replace('/^\s*select\s(.*)$/is', "SELECT FIRST $count SKIP $from $1", $query); } return $query; } // }}} // {{{ nextResult() /** * Move the internal ibase result pointer to the next available result * * @param a valid fbsql result resource * * @access public * * @return true if a result is available otherwise return false */ function nextResult($result) { return false; } // }}} function fetchInto($result, &$ar, $fetchmode, $rownum = null) { if ($rownum !== NULL) { return $this->ibaseRaiseError(DB_ERROR_NOT_CAPABLE); } if ($fetchmode & DB_FETCHMODE_ASSOC) { $ar = get_object_vars(ibase_fetch_object($result)); if ($ar && $this->options['optimize'] == 'portability') { $ar = array_change_key_case($ar, CASE_LOWER); } } else { $ar = ibase_fetch_row($result); } if (!$ar) { if ($errmsg = ibase_errmsg()) { return $this->ibaseRaiseError(null, $errmsg); } else { return null; } } return DB_OK; } function freeResult() { if (is_resource($result)) { return ibase_free_result($result); } if (!isset($this->prepare_tokens[(int)$result])) { return false; } unset($this->prepare_tokens[(int)$result]); unset($this->prepare_types[(int)$result]); return true; } function freeQuery($query) { ibase_free_query($query); return true; } function numCols($result) { $cols = ibase_num_fields($result); if (!$cols) { return $this->ibaseRaiseError(); } return $cols; } function prepare($query) { $this->last_query = $query; $query = $this->modifyQuery($query); $stmt = ibase_prepare($query); $this->manip_query[(int)$stmt] = DB::isManip($query); return $stmt; } function execute($stmt, $data = false) { $result = ibase_execute($stmt, $data); if (!$result) { return $this->ibaseRaiseError(); } if ($this->autocommit) { ibase_commit($this->connection); } return DB::isManip($this->manip_query[(int)$stmt]) ? DB_OK : new DB_result($this, $result); } function autoCommit($onoff = false) { $this->autocommit = $onoff ? 1 : 0; return DB_OK; } function commit() { return ibase_commit($this->connection); } function rollback($trans_number) { return ibase_rollback($this->connection, $trans_number); } function transactionInit($trans_args = 0) { return $trans_args ? ibase_trans($trans_args, $this->connection) : ibase_trans(); } // {{{ nextId() /** * Get the next value in a sequence. * * If the sequence does not exist, it will be created, * unless $ondemand is false. * * @access public * @param string $seq_name the name of the sequence * @param bool $ondemand whether to create the sequence on demand * @return a sequence integer, or a DB error */ function nextId($seq_name, $ondemand = true) { $sqn = strtoupper(preg_replace('/[^a-z0-9_]/i', '_', $seq_name)); $repeat = 0; do { $this->pushErrorHandling(PEAR_ERROR_RETURN); $result = $this->query("SELECT GEN_ID(${sqn}_SEQ, 1) FROM RDB\$GENERATORS" ." WHERE RDB\$GENERATOR_NAME='${sqn}_SEQ'"); $this->popErrorHandling(); if ($ondemand && DB::isError($result)) { $repeat = 1; $result = $this->createSequence($seq_name); if (DB::isError($result)) { return $result; } } else { $repeat = 0; } } while ($repeat); if (DB::isError($result)) { return $result; } $arr = $result->fetchRow(DB_FETCHMODE_ORDERED); $result->free(); return $arr[0]; } // }}} // {{{ createSequence() /** * Create the sequence * * @param string $seq_name the name of the sequence * @return mixed DB_OK on success or DB error on error * @access public */ function createSequence($seq_name) { $sqn = strtoupper(preg_replace('/[^a-z0-9_]/i', '_', $seq_name)); $this->pushErrorHandling(PEAR_ERROR_RETURN); $result = $this->query("CREATE GENERATOR ${sqn}_SEQ"); $this->popErrorHandling(); return $result; } // }}} // {{{ dropSequence() /** * Drop a sequence * * @param string $seq_name the name of the sequence * @return mixed DB_OK on success or DB error on error * @access public */ function dropSequence($seq_name) { $sqn = strtoupper(preg_replace('/[^a-z0-9_]/i', '_', $seq_name)); return $this->query("DELETE FROM RDB\$GENERATORS WHERE RDB\$GENERATOR_NAME='${sqn}_SEQ'"); } // }}} // {{{ _ibaseFieldFlags() /** * get the Flags of a Field * * @param string $field_name the name of the field * @param string $table_name the name of the table * * @return string The flags of the field ("primary_key", "unique_key", "not_null" * "default", "computed" and "blob" are supported) * @access private */ function _ibaseFieldFlags($field_name, $table_name) { $sql = 'SELECT R.RDB$CONSTRAINT_TYPE CTYPE' .' FROM RDB$INDEX_SEGMENTS I' .' JOIN RDB$RELATION_CONSTRAINTS R ON I.RDB$INDEX_NAME=R.RDB$INDEX_NAME' .' WHERE I.RDB$FIELD_NAME=\''.$field_name.'\'' .' AND R.RDB$RELATION_NAME=\''.$table_name.'\''; $result = ibase_query($this->connection, $sql); if (empty($result)) { return $this->ibaseRaiseError(); } if ($obj = @ibase_fetch_object($result)) { ibase_free_result($result); if (isset($obj->CTYPE) && trim($obj->CTYPE) == 'PRIMARY KEY') { $flags = 'primary_key '; } if (isset($obj->CTYPE) && trim($obj->CTYPE) == 'UNIQUE') { $flags .= 'unique_key '; } } $sql = 'SELECT R.RDB$NULL_FLAG AS NFLAG,' .' R.RDB$DEFAULT_SOURCE AS DSOURCE,' .' F.RDB$FIELD_TYPE AS FTYPE,' .' F.RDB$COMPUTED_SOURCE AS CSOURCE' .' FROM RDB$RELATION_FIELDS R ' .' JOIN RDB$FIELDS F ON R.RDB$FIELD_SOURCE=F.RDB$FIELD_NAME' .' WHERE R.RDB$RELATION_NAME=\''.$table_name.'\'' .' AND R.RDB$FIELD_NAME=\''.$field_name.'\''; $result = ibase_query($this->connection, $sql); if (empty($result)) { return $this->ibaseRaiseError(); } if ($obj = @ibase_fetch_object($result)) { ibase_free_result($result); if (isset($obj->NFLAG)) { $flags .= 'not_null '; } if (isset($obj->DSOURCE)) { $flags .= 'default '; } if (isset($obj->CSOURCE)) { $flags .= 'computed '; } if (isset($obj->FTYPE) && $obj->FTYPE == 261) { $flags .= 'blob '; } } return trim($flags); } // }}} // {{{ tableInfo() /** * Returns information about a table or a result set * * NOTE: doesn't support 'flags'and 'table' if called from a db_result * * @param mixed $resource Interbase result identifier or table name * @param int $mode A valid tableInfo mode (DB_TABLEINFO_ORDERTABLE or * DB_TABLEINFO_ORDER) * * @return array An array with all the information */ function tableInfo($result, $mode = null) { $count = 0; $id = 0; $res = array(); /* * depending on $mode, metadata returns the following values: * * - mode is false (default): * $result[]: * [0]["table"] table name * [0]["name"] field name * [0]["type"] field type * [0]["len"] field length * [0]["flags"] field flags * * - mode is DB_TABLEINFO_ORDER * $result[]: * ["num_fields"] number of metadata records * [0]["table"] table name * [0]["name"] field name * [0]["type"] field type * [0]["len"] field length * [0]["flags"] field flags * ["order"][field name] index of field named "field name" * The last one is used, if you have a field name, but no index. * Test: if (isset($result['meta']['myfield'])) { ... * * - mode is DB_TABLEINFO_ORDERTABLE * the same as above. but additionally * ["ordertable"][table name][field name] index of field * named "field name" * * this is, because if you have fields from different * tables with the same field name * they override each * other with DB_TABLEINFO_ORDER * * you can combine DB_TABLEINFO_ORDER and * DB_TABLEINFO_ORDERTABLE with DB_TABLEINFO_ORDER | * DB_TABLEINFO_ORDERTABLE * or with DB_TABLEINFO_FULL */ // if $result is a string, then we want information about a // table without a resultset if (is_string($result)) { $id = ibase_query($this->connection,"SELECT * FROM $result"); if (empty($id)) { return $this->ibaseRaiseError(); } } else { // else we want information about a resultset $id = $result; if (empty($id)) { return $this->ibaseRaiseError(); } } $count = @ibase_num_fields($id); // made this IF due to performance (one if is faster than $count if's) if (empty($mode)) { for ($i=0; $i<$count; $i++) { $info = @ibase_field_info($id, $i); $res[$i]['table'] = (is_string($result)) ? $result : ''; $res[$i]['name'] = $info['name']; $res[$i]['type'] = $info['type']; $res[$i]['len'] = $info['length']; $res[$i]['flags'] = (is_string($result)) ? $this->_ibaseFieldFlags($info['name'], $result) : ''; } } else { // full $res["num_fields"]= $count; for ($i=0; $i<$count; $i++) { $info = @ibase_field_info($id, $i); $res[$i]['table'] = (is_string($result)) ? $result : ''; $res[$i]['name'] = $info['name']; $res[$i]['type'] = $info['type']; $res[$i]['len'] = $info['length']; $res[$i]['flags'] = (is_string($result)) ? $this->_ibaseFieldFlags($info['name'], $result) : ''; if ($mode & DB_TABLEINFO_ORDER) { $res['order'][$res[$i]['name']] = $i; } if ($mode & DB_TABLEINFO_ORDERTABLE) { $res['ordertable'][$res[$i]['table']][$res[$i]['name']] = $i; } } } // free the result only if we were called on a table if (is_resource($id)) { ibase_free_result($id); } return $res; } // }}} // {{{ getSpecialQuery() /** * 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 */ function getSpecialQuery($type) { switch ($type) { case 'tables': default: return null; } return $sql; } // }}} // {{{ ibaseRaiseError() function ibaseRaiseError($errno = null, $errmsg = null) { if ($errmsg === null) $errmsg = ibase_errmsg(); // memo for the interbase php module hackers: we need something similar // to mysql_errno() to retrieve error codes instead of this ugly hack if (preg_match('/^([^0-9\-]+)([0-9\-]+)\s+(.*)$/', $errmsg, $m)) { if ($errno === null) { $ibase_errno = (int)$m[2]; // try to interpret Interbase error code (that's why we need ibase_errno() // in the interbase module to return the real error code) switch ($ibase_errno) { case -204: if (is_int(strpos($m[3], 'Table unknown'))) { $errno = DB_ERROR_NOSUCHTABLE; } break; default: $errno = $this->errorCode($ibase_errno); } } $errmsg = $m[2] . ' ' . $m[3]; } return $this->raiseError($errno, null, null, $errmsg, $this->last_query); } // }}} } /* * Local variables: * tab-width: 4 * c-basic-offset: 4 * End: */ ?> --- NEW FILE: ifx.php --- <?php // // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2002 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 | // | li...@ph... so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Author: Tomas V.V.Cox <co...@id...> | // +----------------------------------------------------------------------+ // // $Id: ifx.php,v 1.1 2002/09/13 18:07:51 bcurtis Exp $ // // Database independent query interface definition for PHP's Informix // extension. // // Legend: // For more info on Informix errors see: // http://www.informix.com/answers/english/ierrors.htm // // TODO: // - set needed env Informix vars on connect // - implement native prepare/execute require_once 'DB/common.php'; class DB_ifx extends DB_common { var $connection; var $affected = 0; var $dsn = array(); var $fetchmode = DB_FETCHMODE_ORDERED; /* Default fetch mode */ function DB_ifx() { $this->phptype = 'ifx'; $this->dbsyntax = 'ifx'; $this->features = array( 'prepare' => false, 'pconnect' => true, 'transactions' => false, 'limit' => 'emulate' ); $this->errorcode_map = array( '-201' => DB_ERROR_SYNTAX, '-206' => DB_ERROR_NOSUCHTABLE, '-217' => DB_ERROR_NOSUCHFIELD, '-329' => DB_ERROR_NODBSELECTED, '-1204' => DB_ERROR_INVALID_DATE, '-1205' => DB_ERROR_INVALID_DATE, '-1206' => DB_ERROR_INVALID_DATE, '-1209' => DB_ERROR_INVALID_DATE, '-1210' => DB_ERROR_INVALID_DATE, '-1212' => DB_ERROR_INVALID_DATE ); } /** * 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 * be persistent * * @return int DB_OK on success, a DB error code on failure */ function connect(&$dsninfo, $persistent = false) { if (!DB::assertExtension('informix')) return $this->raiseError(DB_ERROR_EXTENSION_NOT_FOUND); $this->dsn = $dsninfo; $dbhost = $dsninfo['hostspec'] ? '@' . $dsninfo['hostspec'] : ''; $dbname = $dsninfo['database'] ? $dsninfo['database'] . $dbhost : ''; $user = $dsninfo['username'] ? $dsninfo['username'] : ''; $pw = $dsninfo['password'] ? $dsninfo['password'] : ''; $connect_function = $persistent ? 'ifx_pconnect' : 'ifx_connect'; $this->connection = @$connect_function($dbname, $user, $pw); if (!is_resource($this->connection)) { return $this->ifxraiseError(DB_ERROR_CONNECT_FAILED); } return DB_OK; } /** * Log out and disconnect from the database. * * @return bool TRUE on success, FALSE if not connected. */ function disconnect() { $ret = @ifx_close($this->connection); $this->connection = null; return $ret; } /** * Send a query to Informix and return the results as a * Informix resource identifier. * * @param $query the SQL query * * @return int returns a valid Informix result for successful SELECT * queries, DB_OK for other successful queries. A DB error code * is returned on failure. */ function simpleQuery($query) { $this->last_query = $query; if (preg_match('/(SELECT)/i', $query)) { //TESTME: Use !DB::isManip()? // the scroll is needed for fetching absolute row numbers // in a select query result $result = @ifx_query($query, $this->connection, IFX_SCROLL); } else { $result = @ifx_query($query, $this->connection); } if (!$result) { return $this->ifxraiseError(); } $this->affected = ifx_affected_rows ($result); // Determine which queries that should return data, and which // should return an error code only. if (preg_match('/(SELECT)/i', $query)) { return $result; } return DB_OK; } // {{{ nextResult() /** * Move the internal ifx result pointer to the next available result * * @param a valid fbsql result resource * * @access public * * @return true if a result is available otherwise return false */ function nextResult($result) { return false; } // }}} /** * Gets the number of rows affected by the last query. * if the last query was a select, returns an _estimate_ value. * * @return number of rows affected by the last query */ function affectedRows() { return $this->affected; } /** * Fetch and return a row of data (it uses fetchInto for that) * @param $result Informix result identifier * @param $fetchmode format of fetched row array * @param $rownum the absolute row number to fetch * * @return array a row of data, or false on error */ function fetchRow($result, $fetchmode = DB_FETCHMODE_DEFAULT, $rownum=null) { if ($fetchmode == DB_FETCHMODE_DEFAULT) { $fetchmode = $this->fetchmode; } $res = $this->fetchInto ($result, $arr, $fetchmode, $rownum); if ($res !== DB_OK) { return $res; } return $arr; } /** * Fetch a row and return as array. * * @param $result Informix result identifier * @param $row (reference) array where data from the row is stored * @param $fetchmode how the resulting array should be indexed * @param $rownum the row number to fetch * * @return int an array on success, a DB error code on failure, NULL * if there is no more data */ function fetchInto($result, &$row, $fetchmode, $rownum=null) { if (($rownum !== null) && ($rownum < 0)) { return null; } // if $rownum is null, fetch row will return the next row if (!$row = @ifx_fetch_row($result, $rownum)) { return null; } if ($fetchmode !== DB_FETCHMODE_ASSOC) { $i=0; $order = array(); foreach ($row as $key => $val) { $order[$i++] = $val; } $row = $order; } return DB_OK; } function numRows($result) { return $this->raiseError(DB_ERROR_NOT_CAPABLE); } /** * Get the number of columns in a result set. * * @param $result Informix result identifier * * @return int the number of columns per row in $result */ function numCols($result) { if (!$cols = @ifx_num_fields($result)) { return $this->ifxraiseError(); } return $cols; } /** * Free the internal resources associated with $result. * * @param $result Informix result identifier * * @return bool TRUE on success, DB_error on error */ function freeResult($result) { if (is_resource($result)) { if (!@ifx_free_result($result)) { return $this->ifxraiseError(); } return true; } if (!isset($this->prepare_tokens[(int)$result])) { return false; } unset($this->prepare_tokens[(int)$result]); unset($this->prepare_types[(int)$result]); return true; } function ifxraiseError($errno = null) { if ($errno === null) { $errno = $this->errorCode(ifx_error()); } return $this->raiseError($errno, null, null, null, $this->errorNative()); } /** * Map native error codes to DB's portable ones. Requires that * the DB implementation's constructor fills in the $errorcode_map * property. * * @return int a portable DB error code, or DB_ERROR if this DB * implementation has no mapping for the given error code. */ function errorCode($nativecode) { if (ereg('SQLCODE=(.*)]', $nativecode, $match)) { $code = $match[1]; if (isset($this->errorcode_map[$code])) { return $this->errorcode_map[$code]; } } return DB_ERROR; } /** * Get the native error message of the last error (if any) that * occured on the current connection. * * @return int native Informix error code */ function errorNative() { return ifx_error() . ' ' . ifx_errormsg(); } // {{{ getSpecialQuery() /** * 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 */ function getSpecialQuery($type) { switch ($type) { case 'tables': default: return null; } return $sql; } // }}} } ?> --- NEW FILE: msql.php --- <?php // // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2002 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 | // | li...@ph... so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Author: Sterling Hughes <ste...@ph...> | // +----------------------------------------------------------------------+ // // $Id: msql.php,v 1.1 2002/09/13 18:07:51 bcurtis Exp $ // // Database independent query interface definition for PHP's Mini-SQL // extension. // require_once 'DB/common.php'; class DB_msql extends DB_common { var $connection; var $phptype, $dbsyntax; var $prepare_tokens = array(); var $prepare_types = array(); function DB_msql() { $this->DB_common(); $this->phptype = 'msql'; $this->dbsyntax = 'msql'; $this->features = array( 'prepare' => false, 'pconnect' => true, 'transactions' => false, 'limit' => 'emulate' ); } function connect($dsninfo, $persistent = false) { if (!DB::assertExtension('msql')) return $this->raiseError(DB_ERROR_EXTENSION_NOT_FOUND); $this->dsn = $dsninfo; $user = $dsninfo['username']; $pw = $dsninfo['password']; $dbhost = $dsninfo['hostspec'] ? $dsninfo['hostspec'] : 'localhost'; $connect_function = $persistent ? 'msql_pconnect' : 'msql_connect'; if ($dbhost && $user && $pw) { $conn = $connect_function($dbhost, $user, $pw); } elseif ($dbhost && $user) { $conn = $connect_function($dbhost,$user); } else { $conn = $connect_function($dbhost); } if (!$conn) { $this->raiseError(DB_ERROR_CONNECT_FAILED); } if (!@msql_select_db($dsninfo['database'], $conn)){ return $this->raiseError(DB_ERROR_NODBSELECTED); } $this->connection = $conn; return DB_OK; } function disconnect() { $ret = @msql_close($this->connection); $this->connection = null; return $ret; } function simpleQuery($query) { $this->last_query = $query; $query = $this->modifyQuery($query); $result = @msql_query($query, $this->connection); if (!$result) { return $this->raiseError(); } // Determine which queries that should return data, and which // should return an error code only. return DB::isManip($query) ? DB_OK : $result; } // {{{ nextResult() /** * Move the internal msql result pointer to the next available result * * @param a valid fbsql result resource * * @access public * * @return true if a result is available otherwise return false */ function nextResult($result) { return false; } // }}} function fetchRow($result, $fetchmode = DB_FETCHMODE_DEFAULT, $rownum=null) { if ($fetchmode == DB_FETCHMODE_DEFAULT) { $fetchmode = $this->fetchmode; } $res = $this->fetchInto ($result, $arr, $fetchmode, $rownum); if ($res !== DB_OK) { return $res; } return $arr; } function fetchInto($result, &$ar, $fetchmode, $rownum=null) { if ($rownum !== null) { if (!@msql_data_seek($result, $rownum)) { return null; } } if ($fetchmode & DB_FETCHMODE_ASSOC) { $ar = @msql_fetch_array($result, MSQL_ASSOC); } else { $ar = @msql_fetch_row($result); } if (!$ar) { if ($error = msql_error()) { return $this->raiseError($error); } else { return null; } } return DB_OK; } function freeResult($result) { if (is_resource($result)) { return @msql_free_result($result); } if (!isset($this->prepare_tokens[$result])) { return false; } unset($this->prepare_tokens[$result]); unset($this->prepare_types[$result]); return true; } function numCols($result) { $cols = @msql_num_fields($result); if (!$cols) { return $this->raiseError(); } return $cols; } function numRows($result) { $rows = @msql_num_rows($result); if (!$rows) { return $this->raiseError(); } return $rows; } /** * Gets the number of rows affected by a query. * * @return number of rows affected by the last query */ function affectedRows() { return @msql_affected_rows($this->connection); } // {{{ getSpecialQuery() /** * 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 */ function getSpecialQuery($type) { switch ($type) { case 'tables': default: return null; } return $sql; } // }}} } ?> --- NEW FILE: mssql.php --- <?php // // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2002 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 | // | li...@ph... so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Author: Sterling Hughes <ste...@ph...> | // +----------------------------------------------------------------------+ // // $Id: mssql.php,v 1.1 2002/09/13 18:07:51 bcurtis Exp $ // // Database independent query interface definition for PHP's Microsoft SQL Server // extension. // require_once 'DB/common.php'; class DB_mssql extends DB_common { v... [truncated message content] |