PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/station-games/vendor/cakephp/cakephp/src/Database/Driver.php

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