📄 abstract.php
字号:
<?php/** * Zend Framework * * LICENSE * * This source file is subject to the new BSD license that is bundled * with this package in the file LICENSE.txt. * It is also available through the world-wide-web at this URL: * http://framework.zend.com/license/new-bsd * If you did not receive a copy of the license and are unable to * obtain it through the world-wide-web, please send an email * to license@zend.com so we can send you a copy immediately. * * @category Zend * @package Zend_Db * @subpackage Adapter * @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License * @version $Id: Abstract.php 469 2008-05-08 22:50:56Z matt $ *//** * @see Zend_Db */require_once 'Zend/Db.php';/** * @see Zend_Db_Profiler */require_once 'Zend/Db/Profiler.php';/** * @see Zend_Db_Select */require_once 'Zend/Db/Select.php';/** * Class for connecting to SQL databases and performing common operations. * * @category Zend * @package Zend_Db * @subpackage Adapter * @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */abstract class Zend_Db_Adapter_Abstract{ /** * Added by Piwik */ public function resetConfigArray() { $this->_config = array(); } /** * User-provided configuration * * @var array */ protected $_config = array(); /** * Fetch mode * * @var integer */ protected $_fetchMode = Zend_Db::FETCH_ASSOC; /** * Query profiler * * @var Zend_Db_Profiler */ protected $_profiler; /** * Database connection * * @var object|resource|null */ protected $_connection = null; /** * Specifies the case of column names retrieved in queries * Options * Zend_Db::CASE_NATURAL (default) * Zend_Db::CASE_LOWER * Zend_Db::CASE_UPPER * * @access protected */ protected $_caseFolding = Zend_Db::CASE_NATURAL; /** * Specifies whether the adapter automatically quotes identifiers. * If true, most SQL generated by Zend_Db classes applies * identifier quoting automatically. * If false, developer must quote identifiers themselves * by calling quoteIdentifier(). * * @access protected */ protected $_autoQuoteIdentifiers = true; /** * Constructor. * * $config is an array of key/value pairs containing configuration * options. These options are common to most adapters: * * dbname => (string) The name of the database to user * username => (string) Connect to the database as this username. * password => (string) Password associated with the username. * host => (string) What host to connect to, defaults to localhost * * Some options are used on a case-by-case basis by adapters: * * port => (string) The port of the database * persistent => (boolean) Whether to use a persistent connection or not, defaults to false * protocol => (string) The network protocol, defaults to TCPIP * caseFolding => (int) * * @param array $config An array of configuration keys. * @throws Zend_Db_Adapter_Exception */ public function __construct(array $config = array()) { $this->_checkRequiredOptions($config); $options = array( Zend_Db::CASE_FOLDING => $this->_caseFolding, Zend_DB::AUTO_QUOTE_IDENTIFIERS => $this->_autoQuoteIdentifiers ); $driver_options = array(); // normalize the config and merge it with the defaults if (array_key_exists('options', $config)) { // can't use array_merge() because keys might be integers foreach ((array) $config['options'] as $key => $value) { $options[$key] = $value; } } if (array_key_exists('driver_options', $config)) { // can't use array_merge() because keys might be integers foreach ((array) $config['driver_options'] as $key => $value) { $driver_options[$key] = $value; } } $this->_config = array_merge($this->_config, $config); $this->_config['options'] = $options; $this->_config['driver_options'] = $driver_options; // obtain the case setting, if there is one if (array_key_exists(Zend_Db::CASE_FOLDING, $options)) { $case = (int) $options[Zend_Db::CASE_FOLDING]; switch ($case) { case Zend_Db::CASE_LOWER: case Zend_Db::CASE_UPPER: case Zend_Db::CASE_NATURAL: $this->_caseFolding = $case; break; default: require_once 'Zend/Db/Adapter/Exception.php'; throw new Zend_Db_Adapter_Exception("Case must be one of the following constants: Zend_Db::CASE_NATURAL, Zend_Db::CASE_LOWER, Zend_Db::CASE_UPPER"); } } // obtain quoting property if there is one if (array_key_exists(Zend_Db::AUTO_QUOTE_IDENTIFIERS, $options)) { $this->_autoQuoteIdentifiers = (bool) $options[Zend_Db::AUTO_QUOTE_IDENTIFIERS]; } // create a profiler object $enabled = false; if (array_key_exists('profiler', $this->_config)) { $enabled = (bool) $this->_config['profiler']; unset($this->_config['profiler']); } $this->_profiler = new Zend_Db_Profiler($enabled); } /** * Check for config options that are mandatory. * Throw exceptions if any are missing. * * @param array $config * @throws Zend_Db_Adapter_Exception */ protected function _checkRequiredOptions(array $config) { // we need at least a dbname if (! array_key_exists('dbname', $config)) { require_once 'Zend/Db/Adapter/Exception.php'; throw new Zend_Db_Adapter_Exception("Configuration array must have a key for 'dbname' that names the database instance."); } if (! array_key_exists('password', $config)) { /** * @see Zend_Db_Adapter_Exception */ require_once 'Zend/Db/Adapter/Exception.php'; throw new Zend_Db_Adapter_Exception("Configuration array must have a key for 'password' for login credentials."); } if (! array_key_exists('username', $config)) { /** * @see Zend_Db_Adapter_Exception */ require_once 'Zend/Db/Adapter/Exception.php'; throw new Zend_Db_Adapter_Exception("Configuration array must have a key for 'username' for login credentials."); } } /** * Returns the underlying database connection object or resource. * If not presently connected, this initiates the connection. * * @return object|resource|null */ public function getConnection() { $this->_connect(); return $this->_connection; } /** * Returns the profiler for this adapter. * * @return Zend_Db_Profiler */ public function getProfiler() { return $this->_profiler; } /** * Prepares and executes an SQL statement with bound data. * * @param mixed $sql The SQL statement with placeholders. * May be a string or Zend_Db_Select. * @param mixed $bind An array of data to bind to the placeholders. * @return Zend_Db_Statement_Interface */ public function query($sql, $bind = array()) { // connect to the database if needed $this->_connect(); // is the $sql a Zend_Db_Select object? if ($sql instanceof Zend_Db_Select) { $sql = $sql->__toString(); } // make sure $bind to an array; // don't use (array) typecasting because // because $bind may be a Zend_Db_Expr object if (!is_array($bind)) { $bind = array($bind); } // prepare and execute the statement with profiling $stmt = $this->prepare($sql); $stmt->execute($bind); // return the results embedded in the prepared statement object $stmt->setFetchMode($this->_fetchMode); return $stmt; } /** * Leave autocommit mode and begin a transaction. * * @return bool True */ public function beginTransaction() { $this->_connect(); $q = $this->_profiler->queryStart('begin', Zend_Db_Profiler::TRANSACTION); $this->_beginTransaction(); $this->_profiler->queryEnd($q); return true; } /** * Commit a transaction and return to autocommit mode. * * @return bool True */ public function commit() { $this->_connect(); $q = $this->_profiler->queryStart('commit', Zend_Db_Profiler::TRANSACTION); $this->_commit(); $this->_profiler->queryEnd($q); return true; } /** * Roll back a transaction and return to autocommit mode. * * @return bool True */ public function rollBack() { $this->_connect(); $q = $this->_profiler->queryStart('rollback', Zend_Db_Profiler::TRANSACTION); $this->_rollBack(); $this->_profiler->queryEnd($q); return true; } /** * Inserts a table row with specified data. * * @param mixed $table The table to insert data into. * @param array $bind Column-value pairs. * @return int The number of affected rows. */ public function insert($table, array $bind) { // extract and quote col names from the array keys $cols = array(); $vals = array(); foreach ($bind as $col => $val) { $cols[] = $this->quoteIdentifier($col, true); if ($val instanceof Zend_Db_Expr) { $vals[] = $val->__toString(); unset($bind[$col]); } else { $vals[] = '?'; } } // build the statement $sql = "INSERT INTO " . $this->quoteIdentifier($table, true) . ' (' . implode(', ', $cols) . ') ' . 'VALUES (' . implode(', ', $vals) . ')'; // execute the statement and return the number of affected rows $stmt = $this->query($sql, array_values($bind)); $result = $stmt->rowCount(); return $result; } /** * Updates table rows with specified data based on a WHERE clause. * * @param mixed $table The table to update. * @param array $bind Column-value pairs. * @param mixed $where UPDATE WHERE clause(s). * @return int The number of affected rows. */ public function update($table, array $bind, $where = '') { /** * Build "col = ?" pairs for the statement, * except for Zend_Db_Expr which is treated literally. */ $set = array(); foreach ($bind as $col => $val) { if ($val instanceof Zend_Db_Expr) { $val = $val->__toString(); unset($bind[$col]); } else { $val = '?'; } $set[] = $this->quoteIdentifier($col, true) . ' = ' . $val; } $where = $this->_whereExpr($where); /** * Build the UPDATE statement */ $sql = "UPDATE " . $this->quoteIdentifier($table, true) . ' SET ' . implode(', ', $set) . (($where) ? " WHERE $where" : ''); /** * Execute the statement and return the number of affected rows */ $stmt = $this->query($sql, array_values($bind)); $result = $stmt->rowCount(); return $result; } /** * Deletes table rows based on a WHERE clause. * * @param mixed $table The table to update. * @param mixed $where DELETE WHERE clause(s). * @return int The number of affected rows. */ public function delete($table, $where = '') { $where = $this->_whereExpr($where); /** * Build the DELETE statement */ $sql = "DELETE FROM " . $this->quoteIdentifier($table, true) . (($where) ? " WHERE $where" : ''); /** * Execute the statement and return the number of affected rows */ $stmt = $this->query($sql); $result = $stmt->rowCount(); return $result; } /** * Convert an array, string, or Zend_Db_Expr object * into a string to put in a WHERE clause. * * @param mixed $where * @return string */ protected function _whereExpr($where) { if (empty($where)) { return $where; } if (!is_array($where)) { $where = array($where); } foreach ($where as &$term) { if ($term instanceof Zend_Db_Expr) { $term = $term->__toString(); } $term = '(' . $term . ')'; } $where = implode(' AND ', $where); return $where; } /** * Creates and returns a new Zend_Db_Select object for this adapter. * * @return Zend_Db_Select */ public function select() { return new Zend_Db_Select($this); } /** * Get the fetch mode. * * @return int */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -