- Timestamp:
- 2013/08/02 13:22:57 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/version-2_13-dev/data/module/MDB2/Driver/pgsql.php
r20119 r23022 44 44 // +----------------------------------------------------------------------+ 45 45 // 46 // $Id: pgsql.php ,v 1.203 2008/11/29 14:04:46 afz Exp$46 // $Id: pgsql.php 327317 2012-08-27 15:17:08Z danielc $ 47 47 48 48 /** … … 212 212 } 213 213 $connection = $this->getConnection(); 214 if ( PEAR::isError($connection)) {214 if (MDB2::isError($connection)) { 215 215 return $connection; 216 216 } … … 237 237 { 238 238 $this->debug('Starting transaction/savepoint', __FUNCTION__, array('is_manip' => true, 'savepoint' => $savepoint)); 239 if ( !is_null($savepoint)) {239 if (null !== $savepoint) { 240 240 if (!$this->in_transaction) { 241 241 return $this->raiseError(MDB2_ERROR_INVALID, null, null, … … 244 244 $query = 'SAVEPOINT '.$savepoint; 245 245 return $this->_doQuery($query, true); 246 } elseif ($this->in_transaction) { 246 } 247 if ($this->in_transaction) { 247 248 return MDB2_OK; //nothing to do 248 249 } … … 251 252 register_shutdown_function('MDB2_closeOpenTransactions'); 252 253 } 253 $result = &$this->_doQuery('BEGIN', true);254 if ( PEAR::isError($result)) {254 $result = $this->_doQuery('BEGIN', true); 255 if (MDB2::isError($result)) { 255 256 return $result; 256 257 } … … 280 281 'commit/release savepoint cannot be done changes are auto committed', __FUNCTION__); 281 282 } 282 if ( !is_null($savepoint)) {283 if (null !== $savepoint) { 283 284 $query = 'RELEASE SAVEPOINT '.$savepoint; 284 285 return $this->_doQuery($query, true); 285 286 } 286 287 287 $result = &$this->_doQuery('COMMIT', true);288 if ( PEAR::isError($result)) {288 $result = $this->_doQuery('COMMIT', true); 289 if (MDB2::isError($result)) { 289 290 return $result; 290 291 } … … 314 315 'rollback cannot be done changes are auto committed', __FUNCTION__); 315 316 } 316 if ( !is_null($savepoint)) {317 if (null !== $savepoint) { 317 318 $query = 'ROLLBACK TO SAVEPOINT '.$savepoint; 318 319 return $this->_doQuery($query, true); … … 320 321 321 322 $query = 'ROLLBACK'; 322 $result = &$this->_doQuery($query, true);323 if ( PEAR::isError($result)) {323 $result = $this->_doQuery($query, true); 324 if (MDB2::isError($result)) { 324 325 return $result; 325 326 } … … 339 340 * REPEATABLE READ (prevents nonrepeatable reads) 340 341 * SERIALIZABLE (prevents phantom reads) 342 * @param array some transaction options: 343 * 'wait' => 'WAIT' | 'NO WAIT' 344 * 'rw' => 'READ WRITE' | 'READ ONLY' 345 * 341 346 * @return mixed MDB2_OK on success, a MDB2 error on failure 342 347 * … … 344 349 * @since 2.1.1 345 350 */ 346 function setTransactionIsolation($isolation )351 function setTransactionIsolation($isolation, $options = array()) 347 352 { 348 353 $this->debug('Setting transaction isolation level', __FUNCTION__, array('is_manip' => true)); … … 373 378 function _doConnect($username, $password, $database_name, $persistent = false) 374 379 { 375 if (! PEAR::loadExtension($this->phptype)) {380 if (!extension_loaded($this->phptype)) { 376 381 return $this->raiseError(MDB2_ERROR_NOT_FOUND, null, null, 377 382 'extension '.$this->phptype.' is not compiled into PHP', __FUNCTION__); 378 383 } 379 384 380 385 if ($database_name == '') { 381 386 $database_name = 'template1'; … … 448 453 if (!empty($this->dsn['charset'])) { 449 454 $result = $this->setCharset($this->dsn['charset'], $connection); 450 if ( PEAR::isError($result)) {455 if (MDB2::isError($result)) { 451 456 return $result; 457 } 458 } 459 460 // Enable extra compatibility settings on 8.2 and later 461 if (function_exists('pg_parameter_status')) { 462 $version = pg_parameter_status($connection, 'server_version'); 463 if ($version == false) { 464 return $this->raiseError(null, null, null, 465 'Unable to retrieve server version', __FUNCTION__); 466 } 467 $version = explode ('.', $version); 468 if ( $version['0'] > 8 469 || ($version['0'] == 8 && $version['1'] >= 2) 470 ) { 471 if (!@pg_query($connection, "SET SESSION STANDARD_CONFORMING_STRINGS = OFF")) { 472 return $this->raiseError(null, null, null, 473 'Unable to set standard_conforming_strings to off', __FUNCTION__); 474 } 475 476 if (!@pg_query($connection, "SET SESSION ESCAPE_STRING_WARNING = OFF")) { 477 return $this->raiseError(null, null, null, 478 'Unable to set escape_string_warning to off', __FUNCTION__); 479 } 452 480 } 453 481 } … … 483 511 $this->database_name, 484 512 $this->options['persistent']); 485 if ( PEAR::isError($connection)) {513 if (MDB2::isError($connection)) { 486 514 return $connection; 487 515 } … … 510 538 function setCharset($charset, $connection = null) 511 539 { 512 if ( is_null($connection)) {540 if (null === $connection) { 513 541 $connection = $this->getConnection(); 514 if ( PEAR::isError($connection)) {542 if (MDB2::isError($connection)) { 515 543 return $connection; 516 544 } … … 545 573 $this->escape($name), 546 574 $this->options['persistent']); 547 if (! PEAR::isError($res)) {575 if (!MDB2::isError($res)) { 548 576 return true; 549 577 } … … 596 624 // {{{ standaloneQuery() 597 625 598 /**626 /** 599 627 * execute a query as DBA 600 628 * … … 606 634 * @access public 607 635 */ 608 function &standaloneQuery($query, $types = null, $is_manip = false)636 function standaloneQuery($query, $types = null, $is_manip = false) 609 637 { 610 638 $user = $this->options['DBA_username']? $this->options['DBA_username'] : $this->dsn['username']; 611 639 $pass = $this->options['DBA_password']? $this->options['DBA_password'] : $this->dsn['password']; 612 640 $connection = $this->_doConnect($user, $pass, $this->database_name, $this->options['persistent']); 613 if ( PEAR::isError($connection)) {641 if (MDB2::isError($connection)) { 614 642 return $connection; 615 643 } … … 620 648 $query = $this->_modifyQuery($query, $is_manip, $limit, $offset); 621 649 622 $result = &$this->_doQuery($query, $is_manip, $connection, $this->database_name);623 if (! PEAR::isError($result)) {650 $result = $this->_doQuery($query, $is_manip, $connection, $this->database_name); 651 if (!MDB2::isError($result)) { 624 652 if ($is_manip) { 625 653 $result = $this->_affectedRows($connection, $result); 626 654 } else { 627 $result = & $this->_wrapResult($result, $types, true, false, $limit, $offset);655 $result = $this->_wrapResult($result, $types, true, true, $limit, $offset); 628 656 } 629 657 } … … 645 673 * @access protected 646 674 */ 647 function &_doQuery($query, $is_manip = false, $connection = null, $database_name = null)675 function _doQuery($query, $is_manip = false, $connection = null, $database_name = null) 648 676 { 649 677 $this->last_query = $query; 650 678 $result = $this->debug($query, 'query', array('is_manip' => $is_manip, 'when' => 'pre')); 651 679 if ($result) { 652 if ( PEAR::isError($result)) {680 if (MDB2::isError($result)) { 653 681 return $result; 654 682 } … … 660 688 } 661 689 662 if ( is_null($connection)) {690 if (null === $connection) { 663 691 $connection = $this->getConnection(); 664 if ( PEAR::isError($connection)) {692 if (MDB2::isError($connection)) { 665 693 return $connection; 666 694 } … … 670 698 $result = @$function($connection, $query); 671 699 if (!$result) { 672 $err = &$this->raiseError(null, null, null,700 $err = $this->raiseError(null, null, null, 673 701 'Could not execute statement', __FUNCTION__); 674 702 return $err; 675 703 } elseif ($this->options['multi_query']) { 676 704 if (!($result = @pg_get_result($connection))) { 677 $err = &$this->raiseError(null, null, null,705 $err = $this->raiseError(null, null, null, 678 706 'Could not get the first result from a multi query', __FUNCTION__); 679 707 return $err; … … 698 726 function _affectedRows($connection, $result = null) 699 727 { 700 if ( is_null($connection)) {728 if (null === $connection) { 701 729 $connection = $this->getConnection(); 702 if ( PEAR::isError($connection)) {730 if (MDB2::isError($connection)) { 703 731 return $connection; 704 732 } … … 737 765 return $query; 738 766 } 739 767 740 768 // }}} 741 769 // {{{ _modifyManipQuery() 742 770 743 771 /** 744 772 * Changes a manip query string for various DBMS specific reasons … … 786 814 } else { 787 815 $server_info = $this->queryOne($query, 'text'); 788 if ( PEAR::isError($server_info)) {816 if (MDB2::isError($server_info)) { 789 817 return $server_info; 790 818 } … … 792 820 // cache server_info 793 821 $this->connected_server_info = $server_info; 794 if (!$native && ! PEAR::isError($server_info)) {822 if (!$native && !MDB2::isError($server_info)) { 795 823 $tmp = explode('.', $server_info, 3); 796 824 if (empty($tmp[2]) … … 842 870 * @see bindParam, execute 843 871 */ 844 function &prepare($query, $types = null, $result_types = null, $lobs = array())872 function prepare($query, $types = null, $result_types = null, $lobs = array()) 845 873 { 846 874 if ($this->options['emulate_prepared']) { 847 $obj =& parent::prepare($query, $types, $result_types, $lobs); 848 return $obj; 875 return parent::prepare($query, $types, $result_types, $lobs); 849 876 } 850 877 $is_manip = ($result_types === MDB2_PREPARE_MANIP); … … 854 881 $result = $this->debug($query, __FUNCTION__, array('is_manip' => $is_manip, 'when' => 'pre')); 855 882 if ($result) { 856 if ( PEAR::isError($result)) {883 if (MDB2::isError($result)) { 857 884 return $result; 858 885 } … … 886 913 break; 887 914 } 888 if ( is_null($placeholder_type)) {915 if (null === $placeholder_type) { 889 916 $placeholder_type_guess = $query[$p_position]; 890 917 } 891 918 892 919 $new_pos = $this->_skipDelimitedStrings($query, $position, $p_position); 893 if ( PEAR::isError($new_pos)) {920 if (MDB2::isError($new_pos)) { 894 921 return $new_pos; 895 922 } … … 900 927 901 928 if ($query[$position] == $placeholder_type_guess) { 902 if ( is_null($placeholder_type)) {929 if (null === $placeholder_type) { 903 930 $placeholder_type = $query[$p_position]; 904 931 $question = $colon = $placeholder_type; … … 917 944 $param = preg_replace($regexp, '\\1', $query); 918 945 if ($param === '') { 919 $err = &$this->raiseError(MDB2_ERROR_SYNTAX, null, null,946 $err = $this->raiseError(MDB2_ERROR_SYNTAX, null, null, 920 947 'named parameter name must match "bindname_format" option', __FUNCTION__); 921 948 return $err; … … 930 957 $pgtypes[] = $this->datatype->mapPrepareDatatype($types[$parameter]); 931 958 } else { 932 if (version_compare(PHP_VERSION, '5.0.0', '>')) { 933 $pgtypes[] = 'text'; 934 } 959 $pgtypes[] = 'text'; 935 960 } 936 961 } 937 if (($key_parameter = array_search($name, $positions))) { 938 $next_parameter = 1; 939 foreach ($positions as $key => $value) { 940 if ($key_parameter == $key) { 941 break; 942 } 943 ++$next_parameter; 944 } 962 if (($key_parameter = array_search($name, $positions)) !== false) { 963 //$next_parameter = 1; 964 $parameter = $key_parameter + 1; 965 //foreach ($positions as $key => $value) { 966 // if ($key_parameter == $key) { 967 // break; 968 // } 969 // ++$next_parameter; 970 //} 945 971 } else { 946 972 ++$parameter; 947 $next_parameter = $parameter;973 //$next_parameter = $parameter; 948 974 $positions[] = $name; 949 975 } … … 955 981 } 956 982 $connection = $this->getConnection(); 957 if ( PEAR::isError($connection)) {983 if (MDB2::isError($connection)) { 958 984 return $connection; 959 985 } … … 961 987 $statement_name = sprintf($this->options['statement_format'], $this->phptype, $prep_statement_counter++ . sha1(microtime() + mt_rand())); 962 988 $statement_name = substr(strtolower($statement_name), 0, $this->options['max_identifiers_length']); 963 if ( $pgtypes === false) {989 if (false === $pgtypes) { 964 990 $result = @pg_prepare($connection, $statement_name, $query); 965 991 if (!$result) { 966 $err = &$this->raiseError(null, null, null,992 $err = $this->raiseError(null, null, null, 967 993 'Unable to create prepared statement handle', __FUNCTION__); 968 994 return $err; … … 974 1000 } 975 1001 $query = 'PREPARE '.$statement_name.$types_string.' AS '.$query; 976 $statement = &$this->_doQuery($query, true, $connection);977 if ( PEAR::isError($statement)) {1002 $statement = $this->_doQuery($query, true, $connection); 1003 if (MDB2::isError($statement)) { 978 1004 return $statement; 979 1005 } … … 1005 1031 } 1006 1032 $schema_list = $this->queryOne("SELECT array_to_string(current_schemas(false), ',')"); 1007 if ( PEAR::isError($schema_list) || empty($schema_list) || count($schema_list) < 2) {1033 if (MDB2::isError($schema_list) || empty($schema_list) || count($schema_list) < 2) { 1008 1034 $order_by = ' a.attnum'; 1009 1035 $schema_clause = ' AND n.nspname=current_schema()'; … … 1040 1066 ORDER BY ".$order_by; 1041 1067 $seqname = $this->queryOne($query); 1042 if (! PEAR::isError($seqname) && !empty($seqname) && is_string($seqname)) {1068 if (!MDB2::isError($seqname) && !empty($seqname) && is_string($seqname)) { 1043 1069 return $seqname; 1044 1070 } … … 1070 1096 $this->popExpect(); 1071 1097 $this->popErrorHandling(); 1072 if ( PEAR::isError($result)) {1098 if (MDB2::isError($result)) { 1073 1099 if ($ondemand && $result->getCode() == MDB2_ERROR_NOSUCHTABLE) { 1074 1100 $this->loadModule('Manager', null, true); 1075 1101 $result = $this->manager->createSequence($seq_name); 1076 if ( PEAR::isError($result)) {1102 if (MDB2::isError($result)) { 1077 1103 return $this->raiseError($result, null, null, 1078 1104 'on demand sequence could not be created', __FUNCTION__); … … 1143 1169 * @access public 1144 1170 */ 1145 function &fetchRow($fetchmode = MDB2_FETCHMODE_DEFAULT, $rownum = null)1146 { 1147 if ( !is_null($rownum)) {1171 function fetchRow($fetchmode = MDB2_FETCHMODE_DEFAULT, $rownum = null) 1172 { 1173 if (null !== $rownum) { 1148 1174 $seek = $this->seek($rownum); 1149 if ( PEAR::isError($seek)) {1175 if (MDB2::isError($seek)) { 1150 1176 return $seek; 1151 1177 } … … 1154 1180 $fetchmode = $this->db->fetchmode; 1155 1181 } 1156 if ($fetchmode & MDB2_FETCHMODE_ASSOC) { 1182 if ( $fetchmode == MDB2_FETCHMODE_ASSOC 1183 || $fetchmode == MDB2_FETCHMODE_OBJECT 1184 ) { 1157 1185 $row = @pg_fetch_array($this->result, null, PGSQL_ASSOC); 1158 1186 if (is_array($row) … … 1165 1193 } 1166 1194 if (!$row) { 1167 if ( $this->result === false) {1168 $err = &$this->db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null,1195 if (false === $this->result) { 1196 $err = $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null, 1169 1197 'resultset has already been freed', __FUNCTION__); 1170 1198 return $err; 1171 1199 } 1172 $null = null; 1173 return $null; 1200 return null; 1174 1201 } 1175 1202 $mode = $this->db->options['portability'] & MDB2_PORTABILITY_EMPTY_TO_NULL; … … 1185 1212 $this->db->_fixResultArrayValues($row, $mode); 1186 1213 } 1187 if (!empty($this->types)) { 1214 if ( ( $fetchmode != MDB2_FETCHMODE_ASSOC 1215 && $fetchmode != MDB2_FETCHMODE_OBJECT) 1216 && !empty($this->types) 1217 ) { 1188 1218 $row = $this->db->datatype->convertResultRow($this->types, $row, $rtrim); 1219 } elseif (($fetchmode == MDB2_FETCHMODE_ASSOC 1220 || $fetchmode == MDB2_FETCHMODE_OBJECT) 1221 && !empty($this->types_assoc) 1222 ) { 1223 $row = $this->db->datatype->convertResultRow($this->types_assoc, $row, $rtrim); 1189 1224 } 1190 1225 if (!empty($this->values)) { … … 1196 1231 $row = (object) $row; 1197 1232 } else { 1198 $row = &new $object_class($row); 1233 $rowObj = new $object_class($row); 1234 $row = $rowObj; 1199 1235 } 1200 1236 } … … 1219 1255 $columns = array(); 1220 1256 $numcols = $this->numCols(); 1221 if ( PEAR::isError($numcols)) {1257 if (MDB2::isError($numcols)) { 1222 1258 return $numcols; 1223 1259 } … … 1245 1281 { 1246 1282 $cols = @pg_num_fields($this->result); 1247 if ( is_null($cols)) {1248 if ( $this->result === false) {1283 if (null === $cols) { 1284 if (false === $this->result) { 1249 1285 return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null, 1250 1286 'resultset has already been freed', __FUNCTION__); 1251 } elseif (is_null($this->result)) { 1287 } 1288 if (null === $this->result) { 1252 1289 return count($this->types); 1253 1290 } … … 1270 1307 { 1271 1308 $connection = $this->db->getConnection(); 1272 if ( PEAR::isError($connection)) {1309 if (MDB2::isError($connection)) { 1273 1310 return $connection; 1274 1311 } … … 1293 1330 if (is_resource($this->result) && $this->db->connection) { 1294 1331 $free = @pg_free_result($this->result); 1295 if ( $free === false) {1332 if (false === $free) { 1296 1333 return $this->db->raiseError(null, null, null, 1297 1334 'Could not free result', __FUNCTION__); … … 1324 1361 { 1325 1362 if ($this->rownum != ($rownum - 1) && !@pg_result_seek($this->result, $rownum)) { 1326 if ( $this->result === false) {1363 if (false === $this->result) { 1327 1364 return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null, 1328 1365 'resultset has already been freed', __FUNCTION__); 1329 } elseif (is_null($this->result)) { 1366 } 1367 if (null === $this->result) { 1330 1368 return MDB2_OK; 1331 1369 } … … 1349 1387 { 1350 1388 $numrows = $this->numRows(); 1351 if ( PEAR::isError($numrows)) {1389 if (MDB2::isError($numrows)) { 1352 1390 return $numrows; 1353 1391 } … … 1367 1405 { 1368 1406 $rows = @pg_num_rows($this->result); 1369 if ( is_null($rows)) {1370 if ( $this->result === false) {1407 if (null === $rows) { 1408 if (false === $this->result) { 1371 1409 return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null, 1372 1410 'resultset has already been freed', __FUNCTION__); 1373 } elseif (is_null($this->result)) { 1411 } 1412 if (null === $this->result) { 1374 1413 return 0; 1375 1414 } … … 1402 1441 * @access private 1403 1442 */ 1404 function &_execute($result_class = true, $result_wrap_class = false) 1405 { 1406 if (is_null($this->statement)) { 1407 $result =& parent::_execute($result_class, $result_wrap_class); 1408 return $result; 1443 function _execute($result_class = true, $result_wrap_class = true) 1444 { 1445 if (null === $this->statement) { 1446 return parent::_execute($result_class, $result_wrap_class); 1409 1447 } 1410 1448 $this->db->last_query = $this->query; … … 1416 1454 1417 1455 $connection = $this->db->getConnection(); 1418 if ( PEAR::isError($connection)) {1456 if (MDB2::isError($connection)) { 1419 1457 return $connection; 1420 1458 } … … 1454 1492 } 1455 1493 $quoted = $this->db->quote($value, $type, $query); 1456 if ( PEAR::isError($quoted)) {1494 if (MDB2::isError($quoted)) { 1457 1495 return $quoted; 1458 1496 } … … 1467 1505 $result = @pg_execute($connection, $this->statement, $parameters); 1468 1506 if (!$result) { 1469 $err = &$this->db->raiseError(null, null, null,1507 $err = $this->db->raiseError(null, null, null, 1470 1508 'Unable to execute statement', __FUNCTION__); 1471 1509 return $err; … … 1473 1511 } else { 1474 1512 $result = $this->db->_doQuery($query, $this->is_manip, $connection); 1475 if ( PEAR::isError($result)) {1513 if (MDB2::isError($result)) { 1476 1514 return $result; 1477 1515 } … … 1483 1521 } 1484 1522 1485 $result = &$this->db->_wrapResult($result, $this->result_types,1523 $result = $this->db->_wrapResult($result, $this->result_types, 1486 1524 $result_class, $result_wrap_class, $this->limit, $this->offset); 1487 1525 $this->db->debug($this->query, 'execute', array('is_manip' => $this->is_manip, 'when' => 'post', 'result' => $result)); … … 1500 1538 function free() 1501 1539 { 1502 if ( is_null($this->positions)) {1540 if (null === $this->positions) { 1503 1541 return $this->db->raiseError(MDB2_ERROR, null, null, 1504 1542 'Prepared statement has already been freed', __FUNCTION__); … … 1506 1544 $result = MDB2_OK; 1507 1545 1508 if ( !is_null($this->statement)) {1546 if (null !== $this->statement) { 1509 1547 $connection = $this->db->getConnection(); 1510 if ( PEAR::isError($connection)) {1548 if (MDB2::isError($connection)) { 1511 1549 return $connection; 1512 1550 } … … 1518 1556 return $result; 1519 1557 } 1558 1559 /** 1560 * drop an existing table 1561 * 1562 * @param string $name name of the table that should be dropped 1563 * @return mixed MDB2_OK on success, a MDB2 error on failure 1564 * @access public 1565 */ 1566 function dropTable($name) 1567 { 1568 $db = $this->getDBInstance(); 1569 if (MDB2::isError($db)) { 1570 return $db; 1571 } 1572 1573 $name = $db->quoteIdentifier($name, true); 1574 $result = $db->exec("DROP TABLE $name"); 1575 1576 if (MDB2::isError($result)) { 1577 $result = $db->exec("DROP TABLE $name CASCADE"); 1578 } 1579 1580 return $result; 1581 } 1520 1582 } 1521 1583 ?>
Note: See TracChangeset
for help on using the changeset viewer.