PageRenderTime 28ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/station-games/vendor/cakephp/cakephp/src/Database/Schema/SqliteSchema.php

https://gitlab.com/ViniciusP/project-games
PHP | 451 lines | 325 code | 40 blank | 86 comment | 46 complexity | 25e6948cb7448fa55879407310064ec2 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\Schema;
  16. use Cake\Database\Exception;
  17. /**
  18. * Schema management/reflection features for Sqlite
  19. */
  20. class SqliteSchema extends BaseSchema
  21. {
  22. /**
  23. * Array containing the foreign keys constraints names
  24. * Necessary for composite foreign keys to be handled
  25. *
  26. * @var array
  27. */
  28. protected $_constraintsIdMap = [];
  29. /**
  30. * Convert a column definition to the abstract types.
  31. *
  32. * The returned type will be a type that
  33. * Cake\Database\Type can handle.
  34. *
  35. * @param string $column The column type + length
  36. * @throws \Cake\Database\Exception when unable to parse column type
  37. * @return array Array of column information.
  38. */
  39. protected function _convertColumn($column)
  40. {
  41. preg_match('/(unsigned)?\s*([a-z]+)(?:\(([0-9,]+)\))?/i', $column, $matches);
  42. if (empty($matches)) {
  43. throw new Exception(sprintf('Unable to parse column type from "%s"', $column));
  44. }
  45. $unsigned = false;
  46. if (strtolower($matches[1]) === 'unsigned') {
  47. $unsigned = true;
  48. }
  49. $col = strtolower($matches[2]);
  50. $length = null;
  51. if (isset($matches[3])) {
  52. $length = (int)$matches[3];
  53. }
  54. if ($col === 'bigint') {
  55. return ['type' => 'biginteger', 'length' => $length, 'unsigned' => $unsigned];
  56. }
  57. if (strpos($col, 'decimal') !== false) {
  58. return ['type' => 'decimal', 'length' => null, 'unsigned' => $unsigned];
  59. }
  60. if (strpos($col, 'int') !== false) {
  61. return ['type' => 'integer', 'length' => $length, 'unsigned' => $unsigned];
  62. }
  63. if (in_array($col, ['float', 'real', 'double'])) {
  64. return ['type' => 'float', 'length' => null, 'unsigned' => $unsigned];
  65. }
  66. if (strpos($col, 'boolean') !== false) {
  67. return ['type' => 'boolean', 'length' => null];
  68. }
  69. if ($col === 'char' && $length === 36) {
  70. return ['type' => 'uuid', 'length' => null];
  71. }
  72. if ($col === 'char') {
  73. return ['type' => 'string', 'fixed' => true, 'length' => $length];
  74. }
  75. if (strpos($col, 'char') !== false) {
  76. return ['type' => 'string', 'length' => $length];
  77. }
  78. if (in_array($col, ['blob', 'clob'])) {
  79. return ['type' => 'binary', 'length' => null];
  80. }
  81. if (in_array($col, ['date', 'time', 'timestamp', 'datetime'])) {
  82. return ['type' => $col, 'length' => null];
  83. }
  84. return ['type' => 'text', 'length' => null];
  85. }
  86. /**
  87. * {@inheritDoc}
  88. */
  89. public function listTablesSql($config)
  90. {
  91. return [
  92. 'SELECT name FROM sqlite_master WHERE type="table" ' .
  93. 'AND name != "sqlite_sequence" ORDER BY name',
  94. []
  95. ];
  96. }
  97. /**
  98. * {@inheritDoc}
  99. */
  100. public function describeColumnSql($tableName, $config)
  101. {
  102. $sql = sprintf(
  103. 'PRAGMA table_info(%s)',
  104. $this->_driver->quoteIdentifier($tableName)
  105. );
  106. return [$sql, []];
  107. }
  108. /**
  109. * {@inheritDoc}
  110. */
  111. public function convertColumnDescription(Table $table, $row)
  112. {
  113. $field = $this->_convertColumn($row['type']);
  114. $field += [
  115. 'null' => !$row['notnull'],
  116. 'default' => $row['dflt_value'] === null ? null : trim($row['dflt_value'], "'"),
  117. ];
  118. $primary = $table->constraint('primary');
  119. if ($row['pk'] && empty($primary)) {
  120. $field['null'] = false;
  121. $field['autoIncrement'] = true;
  122. }
  123. // SQLite does not support autoincrement on composite keys.
  124. if ($row['pk'] && !empty($primary)) {
  125. $existingColumn = $primary['columns'][0];
  126. $table->addColumn($existingColumn, ['autoIncrement' => null] + $table->column($existingColumn));
  127. }
  128. $table->addColumn($row['name'], $field);
  129. if ($row['pk']) {
  130. $constraint = (array)$table->constraint('primary') + [
  131. 'type' => Table::CONSTRAINT_PRIMARY,
  132. 'columns' => []
  133. ];
  134. $constraint['columns'] = array_merge($constraint['columns'], [$row['name']]);
  135. $table->addConstraint('primary', $constraint);
  136. }
  137. }
  138. /**
  139. * {@inheritDoc}
  140. */
  141. public function describeIndexSql($tableName, $config)
  142. {
  143. $sql = sprintf(
  144. 'PRAGMA index_list(%s)',
  145. $this->_driver->quoteIdentifier($tableName)
  146. );
  147. return [$sql, []];
  148. }
  149. /**
  150. * {@inheritDoc}
  151. *
  152. * Since SQLite does not have a way to get metadata about all indexes at once,
  153. * additional queries are done here. Sqlite constraint names are not
  154. * stable, and the names for constraints will not match those used to create
  155. * the table. This is a limitation in Sqlite's metadata features.
  156. *
  157. */
  158. public function convertIndexDescription(Table $table, $row)
  159. {
  160. $sql = sprintf(
  161. 'PRAGMA index_info(%s)',
  162. $this->_driver->quoteIdentifier($row['name'])
  163. );
  164. $statement = $this->_driver->prepare($sql);
  165. $statement->execute();
  166. $columns = [];
  167. foreach ($statement->fetchAll('assoc') as $column) {
  168. $columns[] = $column['name'];
  169. }
  170. $statement->closeCursor();
  171. if ($row['unique']) {
  172. $table->addConstraint($row['name'], [
  173. 'type' => Table::CONSTRAINT_UNIQUE,
  174. 'columns' => $columns
  175. ]);
  176. } else {
  177. $table->addIndex($row['name'], [
  178. 'type' => Table::INDEX_INDEX,
  179. 'columns' => $columns
  180. ]);
  181. }
  182. }
  183. /**
  184. * {@inheritDoc}
  185. */
  186. public function describeForeignKeySql($tableName, $config)
  187. {
  188. $sql = sprintf('PRAGMA foreign_key_list(%s)', $this->_driver->quoteIdentifier($tableName));
  189. return [$sql, []];
  190. }
  191. /**
  192. * {@inheritDoc}
  193. */
  194. public function convertForeignKeyDescription(Table $table, $row)
  195. {
  196. $name = $row['from'] . '_fk';
  197. $update = isset($row['on_update']) ? $row['on_update'] : '';
  198. $delete = isset($row['on_delete']) ? $row['on_delete'] : '';
  199. $data = [
  200. 'type' => Table::CONSTRAINT_FOREIGN,
  201. 'columns' => [$row['from']],
  202. 'references' => [$row['table'], $row['to']],
  203. 'update' => $this->_convertOnClause($update),
  204. 'delete' => $this->_convertOnClause($delete),
  205. ];
  206. if (isset($this->_constraintsIdMap[$table->name()][$row['id']])) {
  207. $name = $this->_constraintsIdMap[$table->name()][$row['id']];
  208. } else {
  209. $this->_constraintsIdMap[$table->name()][$row['id']] = $name;
  210. }
  211. $table->addConstraint($name, $data);
  212. }
  213. /**
  214. * {@inheritDoc}
  215. *
  216. * @throws \Cake\Database\Exception when the column type is unknown
  217. */
  218. public function columnSql(Table $table, $name)
  219. {
  220. $data = $table->column($name);
  221. $typeMap = [
  222. 'uuid' => ' CHAR(36)',
  223. 'biginteger' => ' BIGINT',
  224. 'boolean' => ' BOOLEAN',
  225. 'binary' => ' BLOB',
  226. 'float' => ' FLOAT',
  227. 'decimal' => ' DECIMAL',
  228. 'date' => ' DATE',
  229. 'time' => ' TIME',
  230. 'datetime' => ' DATETIME',
  231. 'timestamp' => ' TIMESTAMP',
  232. ];
  233. $out = $this->_driver->quoteIdentifier($name);
  234. $hasUnsigned = ['biginteger', 'integer', 'float', 'decimal'];
  235. if (in_array($data['type'], $hasUnsigned, true) &&
  236. isset($data['unsigned']) && $data['unsigned'] === true
  237. ) {
  238. if ($data['type'] !== 'integer' || [$name] !== (array)$table->primaryKey()) {
  239. $out .= ' UNSIGNED';
  240. }
  241. }
  242. if (isset($typeMap[$data['type']])) {
  243. $out .= $typeMap[$data['type']];
  244. }
  245. if ($data['type'] === 'text' && $data['length'] !== Table::LENGTH_TINY) {
  246. $out .= ' TEXT';
  247. }
  248. if ($data['type'] === 'string' || ($data['type'] === 'text' && $data['length'] === Table::LENGTH_TINY)) {
  249. $out .= ' VARCHAR';
  250. if (isset($data['length'])) {
  251. $out .= '(' . (int)$data['length'] . ')';
  252. }
  253. }
  254. if ($data['type'] === 'integer') {
  255. $out .= ' INTEGER';
  256. if (isset($data['length']) && [$name] !== (array)$table->primaryKey()) {
  257. $out .= '(' . (int)$data['length'] . ')';
  258. }
  259. }
  260. $hasPrecision = ['float', 'decimal'];
  261. if (in_array($data['type'], $hasPrecision, true) &&
  262. (isset($data['length']) || isset($data['precision']))
  263. ) {
  264. $out .= '(' . (int)$data['length'] . ',' . (int)$data['precision'] . ')';
  265. }
  266. if (isset($data['null']) && $data['null'] === false) {
  267. $out .= ' NOT NULL';
  268. }
  269. if ($data['type'] === 'integer' && [$name] === (array)$table->primaryKey()) {
  270. $out .= ' PRIMARY KEY AUTOINCREMENT';
  271. }
  272. if (isset($data['null']) && $data['null'] === true) {
  273. $out .= ' DEFAULT NULL';
  274. unset($data['default']);
  275. }
  276. if (isset($data['default'])) {
  277. $out .= ' DEFAULT ' . $this->_driver->schemaValue($data['default']);
  278. }
  279. return $out;
  280. }
  281. /**
  282. * {@inheritDoc}
  283. *
  284. * Note integer primary keys will return ''. This is intentional as Sqlite requires
  285. * that integer primary keys be defined in the column definition.
  286. *
  287. */
  288. public function constraintSql(Table $table, $name)
  289. {
  290. $data = $table->constraint($name);
  291. if ($data['type'] === Table::CONSTRAINT_PRIMARY &&
  292. count($data['columns']) === 1 &&
  293. $table->column($data['columns'][0])['type'] === 'integer'
  294. ) {
  295. return '';
  296. }
  297. $clause = '';
  298. if ($data['type'] === Table::CONSTRAINT_PRIMARY) {
  299. $type = 'PRIMARY KEY';
  300. }
  301. if ($data['type'] === Table::CONSTRAINT_UNIQUE) {
  302. $type = 'UNIQUE';
  303. }
  304. if ($data['type'] === Table::CONSTRAINT_FOREIGN) {
  305. $type = 'FOREIGN KEY';
  306. $clause = sprintf(
  307. ' REFERENCES %s (%s) ON UPDATE %s ON DELETE %s',
  308. $this->_driver->quoteIdentifier($data['references'][0]),
  309. $this->_convertConstraintColumns($data['references'][1]),
  310. $this->_foreignOnClause($data['update']),
  311. $this->_foreignOnClause($data['delete'])
  312. );
  313. }
  314. $columns = array_map(
  315. [$this->_driver, 'quoteIdentifier'],
  316. $data['columns']
  317. );
  318. return sprintf(
  319. 'CONSTRAINT %s %s (%s)%s',
  320. $this->_driver->quoteIdentifier($name),
  321. $type,
  322. implode(', ', $columns),
  323. $clause
  324. );
  325. }
  326. /**
  327. * {@inheritDoc}
  328. *
  329. * SQLite can not properly handle adding a constraint to an existing table.
  330. * This method is no-op
  331. */
  332. public function addConstraintSql(Table $table)
  333. {
  334. return [];
  335. }
  336. /**
  337. * {@inheritDoc}
  338. *
  339. * SQLite can not properly handle dropping a constraint to an existing table.
  340. * This method is no-op
  341. */
  342. public function dropConstraintSql(Table $table)
  343. {
  344. return [];
  345. }
  346. /**
  347. * {@inheritDoc}
  348. */
  349. public function indexSql(Table $table, $name)
  350. {
  351. $data = $table->index($name);
  352. $columns = array_map(
  353. [$this->_driver, 'quoteIdentifier'],
  354. $data['columns']
  355. );
  356. return sprintf(
  357. 'CREATE INDEX %s ON %s (%s)',
  358. $this->_driver->quoteIdentifier($name),
  359. $this->_driver->quoteIdentifier($table->name()),
  360. implode(', ', $columns)
  361. );
  362. }
  363. /**
  364. * {@inheritDoc}
  365. */
  366. public function createTableSql(Table $table, $columns, $constraints, $indexes)
  367. {
  368. $lines = array_merge($columns, $constraints);
  369. $content = implode(",\n", array_filter($lines));
  370. $temporary = $table->temporary() ? ' TEMPORARY ' : ' ';
  371. $table = sprintf("CREATE%sTABLE \"%s\" (\n%s\n)", $temporary, $table->name(), $content);
  372. $out = [$table];
  373. foreach ($indexes as $index) {
  374. $out[] = $index;
  375. }
  376. return $out;
  377. }
  378. /**
  379. * {@inheritDoc}
  380. */
  381. public function truncateTableSql(Table $table)
  382. {
  383. $name = $table->name();
  384. $sql = [];
  385. if ($this->hasSequences()) {
  386. $sql[] = sprintf('DELETE FROM sqlite_sequence WHERE name="%s"', $name);
  387. }
  388. $sql[] = sprintf('DELETE FROM "%s"', $name);
  389. return $sql;
  390. }
  391. /**
  392. * Returns whether there is any table in this connection to SQLite containing
  393. * sequences
  394. *
  395. * @return bool
  396. */
  397. public function hasSequences()
  398. {
  399. $result = $this->_driver
  400. ->prepare('SELECT 1 FROM sqlite_master WHERE name = "sqlite_sequence"');
  401. $result->execute();
  402. $this->_hasSequences = (bool)$result->rowCount();
  403. $result->closeCursor();
  404. return $this->_hasSequences;
  405. }
  406. }