00001 <?php
00027 require_once 'Zend/Db/Adapter/Abstract.php';
00028
00029
00033 require_once 'Zend/Db/Statement/Pdo.php';
00034
00035
00045 abstract class Zend_Db_Adapter_Pdo_Abstract extends Zend_Db_Adapter_Abstract
00046 {
00047
00053 protected $_defaultStmtClass = 'Zend_Db_Statement_Pdo';
00054
00060 protected function _dsn()
00061 {
00062
00063 $dsn = $this->_config;
00064
00065
00066 unset($dsn['username']);
00067 unset($dsn['password']);
00068 unset($dsn['options']);
00069 unset($dsn['charset']);
00070 unset($dsn['persistent']);
00071 unset($dsn['driver_options']);
00072
00073
00074 foreach ($dsn as $key => $val) {
00075 $dsn[$key] = "$key=$val";
00076 }
00077
00078 return $this->_pdoType . ':' . implode(';', $dsn);
00079 }
00080
00087 protected function _connect()
00088 {
00089
00090 if ($this->_connection) {
00091 return;
00092 }
00093
00094
00095 $dsn = $this->_dsn();
00096
00097
00098 if (!extension_loaded('pdo')) {
00102 require_once 'Zend/Db/Adapter/Exception.php';
00103 throw new Zend_Db_Adapter_Exception('The PDO extension is required for this adapter but the extension is not loaded');
00104 }
00105
00106
00107 if (!in_array($this->_pdoType, PDO::getAvailableDrivers())) {
00111 require_once 'Zend/Db/Adapter/Exception.php';
00112 throw new Zend_Db_Adapter_Exception('The ' . $this->_pdoType . ' driver is not currently installed');
00113 }
00114
00115
00116 $q = $this->_profiler->queryStart('connect', Zend_Db_Profiler::CONNECT);
00117
00118
00119 if (isset($this->_config['persistent']) && ($this->_config['persistent'] == true)) {
00120 $this->_config['driver_options'][PDO::ATTR_PERSISTENT] = true;
00121 }
00122
00123 try {
00124 $this->_connection = new PDO(
00125 $dsn,
00126 $this->_config['username'],
00127 $this->_config['password'],
00128 $this->_config['driver_options']
00129 );
00130
00131 $this->_profiler->queryEnd($q);
00132
00133
00134 $this->_connection->setAttribute(PDO::ATTR_CASE, $this->_caseFolding);
00135
00136
00137 $this->_connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
00138
00139 } catch (PDOException $e) {
00143 require_once 'Zend/Db/Adapter/Exception.php';
00144 throw new Zend_Db_Adapter_Exception($e->getMessage(), $e);
00145 }
00146
00147 }
00148
00154 public function isConnected()
00155 {
00156 return ((bool) ($this->_connection instanceof PDO));
00157 }
00158
00164 public function closeConnection()
00165 {
00166 $this->_connection = null;
00167 }
00168
00176 public function prepare($sql)
00177 {
00178 $this->_connect();
00179 $stmtClass = $this->_defaultStmtClass;
00180 if (!class_exists($stmtClass)) {
00181 require_once 'Zend/Loader.php';
00182 Zend_Loader::loadClass($stmtClass);
00183 }
00184 $stmt = new $stmtClass($this, $sql);
00185 $stmt->setFetchMode($this->_fetchMode);
00186 return $stmt;
00187 }
00188
00206 public function lastInsertId($tableName = null, $primaryKey = null)
00207 {
00208 $this->_connect();
00209 return $this->_connection->lastInsertId();
00210 }
00211
00221 public function query($sql, $bind = array())
00222 {
00223 if (empty($bind) && $sql instanceof Zend_Db_Select) {
00224 $bind = $sql->getBind();
00225 }
00226
00227 if (is_array($bind)) {
00228 foreach ($bind as $name => $value) {
00229 if (!is_int($name) && !preg_match('/^:/', $name)) {
00230 $newName = ":$name";
00231 unset($bind[$name]);
00232 $bind[$newName] = $value;
00233 }
00234 }
00235 }
00236
00237 try {
00238 return parent::query($sql, $bind);
00239 } catch (PDOException $e) {
00243 require_once 'Zend/Db/Statement/Exception.php';
00244 throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
00245 }
00246 }
00247
00256 public function exec($sql)
00257 {
00258 if ($sql instanceof Zend_Db_Select) {
00259 $sql = $sql->assemble();
00260 }
00261
00262 try {
00263 $affected = $this->getConnection()->exec($sql);
00264
00265 if ($affected === false) {
00266 $errorInfo = $this->getConnection()->errorInfo();
00270 require_once 'Zend/Db/Adapter/Exception.php';
00271 throw new Zend_Db_Adapter_Exception($errorInfo[2]);
00272 }
00273
00274 return $affected;
00275 } catch (PDOException $e) {
00279 require_once 'Zend/Db/Adapter/Exception.php';
00280 throw new Zend_Db_Adapter_Exception($e->getMessage(), $e);
00281 }
00282 }
00283
00290 protected function _quote($value)
00291 {
00292 if (is_int($value) || is_float($value)) {
00293 return $value;
00294 }
00295 $this->_connect();
00296 return $this->_connection->quote($value);
00297 }
00298
00302 protected function _beginTransaction()
00303 {
00304 $this->_connect();
00305 $this->_connection->beginTransaction();
00306 }
00307
00311 protected function _commit()
00312 {
00313 $this->_connect();
00314 $this->_connection->commit();
00315 }
00316
00320 protected function _rollBack() {
00321 $this->_connect();
00322 $this->_connection->rollBack();
00323 }
00324
00334 public function setFetchMode($mode)
00335 {
00336
00337 if (!extension_loaded('pdo')) {
00341 require_once 'Zend/Db/Adapter/Exception.php';
00342 throw new Zend_Db_Adapter_Exception('The PDO extension is required for this adapter but the extension is not loaded');
00343 }
00344 switch ($mode) {
00345 case PDO::FETCH_LAZY:
00346 case PDO::FETCH_ASSOC:
00347 case PDO::FETCH_NUM:
00348 case PDO::FETCH_BOTH:
00349 case PDO::FETCH_NAMED:
00350 case PDO::FETCH_OBJ:
00351 $this->_fetchMode = $mode;
00352 break;
00353 default:
00357 require_once 'Zend/Db/Adapter/Exception.php';
00358 throw new Zend_Db_Adapter_Exception("Invalid fetch mode '$mode' specified");
00359 break;
00360 }
00361 }
00362
00369 public function supportsParameters($type)
00370 {
00371 switch ($type) {
00372 case 'positional':
00373 case 'named':
00374 default:
00375 return true;
00376 }
00377 }
00378
00384 public function getServerVersion()
00385 {
00386 $this->_connect();
00387 try {
00388 $version = $this->_connection->getAttribute(PDO::ATTR_SERVER_VERSION);
00389 } catch (PDOException $e) {
00390
00391 return null;
00392 }
00393 $matches = null;
00394 if (preg_match('/((?:[0-9]{1,2}\.){1,3}[0-9]{1,2})/', $version, $matches)) {
00395 return $matches[1];
00396 } else {
00397 return null;
00398 }
00399 }
00400 }
00401