PDODriverTrait.php 5.1 KB

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