- 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/mysql.php
r20764 r23022 44 44 // +----------------------------------------------------------------------+ 45 45 // 46 // $Id: mysql.php ,v 1.214 2008/11/16 21:45:08 quipo Exp$46 // $Id: mysql.php 327320 2012-08-27 15:52:50Z danielc $ 47 47 // 48 48 … … 58 58 // {{{ properties 59 59 60 var $string_quoting = array('start' => "'", 'end' => "'", 'escape' => '\\', 'escape_pattern' => '\\'); 61 62 var $identifier_quoting = array('start' => '`', 'end' => '`', 'escape' => '`'); 63 64 var $sql_comments = array( 60 public $string_quoting = array( 61 'start' => "'", 62 'end' => "'", 63 'escape' => '\\', 64 'escape_pattern' => '\\', 65 ); 66 67 public $identifier_quoting = array( 68 'start' => '`', 69 'end' => '`', 70 'escape' => '`', 71 ); 72 73 public $sql_comments = array( 65 74 array('start' => '-- ', 'end' => "\n", 'escape' => false), 66 75 array('start' => '#', 'end' => "\n", 'escape' => false), … … 68 77 ); 69 78 70 var$server_capabilities_checked = false;71 72 var$start_transaction = false;73 74 var$varchar_max_length = 255;79 protected $server_capabilities_checked = false; 80 81 protected $start_transaction = false; 82 83 public $varchar_max_length = 255; 75 84 76 85 // }}} … … 118 127 // }}} 119 128 // {{{ _reCheckSupportedOptions() 120 129 121 130 /** 122 131 * If the user changes certain options, other capabilities may depend … … 298 307 } 299 308 $connection = $this->getConnection(); 300 if ( PEAR::isError($connection)) {309 if (MDB2::isError($connection)) { 301 310 return $connection; 302 311 } … … 339 348 } 340 349 $query = $this->start_transaction ? 'START TRANSACTION' : 'SET AUTOCOMMIT = 0'; 341 $result = &$this->_doQuery($query, true);342 if ( PEAR::isError($result)) {350 $result = $this->_doQuery($query, true); 351 if (MDB2::isError($result)) { 343 352 return $result; 344 353 } … … 386 395 } 387 396 388 $result = &$this->_doQuery('COMMIT', true);389 if ( PEAR::isError($result)) {397 $result = $this->_doQuery('COMMIT', true); 398 if (MDB2::isError($result)) { 390 399 return $result; 391 400 } 392 401 if (!$this->start_transaction) { 393 402 $query = 'SET AUTOCOMMIT = 1'; 394 $result = &$this->_doQuery($query, true);395 if ( PEAR::isError($result)) {403 $result = $this->_doQuery($query, true); 404 if (MDB2::isError($result)) { 396 405 return $result; 397 406 } … … 432 441 433 442 $query = 'ROLLBACK'; 434 $result = &$this->_doQuery($query, true);435 if ( PEAR::isError($result)) {443 $result = $this->_doQuery($query, true); 444 if (MDB2::isError($result)) { 436 445 return $result; 437 446 } 438 447 if (!$this->start_transaction) { 439 448 $query = 'SET AUTOCOMMIT = 1'; 440 $result = &$this->_doQuery($query, true);441 if ( PEAR::isError($result)) {449 $result = $this->_doQuery($query, true); 450 if (MDB2::isError($result)) { 442 451 return $result; 443 452 } … … 458 467 * REPEATABLE READ (prevents nonrepeatable reads) 459 468 * SERIALIZABLE (prevents phantom reads) 469 * @param array some transaction options: 470 * 'wait' => 'WAIT' | 'NO WAIT' 471 * 'rw' => 'READ WRITE' | 'READ ONLY' 472 * 460 473 * @return mixed MDB2_OK on success, a MDB2 error on failure 461 474 * … … 463 476 * @since 2.1.1 464 477 */ 465 function setTransactionIsolation($isolation )478 function setTransactionIsolation($isolation, $options = array()) 466 479 { 467 480 $this->debug('Setting transaction isolation level', __FUNCTION__, array('is_manip' => true)); … … 496 509 function _doConnect($username, $password, $persistent = false) 497 510 { 498 if (! PEAR::loadExtension($this->phptype)) {511 if (!extension_loaded($this->phptype)) { 499 512 return $this->raiseError(MDB2_ERROR_NOT_FOUND, null, null, 500 513 'extension '.$this->phptype.' is not compiled into PHP', __FUNCTION__); … … 502 515 503 516 $params = array(); 504 if ($this->dsn['protocol'] && $this->dsn['protocol'] == 'unix') { 517 $unix = ($this->dsn['protocol'] && $this->dsn['protocol'] == 'unix'); 518 if (empty($this->dsn['hostspec'])) { 519 $this->dsn['hostspec'] = $unix ? '' : 'localhost'; 520 } 521 if ($this->dsn['hostspec']) { 522 $params[0] = $this->dsn['hostspec'] . ($this->dsn['port'] ? ':' . $this->dsn['port'] : ''); 523 } else { 505 524 $params[0] = ':' . $this->dsn['socket']; 506 } else {507 $params[0] = $this->dsn['hostspec'] ? $this->dsn['hostspec']508 : 'localhost';509 if ($this->dsn['port']) {510 $params[0].= ':' . $this->dsn['port'];511 }512 525 } 513 526 $params[] = $username ? $username : null; … … 539 552 if (!empty($this->dsn['charset'])) { 540 553 $result = $this->setCharset($this->dsn['charset'], $connection); 541 if ( PEAR::isError($result)) {554 if (MDB2::isError($result)) { 542 555 $this->disconnect(false); 543 556 return $result; … … 574 587 $this->options['persistent'] 575 588 ); 576 if ( PEAR::isError($connection)) {589 if (MDB2::isError($connection)) { 577 590 return $connection; 578 591 } … … 615 628 if (is_null($connection)) { 616 629 $connection = $this->getConnection(); 617 if ( PEAR::isError($connection)) {630 if (MDB2::isError($connection)) { 618 631 return $connection; 619 632 } … … 627 640 if (function_exists('mysql_set_charset') && version_compare($client_info, '5.0.6')) { 628 641 if (!$result = mysql_set_charset($charset, $connection)) { 629 $err = &$this->raiseError(null, null, null,642 $err = $this->raiseError(null, null, null, 630 643 'Could not set client character set', __FUNCTION__); 631 644 return $err; … … 635 648 $query = "SET NAMES '".mysql_real_escape_string($charset, $connection)."'"; 636 649 if (!is_null($collation)) { 637 $query .= " COLLATE '".mysql i_real_escape_string($connection, $collation)."'";650 $query .= " COLLATE '".mysql_real_escape_string($collation, $connection)."'"; 638 651 } 639 652 return $this->_doQuery($query, true, $connection); … … 656 669 $this->dsn['password'], 657 670 $this->options['persistent']); 658 if ( PEAR::isError($connection)) {671 if (MDB2::isError($connection)) { 659 672 return $connection; 660 673 } … … 710 723 // {{{ standaloneQuery() 711 724 712 /**725 /** 713 726 * execute a query as DBA 714 727 * … … 720 733 * @access public 721 734 */ 722 function &standaloneQuery($query, $types = null, $is_manip = false)735 function standaloneQuery($query, $types = null, $is_manip = false) 723 736 { 724 737 $user = $this->options['DBA_username']? $this->options['DBA_username'] : $this->dsn['username']; 725 738 $pass = $this->options['DBA_password']? $this->options['DBA_password'] : $this->dsn['password']; 726 739 $connection = $this->_doConnect($user, $pass, $this->options['persistent']); 727 if ( PEAR::isError($connection)) {740 if (MDB2::isError($connection)) { 728 741 return $connection; 729 742 } … … 733 746 $this->offset = $this->limit = 0; 734 747 $query = $this->_modifyQuery($query, $is_manip, $limit, $offset); 735 736 $result = &$this->_doQuery($query, $is_manip, $connection, $this->database_name);737 if (! PEAR::isError($result)) {748 749 $result = $this->_doQuery($query, $is_manip, $connection, $this->database_name); 750 if (!MDB2::isError($result)) { 738 751 $result = $this->_affectedRows($connection, $result); 739 752 } … … 755 768 * @access protected 756 769 */ 757 function &_doQuery($query, $is_manip = false, $connection = null, $database_name = null)770 function _doQuery($query, $is_manip = false, $connection = null, $database_name = null) 758 771 { 759 772 $this->last_query = $query; 760 773 $result = $this->debug($query, 'query', array('is_manip' => $is_manip, 'when' => 'pre')); 761 774 if ($result) { 762 if ( PEAR::isError($result)) {775 if (MDB2::isError($result)) { 763 776 return $result; 764 777 } … … 772 785 if (is_null($connection)) { 773 786 $connection = $this->getConnection(); 774 if ( PEAR::isError($connection)) {787 if (MDB2::isError($connection)) { 775 788 return $connection; 776 789 } … … 794 807 ? 'mysql_query' : 'mysql_unbuffered_query'; 795 808 $result = @$function($query, $connection); 796 if (!$result ) {797 $err = &$this->raiseError(null, null, null,809 if (!$result && 0 !== mysql_errno($connection)) { 810 $err = $this->raiseError(null, null, null, 798 811 'Could not execute statement', __FUNCTION__); 799 812 return $err; … … 819 832 if (is_null($connection)) { 820 833 $connection = $this->getConnection(); 821 if ( PEAR::isError($connection)) {834 if (MDB2::isError($connection)) { 822 835 return $connection; 823 836 } … … 893 906 { 894 907 $connection = $this->getConnection(); 895 if ( PEAR::isError($connection)) {908 if (MDB2::isError($connection)) { 896 909 return $connection; 897 910 } … … 946 959 $this->start_transaction = false; 947 960 $this->varchar_max_length = 255; 948 961 949 962 $server_info = $this->getServerVersion(); 950 963 if (is_array($server_info)) { … … 984 997 985 998 /** 986 * Utility method, used by prepare() to avoid misinterpreting MySQL user 999 * Utility method, used by prepare() to avoid misinterpreting MySQL user 987 1000 * defined variables (SELECT @x:=5) for placeholders. 988 1001 * Check if the placeholder is a false positive, i.e. if it is an user defined … … 1033 1046 * @see bindParam, execute 1034 1047 */ 1035 function &prepare($query, $types = null, $result_types = null, $lobs = array()) 1036 { 1048 function prepare($query, $types = null, $result_types = null, $lobs = array()) 1049 { 1050 // connect to get server capabilities (http://pear.php.net/bugs/16147) 1051 $connection = $this->getConnection(); 1052 if (MDB2::isError($connection)) { 1053 return $connection; 1054 } 1055 1037 1056 if ($this->options['emulate_prepared'] 1038 1057 || $this->supported['prepared_statements'] !== true 1039 1058 ) { 1040 $obj =& parent::prepare($query, $types, $result_types, $lobs); 1041 return $obj; 1059 return parent::prepare($query, $types, $result_types, $lobs); 1042 1060 } 1043 1061 $is_manip = ($result_types === MDB2_PREPARE_MANIP); … … 1048 1066 $result = $this->debug($query, __FUNCTION__, array('is_manip' => $is_manip, 'when' => 'pre')); 1049 1067 if ($result) { 1050 if ( PEAR::isError($result)) {1068 if (MDB2::isError($result)) { 1051 1069 return $result; 1052 1070 } … … 1073 1091 $placeholder_type_guess = $query[$p_position]; 1074 1092 } 1075 1093 1076 1094 $new_pos = $this->_skipDelimitedStrings($query, $position, $p_position); 1077 if ( PEAR::isError($new_pos)) {1095 if (MDB2::isError($new_pos)) { 1078 1096 return $new_pos; 1079 1097 } … … 1082 1100 continue; //evaluate again starting from the new position 1083 1101 } 1084 1102 1085 1103 //make sure this is not part of an user defined variable 1086 1104 $new_pos = $this->_skipUserDefinedVariable($query, $position); … … 1099 1117 $parameter = preg_replace($regexp, '\\1', $query); 1100 1118 if ($parameter === '') { 1101 $err = &$this->raiseError(MDB2_ERROR_SYNTAX, null, null,1119 $err = $this->raiseError(MDB2_ERROR_SYNTAX, null, null, 1102 1120 'named parameter name must match "bindname_format" option', __FUNCTION__); 1103 1121 return $err; … … 1113 1131 } 1114 1132 } 1115 $connection = $this->getConnection(); 1116 if (PEAR::isError($connection)) { 1117 return $connection; 1118 } 1133 1119 1134 static $prep_statement_counter = 1; 1120 1135 $statement_name = sprintf($this->options['statement_format'], $this->phptype, $prep_statement_counter++ . sha1(microtime() + mt_rand())); 1121 1136 $statement_name = substr(strtolower($statement_name), 0, $this->options['max_identifiers_length']); 1122 1137 $query = "PREPARE $statement_name FROM ".$this->quote($query, 'text'); 1123 $statement = &$this->_doQuery($query, true, $connection);1124 if ( PEAR::isError($statement)) {1138 $statement = $this->_doQuery($query, true, $connection); 1139 if (MDB2::isError($statement)) { 1125 1140 return $statement; 1126 1141 } … … 1216 1231 $type = isset($fields[$name]['type']) ? $fields[$name]['type'] : null; 1217 1232 $value = $this->quote($fields[$name]['value'], $type); 1218 if ( PEAR::isError($value)) {1233 if (MDB2::isError($value)) { 1219 1234 return $value; 1220 1235 } … … 1235 1250 1236 1251 $connection = $this->getConnection(); 1237 if ( PEAR::isError($connection)) {1252 if (MDB2::isError($connection)) { 1238 1253 return $connection; 1239 1254 } … … 1241 1256 $table = $this->quoteIdentifier($table, true); 1242 1257 $query = "REPLACE INTO $table ($query) VALUES ($values)"; 1243 $result = &$this->_doQuery($query, true, $connection);1244 if ( PEAR::isError($result)) {1258 $result = $this->_doQuery($query, true, $connection); 1259 if (MDB2::isError($result)) { 1245 1260 return $result; 1246 1261 } … … 1269 1284 $this->pushErrorHandling(PEAR_ERROR_RETURN); 1270 1285 $this->expectError(MDB2_ERROR_NOSUCHTABLE); 1271 $result = &$this->_doQuery($query, true);1286 $result = $this->_doQuery($query, true); 1272 1287 $this->popExpect(); 1273 1288 $this->popErrorHandling(); 1274 if ( PEAR::isError($result)) {1289 if (MDB2::isError($result)) { 1275 1290 if ($ondemand && $result->getCode() == MDB2_ERROR_NOSUCHTABLE) { 1276 1291 $this->loadModule('Manager', null, true); 1277 1292 $result = $this->manager->createSequence($seq_name); 1278 if ( PEAR::isError($result)) {1293 if (MDB2::isError($result)) { 1279 1294 return $this->raiseError($result, null, null, 1280 1295 'on demand sequence '.$seq_name.' could not be created', __FUNCTION__); … … 1288 1303 if (is_numeric($value)) { 1289 1304 $query = "DELETE FROM $sequence_name WHERE $seqcol_name < $value"; 1290 $result = &$this->_doQuery($query, true);1291 if ( PEAR::isError($result)) {1305 $result = $this->_doQuery($query, true); 1306 if (MDB2::isError($result)) { 1292 1307 $this->warnings[] = 'nextID: could not delete previous sequence table values from '.$seq_name; 1293 1308 } … … 1311 1326 { 1312 1327 // not using mysql_insert_id() due to http://pear.php.net/bugs/bug.php?id=8051 1313 return $this->queryOne('SELECT LAST_INSERT_ID()', 'integer'); 1328 // not casting to integer to handle BIGINT http://pear.php.net/bugs/bug.php?id=17650 1329 return $this->queryOne('SELECT LAST_INSERT_ID()'); 1314 1330 } 1315 1331 … … 1353 1369 * @access public 1354 1370 */ 1355 function &fetchRow($fetchmode = MDB2_FETCHMODE_DEFAULT, $rownum = null)1371 function fetchRow($fetchmode = MDB2_FETCHMODE_DEFAULT, $rownum = null) 1356 1372 { 1357 1373 if (!is_null($rownum)) { 1358 1374 $seek = $this->seek($rownum); 1359 if ( PEAR::isError($seek)) {1375 if (MDB2::isError($seek)) { 1360 1376 return $seek; 1361 1377 } … … 1364 1380 $fetchmode = $this->db->fetchmode; 1365 1381 } 1366 if ($fetchmode & MDB2_FETCHMODE_ASSOC) { 1382 if ( $fetchmode == MDB2_FETCHMODE_ASSOC 1383 || $fetchmode == MDB2_FETCHMODE_OBJECT 1384 ) { 1367 1385 $row = @mysql_fetch_assoc($this->result); 1368 1386 if (is_array($row) … … 1377 1395 if (!$row) { 1378 1396 if ($this->result === false) { 1379 $err = &$this->db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null,1397 $err = $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null, 1380 1398 'resultset has already been freed', __FUNCTION__); 1381 1399 return $err; 1382 1400 } 1383 $null = null; 1384 return $null; 1401 return null; 1385 1402 } 1386 1403 $mode = $this->db->options['portability'] & MDB2_PORTABILITY_EMPTY_TO_NULL; … … 1396 1413 $this->db->_fixResultArrayValues($row, $mode); 1397 1414 } 1398 if (!empty($this->types)) { 1415 if ( ( $fetchmode != MDB2_FETCHMODE_ASSOC 1416 && $fetchmode != MDB2_FETCHMODE_OBJECT) 1417 && !empty($this->types) 1418 ) { 1399 1419 $row = $this->db->datatype->convertResultRow($this->types, $row, $rtrim); 1420 } elseif (($fetchmode == MDB2_FETCHMODE_ASSOC 1421 || $fetchmode == MDB2_FETCHMODE_OBJECT) 1422 && !empty($this->types_assoc) 1423 ) { 1424 $row = $this->db->datatype->convertResultRow($this->types_assoc, $row, $rtrim); 1400 1425 } 1401 1426 if (!empty($this->values)) { … … 1407 1432 $row = (object) $row; 1408 1433 } else { 1409 $row = &new $object_class($row); 1434 $rowObj = new $object_class($row); 1435 $row = $rowObj; 1410 1436 } 1411 1437 } … … 1430 1456 $columns = array(); 1431 1457 $numcols = $this->numCols(); 1432 if ( PEAR::isError($numcols)) {1458 if (MDB2::isError($numcols)) { 1433 1459 return $numcols; 1434 1460 } … … 1539 1565 { 1540 1566 $numrows = $this->numRows(); 1541 if ( PEAR::isError($numrows)) {1567 if (MDB2::isError($numrows)) { 1542 1568 return $numrows; 1543 1569 } … … 1569 1595 return $rows; 1570 1596 } 1597 1598 // }}} 1571 1599 } 1572 1600 … … 1592 1620 * @access private 1593 1621 */ 1594 function &_execute($result_class = true, $result_wrap_class = false)1622 function _execute($result_class = true, $result_wrap_class = true) 1595 1623 { 1596 1624 if (is_null($this->statement)) { 1597 $result = &parent::_execute($result_class, $result_wrap_class);1625 $result = parent::_execute($result_class, $result_wrap_class); 1598 1626 return $result; 1599 1627 } … … 1606 1634 1607 1635 $connection = $this->db->getConnection(); 1608 if ( PEAR::isError($connection)) {1636 if (MDB2::isError($connection)) { 1609 1637 return $connection; 1610 1638 } … … 1618 1646 'Unable to bind to missing placeholder: '.$parameter, __FUNCTION__); 1619 1647 } 1648 $close = false; 1620 1649 $value = $this->values[$parameter]; 1621 1650 $type = array_key_exists($parameter, $this->types) ? $this->types[$parameter] : null; … … 1640 1669 } 1641 1670 $quoted = $this->db->quote($value, $type); 1642 if ( PEAR::isError($quoted)) {1671 if (MDB2::isError($quoted)) { 1643 1672 return $quoted; 1644 1673 } 1645 1674 $param_query = 'SET @'.$parameter.' = '.$quoted; 1646 1675 $result = $this->db->_doQuery($param_query, true, $connection); 1647 if ( PEAR::isError($result)) {1676 if (MDB2::isError($result)) { 1648 1677 return $result; 1649 1678 } … … 1653 1682 1654 1683 $result = $this->db->_doQuery($query, $this->is_manip, $connection); 1655 if ( PEAR::isError($result)) {1684 if (MDB2::isError($result)) { 1656 1685 return $result; 1657 1686 } … … 1662 1691 } 1663 1692 1664 $result = &$this->db->_wrapResult($result, $this->result_types,1693 $result = $this->db->_wrapResult($result, $this->result_types, 1665 1694 $result_class, $result_wrap_class, $this->limit, $this->offset); 1666 1695 $this->db->debug($this->query, 'execute', array('is_manip' => $this->is_manip, 'when' => 'post', 'result' => $result)); … … 1687 1716 if (!is_null($this->statement)) { 1688 1717 $connection = $this->db->getConnection(); 1689 if ( PEAR::isError($connection)) {1718 if (MDB2::isError($connection)) { 1690 1719 return $connection; 1691 1720 }
Note: See TracChangeset
for help on using the changeset viewer.