cox Mon Mar 26 15:31:50 2001 EDT Modified files: /php4/pear DB.php /php4/pear/DB pgsql.php Log: pgsql.php * connect() always use pg_connect string instead of the deprecated params mode * removed duplicated functions prepare() and execute() (now in common.php) * pgsqlRaiseError() always fills native error param on DB_error objs * added third param $rownum to fetchInto() so users can fetch also absolute row numbers * changed fetchRow() to use fetchInto() (can not erase, still used in get*() from common.php DB.php * added third param $rownum to fetchInto()/fetchRow() so users can fetch also absolute row numbers * changed fetchRow() to use fetchInto()
Index: php4/pear/DB.php diff -u php4/pear/DB.php:1.53 php4/pear/DB.php:1.54 --- php4/pear/DB.php:1.53 Fri Mar 23 23:00:44 2001 +++ php4/pear/DB.php Mon Mar 26 15:31:49 2001 @@ -17,7 +17,7 @@ // | Tomas V.V.Cox <[EMAIL PROTECTED]> | // +----------------------------------------------------------------------+ // -// $Id: DB.php,v 1.53 2001/03/24 07:00:44 ssb Exp $ +// $Id: DB.php,v 1.54 2001/03/26 23:31:49 cox Exp $ // // Database independent query interface. // @@ -135,7 +135,7 @@ /** * these are constants for the tableInfo-function * they are bitwised or'ed. so if there are more constants to be defined - * in the future, adjust DB_TABLEINFO_FULL accordingly + * in the future, adjust DB_TABLEINFO_FULL accordingly */ define('DB_TABLEINFO_ORDER', 1); @@ -579,29 +579,36 @@ } /** - * Fetch and return a row of data. + * Fetch and return a row of data (it uses fetchInto for that) + * @param $fetchmode format of fetched row array + * @param $rownum the absolute row number to fetch + * * @return array a row of data, or false on error */ - function fetchRow($fetchmode = DB_FETCHMODE_DEFAULT) + function fetchRow($fetchmode = DB_FETCHMODE_DEFAULT, $rownum=0) { - if ($fetchmode == DB_FETCHMODE_DEFAULT) { - $fetchmode = $this->dbh->fetchmode; + $res = $this->fetchInto ($arr, $fetchmode, $rownum); + if ($res !== DB_OK) { + return $res; } - return $this->dbh->fetchRow($this->result, $fetchmode); + return $arr; } /** * Fetch a row of data into an existing array. + * + * @param $arr reference to data array + * @param $fetchmode format of fetched row array + * @param $rownum the absolute row number to fetch * - * @param $arr reference to data array * @return int error code */ - function fetchInto(&$arr, $fetchmode = DB_FETCHMODE_DEFAULT) + function fetchInto(&$arr, $fetchmode = DB_FETCHMODE_DEFAULT, $rownum=0) { if ($fetchmode == DB_FETCHMODE_DEFAULT) { $fetchmode = $this->dbh->fetchmode; } - return $this->dbh->fetchInto($this->result, $arr, $fetchmode); + return $this->dbh->fetchInto($this->result, $arr, $fetchmode, $rownum=0); } /** Index: php4/pear/DB/pgsql.php diff -u php4/pear/DB/pgsql.php:1.31 php4/pear/DB/pgsql.php:1.32 --- php4/pear/DB/pgsql.php:1.31 Fri Mar 9 22:04:14 2001 +++ php4/pear/DB/pgsql.php Mon Mar 26 15:31:50 2001 @@ -39,11 +39,12 @@ var $prepare_tokens = array(); var $prepare_types = array(); var $transaction_opcount = 0; - var $numrows; - var $row; - var $affected; + var $dsn = array(); + var $row = array(); + var $numrows = array(); + var $affected = 0; var $autocommit = true; - var $dsn; + var $fetchmode = DB_FETCHMODE_ORDERED; // }}} // {{{ constructor @@ -60,9 +61,6 @@ ); $this->errorcode_map = array( ); - $this->numrows = array(); - $this->row = array(); - $this->affected = 0; } // }}} @@ -77,53 +75,32 @@ * * @return int DB_OK on success, a DB error code on failure */ - function connect($dsn, $persistent = false) + function connect($dsninfo, $persistent = false) { - if (is_array($dsn)) { - $dsninfo = &$dsn; - } else { - $dsninfo = DB::parseDSN($dsn); - } - if (!$dsninfo || !$dsninfo['phptype']) { - return $this->raiseError(); // XXX ERRORMSG - } $this->dsn = $dsninfo; - $dbhost = $dsninfo['hostspec'] ? $dsninfo['hostspec'] : 'unix'; - if ($dbhost == 'unix') { - $protocol = 'unix'; - } else { - $protocol = $dsninfo['protocol'] ? $dsninfo['protocol'] : 'tcp'; + $host = $dsninfo['hostspec']; + $connstr = ''; + if (($host !== false) && ($dsninfo['protocol'] != 'unix')){ + if (($pos=strpos(':', $host)) !== false) { + $dbhost = substr($host, 0, $pos); + $port = substr($host, $pos+1); + } else { + $dbhost = $host; + $port = '5432'; + } + $connstr="host=".$dsninfo['hostspec']." port=".$port; } - $user = $dsninfo['username']; - $pw = $dsninfo['password']; - $dbname = $dsninfo['database']; - $options = (!empty($dsninfo['options'])) ? $dsninfo['options'] : NULL; - $tty = (!empty($dsninfo['tty'])) ? $dsninfo['tty'] : NULL; - $port = (!empty($dsninfo['port'])) ? $dsninfo['port'] : '5432'; + $connstr .= $dsninfo['database'] ? " dbname=".$dsninfo['database'] : NULL; + $connstr .= $dsninfo['username'] ? " user=".$dsninfo['username'] : NULL; + $connstr .= $dsninfo['password'] ? " password=".$dsninfo['password'] : NULL; + $connstr .= (!empty($dsninfo['options'])) ? " options=".$dsninfo['options'] : +NULL; + $connstr .= (!empty($dsninfo['tty'])) ? " tty=".$dsninfo['tty'] : NULL; $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; - } + $conn = @$connect_function($connstr); if ($conn == false) { - return $this->raiseError(); // XXX ERRORMSG + return $this->raiseError(DB_ERROR_CONNECT_FAILED); } $this->connection = $conn; return DB_OK; @@ -188,7 +165,11 @@ UNLISTEN, UPDATE, VACUUM */ $this->row[$result] = 0; // reset the row counter. - $this->numrows[$result] = @pg_numrows($result); + $numrows = $this->numrows($result); + if (is_object($numrows)) { + return $numrows; + } + $this->numrows[$result] = $numrows; $this->affected = 0; return $result; } else { @@ -230,85 +211,61 @@ return $code; } } - //php_error(E_WARNING, get_class($this)."::errorCode: no mapping for $nativecode"); // Fall back to DB_ERROR if there was no mapping. return DB_ERROR; - //return $errormsg; } // }}} - // {{{ fetchRow() - /** - * Fetch a row and return as array. - * + * Fetch and return a row of data (it uses fetchInto for that) * @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 + * @param $fetchmode format of fetched row array + * @param $rownum the absolute row number to fetch + * + * @return array a row of data, or false on error */ - function &fetchRow($result, $fetchmode = DB_FETCHMODE_DEFAULT) + function fetchRow($result, $fetchmode = DB_FETCHMODE_DEFAULT, $rownum=0) { if ($fetchmode == DB_FETCHMODE_DEFAULT) { $fetchmode = $this->fetchmode; } - if ($this->row[$result] >= $this->numrows[$result]) { - return NULL; + $res = $this->fetchInto ($result, $arr, $fetchmode, $rownum); + if ($res !== DB_OK) { + return $res; } - 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->pgsqlRaiseError(); - if (!$err) { - return NULL; - } - return $err; - } - $this->row[$result]++; - return $row; + return $arr; } - // }}} // {{{ 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 $row (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 * * @return int DB_OK on success, a DB error code on failure */ - function fetchInto($result, &$arr, $fetchmode = DB_FETCHMODE_DEFAULT) + function fetchInto($result, &$row, $fetchmode, $rownum=0) { - if ($fetchmode == DB_FETCHMODE_DEFAULT) { - $fetchmode = $this->fetchmode; - } - if ($this->row[$result]>=$this->numrows[$result]){ + $rownum = ($rownum > 0) ? $rownum : $this->row[$result]; + if ($rownum >= $this->numrows[$result]) { return NULL; } if ($fetchmode & DB_FETCHMODE_ASSOC) { - $arr = @pg_fetch_array($result, $this->row[$result], PGSQL_ASSOC); + $row = @pg_fetch_array($result, $rownum, PGSQL_ASSOC); } else { - $arr = @pg_fetch_row($result, $this->row[$result]); + $row = @pg_fetch_row($result, $rownum); } - 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); // XXX ERRORMSG + if (!$row) { + $err = $this->pgsqlRaiseError(); + if ($err) { + return $err; + } } - $this->row[$result]++; + $this->row[$result] = ++$rownum; return DB_OK; } @@ -335,7 +292,7 @@ unset($this->row[$result]); unset($this->numrows[$result]); $this->affected = 0; - return true; + return true; } // }}} @@ -391,54 +348,6 @@ } // }}} - // {{{ 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; - } - - // }}} - // {{{ execute() - - /** - * @return mixed returns a DB result object for successful SELECT - * queries, DB_OK for other successful queries. A DB - * error is returned on failure. - */ - function execute($stmt, $data = false) - { - $realquery = $this->executeEmulateQuery($stmt, $data); - $result = $this->simpleQuery($realquery); - if (DB::isError($result) || $result === DB_OK) { - return $result; - } else { - return new DB_result($this, $result); - } - } - - // }}} // {{{ autoCommit() /** @@ -501,17 +410,13 @@ */ function affectedRows() { - $result = $this->affected; - if ($result === false) { - return $this->raiseError(); - } - return $result; - } + return $this->affected; + } // }}} // {{{ nextId() /** - * Get the next value in a sequence. + * Get the next value in a sequence. * * We are using native PostgreSQL sequences. If a sequence does * not exist, it will be created, unless $ondemand is false. @@ -583,10 +488,13 @@ function pgsqlRaiseError($errno = null) { + $native = $this->errorNative(); if ($errno === null) { - return $this->raiseError($this->errorCode(pg_errormessage($this->connection))); + $err = $this->errorCode($native); + } else { + $err = $errno; } - return $this->raiseError($this->errorCode($errno)); + return $this->raiseError($err, null, null, null, $native); } // }}}
-- PHP CVS Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]