📄 pgsql.php
字号:
<?phpif (!function_exists('pg_fetch_assoc')) { function pg_fetch_assoc ($result) { return @pg_fetch_array($result, NULL, PGSQL_ASSOC); }}/*** @package Library* This file has the MySQLDb class in it.** @version $Id: pgsql.php,v 1.3 2004/11/25 02:33:06 chris Exp $* @author Chris <chris@interspire.com>* @filesource*/// Include the base database class.require_once(dirname(__FILE__) . '/db.php');/*** @package Library* @subpackage Db* This is the class for the MySQL database system.*/class PGSQLDb extends Db { /** * @var connection The global database connection. */ var $connection = null; /** * @var fulltext Does your database support postgresql fulltext indexing? It's a complicated procedure and an optional thing so we set this to false by default. */ var $_fulltext = false; /** * Constructor * * Sets up the database connection. * Can pass in the hostname, username, password and database name if you want to. * If you don't it will set up the base class, then you'll have to call Connect yourself. * * @param servername Name of the server to connect to. * @param username Username to connect to the server with. * @param password Password to connect with. * @param databasename Database name to connect to. * * @return mixed Returns false if no connection can be made - the error can be fetched by the Error() method. Returns the connection result if it can be made. Will return Null if you don't pass in the connection details. * * @see Connect * @see GetError */ function PGSQLDb($hostname='', $username='', $password='', $databasename='') { if ($hostname && $username && $databasename) { $connection = &$this->Connect($hostname, $username, $password, $databasename); return $connection; } return null; } /** * Connect * * This function will connect to the database based on the details passed in. * * @param servername Name of the server to connect to. * @param username Username to connect to the server with. * @param password Password to connect with. * @param databasename Database name to connect to. * @return mixed Will return the connection if it's successful. Otherwise returns false. * * @see Error */ function Connect($hostname='', $username='', $password='', $databasename='') { if ($hostname == '') { $this->SetError('No server name to connect to'); return false; } if ($username == '') { $this->SetError('No username name to connect to server ' . $servername . ' with'); return false; } if ($databasename == '') { $this->SetError('No database name to connect to'); return false; } $connection_string = 'dbname=' . addslashes($databasename); if ($hostname != 'localhost') $connection_string .= ' host=' . addslashes($hostname); $connection_string .= ' user=' . addslashes($username); if ($password != '') $connection_string .= ' password=' . addslashes($password); if (!$connection_result = pg_connect($connection_string)) { $this->SetError(pg_last_error($connection_result)); return false; } $this->connection = &$connection_result; return $this->connection; } /** * Disconnect * * This function will disconnect from the database handler passed in. * * @return mixed Will return the connection if it's successful. Otherwise returns false. */ function Disconnect($resource=null) { if (is_null($resource)) { $this->SetError('Resource is a null object'); return false; } if (!is_resource($resource)) { $this->SetError('Resource ' . $resource . ' is not really a resource'); return false; } $close_success = pg_close($resource); return $close_success; } /** * Query * * This function will run a query against the database and return the result of the query. * * @return mixed Returns false if the query is empty or if there is no result. Otherwise returns the result of the query. */ function Query($query='') { if (!$query) { $this->SetError('Query passed in is empty'); return false; } if (!$this->connection) { $this->SetError('No valid connection'); return false; } $result = pg_query($query); if (!$result) { $this->SetError(pg_last_error()); return false; } return $result; } /** * Fetch * * This function will fetch a result from the result set passed in. * * @return mixed Returns false if the result is empty. Otherwise returns the next result. */ function Fetch($resource=null) { if (is_null($resource)) { $this->SetError('Resource is a null object'); return false; } if (!is_resource($resource)) { $this->SetError('Resource ' . $resource . ' is not really a resource'); return false; } return stripslashes_array(pg_fetch_assoc($resource)); } /** * Update * Runs an update query against the database. * * @param Query Query to run * * @return boolean Returns true if the query is successful. Returns false if there is no query, no valid connection or the query failed. */ function Update($query='') { if (!$query) { $this->SetError('Query passed in is empty'); return false; } if (!$this->connection) { $this->SetError('No valid connection'); return false; } $result = $this->Query($query); if (!$result) { $this->SetError(pg_last_error()); return false; } return true; } /** * NextID * Fetches the next id from the sequence passed in * * @param SequenceName Sequence Name to fetch the next id for. * * @see Query * * @return mixed Returns false if there is no sequence name or if it can't fetch the next id. Otherwise returns the next id */ function NextId($sequencename=false) { if (!$sequencename) return false; $query = "SELECT nextval('" . $sequencename . "') AS nextid"; $result = $this->Query($query); $row = $this->Fetch($result); return $row['nextid']; } /** * FullText * Fulltext works out how to handle full text searches. Returns an sql statement to append to enable full text searching. * * @param fields Fields to search against * @param searchstring String to search for against the fields * * @return mixed Returns false if either fields or searchstring aren't present, otherwise returns a string to append to an sql statement. */ function FullText($fields=null, $searchstring=null) { if (is_null($fields) or is_null($searchstring)) return false; if (!is_array($fields)) { $fields = explode(',', $fields); }# if (!$this->_fulltext) { $query = ''; $subqueries = array(); foreach($fields as $field) { $subqueries[]= $field . ' ILIKE \'%' . addslashes($searchstring) . '\''; } $query = implode(' OR ', $subqueries); return $query;# }# return $query; } /** * AddLimit * This function creates the SQL to add a limit clause to an sql statement. * * @param offset Where to start fetching the results * @param numtofetch Number of results to fetch * * @return string The string to add to the end of the sql statement */ function AddLimit($offset=0, $numtofetch=0) { if ($offset < 0) $offset = 0; if ($numtofetch <= 0) $numtofetch = 10; $query = ' LIMIT ' . $numtofetch . ' OFFSET ' . $offset; return $query; } /** * CountResult * Returns the number of rows returned for the resource passed in * * @param offset Where to start fetching the results * @param resource The result from calling Query * * @see Query * @see SetError * * @return int Number of rows from the result */ function CountResult($resource = null) { if (is_null($resource)) { $this->SetError('Resource is a null object'); return false; } if (!is_resource($resource)) { $this->SetError('Resource ' . $resource . ' is not really a resource'); return false; } $count = pg_num_rows($resource); return $count; }}?>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -