Driver.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 MIT License (http://www.opensource.org/licenses/mit-license.php)
  14. */
  15. namespace Cake\Database;
  16. use Cake\Database\Query;
  17. use Cake\Database\QueryCompiler;
  18. use Cake\Database\ValueBinder;
  19. /**
  20. * Represents a database diver containing all specificities for
  21. * a database engine including its SQL dialect
  22. *
  23. */
  24. abstract class Driver {
  25. /**
  26. * Configuration data.
  27. *
  28. * @var array
  29. */
  30. protected $_config;
  31. /**
  32. * Base configuration that is merged into the user
  33. * supplied configuration data.
  34. *
  35. * @var array
  36. */
  37. protected $_baseConfig = [];
  38. /**
  39. * Indicates whether or not the driver is doing automatic identifier quoting
  40. * for all queries
  41. *
  42. * @var bool
  43. */
  44. protected $_autoQuoting = false;
  45. /**
  46. * Constructor
  47. *
  48. * @param array $config The configuration for the driver.
  49. */
  50. public function __construct($config = []) {
  51. $config += $this->_baseConfig;
  52. $this->_config = $config;
  53. if (!empty($config['quoteIdentifiers'])) {
  54. $this->autoQuoting(true);
  55. }
  56. }
  57. /**
  58. * Establishes a connection to the database server
  59. *
  60. * @return boolean true con success
  61. */
  62. public abstract function connect();
  63. /**
  64. * Disconnects from database server
  65. *
  66. * @return void
  67. */
  68. public abstract function disconnect();
  69. /**
  70. * Returns correct connection resource or object that is internally used
  71. * If first argument is passed,
  72. *
  73. * @param null|\PDO instance $connection
  74. * @return void
  75. */
  76. public abstract function connection($connection = null);
  77. /**
  78. * Returns whether php is able to use this driver for connecting to database
  79. *
  80. * @return boolean true if it is valid to use this driver
  81. */
  82. public abstract function enabled();
  83. /**
  84. * Prepares a sql statement to be executed
  85. *
  86. * @param string|\Cake\Database\Query $query
  87. * @return \Cake\Database\StatementInterface
  88. */
  89. public abstract function prepare($query);
  90. /**
  91. * Starts a transaction
  92. *
  93. * @return boolean true on success, false otherwise
  94. */
  95. public abstract function beginTransaction();
  96. /**
  97. * Commits a transaction
  98. *
  99. * @return boolean true on success, false otherwise
  100. */
  101. public abstract function commitTransaction();
  102. /**
  103. * Rollsback a transaction
  104. *
  105. * @return boolean true on success, false otherwise
  106. */
  107. public abstract function rollbackTransaction();
  108. /**
  109. * Returns whether this driver supports save points for nested transactions
  110. *
  111. * @return boolean true if save points are supported, false otherwise
  112. */
  113. public function supportsSavePoints() {
  114. return true;
  115. }
  116. /**
  117. * Returns a value in a safe representation to be used in a query string
  118. *
  119. * @param mixed $value
  120. * @param string $type Type to be used for determining kind of quoting to perform
  121. * @return string
  122. */
  123. public abstract function quote($value, $type);
  124. /**
  125. * Checks if the driver supports quoting
  126. *
  127. * @return boolean
  128. */
  129. public function supportsQuoting() {
  130. return true;
  131. }
  132. /**
  133. * Returns a callable function that will be used to transform a passed Query object.
  134. * This function, in turn, will return an instance of a Query object that has been
  135. * transformed to accommodate any specificities of the SQL dialect in use.
  136. *
  137. * @param string $type the type of query to be transformed
  138. * (select, insert, update, delete)
  139. * @return callable
  140. */
  141. public abstract function queryTranslator($type);
  142. /**
  143. * Get the schema dialect.
  144. *
  145. * Used by Cake\Database\Schema package to reflect schema and
  146. * generate schema.
  147. *
  148. * If all the tables that use this Driver specify their
  149. * own schemas, then this may return null.
  150. *
  151. * @return \Cake\Database\Schema\BaseSchema
  152. */
  153. public abstract function schemaDialect();
  154. /**
  155. * Quotes a database identifier (a column name, table name, etc..) to
  156. * be used safely in queries without the risk of using reserved words
  157. *
  158. * @param string $identifier
  159. * @return string
  160. */
  161. public abstract function quoteIdentifier($identifier);
  162. /**
  163. * Escapes values for use in schema definitions.
  164. *
  165. * @param mixed $value The value to escape.
  166. * @return string String for use in schema definitions.
  167. */
  168. public function schemaValue($value) {
  169. if ($value === null) {
  170. return 'NULL';
  171. }
  172. if ($value === false) {
  173. return 'FALSE';
  174. }
  175. if ($value === true) {
  176. return 'TRUE';
  177. }
  178. if (is_float($value)) {
  179. return str_replace(',', '.', strval($value));
  180. }
  181. if ((is_int($value) || $value === '0') || (
  182. is_numeric($value) && strpos($value, ',') === false &&
  183. $value[0] != '0' && strpos($value, 'e') === false)
  184. ) {
  185. return $value;
  186. }
  187. return $this->_connection->quote($value, \PDO::PARAM_STR);
  188. }
  189. /**
  190. * Returns last id generated for a table or sequence in database
  191. *
  192. * @param string $table table name or sequence to get last insert value from
  193. * @param string $column the name of the column representing the primary key
  194. * @return string|integer
  195. */
  196. public function lastInsertId($table = null, $column = null) {
  197. return $this->_connection->lastInsertId($table, $column);
  198. }
  199. /**
  200. * Check whether or not the driver is connected.
  201. *
  202. * @return boolean
  203. */
  204. public function isConnected() {
  205. return $this->_connection !== null;
  206. }
  207. /**
  208. * Returns whether or not this driver should automatically quote identifiers
  209. * in queries
  210. *
  211. * If called with a boolean argument, it will toggle the auto quoting setting
  212. * to the passed value
  213. *
  214. * @param boolean $enable whether to enable auto quoting
  215. * @return boolean
  216. */
  217. public function autoQuoting($enable = null) {
  218. if ($enable === null) {
  219. return $this->_autoQuoting;
  220. }
  221. return $this->_autoQuoting = (bool)$enable;
  222. }
  223. /**
  224. * Transforms the passed query to this Driver's dialect and returns an instance
  225. * of the transformed query and the full compiled SQL string
  226. *
  227. * @return array containing 2 entries. The first enty is the transformed query
  228. * and the secod one the compiled SQL
  229. */
  230. public function compileQuery(Query $query, ValueBinder $generator) {
  231. $processor = $this->newCompiler();
  232. $translator = $this->queryTranslator($query->type());
  233. $query = $translator($query);
  234. return [$query, $processor->compile($query, $generator)];
  235. }
  236. /**
  237. * Returns an instance of a QueryCompiler
  238. *
  239. * @return \Cake\Database\QueryCompiler
  240. */
  241. public function newCompiler() {
  242. return new QueryCompiler;
  243. }
  244. /**
  245. * Destructor
  246. */
  247. public function __destruct() {
  248. $this->_connection = null;
  249. }
  250. }