Driver.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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;
  16. use InvalidArgumentException;
  17. use PDO;
  18. /**
  19. * Represents a database driver containing all specificities for
  20. * a database engine including its SQL dialect
  21. */
  22. abstract class Driver
  23. {
  24. /**
  25. * Configuration data.
  26. *
  27. * @var array
  28. */
  29. protected $_config;
  30. /**
  31. * Base configuration that is merged into the user
  32. * supplied configuration data.
  33. *
  34. * @var array
  35. */
  36. protected $_baseConfig = [];
  37. /**
  38. * Indicates whether or not the driver is doing automatic identifier quoting
  39. * for all queries
  40. *
  41. * @var bool
  42. */
  43. protected $_autoQuoting = false;
  44. /**
  45. * Constructor
  46. *
  47. * @param array $config The configuration for the driver.
  48. * @throws \InvalidArgumentException
  49. */
  50. public function __construct($config = [])
  51. {
  52. if (empty($config['username']) && !empty($config['login'])) {
  53. throw new InvalidArgumentException(
  54. 'Please pass "username" instead of "login" for connecting to the database'
  55. );
  56. }
  57. $config += $this->_baseConfig;
  58. $this->_config = $config;
  59. if (!empty($config['quoteIdentifiers'])) {
  60. $this->enableAutoQuoting();
  61. }
  62. }
  63. /**
  64. * Establishes a connection to the database server
  65. *
  66. * @return bool true on success
  67. */
  68. abstract public function connect();
  69. /**
  70. * Disconnects from database server
  71. *
  72. * @return void
  73. */
  74. abstract public function disconnect();
  75. /**
  76. * Returns correct connection resource or object that is internally used
  77. * If first argument is passed,
  78. *
  79. * @param null|\PDO $connection The connection object
  80. * @return \Cake\Database\Connection
  81. */
  82. abstract public function connection($connection = null);
  83. /**
  84. * Returns whether php is able to use this driver for connecting to database
  85. *
  86. * @return bool true if it is valid to use this driver
  87. */
  88. abstract public function enabled();
  89. /**
  90. * Prepares a sql statement to be executed
  91. *
  92. * @param string|\Cake\Database\Query $query The query to convert into a statement.
  93. * @return \Cake\Database\StatementInterface
  94. */
  95. abstract public function prepare($query);
  96. /**
  97. * Starts a transaction
  98. *
  99. * @return bool true on success, false otherwise
  100. */
  101. abstract public function beginTransaction();
  102. /**
  103. * Commits a transaction
  104. *
  105. * @return bool true on success, false otherwise
  106. */
  107. abstract public function commitTransaction();
  108. /**
  109. * Rollsback a transaction
  110. *
  111. * @return bool true on success, false otherwise
  112. */
  113. abstract public function rollbackTransaction();
  114. /**
  115. * Get the SQL for releasing a save point.
  116. *
  117. * @param string $name The table name
  118. * @return string
  119. */
  120. abstract public function releaseSavePointSQL($name);
  121. /**
  122. * Get the SQL for creating a save point.
  123. *
  124. * @param string $name The table name
  125. * @return string
  126. */
  127. abstract public function savePointSQL($name);
  128. /**
  129. * Get the SQL for rollingback a save point.
  130. *
  131. * @param string $name The table name
  132. * @return string
  133. */
  134. abstract public function rollbackSavePointSQL($name);
  135. /**
  136. * Get the SQL for disabling foreign keys
  137. *
  138. * @return string
  139. */
  140. abstract public function disableForeignKeySQL();
  141. /**
  142. * Get the SQL for enabling foreign keys
  143. *
  144. * @return string
  145. */
  146. abstract public function enableForeignKeySQL();
  147. /**
  148. * Returns whether the driver supports adding or dropping constraints
  149. * to already created tables.
  150. *
  151. * @return bool true if driver supports dynamic constraints
  152. */
  153. abstract public function supportsDynamicConstraints();
  154. /**
  155. * Returns whether this driver supports save points for nested transactions
  156. *
  157. * @return bool true if save points are supported, false otherwise
  158. */
  159. public function supportsSavePoints()
  160. {
  161. return true;
  162. }
  163. /**
  164. * Returns a value in a safe representation to be used in a query string
  165. *
  166. * @param mixed $value The value to quote.
  167. * @param string $type Type to be used for determining kind of quoting to perform
  168. * @return string
  169. */
  170. abstract public function quote($value, $type);
  171. /**
  172. * Checks if the driver supports quoting
  173. *
  174. * @return bool
  175. */
  176. public function supportsQuoting()
  177. {
  178. return true;
  179. }
  180. /**
  181. * Returns a callable function that will be used to transform a passed Query object.
  182. * This function, in turn, will return an instance of a Query object that has been
  183. * transformed to accommodate any specificities of the SQL dialect in use.
  184. *
  185. * @param string $type the type of query to be transformed
  186. * (select, insert, update, delete)
  187. * @return callable
  188. */
  189. abstract public function queryTranslator($type);
  190. /**
  191. * Get the schema dialect.
  192. *
  193. * Used by Cake\Database\Schema package to reflect schema and
  194. * generate schema.
  195. *
  196. * If all the tables that use this Driver specify their
  197. * own schemas, then this may return null.
  198. *
  199. * @return \Cake\Database\Schema\BaseSchema
  200. */
  201. abstract public function schemaDialect();
  202. /**
  203. * Quotes a database identifier (a column name, table name, etc..) to
  204. * be used safely in queries without the risk of using reserved words
  205. *
  206. * @param string $identifier The identifier expression to quote.
  207. * @return string
  208. */
  209. abstract public function quoteIdentifier($identifier);
  210. /**
  211. * Escapes values for use in schema definitions.
  212. *
  213. * @param mixed $value The value to escape.
  214. * @return string String for use in schema definitions.
  215. */
  216. public function schemaValue($value)
  217. {
  218. if ($value === null) {
  219. return 'NULL';
  220. }
  221. if ($value === false) {
  222. return 'FALSE';
  223. }
  224. if ($value === true) {
  225. return 'TRUE';
  226. }
  227. if (is_float($value)) {
  228. return str_replace(',', '.', (string)$value);
  229. }
  230. if ((is_int($value) || $value === '0') || (
  231. is_numeric($value) && strpos($value, ',') === false &&
  232. $value[0] !== '0' && strpos($value, 'e') === false)
  233. ) {
  234. return (string)$value;
  235. }
  236. return $this->_connection->quote($value, PDO::PARAM_STR);
  237. }
  238. /**
  239. * Returns the schema name that's being used
  240. *
  241. * @return string
  242. */
  243. public function schema()
  244. {
  245. return $this->_config['schema'];
  246. }
  247. /**
  248. * Returns last id generated for a table or sequence in database
  249. *
  250. * @param string|null $table table name or sequence to get last insert value from
  251. * @param string|null $column the name of the column representing the primary key
  252. * @return string|int
  253. */
  254. public function lastInsertId($table = null, $column = null)
  255. {
  256. return $this->_connection->lastInsertId($table, $column);
  257. }
  258. /**
  259. * Check whether or not the driver is connected.
  260. *
  261. * @return bool
  262. */
  263. public function isConnected()
  264. {
  265. return $this->_connection !== null;
  266. }
  267. /**
  268. * Sets whether or not this driver should automatically quote identifiers
  269. * in queries.
  270. *
  271. * @param bool $enable Whether to enable auto quoting
  272. * @return $this
  273. */
  274. public function enableAutoQuoting($enable = true)
  275. {
  276. $this->_autoQuoting = (bool)$enable;
  277. return $this;
  278. }
  279. /**
  280. * Returns whether or not this driver should automatically quote identifiers
  281. * in queries
  282. *
  283. * @return bool
  284. */
  285. public function isAutoQuotingEnabled()
  286. {
  287. return $this->_autoQuoting;
  288. }
  289. /**
  290. * Returns whether or not this driver should automatically quote identifiers
  291. * in queries
  292. *
  293. * If called with a boolean argument, it will toggle the auto quoting setting
  294. * to the passed value
  295. *
  296. * @deprecated 3.4.0 use enableAutoQuoting()/isAutoQuotingEnabled() instead.
  297. * @param bool|null $enable Whether to enable auto quoting
  298. * @return bool
  299. */
  300. public function autoQuoting($enable = null)
  301. {
  302. if ($enable !== null) {
  303. $this->enableAutoQuoting($enable);
  304. }
  305. return $this->isAutoQuotingEnabled();
  306. }
  307. /**
  308. * Transforms the passed query to this Driver's dialect and returns an instance
  309. * of the transformed query and the full compiled SQL string
  310. *
  311. * @param \Cake\Database\Query $query The query to compile.
  312. * @param \Cake\Database\ValueBinder $generator The value binder to use.
  313. * @return array containing 2 entries. The first entity is the transformed query
  314. * and the second one the compiled SQL
  315. */
  316. public function compileQuery(Query $query, ValueBinder $generator)
  317. {
  318. $processor = $this->newCompiler();
  319. $translator = $this->queryTranslator($query->type());
  320. $query = $translator($query);
  321. return [$query, $processor->compile($query, $generator)];
  322. }
  323. /**
  324. * Returns an instance of a QueryCompiler
  325. *
  326. * @return \Cake\Database\QueryCompiler
  327. */
  328. public function newCompiler()
  329. {
  330. return new QueryCompiler();
  331. }
  332. /**
  333. * Destructor
  334. */
  335. public function __destruct()
  336. {
  337. $this->_connection = null;
  338. }
  339. /**
  340. * Returns an array that can be used to describe the internal state of this
  341. * object.
  342. *
  343. * @return array
  344. */
  345. public function __debugInfo()
  346. {
  347. return [
  348. 'connected' => $this->_connection !== null
  349. ];
  350. }
  351. }