PDODriverTrait.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Database\Driver;
  16. use Cake\Database\Query;
  17. use Cake\Database\Statement\PDOStatement;
  18. use PDO;
  19. /**
  20. * PDO driver trait
  21. */
  22. trait PDODriverTrait
  23. {
  24. /**
  25. * Instance of PDO.
  26. *
  27. * @var \PDO
  28. */
  29. protected $_connection;
  30. /**
  31. * Establishes a connection to the database server
  32. *
  33. * @param string $dsn A Driver-specific PDO-DSN
  34. * @param array $config configuration to be used for creating connection
  35. * @return bool true on success
  36. */
  37. protected function _connect($dsn, array $config)
  38. {
  39. $connection = new PDO(
  40. $dsn,
  41. $config['username'],
  42. $config['password'],
  43. $config['flags']
  44. );
  45. $this->connection($connection);
  46. return true;
  47. }
  48. /**
  49. * Returns correct connection resource or object that is internally used
  50. * If first argument is passed, it will set internal connection object or
  51. * result to the value passed
  52. *
  53. * @param null|\PDO $connection The PDO connection instance.
  54. * @return mixed connection object used internally
  55. */
  56. public function connection($connection = null)
  57. {
  58. if ($connection !== null) {
  59. $this->_connection = $connection;
  60. }
  61. return $this->_connection;
  62. }
  63. /**
  64. * Disconnects from database server
  65. *
  66. * @return void
  67. */
  68. public function disconnect()
  69. {
  70. $this->_connection = null;
  71. }
  72. /**
  73. * Check whether or not the driver is connected.
  74. *
  75. * @return bool
  76. */
  77. public function isConnected()
  78. {
  79. if ($this->_connection === null) {
  80. $connected = false;
  81. } else {
  82. try {
  83. $connected = $this->_connection->query('SELECT 1');
  84. } catch (\PDOException $e) {
  85. $connected = false;
  86. }
  87. }
  88. $this->connected = !empty($connected);
  89. return $this->connected;
  90. }
  91. /**
  92. * Prepares a sql statement to be executed
  93. *
  94. * @param string|\Cake\Database\Query $query The query to turn into a prepared statement.
  95. * @return \Cake\Database\StatementInterface
  96. */
  97. public function prepare($query)
  98. {
  99. $this->connect();
  100. $isObject = $query instanceof Query;
  101. $statement = $this->_connection->prepare($isObject ? $query->sql() : $query);
  102. return new PDOStatement($statement, $this);
  103. }
  104. /**
  105. * Starts a transaction
  106. *
  107. * @return bool true on success, false otherwise
  108. */
  109. public function beginTransaction()
  110. {
  111. $this->connect();
  112. if ($this->_connection->inTransaction()) {
  113. return true;
  114. }
  115. return $this->_connection->beginTransaction();
  116. }
  117. /**
  118. * Commits a transaction
  119. *
  120. * @return bool true on success, false otherwise
  121. */
  122. public function commitTransaction()
  123. {
  124. $this->connect();
  125. if (!$this->_connection->inTransaction()) {
  126. return false;
  127. }
  128. return $this->_connection->commit();
  129. }
  130. /**
  131. * Rollsback a transaction
  132. *
  133. * @return bool true on success, false otherwise
  134. */
  135. public function rollbackTransaction()
  136. {
  137. if (!$this->_connection->inTransaction()) {
  138. return false;
  139. }
  140. return $this->_connection->rollback();
  141. }
  142. /**
  143. * Returns a value in a safe representation to be used in a query string
  144. *
  145. * @param mixed $value The value to quote.
  146. * @param string $type Type to be used for determining kind of quoting to perform
  147. * @return string
  148. */
  149. public function quote($value, $type)
  150. {
  151. $this->connect();
  152. return $this->_connection->quote($value, $type);
  153. }
  154. /**
  155. * Returns last id generated for a table or sequence in database
  156. *
  157. * @param string|null $table table name or sequence to get last insert value from
  158. * @param string|null $column the name of the column representing the primary key
  159. * @return string|int
  160. */
  161. public function lastInsertId($table = null, $column = null)
  162. {
  163. $this->connect();
  164. return $this->_connection->lastInsertId($table);
  165. }
  166. /**
  167. * Checks if the driver supports quoting, as PDO_ODBC does not support it.
  168. *
  169. * @return bool
  170. */
  171. public function supportsQuoting()
  172. {
  173. $this->connect();
  174. return $this->_connection->getAttribute(PDO::ATTR_DRIVER_NAME) !== 'odbc';
  175. }
  176. }