PageRenderTime 38ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/Horde/Db/Adapter/Pdo/Abstract.php

https://github.com/maintainable/framework
PHP | 197 lines | 80 code | 24 blank | 93 comment | 4 complexity | 5495061aa2e446be8bfc09dd9dcd9393 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright 2007 Maintainable Software, LLC
  4. * Copyright 2006-2008 The Horde Project (http://www.horde.org/)
  5. *
  6. * @author Mike Naberezny <mike@maintainable.com>
  7. * @author Derek DeVries <derek@maintainable.com>
  8. * @author Chuck Hagenbuch <chuck@horde.org>
  9. * @license http://opensource.org/licenses/bsd-license.php
  10. * @category Horde
  11. * @package Horde_Db
  12. * @subpackage Adapter
  13. */
  14. /**
  15. * @author Mike Naberezny <mike@maintainable.com>
  16. * @author Derek DeVries <derek@maintainable.com>
  17. * @author Chuck Hagenbuch <chuck@horde.org>
  18. * @license http://opensource.org/licenses/bsd-license.php
  19. * @category Horde
  20. * @package Horde_Db
  21. * @subpackage Adapter
  22. */
  23. abstract class Horde_Db_Adapter_Pdo_Abstract extends Horde_Db_Adapter_Abstract
  24. {
  25. /*##########################################################################
  26. # Connection Management
  27. ##########################################################################*/
  28. /**
  29. * Connect to the db
  30. */
  31. public function connect()
  32. {
  33. list($dsn, $user, $pass) = $this->_parseConfig();
  34. $oldErrorReporting = error_reporting(0);
  35. try {
  36. $pdo = new PDO($dsn, $user, $pass);
  37. error_reporting($oldErrorReporting);
  38. } catch (PDOException $e) {
  39. error_reporting($oldErrorReporting);
  40. $msg = "Could not instantiate PDO with DSN \"$dsn\". PDOException: "
  41. . $e->getMessage();
  42. throw new Horde_Db_Exception($msg);
  43. }
  44. $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  45. $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, true);
  46. $this->_connection = $pdo;
  47. $this->_active = true;
  48. }
  49. /**
  50. * Check if the connection is active
  51. *
  52. * @return boolean
  53. */
  54. public function isActive()
  55. {
  56. return isset($this->_connection) && $this->_connection->query('SELECT 1');
  57. }
  58. /*##########################################################################
  59. # Database Statements
  60. ##########################################################################*/
  61. /**
  62. * Returns an array of record hashes with the column names as keys and
  63. * column values as values.
  64. *
  65. * @param string $sql
  66. * @param mixed $arg1 Either an array of bound parameters or a query name.
  67. * @param string $arg2 If $arg1 contains bound parameters, the query name.
  68. */
  69. public function selectAll($sql, $arg1=null, $arg2=null)
  70. {
  71. $result = $this->execute($sql, $arg1, $arg2);
  72. return $result ? $result->fetchAll(PDO::FETCH_BOTH) : array();
  73. }
  74. /**
  75. * Returns a record hash with the column names as keys and column values
  76. * as values.
  77. *
  78. * @param string $sql
  79. * @param mixed $arg1 Either an array of bound parameters or a query name.
  80. * @param string $arg2 If $arg1 contains bound parameters, the query name.
  81. * @return array
  82. */
  83. public function selectOne($sql, $arg1=null, $arg2=null)
  84. {
  85. $result = $this->execute($sql, $arg1, $arg2);
  86. return $result ? $result->fetch(PDO::FETCH_BOTH) : array();
  87. }
  88. /**
  89. * Returns a single value from a record
  90. *
  91. * @param string $sql
  92. * @param mixed $arg1 Either an array of bound parameters or a query name.
  93. * @param string $arg2 If $arg1 contains bound parameters, the query name.
  94. * @return string
  95. */
  96. public function selectValue($sql, $arg1=null, $arg2=null)
  97. {
  98. $result = $this->execute($sql, $arg1, $arg2);
  99. return $result ? $result->fetchColumn(0) : null;
  100. }
  101. /**
  102. * Returns an array of the values of the first column in a select:
  103. * select_values("SELECT id FROM companies LIMIT 3") => [1,2,3]
  104. *
  105. * @param string $sql
  106. * @param mixed $arg1 Either an array of bound parameters or a query name.
  107. * @param string $arg2 If $arg1 contains bound parameters, the query name.
  108. */
  109. public function selectValues($sql, $arg1=null, $arg2=null)
  110. {
  111. $result = $this->execute($sql, $arg1, $arg2);
  112. return $result ? $result->fetchAll(PDO::FETCH_COLUMN, 0) : array();
  113. }
  114. /*##########################################################################
  115. # Quoting
  116. ##########################################################################*/
  117. /**
  118. * Quotes a string, escaping any ' (single quote) and \ (backslash)
  119. * characters..
  120. *
  121. * @param string $string
  122. * @return string
  123. */
  124. public function quoteString($string)
  125. {
  126. return $this->_connection->quote($string);
  127. }
  128. /*##########################################################################
  129. # Protected
  130. ##########################################################################*/
  131. /**
  132. * Parse configuration array into options for PDO constructor.
  133. *
  134. * @throws Horde_Db_Exception
  135. * @return array [dsn, username, password]
  136. */
  137. protected function _parseConfig()
  138. {
  139. // check required config keys are present
  140. $required = array('adapter', 'username');
  141. $diff = array_diff_key(array_flip($required), $this->_config);
  142. if (! empty($diff)) {
  143. $msg = 'Required config missing: ' . implode(', ', array_keys($diff));
  144. throw new Horde_Db_Exception($msg);
  145. }
  146. // try an empty password if it's not set.
  147. if (!isset($this->_config['password'])) {
  148. $this->_config['password'] = '';
  149. }
  150. // collect options to build PDO Data Source Name (DSN) string
  151. $dsnOpts = $this->_config;
  152. unset($dsnOpts['adapter'], $dsnOpts['username'], $dsnOpts['password']);
  153. // rewrite rails config key names to pdo equivalents
  154. $rails2pdo = array('database' => 'dbname', 'socket' => 'unix_socket');
  155. foreach ($rails2pdo as $from => $to) {
  156. if (isset($dsnOpts[$from])) {
  157. $dsnOpts[$to] = $dsnOpts[$from];
  158. unset($dsnOpts[$from]);
  159. }
  160. }
  161. // build DSN string
  162. $dsn = $this->_config['adapter'] . ':';
  163. foreach ($dsnOpts as $k => $v) {
  164. $dsn .= "$k=$v;";
  165. }
  166. $dsn = rtrim($dsn, ';');
  167. // return DSN and user/pass for connection
  168. return array(
  169. $dsn,
  170. $this->_config['username'],
  171. $this->_config['password']);
  172. }
  173. }