diff options
author | Dries Buytaert <dries@buytaert.net> | 2009-09-29 15:13:57 +0000 |
---|---|---|
committer | Dries Buytaert <dries@buytaert.net> | 2009-09-29 15:13:57 +0000 |
commit | cef10893892a1c40f73fd972969c3512b0983cd6 (patch) | |
tree | c295a5dea1cc8f5d0ced7e7c967c70cf34f33c73 /includes/database/sqlite | |
parent | 0a0b067c2404b041454cc7a5fc8cfbbb9ecd6027 (diff) | |
download | brdo-cef10893892a1c40f73fd972969c3512b0983cd6.tar.gz brdo-cef10893892a1c40f73fd972969c3512b0983cd6.tar.bz2 |
- Patch #570900 by Crell | asimmonds: Changed Destroy remnants of update_sql().
Diffstat (limited to 'includes/database/sqlite')
-rw-r--r-- | includes/database/sqlite/schema.inc | 119 |
1 files changed, 49 insertions, 70 deletions
diff --git a/includes/database/sqlite/schema.inc b/includes/database/sqlite/schema.inc index 4f3df5f74..893f54be7 100644 --- a/includes/database/sqlite/schema.inc +++ b/includes/database/sqlite/schema.inc @@ -86,16 +86,16 @@ class DatabaseSchema_sqlite extends DatabaseSchema { * Build the SQL expression for keys. */ protected function createKeySql($fields) { - $ret = array(); + $return = array(); foreach ($fields as $field) { if (is_array($field)) { - $ret[] = $field[0]; + $return[] = $field[0]; } else { - $ret[] = $field; + $return[] = $field; } } - return implode(', ', $ret); + return implode(', ', $return); } /** @@ -214,34 +214,28 @@ class DatabaseSchema_sqlite extends DatabaseSchema { /** * Rename a table. * - * @param $ret - * Array to which query results will be added. * @param $table * The table to be renamed. * @param $new_name * The new name for the table. */ - public function renameTable(&$ret, $table, $new_name) { - $ret[] = update_sql('ALTER TABLE {' . $table . '} RENAME TO {' . $new_name . '}'); + public function renameTable($table, $new_name) { + $this->connection->query('ALTER TABLE {' . $table . '} RENAME TO {' . $new_name . '}'); } /** * Drop a table. * - * @param $ret - * Array to which query results will be added. * @param $table * The table to be dropped. */ - public function dropTable(&$ret, $table) { - $ret[] = update_sql('DROP TABLE {' . $table . '}'); + public function dropTable($table) { + $this->connection->query('DROP TABLE {' . $table . '}'); } /** * Add a new field to a table. * - * @param $ret - * Array to which query results will be added. * @param $table * Name of the table to be altered. * @param $field @@ -249,11 +243,11 @@ class DatabaseSchema_sqlite extends DatabaseSchema { * @param $spec * The field specification array, as taken from a schema definition. */ - public function addField(&$ret, $table, $field, $spec, $keys_new = array()) { + public function addField($table, $field, $spec, $keys_new = array()) { // TODO: $keys_new is not supported yet. $query = 'ALTER TABLE {' . $table . '} ADD '; $query .= $this->createFieldSql($field, $this->processField($spec)); - $ret[] = update_sql($query); + $this->connection->query($query); } /** @@ -262,30 +256,32 @@ class DatabaseSchema_sqlite extends DatabaseSchema { * As SQLite does not support ALTER TABLE (with a few exceptions) it is * necessary to create a new table and copy over the old content. * - * @param $ret - * Array to which query results will be added. * @param $table * Name of the table to be altered. * @param $new_schema * The new schema array for the table. */ - protected function alterTable(&$ret, $table, $new_schema) { + protected function alterTable($table, $new_schema) { $i = 0; do { $new_table = $table . '_' . $i++; } while ($this->tableExists($new_table)); - $this->createTable($ret, $new_table, $new_schema); - $fields = implode(', ', array_keys($new_schema['fields'])); - $ret[] = update_sql('INSERT INTO {' . $new_table . "} ($fields) SELECT $fields FROM {" . $table . '}'); - $old_count = db_query('SELECT COUNT(*) FROM {' . $table . '}')->fetchField(); - $new_count = db_query('SELECT COUNT(*) FROM {' . $new_table . '}')->fetchField(); + + $this->createTable($new_table, $new_schema); + + $select = $this->connection->select($table)->fields($new_schema['fields']); + $this->connection->insert($new_table) + ->from($select) + ->execute(); + $old_count = $this->connection->query('SELECT COUNT(*) FROM {' . $table . '}')->fetchField(); + $new_count = $this->connection->query('SELECT COUNT(*) FROM {' . $new_table . '}')->fetchField(); if ($old_count == $new_count) { do { $temp_table = $table . '_' . $i++; } while ($this->tableExists($temp_table)); - $this->renameTable($ret, $table, $temp_table); - $this->renameTable($ret, $new_table, $table); - $this->dropTable($ret, $temp_table); + $this->renameTable($table, $temp_table); + $this->renameTable($new_table, $table); + $this->dropTable($temp_table); } } @@ -305,7 +301,8 @@ class DatabaseSchema_sqlite extends DatabaseSchema { protected function introspectSchema($table) { $mapped_fields = array_flip($this->getFieldTypeMap()); $schema = array(); - foreach (db_query("PRAGMA table_info('{" . $table . "}')") as $row) { + $result = $this->connection->query("PRAGMA table_info('{" . $table . "}')"); + foreach ($result as $row) { if (preg_match('/^([^(]+)\((.*)\)$/', $row->type, $matches)) { $type = $matches[1]; $length = $matches[2]; @@ -334,7 +331,8 @@ class DatabaseSchema_sqlite extends DatabaseSchema { } } $indexes = array(); - foreach (db_query("PRAGMA index_list('{" . $table . "}')") as $row) { + $result = $this->connection->query("PRAGMA index_list('{" . $table . "}')"); + foreach ($result as $row) { if (strpos($row->name, 'sqlite_autoindex_') !== 0) { $indexes[] = array( 'schema_key' => $row->unique ? 'unique keys' : 'indexes', @@ -346,7 +344,8 @@ class DatabaseSchema_sqlite extends DatabaseSchema { foreach ($indexes as $index) { $name = $index['name']; $index_name = substr($name, $n); - foreach (db_query("PRAGMA index_info('$name')") as $row) { + $result = $this->connection->query("PRAGMA index_info('$name')"); + foreach ($result as $row) { $schema[$index['schema_key']][$index_name][] = $row->name; } } @@ -359,14 +358,12 @@ class DatabaseSchema_sqlite extends DatabaseSchema { * This implementation can't use ALTER TABLE directly, because SQLite only * supports a limited subset of that command. * - * @param $ret - * Array to which query results will be added. * @param $table * The table to be altered. * @param $field * The field to be dropped. */ - public function dropField(&$ret, $table, $field) { + public function dropField($table, $field) { $new_schema = $this->introspectSchema($table); unset($new_schema['fields'][$field]); foreach ($new_schema['indexes'] as $index => $fields) { @@ -380,7 +377,7 @@ class DatabaseSchema_sqlite extends DatabaseSchema { unset($new_schema['indexes'][$index]); } } - $this->alterTable($ret, $table, $new_schema); + $this->alterTable($table, $new_schema); } /** @@ -389,8 +386,6 @@ class DatabaseSchema_sqlite extends DatabaseSchema { * This implementation can't use ALTER TABLE directly, because SQLite only * supports a limited subset of that command. * - * @param $ret - * Array to which query results will be added. * @param $table * Name of the table. * @param $field @@ -404,7 +399,7 @@ class DatabaseSchema_sqlite extends DatabaseSchema { * table along with changing the field. The format is the same as a * table specification but without the 'fields' element. */ - public function changeField(&$ret, $table, $field, $field_new, $spec, $keys_new = array()) { + public function changeField($table, $field, $field_new, $spec, $keys_new = array()) { $new_schema = $this->introspectSchema($table); unset($new_schema['fields'][$field]); $new_schema['fields'][$field_new] = $spec; @@ -417,14 +412,12 @@ class DatabaseSchema_sqlite extends DatabaseSchema { $new_schema[$k] = $keys_new[$k] + $new_schema[$k]; } } - $this->alterTable($ret, $table, $new_schema); + $this->alterTable($table, $new_schema); } /** * Add an index. * - * @param $ret - * Array to which query results will be added. * @param $table * The table to be altered. * @param $name @@ -432,33 +425,29 @@ class DatabaseSchema_sqlite extends DatabaseSchema { * @param $fields * An array of field names. */ - public function addIndex(&$ret, $table, $name, $fields) { + public function addIndex($table, $name, $fields) { $schema['indexes'][$name] = $fields; $statements = $this->createIndexSql($table, $schema); foreach ($statements as $statement) { - $ret[] = update_sql($statement); + $this->connection->query($statement); } } /** * Drop an index. * - * @param $ret - * Array to which query results will be added. * @param $table * The table to be altered. * @param $name * The name of the index. */ - public function dropIndex(&$ret, $table, $name) { - $ret[] = update_sql('DROP INDEX ' . '{' . $table . '}_' . $name); + public function dropIndex($table, $name) { + $this->connection->query('DROP INDEX ' . '{' . $table . '}_' . $name); } /** * Add a unique key. * - * @param $ret - * Array to which query results will be added. * @param $table * The table to be altered. * @param $name @@ -466,26 +455,24 @@ class DatabaseSchema_sqlite extends DatabaseSchema { * @param $fields * An array of field names. */ - public function addUniqueKey(&$ret, $table, $name, $fields) { + public function addUniqueKey($table, $name, $fields) { $schema['unique keys'][$name] = $fields; $statements = $this->createIndexSql($table, $schema); foreach ($statements as $statement) { - $ret[] = update_sql($statement); + $this->connection->query($statement); } } /** * Drop a unique key. * - * @param $ret - * Array to which query results will be added. * @param $table * The table to be altered. * @param $name * The name of the key. */ - public function dropUniqueKey(&$ret, $table, $name) { - $ret[] = update_sql('DROP INDEX ' . '{' . $table . '}_' . $name); + public function dropUniqueKey($table, $name) { + $this->connection->query('DROP INDEX ' . '{' . $table . '}_' . $name); } /** @@ -494,17 +481,15 @@ class DatabaseSchema_sqlite extends DatabaseSchema { * This implementation can't use ALTER TABLE directly, because SQLite only * supports a limited subset of that command. * - * @param $ret - * Array to which query results will be added. * @param $table * The table to be altered. * @param $fields * Fields for the primary key. */ - public function addPrimaryKey(&$ret, $table, $fields) { + public function addPrimaryKey($table, $fields) { $new_schema = $this->introspectSchema($table); $new_schema['primary key'] = $fields; - $this->alterTable($ret, $table, $new_schema); + $this->alterTable($table, $new_schema); } /** @@ -513,15 +498,13 @@ class DatabaseSchema_sqlite extends DatabaseSchema { * This implementation can't use ALTER TABLE directly, because SQLite only * supports a limited subset of that command.` * - * @param $ret - * Array to which query results will be added. * @param $table * The table to be altered. */ - public function dropPrimaryKey(&$ret, $table) { + public function dropPrimaryKey($table) { $new_schema = $this->introspectSchema($table); unset($new_schema['primary key']); - $this->alterTable($ret, $table, $new_schema); + $this->alterTable($table, $new_schema); } /** @@ -530,8 +513,6 @@ class DatabaseSchema_sqlite extends DatabaseSchema { * This implementation can't use ALTER TABLE directly, because SQLite only * supports a limited subset of that command. * - * @param $ret - * Array to which query results will be added. * @param $table * The table to be altered. * @param $field @@ -539,10 +520,10 @@ class DatabaseSchema_sqlite extends DatabaseSchema { * @param $default * Default value to be set. NULL for 'default NULL'. */ - public function fieldSetDefault(&$ret, $table, $field, $default) { + public function fieldSetDefault($table, $field, $default) { $new_schema = $this->introspectSchema($table); $new_schema['fields'][$field]['default'] = $default; - $this->alterTable($ret, $table, $new_schema); + $this->alterTable($table, $new_schema); } /** @@ -551,17 +532,15 @@ class DatabaseSchema_sqlite extends DatabaseSchema { * This implementation can't use ALTER TABLE directly, because SQLite only * supports a limited subset of that command. * - * @param $ret - * Array to which query results will be added. * @param $table * The table to be altered. * @param $field * The field to be altered. */ - public function fieldSetNoDefault(&$ret, $table, $field) { + public function fieldSetNoDefault($table, $field) { $new_schema = $this->introspectSchema($table); unset($new_schema['fields'][$field]['default']); - $this->alterTable($ret, $table, $new_schema); + $this->alterTable($table, $new_schema); } /** |