cvs: php4 /pear/DB STATUS common.php ibase.php msql.php mssql.php mysql.php oci8.php odbc.php pgsql.php sybase.php

From: Date: Sun, 10 Dec 2000 02:07:00 +0000
Subject: cvs: php4 /pear/DB STATUS common.php ibase.php msql.php mssql.php mysql.php oci8.php odbc.php pgsql.php sybase.php
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-3049@lists.php.net to get a copy of this message
ssb Sat Dec 9 18:07:00 2000 EDT Modified files: /php4/pear/DB STATUS common.php ibase.php msql.php mssql.php mysql.php oci8.php odbc.php pgsql.php sybase.php Log: * make sure all DB backends call the DB_common constructor * removed more duplicated code that can use DB_common defaults (execute/prepare emulation, "not capable" versions of transaction functions and numRows). * implemented numRows for msql and mssql (untested)

Index: php4/pear/DB/STATUS diff -u php4/pear/DB/STATUS:1.9 php4/pear/DB/STATUS:1.10 --- php4/pear/DB/STATUS:1.9 Sat Dec 9 17:13:12 2000 +++ php4/pear/DB/STATUS Sat Dec 9 18:07:00 2000 @@ -14,7 +14,7 @@ FEATURE ibase msql mssql mysql oci8 odbc pgsql sybase simpleQuery x x x T x x T x numCols x x x T x - T x -numRows - - - T - - T - +numRows - x - T - - T - errorNative - - - T x - T - prepare/execute x e e E e e E e sequences - - - E - - - - Index: php4/pear/DB/common.php diff -u php4/pear/DB/common.php:1.30 php4/pear/DB/common.php:1.31 --- php4/pear/DB/common.php:1.30 Sat Dec 9 17:32:16 2000 +++ php4/pear/DB/common.php Sat Dec 9 18:07:00 2000 @@ -41,6 +41,7 @@ var $last_query = ''; var $fetchmode = DB_FETCHMODE_ORDERED; var $seq_table = "db_sequence"; + var $options = array(); // }}} // {{{ toString() @@ -294,8 +295,8 @@ $token = 0; $types = array(); - for ($i = 0; $i < strlen( $query ); $i++) { - switch ($query[ $i ]) { + for ($i = 0; $i < strlen($query); $i++) { + switch ($query[$i]) { case "?": $types[$token++] = DB_PARAM_SCALAR; break; @@ -315,6 +316,21 @@ } // }}} + // {{{ execute() + + function execute($stmt, $data = false) + { + $realquery = $this->executeEmulateQuery($stmt, $data); + $this->last_query = $realquery; + $result = $this->simpleQuery($realquery); + if (DB::isError($result) || $result === DB_OK) { + return $result; + } else { + return new DB_result($this, $result); + } + } + + // }}} // {{{ executeEmulateQuery() /** @@ -749,6 +765,38 @@ } return $results; + } + + // }}} + // {{{ autoCommit() + + function autoCommit($onoff=false) + { + return $this->raiseError(DB_ERROR_NOT_CAPABLE); + } + + // }}} + // {{{ commit() + + function commit() + { + return $this->raiseError(DB_ERROR_NOT_CAPABLE); + } + + // }}} + // {{{ rollback() + + function rollback() + { + return $this->raiseError(DB_ERROR_NOT_CAPABLE); + } + + // }}} + // {{{ numRows() + + function numRows($result) + { + return $this->raiseError(DB_ERROR_NOT_CAPABLE); } // }}} Index: php4/pear/DB/ibase.php diff -u php4/pear/DB/ibase.php:1.12 php4/pear/DB/ibase.php:1.13 --- php4/pear/DB/ibase.php:1.12 Sat Dec 9 17:32:16 2000 +++ php4/pear/DB/ibase.php Sat Dec 9 18:07:00 2000 @@ -16,7 +16,7 @@ // | Authors: Sterling Hughes <sterling@php.net> | // +----------------------------------------------------------------------+ // -// $Id: ibase.php,v 1.12 2000/12/10 01:32:16 ssb Exp $ +// $Id: ibase.php,v 1.13 2000/12/10 02:07:00 ssb Exp $ // // Database independent query interface definition for PHP's Interbase // extension. @@ -33,6 +33,7 @@ function DB_ibase() { + $this->DB_common(); $this->phptype = 'ibase'; $this->dbsyntax = 'ibase'; $this->features = array( Index: php4/pear/DB/msql.php diff -u php4/pear/DB/msql.php:1.11 php4/pear/DB/msql.php:1.12 --- php4/pear/DB/msql.php:1.11 Sat Dec 9 17:32:16 2000 +++ php4/pear/DB/msql.php Sat Dec 9 18:07:00 2000 @@ -16,7 +16,7 @@ // | Authors: Sterling Hughes <sterling@php.net> | // +----------------------------------------------------------------------+ // -// $Id: msql.php,v 1.11 2000/12/10 01:32:16 ssb Exp $ +// $Id: msql.php,v 1.12 2000/12/10 02:07:00 ssb Exp $ // // Database independent query interface definition for PHP's Mini-SQL // extension. @@ -31,7 +31,9 @@ var $prepare_tokens = array(); var $prepare_types = array(); - function DB_msql() { + function DB_msql() + { + $this->DB_common(); $this->phptype = 'msql'; $this->dbsyntax = 'msql'; $this->features = array( @@ -41,7 +43,8 @@ ); } - function connect($dsn, $persistent = false) { + function connect($dsn, $persistent = false) + { if(is_array($dsn)) { $dsninfo = &$dsn; } else { @@ -71,11 +74,13 @@ return DB_OK; } - function disconnect() { + function disconnect() + { return @msql_close($this->connection); } - function simpleQuery($stmt) { + function simpleQuery($stmt) + { $this->last_query = $stmt; $result = @msql_query($stmt, $this->connection); if (!$result) { @@ -86,7 +91,8 @@ return DB::isManip($stmt) ? DB_OK : $result; } - function &fetchRow($result, $fetchmode=DB_FETCHMODE_DEFAULT) { + function &fetchRow($result, $fetchmode=DB_FETCHMODE_DEFAULT) + { if ($fetchmode == DB_FETCHMODE_DEFAULT) { $fetchmode = $this->fetchmode; } @@ -101,7 +107,8 @@ return $row; } - function fetchInto($result, &$ar, $fetchmode=DB_FETCHMODE_DEFAULT) { + function fetchInto($result, &$ar, $fetchmode=DB_FETCHMODE_DEFAULT) + { if ($fetchmode == DB_FETCHMODE_DEFAULT) { $fetchmode = $this->fetchmode; } @@ -116,7 +123,8 @@ return DB_OK; } - function freeResult($result) { + function freeResult($result) + { if (is_resource($result)) { return @msql_free_result($result); } @@ -128,7 +136,8 @@ return true; } - function numCols($result) { + function numCols($result) + { $cols = @msql_num_fields($result); if (!$cols) { return $this->raiseError(); @@ -136,47 +145,13 @@ return $cols; } - function prepare($query) { - $tokens = split('[\&\?]', $query); - $token = 0; - $types = array(); - for ($i = 0; $i < strlen($query); $i++) { - switch ($query[$i]) { - case '?': - $types[$token++] = DB_PARAM_SCALAR; - break; - case '&': - $types[$token++] = DB_PARAM_OPAQUE; - break; - } - } - $this->prepare_tokens[] = &$tokens; - end($this->prepare_tokens); - $k = key($this->prepare_tokens); - $this->prepare_types[$k] = $types; - return $k; - } - - function execute($stmt, $data = false) { - $realquery = $this->executeEmulateQuery($stmt, $data); - $this->last_query = $realquery; - $result = @msql_query($realquery, $this->connection); - if (!$result) { + function numRows($result) + { + $rows = @msql_num_rows($result); + if (!$rows) { return $this->raiseError(); } - return DB::isManip($realquery) ? DB_OK : $result; - } - - function autoCommit($onoff=false) { - return $this->raiseError(DB_ERROR_NOT_CAPABLE); - } - - function commit() { - return $this->raiseError(DB_ERROR_NOT_CAPABLE); - } - - function rollback() { - return $this->raiseError(DB_ERROR_NOT_CAPABLE); + return $rows; } } Index: php4/pear/DB/mssql.php diff -u php4/pear/DB/mssql.php:1.14 php4/pear/DB/mssql.php:1.15 --- php4/pear/DB/mssql.php:1.14 Sat Dec 9 17:32:16 2000 +++ php4/pear/DB/mssql.php Sat Dec 9 18:07:00 2000 @@ -16,7 +16,7 @@ // | Authors: Sterling Hughes <sterling@php.net> | // +----------------------------------------------------------------------+ // -// $Id: mssql.php,v 1.14 2000/12/10 01:32:16 ssb Exp $ +// $Id: mssql.php,v 1.15 2000/12/10 02:07:00 ssb Exp $ // // Database independent query interface definition for PHP's Microsoft SQL Server // extension. @@ -31,7 +31,9 @@ var $prepare_tokens = array(); var $prepare_types = array(); - function DB_mssql() { + function DB_mssql() + { + $this->DB_common(); $this->phptype = 'mssql'; $this->dbsyntax = 'mssql'; $this->features = array( @@ -41,7 +43,8 @@ ); } - function connect($dsn, $persistent = false) { + function connect($dsn, $persistent = false) + { if(is_array($dsn)) { $dsninfo = &$dsn; } else { @@ -71,11 +74,13 @@ return DB_OK; } - function disconnect() { + function disconnect() + { return @mssql_close($this->connection); } - function simpleQuery($stmt) { + function simpleQuery($stmt) + { $this->last_query = $query; $result = @mssql_query($stmt, $this->connection); if (!$result) { @@ -86,7 +91,8 @@ return DB::isManip($stmt) ? DB_OK : $result; } - function &fetchRow($result, $fetchmode=DB_FETCHMODE_DEFAULT) { + function &fetchRow($result, $fetchmode=DB_FETCHMODE_DEFAULT) + { if ($fetchmode == DB_FETCHMODE_DEFAULT) { $fetchmode = $this->fetchmode; } @@ -101,7 +107,8 @@ return $row; } - function fetchInto($result, &$ar, $fetchmode=DB_FETCHMODE_DEFAULT) { + function fetchInto($result, &$ar, $fetchmode=DB_FETCHMODE_DEFAULT) + { if ($fetchmode == DB_FETCHMODE_DEFAULT) { $fetchmode = $this->fetchmode; } @@ -116,7 +123,8 @@ return DB_OK; } - function freeResult($result) { + function freeResult($result) + { if (is_resource($result)) { return @mssql_free_result($result); } @@ -128,7 +136,8 @@ return true; } - function numCols($result) { + function numCols($result) + { $cols = @mssql_num_fields($result); if (!$cols) { return $this->raiseError(); @@ -136,47 +145,13 @@ return $cols; } - function prepare($query) { - $tokens = split('[\&\?]', $query); - $token = 0; - $types = array(); - for ($i = 0; $i < strlen($query); $i++) { - switch ($query[$i]) { - case '?': - $types[$token++] = DB_PARAM_SCALAR; - break; - case '&': - $types[$token++] = DB_PARAM_OPAQUE; - break; - } - } - $this->prepare_tokens[] = &$tokens; - end($this->prepare_tokens); - $k = key($this->prepare_tokens); - $this->prepare_types[$k] = $types; - return $k; - } - - function execute($stmt, $data = false) { - $realquery = $this->executeEmulateQuery($stmt, $data); - $this->last_query = $realquery; - $result = @mssql_query($realquery, $this->connection); - if (!$result) { + function numRows($result) + { + $rows = @mssql_num_rows($result); + if (!$rows) { return $this->raiseError(); } - return DB::isManip($realquery) ? DB_OK : $result; - } - - function autoCommit($onoff=false) { - return $this->raiseError(DB_ERROR_NOT_CAPABLE); - } - - function commit() { - return $this->raiseError(DB_ERROR_NOT_CAPABLE); - } - - function rollback() { - return $this->raiseError(DB_ERROR_NOT_CAPABLE); + return $rows; } } Index: php4/pear/DB/mysql.php diff -u php4/pear/DB/mysql.php:1.38 php4/pear/DB/mysql.php:1.39 --- php4/pear/DB/mysql.php:1.38 Sat Dec 9 17:32:16 2000 +++ php4/pear/DB/mysql.php Sat Dec 9 18:07:00 2000 @@ -48,7 +48,9 @@ * @access public */ - function DB_mysql() { + function DB_mysql() + { + $this->DB_common(); $this->phptype = "mysql"; $this->dbsyntax = "mysql"; $this->features = array( @@ -87,7 +89,8 @@ * @return int DB_OK on success, a DB error on failure */ - function connect($dsn, $persistent = false) { + function connect($dsn, $persistent = false) + { if (is_array($dsn)) { $dsninfo = &$dsn; } else { @@ -138,7 +141,8 @@ * * @return bool TRUE on success, FALSE if not connected. */ - function disconnect() { + function disconnect() + { return mysql_close($this->connection); } @@ -157,7 +161,8 @@ * queries, DB_OK for other successful queries. A DB error is * returned on failure. */ - function simpleQuery($query) { + function simpleQuery($query) + { $this->last_query = $query; $result = mysql_query( $query, $this->connection ); @@ -184,7 +189,8 @@ * @return mixed an array on success, a DB error on failure, NULL * if there is no more data */ - function &fetchRow($result, $fetchmode = DB_FETCHMODE_DEFAULT) { + function &fetchRow($result, $fetchmode = DB_FETCHMODE_DEFAULT) + { if ($fetchmode == DB_FETCHMODE_DEFAULT) { $fetchmode = $this->fetchmode; } @@ -222,7 +228,8 @@ * * @return int DB_OK on success, a DB error on failure */ - function fetchInto($result, &$arr, $fetchmode = DB_FETCHMODE_DEFAULT) { + function fetchInto($result, &$arr, $fetchmode = DB_FETCHMODE_DEFAULT) + { if ($fetchmode == DB_FETCHMODE_DEFAULT) { $fetchmode = $this->fetchmode; } @@ -258,7 +265,8 @@ * * @return bool TRUE on success, FALSE if $result is invalid */ - function freeResult($result) { + function freeResult($result) + { if (is_resource($result)) { return mysql_free_result($result); } @@ -285,7 +293,8 @@ * * @return int the number of columns per row in $result */ - function numCols($result) { + function numCols($result) + { $cols = mysql_num_fields($result); if (!$cols) { @@ -355,118 +364,9 @@ * @return int native MySQL error code */ - function errorNative() { + function errorNative() + { return mysql_errno($this->connection); - } - - // }}} - // {{{ prepare() - - /** - * Prepares a query for multiple execution with execute(). With - * MySQL, this is emulated. - * - * @param $query the SQL query to prepare for execution. The - * characters "?" and "&" have special meanings. "?" is a scalar - * placeholder, the value provided for it when execute()ing is - * inserted as-is. "&" is an opaque placeholder, the value - * provided on execute() is a file name, and the contents of that - * file will be used in the query. - * - * @access public - * - * @return int identifier for this prepared query - */ - - function prepare($query) { - $tokens = split("[\&\?]", $query); - $token = 0; - $types = array(); - - for ($i = 0; $i < strlen($query); $i++) { - switch ($query[$i]) { - case "?": - $types[$token++] = DB_PARAM_SCALAR; - break; - case '&': - $types[$token++] = DB_PARAM_OPAQUE; - break; - } - } - - $this->prepare_tokens[] = &$tokens; - end($this->prepare_tokens); - - $k = key($this->prepare_tokens); - $this->prepare_types[$k] = $types; - - return $k; - } - - // }}} - // {{{ execute() - - /** - * Executes a prepared query and substitutes placeholders with - * provided values. - * - * @return int returns a MySQL result resource for successful - * SELECT queries, DB_OK for other successful queries. A DB error - * is returned on failure. - * - * @see DB_mysql::prepare - */ - function execute($stmt, $data = false) { - $realquery = $this->executeEmulateQuery($stmt, $data); - $this->last_query = $realquery; - $result = mysql_query($realquery, $this->connection); - if (!$result) { - return $this->raiseError($this->errorCode(mysql_errno($this->connection))); - } - return DB::isManip($realquery) ? DB_OK : $result; - } - - // }}} - // {{{ autoCommit() - - /** - * Enable/disable automatic commits [not supported by MySQL] - * - * @access public - * - * @return object DB error code (not capable) - */ - function autoCommit($onoff = false) { - return $this->raiseError(DB_ERROR_NOT_CAPABLE); - } - - // }}} - // {{{ commit() - - /** - * Commit transactions on the current connection [not supported by MySQL] - * - * @access public - * - * @return object DB error code (not capable) - */ - function commit() { - return $this->raiseError(DB_ERROR_NOT_CAPABLE); - } - - // }}} - // {{{ rollback() - - /** - * Roll back all uncommitted transactions on the current connection. - * [not supported by MySQL] - * - * @access public - * - * @return object DB error code (not capable) - */ - function rollback() { - return $this->raiseError(DB_ERROR_NOT_CAPABLE); } // }}} Index: php4/pear/DB/oci8.php diff -u php4/pear/DB/oci8.php:1.11 php4/pear/DB/oci8.php:1.12 --- php4/pear/DB/oci8.php:1.11 Sat Dec 9 17:32:16 2000 +++ php4/pear/DB/oci8.php Sat Dec 9 18:07:00 2000 @@ -45,6 +45,7 @@ // {{{ constructor function DB_oci8() { + $this->DB_common(); $this->phptype = 'oci8'; $this->dbsyntax = 'oci8'; $this->features = array( Index: php4/pear/DB/odbc.php diff -u php4/pear/DB/odbc.php:1.18 php4/pear/DB/odbc.php:1.19 --- php4/pear/DB/odbc.php:1.18 Sat Dec 9 17:32:16 2000 +++ php4/pear/DB/odbc.php Sat Dec 9 18:07:00 2000 @@ -33,17 +33,19 @@ require_once 'DB/common.php'; -class DB_odbc extends DB_common { +class DB_odbc extends DB_common +{ // {{{ properties var $connection; var $phptype, $dbsyntax; // }}} - // {{{ constructor - function DB_odbc() { + function DB_odbc() + { + $this->DB_common(); $this->phptype = 'odbc'; $this->dbsyntax = 'unknown'; // Let's be very pessimistic about the features of an odbc @@ -81,7 +83,6 @@ } // }}} - // {{{ connect() /** @@ -146,7 +147,8 @@ // }}} // {{{ disconnect() - function disconnect() { + function disconnect() + { $err = odbc_close($this->connection); // XXX ERRORMSG return $err; } @@ -164,7 +166,8 @@ * queries, DB_OK for other successful queries. A DB error code * is returned on failure. */ - function simpleQuery($query) { + function simpleQuery($query) + { $this->last_query = $query; $result = odbc_exec($this->connection, $query); if (!$result) { @@ -213,7 +216,8 @@ // }}} // {{{ freeResult() - function freeResult($result) { + function freeResult($result) + { $err = odbc_free_result($result); // XXX ERRORMSG return $err; } @@ -221,22 +225,12 @@ // }}} // {{{ quoteString() - function quoteString($string) { + function quoteString($string) + { return str_replace("'", "''", $string); } // }}} - - // prepare - // execute - // errorCode - // errorMsg - // errorNative - // longReadlen - // binMode - // autoCommit - // commit - // rollback } // Local variables: Index: php4/pear/DB/pgsql.php diff -u php4/pear/DB/pgsql.php:1.21 php4/pear/DB/pgsql.php:1.22 --- php4/pear/DB/pgsql.php:1.21 Sat Dec 9 17:32:16 2000 +++ php4/pear/DB/pgsql.php Sat Dec 9 18:07:00 2000 @@ -25,140 +25,144 @@ // XXX legend: // // XXX ERRORMSG: The error message from the pgsql function should -// be registered here. +// be registered here. // require_once 'DB/common.php'; -class DB_pgsql extends DB_common { +class DB_pgsql extends DB_common +{ // {{{ properties - var $connection; - var $phptype, $dbsyntax; - var $prepare_tokens = array(); - var $prepare_types = array(); - var $numrows; - var $row; - var $affected; + var $connection; + var $phptype, $dbsyntax; + var $prepare_tokens = array(); + var $prepare_types = array(); + var $numrows; + var $row; + var $affected; var $autocommit = 1; // }}} - // {{{ constructor - function DB_pgsql() { - $this->phptype = 'pgsql'; - $this->dbsyntax = 'pgsql'; - $this->features = array( - 'prepare' => false, - 'pconnect' => true, - 'transactions' => true - ); - $this->errorcode_map = array(); - $this->numrows = array(); - $this->row = array(); - $this->affected = 0; - } + function DB_pgsql() + { + $this->DB_common(); + $this->phptype = 'pgsql'; + $this->dbsyntax = 'pgsql'; + $this->features = array( + 'prepare' => false, + 'pconnect' => true, + 'transactions' => true + ); + $this->errorcode_map = array(); + $this->numrows = array(); + $this->row = array(); + $this->affected = 0; + } // }}} - // {{{ 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 - * - * @return int DB_OK on success, a DB error code on failure - */ - function connect($dsn, $persistent = false) { - if (is_array($dsn)) { - $dsninfo = &$dsn; - } else { - $dsninfo = DB::parseDSN($dsn); - } - if (!$dsninfo || !$dsninfo['phptype']) { - return $this->raiseError(); // XXX ERRORMSG - } - $dbhost = $dsninfo['hostspec'] ? $dsninfo['hostspec'] : 'localhost'; - $protocol = $dsninfo['protocol'] ? $dsninfo['protocol'] : 'tcp'; - $user = $dsninfo['username']; - $pw = $dsninfo['password']; - $dbname = $dsninfo['database']; - $options = $dsninfo['options']; - $tty = $dsninfo['tty']; - $port = $dsninfo['port'] ? $dsninfo['port'] : '5432'; - - $connect_function = $persistent ? 'pg_pconnect' : 'pg_connect'; - - if (($protocol == 'unix') && $dbname) { - $connect_params = "dbname=$dbname"; - if ($user) { - $connect_params .= " user=$user"; - } - if ($pw) { - $connect_params .= " password=$pw"; - } - $conn = $connect_function($connect_params); - } elseif ($dbhost && $user && $pw && $dbname) { - $conn = $connect_function( - "host=$dbhost port=$port dbname=$dbname user=$user password=$pw"); - } elseif ($dbhost && $dbname && $options && $tty) { - $conn = $connect_function($dbhost, $port, $options, $tty, $dbname); - } elseif ($dbhost && $dbname) { - $conn = $connect_function($dbhost, $port, $dbname); - } else { - $conn = false; - } - if ($conn == false) { - return $this->raiseError(); // XXX ERRORMSG - } - $this->connection = $conn; - return DB_OK; - } + /** + * 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($dsn, $persistent = false) + { + if (is_array($dsn)) { + $dsninfo = &$dsn; + } else { + $dsninfo = DB::parseDSN($dsn); + } + if (!$dsninfo || !$dsninfo['phptype']) { + return $this->raiseError(); // XXX ERRORMSG + } + $dbhost = $dsninfo['hostspec'] ? $dsninfo['hostspec'] : 'localhost'; + $protocol = $dsninfo['protocol'] ? $dsninfo['protocol'] : 'tcp'; + $user = $dsninfo['username']; + $pw = $dsninfo['password']; + $dbname = $dsninfo['database']; + $options = $dsninfo['options']; + $tty = $dsninfo['tty']; + $port = $dsninfo['port'] ? $dsninfo['port'] : '5432'; + + $connect_function = $persistent ? 'pg_pconnect' : 'pg_connect'; + + if (($protocol == 'unix') && $dbname) { + $connect_params = "dbname=$dbname"; + if ($user) { + $connect_params .= " user=$user"; + } + if ($pw) { + $connect_params .= " password=$pw"; + } + $conn = $connect_function($connect_params); + } elseif ($dbhost && $user && $pw && $dbname) { + $conn = $connect_function( + "host=$dbhost port=$port dbname=$dbname user=$user password=$pw"); + } elseif ($dbhost && $dbname && $options && $tty) { + $conn = $connect_function($dbhost, $port, $options, $tty, $dbname); + } elseif ($dbhost && $dbname) { + $conn = $connect_function($dbhost, $port, $dbname); + } else { + $conn = false; + } + if ($conn == false) { + return $this->raiseError(); // XXX ERRORMSG + } + $this->connection = $conn; + return DB_OK; + } // }}} // {{{ disconnect() - /** - * Log out and disconnect from the database. - * - * @return bool TRUE on success, FALSE if not connected. - */ - function disconnect() { - return pg_close($this->connection); // XXX ERRORMSG - } + /** + * Log out and disconnect from the database. + * + * @return bool TRUE on success, FALSE if not connected. + */ + function disconnect() + { + return pg_close($this->connection); // XXX ERRORMSG + } // }}} // {{{ simpleQuery() - /** - * Send a query to PostgreSQL and return the results as a - * PostgreSQL resource identifier. - * - * @param $query the SQL query - * - * @return int returns a valid PostgreSQL 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; - $result = @pg_exec($this->connection, $query); - if (!$result) { - return $this->raiseError(pg_errormessage($this->connection)); - } + /** + * Send a query to PostgreSQL and return the results as a + * PostgreSQL resource identifier. + * + * @param $query the SQL query + * + * @return int returns a valid PostgreSQL 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; + $result = @pg_exec($this->connection, $query); + if (!$result) { + return $this->raiseError(pg_errormessage($this->connection)); + } if ($this->autocommit && DB::isManip($query)) { $this->commit(); } - // Determine which queries that should return data, and which - // should return an error code only. - if (DB::isManip($query)) { - $this->affected = pg_cmdtuples($result); - return DB_OK; - } elseif (preg_match('/^\s*(SELECT)\s/i', $query) && + // Determine which queries that should return data, and which + // should return an error code only. + if (DB::isManip($query)) { + $this->affected = pg_cmdtuples($result); + return DB_OK; + } elseif (preg_match('/^\s*(SELECT)\s/i', $query) && !preg_match('/^\s*(SELECT\s+INTO)\s/i', $query)) { /* PostgreSQL commands: ABORT, ALTER, BEGIN, CLOSE, CLUSTER, COMMIT, COPY, @@ -167,254 +171,264 @@ REVOKE, ROLLBACK, SELECT, SELECT INTO, SET, SHOW, UNLISTEN, UPDATE, VACUUM */ - $this->row[$result] = 0; // reset the row counter. - $this->numrows[$result] = pg_numrows($result); + $this->row[$result] = 0; // reset the row counter. + $this->numrows[$result] = pg_numrows($result); $this->affected = 0; - return $result; - } else { + return $result; + } else { $this->affected = 0; - return DB_OK; - } - } + return DB_OK; + } + } - // }}} + // }}} // {{{ fetchRow() - /** - * Fetch a row and return as array. - * - * @param $result PostgreSQL result identifier - * @param $fetchmode how the resulting array should be indexed - * - * @return int an array on success, a DB error code on failure, NULL - * if there is no more data - */ - function &fetchRow($result, $fetchmode = DB_FETCHMODE_DEFAULT) { - if ($fetchmode == DB_FETCHMODE_DEFAULT) { - $fetchmode = $this->fetchmode; - } - if ($this->row[$result]>=$this->numrows[$result]){ - return NULL; - } - if ($fetchmode & DB_FETCHMODE_ASSOC) { - $row = pg_fetch_array($result, $this->row[$result], PGSQL_ASSOC); - } else { - $row = pg_fetch_row($result, $this->row[$result]); - } - if (!$row) { - $err = $this->raiseError(pg_errormessage($this->connection)); - if (!$err) { - return NULL; - } - return $err; - } - $this->row[$result]++; - return $row; - } + /** + * Fetch a row and return as array. + * + * @param $result PostgreSQL result identifier + * @param $fetchmode how the resulting array should be indexed + * + * @return int an array on success, a DB error code on failure, NULL + * if there is no more data + */ + function &fetchRow($result, $fetchmode = DB_FETCHMODE_DEFAULT) + { + if ($fetchmode == DB_FETCHMODE_DEFAULT) { + $fetchmode = $this->fetchmode; + } + if ($this->row[$result]>=$this->numrows[$result]){ + return NULL; + } + if ($fetchmode & DB_FETCHMODE_ASSOC) { + $row = pg_fetch_array($result, $this->row[$result], PGSQL_ASSOC); + } else { + $row = pg_fetch_row($result, $this->row[$result]); + } + if (!$row) { + $err = $this->raiseError(pg_errormessage($this->connection)); + if (!$err) { + return NULL; + } + return $err; + } + $this->row[$result]++; + return $row; + } - // }}} + // }}} // {{{ fetchInto() - /** - * Fetch a row and insert the data into an existing array. - * - * @param $result PostgreSQL result identifier - * @param $arr (reference) array where data from the row is stored - * @param $fetchmode how the array data should be indexed - * - * @return int DB_OK on success, a DB error code on failure - */ - function fetchInto($result, &$arr, $fetchmode = DB_FETCHMODE_DEFAULT) { - if ($fetchmode == DB_FETCHMODE_DEFAULT) { - $fetchmode = $this->fetchmode; - } - if ($this->row[$result]>=$this->numrows[$result]){ - return NULL; - } - if ($fetchmode & DB_FETCHMODE_ASSOC) { - $arr = pg_fetch_array($result, $this->row[$result]); - } else { - $arr = pg_fetch_row($result, $this->row[$result]); - } - if (!$arr) { - /* - $errno = pg_errormessage($this->connection); - if (!$errno) { - return NULL; - } - return $errno; - */ - // the error codes are not supported in pgsql. - return $this->raiseError(DB_ERROR_NOT_CAPABLE); - } - $this->row[$result]++; - return DB_OK; - } + /** + * Fetch a row and insert the data into an existing array. + * + * @param $result PostgreSQL result identifier + * @param $arr (reference) array where data from the row is stored + * @param $fetchmode how the array data should be indexed + * + * @return int DB_OK on success, a DB error code on failure + */ + function fetchInto($result, &$arr, $fetchmode = DB_FETCHMODE_DEFAULT) + { + if ($fetchmode == DB_FETCHMODE_DEFAULT) { + $fetchmode = $this->fetchmode; + } + if ($this->row[$result]>=$this->numrows[$result]){ + return NULL; + } + if ($fetchmode & DB_FETCHMODE_ASSOC) { + $arr = pg_fetch_array($result, $this->row[$result]); + } else { + $arr = pg_fetch_row($result, $this->row[$result]); + } + if (!$arr) { + /* + $errno = pg_errormessage($this->connection); + if (!$errno) { + return NULL; + } + return $errno; + */ + // the error codes are not supported in pgsql. + return $this->raiseError(DB_ERROR_NOT_CAPABLE); + } + $this->row[$result]++; + return DB_OK; + } - // }}} + // }}} // {{{ freeResult() - /** - * Free the internal resources associated with $result. - * - * @param $result PostgreSQL result identifier or DB statement identifier - * - * @return bool TRUE on success, FALSE if $result is invalid - */ - function freeResult($result) { - if (is_resource($result)) { - return pg_freeresult($result); - } - if (!isset($this->prepare_tokens[$result])) { - return false; - } - unset($this->prepare_tokens[$result]); - unset($this->prepare_types[$result]); - unset($this->row[$result]); - unset($this->numrows[$result]); - $this->affected = 0; - return true; - } + /** + * Free the internal resources associated with $result. + * + * @param $result PostgreSQL result identifier or DB statement identifier + * + * @return bool TRUE on success, FALSE if $result is invalid + */ + function freeResult($result) + { + if (is_resource($result)) { + return pg_freeresult($result); + } + if (!isset($this->prepare_tokens[$result])) { + return false; + } + unset($this->prepare_tokens[$result]); + unset($this->prepare_types[$result]); + unset($this->row[$result]); + unset($this->numrows[$result]); + $this->affected = 0; + return true; + } - // }}} + // }}} // {{{ numCols() - /** - * Get the number of columns in a result set. - * - * @param $result PostgreSQL result identifier - * - * @return int the number of columns per row in $result - */ - function numCols($result) { - $cols = pg_numfields($result); - if (!$cols) { - return $this->raiseError(pg_errormessage($this->connection)); - } - return $cols; - } + /** + * Get the number of columns in a result set. + * + * @param $result PostgreSQL result identifier + * + * @return int the number of columns per row in $result + */ + function numCols($result) + { + $cols = pg_numfields($result); + if (!$cols) { + return $this->raiseError(pg_errormessage($this->connection)); + } + return $cols; + } // }}} // {{{ numRows() - /** - * Get the number of rows in a result set. - * - * @param $result PostgreSQL result identifier - * - * @return int the number of rows in $result - */ - function numRows($result) { - $rows = pg_numrows($result); - if (!$rows) { - return $this->raiseError(pg_errormessage($this->connection)); - } - return $rows; - } + /** + * Get the number of rows in a result set. + * + * @param $result PostgreSQL result identifier + * + * @return int the number of rows in $result + */ + function numRows($result) + { + $rows = pg_numrows($result); + if (!$rows) { + return $this->raiseError(pg_errormessage($this->connection)); + } + return $rows; + } // }}} // {{{ errorNative() - /** - * Get the native error code of the last error (if any) that - * occured on the current connection. - * - * @return int native PostgreSQL error code - */ - function errorNative() { + /** + * Get the native error code of the last error (if any) that + * occured on the current connection. + * + * @return int native PostgreSQL error code + */ + function errorNative() + { return pg_errormessage($this->connection); - } + } // }}} // {{{ prepare() - /** - * Prepares a query for multiple execution with execute(). With - * PostgreSQL, this is emulated. - */ - function prepare($query) { - $tokens = split('[\&\?]', $query); - $token = 0; - $types = array(); - for ($i = 0; $i < strlen($query); $i++) { - switch ($query[$i]) { - case '?': - $types[$token++] = DB_PARAM_SCALAR; - break; - case '&': - $types[$token++] = DB_PARAM_OPAQUE; - break; - } - } - $this->prepare_tokens[] = &$tokens; - end($this->prepare_tokens); - $k = key($this->prepare_tokens); - $this->prepare_types[$k] = $types; - return $k; - } + /** + * Prepares a query for multiple execution with execute(). With + * PostgreSQL, this is emulated. + */ + function prepare($query) + { + $tokens = split('[\&\?]', $query); + $token = 0; + $types = array(); + for ($i = 0; $i < strlen($query); $i++) { + switch ($query[$i]) { + case '?': + $types[$token++] = DB_PARAM_SCALAR; + break; + case '&': + $types[$token++] = DB_PARAM_OPAQUE; + break; + } + } + $this->prepare_tokens[] = &$tokens; + end($this->prepare_tokens); + $k = key($this->prepare_tokens); + $this->prepare_types[$k] = $types; + return $k; + } // }}} // {{{ execute() - /** - * @return int returns a PostgreSQL result resource for successful - * SELECT queries, DB_OK for other successful queries. A DB error - * code is returned on failure. - */ - function execute($stmt, $data = false) { - $realquery = $this->executeEmulateQuery($stmt, $data); - $this->last_query = $realquery; + /** + * @return int returns a PostgreSQL result resource for successful + * SELECT queries, DB_OK for other successful queries. A DB error + * code is returned on failure. + */ + function execute($stmt, $data = false) + { + $realquery = $this->executeEmulateQuery($stmt, $data); + $this->last_query = $realquery; $result = $this->simpleQuery($realquery); if (DB::isError($result) || $result === DB_OK) { return $result; } else { return new DB_result($this, $result); } - } + } // }}} // {{{ autoCommit() - /** - * Enable/disable automatic commits - */ - function autoCommit($onoff = false) { + /** + * Enable/disable automatic commits + */ + function autoCommit($onoff = false) + { $this->autocommit = $onoff ? true : false; return DB_OK; - } + } // }}} // {{{ commit() - /** - * Commit transactions on the current connection - */ - function commit() { + /** + * Commit transactions on the current connection + */ + function commit() + { // hack to shut up error messages from ext/pgsql or libpq.a @fclose(@fopen("php://stderr", "w")); - $result = @pg_exec($this->connection, "end;"); - if (!$result) { - return $this->raiseError(pg_errormessage($this->connection)); - } - return DB_OK; - } + $result = @pg_exec($this->connection, "end;"); + if (!$result) { + return $this->raiseError(pg_errormessage($this->connection)); + } + return DB_OK; + } // }}} // {{{ rollback() - /** - * Roll back all uncommitted transactions on the current connection. - */ - function rollback() { - $result = @pg_exec($this->connection, "abort;"); - if (!$result) { - return $this->raiseError(pg_errormessage($this->connection)); - } - return DB_OK; - } + /** + * Roll back all uncommitted transactions on the current connection. + */ + function rollback() + { + $result = @pg_exec($this->connection, "abort;"); + if (!$result) { + return $this->raiseError(pg_errormessage($this->connection)); + } + return DB_OK; + } // }}} - // {{{ affectedRows() /** @@ -423,7 +437,8 @@ * * @return number of rows affected by the last query or DB_ERROR */ - function affectedRows() { + function affectedRows() + { $result = $this->affected; if ($result === false) { return $this->raiseError(); @@ -431,12 +446,6 @@ return $result; } // }}} - - // TODO/wishlist: - // simpleFetch - // simpleGet - // longReadlen - // binmode } // Local variables: Index: php4/pear/DB/sybase.php diff -u php4/pear/DB/sybase.php:1.10 php4/pear/DB/sybase.php:1.11 --- php4/pear/DB/sybase.php:1.10 Sat Dec 9 17:32:16 2000 +++ php4/pear/DB/sybase.php Sat Dec 9 18:07:00 2000 @@ -16,7 +16,7 @@ // | Authors: Sterling Hughes <sterling@php.net> | // +----------------------------------------------------------------------+ // -// $Id: sybase.php,v 1.10 2000/12/10 01:32:16 ssb Exp $ +// $Id: sybase.php,v 1.11 2000/12/10 02:07:00 ssb Exp $ // // Database independent query interface definition for PHP's Sybase // extension. @@ -26,13 +26,19 @@ class DB_sybase extends DB_common { + // {{{ properties + var $connection; var $phptype, $dbsyntax; var $prepare_tokens = array(); var $prepare_types = array(); + // }}} + // {{{ constructor + function DB_sybase() { + $this->DB_common(); $this->phptype = 'sybase'; $this->dbsyntax = 'sybase'; $this->features = array( @@ -42,9 +48,12 @@ ); } + // }}} + // {{{ connect() + function connect($dsn, $persistent = false) { - if(is_array($dsn)) { + if (is_array($dsn)) { $dsninfo = &$dsn; } else { $dsninfo = DB::parseDSN($dsn); @@ -60,11 +69,17 @@ return DB_OK; } + // }}} + // {{{ disconnect() + function disconnect() { return @sybase_close($this->connection); } + // }}} + // {{{ simpleQuery() + function simpleQuery($stmt) { $this->last_query = $stmt; @@ -77,6 +92,9 @@ return DB::isManip($stmt) ? DB_OK : $result; } + // }}} + // {{{ fetchRow() + function &fetchRow($result, $fetchmode=DB_FETCHMODE_DEFAULT) { if ($fetchmode == DB_FETCHMODE_DEFAULT) { @@ -89,6 +107,9 @@ return $row; } + // }}} + // {{{ fetchInto() + function fetchInto($result, &$ar, $fetchmode=DB_FETCHMODE_DEFAULT) { if ($fetchmode == DB_FETCHMODE_DEFAULT) { @@ -101,6 +122,9 @@ return DB_OK; } + // }}} + // {{{ freeResult() + function freeResult($result) { if (is_resource($result)) { @@ -114,6 +138,9 @@ return true; } + // }}} + // {{{ numCols() + function numCols($result) { $cols = @sybase_num_fields($result); @@ -123,53 +150,7 @@ return $cols; } - function prepare($query) - { - $tokens = split('[\&\?]', $query); - $token = 0; - $types = array(); - for ($i = 0; $i < strlen($query); $i++) { - switch ($query[$i]) { - case '?': - $types[$token++] = DB_PARAM_SCALAR; - break; - case '&': - $types[$token++] = DB_PARAM_OPAQUE; - break; - } - } - $this->prepare_tokens[] = &$tokens; - end($this->prepare_tokens); - $k = key($this->prepare_tokens); - $this->prepare_types[$k] = $types; - return $k; - } - - function execute($stmt, $data = false) - { - $realquery = $this->executeEmulateQuery($stmt, $data); - $this->last_query = $realquery; - $result = @sybase_query($realquery, $this->connection); - if (!$result) { - return $this->raiseError(); - } - return DB::isManip($realquery) ? DB_OK : $result; - } - - function autoCommit($onoff = false) - { - return $this->raiseError(DB_ERROR_NOT_CAPABLE); - } - - function commit() - { - return $this->raiseError(DB_ERROR_NOT_CAPABLE); - } - - function rollback() - { - return $this->raiseError(DB_ERROR_NOT_CAPABLE); - } + // }}} } /*
« previous php.cvs (#3049) next »