cvs: pear /MDB pgsql.php

From: Date: Wed, 17 Jul 2002 14:40:32 +0000
Subject: cvs: pear /MDB pgsql.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-5167@lists.php.net to get a copy of this message
lsmith Wed Jul 17 10:40:32 2002 EDT Modified files: /pear/MDB pgsql.php Log: API changes: changed most of the MDB methods to either return data or an error (rather than returning data in a var that was passed by reference) changed method names from get*FieldValue to get*Value removed a couple of redundant methods PHPDoc comment improvements reordered some methods fixed a bug in connect() that was causing an infinite loop when transaction support was enabled

Index: pear/MDB/pgsql.php diff -u pear/MDB/pgsql.php:1.7 pear/MDB/pgsql.php:1.8 --- pear/MDB/pgsql.php:1.7 Wed Jul 10 06:56:41 2002 +++ pear/MDB/pgsql.php Wed Jul 17 10:40:32 2002 @@ -41,20 +41,17 @@ // +----------------------------------------------------------------------+ // | Author: Paul Cooper <pgc@ucecom.com> | // +----------------------------------------------------------------------+ -// -// $Id: pgsql.php,v 1.7 2002/07/10 10:56:41 lsmith Exp $ -// + +// $Id: pgsql.php,v 1.8 2002/07/17 14:40:32 lsmith Exp $ + // MDB postgresql driver. -// if (!defined("MDB_PGSQL_INCLUDED")) { define("MDB_PGSQL_INCLUDED", 1); +require_once (dirname(__FILE__) . "/common.php"); -require_once "common.php"; - -class MDB_pgsql extends MDB_common -{ +class MDB_driver_pgsql extends MDB_common { var $connection = 0; var $connected_host; var $connected_port; @@ -67,21 +64,21 @@ var $escape_quotes = "\\"; var $manager_class_name = "MDB_manager_pgsql_class"; var $manager_include = "manager_pgsql.php"; - var $manager_included_constant = "MDB_MANAGER_PGSQL_INCLUDED"; + var $manager_included_constant = "MDB_MANAGER_PGSQL_INCLUDED"; - // }}} + // }}} // {{{ constructor /** * Constructor - */ - function MDB_pgsql() + **/ + function MDB_driver_pgsql() { $this->MDB_common(); $this->phptype = 'pgsql'; $this->dbsyntax = 'pgsql'; if (!function_exists("pg_connect")) { - return("PostgreSQL support is not available in this PHP configuration"); + return ("PostgreSQL support is not available in this PHP configuration"); } $this->supported["Sequences"] = 1; $this->supported["Indexes"] = 1; @@ -93,31 +90,26 @@ $this->supported["LOBs"] = 1; $this->supported["Replace"] = 1; $this->supported["SubSelects"] = 1; - - if (function_exists("pg_cmdTuples")) - { + + if (function_exists("pg_cmdTuples")) { $connection = $this->_doConnect("template1", 0); - if (!MDB::isError($connection)) - { - if(($result = @pg_Exec($connection,"BEGIN"))) - { + if (!MDB::isError($connection)) { + if (($result = @pg_Exec($connection, "BEGIN"))) { $error_reporting = error_reporting(63); @pg_cmdTuples($result); - if (!isset($php_errormsg) - || strcmp($php_errormsg, "This compilation does not support pg_cmdtuples()")) - { + if (!isset($php_errormsg) || strcmp($php_errormsg, "This compilation does not support pg_cmdtuples()")) { $this->supported["AffectedRows"] = 1; } error_reporting($error_reporting); } else { - $err = $this->raiseError(DB_ERROR, NULL, NULL, "Setup: ".pg_ErrorMessage($connection)); + $err = $this->raiseError(DB_ERROR, NULL, NULL, "Setup: " . pg_ErrorMessage($connection)); } pg_Close($connection); } else { $err = $this->raiseError(DB_ERROR, NULL, NULL, "Setup: could not execute BEGIN"); } if (MDB::isError($err)) { - return($err); + return ($err); } } @@ -126,63 +118,150 @@ $this->errorcode_map = array(); } + // }}} + // {{{ pgsqlRaiseError() + function pgsqlRaiseError($errno = NULL) + { + $native = $this->errorNative(); + if ($errno === NULL) { + $err = $this->errorCode($native); + } else { + $err = $errno; + } + return $this->raiseError($err, NULL, NULL, NULL, $native); + } + + // }}} + // {{{ autoCommit() + /** + * Define whether database changes done on the database be automatically + * committed. This function may also implicitly start or end a transaction. + * + * @param boolean $auto_commit flag that indicates whether the database + * changes should be committed right after executing every query + * statement. If this argument is 0 a transaction implicitly started. + * Otherwise, if a transaction is in progress it is ended by committing + * any database changes that were pending. + * + * @access public + * + * @return mixed DB_OK on success, a DB error on failure + */ + function autoCommit($auto_commit) + { + if (((!$this->auto_commit) == (!$auto_commit))) { + return (DB_OK); + } + if ($this->connection) { + if (!$this->_doQuery($auto_commit ? "END" : "BEGIN")) + return (0); + } + $this->auto_commit = $auto_commit; + return ($this->registerTransactionShutdown($auto_commit)); + } + + // }}} + // {{{ commit() + /** + * Commit the database changes done during a transaction that is in + * progress. This function may only be called when auto-committing is + * disabled, otherwise it will fail. Therefore, a new transaction is + * implicitly started after committing the pending changes. + * + * @access public + * + * @return mixed DB_OK on success, a DB error on failure + */ + function commit() + { + if ($this->auto_commit) { + return ($this->raiseError(DB_ERROR, '', '', "Commit: transaction changes are being auto commited")); + } + return ($this->_doQuery("COMMIT") && $this->_doQuery("BEGIN")); + } // }}} + // {{{ rollback() + /** + * Cancel any database changes done during a transaction that is in + * progress. This function may only be called when auto-committing is + * disabled, otherwise it will fail. Therefore, a new transaction is + * implicitly started after canceling the pending changes. + * + * @access public + * + * @return mixed DB_OK on success, a DB error on failure + */ + function rollback() + { + if ($this->auto_commit) { + return ($this->raiseError(DB_ERROR, '', '', "Rollback: transactions can not be rolled back when changes are auto commited")); + } + return ($this->_doQuery("ROLLBACK") && $this->_doQuery("BEGIN")); + } + + // }}} // {{{ _doConnect /** * Does the grunt work of connecting to the database * - * @access private + * @access private * @return mixed connection resource on success, MDB_Error on failure - */ + **/ function _doConnect($database_name, $persistent) { - $function=($persistent ? "pg_pconnect" : "pg_connect"); - if(!function_exists($function)) - return($this->raiseError(DB_ERROR_UNSUPPORTED, NULL, NULL, "doConnect: PostgreSQL support is not available in this PHP configuration")); - $port = (IsSet($this->options["port"]) ? $this->options["port"] : ""); - $connect_string = "dbname=$database_name host=" . $this->host; + $function = ($persistent ? "pg_pconnect" : "pg_connect"); + if (!function_exists($function)) { + return ($this->raiseError(DB_ERROR_UNSUPPORTED, NULL, NULL, "doConnect: PostgreSQL support is not available in this PHP configuration")); + } + $port = (isset($this->options["port"]) ? $this->options["port"] : ""); + $connect_string = "dbname=".$database_name; + if ($this->host != "") { + $connect_string .= " host=".$this->host; + } if ($port != "") { - $connect_string .= " port=" . strval($port); + $connect_string .= " port=".strval($port); } if ($this->user != "") { - $connect_string .= " user=" . $this->user; + $connect_string .= " user=".$this->user; } if ($this->password != "") { - $connect_string .= " password=" . $this->password; + $connect_string .= " password=".$this->password; } - if(($connection = @$function($connect_string)) > 0) { - return($connection); + if (($connection = @$function($connect_string)) > 0) { + return ($connection); } - - return($this->raiseError(DB_ERROR_CONNECT_FAILED, '', '', "doConnect: " . IsSet($php_errormsg) ? $php_errormsg : "Could not connect to PostgreSQL server")); + return ($this->raiseError(DB_ERROR_CONNECT_FAILED, '', '', "doConnect: " . isset($php_errormsg) ? $php_errormsg : "Could not connect to PostgreSQL server")); } - // }}} - // {{{ Connect to the database + // }}} + // {{{ connect() /** * Connect to the database * * @return TRUE on success, MDB_Error on failure - */ + **/ function connect() { - $port = (IsSet($this->options["port"]) ? $this->options["port"] : ""); - if($this->connection != 0) { - if(!strcmp($this->connected_host, $this->host) - && !strcmp($this->connected_port, $port) - && !strcmp($this->selected_database, $this->database_name) - && ($this->opened_persistent == $this->options['persistent'])) - return(1); + $port = (isset($this->options["port"]) ? $this->options["port"] : ""); + if ($this->connection != 0) { + if (!strcmp($this->connected_host, $this->host) + && !strcmp($this->connected_port, $port) + && !strcmp($this->selected_database, $this->database_name) + && ($this->opened_persistent == $this->options['persistent'])) + { + return (DB_OK); + } pg_Close($this->connection); $this->affected_rows = -1; $this->connection = 0; } $this->connection = $this->_doConnect($this->database_name, $this->options['persistent']); - if(MDB::isError($this->connection)) { + if (MDB::isError($this->connection)) { return $this->connection; } - if(!$this->auto_commit && !MDB::isError($trans_result = $this->query("BEGIN"))) { + + if (!$this->auto_commit && MDB::isError($trans_result = $this->_doQuery("BEGIN"))) { pg_Close($this->connection); $this->connection = 0; $this->affected_rows = -1; @@ -192,19 +271,19 @@ $this->connected_port = $port; $this->selected_database = $this->database_name; $this->opened_persistent = $this->options['persistent']; - return(DB_OK); + return (DB_OK); } - // }}} - // {{{ Close the database connection + // }}} + // {{{ close() /** * Close the database connection - */ + **/ function close() { - if($this->connection != 0) { - if(!$this->auto_commit) { - $this->query("END"); + if ($this->connection != 0) { + if (!$this->auto_commit) { + $this->_doQuery("END"); } pg_Close($this->connection); $this->connection = 0; @@ -212,39 +291,36 @@ } } - // }}} - // {{{ Execute a query + // }}} + // {{{ _doQuery() /** * Execute a query - * * @param string $query the SQL query * * @return mixed result identifier if query executed, else MDB_error - * @access private - */ + * @access private + **/ function _doQuery($query) { - if(($result = @pg_Exec($this->connection, $query))) { - $this->affected_rows=(IsSet($this->supported["AffectedRows"]) ? pg_cmdTuples($result) : -1); + if (($result = @pg_Exec($this->connection, $query))) { + $this->affected_rows = (isset($this->supported["AffectedRows"]) ? pg_cmdTuples($result) : -1); } else { $error = pg_ErrorMessage($this->connection); return $this->raiseError(NULL, NULL, NULL, "query: $query:" . $error, $error); } - return($result); + return ($result); } - // }}} - // {{{ Execute a query + // }}} + // {{{ query() /** * Execute a query - * * @param string $query the SQL query - * @param array $types array that contains the types of the columns in - * the result set - * + * @param array $types array that contains the types of the columns in + * the result set * @return mixed result identifier if query executed, else MDB_error - * @access public - */ + * @access public + **/ function query($query, $types = NULL) { $ismanip = MDB::isManip($query); @@ -252,33 +328,30 @@ $limit = $this->selected_row_limit; $this->first_selected_row = $this->selected_row_limit = 0; $connected = $this->connect(); - if(MDB::isError($connected)) { + if (MDB::isError($connected)) { return $connected; } if (!$ismanip && $limit > 0 && - substr(strtolower(ltrim($query)), - 0, 6) == "select") - { - if($this->auto_commit && MDB::isError($this->_doQuery("BEGIN"))) { + substr(strtolower(ltrim($query)), + 0, 6) == "select") { + if ($this->auto_commit && MDB::isError($this->_doQuery("BEGIN"))) { return $this->raiseError(DB_ERROR); } $result = $this->_doQuery("DECLARE select_cursor SCROLL CURSOR FOR $query"); - if(!MDB::isError($result)) - { - if($first > 0 && MDB::isError($result = $this->_doQuery("MOVE FORWARD $first FROM select_cursor"))) { + if (!MDB::isError($result)) { + if ($first > 0 && MDB::isError($result = $this->_doQuery("MOVE FORWARD $first FROM select_cursor"))) { $this->freeResult($result); return $result; } - if(MDB::isError($result = $this->_doQuery("FETCH FORWARD $limit FROM select_cursor"))) - { + if (MDB::isError($result = $this->_doQuery("FETCH FORWARD $limit FROM select_cursor"))) { $this->freeResult($result); return $result; } } else { return $result; } - if($this->auto_commit && MDB::isError($result2 = $this->_doQuery("END"))) { + if ($this->auto_commit && MDB::isError($result2 = $this->_doQuery("END"))) { $this->freeResult($result); return $result2; } @@ -292,9 +365,7 @@ if ($ismanip) { $this->affected_rows = @pg_cmdtuples($result); return DB_OK; - } elseif (preg_match('/^\s*\(?\s*SELECT\s+/si', $query) && - !preg_match('/^\s*\(?\s*SELECT\s+INTO\s/si', $query)) - { + } elseif (preg_match('/^\s*\(?\s*SELECT\s+/si', $query) && !preg_match('/^\s*\(?\s*SELECT\s+INTO\s/si', $query)) { $this->highest_fetched_row[$result] = -1; return $result; } else { @@ -304,307 +375,734 @@ return $this->raiseError(DB_ERROR); } + // }}} + // {{{ endOfResult() + /** + * check if the end of the result set has been reached + * + * @param resource $result result identifier + * + * @return mixed TRUE or FALSE on sucess, a DB error on failure + * + * @access public + */ function endOfResult($result) { - if(!isset($this->highest_fetched_row[$result])) - { - return $this->RaiseError(DB_ERROR, '', '', "End of result attempted to check the end of an unknown result"); - } - return($this->highest_fetched_row[$result]>=$this->numRows($result)- 1); - } - - function fetchResult($result, $row, $field) - { - $this->highest_fetched_row[$result] = max($this->highest_fetched_row[$result], $row); - return(pg_result($result, $row, $field)); + if (!isset($this->highest_fetched_row[$result])) { + return $this->RaiseError(DB_ERROR, '', '', "End of result attempted to check the end of an unknown result"); + } + return ($this->highest_fetched_row[$result] >= $this->numRows($result) - 1); } - function fetchResultArray($result,&$array,$row) + // }}} + // {{{ fetch() + /** + * fetch value from a result set + * + * @param resource $result result identifier + * @param int $row number of the row where the data can be found + * @param int $field field number where the data can be found + * + * @return mixed string on success, a DB error on failure + * + * @access public + */ + function fetch($result, $row, $field) { - if(!($array = pg_fetch_row($result,$row))) { - return($this->SetError("Fetch result array", pg_ErrorMessage($this->connection))); - } $this->highest_fetched_row[$result] = max($this->highest_fetched_row[$result], $row); - return($this->ConvertResultRow($result,$array)); + return (pg_result($result, $row, $field)); } + // }}} + // {{{ retrieveLob() + /** + * fetch a float value from a result set + * + * @param int $lob handle to a lob created by the createLob() function + * + * @return mixed DB_Ok on success, a DB error on failure + * + * @access public + */ function retrieveLOB($lob) { - if(!IsSet($this->lobs[$lob])) { - return($this->SetError("Retrieve LOB","it was not specified a valid lob")); + if (!isset($this->lobs[$lob])) { + return ($this->SetError("Retrieve LOB", "it was not specified a valid lob")); } - if(!IsSet($this->lobs[$lob]["Value"])) { - if($this->auto_commit) { - if(!@pg_Exec($this->connection, "BEGIN")) { - return($this->SetError("Retrieve LOB", pg_ErrorMessage($this->connection))); + if (!isset($this->lobs[$lob]["Value"])) { + if ($this->auto_commit) { + if (!@pg_Exec($this->connection, "BEGIN")) { + return ($this->SetError("Retrieve LOB", pg_ErrorMessage($this->connection))); } $this->lobs[$lob]["InTransaction"] = 1; } $this->lobs[$lob]["Value"] = $this->FetchResult($this->lobs[$lob]["Result"], $this->lobs[$lob]["Row"], $this->lobs[$lob]["Field"]); - if(!($this->lobs[$lob]["Handle"] = pg_loopen($this->connection, $this->lobs[$lob]["Value"], "r"))) { - if(IsSet($this->lobs[$lob]["InTransaction"])) { + if (!($this->lobs[$lob]["Handle"] = pg_loopen($this->connection, $this->lobs[$lob]["Value"], "r"))) { + if (isset($this->lobs[$lob]["InTransaction"])) { @pg_Exec($this->connection, "END"); UnSet($this->lobs[$lob]["InTransaction"]); } Unset($this->lobs[$lob]["Value"]); - return($this->SetError("Retrieve LOB",pg_ErrorMessage($this->connection))); + return ($this->SetError("Retrieve LOB", pg_ErrorMessage($this->connection))); } } - return(1); + return (DB_OK); } + // }}} + // {{{ endOfResultLob() + /** + * Determine whether it was reached the end of the large object and + * therefore there is no more data to be read for the its input stream. + * + * @param int $lob handle to a lob created by the createLob() function + * + * @return mixed TRUE or FALSE on success, a DB error on failure + * + * @access public + */ function endOfResultLOB($lob) { - if(!$this->retrieveLOB($lob)) { - return(0); + if (!$this->retrieveLOB($lob)) { + return (0); } - return(IsSet($this->lobs[$lob]["EndOfLOB"])); + return (isset($this->lobs[$lob]["EndOfLOB"])); } + // }}} + // {{{ readResultLob() + /** + * Read data from large object input stream. + * + * @param int $lob handle to a lob created by the createLob() function + * @param blob $data reference to a variable that will hold data to be + * read from the large object input stream + * @param int $length integer value that indicates the largest ammount of + * data to be read from the large object input stream. + * + * @return mixed length on success, a DB error on failure + * + * @access public + */ function readResultLOB($lob, &$data, $length) { - if(!$this->RetrieveLOB($lob)) { - return(-1); + if (!$this->RetrieveLOB($lob)) { + return (-1); } $data = pg_loread($this->lobs[$lob]["Handle"], $length); - if(GetType($data) != "string") { + if (GetType($data) != "string") { $this->SetError("Read Result LOB", pg_ErrorMessage($this->connection)); - return(-1); + return (-1); } - if(($length = strlen($data)) == 0) { + if (($length = strlen($data)) == 0) { $this->lobs[$lob]["EndOfLOB"] = 1; } - return($length); + return ($length); } + // }}} + // {{{ destroyResultLob() + /** + * Free any resources allocated during the lifetime of the large object + * handler object. + * + * @param int $lob handle to a lob created by the createLob() function + * + * @access public + */ function destroyResultLOB($lob) { - if(IsSet($this->lobs[$lob])) { - if(IsSet($this->lobs[$lob]["Value"])) { + if (isset($this->lobs[$lob])) { + if (isset($this->lobs[$lob]["Value"])) { pg_loclose($this->lobs[$lob]["Handle"]); - if(IsSet($this->lobs[$lob]["InTransaction"])) { + if (isset($this->lobs[$lob]["InTransaction"])) { @pg_Exec($this->connection, "END"); } } - $this->lobs[$lob]=""; + $this->lobs[$lob] = ""; } } - function fetchCLOBResult($result, $row, $field) + // }}} + // {{{ fetchClobResult() + /** + * fetch a clob value from a result set + * + * @param resource $result result identifier + * @param int $row number of the row where the data can be found + * @param int $field field number where the data can be found + * + * @return mixed content of the specified data cell, a DB error on failure, + * a DB error on failure + * + * @access public + */ + function fetchClobResult($result, $row, $field) { - return($this->fetchLOBResult($result, $row, $field)); + return ($this->fetchLOBResult($result, $row, $field)); } + // }}} + // {{{ fetchBlobResult() + /** + * fetch a blob value from a result set + * + * @param resource $result result identifier + * @param int $row number of the row where the data can be found + * @param int $field field number where the data can be found + * + * @return mixed content of the specified data cell, a DB error on failure + * + * @access public + */ function fetchBLOBResult($result, $row, $field) { - return($this->FetchLOBResult($result, $row, $field)); + return ($this->FetchLOBResult($result, $row, $field)); } + // }}} + // {{{ resultIsNull() + /** + * Determine whether the value of a query result located in given row and + * field is a NULL. + * + * @param resource $result result identifier + * @param int $row number of the row where the data can be found + * @param int $field field number where the data can be found + * + * @return mixed TRUE or FALSE on success, a DB error on failure + * + * @access public + */ function resultIsNull($result, $row, $field) { $this->highest_fetched_row[$result] = max($this->highest_fetched_row[$result], $row); - return(@pg_FieldIsNull($result, $row, $field)); + return (@pg_FieldIsNull($result, $row, $field)); } - function numberOfRows($result) + // }}} + // {{{ numRows() + /** + * returns the number of rows in a result object + * + * @param object DB_Result the result object to check + * + * @return mixed DB_Error or the number of rows + * + * @access public + */ + function numRows($result) { - return(pg_numrows($result)); + return (pg_numrows($result)); } + // }}} + // {{{ freeResult() + /** + * Free the internal resources associated with $result. + * + * @param $result result identifier + * + * @access public + * + * @return bool TRUE on success, FALSE if $result is invalid + */ function freeResult($result) { UnSet($this->highest_fetched_row[$result]); UnSet($this->columns[$result]); UnSet($this->result_types[$result]); - return(pg_freeresult($result)); + return (pg_freeresult($result)); } + // }}} + // {{{ _standaloneQuery() + /** + * + * + * @param string $query + * + * @access private + */ function _standaloneQuery($query) { - if(($connection = $this->_doConnect("template1", 0)) == 0) { - return(0); + if (($connection = $this->_doConnect("template1", 0)) == 0) { + return (0); } - if(!($success= @pg_Exec($connection, $query))) { + if (!($success = @pg_Exec($connection, $query))) { $this->SetError("Standalone query", pg_ErrorMessage($connection)); } pg_Close($connection); - return($success); + return ($success); } - function getTextFieldTypeDeclaration($name, &$field) + // }}} + // {{{ getTextDeclaration() + /** + * Obtain DBMS specific SQL code portion needed to declare an text type + * field to be used in statements like CREATE TABLE. + * + * @param string $name name the field to be declared. + * @param string $field associative array with the name of the properties + * of the field being declared as array indexes. Currently, the types + * of supported field properties are as follows: + * + * length + * Integer value that determines the maximum length of the text + * field. If this argument is missing the field should be + * declared to have the longest length allowed by the DBMS. + * + * default + * Text value to be used as default for this field. + * + * notNULL + * Boolean flag that indicates whether this field is constrained + * to not be set to NULL. + * + * @access public + * + * @return string DBMS specific SQL code portion that should be used to + * declare the specified field. + */ + function getTextDeclaration($name, &$field) { - return((IsSet($field["length"]) ? "$name VARCHAR (".$field["length"].")" : "$name TEXT").(IsSet($field["default"]) ? " DEFAULT '".$field["default"]."'" : "").(IsSet($field["notnull"]) ? " NOT NULL" : "")); + return ((isset($field["length"]) ? "$name VARCHAR (" . $field["length"] . ")" : "$name TEXT") . (isset($field["default"]) ? " DEFAULT '" . $field["default"] . "'" : "") . (isset($field["notNULL"]) ? " NOT NULL" : "")); } - function getCLOBFieldTypeDeclaration($name, &$field) + // }}} + // {{{ getCLOBDeclaration() + /** + * Obtain DBMS specific SQL code portion needed to declare an character + * large object type field to be used in statements like CREATE TABLE. + * + * @param string $name name the field to be declared. + * @param string $field associative array with the name of the properties + * of the field being declared as array indexes. Currently, the types + * of supported field properties are as follows: + * + * length + * Integer value that determines the maximum length of the large + * object field. If this argument is missing the field should be + * declared to have the longest length allowed by the DBMS. + * + * notNULL + * Boolean flag that indicates whether this field is constrained + * to not be set to NULL. + * + * @access public + * + * @return string DBMS specific SQL code portion that should be used to + * declare the specified field. + */ + function getCLOBDeclaration($name, &$field) { - return("$name OID" . (IsSet($field["notnull"]) ? " NOT NULL" : "")); + return ("$name OID" . (isset($field["notNULL"]) ? " NOT NULL" : "")); } - function getBLOBFieldTypeDeclaration($name, &$field) + // }}} + // {{{ getBLOBDeclaration() + /** + * Obtain DBMS specific SQL code portion needed to declare an binary large + * object type field to be used in statements like CREATE TABLE. + * + * @param string $name name the field to be declared. + * @param string $field associative array with the name of the properties + * of the field being declared as array indexes. Currently, the types + * of supported field properties are as follows: + * + * length + * Integer value that determines the maximum length of the large + * object field. If this argument is missing the field should be + * declared to have the longest length allowed by the DBMS. + * + * notNULL + * Boolean flag that indicates whether this field is constrained + * to not be set to NULL. + * + * @access public + * + * @return string DBMS specific SQL code portion that should be used to + * declare the specified field. + */ + function getBLOBDeclaration($name, &$field) { - return("$name OID" . (IsSet($field["notnull"]) ? " NOT NULL" : "")); + return ("$name OID" . (isset($field["notNULL"]) ? " NOT NULL" : "")); } - function getDateFieldTypeDeclaration($name, &$field) + // }}} + // {{{ getDateDeclaration() + /** + * Obtain DBMS specific SQL code portion needed to declare a date type + * field to be used in statements like CREATE TABLE. + * + * @param string $name name the field to be declared. + * @param string $field associative array with the name of the properties + * of the field being declared as array indexes. Currently, the types + * of supported field properties are as follows: + * + * default + * Date value to be used as default for this field. + * + * notNULL + * Boolean flag that indicates whether this field is constrained + * to not be set to NULL. + * + * @access public + * + * @return string DBMS specific SQL code portion that should be used to + * declare the specified field. + */ + function getDateDeclaration($name, &$field) { - return($name." DATE".(IsSet($field["default"]) ? " DEFAULT '".$field["default"]."'" : "").(IsSet($field["notnull"]) ? " NOT NULL" : "")); + return ($name . " DATE" . (isset($field["default"]) ? " DEFAULT '" . $field["default"] . "'" : "") . (isset($field["notNULL"]) ? " NOT NULL" : "")); } - function getTimeFieldTypeDeclaration($name, &$field) + // }}} + // {{{ getTimeDeclaration() + /** + * Obtain DBMS specific SQL code portion needed to declare a time + * field to be used in statements like CREATE TABLE. + * + * @param string $name name the field to be declared. + * @param string $field associative array with the name of the properties + * of the field being declared as array indexes. Currently, the types + * of supported field properties are as follows: + * + * default + * Time value to be used as default for this field. + * + * notNULL + * Boolean flag that indicates whether this field is constrained + * to not be set to NULL. + * + * @access public + * + * @return string DBMS specific SQL code portion that should be used to + * declare the specified field. + */ + function getTimeDeclaration($name, &$field) { - return($name." TIME".(IsSet($field["default"]) ? " DEFAULT '".$field["default"]."'" : "").(IsSet($field["notnull"]) ? " NOT NULL" : "")); + return ($name . " TIME" . (isset($field["default"]) ? " DEFAULT '" . $field["default"] . "'" : "") . (isset($field["notNULL"]) ? " NOT NULL" : "")); } - function getFloatFieldTypeDeclaration($name, &$field) + // }}} + // {{{ getFloatDeclaration() + /** + * Obtain DBMS specific SQL code portion needed to declare a float type + * field to be used in statements like CREATE TABLE. + * + * @param string $name name the field to be declared. + * @param string $field associative array with the name of the properties + * of the field being declared as array indexes. Currently, the types + * of supported field properties are as follows: + * + * default + * Float value to be used as default for this field. + * + * notNULL + * Boolean flag that indicates whether this field is constrained + * to not be set to NULL. + * + * @access public + * + * @return string DBMS specific SQL code portion that should be used to + * declare the specified field. + */ + function getFloatDeclaration($name, &$field) { - return("$name FLOAT8 ".(IsSet($field["default"]) ? " DEFAULT ".$this->GetFloatFieldValue($field["default"]) : "").(IsSet($field["notnull"]) ? " NOT NULL" : "")); + return ("$name FLOAT8 " . (isset($field["default"]) ? " DEFAULT " . $this->getFloatValue($field["default"]) : "") . (isset($field["notNULL"]) ? " NOT NULL" : "")); } - function getDecimalFieldTypeDeclaration($name, &$field) + // }}} + // {{{ getDecimalDeclaration() + /** + * Obtain DBMS specific SQL code portion needed to declare a decimal type + * field to be used in statements like CREATE TABLE. + * + * @param string $name name the field to be declared. + * @param string $field associative array with the name of the properties + * of the field being declared as array indexes. Currently, the types + * of supported field properties are as follows: + * + * default + * Decimal value to be used as default for this field. + * + * notNULL + * Boolean flag that indicates whether this field is constrained + * to not be set to NULL. + * + * @access public + * + * @return string DBMS specific SQL code portion that should be used to + * declare the specified field. + */ + function getDecimalDeclaration($name, &$field) { - return("$name INT8 ".(IsSet($field["default"]) ? " DEFAULT ".$this->GetDecimalFieldValue($field["default"]) : "").(IsSet($field["notnull"]) ? " NOT NULL" : "")); + return ("$name INT8 " . (isset($field["default"]) ? " DEFAULT " . $this->getDecimalValue($field["default"]) : "") . (isset($field["notNULL"]) ? " NOT NULL" : "")); } - function getLOBFieldValue($prepared_query, $parameter, $lob, &$value) + // }}} + // {{{ getLobValue() + /** + * Convert a text value into a DBMS specific format that is suitable to + * compose query statements. + * + * @param resource $prepared_query query handle from prepare() + * @param $parameter + * @param $lob + * + * @access public + * + * @return string text string that represents the given argument value in + * a DBMS specific format. + */ + function getLobValue($prepared_query, $parameter, $lob) { - if(!$this->connect()) { - return(0); + if (!$this->connect()) { + return (0); } - if($this->auto_commit && !@pg_Exec($this->connection, "BEGIN")) { - return(0); + if ($this->auto_commit && !@pg_Exec($this->connection, "BEGIN")) { + return (0); } $success = 1; - if(($lo = pg_locreate($this->connection))) { - if(($handle=pg_loopen($this->connection,$lo,"w"))) { - while(!MetabaseEndOfLOB($lob)) { - if(MetabaseReadLOB($lob, $data, $this->options['lob_buffer_length']) < 0) { - $this->SetError("Get LOB field value", MetabaseLOBError($lob)); + if (($lo = pg_locreate($this->connection))) { + if (($handle = pg_loopen($this->connection, $lo, "w"))) { + while (!endOfLOB($lob)) { + if (readLOB($lob, $data, $this->options['lob_buffer_length']) < 0) { + $this->SetError("Get LOB field value", lobError($lob)); $success = 0; break; } - if(!pg_lowrite($handle, $data)) { + if (!pg_lowrite($handle, $data)) { $this->SetError("Get LOB field value", pg_ErrorMessage($this->connection)); $success = 0; break; } } pg_loclose($handle); - if($success) + if ($success) $value = strval($lo); } else { $this->SetError("Get LOB field value", pg_ErrorMessage($this->connection)); $success = 0; } - if(!$success) - pg_lounlink($this->connection,$lo); + if (!$success) + pg_lounlink($this->connection, $lo); } else { $this->SetError("Get LOB field value", pg_ErrorMessage($this->connection)); $success = 0; } - if($this->auto_commit) + if ($this->auto_commit) @pg_Exec($this->connection, "END"); - return($success); + return ($value); } - function getCLOBFieldValue($prepared_query, $parameter, $clob, &$value) + // }}} + // {{{ getClobValue() + /** + * Convert a text value into a DBMS specific format that is suitable to + * compose query statements. + * + * @param resource $prepared_query query handle from prepare() + * @param $parameter + * @param $clob + * + * @access public + * + * @return string text string that represents the given argument value in + * a DBMS specific format. + */ + function getClobValue($prepared_query, $parameter, $clob, &$value) { - return($this->GetLOBFieldValue($prepared_query, $parameter, $clob, $value)); + return ($this->getLobValue($prepared_query, $parameter, $clob, $value)); } - function freeCLOBValue($prepared_query, $clob, &$value, $success) + // }}} + // {{{ freeClobValue() + /** + * free a character large object + * + * @param resource $prepared_query query handle from prepare() + * @param string $blob + * @param string $value + * + * @access private + */ + function freeClobValue($prepared_query, $clob, &$value) { - if(!$success) { - pg_lounlink($this->connection,intval($value)); - } + pg_lounlink($this->connection, intval($value)); } - function getBLOBFieldValue($prepared_query, $parameter, $blob, &$value) + // }}} + // {{{ getBlobValue() + /** + * Convert a text value into a DBMS specific format that is suitable to + * compose query statements. + * + * @param resource $prepared_query query handle from prepare() + * @param $parameter + * @param $blob + * + * @access public + * + * @return string text string that represents the given argument value in + * a DBMS specific format. + */ + function getBlobValue($prepared_query, $parameter, $blob, &$value) { - return($this->GetLOBFieldValue($prepared_query, $parameter, $blob, $value)); + return ($this->getLobValue($prepared_query, $parameter, $blob, $value)); } - function freeBLOBValue($prepared_query, $blob, &$value, $success) + // }}} + // {{{ freeBlobValue() + /** + * free a binary large object + * + * @param resource $prepared_query query handle from prepare() + * @param string $blob + * @param string $value + * + * @access private + */ + function freeBlobValue($prepared_query, $blob, &$value) { - if(!$success) { - pg_lounlink($this->connection, intval($value)); - } + pg_lounlink($this->connection, intval($value)); } - function getFloatFieldValue($value) + // }}} + // {{{ getFloatValue() + /** + * Convert a text value into a DBMS specific format that is suitable to + * compose query statements. + * + * @param string $value text string value that is intended to be converted. + * + * @access public + * + * @return string text string that represents the given argument value in + * a DBMS specific format. + */ + function getFloatValue($value) { - return(!strcmp($value,"NULL") ? "NULL" : "$value"); + return (!strcmp($value, "NULL") ? "NULL" : "$value"); } - function getDecimalFieldValue($value) + // }}} + // {{{ getDecimalValue() + /** + * Convert a text value into a DBMS specific format that is suitable to + * compose query statements. + * + * @param string $value text string value that is intended to be converted. + * + * @access public + * + * @return string text string that represents the given argument value in + * a DBMS specific format. + */ + function getDecimalValue($value) { - return(!strcmp($value,"NULL") ? "NULL" : strval(round($value * $this->decimal_factor))); + return (!strcmp($value, "NULL") ? "NULL" : strval(round($value * $this->decimal_factor))); } - function getColumnNames($result, &$column_names) + // }}} + // {{{ getColumnNames() + + /** + * Retrieve the names of columns returned by the DBMS in a query result. + * + * @param resource $result result identifier + * + * @access public + * + * @return mixed associative array variable + * that holds the names of columns. The indexes of the array are + * the column names mapped to lower case and the values are the + * respective numbers of the columns starting from 0. Some DBMS may + * not return any columns when the result set does not contain any + * rows. + * a DB error on failure + */ + function getColumnNames($result) { - if(!IsSet($this->highest_fetched_row[$result])) { - return($this->SetError("Get Column Names", "it was specified an inexisting result set")); + if (!isset($this->highest_fetched_row[$result])) { + return ($this->SetError("Get Column Names", "it was specified an inexisting result set")); } - if(!IsSet($this->columns[$result])) { + if (!isset($this->columns[$result])) { $this->columns[$result] = array(); $columns = pg_numfields($result); for($column = 0; $column < $columns; $column++) - $this->columns[$result][strtolower(pg_fieldname($result, $column))] = $column; + $this->columns[$result][strtolower(pg_fieldname($result, $column))] = $column; } - $column_names = $this->columns[$result]; - return (DB_OK); + return ($this->columns[$result]); } - function numberOfColumns($result) + // }}} + // {{{ numCols() + /** + * Count the number of columns returned by the DBMS in a query result. + * + * @param resource $result result identifier + * + * @access public + * + * @return mixed integer value with the number of columns, a DB error + * on failure + */ + function numCols($result) { - if(!IsSet($this->highest_fetched_row[$result])) { + if (!isset($this->highest_fetched_row[$result])) { $this->SetError("Number of columns", "it was specified an inexisting result set"); - return(-1); + return (-1); } - return(pg_numfields($result)); + return (pg_numfields($result)); } - function convertResult(&$value, $type) + // }}} + // {{{ convertResult() + /** + * convert a value to a RDBMS indepdenant MDB type + * + * @param mixed $value value to be converted + * @param int $type constant that specifies which type to convert to + * + * @return mixed converted value or a DB error on failure + * + * @access public + */ + function convertResult($value, $type) { - switch($type) { - case METABASE_TYPE_BOOLEAN: - $value = (strcmp($value,"Y") ? 0 : 1); - return (DB_OK); - case METABASE_TYPE_DECIMAL: - $value = sprintf("%." . $this->options['decimal_places'] . "f", doubleval($value)/$this->decimal_factor); - return (DB_OK); - case METABASE_TYPE_FLOAT: - $value = doubleval($value); - return (DB_OK); - case METABASE_TYPE_DATE: - case METABASE_TYPE_TIME: - return (DB_OK); - case METABASE_TYPE_TIMESTAMP: - $value = substr($value, 0, strlen("YYYY-MM-DD HH:MM:SS")); - return (DB_OK); - default: - return($this->BaseConvertResult($value, $type)); + switch ($type) { + case METABASE_TYPE_BOOLEAN: + return (strcmp($value, "Y") ? 0 : 1); + case METABASE_TYPE_DECIMAL: + return (sprintf("%." . $this->options['decimal_places'] . "f", doubleval($value) / $this->decimal_factor)); + case METABASE_TYPE_FLOAT: + return doubleval($value); + case METABASE_TYPE_DATE: + return ($value); + case METABASE_TYPE_TIME: + return ($value); + case METABASE_TYPE_TIMESTAMP: + return substr($value, 0, strlen("YYYY-MM-DD HH:MM:SS")); + default: + return ($this->baseConvertResult($value, $type)); } } // }}} // {{{ nextId() - /** - * Get the next value in a sequence. + * returns the next free id of a sequence * - * We are using native PostgreSQL sequences. If a sequence does - * not exist, it will be created, unless $ondemand is false. + * @param string $seq_name name of the sequence + * @param boolean $ondemand when TRUE the seqence is + * automatic created, if it + * not exists * - * @access public - * @param string $name name of the sequence - * @param int $value reference to a var into - * which the Id will be stored - * @param bool $ondemand whether to create the sequence on demand - * @return a sequence integer, or a DB error + * @return mixed DB_Error or id */ - function nextId($seq_name, &$value, $ondemand = true) + function nextId($seq_name, $ondemand = TRUE) { $seqname = $this->getSequenceName($seq_name); $repeat = 0; @@ -612,175 +1110,131 @@ $this->pushErrorHandling(PEAR_ERROR_RETURN); $result = $this->query("SELECT NEXTVAL('${seqname}')"); $this->popErrorHandling(); - if ($ondemand && MDB::isError($result) && - $result->getCode() == DB_ERROR_NOSUCHTABLE) { + if ($ondemand && MDB::isError($result) && $result->getCode() == DB_ERROR_NOSUCHTABLE) { $repeat = 1; $result = $this->createSequence($seq_name); - if (DB::isError($result)) { + if (MDB::isError($result)) { return $this->raiseError($result); } } else { $repeat = 0; } - } while ($repeat); + } + while ($repeat); if (MDB::isError($result)) { return $this->raiseError($result); } $this->fetchInto($result, $arr, DB_FETCHMODE_ORDERED); $this->freeResult($result); - $value = $arr[0]; - return (DB_OK); + return ($arr[0]); } - function currId($name, &$value) + // }}} + // {{{ currId() + /** + * returns the current id of a sequence + * + * @param string $seq_name name of the sequence + * + * @return mixed DB_Error or id + */ + function currId($name) { $seqname = $this->getSequenceName($seq_name); - if(!($result = $this->query("SELECT last_value FROM $seqname"))) { - return(0); + if (!($result = $this->query("SELECT last_value FROM $seqname"))) { + return (0); } - if($this->numRows($result) == 0) { + if ($this->numRows($result) == 0) { $this->FreeResult($result); - return($this->SetError("Get sequence current value","could not find value in sequence table")); - } - $value = intval($this->fetchResult($result,0,0)); - $this->freeResult($result); - return (DB_OK); - } - - function autoCommit($auto_commit) - { - if(((!$this->auto_commit) == (!$auto_commit))) { - return (DB_OK); - } - if($this->connection) { - if(!$this->Query($auto_commit ? "END" : "BEGIN")) - return(0); - } - $this->auto_commit=$auto_commit; - return($this->registerTransactionShutdown($auto_commit)); - } - - function commit() - { - if($this->auto_commit) { - return($this->SetError("Commit transaction","transaction changes are being auto commited")); + return ($this->SetError("Get sequence current value", "could not find value in sequence table")); } - return($this->query("COMMIT") && $this->query("BEGIN")); - } - - function rollback() - { - if($this->auto_commit) { - return($this->SetError("Rollback transaction","transactions can not be rolled back when changes are auto commited")); - } - return($this->query("ROLLBACK") && $this->query("BEGIN")); - } - - function fetch($result, $row, $field) - { - $this->highest_fetched_row[$result] = max($this->highest_fetched_row[$result], $row); - $res = @pg_result($result, $row, $field); - if (!$res && $res != NULL) { - return $this->pgsqlRaiseError(); - } - return ($res); + $value = intval($this->fetchResult($result, 0, 0)); + $this->freeResult($result); + return ($value); } // }}} - // {{{ numCols() - + // {{{ fetch() /** - * Get the number of columns in a result set. + * fetch value from a result set * - * @param $result resource PostgreSQL result identifier + * @param resource $result result identifier + * @param int $row number of the row where the data can be found + * @param int $field field number where the data can be found * - * @return int the number of columns per row in $result + * @return mixed string on success, a DB error on failure + * + * @access public */ - function numCols($result) + function fetch($result, $row, $field) { - $cols = @pg_numfields($result); - if (!$cols) { + $this->highest_fetched_row[$result] = max($this->highest_fetched_row[$result], $row); + $res = @pg_result($result, $row, $field); + if (!$res && $res != NULL) { return $this->pgsqlRaiseError(); } - return $cols; - } - - // }}} - // {{{ pgsqlRaiseError() - - function pgsqlRaiseError($errno = null) - { - $native = $this->errorNative(); - if ($errno === null) { - $err = $this->errorCode($native); - } else { - $err = $errno; - } - return $this->raiseError($err, null, null, null, $native); + return ($res); } // }}} // {{{ tableInfo() - /** - * Returns information about a table or a result set + * returns meta data about the result set * - * NOTE: doesn't support table name and flags if called from a db_result + * @param resource $result result identifier + * @param mixed $mode depends on implementation * - * @param mixed $resource PostgreSQL result identifier or table name - * @param int $mode A valid tableInfo mode (DB_TABLEINFO_ORDERTABLE or - * DB_TABLEINFO_ORDER) + * @return array an nested array, or a DB error * - * @return array An array with all the information + * @access public */ - function tableInfo($result, $mode = null) + function tableInfo($result, $mode = NULL) { $count = 0; - $id = 0; - $res = array(); + $id = 0; + $res = array(); - /* + /** * depending on $mode, metadata returns the following values: - * - * - mode is false (default): + * + * - 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 - * + * [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'])) { ... - * + * ["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 - */ + * 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 + // if $result is a string, then we want information about a // table without a resultset - if (is_string($result)) { - $id = pg_exec($this->connection,"SELECT * FROM $result"); + $id = pg_exec($this->connection, "SELECT * FROM $result"); if (empty($id)) { return $this->pgsqlRaiseError(); } @@ -791,32 +1245,30 @@ } } - $count = @pg_numfields($id); + $count = @pg_numfields($id); // made this IF due to performance (one if is faster than $count if's) if (empty($mode)) { - - for ($i=0; $i<$count; $i++) { + for ($i = 0; $i < $count; $i++) { $res[$i]['table'] = (is_string($result)) ? $result : ''; - $res[$i]['name'] = @pg_fieldname ($id, $i); - $res[$i]['type'] = @pg_fieldtype ($id, $i); - $res[$i]['len'] = @pg_fieldsize ($id, $i); + $res[$i]['name'] = @pg_fieldname ($id, $i); + $res[$i]['type'] = @pg_fieldtype ($id, $i); + $res[$i]['len'] = @pg_fieldsize ($id, $i); $res[$i]['flags'] = (is_string($result)) ? $this->_pgFieldflags($id, $i, $result) : ''; } - } else { // full - $res["num_fields"]= $count; + $res["num_fields"] = $count; - for ($i=0; $i<$count; $i++) { + for ($i = 0; $i < $count; $i++) { $res[$i]['table'] = (is_string($result)) ? $result : ''; - $res[$i]['name'] = @pg_fieldname ($id, $i); - $res[$i]['type'] = @pg_fieldtype ($id, $i); - $res[$i]['len'] = @pg_fieldsize ($id, $i); + $res[$i]['name'] = @pg_fieldname ($id, $i); + $res[$i]['type'] = @pg_fieldtype ($id, $i); + $res[$i]['len'] = @pg_fieldsize ($id, $i); $res[$i]['flags'] = (is_string($result)) ? $this->_pgFieldFlags($id, $i, $result) : ''; - if ($mode & DB_TABLEINFO_ORDER) { + if ($mode &DB_TABLEINFO_ORDER) { $res['order'][$res[$i]['name']] = $i; } - if ($mode & DB_TABLEINFO_ORDERTABLE) { + if ($mode &DB_TABLEINFO_ORDERTABLE) { $res['ordertable'][$res[$i]['table']][$res[$i]['name']] = $i; } } @@ -829,35 +1281,21 @@ return $res; } - /** - * Get the number of rows in a result set. - * - * @param $result resource PostgreSQL result identifier - * - * @return int the number of rows in $result - */ - function numRows($result) - { - $rows = @pg_numrows($result); - if ($rows === null) { - return $this->pgsqlRaiseError(); - } - return $rows; - } - + // }}} // {{{ fetchInto() /** * Fetch a row and insert the data into an existing array. * - * @param $result PostgreSQL result identifier - * @param $array (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 + * @param resource $result result identifier + * @param int $fetchmode how the array data should be indexed + * @param int $rownum the row number to fetch * - * @return int DB_OK on success, a DB error code on failure + * @return int data array on success, a DB error on failure + * + * @access public */ - function fetchInto($result, &$array, $fetchmode = DB_FETCHMODE_DEFAULT, $rownum = NULL) + function fetchInto($result, $fetchmode = DB_FETCHMODE_DEFAULT, $rownum = NULL) { if ($rownum == NULL) { ++$this->highest_fetched_row[$result]; @@ -868,7 +1306,7 @@ if ($rownum + 1 > $this->numRows($result)) { return NULL; } - if ($fetchmode & DB_FETCHMODE_ASSOC) { + if ($fetchmode &DB_FETCHMODE_ASSOC) { $array = @pg_fetch_array($result, $rownum, PGSQL_ASSOC); } else { $array = @pg_fetch_row($result, $rownum); @@ -876,7 +1314,7 @@ if (!$array) { $errno = @pg_errormessage($this->connection); if (!$errno) { - if($this->options['autofree']) { + if ($this->options['autofree']) { $this->freeResult($result); } return NULL; @@ -886,42 +1324,39 @@ if (!$this->convertResultRow($result, $array)) { return $this->raiseError(); } - return DB_OK; + return ($array); } - // }}} + // }}} // {{{ _pgFieldFlags() - /** * Flags of a Field - * * @param int $resource PostgreSQL result identifier * @param int $num_field the field number - * - * @return string The flags of the field ("not_null", "default_xx", "primary_key", - * "unique" and "multiple_key" are supported) - * @access private - */ + * @return string The flags of the field ("not_NULL", "default_xx", "primary_key", + * "unique" and "multiple_key" are supported) + * @access private + **/ function _pgFieldFlags($resource, $num_field, $table_name) { $field_name = @pg_fieldname($resource, $num_field); - $result = pg_exec($this->connection, "SELECT f.attnotnull, f.atthasdef - FROM pg_attribute f, pg_class tab, pg_type typ - WHERE tab.relname = typ.typname - AND typ.typrelid = f.attrelid - AND f.attname = '$field_name' - AND tab.relname = '$table_name'"); + $result = pg_exec($this->connection, "SELECT f.attnotNULL, f.atthasdef + FROM pg_attribute f, pg_class tab, pg_type typ + WHERE tab.relname = typ.typname + AND typ.typrelid = f.attrelid + AND f.attname = '$field_name' + AND tab.relname = '$table_name'"); if (pg_numrows($result) > 0) { $row = pg_fetch_row($result, 0); - $flags = ($row[0] == 't') ? 'not_null ' : ''; + $flags = ($row[0] == 't') ? 'not_NULL ' : ''; if ($row[1] == 't') { $result = pg_exec($this->connection, "SELECT a.adsrc - FROM pg_attribute f, pg_class tab, pg_type typ, pg_attrdef a - WHERE tab.relname = typ.typname AND typ.typrelid = f.attrelid - AND f.attrelid = a.adrelid AND f.attname = '$field_name' - AND tab.relname = '$table_name'"); + FROM pg_attribute f, pg_class tab, pg_type typ, pg_attrdef a + WHERE tab.relname = typ.typname AND typ.typrelid = f.attrelid + AND f.attrelid = a.adrelid AND f.attname = '$field_name' + AND tab.relname = '$table_name'"); $row = pg_fetch_row($result, 0); $num = str_replace('\'', '', $row[0]); @@ -929,12 +1364,12 @@ } } $result = pg_exec($this->connection, "SELECT i.indisunique, i.indisprimary, i.indkey - FROM pg_attribute f, pg_class tab, pg_type typ, pg_index i - WHERE tab.relname = typ.typname - AND typ.typrelid = f.attrelid - AND f.attrelid = i.indrelid - AND f.attname = '$field_name' - AND tab.relname = '$table_name'"); + FROM pg_attribute f, pg_class tab, pg_type typ, pg_index i + WHERE tab.relname = typ.typname + AND typ.typrelid = f.attrelid + AND f.attrelid = i.indrelid + AND f.attname = '$field_name' + AND tab.relname = '$table_name'"); $count = pg_numrows($result); for ($i = 0; $i < $count ; $i++) { @@ -951,7 +1386,7 @@ return trim($flags); } - } + } ?>
« previous php.pear.cvs (#5167) next »