Driver.php 5.8 KB

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