PageRenderTime 52ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Database/Schema/SqliteSchema.php

https://gitlab.com/0072016/0072016-fbphp
PHP | 483 lines | 333 code | 53 blank | 97 comment | 53 complexity | 9c49c6aa146f1a606a221e2cc95b3e6a 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' => $this->_defaultValue($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. * Manipulate the default value.
  140. *
  141. * Sqlite includes quotes and bared NULLs in default values.
  142. * We need to remove those.
  143. *
  144. * @param string|null $default The default value.
  145. * @return string|null
  146. */
  147. protected function _defaultValue($default)
  148. {
  149. if ($default === 'NULL') {
  150. return null;
  151. }
  152. // Remove quotes
  153. if (preg_match("/^'(.*)'$/", $default, $matches)) {
  154. return str_replace("''", "'", $matches[1]);
  155. }
  156. return $default;
  157. }
  158. /**
  159. * {@inheritDoc}
  160. */
  161. public function describeIndexSql($tableName, $config)
  162. {
  163. $sql = sprintf(
  164. 'PRAGMA index_list(%s)',
  165. $this->_driver->quoteIdentifier($tableName)
  166. );
  167. return [$sql, []];
  168. }
  169. /**
  170. * {@inheritDoc}
  171. *
  172. * Since SQLite does not have a way to get metadata about all indexes at once,
  173. * additional queries are done here. Sqlite constraint names are not
  174. * stable, and the names for constraints will not match those used to create
  175. * the table. This is a limitation in Sqlite's metadata features.
  176. *
  177. */
  178. public function convertIndexDescription(Table $table, $row)
  179. {
  180. $sql = sprintf(
  181. 'PRAGMA index_info(%s)',
  182. $this->_driver->quoteIdentifier($row['name'])
  183. );
  184. $statement = $this->_driver->prepare($sql);
  185. $statement->execute();
  186. $columns = [];
  187. foreach ($statement->fetchAll('assoc') as $column) {
  188. $columns[] = $column['name'];
  189. }
  190. $statement->closeCursor();
  191. if ($row['unique']) {
  192. $table->addConstraint($row['name'], [
  193. 'type' => Table::CONSTRAINT_UNIQUE,
  194. 'columns' => $columns
  195. ]);
  196. } else {
  197. $table->addIndex($row['name'], [
  198. 'type' => Table::INDEX_INDEX,
  199. 'columns' => $columns
  200. ]);
  201. }
  202. }
  203. /**
  204. * {@inheritDoc}
  205. */
  206. public function describeForeignKeySql($tableName, $config)
  207. {
  208. $sql = sprintf('PRAGMA foreign_key_list(%s)', $this->_driver->quoteIdentifier($tableName));
  209. return [$sql, []];
  210. }
  211. /**
  212. * {@inheritDoc}
  213. */
  214. public function convertForeignKeyDescription(Table $table, $row)
  215. {
  216. $name = $row['from'] . '_fk';
  217. $update = isset($row['on_update']) ? $row['on_update'] : '';
  218. $delete = isset($row['on_delete']) ? $row['on_delete'] : '';
  219. $data = [
  220. 'type' => Table::CONSTRAINT_FOREIGN,
  221. 'columns' => [$row['from']],
  222. 'references' => [$row['table'], $row['to']],
  223. 'update' => $this->_convertOnClause($update),
  224. 'delete' => $this->_convertOnClause($delete),
  225. ];
  226. if (isset($this->_constraintsIdMap[$table->name()][$row['id']])) {
  227. $name = $this->_constraintsIdMap[$table->name()][$row['id']];
  228. } else {
  229. $this->_constraintsIdMap[$table->name()][$row['id']] = $name;
  230. }
  231. $table->addConstraint($name, $data);
  232. }
  233. /**
  234. * {@inheritDoc}
  235. *
  236. * @throws \Cake\Database\Exception when the column type is unknown
  237. */
  238. public function columnSql(Table $table, $name)
  239. {
  240. $data = $table->column($name);
  241. $typeMap = [
  242. 'uuid' => ' CHAR(36)',
  243. 'biginteger' => ' BIGINT',
  244. 'boolean' => ' BOOLEAN',
  245. 'binary' => ' BLOB',
  246. 'float' => ' FLOAT',
  247. 'decimal' => ' DECIMAL',
  248. 'date' => ' DATE',
  249. 'time' => ' TIME',
  250. 'datetime' => ' DATETIME',
  251. 'timestamp' => ' TIMESTAMP',
  252. ];
  253. $out = $this->_driver->quoteIdentifier($name);
  254. $hasUnsigned = ['biginteger', 'integer', 'float', 'decimal'];
  255. if (in_array($data['type'], $hasUnsigned, true) &&
  256. isset($data['unsigned']) && $data['unsigned'] === true
  257. ) {
  258. if ($data['type'] !== 'integer' || [$name] !== (array)$table->primaryKey()) {
  259. $out .= ' UNSIGNED';
  260. }
  261. }
  262. if (isset($typeMap[$data['type']])) {
  263. $out .= $typeMap[$data['type']];
  264. }
  265. if ($data['type'] === 'text' && $data['length'] !== Table::LENGTH_TINY) {
  266. $out .= ' TEXT';
  267. }
  268. if ($data['type'] === 'string' || ($data['type'] === 'text' && $data['length'] === Table::LENGTH_TINY)) {
  269. $out .= ' VARCHAR';
  270. if (isset($data['length'])) {
  271. $out .= '(' . (int)$data['length'] . ')';
  272. }
  273. }
  274. if ($data['type'] === 'integer') {
  275. $out .= ' INTEGER';
  276. if (isset($data['length']) && [$name] !== (array)$table->primaryKey()) {
  277. $out .= '(' . (int)$data['length'] . ')';
  278. }
  279. }
  280. $hasPrecision = ['float', 'decimal'];
  281. if (in_array($data['type'], $hasPrecision, true) &&
  282. (isset($data['length']) || isset($data['precision']))
  283. ) {
  284. $out .= '(' . (int)$data['length'] . ',' . (int)$data['precision'] . ')';
  285. }
  286. if (isset($data['null']) && $data['null'] === false) {
  287. $out .= ' NOT NULL';
  288. }
  289. if ($data['type'] === 'integer' && [$name] === (array)$table->primaryKey()) {
  290. $out .= ' PRIMARY KEY AUTOINCREMENT';
  291. }
  292. if (isset($data['null']) && $data['null'] === true) {
  293. $out .= ' DEFAULT NULL';
  294. unset($data['default']);
  295. }
  296. if (isset($data['default'])) {
  297. $out .= ' DEFAULT ' . $this->_driver->schemaValue($data['default']);
  298. }
  299. return $out;
  300. }
  301. /**
  302. * {@inheritDoc}
  303. *
  304. * Note integer primary keys will return ''. This is intentional as Sqlite requires
  305. * that integer primary keys be defined in the column definition.
  306. *
  307. */
  308. public function constraintSql(Table $table, $name)
  309. {
  310. $data = $table->constraint($name);
  311. if ($data['type'] === Table::CONSTRAINT_PRIMARY &&
  312. count($data['columns']) === 1 &&
  313. $table->column($data['columns'][0])['type'] === 'integer'
  314. ) {
  315. return '';
  316. }
  317. $clause = '';
  318. if ($data['type'] === Table::CONSTRAINT_PRIMARY) {
  319. $type = 'PRIMARY KEY';
  320. }
  321. if ($data['type'] === Table::CONSTRAINT_UNIQUE) {
  322. $type = 'UNIQUE';
  323. }
  324. if ($data['type'] === Table::CONSTRAINT_FOREIGN) {
  325. $type = 'FOREIGN KEY';
  326. $clause = sprintf(
  327. ' REFERENCES %s (%s) ON UPDATE %s ON DELETE %s',
  328. $this->_driver->quoteIdentifier($data['references'][0]),
  329. $this->_convertConstraintColumns($data['references'][1]),
  330. $this->_foreignOnClause($data['update']),
  331. $this->_foreignOnClause($data['delete'])
  332. );
  333. }
  334. $columns = array_map(
  335. [$this->_driver, 'quoteIdentifier'],
  336. $data['columns']
  337. );
  338. return sprintf(
  339. 'CONSTRAINT %s %s (%s)%s',
  340. $this->_driver->quoteIdentifier($name),
  341. $type,
  342. implode(', ', $columns),
  343. $clause
  344. );
  345. }
  346. /**
  347. * {@inheritDoc}
  348. *
  349. * SQLite can not properly handle adding a constraint to an existing table.
  350. * This method is no-op
  351. */
  352. public function addConstraintSql(Table $table)
  353. {
  354. return [];
  355. }
  356. /**
  357. * {@inheritDoc}
  358. *
  359. * SQLite can not properly handle dropping a constraint to an existing table.
  360. * This method is no-op
  361. */
  362. public function dropConstraintSql(Table $table)
  363. {
  364. return [];
  365. }
  366. /**
  367. * {@inheritDoc}
  368. */
  369. public function indexSql(Table $table, $name)
  370. {
  371. $data = $table->index($name);
  372. $columns = array_map(
  373. [$this->_driver, 'quoteIdentifier'],
  374. $data['columns']
  375. );
  376. return sprintf(
  377. 'CREATE INDEX %s ON %s (%s)',
  378. $this->_driver->quoteIdentifier($name),
  379. $this->_driver->quoteIdentifier($table->name()),
  380. implode(', ', $columns)
  381. );
  382. }
  383. /**
  384. * {@inheritDoc}
  385. */
  386. public function createTableSql(Table $table, $columns, $constraints, $indexes)
  387. {
  388. $lines = array_merge($columns, $constraints);
  389. $content = implode(",\n", array_filter($lines));
  390. $temporary = $table->temporary() ? ' TEMPORARY ' : ' ';
  391. $table = sprintf("CREATE%sTABLE \"%s\" (\n%s\n)", $temporary, $table->name(), $content);
  392. $out = [$table];
  393. foreach ($indexes as $index) {
  394. $out[] = $index;
  395. }
  396. return $out;
  397. }
  398. /**
  399. * {@inheritDoc}
  400. */
  401. public function truncateTableSql(Table $table)
  402. {
  403. $name = $table->name();
  404. $sql = [];
  405. if ($this->hasSequences()) {
  406. $sql[] = sprintf('DELETE FROM sqlite_sequence WHERE name="%s"', $name);
  407. }
  408. $sql[] = sprintf('DELETE FROM "%s"', $name);
  409. return $sql;
  410. }
  411. /**
  412. * Returns whether there is any table in this connection to SQLite containing
  413. * sequences
  414. *
  415. * @return bool
  416. */
  417. public function hasSequences()
  418. {
  419. $result = $this->_driver
  420. ->prepare('SELECT 1 FROM sqlite_master WHERE name = "sqlite_sequence"');
  421. $result->execute();
  422. $this->_hasSequences = (bool)$result->rowCount();
  423. $result->closeCursor();
  424. return $this->_hasSequences;
  425. }
  426. }