Ibm
2 years ago
Abstract.php
2 years ago
Ibm.php
2 years ago
Mssql.php
2 years ago
Mysql.php
1 year ago
Oci.php
2 years ago
Pgsql.php
2 years ago
Sqlite.php
2 years ago
Abstract.php
359 lines
| 1 | <?php |
| 2 | |
| 3 | namespace { |
| 4 | /** |
| 5 | * Zend Framework |
| 6 | * |
| 7 | * LICENSE |
| 8 | * |
| 9 | * This source file is subject to the new BSD license that is bundled |
| 10 | * with this package in the file LICENSE.txt. |
| 11 | * It is also available through the world-wide-web at this URL: |
| 12 | * http://framework.zend.com/license/new-bsd |
| 13 | * If you did not receive a copy of the license and are unable to |
| 14 | * obtain it through the world-wide-web, please send an email |
| 15 | * to license@zend.com so we can send you a copy immediately. |
| 16 | * |
| 17 | * @category Zend |
| 18 | * @package Zend_Db |
| 19 | * @subpackage Adapter |
| 20 | * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) |
| 21 | * @license http://framework.zend.com/license/new-bsd New BSD License |
| 22 | * @version $Id: Abstract.php 23775 2011-03-01 17:25:24Z ralph $ |
| 23 | */ |
| 24 | /** |
| 25 | * @see Zend_Db_Adapter_Abstract |
| 26 | */ |
| 27 | // require_once 'Zend/Db/Adapter/Abstract.php'; |
| 28 | /** |
| 29 | * @see Zend_Db_Statement_Pdo |
| 30 | */ |
| 31 | // require_once 'Zend/Db/Statement/Pdo.php'; |
| 32 | /** |
| 33 | * Class for connecting to SQL databases and performing common operations using PDO. |
| 34 | * |
| 35 | * @category Zend |
| 36 | * @package Zend_Db |
| 37 | * @subpackage Adapter |
| 38 | * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) |
| 39 | * @license http://framework.zend.com/license/new-bsd New BSD License |
| 40 | */ |
| 41 | abstract class Zend_Db_Adapter_Pdo_Abstract extends \Zend_Db_Adapter_Abstract |
| 42 | { |
| 43 | /** |
| 44 | * Default class name for a DB statement. |
| 45 | * |
| 46 | * @var string |
| 47 | */ |
| 48 | protected $_defaultStmtClass = 'Zend_Db_Statement_Pdo'; |
| 49 | /** |
| 50 | * Creates a PDO DSN for the adapter from $this->_config settings. |
| 51 | * |
| 52 | * @return string |
| 53 | */ |
| 54 | protected function _dsn() |
| 55 | { |
| 56 | // baseline of DSN parts |
| 57 | $dsn = $this->_config; |
| 58 | // don't pass the username, password, charset, persistent and driver_options in the DSN |
| 59 | unset($dsn['username']); |
| 60 | unset($dsn['password']); |
| 61 | unset($dsn['options']); |
| 62 | unset($dsn['charset']); |
| 63 | unset($dsn['persistent']); |
| 64 | unset($dsn['driver_options']); |
| 65 | // use all remaining parts in the DSN |
| 66 | foreach ($dsn as $key => $val) { |
| 67 | $dsn[$key] = "{$key}={$val}"; |
| 68 | } |
| 69 | return $this->_pdoType . ':' . \implode(';', $dsn); |
| 70 | } |
| 71 | /** |
| 72 | * Creates a PDO object and connects to the database. |
| 73 | * |
| 74 | * @return void |
| 75 | * @throws Zend_Db_Adapter_Exception |
| 76 | */ |
| 77 | protected function _connect() |
| 78 | { |
| 79 | // if we already have a PDO object, no need to re-connect. |
| 80 | if ($this->_connection) { |
| 81 | return; |
| 82 | } |
| 83 | // get the dsn first, because some adapters alter the $_pdoType |
| 84 | $dsn = $this->_dsn(); |
| 85 | // check for PDO extension |
| 86 | if (!\extension_loaded('pdo')) { |
| 87 | /** |
| 88 | * @see Zend_Db_Adapter_Exception |
| 89 | */ |
| 90 | // require_once 'Zend/Db/Adapter/Exception.php'; |
| 91 | throw new \Zend_Db_Adapter_Exception('The PDO extension is required for this adapter but the extension is not loaded'); |
| 92 | } |
| 93 | // check the PDO driver is available |
| 94 | if (!\in_array($this->_pdoType, \PDO::getAvailableDrivers())) { |
| 95 | /** |
| 96 | * @see Zend_Db_Adapter_Exception |
| 97 | */ |
| 98 | // require_once 'Zend/Db/Adapter/Exception.php'; |
| 99 | throw new \Zend_Db_Adapter_Exception('The ' . $this->_pdoType . ' driver is not currently installed'); |
| 100 | } |
| 101 | // create PDO connection |
| 102 | $q = $this->_profiler->queryStart('connect', \Zend_Db_Profiler::CONNECT); |
| 103 | // add the persistence flag if we find it in our config array |
| 104 | if (isset($this->_config['persistent']) && $this->_config['persistent'] == \true) { |
| 105 | $this->_config['driver_options'][\PDO::ATTR_PERSISTENT] = \true; |
| 106 | } |
| 107 | try { |
| 108 | $this->_connection = new \PDO($dsn, $this->_config['username'] ?? null, $this->_config['password'] ?? null, $this->_config['driver_options'] ?? null); |
| 109 | $this->_profiler->queryEnd($q); |
| 110 | // set the PDO connection to perform case-folding on array keys, or not |
| 111 | $this->_connection->setAttribute(\PDO::ATTR_CASE, $this->_caseFolding); |
| 112 | // always use exceptions. |
| 113 | $this->_connection->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); |
| 114 | } catch (\PDOException $e) { |
| 115 | /** |
| 116 | * @see Zend_Db_Adapter_Exception |
| 117 | */ |
| 118 | // require_once 'Zend/Db/Adapter/Exception.php'; |
| 119 | throw new \Zend_Db_Adapter_Exception($e->getMessage(), $e->getCode(), $e); |
| 120 | } |
| 121 | } |
| 122 | /** |
| 123 | * Test if a connection is active |
| 124 | * |
| 125 | * @return boolean |
| 126 | */ |
| 127 | public function isConnected() |
| 128 | { |
| 129 | return (bool) ($this->_connection instanceof \PDO); |
| 130 | } |
| 131 | /** |
| 132 | * Force the connection to close. |
| 133 | * |
| 134 | * @return void |
| 135 | */ |
| 136 | public function closeConnection() |
| 137 | { |
| 138 | $this->_connection = null; |
| 139 | } |
| 140 | /** |
| 141 | * Prepares an SQL statement. |
| 142 | * |
| 143 | * @param string $sql The SQL statement with placeholders. |
| 144 | * @param array $bind An array of data to bind to the placeholders. |
| 145 | * @return PDOStatement |
| 146 | */ |
| 147 | public function prepare($sql) |
| 148 | { |
| 149 | $this->_connect(); |
| 150 | $stmtClass = $this->_defaultStmtClass; |
| 151 | if (!\class_exists($stmtClass)) { |
| 152 | // require_once 'Zend/Loader.php'; |
| 153 | \Zend_Loader::loadClass($stmtClass); |
| 154 | } |
| 155 | $stmt = new $stmtClass($this, $sql); |
| 156 | $stmt->setFetchMode($this->_fetchMode); |
| 157 | return $stmt; |
| 158 | } |
| 159 | /** |
| 160 | * Gets the last ID generated automatically by an IDENTITY/AUTOINCREMENT column. |
| 161 | * |
| 162 | * As a convention, on RDBMS brands that support sequences |
| 163 | * (e.g. Oracle, PostgreSQL, DB2), this method forms the name of a sequence |
| 164 | * from the arguments and returns the last id generated by that sequence. |
| 165 | * On RDBMS brands that support IDENTITY/AUTOINCREMENT columns, this method |
| 166 | * returns the last value generated for such a column, and the table name |
| 167 | * argument is disregarded. |
| 168 | * |
| 169 | * On RDBMS brands that don't support sequences, $tableName and $primaryKey |
| 170 | * are ignored. |
| 171 | * |
| 172 | * @param string $tableName OPTIONAL Name of table. |
| 173 | * @param string $primaryKey OPTIONAL Name of primary key column. |
| 174 | * @return string |
| 175 | */ |
| 176 | public function lastInsertId($tableName = null, $primaryKey = null) |
| 177 | { |
| 178 | $this->_connect(); |
| 179 | return $this->_connection->lastInsertId(); |
| 180 | } |
| 181 | /** |
| 182 | * Special handling for PDO query(). |
| 183 | * All bind parameter names must begin with ':' |
| 184 | * |
| 185 | * @param string|Zend_Db_Select $sql The SQL statement with placeholders. |
| 186 | * @param array $bind An array of data to bind to the placeholders. |
| 187 | * @return Zend_Db_Statement_Pdo |
| 188 | * @throws Zend_Db_Adapter_Exception To re-throw PDOException. |
| 189 | */ |
| 190 | public function query($sql, $bind = array()) |
| 191 | { |
| 192 | if (empty($bind) && $sql instanceof \Zend_Db_Select) { |
| 193 | $bind = $sql->getBind(); |
| 194 | } |
| 195 | if (\is_array($bind)) { |
| 196 | foreach ($bind as $name => $value) { |
| 197 | if (!\is_int($name) && !\preg_match('/^:/', $name)) { |
| 198 | $newName = ":{$name}"; |
| 199 | unset($bind[$name]); |
| 200 | $bind[$newName] = $value; |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | try { |
| 205 | return parent::query($sql, $bind); |
| 206 | } catch (\PDOException $e) { |
| 207 | /** |
| 208 | * @see Zend_Db_Statement_Exception |
| 209 | */ |
| 210 | // require_once 'Zend/Db/Statement/Exception.php'; |
| 211 | throw new \Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e); |
| 212 | } |
| 213 | } |
| 214 | /** |
| 215 | * Executes an SQL statement and return the number of affected rows |
| 216 | * |
| 217 | * @param mixed $sql The SQL statement with placeholders. |
| 218 | * May be a string or Zend_Db_Select. |
| 219 | * @return integer Number of rows that were modified |
| 220 | * or deleted by the SQL statement |
| 221 | */ |
| 222 | public function exec($sql) |
| 223 | { |
| 224 | if ($sql instanceof \Zend_Db_Select) { |
| 225 | $sql = $sql->assemble(); |
| 226 | } |
| 227 | try { |
| 228 | $affected = $this->getConnection()->exec($sql); |
| 229 | if ($affected === \false) { |
| 230 | $errorInfo = $this->getConnection()->errorInfo(); |
| 231 | /** |
| 232 | * @see Zend_Db_Adapter_Exception |
| 233 | */ |
| 234 | // require_once 'Zend/Db/Adapter/Exception.php'; |
| 235 | throw new \Zend_Db_Adapter_Exception($errorInfo[2]); |
| 236 | } |
| 237 | return $affected; |
| 238 | } catch (\PDOException $e) { |
| 239 | /** |
| 240 | * @see Zend_Db_Adapter_Exception |
| 241 | */ |
| 242 | // require_once 'Zend/Db/Adapter/Exception.php'; |
| 243 | throw new \Zend_Db_Adapter_Exception($e->getMessage(), $e->getCode(), $e); |
| 244 | } |
| 245 | } |
| 246 | /** |
| 247 | * Quote a raw string. |
| 248 | * |
| 249 | * @param string $value Raw string |
| 250 | * @return string Quoted string |
| 251 | */ |
| 252 | protected function _quote($value) |
| 253 | { |
| 254 | if (\is_int($value) || \is_float($value)) { |
| 255 | return $value; |
| 256 | } |
| 257 | $this->_connect(); |
| 258 | return $this->_connection->quote($value); |
| 259 | } |
| 260 | /** |
| 261 | * Begin a transaction. |
| 262 | */ |
| 263 | protected function _beginTransaction() |
| 264 | { |
| 265 | $this->_connect(); |
| 266 | $this->_connection->beginTransaction(); |
| 267 | } |
| 268 | /** |
| 269 | * Commit a transaction. |
| 270 | */ |
| 271 | protected function _commit() |
| 272 | { |
| 273 | $this->_connect(); |
| 274 | $this->_connection->commit(); |
| 275 | } |
| 276 | /** |
| 277 | * Roll-back a transaction. |
| 278 | */ |
| 279 | protected function _rollBack() |
| 280 | { |
| 281 | $this->_connect(); |
| 282 | $this->_connection->rollBack(); |
| 283 | } |
| 284 | /** |
| 285 | * Set the PDO fetch mode. |
| 286 | * |
| 287 | * @todo Support FETCH_CLASS and FETCH_INTO. |
| 288 | * |
| 289 | * @param int $mode A PDO fetch mode. |
| 290 | * @return void |
| 291 | * @throws Zend_Db_Adapter_Exception |
| 292 | */ |
| 293 | public function setFetchMode($mode) |
| 294 | { |
| 295 | //check for PDO extension |
| 296 | if (!\extension_loaded('pdo')) { |
| 297 | /** |
| 298 | * @see Zend_Db_Adapter_Exception |
| 299 | */ |
| 300 | // require_once 'Zend/Db/Adapter/Exception.php'; |
| 301 | throw new \Zend_Db_Adapter_Exception('The PDO extension is required for this adapter but the extension is not loaded'); |
| 302 | } |
| 303 | switch ($mode) { |
| 304 | case \PDO::FETCH_LAZY: |
| 305 | case \PDO::FETCH_ASSOC: |
| 306 | case \PDO::FETCH_NUM: |
| 307 | case \PDO::FETCH_BOTH: |
| 308 | case \PDO::FETCH_NAMED: |
| 309 | case \PDO::FETCH_OBJ: |
| 310 | $this->_fetchMode = $mode; |
| 311 | break; |
| 312 | default: |
| 313 | /** |
| 314 | * @see Zend_Db_Adapter_Exception |
| 315 | */ |
| 316 | // require_once 'Zend/Db/Adapter/Exception.php'; |
| 317 | throw new \Zend_Db_Adapter_Exception("Invalid fetch mode '{$mode}' specified"); |
| 318 | break; |
| 319 | } |
| 320 | } |
| 321 | /** |
| 322 | * Check if the adapter supports real SQL parameters. |
| 323 | * |
| 324 | * @param string $type 'positional' or 'named' |
| 325 | * @return bool |
| 326 | */ |
| 327 | public function supportsParameters($type) |
| 328 | { |
| 329 | switch ($type) { |
| 330 | case 'positional': |
| 331 | case 'named': |
| 332 | default: |
| 333 | return \true; |
| 334 | } |
| 335 | } |
| 336 | /** |
| 337 | * Retrieve server version in PHP style |
| 338 | * |
| 339 | * @return string |
| 340 | */ |
| 341 | public function getServerVersion() |
| 342 | { |
| 343 | $this->_connect(); |
| 344 | try { |
| 345 | $version = $this->_connection->getAttribute(\PDO::ATTR_SERVER_VERSION); |
| 346 | } catch (\PDOException $e) { |
| 347 | // In case of the driver doesn't support getting attributes |
| 348 | return null; |
| 349 | } |
| 350 | $matches = null; |
| 351 | if (\preg_match('/((?:[0-9]{1,2}\\.){1,3}[0-9]{1,2})/', $version, $matches)) { |
| 352 | return $matches[1]; |
| 353 | } else { |
| 354 | return null; |
| 355 | } |
| 356 | } |
| 357 | } |
| 358 | } |
| 359 |