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/pgsql/schema.inc | |
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/pgsql/schema.inc')
-rw-r--r-- | includes/database/pgsql/schema.inc | 147 |
1 files changed, 55 insertions, 92 deletions
diff --git a/includes/database/pgsql/schema.inc b/includes/database/pgsql/schema.inc index c99fc78b9..e2aefe108 100644 --- a/includes/database/pgsql/schema.inc +++ b/includes/database/pgsql/schema.inc @@ -52,7 +52,11 @@ class DatabaseSchema_pgsql extends DatabaseSchema { 'sequences' => array(), ); // Don't use {} around information_schema.columns table. - $result = db_query("SELECT column_name, data_type, column_default FROM information_schema.columns WHERE table_schema = :schema AND table_name = :table AND (data_type = 'bytea' OR (numeric_precision IS NOT NULL AND column_default LIKE :default))", array(':schema' => $schema, ':table' => $table_name, ':default' => '%nextval%')); + $result = $this->connection->query("SELECT column_name, data_type, column_default FROM information_schema.columns WHERE table_schema = :schema AND table_name = :table AND (data_type = 'bytea' OR (numeric_precision IS NOT NULL AND column_default LIKE :default))", array( + ':schema' => $schema, + ':table' => $table_name, + ':default' => '%nextval%', + )); foreach ($result as $column) { if ($column->data_type == 'bytea') { $table_information->blob_fields[$column->column_name] = TRUE; @@ -245,7 +249,7 @@ class DatabaseSchema_pgsql extends DatabaseSchema { 'date:normal' => 'date', 'datetime:normal' => 'timestamp without time zone', - + 'time:normal' => 'time without time zone', 'serial:tiny' => 'serial', @@ -258,49 +262,29 @@ class DatabaseSchema_pgsql extends DatabaseSchema { } protected function _createKeySql($fields) { - $ret = array(); + $return = array(); foreach ($fields as $field) { if (is_array($field)) { - $ret[] = 'substr(' . $field[0] . ', 1, ' . $field[1] . ')'; + $return[] = 'substr(' . $field[0] . ', 1, ' . $field[1] . ')'; } else { - $ret[] = '"' . $field . '"'; + $return[] = '"' . $field . '"'; } } - return implode(', ', $ret); + return implode(', ', $return); } - /** - * 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. - */ - function renameTable(&$ret, $table, $new_name) { - $ret[] = update_sql('ALTER TABLE {' . $table . '} RENAME TO {' . $new_name . '}'); + 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 @@ -321,7 +305,7 @@ class DatabaseSchema_pgsql extends DatabaseSchema { * * @see db_change_field() */ - public function addField(&$ret, $table, $field, $spec, $new_keys = array()) { + public function addField($table, $field, $spec, $new_keys = array()) { $fixnull = FALSE; if (!empty($spec['not null']) && !isset($spec['default'])) { $fixnull = TRUE; @@ -329,44 +313,39 @@ class DatabaseSchema_pgsql extends DatabaseSchema { } $query = 'ALTER TABLE {' . $table . '} ADD COLUMN '; $query .= $this->createFieldSql($field, $this->processField($spec)); - $ret[] = update_sql($query); + $this->connection->query($query); if (isset($spec['initial'])) { - // All this because update_sql does not support %-placeholders. - $sql = 'UPDATE {' . $table . '} SET ' . $field . ' = :value'; - $result = db_query($sql, array(':value' => $spec['initial'])); - $ret[] = array('success' => $result !== FALSE, 'query' => check_plain($sql . ' (' . $spec['initial'] . ')')); + $this->connection->update($table) + ->fields(array($field, $spec['initial'])) + ->execute(); } if ($fixnull) { - $ret[] = update_sql("ALTER TABLE {" . $table . "} ALTER $field SET NOT NULL"); + $this->connection->query("ALTER TABLE {" . $table . "} ALTER $field SET NOT NULL"); } if (isset($new_keys)) { - $this->_createKeys($ret, $table, $new_keys); + $this->_createKeys($table, $new_keys); } // Add column comment. if (!empty($spec['description'])) { - $ret[] = update_sql('COMMENT ON COLUMN {' . $table . '}.' . $field . ' IS ' . $this->prepareComment($spec['description'])); + $this->connection->query('COMMENT ON COLUMN {' . $table . '}.' . $field . ' IS ' . $this->prepareComment($spec['description'])); } } /** * Drop a field. * - * @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) { - $ret[] = update_sql('ALTER TABLE {' . $table . '} DROP COLUMN "' . $field . '"'); + public function dropField($table, $field) { + $this->connection->query('ALTER TABLE {' . $table . '} DROP COLUMN "' . $field . '"'); } /** * Set the default value for a field. * - * @param $ret - * Array to which query results will be added. * @param $table * The table to be altered. * @param $field @@ -374,7 +353,7 @@ class DatabaseSchema_pgsql 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) { if (is_null($default)) { $default = 'NULL'; } @@ -382,54 +361,46 @@ class DatabaseSchema_pgsql extends DatabaseSchema { $default = is_string($default) ? "'$default'" : $default; } - $ret[] = update_sql('ALTER TABLE {' . $table . '} ALTER COLUMN "' . $field . '" SET DEFAULT ' . $default); + $this->connection->query('ALTER TABLE {' . $table . '} ALTER COLUMN "' . $field . '" SET DEFAULT ' . $default); } /** * Set a field to have no default value. * - * @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) { - $ret[] = update_sql('ALTER TABLE {' . $table . '} ALTER COLUMN "' . $field . '" DROP DEFAULT'); + public function fieldSetNoDefault($table, $field) { + $this->connection->query('ALTER TABLE {' . $table . '} ALTER COLUMN "' . $field . '" DROP DEFAULT'); } /** * Add a primary key. * - * @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) { - $ret[] = update_sql('ALTER TABLE {' . $table . '} ADD PRIMARY KEY (' . implode(',', $fields) . ')'); + public function addPrimaryKey($table, $fields) { + $this->connection->query('ALTER TABLE {' . $table . '} ADD PRIMARY KEY (' . implode(',', $fields) . ')'); } /** * Drop the primary key. * - * @param $ret - * Array to which query results will be added. * @param $table * The table to be altered. */ - public function dropPrimaryKey(&$ret, $table) { - $ret[] = update_sql('ALTER TABLE {' . $table . '} DROP CONSTRAINT {' . $table . '}_pkey'); + public function dropPrimaryKey($table) { + $this->connection->query('ALTER TABLE {' . $table . '} DROP CONSTRAINT {' . $table . '}_pkey'); } /** * Add a unique key. * - * @param $ret - * Array to which query results will be added. * @param $table * The table to be altered. * @param $name @@ -437,31 +408,27 @@ class DatabaseSchema_pgsql extends DatabaseSchema { * @param $fields * An array of field names. */ - function addUniqueKey(&$ret, $table, $name, $fields) { + function addUniqueKey($table, $name, $fields) { $name = '{' . $table . '}_' . $name . '_key'; - $ret[] = update_sql('ALTER TABLE {' . $table . '} ADD CONSTRAINT "' . $name . '" UNIQUE (' . implode(',', $fields) . ')'); + $this->connection->query('ALTER TABLE {' . $table . '} ADD CONSTRAINT "' . $name . '" UNIQUE (' . implode(',', $fields) . ')'); } /** * 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) { + public function dropUniqueKey($table, $name) { $name = '{' . $table . '}_' . $name . '_key'; - $ret[] = update_sql('ALTER TABLE {' . $table . '} DROP CONSTRAINT "' . $name . '"'); + $this->connection->query('ALTER TABLE {' . $table . '} DROP CONSTRAINT "' . $name . '"'); } /** * Add an index. * - * @param $ret - * Array to which query results will be added. * @param $table * The table to be altered. * @param $name @@ -469,23 +436,21 @@ class DatabaseSchema_pgsql extends DatabaseSchema { * @param $fields * An array of field names. */ - public function addIndex(&$ret, $table, $name, $fields) { - $ret[] = update_sql($this->_createIndexSql($table, $name, $fields)); + public function addIndex($table, $name, $fields) { + $this->connection->query($this->_createIndexSql($table, $name, $fields)); } /** * 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) { + public function dropIndex($table, $name) { $name = '{' . $table . '}_' . $name . '_idx'; - $ret[] = update_sql('DROP INDEX ' . $name); + $this->connection->query('DROP INDEX ' . $name); } /** @@ -511,8 +476,8 @@ class DatabaseSchema_pgsql extends DatabaseSchema { * and you want to change foo.bar to be type serial, leaving it as the * primary key. The correct sequence is: * @code - * db_drop_primary_key($ret, 'foo'); - * db_change_field($ret, 'foo', 'bar', 'bar', + * db_drop_primary_key('foo'); + * db_change_field('foo', 'bar', 'bar', * array('type' => 'serial', 'not null' => TRUE), * array('primary key' => array('bar'))); * @endcode @@ -535,8 +500,6 @@ class DatabaseSchema_pgsql extends DatabaseSchema { * unless you are converting a field to be type serial. You can use * the $new_keys argument in all cases. * - * @param $ret - * Array to which query results will be added. * @param $table * Name of the table. * @param $field @@ -550,15 +513,15 @@ class DatabaseSchema_pgsql 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, $new_keys = array()) { - $ret[] = update_sql('ALTER TABLE {' . $table . '} RENAME "' . $field . '" TO "' . $field . '_old"'); + public function changeField($table, $field, $field_new, $spec, $new_keys = array()) { + $this->connection->query('ALTER TABLE {' . $table . '} RENAME "' . $field . '" TO "' . $field . '_old"'); $not_null = isset($spec['not null']) ? $spec['not null'] : FALSE; unset($spec['not null']); if (!array_key_exists('size', $spec)) { $spec['size'] = 'normal'; } - $this->addField($ret, $table, "$field_new", $spec); + $this->addField($table, "$field_new", $spec); // We need to typecast the new column to best be able to transfer the data // Schema_pgsql::getFieldTypeMap() will return possibilities that are not @@ -568,16 +531,16 @@ class DatabaseSchema_pgsql extends DatabaseSchema { if (in_array($typecast, array('serial', 'bigserial', 'numeric'))) { $typecast = 'int'; } - $ret[] = update_sql("UPDATE {" . $table . "} SET $field_new = CAST(" . $field . "_old as " . $typecast . ")"); + $this->connection->query("UPDATE {" . $table . "} SET $field_new = CAST(" . $field . "_old as " . $typecast . ")"); if ($not_null) { - $ret[] = update_sql("ALTER TABLE {" . $table . "} ALTER $field_new SET NOT NULL"); + $this->connection->query("ALTER TABLE {" . $table . "} ALTER $field_new SET NOT NULL"); } - $this->dropField($ret, $table, $field . '_old'); + $this->dropField($table, $field . '_old'); if (isset($new_keys)) { - $this->_createKeys($ret, $table, $new_keys); + $this->_createKeys($table, $new_keys); } } @@ -587,18 +550,18 @@ class DatabaseSchema_pgsql extends DatabaseSchema { return $query; } - protected function _createKeys(&$ret, $table, $new_keys) { + protected function _createKeys($table, $new_keys) { if (isset($new_keys['primary key'])) { - $this->addPrimaryKey($ret, $table, $new_keys['primary key']); + $this->addPrimaryKey($table, $new_keys['primary key']); } if (isset($new_keys['unique keys'])) { foreach ($new_keys['unique keys'] as $name => $fields) { - $this->addUniqueKey($ret, $table, $name, $fields); + $this->addUniqueKey($table, $name, $fields); } } if (isset($new_keys['indexes'])) { foreach ($new_keys['indexes'] as $name => $fields) { - $this->addIndex($ret, $table, $name, $fields); + $this->addIndex($table, $name, $fields); } } } @@ -610,8 +573,8 @@ class DatabaseSchema_pgsql extends DatabaseSchema { $table = $this->connection->prefixTables('{' . $table . '}'); // Don't use {} around pg_class, pg_attribute tables. if (isset($column)) { - return db_query('SELECT col_description(oid, attnum) FROM pg_class, pg_attribute WHERE attrelid = oid AND relname = ? AND attname = ?', array($table, $column))->fetchField(); + $this->connection->query('SELECT col_description(oid, attnum) FROM pg_class, pg_attribute WHERE attrelid = oid AND relname = ? AND attname = ?', array($table, $column))->fetchField(); } - return db_query('SELECT obj_description(oid, ?) FROM pg_class WHERE relname = ?', array('pg_class', $table))->fetchField(); + $this->connection->query('SELECT obj_description(oid, ?) FROM pg_class WHERE relname = ?', array('pg_class', $table))->fetchField(); } } |