PDO.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <?php defined('SYSPATH') OR die('No direct script access.');
  2. /**
  3. * PDO database connection.
  4. *
  5. * @package Kohana/Database
  6. * @category Drivers
  7. * @author Kohana Team
  8. * @copyright (c) 2008-2009 Kohana Team
  9. * @license http://kohanaphp.com/license
  10. */
  11. class Kohana_Database_PDO extends Database {
  12. // PDO uses no quoting for identifiers
  13. protected $_identifier = '';
  14. public function __construct($name, array $config)
  15. {
  16. parent::__construct($name, $config);
  17. if (isset($this->_config['identifier']))
  18. {
  19. // Allow the identifier to be overloaded per-connection
  20. $this->_identifier = (string) $this->_config['identifier'];
  21. }
  22. }
  23. public function connect()
  24. {
  25. if ($this->_connection)
  26. return;
  27. // Extract the connection parameters, adding required variabels
  28. extract($this->_config['connection'] + array(
  29. 'dsn' => '',
  30. 'username' => NULL,
  31. 'password' => NULL,
  32. 'persistent' => FALSE,
  33. ));
  34. // Clear the connection parameters for security
  35. unset($this->_config['connection']);
  36. // Force PDO to use exceptions for all errors
  37. $options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
  38. if ( ! empty($persistent))
  39. {
  40. // Make the connection persistent
  41. $options[PDO::ATTR_PERSISTENT] = TRUE;
  42. }
  43. try
  44. {
  45. // Create a new PDO connection
  46. $this->_connection = new PDO($dsn, $username, $password, $options);
  47. }
  48. catch (PDOException $e)
  49. {
  50. throw new Database_Exception(':error',
  51. array(':error' => $e->getMessage()),
  52. $e->getCode());
  53. }
  54. }
  55. /**
  56. * Create or redefine a SQL aggregate function.
  57. *
  58. * [!!] Works only with SQLite
  59. *
  60. * @link http://php.net/manual/function.pdo-sqlitecreateaggregate
  61. *
  62. * @param string $name Name of the SQL function to be created or redefined
  63. * @param callback $step Called for each row of a result set
  64. * @param callback $final Called after all rows of a result set have been processed
  65. * @param integer $arguments Number of arguments that the SQL function takes
  66. *
  67. * @return boolean
  68. */
  69. public function create_aggregate($name, $step, $final, $arguments = -1)
  70. {
  71. $this->_connection or $this->connect();
  72. return $this->_connection->sqliteCreateAggregate(
  73. $name, $step, $final, $arguments
  74. );
  75. }
  76. /**
  77. * Create or redefine a SQL function.
  78. *
  79. * [!!] Works only with SQLite
  80. *
  81. * @link http://php.net/manual/function.pdo-sqlitecreatefunction
  82. *
  83. * @param string $name Name of the SQL function to be created or redefined
  84. * @param callback $callback Callback which implements the SQL function
  85. * @param integer $arguments Number of arguments that the SQL function takes
  86. *
  87. * @return boolean
  88. */
  89. public function create_function($name, $callback, $arguments = -1)
  90. {
  91. $this->_connection or $this->connect();
  92. return $this->_connection->sqliteCreateFunction(
  93. $name, $callback, $arguments
  94. );
  95. }
  96. public function disconnect()
  97. {
  98. // Destroy the PDO object
  99. $this->_connection = NULL;
  100. return parent::disconnect();
  101. }
  102. public function set_charset($charset)
  103. {
  104. // Make sure the database is connected
  105. $this->_connection OR $this->connect();
  106. // This SQL-92 syntax is not supported by all drivers
  107. $this->_connection->exec('SET NAMES '.$this->quote($charset));
  108. }
  109. public function query($type, $sql, $as_object = FALSE, array $params = NULL)
  110. {
  111. // Make sure the database is connected
  112. $this->_connection or $this->connect();
  113. if (Kohana::$profiling)
  114. {
  115. // Benchmark this query for the current instance
  116. $benchmark = Profiler::start("Database ({$this->_instance})", $sql);
  117. }
  118. try
  119. {
  120. $result = $this->_connection->query($sql);
  121. }
  122. catch (Exception $e)
  123. {
  124. if (isset($benchmark))
  125. {
  126. // This benchmark is worthless
  127. Profiler::delete($benchmark);
  128. }
  129. // Convert the exception in a database exception
  130. throw new Database_Exception(':error [ :query ]',
  131. array(
  132. ':error' => $e->getMessage(),
  133. ':query' => $sql
  134. ),
  135. $e->getCode());
  136. }
  137. if (isset($benchmark))
  138. {
  139. Profiler::stop($benchmark);
  140. }
  141. // Set the last query
  142. $this->last_query = $sql;
  143. if ($type === Database::SELECT)
  144. {
  145. // Convert the result into an array, as PDOStatement::rowCount is not reliable
  146. if ($as_object === FALSE)
  147. {
  148. $result->setFetchMode(PDO::FETCH_ASSOC);
  149. }
  150. elseif (is_string($as_object))
  151. {
  152. $result->setFetchMode(PDO::FETCH_CLASS, $as_object, $params);
  153. }
  154. else
  155. {
  156. $result->setFetchMode(PDO::FETCH_CLASS, 'stdClass');
  157. }
  158. $result = $result->fetchAll();
  159. // Return an iterator of results
  160. return new Database_Result_Cached($result, $sql, $as_object, $params);
  161. }
  162. elseif ($type === Database::INSERT)
  163. {
  164. // Return a list of insert id and rows created
  165. return array(
  166. $this->_connection->lastInsertId(),
  167. $result->rowCount(),
  168. );
  169. }
  170. else
  171. {
  172. // Return the number of rows affected
  173. return $result->rowCount();
  174. }
  175. }
  176. public function begin($mode = NULL)
  177. {
  178. // Make sure the database is connected
  179. $this->_connection or $this->connect();
  180. return $this->_connection->beginTransaction();
  181. }
  182. public function commit()
  183. {
  184. // Make sure the database is connected
  185. $this->_connection or $this->connect();
  186. return $this->_connection->commit();
  187. }
  188. public function rollback()
  189. {
  190. // Make sure the database is connected
  191. $this->_connection or $this->connect();
  192. return $this->_connection->rollBack();
  193. }
  194. public function list_tables($like = NULL)
  195. {
  196. throw new Kohana_Exception('Database method :method is not supported by :class',
  197. array(':method' => __FUNCTION__, ':class' => __CLASS__));
  198. }
  199. public function list_columns($table, $like = NULL, $add_prefix = TRUE)
  200. {
  201. throw new Kohana_Exception('Database method :method is not supported by :class',
  202. array(':method' => __FUNCTION__, ':class' => __CLASS__));
  203. }
  204. public function escape($value)
  205. {
  206. // Make sure the database is connected
  207. $this->_connection or $this->connect();
  208. return $this->_connection->quote($value);
  209. }
  210. } // End Database_PDO