Changeset 17877 for branches/comu-ver2/data/module/DB/pgsql.php
- Timestamp:
- 2009/03/06 20:21:51 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/comu-ver2/data/module/DB/pgsql.php
r15532 r17877 20 20 * @author Stig Bakken <ssb@php.net> 21 21 * @author Daniel Convissor <danielc@php.net> 22 * @copyright 1997-200 5The PHP Group22 * @copyright 1997-2007 The PHP Group 23 23 * @license http://www.php.net/license/3_0.txt PHP License 3.0 24 24 * @version CVS: $Id$ … … 29 29 * Obtain the DB_common class so it can be extended from 30 30 */ 31 32 31 require_once DB_PHP_DIR . '/DB/common.php'; 33 32 … … 43 42 * @author Stig Bakken <ssb@php.net> 44 43 * @author Daniel Convissor <danielc@php.net> 45 * @copyright 1997-200 5The PHP Group44 * @copyright 1997-2007 The PHP Group 46 45 * @license http://www.php.net/license/3_0.txt PHP License 3.0 47 * @version Release: @package_version@46 * @version Release: 1.7.14RC1 48 47 * @link http://pear.php.net/package/DB 49 48 */ … … 195 194 * ); 196 195 * 197 * $db = &DB::connect($dsn, $options);196 * $db = DB::connect($dsn, $options); 198 197 * if (PEAR::isError($db)) { 199 198 * die($db->getMessage()); … … 279 278 $params); 280 279 } else { 281 ini_set('track_errors', 1);280 @ini_set('track_errors', 1); 282 281 $this->connection = @call_user_func_array($connect_function, 283 282 $params); 284 ini_set('track_errors', $ini);283 @ini_set('track_errors', $ini); 285 284 } 286 285 … … 322 321 function simpleQuery($query) 323 322 { 324 $ismanip = DB::isManip($query);323 $ismanip = $this->_checkManip($query); 325 324 $this->last_query = $query; 326 325 $query = $this->modifyQuery($query); … … 338 337 return $this->pgsqlRaiseError(); 339 338 } 340 // Determine which queries that should return data, and which 341 // should return an error code only. 339 340 /* 341 * Determine whether queries produce affected rows, result or nothing. 342 * 343 * This logic was introduced in version 1.1 of the file by ssb, 344 * though the regex has been modified slightly since then. 345 * 346 * PostgreSQL commands: 347 * ABORT, ALTER, BEGIN, CLOSE, CLUSTER, COMMIT, COPY, 348 * CREATE, DECLARE, DELETE, DROP TABLE, EXPLAIN, FETCH, 349 * GRANT, INSERT, LISTEN, LOAD, LOCK, MOVE, NOTIFY, RESET, 350 * REVOKE, ROLLBACK, SELECT, SELECT INTO, SET, SHOW, 351 * UNLISTEN, UPDATE, VACUUM 352 */ 342 353 if ($ismanip) { 343 354 $this->affected = @pg_affected_rows($result); 344 355 return DB_OK; 345 } elseif (preg_match('/^\s*\(*\s*(SELECT|EXPLAIN|SHOW)\s/si', $query)) { 346 /* PostgreSQL commands: 347 ABORT, ALTER, BEGIN, CLOSE, CLUSTER, COMMIT, COPY, 348 CREATE, DECLARE, DELETE, DROP TABLE, EXPLAIN, FETCH, 349 GRANT, INSERT, LISTEN, LOAD, LOCK, MOVE, NOTIFY, RESET, 350 REVOKE, ROLLBACK, SELECT, SELECT INTO, SET, SHOW, 351 UNLISTEN, UPDATE, VACUUM 352 */ 356 } elseif (preg_match('/^\s*\(*\s*(SELECT|EXPLAIN|FETCH|SHOW)\s/si', 357 $query)) 358 { 353 359 $this->row[(int)$result] = 0; // reset the row counter. 354 360 $numrows = $this->numRows($result); … … 473 479 474 480 // }}} 475 // {{{ quoteSmart() 476 477 /** 478 * Formats input so it can be safely used in a query 479 * 480 * @param mixed $in the data to be formatted 481 * 482 * @return mixed the formatted data. The format depends on the input's 483 * PHP type: 484 * + null = the string <samp>NULL</samp> 485 * + boolean = string <samp>TRUE</samp> or <samp>FALSE</samp> 486 * + integer or double = the unquoted number 487 * + other (including strings and numeric strings) = 488 * the data escaped according to MySQL's settings 489 * then encapsulated between single quotes 481 // {{{ quoteBoolean() 482 483 /** 484 * Formats a boolean value for use within a query in a locale-independent 485 * manner. 486 * 487 * @param boolean the boolean value to be quoted. 488 * @return string the quoted string. 489 * @see DB_common::quoteSmart() 490 * @since Method available since release 1.7.8. 491 */ 492 function quoteBoolean($boolean) { 493 return $boolean ? 'TRUE' : 'FALSE'; 494 } 495 496 // }}} 497 // {{{ escapeSimple() 498 499 /** 500 * Escapes a string according to the current DBMS's standards 501 * 502 * {@internal PostgreSQL treats a backslash as an escape character, 503 * so they are escaped as well. 504 * 505 * @param string $str the string to be escaped 506 * 507 * @return string the escaped string 490 508 * 491 509 * @see DB_common::quoteSmart() 492 510 * @since Method available since Release 1.6.0 493 511 */ 494 function quoteSmart($in) 495 { 496 if (is_int($in) || is_double($in)) { 497 return $in; 498 } elseif (is_bool($in)) { 499 return $in ? 'TRUE' : 'FALSE'; 500 } elseif (is_null($in)) { 501 return 'NULL'; 512 function escapeSimple($str) 513 { 514 if (function_exists('pg_escape_string')) { 515 /* This fixes an undocumented BC break in PHP 5.2.0 which changed 516 * the prototype of pg_escape_string. I'm not thrilled about having 517 * to sniff the PHP version, quite frankly, but it's the only way 518 * to deal with the problem. Revision 1.331.2.13.2.10 on 519 * php-src/ext/pgsql/pgsql.c (PHP_5_2 branch) is to blame, for the 520 * record. */ 521 if (version_compare(PHP_VERSION, '5.2.0', '>=')) { 522 return pg_escape_string($this->connection, $str); 523 } else { 524 return pg_escape_string($str); 525 } 502 526 } else { 503 return "'" . $this->escapeSimple($in) . "'"; 504 } 505 } 506 507 // }}} 508 // {{{ escapeSimple() 509 510 /** 511 * Escapes a string according to the current DBMS's standards 512 * 513 * {@internal PostgreSQL treats a backslash as an escape character, 514 * so they are escaped as well. 515 * 516 * Not using pg_escape_string() yet because it requires PostgreSQL 517 * to be at version 7.2 or greater.}} 518 * 519 * @param string $str the string to be escaped 520 * 521 * @return string the escaped string 522 * 523 * @see DB_common::quoteSmart() 524 * @since Method available since Release 1.6.0 525 */ 526 function escapeSimple($str) 527 { 528 return str_replace("'", "''", str_replace('\\', '\\\\', $str)); 527 return str_replace("'", "''", str_replace('\\', '\\\\', $str)); 528 } 529 529 } 530 530 … … 677 677 do { 678 678 $this->pushErrorHandling(PEAR_ERROR_RETURN); 679 $result = &$this->query("SELECT NEXTVAL('${seqname}')");679 $result = $this->query("SELECT NEXTVAL('${seqname}')"); 680 680 $this->popErrorHandling(); 681 681 if ($ondemand && DB::isError($result) && … … 781 781 { 782 782 $native = $this->errorNative(); 783 if (!$native) { 784 $native = 'Database connection has been lost.'; 785 $errno = DB_ERROR_CONNECT_FAILED; 786 } 783 787 if ($errno === null) { 784 788 $errno = $this->errorCode($native); … … 817 821 if (!isset($error_regexps)) { 818 822 $error_regexps = array( 823 '/column .* (of relation .*)?does not exist/i' 824 => DB_ERROR_NOSUCHFIELD, 819 825 '/(relation|sequence|table).*does not exist|class .* not found/i' 820 826 => DB_ERROR_NOSUCHTABLE, 821 827 '/index .* does not exist/' 822 828 => DB_ERROR_NOT_FOUND, 823 '/column .* does not exist/i'824 => DB_ERROR_NOSUCHFIELD,825 829 '/relation .* already exists/i' 826 830 => DB_ERROR_ALREADY_EXISTS, … … 978 982 $field_name = @pg_fieldname($resource, $num_field); 979 983 984 // Check if there's a schema in $table_name and update things 985 // accordingly. 986 $from = 'pg_attribute f, pg_class tab, pg_type typ'; 987 if (strpos($table_name, '.') !== false) { 988 $from .= ', pg_namespace nsp'; 989 list($schema, $table) = explode('.', $table_name); 990 $tableWhere = "tab.relname = '$table' AND tab.relnamespace = nsp.oid AND nsp.nspname = '$schema'"; 991 } else { 992 $tableWhere = "tab.relname = '$table_name'"; 993 } 994 980 995 $result = @pg_exec($this->connection, "SELECT f.attnotnull, f.atthasdef 981 FROM pg_attribute f, pg_class tab, pg_type typ996 FROM $from 982 997 WHERE tab.relname = typ.typname 983 998 AND typ.typrelid = f.attrelid 984 999 AND f.attname = '$field_name' 985 AND tab.relname = '$table_name'");1000 AND $tableWhere"); 986 1001 if (@pg_numrows($result) > 0) { 987 1002 $row = @pg_fetch_row($result, 0); … … 990 1005 if ($row[1] == 't') { 991 1006 $result = @pg_exec($this->connection, "SELECT a.adsrc 992 FROM pg_attribute f, pg_class tab, pg_type typ, pg_attrdef a1007 FROM $from, pg_attrdef a 993 1008 WHERE tab.relname = typ.typname AND typ.typrelid = f.attrelid 994 1009 AND f.attrelid = a.adrelid AND f.attname = '$field_name' 995 AND tab.relname = '$table_name'AND f.attnum = a.adnum");1010 AND $tableWhere AND f.attnum = a.adnum"); 996 1011 $row = @pg_fetch_row($result, 0); 997 1012 $num = preg_replace("/'(.*)'::\w+/", "\\1", $row[0]); … … 1002 1017 } 1003 1018 $result = @pg_exec($this->connection, "SELECT i.indisunique, i.indisprimary, i.indkey 1004 FROM pg_attribute f, pg_class tab, pg_type typ, pg_index i1019 FROM $from, pg_index i 1005 1020 WHERE tab.relname = typ.typname 1006 1021 AND typ.typrelid = f.attrelid 1007 1022 AND f.attrelid = i.indrelid 1008 1023 AND f.attname = '$field_name' 1009 AND tab.relname = '$table_name'");1024 AND $tableWhere"); 1010 1025 $count = @pg_numrows($result); 1011 1026 … … 1068 1083 . ' WHERE schemaname NOT IN' 1069 1084 . " ('pg_catalog', 'information_schema', 'pg_toast')"; 1085 case 'schema.views': 1086 return "SELECT schemaname || '.' || viewname from pg_views WHERE schemaname" 1087 . " NOT IN ('information_schema', 'pg_catalog')"; 1070 1088 case 'views': 1071 1089 // Table cols: viewname | viewowner | definition … … 1086 1104 1087 1105 // }}} 1106 // {{{ _checkManip() 1107 1108 /** 1109 * Checks if the given query is a manipulation query. This also takes into 1110 * account the _next_query_manip flag and sets the _last_query_manip flag 1111 * (and resets _next_query_manip) according to the result. 1112 * 1113 * @param string The query to check. 1114 * 1115 * @return boolean true if the query is a manipulation query, false 1116 * otherwise 1117 * 1118 * @access protected 1119 */ 1120 function _checkManip($query) 1121 { 1122 return (preg_match('/^\s*(SAVEPOINT|RELEASE)\s+/i', $query) 1123 || parent::_checkManip($query)); 1124 } 1088 1125 1089 1126 }
Note: See TracChangeset
for help on using the changeset viewer.