[PHPVortex-Commit] phpvortex DB_PostgreSQL.class.php,NONE,1.1 RS_PostgreSQL.class.php,NONE,1.1
Brought to you by:
nop144666
From: Thiago R. <nop...@us...> - 2005-09-28 19:04:32
|
Update of /cvsroot/phpvortex/phpvortex In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv27401 Added Files: DB_PostgreSQL.class.php RS_PostgreSQL.class.php Log Message: Added PostgreSQL DB Class --- NEW FILE: RS_PostgreSQL.class.php --- <?php /** * File for class RS_PostgreSQL. * * @package Vortex * @subpackage DB * @author Thiago Ramon Gonçalves Montoya * @copyright Copyright 2005, Thiago Ramon Gonçalves Montoya * @license http://opensource.org/licenses/lgpl-license.php GNU Lesser General Public License */ /** Require the base class */ require_once('RS_Base.class.php'); /** * Class for PostgreSQL RecordSets. * * @package Vortex * @subpackage DB */ class RS_PostgreSQL extends RS_Base { /** * Get the count of rows in the RecordSet. * * @return int Returns the number of rows in the RecordSet. */ function RowCount() { return pg_num_rows($this->result); } /** * Get a row from the RecordSet. * * Case $row is set, return that row, case else, return the next row. * * @param int $row Row to return, defaults to next. * @param int $type Type of array to return (RS_ROW_NUM | RS_ROW_ASSOC | RS_ROW_BOTH). * @return array Returns the row from the RecordSet, or FALSE if EOF. */ function Row($row = -1, $type = RS_ROW_ASSOC) { if ($row != -1) $this->row = $row; $this->row++; switch ($type) { case RS_ROW_NUM: return pg_fetch_row($this->result, $this->row - 1); break; case RS_ROW_ASSOC: return pg_fetch_assoc($this->result, $this->row - 1); break; case RS_ROW_BOTH: return pg_fetch_array($this->result, $this->row - 1); break; } return FALSE; } /** * Go to a row int the RecordSet. * * @param int $row Row to go to. * @return bool Returns TRUE on success, FALSE if failed. */ function SetRow($row = 0) { $this->row = $row; return TRUE; } /** * Get all rows from the RecordSet. * * @param int $type Type of array to return (RS_ROW_NUM | RS_ROW_ASSOC | RS_ROW_BOTH). * @return array Returns all the rows from the RecordSet. */ function All($type = RS_ROW_ASSOC) { $rows = array(); $this->SetRow(); while ($row = $this->Row(-1, $type)) $rows[] = $row; return $rows; } /** * Get the last auto-generated ID from the RecordSet. * * @return int Returns the last auto-generated ID from the RecordSet. */ function LastId() { return pg_last_oid($this->result); } /** * Get the last error message from the RecordSet. * * @return string Returns a string describing the last error that occurred in the RecordSet. */ function Error() { return pg_result_error($this->result); } /** * Close the RecordSet and free the memory. * * @return bool Returns TRUE if the RecordSet was closed, FALSE if it failed. */ function Close() { return pg_free_result($this->result); } } ?> --- NEW FILE: DB_PostgreSQL.class.php --- <?php /** * File for class DB_PostgreSQL. * * @package Vortex * @subpackage DB * @author Thiago Ramon Gonçalves Montoya * @copyright Copyright 2005, Thiago Ramon Gonçalves Montoya * @license http://opensource.org/licenses/lgpl-license.php GNU Lesser General Public License */ /** Require the base class */ require_once('DB_Base.class.php'); /** Require the RecordSet class header. */ require_once('RS_PostgreSQL.class.php'); /** * Class for PostgreSQL database connection. * * @package Vortex * @subpackage DB */ class DB_PostgreSQL extends DB_Base { /** * Open a new connection to the database if not already connected. * * @param bool $persist Open a persistent connection? * @return bool Returns TRUE if the connection was successfully established, FALSE if an error occurred. */ function Connect($persist=FALSE) { if (!is_null($this->link)) return FALSE; if ($persist) { $this->link = pg_pconnect("host={$this->server} user={$this->user} password={$this->pw} dbname={$this->db}"); } else { $this->link = pg_connect("host={$this->server} user={$this->user} password={$this->pw} dbname={$this->db}", PGSQL_CONNECT_FORCE_NEW); } if ($this->link !== FALSE) { return TRUE; } $this->link = NULL; return FALSE; } /** * Execute a query at the database. * * @param string $sql Query to run. * @return RS_Base Returns a RecordSet object if there is a result to the query or TRUE if it was successfull, FALSE if a error occurred. */ function &Query($sql) { $rs = pg_query($this->link, $sql); if ($rs === FALSE) { return FALSE; } return new RS_PostgreSQL($this, $rs); } /** * Close the connection to the database if still connected. * * @return bool Returns TRUE if the connection was closed, FALSE if it failed. */ function Close() { if (pg_close($this->link)) { $this->link = NULL; return TRUE; } return FALSE; } /** * Get the last error message from the connection. * * @return string Returns a string describing the last error that occurred in the connection. */ function Error() { return pg_last_error($this->link); } /** * Transactions: Begin a new transaction. * * @return bool Returns TRUE if the new transaction began, FALSE if it failed. */ function Begin() { return $this->Query('BEGIN'); } /** * Transactions: Commit a transaction. * * @return bool Returns TRUE if the transaction was commited, FALSE if it failed. */ function Commit() { return $this->Query('COMMIT'); } /** * Transactions: Rollback a transaction. * * @return bool Returns TRUE if the transaction was cancelled, FALSE if it failed. */ function Rollback() { return $this->Query('ROLLBACK'); } /** * Process a string for safe use in a database insertion. * * @param string $data The string to be processed. * @return string Returns the processed string. */ function AddSlashes($data) { return pg_escape_string($data); } } ?> |