3 namespace Drupal\Core\Database\Driver\sqlite;
5 use Drupal\Component\Utility\Unicode;
6 use Drupal\Core\Database\SchemaObjectExistsException;
7 use Drupal\Core\Database\SchemaObjectDoesNotExistException;
8 use Drupal\Core\Database\Schema as DatabaseSchema;
16 * SQLite implementation of \Drupal\Core\Database\Schema.
18 class Schema extends DatabaseSchema {
21 * Override DatabaseSchema::$defaultSchema
23 protected $defaultSchema = 'main';
25 public function tableExists($table) {
26 $info = $this->getPrefixInfo($table);
28 // Don't use {} around sqlite_master table.
29 return (bool) $this->connection->query('SELECT 1 FROM ' . $info['schema'] . '.sqlite_master WHERE type = :type AND name = :name', [':type' => 'table', ':name' => $info['table']])->fetchField();
32 public function fieldExists($table, $column) {
33 $schema = $this->introspectSchema($table);
34 return !empty($schema['fields'][$column]);
38 * Generate SQL to create a new table from a Drupal schema definition.
41 * The name of the table to create.
43 * A Schema API table definition array.
45 * An array of SQL statements to create the table.
47 public function createTableSql($name, $table) {
49 $sql[] = "CREATE TABLE {" . $name . "} (\n" . $this->createColumnsSql($name, $table) . "\n)\n";
50 return array_merge($sql, $this->createIndexSql($name, $table));
54 * Build the SQL expression for indexes.
56 protected function createIndexSql($tablename, $schema) {
58 $info = $this->getPrefixInfo($tablename);
59 if (!empty($schema['unique keys'])) {
60 foreach ($schema['unique keys'] as $key => $fields) {
61 $sql[] = 'CREATE UNIQUE INDEX ' . $info['schema'] . '.' . $info['table'] . '_' . $key . ' ON ' . $info['table'] . ' (' . $this->createKeySql($fields) . ")\n";
64 if (!empty($schema['indexes'])) {
65 foreach ($schema['indexes'] as $key => $fields) {
66 $sql[] = 'CREATE INDEX ' . $info['schema'] . '.' . $info['table'] . '_' . $key . ' ON ' . $info['table'] . ' (' . $this->createKeySql($fields) . ")\n";
73 * Build the SQL expression for creating columns.
75 protected function createColumnsSql($tablename, $schema) {
78 // Add the SQL statement for each field.
79 foreach ($schema['fields'] as $name => $field) {
80 if (isset($field['type']) && $field['type'] == 'serial') {
81 if (isset($schema['primary key']) && ($key = array_search($name, $schema['primary key'])) !== FALSE) {
82 unset($schema['primary key'][$key]);
85 $sql_array[] = $this->createFieldSql($name, $this->processField($field));
89 if (!empty($schema['primary key'])) {
90 $sql_array[] = " PRIMARY KEY (" . $this->createKeySql($schema['primary key']) . ")";
93 return implode(", \n", $sql_array);
97 * Build the SQL expression for keys.
99 protected function createKeySql($fields) {
101 foreach ($fields as $field) {
102 if (is_array($field)) {
103 $return[] = $field[0];
109 return implode(', ', $return);
113 * Set database-engine specific properties for a field.
116 * A field description array, as specified in the schema documentation.
118 protected function processField($field) {
119 if (!isset($field['size'])) {
120 $field['size'] = 'normal';
123 // Set the correct database-engine specific datatype.
124 // In case one is already provided, force it to uppercase.
125 if (isset($field['sqlite_type'])) {
126 $field['sqlite_type'] = Unicode::strtoupper($field['sqlite_type']);
129 $map = $this->getFieldTypeMap();
130 $field['sqlite_type'] = $map[$field['type'] . ':' . $field['size']];
132 // Numeric fields with a specified scale have to be stored as floats.
133 if ($field['sqlite_type'] === 'NUMERIC' && isset($field['scale'])) {
134 $field['sqlite_type'] = 'FLOAT';
138 if (isset($field['type']) && $field['type'] == 'serial') {
139 $field['auto_increment'] = TRUE;
146 * Create an SQL string for a field to be used in table creation or alteration.
148 * Before passing a field out of a schema definition into this function it has
149 * to be processed by db_processField().
154 * The field specification, as per the schema data structure format.
156 protected function createFieldSql($name, $spec) {
157 if (!empty($spec['auto_increment'])) {
158 $sql = $name . " INTEGER PRIMARY KEY AUTOINCREMENT";
159 if (!empty($spec['unsigned'])) {
160 $sql .= ' CHECK (' . $name . '>= 0)';
164 $sql = $name . ' ' . $spec['sqlite_type'];
166 if (in_array($spec['sqlite_type'], ['VARCHAR', 'TEXT'])) {
167 if (isset($spec['length'])) {
168 $sql .= '(' . $spec['length'] . ')';
171 if (isset($spec['binary']) && $spec['binary'] === FALSE) {
172 $sql .= ' COLLATE NOCASE_UTF8';
176 if (isset($spec['not null'])) {
177 if ($spec['not null']) {
185 if (!empty($spec['unsigned'])) {
186 $sql .= ' CHECK (' . $name . '>= 0)';
189 if (isset($spec['default'])) {
190 if (is_string($spec['default'])) {
191 $spec['default'] = $this->connection->quote($spec['default']);
193 $sql .= ' DEFAULT ' . $spec['default'];
196 if (empty($spec['not null']) && !isset($spec['default'])) {
197 $sql .= ' DEFAULT NULL';
204 * This maps a generic data type in combination with its data size
205 * to the engine-specific data type.
207 public function getFieldTypeMap() {
208 // Put :normal last so it gets preserved by array_flip. This makes
209 // it much easier for modules (such as schema.module) to map
210 // database types back into schema types.
211 // $map does not use drupal_static as its value never changes.
213 'varchar_ascii:normal' => 'VARCHAR',
215 'varchar:normal' => 'VARCHAR',
216 'char:normal' => 'CHAR',
218 'text:tiny' => 'TEXT',
219 'text:small' => 'TEXT',
220 'text:medium' => 'TEXT',
221 'text:big' => 'TEXT',
222 'text:normal' => 'TEXT',
224 'serial:tiny' => 'INTEGER',
225 'serial:small' => 'INTEGER',
226 'serial:medium' => 'INTEGER',
227 'serial:big' => 'INTEGER',
228 'serial:normal' => 'INTEGER',
230 'int:tiny' => 'INTEGER',
231 'int:small' => 'INTEGER',
232 'int:medium' => 'INTEGER',
233 'int:big' => 'INTEGER',
234 'int:normal' => 'INTEGER',
236 'float:tiny' => 'FLOAT',
237 'float:small' => 'FLOAT',
238 'float:medium' => 'FLOAT',
239 'float:big' => 'FLOAT',
240 'float:normal' => 'FLOAT',
242 'numeric:normal' => 'NUMERIC',
244 'blob:big' => 'BLOB',
245 'blob:normal' => 'BLOB',
250 public function renameTable($table, $new_name) {
251 if (!$this->tableExists($table)) {
252 throw new SchemaObjectDoesNotExistException(t("Cannot rename @table to @table_new: table @table doesn't exist.", ['@table' => $table, '@table_new' => $new_name]));
254 if ($this->tableExists($new_name)) {
255 throw new SchemaObjectExistsException(t("Cannot rename @table to @table_new: table @table_new already exists.", ['@table' => $table, '@table_new' => $new_name]));
258 $schema = $this->introspectSchema($table);
260 // SQLite doesn't allow you to rename tables outside of the current
261 // database. So the syntax '... RENAME TO database.table' would fail.
262 // So we must determine the full table name here rather than surrounding
263 // the table with curly braces in case the db_prefix contains a reference
264 // to a database outside of our existing database.
265 $info = $this->getPrefixInfo($new_name);
266 $this->connection->query('ALTER TABLE {' . $table . '} RENAME TO ' . $info['table']);
268 // Drop the indexes, there is no RENAME INDEX command in SQLite.
269 if (!empty($schema['unique keys'])) {
270 foreach ($schema['unique keys'] as $key => $fields) {
271 $this->dropIndex($table, $key);
274 if (!empty($schema['indexes'])) {
275 foreach ($schema['indexes'] as $index => $fields) {
276 $this->dropIndex($table, $index);
280 // Recreate the indexes.
281 $statements = $this->createIndexSql($new_name, $schema);
282 foreach ($statements as $statement) {
283 $this->connection->query($statement);
287 public function dropTable($table) {
288 if (!$this->tableExists($table)) {
291 $this->connection->tableDropped = TRUE;
292 $this->connection->query('DROP TABLE {' . $table . '}');
296 public function addField($table, $field, $specification, $keys_new = []) {
297 if (!$this->tableExists($table)) {
298 throw new SchemaObjectDoesNotExistException(t("Cannot add field @table.@field: table doesn't exist.", ['@field' => $field, '@table' => $table]));
300 if ($this->fieldExists($table, $field)) {
301 throw new SchemaObjectExistsException(t("Cannot add field @table.@field: field already exists.", ['@field' => $field, '@table' => $table]));
304 // SQLite doesn't have a full-featured ALTER TABLE statement. It only
305 // supports adding new fields to a table, in some simple cases. In most
306 // cases, we have to create a new table and copy the data over.
307 if (empty($keys_new) && (empty($specification['not null']) || isset($specification['default']))) {
308 // When we don't have to create new keys and we are not creating a
309 // NOT NULL column without a default value, we can use the quicker version.
310 $query = 'ALTER TABLE {' . $table . '} ADD ' . $this->createFieldSql($field, $this->processField($specification));
311 $this->connection->query($query);
313 // Apply the initial value if set.
314 if (isset($specification['initial'])) {
315 $this->connection->update($table)
316 ->fields([$field => $specification['initial']])
319 if (isset($specification['initial_from_field'])) {
320 $this->connection->update($table)
321 ->expression($field, $specification['initial_from_field'])
326 // We cannot add the field directly. Use the slower table alteration
327 // method, starting from the old schema.
328 $old_schema = $this->introspectSchema($table);
329 $new_schema = $old_schema;
331 // Add the new field.
332 $new_schema['fields'][$field] = $specification;
334 // Build the mapping between the old fields and the new fields.
336 if (isset($specification['initial'])) {
337 // If we have a initial value, copy it over.
339 'expression' => ':newfieldinitial',
340 'arguments' => [':newfieldinitial' => $specification['initial']],
343 elseif (isset($specification['initial_from_field'])) {
344 // If we have a initial value, copy it over.
346 'expression' => $specification['initial_from_field'],
351 // Else use the default of the field.
352 $mapping[$field] = NULL;
355 // Add the new indexes.
356 $new_schema = array_merge($new_schema, $keys_new);
358 $this->alterTable($table, $old_schema, $new_schema, $mapping);
363 * Create a table with a new schema containing the old content.
365 * As SQLite does not support ALTER TABLE (with a few exceptions) it is
366 * necessary to create a new table and copy over the old content.
369 * Name of the table to be altered.
371 * The old schema array for the table.
373 * The new schema array for the table.
375 * An optional mapping between the fields of the old specification and the
376 * fields of the new specification. An associative array, whose keys are
377 * the fields of the new table, and values can take two possible forms:
378 * - a simple string, which is interpreted as the name of a field of the
380 * - an associative array with two keys 'expression' and 'arguments',
381 * that will be used as an expression field.
383 protected function alterTable($table, $old_schema, $new_schema, array $mapping = []) {
386 $new_table = $table . '_' . $i++;
387 } while ($this->tableExists($new_table));
389 $this->createTable($new_table, $new_schema);
391 // Build a SQL query to migrate the data from the old table to the new.
392 $select = $this->connection->select($table);
394 // Complete the mapping.
395 $possible_keys = array_keys($new_schema['fields']);
396 $mapping += array_combine($possible_keys, $possible_keys);
398 // Now add the fields.
399 foreach ($mapping as $field_alias => $field_source) {
400 // Just ignore this field (ie. use it's default value).
401 if (!isset($field_source)) {
405 if (is_array($field_source)) {
406 $select->addExpression($field_source['expression'], $field_alias, $field_source['arguments']);
409 $select->addField($table, $field_source, $field_alias);
413 // Execute the data migration query.
414 $this->connection->insert($new_table)
418 $old_count = $this->connection->query('SELECT COUNT(*) FROM {' . $table . '}')->fetchField();
419 $new_count = $this->connection->query('SELECT COUNT(*) FROM {' . $new_table . '}')->fetchField();
420 if ($old_count == $new_count) {
421 $this->dropTable($table);
422 $this->renameTable($new_table, $table);
427 * Find out the schema of a table.
429 * This function uses introspection methods provided by the database to
430 * create a schema array. This is useful, for example, during update when
431 * the old schema is not available.
437 * An array representing the schema, from drupal_get_schema().
440 * If a column of the table could not be parsed.
442 protected function introspectSchema($table) {
443 $mapped_fields = array_flip($this->getFieldTypeMap());
451 $info = $this->getPrefixInfo($table);
452 $result = $this->connection->query('PRAGMA ' . $info['schema'] . '.table_info(' . $info['table'] . ')');
453 foreach ($result as $row) {
454 if (preg_match('/^([^(]+)\((.*)\)$/', $row->type, $matches)) {
456 $length = $matches[2];
462 if (isset($mapped_fields[$type])) {
463 list($type, $size) = explode(':', $mapped_fields[$type]);
464 $schema['fields'][$row->name] = [
467 'not null' => !empty($row->notnull),
468 'default' => trim($row->dflt_value, "'"),
471 $schema['fields'][$row->name]['length'] = $length;
474 $schema['primary key'][] = $row->name;
478 throw new \Exception("Unable to parse the column type " . $row->type);
482 $result = $this->connection->query('PRAGMA ' . $info['schema'] . '.index_list(' . $info['table'] . ')');
483 foreach ($result as $row) {
484 if (strpos($row->name, 'sqlite_autoindex_') !== 0) {
486 'schema_key' => $row->unique ? 'unique keys' : 'indexes',
487 'name' => $row->name,
491 foreach ($indexes as $index) {
492 $name = $index['name'];
493 // Get index name without prefix.
494 $index_name = substr($name, strlen($info['table']) + 1);
495 $result = $this->connection->query('PRAGMA ' . $info['schema'] . '.index_info(' . $name . ')');
496 foreach ($result as $row) {
497 $schema[$index['schema_key']][$index_name][] = $row->name;
503 public function dropField($table, $field) {
504 if (!$this->fieldExists($table, $field)) {
508 $old_schema = $this->introspectSchema($table);
509 $new_schema = $old_schema;
511 unset($new_schema['fields'][$field]);
513 // Handle possible primary key changes.
514 if (isset($new_schema['primary key']) && ($key = array_search($field, $new_schema['primary key'])) !== FALSE) {
515 unset($new_schema['primary key'][$key]);
518 // Handle possible index changes.
519 foreach ($new_schema['indexes'] as $index => $fields) {
520 foreach ($fields as $key => $field_name) {
521 if ($field_name == $field) {
522 unset($new_schema['indexes'][$index][$key]);
525 // If this index has no more fields then remove it.
526 if (empty($new_schema['indexes'][$index])) {
527 unset($new_schema['indexes'][$index]);
530 $this->alterTable($table, $old_schema, $new_schema);
534 public function changeField($table, $field, $field_new, $spec, $keys_new = []) {
535 if (!$this->fieldExists($table, $field)) {
536 throw new SchemaObjectDoesNotExistException(t("Cannot change the definition of field @table.@name: field doesn't exist.", ['@table' => $table, '@name' => $field]));
538 if (($field != $field_new) && $this->fieldExists($table, $field_new)) {
539 throw new SchemaObjectExistsException(t("Cannot rename field @table.@name to @name_new: target field already exists.", ['@table' => $table, '@name' => $field, '@name_new' => $field_new]));
542 $old_schema = $this->introspectSchema($table);
543 $new_schema = $old_schema;
545 // Map the old field to the new field.
546 if ($field != $field_new) {
547 $mapping[$field_new] = $field;
553 // Remove the previous definition and swap in the new one.
554 unset($new_schema['fields'][$field]);
555 $new_schema['fields'][$field_new] = $spec;
557 // Map the former indexes to the new column name.
558 $new_schema['primary key'] = $this->mapKeyDefinition($new_schema['primary key'], $mapping);
559 foreach (['unique keys', 'indexes'] as $k) {
560 foreach ($new_schema[$k] as &$key_definition) {
561 $key_definition = $this->mapKeyDefinition($key_definition, $mapping);
565 // Add in the keys from $keys_new.
566 if (isset($keys_new['primary key'])) {
567 $new_schema['primary key'] = $keys_new['primary key'];
569 foreach (['unique keys', 'indexes'] as $k) {
570 if (!empty($keys_new[$k])) {
571 $new_schema[$k] = $keys_new[$k] + $new_schema[$k];
575 $this->alterTable($table, $old_schema, $new_schema, $mapping);
579 * Utility method: rename columns in an index definition according to a new mapping.
581 * @param $key_definition
582 * The key definition.
586 protected function mapKeyDefinition(array $key_definition, array $mapping) {
587 foreach ($key_definition as &$field) {
588 // The key definition can be an array($field, $length).
589 if (is_array($field)) {
592 if (isset($mapping[$field])) {
593 $field = $mapping[$field];
596 return $key_definition;
602 public function addIndex($table, $name, $fields, array $spec) {
603 if (!$this->tableExists($table)) {
604 throw new SchemaObjectDoesNotExistException(t("Cannot add index @name to table @table: table doesn't exist.", ['@table' => $table, '@name' => $name]));
606 if ($this->indexExists($table, $name)) {
607 throw new SchemaObjectExistsException(t("Cannot add index @name to table @table: index already exists.", ['@table' => $table, '@name' => $name]));
610 $schema['indexes'][$name] = $fields;
611 $statements = $this->createIndexSql($table, $schema);
612 foreach ($statements as $statement) {
613 $this->connection->query($statement);
617 public function indexExists($table, $name) {
618 $info = $this->getPrefixInfo($table);
620 return $this->connection->query('PRAGMA ' . $info['schema'] . '.index_info(' . $info['table'] . '_' . $name . ')')->fetchField() != '';
623 public function dropIndex($table, $name) {
624 if (!$this->indexExists($table, $name)) {
628 $info = $this->getPrefixInfo($table);
630 $this->connection->query('DROP INDEX ' . $info['schema'] . '.' . $info['table'] . '_' . $name);
634 public function addUniqueKey($table, $name, $fields) {
635 if (!$this->tableExists($table)) {
636 throw new SchemaObjectDoesNotExistException(t("Cannot add unique key @name to table @table: table doesn't exist.", ['@table' => $table, '@name' => $name]));
638 if ($this->indexExists($table, $name)) {
639 throw new SchemaObjectExistsException(t("Cannot add unique key @name to table @table: unique key already exists.", ['@table' => $table, '@name' => $name]));
642 $schema['unique keys'][$name] = $fields;
643 $statements = $this->createIndexSql($table, $schema);
644 foreach ($statements as $statement) {
645 $this->connection->query($statement);
649 public function dropUniqueKey($table, $name) {
650 if (!$this->indexExists($table, $name)) {
654 $info = $this->getPrefixInfo($table);
656 $this->connection->query('DROP INDEX ' . $info['schema'] . '.' . $info['table'] . '_' . $name);
660 public function addPrimaryKey($table, $fields) {
661 if (!$this->tableExists($table)) {
662 throw new SchemaObjectDoesNotExistException(t("Cannot add primary key to table @table: table doesn't exist.", ['@table' => $table]));
665 $old_schema = $this->introspectSchema($table);
666 $new_schema = $old_schema;
668 if (!empty($new_schema['primary key'])) {
669 throw new SchemaObjectExistsException(t("Cannot add primary key to table @table: primary key already exists.", ['@table' => $table]));
672 $new_schema['primary key'] = $fields;
673 $this->alterTable($table, $old_schema, $new_schema);
676 public function dropPrimaryKey($table) {
677 $old_schema = $this->introspectSchema($table);
678 $new_schema = $old_schema;
680 if (empty($new_schema['primary key'])) {
684 unset($new_schema['primary key']);
685 $this->alterTable($table, $old_schema, $new_schema);
689 public function fieldSetDefault($table, $field, $default) {
690 if (!$this->fieldExists($table, $field)) {
691 throw new SchemaObjectDoesNotExistException(t("Cannot set default value of field @table.@field: field doesn't exist.", ['@table' => $table, '@field' => $field]));
694 $old_schema = $this->introspectSchema($table);
695 $new_schema = $old_schema;
697 $new_schema['fields'][$field]['default'] = $default;
698 $this->alterTable($table, $old_schema, $new_schema);
701 public function fieldSetNoDefault($table, $field) {
702 if (!$this->fieldExists($table, $field)) {
703 throw new SchemaObjectDoesNotExistException(t("Cannot remove default value of field @table.@field: field doesn't exist.", ['@table' => $table, '@field' => $field]));
706 $old_schema = $this->introspectSchema($table);
707 $new_schema = $old_schema;
709 unset($new_schema['fields'][$field]['default']);
710 $this->alterTable($table, $old_schema, $new_schema);
716 public function findTables($table_expression) {
719 // The SQLite implementation doesn't need to use the same filtering strategy
720 // as the parent one because individually prefixed tables live in their own
721 // schema (database), which means that neither the main database nor any
722 // attached one will contain a prefixed table name, so we just need to loop
723 // over all known schemas and filter by the user-supplied table expression.
724 $attached_dbs = $this->connection->getAttachedDatabases();
725 foreach ($attached_dbs as $schema) {
726 // Can't use query placeholders for the schema because the query would
727 // have to be :prefixsqlite_master, which does not work. We also need to
728 // ignore the internal SQLite tables.
729 $result = $this->connection->query("SELECT name FROM " . $schema . ".sqlite_master WHERE type = :type AND name LIKE :table_name AND name NOT LIKE :pattern", [
731 ':table_name' => $table_expression,
732 ':pattern' => 'sqlite_%',
734 $tables += $result->fetchAllKeyed(0, 0);