Skip to content

Instantly share code, notes, and snippets.

@posulliv
Created May 16, 2012 12:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save posulliv/2710166 to your computer and use it in GitHub Desktop.
Save posulliv/2710166 to your computer and use it in GitHub Desktop.
diff -rupN a/includes/database.inc b/includes/database.inc
--- a/includes/database.inc 2011-05-25 13:43:55.000000000 -0700
+++ b/includes/database.inc 2011-12-20 18:16:01.140029674 -0800
@@ -129,16 +129,17 @@ function db_set_active($name = 'default'
install_goto('install.php');
}
+ // Always set the type of the active database
+ if (is_array($db_url)) {
+ $connect_url = array_key_exists($name, $db_url) ? $db_url[$name] : $db_url['default'];
+ }
+ else {
+ $connect_url = $db_url;
+ }
+
+ $db_type = substr($connect_url, 0, strpos($connect_url, '://'));
if (!isset($db_conns[$name])) {
// Initiate a new connection, using the named DB URL specified.
- if (is_array($db_url)) {
- $connect_url = array_key_exists($name, $db_url) ? $db_url[$name] : $db_url['default'];
- }
- else {
- $connect_url = $db_url;
- }
-
- $db_type = substr($connect_url, 0, strpos($connect_url, '://'));
$handler = "./includes/database.$db_type.inc";
if (is_file($handler)) {
@@ -160,6 +161,43 @@ function db_set_active($name = 'default'
}
/**
+ * Runs a basic query in the active database.
+ *
+ * User-supplied arguments to the query should be passed in as separate
+ * parameters so that they can be properly escaped to avoid SQL injection
+ * attacks.
+ *
+ * @param $query
+ * A string containing an SQL query.
+ * @param ...
+ * A variable number of arguments which are substituted into the query
+ * using printf() syntax. Instead of a variable number of query arguments,
+ * you may also pass a single array containing the query arguments.
+ *
+ * Valid %-modifiers are: %s, %d, %f, %b (binary data, do not enclose
+ * in '') and %%.
+ *
+ * NOTE: using this syntax will cast NULL and FALSE values to decimal 0,
+ * and TRUE values to decimal 1.
+ *
+ * @return
+ * A database query result resource, or FALSE if the query was not
+ * executed correctly.
+ */
+function db_query($query) {
+ $args = func_get_args();
+ array_shift($args);
+ $query = db_prefix_tables($query);
+ if (isset($args[0]) and is_array($args[0])) { // 'All arguments in one array' syntax
+ $args = $args[0];
+ }
+ _db_query_callback($args, TRUE);
+ $query = preg_replace_callback(DB_QUERY_REGEXP, '_db_query_callback', $query);
+ return _db_query($query);
+}
+
+
+/**
* Helper function to show fatal database errors.
*
* Prints a themed maintenance page with the 'Site off-line' text,
@@ -422,6 +460,413 @@ function db_escape_table($string) {
return preg_replace('/[^A-Za-z0-9_]+/', '', $string);
}
+/*
+ * Wrapper functions for database-specific implementations.
+ */
+function db_status_report($phase) {
+ global $db_type;
+ switch ($db_type) {
+ case 'mysql':
+ return db_mysql_status_report($phase);
+ case 'mysqli':
+ return db_mysqli_status_report($phase);
+ case 'pgsql':
+ return db_pgsql_status_report($phase);
+ default:
+ $f = 'db_'. $db_type .'_status_report';
+ return $f($phase);
+ }
+}
+
+function db_version() {
+ global $db_type;
+ switch ($db_type) {
+ case 'mysql':
+ return db_mysql_version();
+ case 'mysqli':
+ return db_mysqli_version();
+ case 'pgsql':
+ return db_pgsql_version();
+ default:
+ $f = 'db_'. $db_type .'_version';
+ return $f();
+ }
+}
+
+function db_connect($url) {
+ global $db_type;
+ switch ($db_type) {
+ case 'mysql':
+ return db_mysql_connect($url);
+ case 'mysqli':
+ return db_mysqli_connect($url);
+ case 'pgsql':
+ return db_pgsql_connect($url);
+ default:
+ $f = 'db_'. $db_type .'_connect';
+ return $f($url);
+ }
+}
+
+function _db_query($query, $debug = 0) {
+ global $db_type;
+ switch ($db_type) {
+ case 'mysql':
+ return _db_mysql_query($query, $debug);
+ case 'mysqli':
+ return _db_mysqli_query($query, $debug);
+ case 'pgsql':
+ return _db_pgsql_query($query, $debug);
+ default:
+ $f = '_db_'. $db_type .'_query';
+ return $f($query, $debug);
+ }
+}
+
+function db_fetch_object($result) {
+ global $db_type;
+ switch ($db_type) {
+ case 'mysql':
+ return db_mysql_fetch_object($result);
+ case 'mysqli':
+ return db_mysqli_fetch_object($result);
+ case 'pgsql':
+ return db_pgsql_fetch_object($result);
+ default:
+ $f = 'db_'. $db_type .'_fetch_object';
+ return $f($result);
+ }
+}
+
+function db_fetch_array($result) {
+ global $db_type;
+ switch ($db_type) {
+ case 'mysql':
+ return db_mysql_fetch_array($result);
+ case 'mysqli':
+ return db_mysqli_fetch_array($result);
+ case 'pgsql':
+ return db_pgsql_fetch_array($result);
+ default:
+ $f = 'db_'. $db_type .'_fetch_array';
+ return $f($result);
+ }
+}
+
+function db_num_rows($result) {
+ global $db_type;
+ switch ($db_type) {
+ case 'mysql':
+ return db_mysql_num_rows($result);
+ case 'mysqli':
+ return db_mysqli_num_rows($result);
+ case 'pgsql':
+ return db_pgsql_num_rows($result);
+ default:
+ $f = 'db_'. $db_type .'_num_rows';
+ return $f($result);
+ }
+}
+
+function db_result($result, $row = 0) {
+ global $db_type;
+ switch ($db_type) {
+ case 'mysql':
+ return db_mysql_result($result, $row);
+ case 'mysqli':
+ return db_mysqli_result($result, $row);
+ case 'pgsql':
+ return db_pgsql_result($result, $row);
+ default:
+ $f = 'db_'. $db_type .'_result';
+ return $f($result, $row);
+ }
+}
+
+function db_error() {
+ global $db_type;
+ switch ($db_type) {
+ case 'mysql':
+ return db_mysql_error();
+ case 'mysqli':
+ return db_mysqli_error();
+ case 'pgsql':
+ return db_pgsql_error();
+ default:
+ $f = 'db_'. $db_type .'_error';
+ return $f();
+ }
+}
+
+function db_next_id($name) {
+ global $db_type;
+ switch ($db_type) {
+ case 'mysql':
+ return db_mysql_next_id($name);
+ case 'mysqli':
+ return db_mysqli_next_id($name);
+ case 'pgsql':
+ return db_pgsql_next_id($name);
+ default:
+ $f = 'db_'. $db_type .'_next_id';
+ return $f($name);
+ }
+}
+
+function db_affected_rows() {
+ global $db_type;
+ switch ($db_type) {
+ case 'mysql':
+ return db_mysql_affected_rows();
+ case 'mysqli':
+ return db_mysqli_affected_rows();
+ case 'pgsql':
+ return db_pgsql_affected_rows();
+ default:
+ $f = 'db_'. $db_type .'_affected_rows';
+ return $f();
+ }
+}
+
+function db_query_range($query) {
+ $args = func_get_args();
+ global $db_type;
+ $args = func_get_args();
+ switch ($db_type) {
+ case 'mysql':
+ return db_mysql_query_range($query, $args);
+ case 'mysqli':
+ return db_mysqli_query_range($query, $args);
+ case 'pgsql':
+ return db_pgsql_query_range($query, $args);
+ default:
+ $f = 'db_'. $db_type .'_query_range';
+ return $f($query, $args);
+ }
+}
+
+function db_query_temporary($query) {
+ $args = func_get_args();
+ global $db_type;
+ $args = func_get_args();
+ switch ($db_type) {
+ case 'mysql':
+ return db_mysql_query_temporary($query, $args);
+ case 'mysqli':
+ return db_mysqli_query_temporary($query, $args);
+ case 'pgsql':
+ return db_pgsql_query_temporary($query, $args);
+ default:
+ $f = 'db_'. $db_type .'_query_temporary';
+ return $f($query, $args);
+ }
+}
+
+function db_encode_blob($data) {
+ global $db_type;
+ switch ($db_type) {
+ case 'mysql':
+ return db_mysql_encode_blob($data);
+ case 'mysqli':
+ return db_mysqli_encode_blob($data);
+ case 'pgsql':
+ return db_pgsql_encode_blob($data);
+ default:
+ $f = 'db_'. $db_type .'_encode_blob';
+ return $f($data);
+ }
+}
+
+function db_decode_blob($data) {
+ global $db_type;
+ switch ($db_type) {
+ case 'mysql':
+ return db_mysql_decode_blob($data);
+ case 'mysqli':
+ return db_mysqli_decode_blob($data);
+ case 'pgsql':
+ return db_pgsql_decode_blob($data);
+ default:
+ $f = 'db_'. $db_type .'_decode_blob';
+ return $f($data);
+ }
+}
+
+function db_escape_string($text) {
+ global $db_type;
+ switch ($db_type) {
+ case 'mysql':
+ return db_mysql_escape_string($text);
+ case 'mysqli':
+ return db_mysqli_escape_string($text);
+ case 'pgsql':
+ return db_pgsql_escape_string($text);
+ default:
+ $f = 'db_'. $db_type .'_escape_string';
+ return $f($text);
+ }
+}
+
+function db_lock_table($table) {
+ global $db_type;
+ switch ($db_type) {
+ case 'mysql':
+ return db_mysql_lock_table($table);
+ case 'mysqli':
+ return db_mysqli_lock_table($table);
+ case 'pgsql':
+ return db_pgsql_lock_table($table);
+ default:
+ $f = 'db_'. $db_type .'_lock_table';
+ return $f($table);
+ }
+}
+
+function db_unlock_tables() {
+ global $db_type;
+ switch ($db_type) {
+ case 'mysql':
+ return db_mysql_unlock_tables();
+ case 'mysqli':
+ return db_mysqli_unlock_tables();
+ case 'pgsql':
+ return db_pgsql_unlock_tables();
+ default:
+ $f = 'db_'. $db_type .'_unlock_tables';
+ return $f();
+ }
+}
+
+function db_table_exists($table) {
+ global $db_type;
+ switch ($db_type) {
+ case 'mysql':
+ return db_mysql_table_exists($table);
+ case 'mysqli':
+ return db_mysqli_table_exists($table);
+ case 'pgsql':
+ return db_pgsql_table_exists($table);
+ default:
+ $f = 'db_'. $db_type .'_table_exists';
+ return $f($table);
+ }
+}
+
+function db_column_exists($table) {
+ global $db_type;
+ switch ($db_type) {
+ case 'mysql':
+ return db_mysql_column_exists($table);
+ case 'mysqli':
+ return db_mysqli_column_exists($table);
+ case 'pgsql':
+ return db_pgsql_column_exists($table);
+ default:
+ $f = 'db_'. $db_type .'_column_exists';
+ return $f($table);
+ }
+}
+
+function db_check_setup() {
+ global $db_type;
+ switch ($db_type) {
+ case 'mysql':
+ return db_mysql_check_setup();
+ case 'mysqli':
+ return db_mysqli_check_setup();
+ case 'pgsql':
+ return db_pgsql_check_setup();
+ default:
+ $f = 'db_'. $db_type .'_check_setup';
+ return $f();
+ }
+}
+
+/**
+ * Returns the last insert id. This function is thread safe.
+ *
+ * @param $table
+ * The name of the table you inserted into.
+ * @param $field
+ * The name of the autoincrement field.
+ */
+function db_last_insert_id($table, $field) {
+ global $db_type;
+ switch ($db_type) {
+ case 'mysql':
+ return db_mysql_last_insert_id($table, $field);
+ case 'mysqli':
+ return db_mysql_last_insert_id($table, $field);
+ case 'pgsql':
+ return db_pgsql_last_insert_id($table, $field);
+ default:
+ $f = 'db_'. $db_type .'_check_setup';
+ return $f();
+ }
+}
+
+function _db_create_key_sql($fields) {
+ global $db_type;
+ switch ($db_type) {
+ case 'mysql':
+ return _db_mysql_create_key_sql($fields);
+ case 'mysqli':
+ return _db_mysql_create_key_sql($fields);
+ case 'pgsql':
+ return _db_pgsql_create_key_sql($fields);
+ default:
+ $f = '_db_'. $db_type .'_check_setup';
+ return $f();
+ }
+}
+
+/**
+ * Set database-engine specific properties for a field.
+ *
+ * @param $field
+ * A field description array, as specified in the schema documentation.
+ */
+function _db_process_field($field) {
+ global $db_type;
+ switch ($db_type) {
+ case 'mysql':
+ return _db_mysql_process_field($field);
+ case 'mysqli':
+ return _db_mysql_process_field($field);
+ case 'pgsql':
+ return _db_pgsql_process_field($field);
+ default:
+ $f = '_db_'. $db_type .'_check_setup';
+ return $f();
+ }
+}
+
+/**
+ * Create an SQL string for a field to be used in table creation or alteration.
+ *
+ * Before passing a field out of a schema definition into this function it has
+ * to be processed by _db_process_field().
+ *
+ * @param $name
+ * Name of the field.
+ * @param $spec
+ * The field specification, as per the schema data structure format.
+ */
+function _db_create_field_sql($name, $spec) {
+ global $db_type;
+ switch ($db_type) {
+ case 'mysql':
+ return _db_mysql_create_field_sql($name, $spec);
+ case 'mysqli':
+ return _db_mysql_create_field_sql($name, $spec);
+ case 'pgsql':
+ return _db_pgsql_create_field_sql($name, $spec);
+ default:
+ $f = '_db_'. $db_type .'_check_setup';
+ return $f();
+ }
+}
+
/**
* @} End of "defgroup database".
*/
@@ -534,6 +979,343 @@ function db_escape_table($string) {
* @see drupal_install_schema()
*/
+/**
+ * This maps a generic data type in combination with its data size
+ * to the engine-specific data type.
+ */
+function db_type_map() {
+ global $db_type;
+ switch ($db_type) {
+ case 'mysql':
+ return db_mysql_type_map();
+ case 'mysqli':
+ return db_mysql_type_map();
+ case 'pgsql':
+ return db_pgsql_type_map();
+ default:
+ $f = 'db_'. $db_type .'_check_setup';
+ return $f();
+ }
+}
+
+/**
+ * Generate SQL to create a new table from a Drupal schema definition.
+ *
+ * @param $name
+ * The name of the table to create.
+ * @param $table
+ * A Schema API table definition array.
+ * @return
+ * An array of SQL statements to create the table.
+ */
+function db_create_table_sql($name, $table) {
+ global $db_type;
+ switch ($db_type) {
+ case 'mysql':
+ return db_mysql_create_table_sql();
+ case 'mysqli':
+ return db_mysql_create_table_sql();
+ case 'pgsql':
+ return db_pgsql_create_table_sql();
+ default:
+ $f = 'db_'. $db_type .'_check_setup';
+ return $f();
+ }
+}
+
+/**
+ * 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 db_rename_table(&$ret, $table, $new_name) {
+ $ret[] = update_sql('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.
+ */
+function db_drop_table(&$ret, $table) {
+ $ret[] = update_sql('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
+ * Name of the field to be added.
+ * @param $spec
+ * The field specification array, as taken from a schema definition.
+ * The specification may also contain the key 'initial', the newly
+ * created field will be set to the value of the key in all rows.
+ * This is most useful for creating NOT NULL columns with no default
+ * value in existing tables.
+ * @param $keys_new
+ * Optional keys and indexes specification to be created on the
+ * table along with adding the field. The format is the same as a
+ * table specification but without the 'fields' element. If you are
+ * adding a type 'serial' field, you MUST specify at least one key
+ * or index including it in this array. @see db_change_field for more
+ * explanation why.
+ */
+function db_add_field(&$ret, $table, $field, $spec, $keys_new = array()) {
+ global $db_type;
+ switch ($db_type) {
+ case 'mysql':
+ return db_mysql_add_field($ret, $table, $field, $spec, $keys_new);
+ case 'mysqli':
+ return db_mysql_add_field($ret, $table, $field, $spec, $keys_new);
+ case 'pgsql':
+ return db_pgsql_add_field($ret, $table, $field, $spec, $keys_new);
+ default:
+ $f = 'db_'. $db_type .'_check_setup';
+ return $f();
+ }
+}
+
+/**
+ * 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.
+ */
+function db_drop_field(&$ret, $table, $field) {
+ global $db_type;
+ switch ($db_type) {
+ case 'mysql':
+ return db_mysql_drop_field($ret, $table, $field);
+ case 'mysqli':
+ return db_mysql_drop_field($ret, $table, $field);
+ case 'pgsql':
+ return db_pgsql_drop_field($ret, $table, $field);
+ default:
+ $f = 'db_'. $db_type .'_drop_field';
+ return $f();
+ }
+}
+
+/**
+ * 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
+ * The field to be altered.
+ * @param $default
+ * Default value to be set. NULL for 'default NULL'.
+ */
+function db_field_set_default(&$ret, $table, $field, $default) {
+ if ($default == NULL) {
+ $default = 'NULL';
+ }
+ else {
+ $default = is_string($default) ? "'$default'" : $default;
+ }
+
+ $ret[] = update_sql('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.
+ */
+function db_field_set_no_default(&$ret, $table, $field) {
+ $ret[] = update_sql('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.
+ */
+function db_add_primary_key(&$ret, $table, $fields) {
+ global $db_type;
+ switch ($db_type) {
+ case 'mysql':
+ return db_mysql_add_primary_key($ret, $table, $fields);
+ case 'mysqli':
+ return db_mysql_add_primary_key($ret, $table, $fields);
+ case 'pgsql':
+ return db_pgsql_add_primary_key($ret, $table, $fields);
+ default:
+ $f = 'db_'. $db_type .'_add_primary_key';
+ return $f();
+ }
+}
+
+/**
+ * Drop the primary key.
+ *
+ * @param $ret
+ * Array to which query results will be added.
+ * @param $table
+ * The table to be altered.
+ */
+function db_drop_primary_key(&$ret, $table) {
+ global $db_type;
+ switch ($db_type) {
+ case 'mysql':
+ return db_mysql_drop_primary_key($ret, $table);
+ case 'mysqli':
+ return db_mysql_drop_primary_key($ret, $table);
+ case 'pgsql':
+ return db_pgsql_drop_primary_key($ret, $table);
+ default:
+ $f = 'db_'. $db_type .'_drop_primary_key';
+ return $f();
+ }
+}
+
+/**
+ * Add 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.
+ * @param $fields
+ * An array of field names.
+ */
+function db_add_unique_key(&$ret, $table, $name, $fields) {
+ global $db_type;
+ switch ($db_type) {
+ case 'mysql':
+ return db_mysql_add_unique_key($ret, $table, $name, $fields);
+ case 'mysqli':
+ return db_mysql_add_unique_key($ret, $table, $name, $fields);
+ case 'pgsql':
+ return db_pgsql_add_unique_key($ret, $table, $name, $fields);
+ default:
+ $f = 'db_'. $db_type .'_add_unique_key';
+ return $f();
+ }
+}
+
+/**
+ * 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.
+ */
+function db_drop_unique_key(&$ret, $table, $name) {
+ global $db_type;
+ switch ($db_type) {
+ case 'mysql':
+ return db_mysql_drop_unique_key($ret, $table, $name);
+ case 'mysqli':
+ return db_mysql_drop_unique_key($ret, $table, $name);
+ case 'pgsql':
+ return db_pgsql_drop_unique_key($ret, $table, $name);
+ default:
+ $f = 'db_'. $db_type .'_drop_unique_key';
+ return $f();
+ }
+}
+
+/**
+ * Add 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.
+ * @param $fields
+ * An array of field names.
+ */
+function db_add_index(&$ret, $table, $name, $fields) {
+ global $db_type;
+ switch ($db_type) {
+ case 'mysql':
+ return db_mysql_add_index($ret, $table, $name, $fields);
+ case 'mysqli':
+ return db_mysql_add_index($ret, $table, $name, $fields);
+ case 'pgsql':
+ return db_pgsql_add_index($ret, $table, $name, $fields);
+ default:
+ $f = 'db_'. $db_type .'_add_index';
+ return $f();
+ }
+}
+
+/**
+ * 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.
+ */
+function db_drop_index(&$ret, $table, $name) {
+ global $db_type;
+ switch ($db_type) {
+ case 'mysql':
+ return db_mysql_drop_index($ret, $table, $name);
+ case 'mysqli':
+ return db_mysql_drop_index($ret, $table, $name);
+ case 'pgsql':
+ return db_pgsql_drop_index($ret, $table, $name);
+ default:
+ $f = 'db_'. $db_type .'_drop_index';
+ return $f();
+ }
+}
+
+function db_change_field(&$ret, $table, $field, $field_new, $spec, $new_keys = array()) {
+ global $db_type;
+ switch ($db_type) {
+ case 'mysql':
+ return db_mysql_change_field($ret, $table, $field, $field_new, $spec, $new_keys);
+ case 'mysqli':
+ return db_mysql_change_field($ret, $table, $field, $field_new, $spec, $new_keys);
+ case 'pgsql':
+ return db_pgsql_change_field($ret, $table, $field, $field_new, $spec, $new_keys);
+ default:
+ $f = 'db_'. $db_type .'_change_field';
+ return $f();
+ }
+}
+
/**
* Create a new table from a Drupal table definition.
*
@@ -620,3 +1402,4 @@ function db_type_placeholder($type) {
/**
* @} End of "defgroup schemaapi".
*/
+
diff -rupN a/includes/database.mysql-common.inc b/includes/database.mysql-common.inc
--- a/includes/database.mysql-common.inc 2011-05-25 13:43:55.000000000 -0700
+++ b/includes/database.mysql-common.inc 2011-12-20 18:15:42.010029683 -0800
@@ -6,42 +6,6 @@
*/
/**
- * Runs a basic query in the active database.
- *
- * User-supplied arguments to the query should be passed in as separate
- * parameters so that they can be properly escaped to avoid SQL injection
- * attacks.
- *
- * @param $query
- * A string containing an SQL query.
- * @param ...
- * A variable number of arguments which are substituted into the query
- * using printf() syntax. Instead of a variable number of query arguments,
- * you may also pass a single array containing the query arguments.
- *
- * Valid %-modifiers are: %s, %d, %f, %b (binary data, do not enclose
- * in '') and %%.
- *
- * NOTE: using this syntax will cast NULL and FALSE values to decimal 0,
- * and TRUE values to decimal 1.
- *
- * @return
- * A database query result resource, or FALSE if the query was not
- * executed correctly.
- */
-function db_query($query) {
- $args = func_get_args();
- array_shift($args);
- $query = db_prefix_tables($query);
- if (isset($args[0]) and is_array($args[0])) { // 'All arguments in one array' syntax
- $args = $args[0];
- }
- _db_query_callback($args, TRUE);
- $query = preg_replace_callback(DB_QUERY_REGEXP, '_db_query_callback', $query);
- return _db_query($query);
-}
-
-/**
* @ingroup schemaapi
* @{
*/
@@ -56,7 +20,7 @@ function db_query($query) {
* @return
* An array of SQL statements to create the table.
*/
-function db_create_table_sql($name, $table) {
+function db_mysql_create_table_sql($name, $table) {
if (empty($table['mysql_suffix'])) {
$table['mysql_suffix'] = '/*!40100 DEFAULT CHARACTER SET utf8';
@@ -112,7 +76,7 @@ function _db_create_keys_sql($spec) {
return $keys;
}
-function _db_create_key_sql($fields) {
+function _db_mysql_create_key_sql($fields) {
$ret = array();
foreach ($fields as $field) {
if (is_array($field)) {
@@ -131,7 +95,7 @@ function _db_create_key_sql($fields) {
* @param $field
* A field description array, as specified in the schema documentation.
*/
-function _db_process_field($field) {
+function _db_mysql_process_field($field) {
if (!isset($field['size'])) {
$field['size'] = 'normal';
@@ -161,7 +125,7 @@ function _db_process_field($field) {
* @param $spec
* The field specification, as per the schema data structure format.
*/
-function _db_create_field_sql($name, $spec) {
+function _db_mysql_create_field_sql($name, $spec) {
$sql = "`". $name ."` ". $spec['mysql_type'];
if (in_array($spec['type'], array('varchar', 'char', 'text')) && isset($spec['length'])) {
@@ -201,7 +165,7 @@ function _db_create_field_sql($name, $sp
* This maps a generic data type in combination with its data size
* to the engine-specific data type.
*/
-function db_type_map() {
+function db_mysql_type_map() {
// Put :normal last so it gets preserved by array_flip. This makes
// it much easier for modules (such as schema.module) to map
// database types back into schema types.
@@ -244,32 +208,6 @@ function db_type_map() {
}
/**
- * 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 db_rename_table(&$ret, $table, $new_name) {
- $ret[] = update_sql('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.
- */
-function db_drop_table(&$ret, $table) {
- $ret[] = update_sql('DROP TABLE {'. $table .'}');
-}
-
-/**
* Add a new field to a table.
*
* @param $ret
@@ -292,7 +230,7 @@ function db_drop_table(&$ret, $table) {
* or index including it in this array. See db_change_field() for more
* explanation why.
*/
-function db_add_field(&$ret, $table, $field, $spec, $keys_new = array()) {
+function db_mysql_add_field(&$ret, $table, $field, $spec, $keys_new = array()) {
$fixnull = FALSE;
if (!empty($spec['not null']) && !isset($spec['default'])) {
$fixnull = TRUE;
@@ -326,48 +264,11 @@ function db_add_field(&$ret, $table, $fi
* @param $field
* The field to be dropped.
*/
-function db_drop_field(&$ret, $table, $field) {
+function db_mysql_drop_field(&$ret, $table, $field) {
$ret[] = update_sql('ALTER TABLE {'. $table .'} DROP '. $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
- * The field to be altered.
- * @param $default
- * Default value to be set. NULL for 'default NULL'.
- */
-function db_field_set_default(&$ret, $table, $field, $default) {
- if ($default === NULL) {
- $default = 'NULL';
- }
- else {
- $default = is_string($default) ? "'$default'" : $default;
- }
-
- $ret[] = update_sql('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.
- */
-function db_field_set_no_default(&$ret, $table, $field) {
- $ret[] = update_sql('ALTER TABLE {'. $table .'} ALTER COLUMN '. $field .' DROP DEFAULT');
-}
-
-/**
* Add a primary key.
*
* @param $ret
@@ -377,7 +278,7 @@ function db_field_set_no_default(&$ret,
* @param $fields
* Fields for the primary key.
*/
-function db_add_primary_key(&$ret, $table, $fields) {
+function db_mysql_add_primary_key(&$ret, $table, $fields) {
$ret[] = update_sql('ALTER TABLE {'. $table .'} ADD PRIMARY KEY ('.
_db_create_key_sql($fields) .')');
}
@@ -390,7 +291,7 @@ function db_add_primary_key(&$ret, $tabl
* @param $table
* The table to be altered.
*/
-function db_drop_primary_key(&$ret, $table) {
+function db_mysql_drop_primary_key(&$ret, $table) {
$ret[] = update_sql('ALTER TABLE {'. $table .'} DROP PRIMARY KEY');
}
@@ -406,7 +307,7 @@ function db_drop_primary_key(&$ret, $tab
* @param $fields
* An array of field names.
*/
-function db_add_unique_key(&$ret, $table, $name, $fields) {
+function db_mysql_add_unique_key(&$ret, $table, $name, $fields) {
$ret[] = update_sql('ALTER TABLE {'. $table .'} ADD UNIQUE KEY '.
$name .' ('. _db_create_key_sql($fields) .')');
}
@@ -421,7 +322,7 @@ function db_add_unique_key(&$ret, $table
* @param $name
* The name of the key.
*/
-function db_drop_unique_key(&$ret, $table, $name) {
+function db_mysql_drop_unique_key(&$ret, $table, $name) {
$ret[] = update_sql('ALTER TABLE {'. $table .'} DROP KEY '. $name);
}
@@ -437,7 +338,7 @@ function db_drop_unique_key(&$ret, $tabl
* @param $fields
* An array of field names.
*/
-function db_add_index(&$ret, $table, $name, $fields) {
+function db_mysql_add_index(&$ret, $table, $name, $fields) {
$query = 'ALTER TABLE {'. $table .'} ADD INDEX '. $name .' ('. _db_create_key_sql($fields) .')';
$ret[] = update_sql($query);
}
@@ -452,7 +353,7 @@ function db_add_index(&$ret, $table, $na
* @param $name
* The name of the index.
*/
-function db_drop_index(&$ret, $table, $name) {
+function db_mysql_drop_index(&$ret, $table, $name) {
$ret[] = update_sql('ALTER TABLE {'. $table .'} DROP INDEX '. $name);
}
@@ -519,7 +420,7 @@ function db_drop_index(&$ret, $table, $n
* table specification but without the 'fields' element.
*/
-function db_change_field(&$ret, $table, $field, $field_new, $spec, $keys_new = array()) {
+function db_mysql_change_field(&$ret, $table, $field, $field_new, $spec, $keys_new = array()) {
$sql = 'ALTER TABLE {'. $table .'} CHANGE `'. $field .'` '.
_db_create_field_sql($field_new, _db_process_field($spec));
if (count($keys_new)) {
@@ -536,6 +437,6 @@ function db_change_field(&$ret, $table,
* @param $field
* The name of the autoincrement field.
*/
-function db_last_insert_id($table, $field) {
+function db_mysql_last_insert_id($table, $field) {
return db_result(db_query('SELECT LAST_INSERT_ID()'));
}
diff -rupN a/includes/database.mysqli.inc b/includes/database.mysqli.inc
--- a/includes/database.mysqli.inc 2011-05-25 13:43:55.000000000 -0700
+++ b/includes/database.mysqli.inc 2011-12-20 18:15:42.010029683 -0800
@@ -17,9 +17,15 @@
require_once './includes/database.mysql-common.inc';
/**
+ * Verify if the database is set up correctly.
+ */
+function db_mysqli_check_setup() {
+}
+
+/**
* Report database status.
*/
-function db_status_report($phase) {
+function db_mysqli_status_report($phase) {
$t = get_t();
$version = db_version();
@@ -42,7 +48,7 @@ function db_status_report($phase) {
*
* @return Database server version
*/
-function db_version() {
+function db_mysqli_version() {
global $active_db;
list($version) = explode('-', mysqli_get_server_info($active_db));
return $version;
@@ -53,7 +59,7 @@ function db_version() {
*
* Note that mysqli does not support persistent connections.
*/
-function db_connect($url) {
+function db_mysqli_connect($url) {
// Check if MySQLi support is present in PHP
if (!function_exists('mysqli_init') && !extension_loaded('mysqli')) {
_db_error_page('Unable to use the MySQLi database because the MySQLi extension for PHP is not installed. Check your <code>php.ini</code> to see how you can enable it.');
@@ -94,7 +100,7 @@ function db_connect($url) {
/**
* Helper function for db_query().
*/
-function _db_query($query, $debug = 0) {
+function _db_mysqli_query($query, $debug = 0) {
global $active_db, $queries, $user;
if (variable_get('dev_query', 0)) {
@@ -145,7 +151,7 @@ function _db_query($query, $debug = 0) {
* An object representing the next row of the result, or FALSE. The attributes
* of this object are the table fields selected by the query.
*/
-function db_fetch_object($result) {
+function db_mysqli_fetch_object($result) {
if ($result) {
$object = mysqli_fetch_object($result);
return isset($object) ? $object : FALSE;
@@ -162,7 +168,7 @@ function db_fetch_object($result) {
* The keys of this object are the names of the table fields selected by the
* query, and the values are the field values for this result row.
*/
-function db_fetch_array($result) {
+function db_mysqli_fetch_array($result) {
if ($result) {
$array = mysqli_fetch_array($result, MYSQLI_ASSOC);
return isset($array) ? $array : FALSE;
@@ -180,7 +186,7 @@ function db_fetch_array($result) {
* @return
* The resulting field or FALSE.
*/
-function db_result($result) {
+function db_mysqli_result($result) {
if ($result && mysqli_num_rows($result) > 0) {
// The mysqli_fetch_row function has an optional second parameter $row
// but that can't be used for compatibility with Oracle, DB2, etc.
@@ -193,7 +199,7 @@ function db_result($result) {
/**
* Determine whether the previous query caused an error.
*/
-function db_error() {
+function db_mysqli_error() {
global $active_db;
return mysqli_errno($active_db);
}
@@ -201,7 +207,7 @@ function db_error() {
/**
* Determine the number of rows changed by the preceding query.
*/
-function db_affected_rows() {
+function db_mysqli_affected_rows() {
global $active_db; /* mysqli connection resource */
return mysqli_affected_rows($active_db);
}
@@ -234,8 +240,7 @@ function db_affected_rows() {
* A database query result resource, or FALSE if the query was not executed
* correctly.
*/
-function db_query_range($query) {
- $args = func_get_args();
+function db_mysqli_query_range($query, $args) {
$count = array_pop($args);
$from = array_pop($args);
array_shift($args);
@@ -282,8 +287,7 @@ function db_query_range($query) {
* A database query result resource, or FALSE if the query was not executed
* correctly.
*/
-function db_query_temporary($query) {
- $args = func_get_args();
+function db_mysqli_query_temporary($query, $args) {
$tablename = array_pop($args);
array_shift($args);
@@ -304,7 +308,7 @@ function db_query_temporary($query) {
* @return
* Encoded data.
*/
-function db_encode_blob($data) {
+function db_mysqli_encode_blob($data) {
global $active_db;
return "'". mysqli_real_escape_string($active_db, $data) ."'";
}
@@ -317,14 +321,14 @@ function db_encode_blob($data) {
* @return
* Decoded data.
*/
-function db_decode_blob($data) {
+function db_mysqli_decode_blob($data) {
return $data;
}
/**
* Prepare user input for use in a database query, preventing SQL injection attacks.
*/
-function db_escape_string($text) {
+function db_mysqli_escape_string($text) {
global $active_db;
return mysqli_real_escape_string($active_db, $text);
}
@@ -332,14 +336,14 @@ function db_escape_string($text) {
/**
* Lock a table.
*/
-function db_lock_table($table) {
+function db_mysqli_lock_table($table) {
db_query('LOCK TABLES {'. db_escape_table($table) .'} WRITE');
}
/**
* Unlock all locked tables.
*/
-function db_unlock_tables() {
+function db_mysqli_unlock_tables() {
db_query('UNLOCK TABLES');
}
@@ -352,7 +356,7 @@ function db_unlock_tables() {
* @return
* TRUE if the table exists, and FALSE if the table does not exist.
*/
-function db_table_exists($table) {
+function db_mysqli_table_exists($table) {
return (bool) db_fetch_object(db_query("SHOW TABLES LIKE '{". db_escape_table($table) ."}'"));
}
@@ -367,7 +371,7 @@ function db_table_exists($table) {
* @return
* TRUE if the column exists, and FALSE if the column does not exist.
*/
-function db_column_exists($table, $column) {
+function db_mysqli_column_exists($table, $column) {
return (bool) db_fetch_object(db_query("SHOW COLUMNS FROM {". db_escape_table($table) ."} LIKE '". db_escape_table($column) ."'"));
}
diff -rupN a/includes/database.mysql.inc b/includes/database.mysql.inc
--- a/includes/database.mysql.inc 2011-05-25 13:43:55.000000000 -0700
+++ b/includes/database.mysql.inc 2011-12-20 18:15:42.010029683 -0800
@@ -16,7 +16,7 @@ require_once './includes/database.mysql-
/**
* Report database status.
*/
-function db_status_report($phase) {
+function db_mysql_status_report($phase) {
$t = get_t();
$version = db_version();
@@ -39,7 +39,7 @@ function db_status_report($phase) {
*
* @return Database server version
*/
-function db_version() {
+function db_mysql_version() {
list($version) = explode('-', mysql_get_server_info());
return $version;
}
@@ -47,7 +47,7 @@ function db_version() {
/**
* Initialize a database connection.
*/
-function db_connect($url) {
+function db_mysql_connect($url) {
$url = parse_url($url);
// Check if MySQL support is present in PHP
@@ -95,7 +95,7 @@ function db_connect($url) {
/**
* Helper function for db_query().
*/
-function _db_query($query, $debug = 0) {
+function _db_mysql_query($query, $debug = 0) {
global $active_db, $queries, $user;
if (variable_get('dev_query', 0)) {
@@ -146,7 +146,7 @@ function _db_query($query, $debug = 0) {
* An object representing the next row of the result, or FALSE. The attributes
* of this object are the table fields selected by the query.
*/
-function db_fetch_object($result) {
+function db_mysql_fetch_object($result) {
if ($result) {
return mysql_fetch_object($result);
}
@@ -162,7 +162,7 @@ function db_fetch_object($result) {
* The keys of this object are the names of the table fields selected by the
* query, and the values are the field values for this result row.
*/
-function db_fetch_array($result) {
+function db_mysql_fetch_array($result) {
if ($result) {
return mysql_fetch_array($result, MYSQL_ASSOC);
}
@@ -180,7 +180,7 @@ function db_fetch_array($result) {
* @return
* The resulting field or FALSE.
*/
-function db_result($result) {
+function db_mysql_result($result) {
if ($result && mysql_num_rows($result) > 0) {
// The mysql_fetch_row function has an optional second parameter $row
// but that can't be used for compatibility with Oracle, DB2, etc.
@@ -193,7 +193,7 @@ function db_result($result) {
/**
* Determine whether the previous query caused an error.
*/
-function db_error() {
+function db_mysql_error() {
global $active_db;
return mysql_errno($active_db);
}
@@ -201,7 +201,7 @@ function db_error() {
/**
* Determine the number of rows changed by the preceding query.
*/
-function db_affected_rows() {
+function db_mysql_affected_rows() {
global $active_db;
return mysql_affected_rows($active_db);
}
@@ -234,8 +234,7 @@ function db_affected_rows() {
* A database query result resource, or FALSE if the query was not executed
* correctly.
*/
-function db_query_range($query) {
- $args = func_get_args();
+function db_mysql_query_range($query, $args) {
$count = array_pop($args);
$from = array_pop($args);
array_shift($args);
@@ -282,8 +281,7 @@ function db_query_range($query) {
* A database query result resource, or FALSE if the query was not executed
* correctly.
*/
-function db_query_temporary($query) {
- $args = func_get_args();
+function db_mysql_query_temporary($query, $args) {
$tablename = array_pop($args);
array_shift($args);
@@ -304,7 +302,7 @@ function db_query_temporary($query) {
* @return
* Encoded data.
*/
-function db_encode_blob($data) {
+function db_mysql_encode_blob($data) {
global $active_db;
return "'". mysql_real_escape_string($data, $active_db) ."'";
}
@@ -317,14 +315,14 @@ function db_encode_blob($data) {
* @return
* Decoded data.
*/
-function db_decode_blob($data) {
+function db_mysql_decode_blob($data) {
return $data;
}
/**
* Prepare user input for use in a database query, preventing SQL injection attacks.
*/
-function db_escape_string($text) {
+function db_mysql_escape_string($text) {
global $active_db;
return mysql_real_escape_string($text, $active_db);
}
@@ -332,14 +330,14 @@ function db_escape_string($text) {
/**
* Lock a table.
*/
-function db_lock_table($table) {
+function db_mysql_lock_table($table) {
db_query('LOCK TABLES {'. db_escape_table($table) .'} WRITE');
}
/**
* Unlock all locked tables.
*/
-function db_unlock_tables() {
+function db_mysql_unlock_tables() {
db_query('UNLOCK TABLES');
}
@@ -352,7 +350,7 @@ function db_unlock_tables() {
* @return
* TRUE if the table exists, and FALSE if the table does not exist.
*/
-function db_table_exists($table) {
+function db_mysql_table_exists($table) {
return (bool) db_fetch_object(db_query("SHOW TABLES LIKE '{". db_escape_table($table) ."}'"));
}
@@ -367,7 +365,7 @@ function db_table_exists($table) {
* @return
* TRUE if the column exists, and FALSE if the column does not exist.
*/
-function db_column_exists($table, $column) {
+function db_mysql_column_exists($table, $column) {
return (bool) db_fetch_object(db_query("SHOW COLUMNS FROM {". db_escape_table($table) ."} LIKE '". db_escape_table($column) ."'"));
}
diff -rupN a/includes/database.pgsql.inc b/includes/database.pgsql.inc
--- a/includes/database.pgsql.inc 2011-05-25 13:43:55.000000000 -0700
+++ b/includes/database.pgsql.inc 2011-12-20 18:15:42.010029683 -0800
@@ -13,14 +13,14 @@
/**
* Report database status.
*/
-function db_status_report() {
+function db_pgsql_status_report($phase) {
$t = get_t();
$version = db_version();
$form['pgsql'] = array(
'title' => $t('PostgreSQL database'),
- 'value' => $version,
+ 'value' => ($phase == 'runtime') ? l($version, 'admin/logs/status/sql') : $version,
);
if (version_compare($version, DRUPAL_MINIMUM_PGSQL) < 0) {
@@ -36,14 +36,14 @@ function db_status_report() {
*
* @return Database server version
*/
-function db_version() {
+function db_pgsql_version() {
return db_result(db_query("SHOW SERVER_VERSION"));
}
/**
* Initialize a database connection.
*/
-function db_connect($url) {
+function db_pgsql_connect($url) {
// Check if PostgreSQL support is present in PHP
if (!function_exists('pg_connect')) {
_db_error_page('Unable to use the PostgreSQL database because the PostgreSQL extension for PHP is not installed. Check your <code>php.ini</code> to see how you can enable it.');
@@ -84,50 +84,14 @@ function db_connect($url) {
// Restore error tracking setting
ini_set('track_errors', $track_errors_previous);
- pg_query($connection, "set client_encoding=\"UTF8\"");
+ //pg_query($connection, "set client_encoding=\"UTF8\"");
return $connection;
}
/**
- * Runs a basic query in the active database.
- *
- * User-supplied arguments to the query should be passed in as separate
- * parameters so that they can be properly escaped to avoid SQL injection
- * attacks.
- *
- * @param $query
- * A string containing an SQL query.
- * @param ...
- * A variable number of arguments which are substituted into the query
- * using printf() syntax. Instead of a variable number of query arguments,
- * you may also pass a single array containing the query arguments.
- *
- * Valid %-modifiers are: %s, %d, %f, %b (binary data, do not enclose
- * in '') and %%.
- *
- * NOTE: using this syntax will cast NULL and FALSE values to decimal 0,
- * and TRUE values to decimal 1.
- *
- * @return
- * A database query result resource, or FALSE if the query was not
- * executed correctly.
- */
-function db_query($query) {
- $args = func_get_args();
- array_shift($args);
- $query = db_prefix_tables($query);
- if (isset($args[0]) and is_array($args[0])) { // 'All arguments in one array' syntax
- $args = $args[0];
- }
- _db_query_callback($args, TRUE);
- $query = preg_replace_callback(DB_QUERY_REGEXP, '_db_query_callback', $query);
- return _db_query($query);
-}
-
-/**
* Helper function for db_query().
*/
-function _db_query($query, $debug = 0) {
+function _db_pgsql_query($query, $debug = 0) {
global $active_db, $last_result, $queries;
if (variable_get('dev_query', 0)) {
@@ -170,7 +134,7 @@ function _db_query($query, $debug = 0) {
* An object representing the next row of the result, or FALSE. The attributes
* of this object are the table fields selected by the query.
*/
-function db_fetch_object($result) {
+function db_pgsql_fetch_object($result) {
if ($result) {
return pg_fetch_object($result);
}
@@ -186,7 +150,7 @@ function db_fetch_object($result) {
* The keys of this object are the names of the table fields selected by the
* query, and the values are the field values for this result row.
*/
-function db_fetch_array($result) {
+function db_pgsql_fetch_array($result) {
if ($result) {
return pg_fetch_assoc($result);
}
@@ -203,7 +167,7 @@ function db_fetch_array($result) {
* @return
* The resulting field or FALSE.
*/
-function db_result($result) {
+function db_pgsql_result($result) {
if ($result && pg_num_rows($result) > 0) {
$array = pg_fetch_row($result);
return $array[0];
@@ -214,7 +178,7 @@ function db_result($result) {
/**
* Determine whether the previous query caused an error.
*/
-function db_error() {
+function db_pgsql_error() {
global $active_db;
return pg_last_error($active_db);
}
@@ -227,14 +191,14 @@ function db_error() {
* @param $field
* The name of the autoincrement field.
*/
-function db_last_insert_id($table, $field) {
+function db_pgsql_last_insert_id($table, $field) {
return db_result(db_query("SELECT CURRVAL('{". db_escape_table($table) ."}_". db_escape_table($field) ."_seq')"));
}
/**
* Determine the number of rows changed by the preceding query.
*/
-function db_affected_rows() {
+function db_pgsql_affected_rows() {
global $last_result;
return empty($last_result) ? 0 : pg_affected_rows($last_result);
}
@@ -268,8 +232,7 @@ function db_affected_rows() {
* A database query result resource, or FALSE if the query was not executed
* correctly.
*/
-function db_query_range($query) {
- $args = func_get_args();
+function db_pgsql_query_range($query, $args) {
$count = array_pop($args);
$from = array_pop($args);
array_shift($args);
@@ -316,8 +279,7 @@ function db_query_range($query) {
* A database query result resource, or FALSE if the query was not executed
* correctly.
*/
-function db_query_temporary($query) {
- $args = func_get_args();
+function db_pgsql_query_temporary($query, $args) {
$tablename = array_pop($args);
array_shift($args);
@@ -339,7 +301,7 @@ function db_query_temporary($query) {
* @return
* Encoded data.
*/
-function db_encode_blob($data) {
+function db_pgsql_encode_blob($data) {
return "'". pg_escape_bytea($data) ."'";
}
@@ -352,7 +314,7 @@ function db_encode_blob($data) {
* @return
* Decoded data.
*/
-function db_decode_blob($data) {
+function db_pgsql_decode_blob($data) {
return pg_unescape_bytea($data);
}
@@ -360,7 +322,7 @@ function db_decode_blob($data) {
* Prepare user input for use in a database query, preventing SQL injection attacks.
* Note: This function requires PostgreSQL 7.2 or later.
*/
-function db_escape_string($text) {
+function db_pgsql_escape_string($text) {
return pg_escape_string($text);
}
@@ -368,7 +330,7 @@ function db_escape_string($text) {
* Lock a table.
* This function automatically starts a transaction.
*/
-function db_lock_table($table) {
+function db_pgsql_lock_table($table) {
db_query('BEGIN; LOCK TABLE {'. db_escape_table($table) .'} IN EXCLUSIVE MODE');
}
@@ -376,7 +338,7 @@ function db_lock_table($table) {
* Unlock all locked tables.
* This function automatically commits a transaction.
*/
-function db_unlock_tables() {
+function db_pgsql_unlock_tables() {
db_query('COMMIT');
}
@@ -389,7 +351,7 @@ function db_unlock_tables() {
* @return
* TRUE if the table exists, and FALSE if the table does not exist.
*/
-function db_table_exists($table) {
+function db_pgsql_table_exists($table) {
return (bool) db_result(db_query("SELECT COUNT(*) FROM pg_class WHERE relname = '{". db_escape_table($table) ."}'"));
}
@@ -404,14 +366,14 @@ function db_table_exists($table) {
* @return
* TRUE if the column exists, and FALSE if the column does not exist.
*/
-function db_column_exists($table, $column) {
+function db_pgsql_column_exists($table, $column) {
return (bool) db_result(db_query("SELECT COUNT(pg_attribute.attname) FROM pg_class, pg_attribute WHERE pg_attribute.attrelid = pg_class.oid AND pg_class.relname = '{". db_escape_table($table) ."}' AND attname = '". db_escape_table($column) ."'"));
}
/**
* Verify if the database is set up correctly.
*/
-function db_check_setup() {
+function db_pgsql_check_setup() {
$t = get_t();
$encoding = db_result(db_query('SHOW server_encoding'));
@@ -433,7 +395,7 @@ function db_check_setup() {
* This maps a generic data type in combination with its data size
* to the engine-specific data type.
*/
-function db_type_map() {
+function db_pgsql_type_map() {
// Put :normal last so it gets preserved by array_flip. This makes
// it much easier for modules (such as schema.module) to map
// database types back into schema types.
@@ -485,7 +447,7 @@ function db_type_map() {
* @return
* An array of SQL statements to create the table.
*/
-function db_create_table_sql($name, $table) {
+function db_pgsql_create_table_sql($name, $table) {
$sql_fields = array();
foreach ($table['fields'] as $field_name => $field) {
$sql_fields[] = _db_create_field_sql($field_name, _db_process_field($field));
@@ -525,7 +487,7 @@ function _db_create_index_sql($table, $n
return $query;
}
-function _db_create_key_sql($fields) {
+function _db_pgsql_create_key_sql($fields) {
$ret = array();
foreach ($fields as $field) {
if (is_array($field)) {
@@ -560,7 +522,7 @@ function _db_create_keys(&$ret, $table,
* @param $field
* A field description array, as specified in the schema documentation.
*/
-function _db_process_field($field) {
+function _db_pgsql_process_field($field) {
if (!isset($field['size'])) {
$field['size'] = 'normal';
}
@@ -586,7 +548,7 @@ function _db_process_field($field) {
* @param $spec
* The field specification, as per the schema data structure format.
*/
-function _db_create_field_sql($name, $spec) {
+function _db_pgsql_create_field_sql($name, $spec) {
$sql = $name .' '. $spec['pgsql_type'];
if ($spec['type'] == 'serial') {
@@ -616,32 +578,6 @@ function _db_create_field_sql($name, $sp
}
/**
- * 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 db_rename_table(&$ret, $table, $new_name) {
- $ret[] = update_sql('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.
- */
-function db_drop_table(&$ret, $table) {
- $ret[] = update_sql('DROP TABLE {'. $table .'}');
-}
-
-/**
* Add a new field to a table.
*
* @param $ret
@@ -664,7 +600,7 @@ function db_drop_table(&$ret, $table) {
* or index including it in this array. See db_change_field() for more
* explanation why.
*/
-function db_add_field(&$ret, $table, $field, $spec, $new_keys = array()) {
+function db_pgsql_add_field(&$ret, $table, $field, $spec, $new_keys = array()) {
$fixnull = FALSE;
if (!empty($spec['not null']) && !isset($spec['default'])) {
$fixnull = TRUE;
@@ -697,48 +633,11 @@ function db_add_field(&$ret, $table, $fi
* @param $field
* The field to be dropped.
*/
-function db_drop_field(&$ret, $table, $field) {
+function db_pgsql_drop_field(&$ret, $table, $field) {
$ret[] = update_sql('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
- * The field to be altered.
- * @param $default
- * Default value to be set. NULL for 'default NULL'.
- */
-function db_field_set_default(&$ret, $table, $field, $default) {
- if ($default == NULL) {
- $default = 'NULL';
- }
- else {
- $default = is_string($default) ? "'$default'" : $default;
- }
-
- $ret[] = update_sql('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.
- */
-function db_field_set_no_default(&$ret, $table, $field) {
- $ret[] = update_sql('ALTER TABLE {'. $table .'} ALTER COLUMN '. $field .' DROP DEFAULT');
-}
-
-/**
* Add a primary key.
*
* @param $ret
@@ -748,7 +647,7 @@ function db_field_set_no_default(&$ret,
* @param $fields
* Fields for the primary key.
*/
-function db_add_primary_key(&$ret, $table, $fields) {
+function db_pgsql_add_primary_key(&$ret, $table, $fields) {
$ret[] = update_sql('ALTER TABLE {'. $table .'} ADD PRIMARY KEY ('.
implode(',', $fields) .')');
}
@@ -761,7 +660,7 @@ function db_add_primary_key(&$ret, $tabl
* @param $table
* The table to be altered.
*/
-function db_drop_primary_key(&$ret, $table) {
+function db_pgsql_drop_primary_key(&$ret, $table) {
$ret[] = update_sql('ALTER TABLE {'. $table .'} DROP CONSTRAINT {'. $table .'}_pkey');
}
@@ -777,7 +676,7 @@ function db_drop_primary_key(&$ret, $tab
* @param $fields
* An array of field names.
*/
-function db_add_unique_key(&$ret, $table, $name, $fields) {
+function db_pgsql_add_unique_key(&$ret, $table, $name, $fields) {
$name = '{'. $table .'}_'. $name .'_key';
$ret[] = update_sql('ALTER TABLE {'. $table .'} ADD CONSTRAINT '.
$name .' UNIQUE ('. implode(',', $fields) .')');
@@ -793,7 +692,7 @@ function db_add_unique_key(&$ret, $table
* @param $name
* The name of the key.
*/
-function db_drop_unique_key(&$ret, $table, $name) {
+function db_pgsql_drop_unique_key(&$ret, $table, $name) {
$name = '{'. $table .'}_'. $name .'_key';
$ret[] = update_sql('ALTER TABLE {'. $table .'} DROP CONSTRAINT '. $name);
}
@@ -810,7 +709,7 @@ function db_drop_unique_key(&$ret, $tabl
* @param $fields
* An array of field names.
*/
-function db_add_index(&$ret, $table, $name, $fields) {
+function db_pgsql_add_index(&$ret, $table, $name, $fields) {
$ret[] = update_sql(_db_create_index_sql($table, $name, $fields));
}
@@ -824,7 +723,7 @@ function db_add_index(&$ret, $table, $na
* @param $name
* The name of the index.
*/
-function db_drop_index(&$ret, $table, $name) {
+function db_pgsql_drop_index(&$ret, $table, $name) {
$name = '{'. $table .'}_'. $name .'_idx';
$ret[] = update_sql('DROP INDEX '. $name);
}
@@ -891,7 +790,7 @@ function db_drop_index(&$ret, $table, $n
* table along with changing the field. The format is the same as a
* table specification but without the 'fields' element.
*/
-function db_change_field(&$ret, $table, $field, $field_new, $spec, $new_keys = array()) {
+function db_pgsql_change_field(&$ret, $table, $field, $field_new, $spec, $new_keys = array()) {
$ret[] = update_sql('ALTER TABLE {'. $table .'} RENAME "'. $field .'" TO "'. $field .'_old"');
$not_null = isset($spec['not null']) ? $spec['not null'] : FALSE;
unset($spec['not null']);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment