📄 pgsql.php
字号:
<?php/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: *//** * The PEAR DB driver for PHP's pgsql extension * for interacting with PostgreSQL databases * * PHP versions 4 and 5 * * LICENSE: This source file is subject to version 3.0 of the PHP license * that is available through the world-wide-web at the following URI: * http://www.php.net/license/3_0.txt. If you did not receive a copy of * the PHP License and are unable to obtain it through the web, please * send a note to license@php.net so we can mail you a copy immediately. * * @category Database * @package DB * @author Rui Hirokawa <hirokawa@php.net> * @author Stig Bakken <ssb@php.net> * @author Daniel Convissor <danielc@php.net> * @copyright 1997-2005 The PHP Group * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @version CVS: $Id: pgsql.php,v 1.126 2005/03/04 23:12:36 danielc Exp $ * @link http://pear.php.net/package/DB *//** * Obtain the DB_common class so it can be extended from */require_once 'DB/common.php';/** * The methods PEAR DB uses to interact with PHP's pgsql extension * for interacting with PostgreSQL databases * * These methods overload the ones declared in DB_common. * * @category Database * @package DB * @author Rui Hirokawa <hirokawa@php.net> * @author Stig Bakken <ssb@php.net> * @author Daniel Convissor <danielc@php.net> * @copyright 1997-2005 The PHP Group * @license http://www.php.net/license/3_0.txt PHP License 3.0 * @version Release: 1.7.6 * @link http://pear.php.net/package/DB */class DB_pgsql extends DB_common{ // {{{ properties /** * The DB driver type (mysql, oci8, odbc, etc.) * @var string */ var $phptype = 'pgsql'; /** * The database syntax variant to be used (db2, access, etc.), if any * @var string */ var $dbsyntax = 'pgsql'; /** * The capabilities of this DB implementation * * The 'new_link' element contains the PHP version that first provided * new_link support for this DBMS. Contains false if it's unsupported. * * Meaning of the 'limit' element: * + 'emulate' = emulate with fetch row by number * + 'alter' = alter the query * + false = skip rows * * @var array */ var $features = array( 'limit' => 'alter', 'new_link' => '4.3.0', 'numrows' => true, 'pconnect' => true, 'prepare' => false, 'ssl' => true, 'transactions' => true, ); /** * A mapping of native error codes to DB error codes * @var array */ var $errorcode_map = array( ); /** * The raw database connection created by PHP * @var resource */ var $connection; /** * The DSN information for connecting to a database * @var array */ var $dsn = array(); /** * Should data manipulation queries be committed automatically? * @var bool * @access private */ var $autocommit = true; /** * The quantity of transactions begun * * {@internal While this is private, it can't actually be designated * private in PHP 5 because it is directly accessed in the test suite.}} * * @var integer * @access private */ var $transaction_opcount = 0; /** * The number of rows affected by a data manipulation query * @var integer */ var $affected = 0; /** * The current row being looked at in fetchInto() * @var array * @access private */ var $row = array(); /** * The number of rows in a given result set * @var array * @access private */ var $_num_rows = array(); // }}} // {{{ constructor /** * This constructor calls <kbd>$this->DB_common()</kbd> * * @return void */ function DB_pgsql() { $this->DB_common(); } // }}} // {{{ connect() /** * Connect to the database server, log in and open the database * * Don't call this method directly. Use DB::connect() instead. * * PEAR DB's pgsql driver supports the following extra DSN options: * + connect_timeout How many seconds to wait for a connection to * be established. Available since PEAR DB 1.7.0. * + new_link If set to true, causes subsequent calls to * connect() to return a new connection link * instead of the existing one. WARNING: this is * not portable to other DBMS's. Available only * if PHP is >= 4.3.0 and PEAR DB is >= 1.7.0. * + options Command line options to be sent to the server. * Available since PEAR DB 1.6.4. * + service Specifies a service name in pg_service.conf that * holds additional connection parameters. * Available since PEAR DB 1.7.0. * + sslmode How should SSL be used when connecting? Values: * disable, allow, prefer or require. * Available since PEAR DB 1.7.0. * + tty This was used to specify where to send server * debug output. Available since PEAR DB 1.6.4. * * Example of connecting to a new link via a socket: * <code> * require_once 'DB.php'; * * $dsn = 'pgsql://user:pass@unix(/tmp)/dbname?new_link=true'; * $options = array( * 'portability' => DB_PORTABILITY_ALL, * ); * * $db =& DB::connect($dsn, $options); * if (PEAR::isError($db)) { * die($db->getMessage()); * } * </code> * * @param array $dsn the data source name * @param bool $persistent should the connection be persistent? * * @return int DB_OK on success. A DB_Error object on failure. * * @link http://www.postgresql.org/docs/current/static/libpq.html#LIBPQ-CONNECT */ function connect($dsn, $persistent = false) { if (!PEAR::loadExtension('pgsql')) { return $this->raiseError(DB_ERROR_EXTENSION_NOT_FOUND); } $this->dsn = $dsn; if ($dsn['dbsyntax']) { $this->dbsyntax = $dsn['dbsyntax']; } $protocol = $dsn['protocol'] ? $dsn['protocol'] : 'tcp'; $params = array(''); if ($protocol == 'tcp') { if ($dsn['hostspec']) { $params[0] .= 'host=' . $dsn['hostspec']; } if ($dsn['port']) { $params[0] .= ' port=' . $dsn['port']; } } elseif ($protocol == 'unix') { // Allow for pg socket in non-standard locations. if ($dsn['socket']) { $params[0] .= 'host=' . $dsn['socket']; } if ($dsn['port']) { $params[0] .= ' port=' . $dsn['port']; } } if ($dsn['database']) { $params[0] .= ' dbname=\'' . addslashes($dsn['database']) . '\''; } if ($dsn['username']) { $params[0] .= ' user=\'' . addslashes($dsn['username']) . '\''; } if ($dsn['password']) { $params[0] .= ' password=\'' . addslashes($dsn['password']) . '\''; } if (!empty($dsn['options'])) { $params[0] .= ' options=' . $dsn['options']; } if (!empty($dsn['tty'])) { $params[0] .= ' tty=' . $dsn['tty']; } if (!empty($dsn['connect_timeout'])) { $params[0] .= ' connect_timeout=' . $dsn['connect_timeout']; } if (!empty($dsn['sslmode'])) { $params[0] .= ' sslmode=' . $dsn['sslmode']; } if (!empty($dsn['service'])) { $params[0] .= ' service=' . $dsn['service']; } if (isset($dsn['new_link']) && ($dsn['new_link'] == 'true' || $dsn['new_link'] === true)) { if (version_compare(phpversion(), '4.3.0', '>=')) { $params[] = PGSQL_CONNECT_FORCE_NEW; } } $connect_function = $persistent ? 'pg_pconnect' : 'pg_connect'; $ini = ini_get('track_errors'); $php_errormsg = ''; if ($ini) { $this->connection = @call_user_func_array($connect_function, $params); } else { ini_set('track_errors', 1); $this->connection = @call_user_func_array($connect_function, $params); ini_set('track_errors', $ini); } if (!$this->connection) { return $this->raiseError(DB_ERROR_CONNECT_FAILED, null, null, null, $php_errormsg); } return DB_OK; } // }}} // {{{ disconnect() /** * Disconnects from the database server * * @return bool TRUE on success, FALSE on failure */ function disconnect() { $ret = @pg_close($this->connection); $this->connection = null; return $ret; } // }}} // {{{ simpleQuery() /** * Sends a query to the database server * * @param string the SQL query string * * @return mixed + a PHP result resrouce for successful SELECT queries * + the DB_OK constant for other successful queries * + a DB_Error object on failure */ function simpleQuery($query) { $ismanip = DB::isManip($query); $this->last_query = $query; $query = $this->modifyQuery($query); if (!$this->autocommit && $ismanip) { if ($this->transaction_opcount == 0) { $result = @pg_exec($this->connection, 'begin;'); if (!$result) { return $this->pgsqlRaiseError(); } } $this->transaction_opcount++; } $result = @pg_exec($this->connection, $query); if (!$result) { return $this->pgsqlRaiseError(); } // Determine which queries that should return data, and which // should return an error code only. if ($ismanip) { $this->affected = @pg_affected_rows($result); return DB_OK; } elseif (preg_match('/^\s*\(*\s*(SELECT|EXPLAIN|SHOW)\s/si', $query)) { /* PostgreSQL commands: ABORT, ALTER, BEGIN, CLOSE, CLUSTER, COMMIT, COPY, CREATE, DECLARE, DELETE, DROP TABLE, EXPLAIN, FETCH, GRANT, INSERT, LISTEN, LOAD, LOCK, MOVE, NOTIFY, RESET, REVOKE, ROLLBACK, SELECT, SELECT INTO, SET, SHOW, UNLISTEN, UPDATE, VACUUM */ $this->row[(int)$result] = 0; // reset the row counter. $numrows = $this->numRows($result); if (is_object($numrows)) { return $numrows; } $this->_num_rows[(int)$result] = $numrows; $this->affected = 0; return $result; } else { $this->affected = 0; return DB_OK; } } // }}}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -