📄 mysql.php
字号:
<?php// vim: set et ts=4 sw=4 fdm=marker:// +----------------------------------------------------------------------+// | PHP versions 4 and 5 |// +----------------------------------------------------------------------+// | Copyright (c) 1998-2008 Manuel Lemos, Tomas V.V.Cox, |// | Stig. S. Bakken, Lukas Smith |// | All rights reserved. |// +----------------------------------------------------------------------+// | MDB2 is a merge of PEAR DB and Metabases that provides a unified DB |// | API as well as database abstraction for PHP applications. |// | This LICENSE is in the BSD license style. |// | |// | Redistribution and use in source and binary forms, with or without |// | modification, are permitted provided that the following conditions |// | are met: |// | |// | Redistributions of source code must retain the above copyright |// | notice, this list of conditions and the following disclaimer. |// | |// | Redistributions in binary form must reproduce the above copyright |// | notice, this list of conditions and the following disclaimer in the |// | documentation and/or other materials provided with the distribution. |// | |// | Neither the name of Manuel Lemos, Tomas V.V.Cox, Stig. S. Bakken, |// | Lukas Smith nor the names of his contributors may be used to endorse |// | or promote products derived from this software without specific prior|// | written permission. |// | |// | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |// | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |// | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |// | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |// | REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |// | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |// | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS|// | OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |// | AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |// | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY|// | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |// | POSSIBILITY OF SUCH DAMAGE. |// +----------------------------------------------------------------------+// | Author: Lukas Smith <smith@pooteeweet.org> |// +----------------------------------------------------------------------+//// $Id: mysql.php,v 1.208 2008/03/13 03:31:55 afz Exp $///** * MDB2 MySQL driver * * @package MDB2 * @category Database * @author Lukas Smith <smith@pooteeweet.org> */class MDB2_Driver_mysql extends MDB2_Driver_Common{ // {{{ properties var $string_quoting = array('start' => "'", 'end' => "'", 'escape' => '\\', 'escape_pattern' => '\\'); var $identifier_quoting = array('start' => '`', 'end' => '`', 'escape' => '`'); var $sql_comments = array( array('start' => '-- ', 'end' => "\n", 'escape' => false), array('start' => '#', 'end' => "\n", 'escape' => false), array('start' => '/*', 'end' => '*/', 'escape' => false), ); var $server_capabilities_checked = false; var $start_transaction = false; var $varchar_max_length = 255; // }}} // {{{ constructor /** * Constructor */ function __construct() { parent::__construct(); $this->phptype = 'mysql'; $this->dbsyntax = 'mysql'; $this->supported['sequences'] = 'emulated'; $this->supported['indexes'] = true; $this->supported['affected_rows'] = true; $this->supported['transactions'] = false; $this->supported['savepoints'] = false; $this->supported['summary_functions'] = true; $this->supported['order_by_text'] = true; $this->supported['current_id'] = 'emulated'; $this->supported['limit_queries'] = true; $this->supported['LOBs'] = true; $this->supported['replace'] = true; $this->supported['sub_selects'] = 'emulated'; $this->supported['triggers'] = false; $this->supported['auto_increment'] = true; $this->supported['primary_key'] = true; $this->supported['result_introspection'] = true; $this->supported['prepared_statements'] = 'emulated'; $this->supported['identifier_quoting'] = true; $this->supported['pattern_escaping'] = true; $this->supported['new_link'] = true; $this->options['DBA_username'] = false; $this->options['DBA_password'] = false; $this->options['default_table_type'] = ''; $this->options['max_identifiers_length'] = 64; $this->_reCheckSupportedOptions(); } // }}} // {{{ _reCheckSupportedOptions() /** * If the user changes certain options, other capabilities may depend * on the new settings, so we need to check them (again). * * @access private */ function _reCheckSupportedOptions() { $this->supported['transactions'] = $this->options['use_transactions']; $this->supported['savepoints'] = $this->options['use_transactions']; if ($this->options['default_table_type']) { switch (strtoupper($this->options['default_table_type'])) { case 'BLACKHOLE': case 'MEMORY': case 'ARCHIVE': case 'CSV': case 'HEAP': case 'ISAM': case 'MERGE': case 'MRG_ISAM': case 'ISAM': case 'MRG_MYISAM': case 'MYISAM': $this->supported['savepoints'] = false; $this->supported['transactions'] = false; $this->warnings[] = $this->options['default_table_type'] . ' is not a supported default table type'; break; } } } // }}} // {{{ function setOption($option, $value) /** * set the option for the db class * * @param string option name * @param mixed value for the option * * @return mixed MDB2_OK or MDB2 Error Object * * @access public */ function setOption($option, $value) { $res = parent::setOption($option, $value); $this->_reCheckSupportedOptions(); } // }}} // {{{ errorInfo() /** * This method is used to collect information about an error * * @param integer $error * @return array * @access public */ function errorInfo($error = null) { if ($this->connection) { $native_code = @mysql_errno($this->connection); $native_msg = @mysql_error($this->connection); } else { $native_code = @mysql_errno(); $native_msg = @mysql_error(); } if (is_null($error)) { static $ecode_map; if (empty($ecode_map)) { $ecode_map = array( 1000 => MDB2_ERROR_INVALID, //hashchk 1001 => MDB2_ERROR_INVALID, //isamchk 1004 => MDB2_ERROR_CANNOT_CREATE, 1005 => MDB2_ERROR_CANNOT_CREATE, 1006 => MDB2_ERROR_CANNOT_CREATE, 1007 => MDB2_ERROR_ALREADY_EXISTS, 1008 => MDB2_ERROR_CANNOT_DROP, 1009 => MDB2_ERROR_CANNOT_DROP, 1010 => MDB2_ERROR_CANNOT_DROP, 1011 => MDB2_ERROR_CANNOT_DELETE, 1022 => MDB2_ERROR_ALREADY_EXISTS, 1029 => MDB2_ERROR_NOT_FOUND, 1032 => MDB2_ERROR_NOT_FOUND, 1044 => MDB2_ERROR_ACCESS_VIOLATION, 1045 => MDB2_ERROR_ACCESS_VIOLATION, 1046 => MDB2_ERROR_NODBSELECTED, 1048 => MDB2_ERROR_CONSTRAINT, 1049 => MDB2_ERROR_NOSUCHDB, 1050 => MDB2_ERROR_ALREADY_EXISTS, 1051 => MDB2_ERROR_NOSUCHTABLE, 1054 => MDB2_ERROR_NOSUCHFIELD, 1060 => MDB2_ERROR_ALREADY_EXISTS, 1061 => MDB2_ERROR_ALREADY_EXISTS, 1062 => MDB2_ERROR_ALREADY_EXISTS, 1064 => MDB2_ERROR_SYNTAX, 1067 => MDB2_ERROR_INVALID, 1072 => MDB2_ERROR_NOT_FOUND, 1086 => MDB2_ERROR_ALREADY_EXISTS, 1091 => MDB2_ERROR_NOT_FOUND, 1100 => MDB2_ERROR_NOT_LOCKED, 1109 => MDB2_ERROR_NOT_FOUND, 1125 => MDB2_ERROR_ALREADY_EXISTS, 1136 => MDB2_ERROR_VALUE_COUNT_ON_ROW, 1138 => MDB2_ERROR_INVALID, 1142 => MDB2_ERROR_ACCESS_VIOLATION, 1143 => MDB2_ERROR_ACCESS_VIOLATION, 1146 => MDB2_ERROR_NOSUCHTABLE, 1149 => MDB2_ERROR_SYNTAX, 1169 => MDB2_ERROR_CONSTRAINT, 1176 => MDB2_ERROR_NOT_FOUND, 1177 => MDB2_ERROR_NOSUCHTABLE, 1213 => MDB2_ERROR_DEADLOCK, 1216 => MDB2_ERROR_CONSTRAINT, 1217 => MDB2_ERROR_CONSTRAINT, 1227 => MDB2_ERROR_ACCESS_VIOLATION, 1235 => MDB2_ERROR_CANNOT_CREATE, 1299 => MDB2_ERROR_INVALID_DATE, 1300 => MDB2_ERROR_INVALID, 1304 => MDB2_ERROR_ALREADY_EXISTS, 1305 => MDB2_ERROR_NOT_FOUND, 1306 => MDB2_ERROR_CANNOT_DROP, 1307 => MDB2_ERROR_CANNOT_CREATE, 1334 => MDB2_ERROR_CANNOT_ALTER, 1339 => MDB2_ERROR_NOT_FOUND, 1356 => MDB2_ERROR_INVALID, 1359 => MDB2_ERROR_ALREADY_EXISTS, 1360 => MDB2_ERROR_NOT_FOUND, 1363 => MDB2_ERROR_NOT_FOUND, 1365 => MDB2_ERROR_DIVZERO, 1451 => MDB2_ERROR_CONSTRAINT, 1452 => MDB2_ERROR_CONSTRAINT, 1542 => MDB2_ERROR_CANNOT_DROP, 1546 => MDB2_ERROR_CONSTRAINT, 1582 => MDB2_ERROR_CONSTRAINT, 2003 => MDB2_ERROR_CONNECT_FAILED, 2019 => MDB2_ERROR_INVALID, ); } if ($this->options['portability'] & MDB2_PORTABILITY_ERRORS) { $ecode_map[1022] = MDB2_ERROR_CONSTRAINT; $ecode_map[1048] = MDB2_ERROR_CONSTRAINT_NOT_NULL; $ecode_map[1062] = MDB2_ERROR_CONSTRAINT; } else { // Doing this in case mode changes during runtime. $ecode_map[1022] = MDB2_ERROR_ALREADY_EXISTS; $ecode_map[1048] = MDB2_ERROR_CONSTRAINT; $ecode_map[1062] = MDB2_ERROR_ALREADY_EXISTS; } if (isset($ecode_map[$native_code])) { $error = $ecode_map[$native_code]; } } return array($error, $native_code, $native_msg); } // }}} // {{{ escape() /** * Quotes a string so it can be safely used in a query. It will quote * the text so it can safely be used within a query. * * @param string the input string to quote * @param bool escape wildcards * * @return string quoted string * * @access public */ function escape($text, $escape_wildcards = false) { if ($escape_wildcards) { $text = $this->escapePattern($text); } $connection = $this->getConnection(); if (PEAR::isError($connection)) { return $connection; } $text = @mysql_real_escape_string($text, $connection); return $text; } // }}} // {{{ beginTransaction() /** * Start a transaction or set a savepoint. * * @param string name of a savepoint to set * @return mixed MDB2_OK on success, a MDB2 error on failure * * @access public */ function beginTransaction($savepoint = null) { $this->debug('Starting transaction/savepoint', __FUNCTION__, array('is_manip' => true, 'savepoint' => $savepoint)); $this->_getServerCapabilities(); if (!is_null($savepoint)) { if (!$this->supports('savepoints')) { return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null, 'savepoints are not supported', __FUNCTION__); } if (!$this->in_transaction) { return $this->raiseError(MDB2_ERROR_INVALID, null, null, 'savepoint cannot be released when changes are auto committed', __FUNCTION__); } $query = 'SAVEPOINT '.$savepoint; return $this->_doQuery($query, true); } elseif ($this->in_transaction) { return MDB2_OK; //nothing to do } if (!$this->destructor_registered && $this->opened_persistent) { $this->destructor_registered = true;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -