Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
All Data Structures Namespaces Files Functions Variables Pages
Abstract.php
Go to the documentation of this file.
1 <?php
27 #require_once 'Zend/Db/Adapter/Abstract.php';
28 
29 
33 #require_once 'Zend/Db/Statement/Pdo.php';
34 
35 
46 {
47 
53  protected $_defaultStmtClass = 'Zend_Db_Statement_Pdo';
54 
60  protected function _dsn()
61  {
62  // baseline of DSN parts
63  $dsn = $this->_config;
64 
65  // don't pass the username, password, charset, persistent and driver_options in the DSN
66  unset($dsn['username']);
67  unset($dsn['password']);
68  unset($dsn['options']);
69  unset($dsn['charset']);
70  unset($dsn['persistent']);
71  unset($dsn['driver_options']);
72 
73  // use all remaining parts in the DSN
74  foreach ($dsn as $key => $val) {
75  $dsn[$key] = "$key=$val";
76  }
77 
78  return $this->_pdoType . ':' . implode(';', $dsn);
79  }
80 
87  protected function _connect()
88  {
89  // if we already have a PDO object, no need to re-connect.
90  if ($this->_connection) {
91  return;
92  }
93 
94  // get the dsn first, because some adapters alter the $_pdoType
95  $dsn = $this->_dsn();
96 
97  // check for PDO extension
98  if (!extension_loaded('pdo')) {
102  #require_once 'Zend/Db/Adapter/Exception.php';
103  throw new Zend_Db_Adapter_Exception('The PDO extension is required for this adapter but the extension is not loaded');
104  }
105 
106  // check the PDO driver is available
107  if (!in_array($this->_pdoType, PDO::getAvailableDrivers())) {
111  #require_once 'Zend/Db/Adapter/Exception.php';
112  throw new Zend_Db_Adapter_Exception('The ' . $this->_pdoType . ' driver is not currently installed');
113  }
114 
115  // create PDO connection
116  $q = $this->_profiler->queryStart('connect', Zend_Db_Profiler::CONNECT);
117 
118  // add the persistence flag if we find it in our config array
119  if (isset($this->_config['persistent']) && ($this->_config['persistent'] == true)) {
120  $this->_config['driver_options'][PDO::ATTR_PERSISTENT] = true;
121  }
122 
123  try {
124  $this->_connection = new PDO(
125  $dsn,
126  $this->_config['username'],
127  $this->_config['password'],
128  $this->_config['driver_options']
129  );
130 
131  $this->_profiler->queryEnd($q);
132 
133  // set the PDO connection to perform case-folding on array keys, or not
134  $this->_connection->setAttribute(PDO::ATTR_CASE, $this->_caseFolding);
135 
136  // always use exceptions.
137  $this->_connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
138 
139  } catch (PDOException $e) {
143  #require_once 'Zend/Db/Adapter/Exception.php';
144  throw new Zend_Db_Adapter_Exception($e->getMessage(), $e->getCode(), $e);
145  }
146 
147  }
148 
154  public function isConnected()
155  {
156  return ((bool) ($this->_connection instanceof PDO));
157  }
158 
164  public function closeConnection()
165  {
166  $this->_connection = null;
167  }
168 
176  public function prepare($sql)
177  {
178  $this->_connect();
179  $stmtClass = $this->_defaultStmtClass;
180  if (!class_exists($stmtClass)) {
181  #require_once 'Zend/Loader.php';
182  Zend_Loader::loadClass($stmtClass);
183  }
184  $stmt = new $stmtClass($this, $sql);
185  $stmt->setFetchMode($this->_fetchMode);
186  return $stmt;
187  }
188 
206  public function lastInsertId($tableName = null, $primaryKey = null)
207  {
208  $this->_connect();
209  return $this->_connection->lastInsertId();
210  }
211 
221  public function query($sql, $bind = array())
222  {
223  if (empty($bind) && $sql instanceof Zend_Db_Select) {
224  $bind = $sql->getBind();
225  }
226 
227  if (is_array($bind)) {
228  foreach ($bind as $name => $value) {
229  if (!is_int($name) && !preg_match('/^:/', $name)) {
230  $newName = ":$name";
231  unset($bind[$name]);
232  $bind[$newName] = $value;
233  }
234  }
235  }
236 
237  try {
238  return parent::query($sql, $bind);
239  } catch (PDOException $e) {
243  #require_once 'Zend/Db/Statement/Exception.php';
244  throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
245  }
246  }
247 
256  public function exec($sql)
257  {
258  if ($sql instanceof Zend_Db_Select) {
259  $sql = $sql->assemble();
260  }
261 
262  try {
263  $affected = $this->getConnection()->exec($sql);
264 
265  if ($affected === false) {
266  $errorInfo = $this->getConnection()->errorInfo();
270  #require_once 'Zend/Db/Adapter/Exception.php';
271  throw new Zend_Db_Adapter_Exception($errorInfo[2]);
272  }
273 
274  return $affected;
275  } catch (PDOException $e) {
279  #require_once 'Zend/Db/Adapter/Exception.php';
280  throw new Zend_Db_Adapter_Exception($e->getMessage(), $e->getCode(), $e);
281  }
282  }
283 
290  protected function _quote($value)
291  {
292  if (is_int($value) || is_float($value)) {
293  return $value;
294  }
295  $this->_connect();
296  return $this->_connection->quote($value);
297  }
298 
302  protected function _beginTransaction()
303  {
304  $this->_connect();
305  $this->_connection->beginTransaction();
306  }
307 
311  protected function _commit()
312  {
313  $this->_connect();
314  $this->_connection->commit();
315  }
316 
320  protected function _rollBack() {
321  $this->_connect();
322  $this->_connection->rollBack();
323  }
324 
334  public function setFetchMode($mode)
335  {
336  //check for PDO extension
337  if (!extension_loaded('pdo')) {
341  #require_once 'Zend/Db/Adapter/Exception.php';
342  throw new Zend_Db_Adapter_Exception('The PDO extension is required for this adapter but the extension is not loaded');
343  }
344  switch ($mode) {
345  case PDO::FETCH_LAZY:
346  case PDO::FETCH_ASSOC:
347  case PDO::FETCH_NUM:
348  case PDO::FETCH_BOTH:
349  case PDO::FETCH_NAMED:
350  case PDO::FETCH_OBJ:
351  $this->_fetchMode = $mode;
352  break;
353  default:
357  #require_once 'Zend/Db/Adapter/Exception.php';
358  throw new Zend_Db_Adapter_Exception("Invalid fetch mode '$mode' specified");
359  break;
360  }
361  }
362 
369  public function supportsParameters($type)
370  {
371  switch ($type) {
372  case 'positional':
373  case 'named':
374  default:
375  return true;
376  }
377  }
378 
384  public function getServerVersion()
385  {
386  $this->_connect();
387  try {
388  $version = $this->_connection->getAttribute(PDO::ATTR_SERVER_VERSION);
389  } catch (PDOException $e) {
390  // In case of the driver doesn't support getting attributes
391  return null;
392  }
393  $matches = null;
394  if (preg_match('/((?:[0-9]{1,2}\.){1,3}[0-9]{1,2})/', $version, $matches)) {
395  return $matches[1];
396  } else {
397  return null;
398  }
399  }
400 }
lastInsertId($tableName=null, $primaryKey=null)
Definition: Abstract.php:206
$tableName
Definition: trigger.php:13
static loadClass($class, $dirs=null)
Definition: Loader.php:52
$type
Definition: item.phtml:13
$value
Definition: gender.phtml:16
if($exist=($block->getProductCollection() && $block->getProductCollection() ->getSize())) $mode
Definition: grid.phtml:15
query($sql, $bind=array())
Definition: Abstract.php:221
if(!isset($_GET['name'])) $name
Definition: log.php:14