· 7 years ago · Oct 10, 2018, 12:06 PM
1diff -rupN a/includes/database.inc b/includes/database.inc
2--- a/includes/database.inc 2011-05-25 13:43:55.000000000 -0700
3+++ b/includes/database.inc 2011-12-20 18:16:01.140029674 -0800
4@@ -129,16 +129,17 @@ function db_set_active($name = 'default'
5 install_goto('install.php');
6 }
7
8+ // Always set the type of the active database
9+ if (is_array($db_url)) {
10+ $connect_url = array_key_exists($name, $db_url) ? $db_url[$name] : $db_url['default'];
11+ }
12+ else {
13+ $connect_url = $db_url;
14+ }
15+
16+ $db_type = substr($connect_url, 0, strpos($connect_url, '://'));
17 if (!isset($db_conns[$name])) {
18 // Initiate a new connection, using the named DB URL specified.
19- if (is_array($db_url)) {
20- $connect_url = array_key_exists($name, $db_url) ? $db_url[$name] : $db_url['default'];
21- }
22- else {
23- $connect_url = $db_url;
24- }
25-
26- $db_type = substr($connect_url, 0, strpos($connect_url, '://'));
27 $handler = "./includes/database.$db_type.inc";
28
29 if (is_file($handler)) {
30@@ -160,6 +161,43 @@ function db_set_active($name = 'default'
31 }
32
33 /**
34+ * Runs a basic query in the active database.
35+ *
36+ * User-supplied arguments to the query should be passed in as separate
37+ * parameters so that they can be properly escaped to avoid SQL injection
38+ * attacks.
39+ *
40+ * @param $query
41+ * A string containing an SQL query.
42+ * @param ...
43+ * A variable number of arguments which are substituted into the query
44+ * using printf() syntax. Instead of a variable number of query arguments,
45+ * you may also pass a single array containing the query arguments.
46+ *
47+ * Valid %-modifiers are: %s, %d, %f, %b (binary data, do not enclose
48+ * in '') and %%.
49+ *
50+ * NOTE: using this syntax will cast NULL and FALSE values to decimal 0,
51+ * and TRUE values to decimal 1.
52+ *
53+ * @return
54+ * A database query result resource, or FALSE if the query was not
55+ * executed correctly.
56+ */
57+function db_query($query) {
58+ $args = func_get_args();
59+ array_shift($args);
60+ $query = db_prefix_tables($query);
61+ if (isset($args[0]) and is_array($args[0])) { // 'All arguments in one array' syntax
62+ $args = $args[0];
63+ }
64+ _db_query_callback($args, TRUE);
65+ $query = preg_replace_callback(DB_QUERY_REGEXP, '_db_query_callback', $query);
66+ return _db_query($query);
67+}
68+
69+
70+/**
71 * Helper function to show fatal database errors.
72 *
73 * Prints a themed maintenance page with the 'Site off-line' text,
74@@ -422,6 +460,413 @@ function db_escape_table($string) {
75 return preg_replace('/[^A-Za-z0-9_]+/', '', $string);
76 }
77
78+/*
79+ * Wrapper functions for database-specific implementations.
80+ */
81+function db_status_report($phase) {
82+ global $db_type;
83+ switch ($db_type) {
84+ case 'mysql':
85+ return db_mysql_status_report($phase);
86+ case 'mysqli':
87+ return db_mysqli_status_report($phase);
88+ case 'pgsql':
89+ return db_pgsql_status_report($phase);
90+ default:
91+ $f = 'db_'. $db_type .'_status_report';
92+ return $f($phase);
93+ }
94+}
95+
96+function db_version() {
97+ global $db_type;
98+ switch ($db_type) {
99+ case 'mysql':
100+ return db_mysql_version();
101+ case 'mysqli':
102+ return db_mysqli_version();
103+ case 'pgsql':
104+ return db_pgsql_version();
105+ default:
106+ $f = 'db_'. $db_type .'_version';
107+ return $f();
108+ }
109+}
110+
111+function db_connect($url) {
112+ global $db_type;
113+ switch ($db_type) {
114+ case 'mysql':
115+ return db_mysql_connect($url);
116+ case 'mysqli':
117+ return db_mysqli_connect($url);
118+ case 'pgsql':
119+ return db_pgsql_connect($url);
120+ default:
121+ $f = 'db_'. $db_type .'_connect';
122+ return $f($url);
123+ }
124+}
125+
126+function _db_query($query, $debug = 0) {
127+ global $db_type;
128+ switch ($db_type) {
129+ case 'mysql':
130+ return _db_mysql_query($query, $debug);
131+ case 'mysqli':
132+ return _db_mysqli_query($query, $debug);
133+ case 'pgsql':
134+ return _db_pgsql_query($query, $debug);
135+ default:
136+ $f = '_db_'. $db_type .'_query';
137+ return $f($query, $debug);
138+ }
139+}
140+
141+function db_fetch_object($result) {
142+ global $db_type;
143+ switch ($db_type) {
144+ case 'mysql':
145+ return db_mysql_fetch_object($result);
146+ case 'mysqli':
147+ return db_mysqli_fetch_object($result);
148+ case 'pgsql':
149+ return db_pgsql_fetch_object($result);
150+ default:
151+ $f = 'db_'. $db_type .'_fetch_object';
152+ return $f($result);
153+ }
154+}
155+
156+function db_fetch_array($result) {
157+ global $db_type;
158+ switch ($db_type) {
159+ case 'mysql':
160+ return db_mysql_fetch_array($result);
161+ case 'mysqli':
162+ return db_mysqli_fetch_array($result);
163+ case 'pgsql':
164+ return db_pgsql_fetch_array($result);
165+ default:
166+ $f = 'db_'. $db_type .'_fetch_array';
167+ return $f($result);
168+ }
169+}
170+
171+function db_num_rows($result) {
172+ global $db_type;
173+ switch ($db_type) {
174+ case 'mysql':
175+ return db_mysql_num_rows($result);
176+ case 'mysqli':
177+ return db_mysqli_num_rows($result);
178+ case 'pgsql':
179+ return db_pgsql_num_rows($result);
180+ default:
181+ $f = 'db_'. $db_type .'_num_rows';
182+ return $f($result);
183+ }
184+}
185+
186+function db_result($result, $row = 0) {
187+ global $db_type;
188+ switch ($db_type) {
189+ case 'mysql':
190+ return db_mysql_result($result, $row);
191+ case 'mysqli':
192+ return db_mysqli_result($result, $row);
193+ case 'pgsql':
194+ return db_pgsql_result($result, $row);
195+ default:
196+ $f = 'db_'. $db_type .'_result';
197+ return $f($result, $row);
198+ }
199+}
200+
201+function db_error() {
202+ global $db_type;
203+ switch ($db_type) {
204+ case 'mysql':
205+ return db_mysql_error();
206+ case 'mysqli':
207+ return db_mysqli_error();
208+ case 'pgsql':
209+ return db_pgsql_error();
210+ default:
211+ $f = 'db_'. $db_type .'_error';
212+ return $f();
213+ }
214+}
215+
216+function db_next_id($name) {
217+ global $db_type;
218+ switch ($db_type) {
219+ case 'mysql':
220+ return db_mysql_next_id($name);
221+ case 'mysqli':
222+ return db_mysqli_next_id($name);
223+ case 'pgsql':
224+ return db_pgsql_next_id($name);
225+ default:
226+ $f = 'db_'. $db_type .'_next_id';
227+ return $f($name);
228+ }
229+}
230+
231+function db_affected_rows() {
232+ global $db_type;
233+ switch ($db_type) {
234+ case 'mysql':
235+ return db_mysql_affected_rows();
236+ case 'mysqli':
237+ return db_mysqli_affected_rows();
238+ case 'pgsql':
239+ return db_pgsql_affected_rows();
240+ default:
241+ $f = 'db_'. $db_type .'_affected_rows';
242+ return $f();
243+ }
244+}
245+
246+function db_query_range($query) {
247+ $args = func_get_args();
248+ global $db_type;
249+ $args = func_get_args();
250+ switch ($db_type) {
251+ case 'mysql':
252+ return db_mysql_query_range($query, $args);
253+ case 'mysqli':
254+ return db_mysqli_query_range($query, $args);
255+ case 'pgsql':
256+ return db_pgsql_query_range($query, $args);
257+ default:
258+ $f = 'db_'. $db_type .'_query_range';
259+ return $f($query, $args);
260+ }
261+}
262+
263+function db_query_temporary($query) {
264+ $args = func_get_args();
265+ global $db_type;
266+ $args = func_get_args();
267+ switch ($db_type) {
268+ case 'mysql':
269+ return db_mysql_query_temporary($query, $args);
270+ case 'mysqli':
271+ return db_mysqli_query_temporary($query, $args);
272+ case 'pgsql':
273+ return db_pgsql_query_temporary($query, $args);
274+ default:
275+ $f = 'db_'. $db_type .'_query_temporary';
276+ return $f($query, $args);
277+ }
278+}
279+
280+function db_encode_blob($data) {
281+ global $db_type;
282+ switch ($db_type) {
283+ case 'mysql':
284+ return db_mysql_encode_blob($data);
285+ case 'mysqli':
286+ return db_mysqli_encode_blob($data);
287+ case 'pgsql':
288+ return db_pgsql_encode_blob($data);
289+ default:
290+ $f = 'db_'. $db_type .'_encode_blob';
291+ return $f($data);
292+ }
293+}
294+
295+function db_decode_blob($data) {
296+ global $db_type;
297+ switch ($db_type) {
298+ case 'mysql':
299+ return db_mysql_decode_blob($data);
300+ case 'mysqli':
301+ return db_mysqli_decode_blob($data);
302+ case 'pgsql':
303+ return db_pgsql_decode_blob($data);
304+ default:
305+ $f = 'db_'. $db_type .'_decode_blob';
306+ return $f($data);
307+ }
308+}
309+
310+function db_escape_string($text) {
311+ global $db_type;
312+ switch ($db_type) {
313+ case 'mysql':
314+ return db_mysql_escape_string($text);
315+ case 'mysqli':
316+ return db_mysqli_escape_string($text);
317+ case 'pgsql':
318+ return db_pgsql_escape_string($text);
319+ default:
320+ $f = 'db_'. $db_type .'_escape_string';
321+ return $f($text);
322+ }
323+}
324+
325+function db_lock_table($table) {
326+ global $db_type;
327+ switch ($db_type) {
328+ case 'mysql':
329+ return db_mysql_lock_table($table);
330+ case 'mysqli':
331+ return db_mysqli_lock_table($table);
332+ case 'pgsql':
333+ return db_pgsql_lock_table($table);
334+ default:
335+ $f = 'db_'. $db_type .'_lock_table';
336+ return $f($table);
337+ }
338+}
339+
340+function db_unlock_tables() {
341+ global $db_type;
342+ switch ($db_type) {
343+ case 'mysql':
344+ return db_mysql_unlock_tables();
345+ case 'mysqli':
346+ return db_mysqli_unlock_tables();
347+ case 'pgsql':
348+ return db_pgsql_unlock_tables();
349+ default:
350+ $f = 'db_'. $db_type .'_unlock_tables';
351+ return $f();
352+ }
353+}
354+
355+function db_table_exists($table) {
356+ global $db_type;
357+ switch ($db_type) {
358+ case 'mysql':
359+ return db_mysql_table_exists($table);
360+ case 'mysqli':
361+ return db_mysqli_table_exists($table);
362+ case 'pgsql':
363+ return db_pgsql_table_exists($table);
364+ default:
365+ $f = 'db_'. $db_type .'_table_exists';
366+ return $f($table);
367+ }
368+}
369+
370+function db_column_exists($table) {
371+ global $db_type;
372+ switch ($db_type) {
373+ case 'mysql':
374+ return db_mysql_column_exists($table);
375+ case 'mysqli':
376+ return db_mysqli_column_exists($table);
377+ case 'pgsql':
378+ return db_pgsql_column_exists($table);
379+ default:
380+ $f = 'db_'. $db_type .'_column_exists';
381+ return $f($table);
382+ }
383+}
384+
385+function db_check_setup() {
386+ global $db_type;
387+ switch ($db_type) {
388+ case 'mysql':
389+ return db_mysql_check_setup();
390+ case 'mysqli':
391+ return db_mysqli_check_setup();
392+ case 'pgsql':
393+ return db_pgsql_check_setup();
394+ default:
395+ $f = 'db_'. $db_type .'_check_setup';
396+ return $f();
397+ }
398+}
399+
400+/**
401+ * Returns the last insert id. This function is thread safe.
402+ *
403+ * @param $table
404+ * The name of the table you inserted into.
405+ * @param $field
406+ * The name of the autoincrement field.
407+ */
408+function db_last_insert_id($table, $field) {
409+ global $db_type;
410+ switch ($db_type) {
411+ case 'mysql':
412+ return db_mysql_last_insert_id($table, $field);
413+ case 'mysqli':
414+ return db_mysql_last_insert_id($table, $field);
415+ case 'pgsql':
416+ return db_pgsql_last_insert_id($table, $field);
417+ default:
418+ $f = 'db_'. $db_type .'_check_setup';
419+ return $f();
420+ }
421+}
422+
423+function _db_create_key_sql($fields) {
424+ global $db_type;
425+ switch ($db_type) {
426+ case 'mysql':
427+ return _db_mysql_create_key_sql($fields);
428+ case 'mysqli':
429+ return _db_mysql_create_key_sql($fields);
430+ case 'pgsql':
431+ return _db_pgsql_create_key_sql($fields);
432+ default:
433+ $f = '_db_'. $db_type .'_check_setup';
434+ return $f();
435+ }
436+}
437+
438+/**
439+ * Set database-engine specific properties for a field.
440+ *
441+ * @param $field
442+ * A field description array, as specified in the schema documentation.
443+ */
444+function _db_process_field($field) {
445+ global $db_type;
446+ switch ($db_type) {
447+ case 'mysql':
448+ return _db_mysql_process_field($field);
449+ case 'mysqli':
450+ return _db_mysql_process_field($field);
451+ case 'pgsql':
452+ return _db_pgsql_process_field($field);
453+ default:
454+ $f = '_db_'. $db_type .'_check_setup';
455+ return $f();
456+ }
457+}
458+
459+/**
460+ * Create an SQL string for a field to be used in table creation or alteration.
461+ *
462+ * Before passing a field out of a schema definition into this function it has
463+ * to be processed by _db_process_field().
464+ *
465+ * @param $name
466+ * Name of the field.
467+ * @param $spec
468+ * The field specification, as per the schema data structure format.
469+ */
470+function _db_create_field_sql($name, $spec) {
471+ global $db_type;
472+ switch ($db_type) {
473+ case 'mysql':
474+ return _db_mysql_create_field_sql($name, $spec);
475+ case 'mysqli':
476+ return _db_mysql_create_field_sql($name, $spec);
477+ case 'pgsql':
478+ return _db_pgsql_create_field_sql($name, $spec);
479+ default:
480+ $f = '_db_'. $db_type .'_check_setup';
481+ return $f();
482+ }
483+}
484+
485 /**
486 * @} End of "defgroup database".
487 */
488@@ -534,6 +979,343 @@ function db_escape_table($string) {
489 * @see drupal_install_schema()
490 */
491
492+/**
493+ * This maps a generic data type in combination with its data size
494+ * to the engine-specific data type.
495+ */
496+function db_type_map() {
497+ global $db_type;
498+ switch ($db_type) {
499+ case 'mysql':
500+ return db_mysql_type_map();
501+ case 'mysqli':
502+ return db_mysql_type_map();
503+ case 'pgsql':
504+ return db_pgsql_type_map();
505+ default:
506+ $f = 'db_'. $db_type .'_check_setup';
507+ return $f();
508+ }
509+}
510+
511+/**
512+ * Generate SQL to create a new table from a Drupal schema definition.
513+ *
514+ * @param $name
515+ * The name of the table to create.
516+ * @param $table
517+ * A Schema API table definition array.
518+ * @return
519+ * An array of SQL statements to create the table.
520+ */
521+function db_create_table_sql($name, $table) {
522+ global $db_type;
523+ switch ($db_type) {
524+ case 'mysql':
525+ return db_mysql_create_table_sql();
526+ case 'mysqli':
527+ return db_mysql_create_table_sql();
528+ case 'pgsql':
529+ return db_pgsql_create_table_sql();
530+ default:
531+ $f = 'db_'. $db_type .'_check_setup';
532+ return $f();
533+ }
534+}
535+
536+/**
537+ * Rename a table.
538+ *
539+ * @param $ret
540+ * Array to which query results will be added.
541+ * @param $table
542+ * The table to be renamed.
543+ * @param $new_name
544+ * The new name for the table.
545+ */
546+function db_rename_table(&$ret, $table, $new_name) {
547+ $ret[] = update_sql('ALTER TABLE {'. $table .'} RENAME TO {'. $new_name .'}');
548+}
549+
550+/**
551+ * Drop a table.
552+ *
553+ * @param $ret
554+ * Array to which query results will be added.
555+ * @param $table
556+ * The table to be dropped.
557+ */
558+function db_drop_table(&$ret, $table) {
559+ $ret[] = update_sql('DROP TABLE {'. $table .'}');
560+}
561+
562+/**
563+ * Add a new field to a table.
564+ *
565+ * @param $ret
566+ * Array to which query results will be added.
567+ * @param $table
568+ * Name of the table to be altered.
569+ * @param $field
570+ * Name of the field to be added.
571+ * @param $spec
572+ * The field specification array, as taken from a schema definition.
573+ * The specification may also contain the key 'initial', the newly
574+ * created field will be set to the value of the key in all rows.
575+ * This is most useful for creating NOT NULL columns with no default
576+ * value in existing tables.
577+ * @param $keys_new
578+ * Optional keys and indexes specification to be created on the
579+ * table along with adding the field. The format is the same as a
580+ * table specification but without the 'fields' element. If you are
581+ * adding a type 'serial' field, you MUST specify at least one key
582+ * or index including it in this array. @see db_change_field for more
583+ * explanation why.
584+ */
585+function db_add_field(&$ret, $table, $field, $spec, $keys_new = array()) {
586+ global $db_type;
587+ switch ($db_type) {
588+ case 'mysql':
589+ return db_mysql_add_field($ret, $table, $field, $spec, $keys_new);
590+ case 'mysqli':
591+ return db_mysql_add_field($ret, $table, $field, $spec, $keys_new);
592+ case 'pgsql':
593+ return db_pgsql_add_field($ret, $table, $field, $spec, $keys_new);
594+ default:
595+ $f = 'db_'. $db_type .'_check_setup';
596+ return $f();
597+ }
598+}
599+
600+/**
601+ * Drop a field.
602+ *
603+ * @param $ret
604+ * Array to which query results will be added.
605+ * @param $table
606+ * The table to be altered.
607+ * @param $field
608+ * The field to be dropped.
609+ */
610+function db_drop_field(&$ret, $table, $field) {
611+ global $db_type;
612+ switch ($db_type) {
613+ case 'mysql':
614+ return db_mysql_drop_field($ret, $table, $field);
615+ case 'mysqli':
616+ return db_mysql_drop_field($ret, $table, $field);
617+ case 'pgsql':
618+ return db_pgsql_drop_field($ret, $table, $field);
619+ default:
620+ $f = 'db_'. $db_type .'_drop_field';
621+ return $f();
622+ }
623+}
624+
625+/**
626+ * Set the default value for a field.
627+ *
628+ * @param $ret
629+ * Array to which query results will be added.
630+ * @param $table
631+ * The table to be altered.
632+ * @param $field
633+ * The field to be altered.
634+ * @param $default
635+ * Default value to be set. NULL for 'default NULL'.
636+ */
637+function db_field_set_default(&$ret, $table, $field, $default) {
638+ if ($default == NULL) {
639+ $default = 'NULL';
640+ }
641+ else {
642+ $default = is_string($default) ? "'$default'" : $default;
643+ }
644+
645+ $ret[] = update_sql('ALTER TABLE {'. $table .'} ALTER COLUMN '. $field .' SET DEFAULT '. $default);
646+}
647+
648+/**
649+ * Set a field to have no default value.
650+ *
651+ * @param $ret
652+ * Array to which query results will be added.
653+ * @param $table
654+ * The table to be altered.
655+ * @param $field
656+ * The field to be altered.
657+ */
658+function db_field_set_no_default(&$ret, $table, $field) {
659+ $ret[] = update_sql('ALTER TABLE {'. $table .'} ALTER COLUMN '. $field .' DROP DEFAULT');
660+}
661+
662+/**
663+ * Add a primary key.
664+ *
665+ * @param $ret
666+ * Array to which query results will be added.
667+ * @param $table
668+ * The table to be altered.
669+ * @param $fields
670+ * Fields for the primary key.
671+ */
672+function db_add_primary_key(&$ret, $table, $fields) {
673+ global $db_type;
674+ switch ($db_type) {
675+ case 'mysql':
676+ return db_mysql_add_primary_key($ret, $table, $fields);
677+ case 'mysqli':
678+ return db_mysql_add_primary_key($ret, $table, $fields);
679+ case 'pgsql':
680+ return db_pgsql_add_primary_key($ret, $table, $fields);
681+ default:
682+ $f = 'db_'. $db_type .'_add_primary_key';
683+ return $f();
684+ }
685+}
686+
687+/**
688+ * Drop the primary key.
689+ *
690+ * @param $ret
691+ * Array to which query results will be added.
692+ * @param $table
693+ * The table to be altered.
694+ */
695+function db_drop_primary_key(&$ret, $table) {
696+ global $db_type;
697+ switch ($db_type) {
698+ case 'mysql':
699+ return db_mysql_drop_primary_key($ret, $table);
700+ case 'mysqli':
701+ return db_mysql_drop_primary_key($ret, $table);
702+ case 'pgsql':
703+ return db_pgsql_drop_primary_key($ret, $table);
704+ default:
705+ $f = 'db_'. $db_type .'_drop_primary_key';
706+ return $f();
707+ }
708+}
709+
710+/**
711+ * Add a unique key.
712+ *
713+ * @param $ret
714+ * Array to which query results will be added.
715+ * @param $table
716+ * The table to be altered.
717+ * @param $name
718+ * The name of the key.
719+ * @param $fields
720+ * An array of field names.
721+ */
722+function db_add_unique_key(&$ret, $table, $name, $fields) {
723+ global $db_type;
724+ switch ($db_type) {
725+ case 'mysql':
726+ return db_mysql_add_unique_key($ret, $table, $name, $fields);
727+ case 'mysqli':
728+ return db_mysql_add_unique_key($ret, $table, $name, $fields);
729+ case 'pgsql':
730+ return db_pgsql_add_unique_key($ret, $table, $name, $fields);
731+ default:
732+ $f = 'db_'. $db_type .'_add_unique_key';
733+ return $f();
734+ }
735+}
736+
737+/**
738+ * Drop a unique key.
739+ *
740+ * @param $ret
741+ * Array to which query results will be added.
742+ * @param $table
743+ * The table to be altered.
744+ * @param $name
745+ * The name of the key.
746+ */
747+function db_drop_unique_key(&$ret, $table, $name) {
748+ global $db_type;
749+ switch ($db_type) {
750+ case 'mysql':
751+ return db_mysql_drop_unique_key($ret, $table, $name);
752+ case 'mysqli':
753+ return db_mysql_drop_unique_key($ret, $table, $name);
754+ case 'pgsql':
755+ return db_pgsql_drop_unique_key($ret, $table, $name);
756+ default:
757+ $f = 'db_'. $db_type .'_drop_unique_key';
758+ return $f();
759+ }
760+}
761+
762+/**
763+ * Add an index.
764+ *
765+ * @param $ret
766+ * Array to which query results will be added.
767+ * @param $table
768+ * The table to be altered.
769+ * @param $name
770+ * The name of the index.
771+ * @param $fields
772+ * An array of field names.
773+ */
774+function db_add_index(&$ret, $table, $name, $fields) {
775+ global $db_type;
776+ switch ($db_type) {
777+ case 'mysql':
778+ return db_mysql_add_index($ret, $table, $name, $fields);
779+ case 'mysqli':
780+ return db_mysql_add_index($ret, $table, $name, $fields);
781+ case 'pgsql':
782+ return db_pgsql_add_index($ret, $table, $name, $fields);
783+ default:
784+ $f = 'db_'. $db_type .'_add_index';
785+ return $f();
786+ }
787+}
788+
789+/**
790+ * Drop an index.
791+ *
792+ * @param $ret
793+ * Array to which query results will be added.
794+ * @param $table
795+ * The table to be altered.
796+ * @param $name
797+ * The name of the index.
798+ */
799+function db_drop_index(&$ret, $table, $name) {
800+ global $db_type;
801+ switch ($db_type) {
802+ case 'mysql':
803+ return db_mysql_drop_index($ret, $table, $name);
804+ case 'mysqli':
805+ return db_mysql_drop_index($ret, $table, $name);
806+ case 'pgsql':
807+ return db_pgsql_drop_index($ret, $table, $name);
808+ default:
809+ $f = 'db_'. $db_type .'_drop_index';
810+ return $f();
811+ }
812+}
813+
814+function db_change_field(&$ret, $table, $field, $field_new, $spec, $new_keys = array()) {
815+ global $db_type;
816+ switch ($db_type) {
817+ case 'mysql':
818+ return db_mysql_change_field($ret, $table, $field, $field_new, $spec, $new_keys);
819+ case 'mysqli':
820+ return db_mysql_change_field($ret, $table, $field, $field_new, $spec, $new_keys);
821+ case 'pgsql':
822+ return db_pgsql_change_field($ret, $table, $field, $field_new, $spec, $new_keys);
823+ default:
824+ $f = 'db_'. $db_type .'_change_field';
825+ return $f();
826+ }
827+}
828+
829 /**
830 * Create a new table from a Drupal table definition.
831 *
832@@ -620,3 +1402,4 @@ function db_type_placeholder($type) {
833 /**
834 * @} End of "defgroup schemaapi".
835 */
836+
837diff -rupN a/includes/database.mysql-common.inc b/includes/database.mysql-common.inc
838--- a/includes/database.mysql-common.inc 2011-05-25 13:43:55.000000000 -0700
839+++ b/includes/database.mysql-common.inc 2011-12-20 18:15:42.010029683 -0800
840@@ -6,42 +6,6 @@
841 */
842
843 /**
844- * Runs a basic query in the active database.
845- *
846- * User-supplied arguments to the query should be passed in as separate
847- * parameters so that they can be properly escaped to avoid SQL injection
848- * attacks.
849- *
850- * @param $query
851- * A string containing an SQL query.
852- * @param ...
853- * A variable number of arguments which are substituted into the query
854- * using printf() syntax. Instead of a variable number of query arguments,
855- * you may also pass a single array containing the query arguments.
856- *
857- * Valid %-modifiers are: %s, %d, %f, %b (binary data, do not enclose
858- * in '') and %%.
859- *
860- * NOTE: using this syntax will cast NULL and FALSE values to decimal 0,
861- * and TRUE values to decimal 1.
862- *
863- * @return
864- * A database query result resource, or FALSE if the query was not
865- * executed correctly.
866- */
867-function db_query($query) {
868- $args = func_get_args();
869- array_shift($args);
870- $query = db_prefix_tables($query);
871- if (isset($args[0]) and is_array($args[0])) { // 'All arguments in one array' syntax
872- $args = $args[0];
873- }
874- _db_query_callback($args, TRUE);
875- $query = preg_replace_callback(DB_QUERY_REGEXP, '_db_query_callback', $query);
876- return _db_query($query);
877-}
878-
879-/**
880 * @ingroup schemaapi
881 * @{
882 */
883@@ -56,7 +20,7 @@ function db_query($query) {
884 * @return
885 * An array of SQL statements to create the table.
886 */
887-function db_create_table_sql($name, $table) {
888+function db_mysql_create_table_sql($name, $table) {
889
890 if (empty($table['mysql_suffix'])) {
891 $table['mysql_suffix'] = '/*!40100 DEFAULT CHARACTER SET utf8';
892@@ -112,7 +76,7 @@ function _db_create_keys_sql($spec) {
893 return $keys;
894 }
895
896-function _db_create_key_sql($fields) {
897+function _db_mysql_create_key_sql($fields) {
898 $ret = array();
899 foreach ($fields as $field) {
900 if (is_array($field)) {
901@@ -131,7 +95,7 @@ function _db_create_key_sql($fields) {
902 * @param $field
903 * A field description array, as specified in the schema documentation.
904 */
905-function _db_process_field($field) {
906+function _db_mysql_process_field($field) {
907
908 if (!isset($field['size'])) {
909 $field['size'] = 'normal';
910@@ -161,7 +125,7 @@ function _db_process_field($field) {
911 * @param $spec
912 * The field specification, as per the schema data structure format.
913 */
914-function _db_create_field_sql($name, $spec) {
915+function _db_mysql_create_field_sql($name, $spec) {
916 $sql = "`". $name ."` ". $spec['mysql_type'];
917
918 if (in_array($spec['type'], array('varchar', 'char', 'text')) && isset($spec['length'])) {
919@@ -201,7 +165,7 @@ function _db_create_field_sql($name, $sp
920 * This maps a generic data type in combination with its data size
921 * to the engine-specific data type.
922 */
923-function db_type_map() {
924+function db_mysql_type_map() {
925 // Put :normal last so it gets preserved by array_flip. This makes
926 // it much easier for modules (such as schema.module) to map
927 // database types back into schema types.
928@@ -244,32 +208,6 @@ function db_type_map() {
929 }
930
931 /**
932- * Rename a table.
933- *
934- * @param $ret
935- * Array to which query results will be added.
936- * @param $table
937- * The table to be renamed.
938- * @param $new_name
939- * The new name for the table.
940- */
941-function db_rename_table(&$ret, $table, $new_name) {
942- $ret[] = update_sql('ALTER TABLE {'. $table .'} RENAME TO {'. $new_name .'}');
943-}
944-
945-/**
946- * Drop a table.
947- *
948- * @param $ret
949- * Array to which query results will be added.
950- * @param $table
951- * The table to be dropped.
952- */
953-function db_drop_table(&$ret, $table) {
954- $ret[] = update_sql('DROP TABLE {'. $table .'}');
955-}
956-
957-/**
958 * Add a new field to a table.
959 *
960 * @param $ret
961@@ -292,7 +230,7 @@ function db_drop_table(&$ret, $table) {
962 * or index including it in this array. See db_change_field() for more
963 * explanation why.
964 */
965-function db_add_field(&$ret, $table, $field, $spec, $keys_new = array()) {
966+function db_mysql_add_field(&$ret, $table, $field, $spec, $keys_new = array()) {
967 $fixnull = FALSE;
968 if (!empty($spec['not null']) && !isset($spec['default'])) {
969 $fixnull = TRUE;
970@@ -326,48 +264,11 @@ function db_add_field(&$ret, $table, $fi
971 * @param $field
972 * The field to be dropped.
973 */
974-function db_drop_field(&$ret, $table, $field) {
975+function db_mysql_drop_field(&$ret, $table, $field) {
976 $ret[] = update_sql('ALTER TABLE {'. $table .'} DROP '. $field);
977 }
978
979 /**
980- * Set the default value for a field.
981- *
982- * @param $ret
983- * Array to which query results will be added.
984- * @param $table
985- * The table to be altered.
986- * @param $field
987- * The field to be altered.
988- * @param $default
989- * Default value to be set. NULL for 'default NULL'.
990- */
991-function db_field_set_default(&$ret, $table, $field, $default) {
992- if ($default === NULL) {
993- $default = 'NULL';
994- }
995- else {
996- $default = is_string($default) ? "'$default'" : $default;
997- }
998-
999- $ret[] = update_sql('ALTER TABLE {'. $table .'} ALTER COLUMN '. $field .' SET DEFAULT '. $default);
1000-}
1001-
1002-/**
1003- * Set a field to have no default value.
1004- *
1005- * @param $ret
1006- * Array to which query results will be added.
1007- * @param $table
1008- * The table to be altered.
1009- * @param $field
1010- * The field to be altered.
1011- */
1012-function db_field_set_no_default(&$ret, $table, $field) {
1013- $ret[] = update_sql('ALTER TABLE {'. $table .'} ALTER COLUMN '. $field .' DROP DEFAULT');
1014-}
1015-
1016-/**
1017 * Add a primary key.
1018 *
1019 * @param $ret
1020@@ -377,7 +278,7 @@ function db_field_set_no_default(&$ret,
1021 * @param $fields
1022 * Fields for the primary key.
1023 */
1024-function db_add_primary_key(&$ret, $table, $fields) {
1025+function db_mysql_add_primary_key(&$ret, $table, $fields) {
1026 $ret[] = update_sql('ALTER TABLE {'. $table .'} ADD PRIMARY KEY ('.
1027 _db_create_key_sql($fields) .')');
1028 }
1029@@ -390,7 +291,7 @@ function db_add_primary_key(&$ret, $tabl
1030 * @param $table
1031 * The table to be altered.
1032 */
1033-function db_drop_primary_key(&$ret, $table) {
1034+function db_mysql_drop_primary_key(&$ret, $table) {
1035 $ret[] = update_sql('ALTER TABLE {'. $table .'} DROP PRIMARY KEY');
1036 }
1037
1038@@ -406,7 +307,7 @@ function db_drop_primary_key(&$ret, $tab
1039 * @param $fields
1040 * An array of field names.
1041 */
1042-function db_add_unique_key(&$ret, $table, $name, $fields) {
1043+function db_mysql_add_unique_key(&$ret, $table, $name, $fields) {
1044 $ret[] = update_sql('ALTER TABLE {'. $table .'} ADD UNIQUE KEY '.
1045 $name .' ('. _db_create_key_sql($fields) .')');
1046 }
1047@@ -421,7 +322,7 @@ function db_add_unique_key(&$ret, $table
1048 * @param $name
1049 * The name of the key.
1050 */
1051-function db_drop_unique_key(&$ret, $table, $name) {
1052+function db_mysql_drop_unique_key(&$ret, $table, $name) {
1053 $ret[] = update_sql('ALTER TABLE {'. $table .'} DROP KEY '. $name);
1054 }
1055
1056@@ -437,7 +338,7 @@ function db_drop_unique_key(&$ret, $tabl
1057 * @param $fields
1058 * An array of field names.
1059 */
1060-function db_add_index(&$ret, $table, $name, $fields) {
1061+function db_mysql_add_index(&$ret, $table, $name, $fields) {
1062 $query = 'ALTER TABLE {'. $table .'} ADD INDEX '. $name .' ('. _db_create_key_sql($fields) .')';
1063 $ret[] = update_sql($query);
1064 }
1065@@ -452,7 +353,7 @@ function db_add_index(&$ret, $table, $na
1066 * @param $name
1067 * The name of the index.
1068 */
1069-function db_drop_index(&$ret, $table, $name) {
1070+function db_mysql_drop_index(&$ret, $table, $name) {
1071 $ret[] = update_sql('ALTER TABLE {'. $table .'} DROP INDEX '. $name);
1072 }
1073
1074@@ -519,7 +420,7 @@ function db_drop_index(&$ret, $table, $n
1075 * table specification but without the 'fields' element.
1076 */
1077
1078-function db_change_field(&$ret, $table, $field, $field_new, $spec, $keys_new = array()) {
1079+function db_mysql_change_field(&$ret, $table, $field, $field_new, $spec, $keys_new = array()) {
1080 $sql = 'ALTER TABLE {'. $table .'} CHANGE `'. $field .'` '.
1081 _db_create_field_sql($field_new, _db_process_field($spec));
1082 if (count($keys_new)) {
1083@@ -536,6 +437,6 @@ function db_change_field(&$ret, $table,
1084 * @param $field
1085 * The name of the autoincrement field.
1086 */
1087-function db_last_insert_id($table, $field) {
1088+function db_mysql_last_insert_id($table, $field) {
1089 return db_result(db_query('SELECT LAST_INSERT_ID()'));
1090 }
1091diff -rupN a/includes/database.mysqli.inc b/includes/database.mysqli.inc
1092--- a/includes/database.mysqli.inc 2011-05-25 13:43:55.000000000 -0700
1093+++ b/includes/database.mysqli.inc 2011-12-20 18:15:42.010029683 -0800
1094@@ -17,9 +17,15 @@
1095 require_once './includes/database.mysql-common.inc';
1096
1097 /**
1098+ * Verify if the database is set up correctly.
1099+ */
1100+function db_mysqli_check_setup() {
1101+}
1102+
1103+/**
1104 * Report database status.
1105 */
1106-function db_status_report($phase) {
1107+function db_mysqli_status_report($phase) {
1108 $t = get_t();
1109
1110 $version = db_version();
1111@@ -42,7 +48,7 @@ function db_status_report($phase) {
1112 *
1113 * @return Database server version
1114 */
1115-function db_version() {
1116+function db_mysqli_version() {
1117 global $active_db;
1118 list($version) = explode('-', mysqli_get_server_info($active_db));
1119 return $version;
1120@@ -53,7 +59,7 @@ function db_version() {
1121 *
1122 * Note that mysqli does not support persistent connections.
1123 */
1124-function db_connect($url) {
1125+function db_mysqli_connect($url) {
1126 // Check if MySQLi support is present in PHP
1127 if (!function_exists('mysqli_init') && !extension_loaded('mysqli')) {
1128 _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.');
1129@@ -94,7 +100,7 @@ function db_connect($url) {
1130 /**
1131 * Helper function for db_query().
1132 */
1133-function _db_query($query, $debug = 0) {
1134+function _db_mysqli_query($query, $debug = 0) {
1135 global $active_db, $queries, $user;
1136
1137 if (variable_get('dev_query', 0)) {
1138@@ -145,7 +151,7 @@ function _db_query($query, $debug = 0) {
1139 * An object representing the next row of the result, or FALSE. The attributes
1140 * of this object are the table fields selected by the query.
1141 */
1142-function db_fetch_object($result) {
1143+function db_mysqli_fetch_object($result) {
1144 if ($result) {
1145 $object = mysqli_fetch_object($result);
1146 return isset($object) ? $object : FALSE;
1147@@ -162,7 +168,7 @@ function db_fetch_object($result) {
1148 * The keys of this object are the names of the table fields selected by the
1149 * query, and the values are the field values for this result row.
1150 */
1151-function db_fetch_array($result) {
1152+function db_mysqli_fetch_array($result) {
1153 if ($result) {
1154 $array = mysqli_fetch_array($result, MYSQLI_ASSOC);
1155 return isset($array) ? $array : FALSE;
1156@@ -180,7 +186,7 @@ function db_fetch_array($result) {
1157 * @return
1158 * The resulting field or FALSE.
1159 */
1160-function db_result($result) {
1161+function db_mysqli_result($result) {
1162 if ($result && mysqli_num_rows($result) > 0) {
1163 // The mysqli_fetch_row function has an optional second parameter $row
1164 // but that can't be used for compatibility with Oracle, DB2, etc.
1165@@ -193,7 +199,7 @@ function db_result($result) {
1166 /**
1167 * Determine whether the previous query caused an error.
1168 */
1169-function db_error() {
1170+function db_mysqli_error() {
1171 global $active_db;
1172 return mysqli_errno($active_db);
1173 }
1174@@ -201,7 +207,7 @@ function db_error() {
1175 /**
1176 * Determine the number of rows changed by the preceding query.
1177 */
1178-function db_affected_rows() {
1179+function db_mysqli_affected_rows() {
1180 global $active_db; /* mysqli connection resource */
1181 return mysqli_affected_rows($active_db);
1182 }
1183@@ -234,8 +240,7 @@ function db_affected_rows() {
1184 * A database query result resource, or FALSE if the query was not executed
1185 * correctly.
1186 */
1187-function db_query_range($query) {
1188- $args = func_get_args();
1189+function db_mysqli_query_range($query, $args) {
1190 $count = array_pop($args);
1191 $from = array_pop($args);
1192 array_shift($args);
1193@@ -282,8 +287,7 @@ function db_query_range($query) {
1194 * A database query result resource, or FALSE if the query was not executed
1195 * correctly.
1196 */
1197-function db_query_temporary($query) {
1198- $args = func_get_args();
1199+function db_mysqli_query_temporary($query, $args) {
1200 $tablename = array_pop($args);
1201 array_shift($args);
1202
1203@@ -304,7 +308,7 @@ function db_query_temporary($query) {
1204 * @return
1205 * Encoded data.
1206 */
1207-function db_encode_blob($data) {
1208+function db_mysqli_encode_blob($data) {
1209 global $active_db;
1210 return "'". mysqli_real_escape_string($active_db, $data) ."'";
1211 }
1212@@ -317,14 +321,14 @@ function db_encode_blob($data) {
1213 * @return
1214 * Decoded data.
1215 */
1216-function db_decode_blob($data) {
1217+function db_mysqli_decode_blob($data) {
1218 return $data;
1219 }
1220
1221 /**
1222 * Prepare user input for use in a database query, preventing SQL injection attacks.
1223 */
1224-function db_escape_string($text) {
1225+function db_mysqli_escape_string($text) {
1226 global $active_db;
1227 return mysqli_real_escape_string($active_db, $text);
1228 }
1229@@ -332,14 +336,14 @@ function db_escape_string($text) {
1230 /**
1231 * Lock a table.
1232 */
1233-function db_lock_table($table) {
1234+function db_mysqli_lock_table($table) {
1235 db_query('LOCK TABLES {'. db_escape_table($table) .'} WRITE');
1236 }
1237
1238 /**
1239 * Unlock all locked tables.
1240 */
1241-function db_unlock_tables() {
1242+function db_mysqli_unlock_tables() {
1243 db_query('UNLOCK TABLES');
1244 }
1245
1246@@ -352,7 +356,7 @@ function db_unlock_tables() {
1247 * @return
1248 * TRUE if the table exists, and FALSE if the table does not exist.
1249 */
1250-function db_table_exists($table) {
1251+function db_mysqli_table_exists($table) {
1252 return (bool) db_fetch_object(db_query("SHOW TABLES LIKE '{". db_escape_table($table) ."}'"));
1253 }
1254
1255@@ -367,7 +371,7 @@ function db_table_exists($table) {
1256 * @return
1257 * TRUE if the column exists, and FALSE if the column does not exist.
1258 */
1259-function db_column_exists($table, $column) {
1260+function db_mysqli_column_exists($table, $column) {
1261 return (bool) db_fetch_object(db_query("SHOW COLUMNS FROM {". db_escape_table($table) ."} LIKE '". db_escape_table($column) ."'"));
1262 }
1263
1264diff -rupN a/includes/database.mysql.inc b/includes/database.mysql.inc
1265--- a/includes/database.mysql.inc 2011-05-25 13:43:55.000000000 -0700
1266+++ b/includes/database.mysql.inc 2011-12-20 18:15:42.010029683 -0800
1267@@ -16,7 +16,7 @@ require_once './includes/database.mysql-
1268 /**
1269 * Report database status.
1270 */
1271-function db_status_report($phase) {
1272+function db_mysql_status_report($phase) {
1273 $t = get_t();
1274
1275 $version = db_version();
1276@@ -39,7 +39,7 @@ function db_status_report($phase) {
1277 *
1278 * @return Database server version
1279 */
1280-function db_version() {
1281+function db_mysql_version() {
1282 list($version) = explode('-', mysql_get_server_info());
1283 return $version;
1284 }
1285@@ -47,7 +47,7 @@ function db_version() {
1286 /**
1287 * Initialize a database connection.
1288 */
1289-function db_connect($url) {
1290+function db_mysql_connect($url) {
1291 $url = parse_url($url);
1292
1293 // Check if MySQL support is present in PHP
1294@@ -95,7 +95,7 @@ function db_connect($url) {
1295 /**
1296 * Helper function for db_query().
1297 */
1298-function _db_query($query, $debug = 0) {
1299+function _db_mysql_query($query, $debug = 0) {
1300 global $active_db, $queries, $user;
1301
1302 if (variable_get('dev_query', 0)) {
1303@@ -146,7 +146,7 @@ function _db_query($query, $debug = 0) {
1304 * An object representing the next row of the result, or FALSE. The attributes
1305 * of this object are the table fields selected by the query.
1306 */
1307-function db_fetch_object($result) {
1308+function db_mysql_fetch_object($result) {
1309 if ($result) {
1310 return mysql_fetch_object($result);
1311 }
1312@@ -162,7 +162,7 @@ function db_fetch_object($result) {
1313 * The keys of this object are the names of the table fields selected by the
1314 * query, and the values are the field values for this result row.
1315 */
1316-function db_fetch_array($result) {
1317+function db_mysql_fetch_array($result) {
1318 if ($result) {
1319 return mysql_fetch_array($result, MYSQL_ASSOC);
1320 }
1321@@ -180,7 +180,7 @@ function db_fetch_array($result) {
1322 * @return
1323 * The resulting field or FALSE.
1324 */
1325-function db_result($result) {
1326+function db_mysql_result($result) {
1327 if ($result && mysql_num_rows($result) > 0) {
1328 // The mysql_fetch_row function has an optional second parameter $row
1329 // but that can't be used for compatibility with Oracle, DB2, etc.
1330@@ -193,7 +193,7 @@ function db_result($result) {
1331 /**
1332 * Determine whether the previous query caused an error.
1333 */
1334-function db_error() {
1335+function db_mysql_error() {
1336 global $active_db;
1337 return mysql_errno($active_db);
1338 }
1339@@ -201,7 +201,7 @@ function db_error() {
1340 /**
1341 * Determine the number of rows changed by the preceding query.
1342 */
1343-function db_affected_rows() {
1344+function db_mysql_affected_rows() {
1345 global $active_db;
1346 return mysql_affected_rows($active_db);
1347 }
1348@@ -234,8 +234,7 @@ function db_affected_rows() {
1349 * A database query result resource, or FALSE if the query was not executed
1350 * correctly.
1351 */
1352-function db_query_range($query) {
1353- $args = func_get_args();
1354+function db_mysql_query_range($query, $args) {
1355 $count = array_pop($args);
1356 $from = array_pop($args);
1357 array_shift($args);
1358@@ -282,8 +281,7 @@ function db_query_range($query) {
1359 * A database query result resource, or FALSE if the query was not executed
1360 * correctly.
1361 */
1362-function db_query_temporary($query) {
1363- $args = func_get_args();
1364+function db_mysql_query_temporary($query, $args) {
1365 $tablename = array_pop($args);
1366 array_shift($args);
1367
1368@@ -304,7 +302,7 @@ function db_query_temporary($query) {
1369 * @return
1370 * Encoded data.
1371 */
1372-function db_encode_blob($data) {
1373+function db_mysql_encode_blob($data) {
1374 global $active_db;
1375 return "'". mysql_real_escape_string($data, $active_db) ."'";
1376 }
1377@@ -317,14 +315,14 @@ function db_encode_blob($data) {
1378 * @return
1379 * Decoded data.
1380 */
1381-function db_decode_blob($data) {
1382+function db_mysql_decode_blob($data) {
1383 return $data;
1384 }
1385
1386 /**
1387 * Prepare user input for use in a database query, preventing SQL injection attacks.
1388 */
1389-function db_escape_string($text) {
1390+function db_mysql_escape_string($text) {
1391 global $active_db;
1392 return mysql_real_escape_string($text, $active_db);
1393 }
1394@@ -332,14 +330,14 @@ function db_escape_string($text) {
1395 /**
1396 * Lock a table.
1397 */
1398-function db_lock_table($table) {
1399+function db_mysql_lock_table($table) {
1400 db_query('LOCK TABLES {'. db_escape_table($table) .'} WRITE');
1401 }
1402
1403 /**
1404 * Unlock all locked tables.
1405 */
1406-function db_unlock_tables() {
1407+function db_mysql_unlock_tables() {
1408 db_query('UNLOCK TABLES');
1409 }
1410
1411@@ -352,7 +350,7 @@ function db_unlock_tables() {
1412 * @return
1413 * TRUE if the table exists, and FALSE if the table does not exist.
1414 */
1415-function db_table_exists($table) {
1416+function db_mysql_table_exists($table) {
1417 return (bool) db_fetch_object(db_query("SHOW TABLES LIKE '{". db_escape_table($table) ."}'"));
1418 }
1419
1420@@ -367,7 +365,7 @@ function db_table_exists($table) {
1421 * @return
1422 * TRUE if the column exists, and FALSE if the column does not exist.
1423 */
1424-function db_column_exists($table, $column) {
1425+function db_mysql_column_exists($table, $column) {
1426 return (bool) db_fetch_object(db_query("SHOW COLUMNS FROM {". db_escape_table($table) ."} LIKE '". db_escape_table($column) ."'"));
1427 }
1428
1429diff -rupN a/includes/database.pgsql.inc b/includes/database.pgsql.inc
1430--- a/includes/database.pgsql.inc 2011-05-25 13:43:55.000000000 -0700
1431+++ b/includes/database.pgsql.inc 2011-12-20 18:15:42.010029683 -0800
1432@@ -13,14 +13,14 @@
1433 /**
1434 * Report database status.
1435 */
1436-function db_status_report() {
1437+function db_pgsql_status_report($phase) {
1438 $t = get_t();
1439
1440 $version = db_version();
1441
1442 $form['pgsql'] = array(
1443 'title' => $t('PostgreSQL database'),
1444- 'value' => $version,
1445+ 'value' => ($phase == 'runtime') ? l($version, 'admin/logs/status/sql') : $version,
1446 );
1447
1448 if (version_compare($version, DRUPAL_MINIMUM_PGSQL) < 0) {
1449@@ -36,14 +36,14 @@ function db_status_report() {
1450 *
1451 * @return Database server version
1452 */
1453-function db_version() {
1454+function db_pgsql_version() {
1455 return db_result(db_query("SHOW SERVER_VERSION"));
1456 }
1457
1458 /**
1459 * Initialize a database connection.
1460 */
1461-function db_connect($url) {
1462+function db_pgsql_connect($url) {
1463 // Check if PostgreSQL support is present in PHP
1464 if (!function_exists('pg_connect')) {
1465 _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.');
1466@@ -84,50 +84,14 @@ function db_connect($url) {
1467 // Restore error tracking setting
1468 ini_set('track_errors', $track_errors_previous);
1469
1470- pg_query($connection, "set client_encoding=\"UTF8\"");
1471+ //pg_query($connection, "set client_encoding=\"UTF8\"");
1472 return $connection;
1473 }
1474
1475 /**
1476- * Runs a basic query in the active database.
1477- *
1478- * User-supplied arguments to the query should be passed in as separate
1479- * parameters so that they can be properly escaped to avoid SQL injection
1480- * attacks.
1481- *
1482- * @param $query
1483- * A string containing an SQL query.
1484- * @param ...
1485- * A variable number of arguments which are substituted into the query
1486- * using printf() syntax. Instead of a variable number of query arguments,
1487- * you may also pass a single array containing the query arguments.
1488- *
1489- * Valid %-modifiers are: %s, %d, %f, %b (binary data, do not enclose
1490- * in '') and %%.
1491- *
1492- * NOTE: using this syntax will cast NULL and FALSE values to decimal 0,
1493- * and TRUE values to decimal 1.
1494- *
1495- * @return
1496- * A database query result resource, or FALSE if the query was not
1497- * executed correctly.
1498- */
1499-function db_query($query) {
1500- $args = func_get_args();
1501- array_shift($args);
1502- $query = db_prefix_tables($query);
1503- if (isset($args[0]) and is_array($args[0])) { // 'All arguments in one array' syntax
1504- $args = $args[0];
1505- }
1506- _db_query_callback($args, TRUE);
1507- $query = preg_replace_callback(DB_QUERY_REGEXP, '_db_query_callback', $query);
1508- return _db_query($query);
1509-}
1510-
1511-/**
1512 * Helper function for db_query().
1513 */
1514-function _db_query($query, $debug = 0) {
1515+function _db_pgsql_query($query, $debug = 0) {
1516 global $active_db, $last_result, $queries;
1517
1518 if (variable_get('dev_query', 0)) {
1519@@ -170,7 +134,7 @@ function _db_query($query, $debug = 0) {
1520 * An object representing the next row of the result, or FALSE. The attributes
1521 * of this object are the table fields selected by the query.
1522 */
1523-function db_fetch_object($result) {
1524+function db_pgsql_fetch_object($result) {
1525 if ($result) {
1526 return pg_fetch_object($result);
1527 }
1528@@ -186,7 +150,7 @@ function db_fetch_object($result) {
1529 * The keys of this object are the names of the table fields selected by the
1530 * query, and the values are the field values for this result row.
1531 */
1532-function db_fetch_array($result) {
1533+function db_pgsql_fetch_array($result) {
1534 if ($result) {
1535 return pg_fetch_assoc($result);
1536 }
1537@@ -203,7 +167,7 @@ function db_fetch_array($result) {
1538 * @return
1539 * The resulting field or FALSE.
1540 */
1541-function db_result($result) {
1542+function db_pgsql_result($result) {
1543 if ($result && pg_num_rows($result) > 0) {
1544 $array = pg_fetch_row($result);
1545 return $array[0];
1546@@ -214,7 +178,7 @@ function db_result($result) {
1547 /**
1548 * Determine whether the previous query caused an error.
1549 */
1550-function db_error() {
1551+function db_pgsql_error() {
1552 global $active_db;
1553 return pg_last_error($active_db);
1554 }
1555@@ -227,14 +191,14 @@ function db_error() {
1556 * @param $field
1557 * The name of the autoincrement field.
1558 */
1559-function db_last_insert_id($table, $field) {
1560+function db_pgsql_last_insert_id($table, $field) {
1561 return db_result(db_query("SELECT CURRVAL('{". db_escape_table($table) ."}_". db_escape_table($field) ."_seq')"));
1562 }
1563
1564 /**
1565 * Determine the number of rows changed by the preceding query.
1566 */
1567-function db_affected_rows() {
1568+function db_pgsql_affected_rows() {
1569 global $last_result;
1570 return empty($last_result) ? 0 : pg_affected_rows($last_result);
1571 }
1572@@ -268,8 +232,7 @@ function db_affected_rows() {
1573 * A database query result resource, or FALSE if the query was not executed
1574 * correctly.
1575 */
1576-function db_query_range($query) {
1577- $args = func_get_args();
1578+function db_pgsql_query_range($query, $args) {
1579 $count = array_pop($args);
1580 $from = array_pop($args);
1581 array_shift($args);
1582@@ -316,8 +279,7 @@ function db_query_range($query) {
1583 * A database query result resource, or FALSE if the query was not executed
1584 * correctly.
1585 */
1586-function db_query_temporary($query) {
1587- $args = func_get_args();
1588+function db_pgsql_query_temporary($query, $args) {
1589 $tablename = array_pop($args);
1590 array_shift($args);
1591
1592@@ -339,7 +301,7 @@ function db_query_temporary($query) {
1593 * @return
1594 * Encoded data.
1595 */
1596-function db_encode_blob($data) {
1597+function db_pgsql_encode_blob($data) {
1598 return "'". pg_escape_bytea($data) ."'";
1599 }
1600
1601@@ -352,7 +314,7 @@ function db_encode_blob($data) {
1602 * @return
1603 * Decoded data.
1604 */
1605-function db_decode_blob($data) {
1606+function db_pgsql_decode_blob($data) {
1607 return pg_unescape_bytea($data);
1608 }
1609
1610@@ -360,7 +322,7 @@ function db_decode_blob($data) {
1611 * Prepare user input for use in a database query, preventing SQL injection attacks.
1612 * Note: This function requires PostgreSQL 7.2 or later.
1613 */
1614-function db_escape_string($text) {
1615+function db_pgsql_escape_string($text) {
1616 return pg_escape_string($text);
1617 }
1618
1619@@ -368,7 +330,7 @@ function db_escape_string($text) {
1620 * Lock a table.
1621 * This function automatically starts a transaction.
1622 */
1623-function db_lock_table($table) {
1624+function db_pgsql_lock_table($table) {
1625 db_query('BEGIN; LOCK TABLE {'. db_escape_table($table) .'} IN EXCLUSIVE MODE');
1626 }
1627
1628@@ -376,7 +338,7 @@ function db_lock_table($table) {
1629 * Unlock all locked tables.
1630 * This function automatically commits a transaction.
1631 */
1632-function db_unlock_tables() {
1633+function db_pgsql_unlock_tables() {
1634 db_query('COMMIT');
1635 }
1636
1637@@ -389,7 +351,7 @@ function db_unlock_tables() {
1638 * @return
1639 * TRUE if the table exists, and FALSE if the table does not exist.
1640 */
1641-function db_table_exists($table) {
1642+function db_pgsql_table_exists($table) {
1643 return (bool) db_result(db_query("SELECT COUNT(*) FROM pg_class WHERE relname = '{". db_escape_table($table) ."}'"));
1644 }
1645
1646@@ -404,14 +366,14 @@ function db_table_exists($table) {
1647 * @return
1648 * TRUE if the column exists, and FALSE if the column does not exist.
1649 */
1650-function db_column_exists($table, $column) {
1651+function db_pgsql_column_exists($table, $column) {
1652 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) ."'"));
1653 }
1654
1655 /**
1656 * Verify if the database is set up correctly.
1657 */
1658-function db_check_setup() {
1659+function db_pgsql_check_setup() {
1660 $t = get_t();
1661
1662 $encoding = db_result(db_query('SHOW server_encoding'));
1663@@ -433,7 +395,7 @@ function db_check_setup() {
1664 * This maps a generic data type in combination with its data size
1665 * to the engine-specific data type.
1666 */
1667-function db_type_map() {
1668+function db_pgsql_type_map() {
1669 // Put :normal last so it gets preserved by array_flip. This makes
1670 // it much easier for modules (such as schema.module) to map
1671 // database types back into schema types.
1672@@ -485,7 +447,7 @@ function db_type_map() {
1673 * @return
1674 * An array of SQL statements to create the table.
1675 */
1676-function db_create_table_sql($name, $table) {
1677+function db_pgsql_create_table_sql($name, $table) {
1678 $sql_fields = array();
1679 foreach ($table['fields'] as $field_name => $field) {
1680 $sql_fields[] = _db_create_field_sql($field_name, _db_process_field($field));
1681@@ -525,7 +487,7 @@ function _db_create_index_sql($table, $n
1682 return $query;
1683 }
1684
1685-function _db_create_key_sql($fields) {
1686+function _db_pgsql_create_key_sql($fields) {
1687 $ret = array();
1688 foreach ($fields as $field) {
1689 if (is_array($field)) {
1690@@ -560,7 +522,7 @@ function _db_create_keys(&$ret, $table,
1691 * @param $field
1692 * A field description array, as specified in the schema documentation.
1693 */
1694-function _db_process_field($field) {
1695+function _db_pgsql_process_field($field) {
1696 if (!isset($field['size'])) {
1697 $field['size'] = 'normal';
1698 }
1699@@ -586,7 +548,7 @@ function _db_process_field($field) {
1700 * @param $spec
1701 * The field specification, as per the schema data structure format.
1702 */
1703-function _db_create_field_sql($name, $spec) {
1704+function _db_pgsql_create_field_sql($name, $spec) {
1705 $sql = $name .' '. $spec['pgsql_type'];
1706
1707 if ($spec['type'] == 'serial') {
1708@@ -616,32 +578,6 @@ function _db_create_field_sql($name, $sp
1709 }
1710
1711 /**
1712- * Rename a table.
1713- *
1714- * @param $ret
1715- * Array to which query results will be added.
1716- * @param $table
1717- * The table to be renamed.
1718- * @param $new_name
1719- * The new name for the table.
1720- */
1721-function db_rename_table(&$ret, $table, $new_name) {
1722- $ret[] = update_sql('ALTER TABLE {'. $table .'} RENAME TO {'. $new_name .'}');
1723-}
1724-
1725-/**
1726- * Drop a table.
1727- *
1728- * @param $ret
1729- * Array to which query results will be added.
1730- * @param $table
1731- * The table to be dropped.
1732- */
1733-function db_drop_table(&$ret, $table) {
1734- $ret[] = update_sql('DROP TABLE {'. $table .'}');
1735-}
1736-
1737-/**
1738 * Add a new field to a table.
1739 *
1740 * @param $ret
1741@@ -664,7 +600,7 @@ function db_drop_table(&$ret, $table) {
1742 * or index including it in this array. See db_change_field() for more
1743 * explanation why.
1744 */
1745-function db_add_field(&$ret, $table, $field, $spec, $new_keys = array()) {
1746+function db_pgsql_add_field(&$ret, $table, $field, $spec, $new_keys = array()) {
1747 $fixnull = FALSE;
1748 if (!empty($spec['not null']) && !isset($spec['default'])) {
1749 $fixnull = TRUE;
1750@@ -697,48 +633,11 @@ function db_add_field(&$ret, $table, $fi
1751 * @param $field
1752 * The field to be dropped.
1753 */
1754-function db_drop_field(&$ret, $table, $field) {
1755+function db_pgsql_drop_field(&$ret, $table, $field) {
1756 $ret[] = update_sql('ALTER TABLE {'. $table .'} DROP COLUMN '. $field);
1757 }
1758
1759 /**
1760- * Set the default value for a field.
1761- *
1762- * @param $ret
1763- * Array to which query results will be added.
1764- * @param $table
1765- * The table to be altered.
1766- * @param $field
1767- * The field to be altered.
1768- * @param $default
1769- * Default value to be set. NULL for 'default NULL'.
1770- */
1771-function db_field_set_default(&$ret, $table, $field, $default) {
1772- if ($default == NULL) {
1773- $default = 'NULL';
1774- }
1775- else {
1776- $default = is_string($default) ? "'$default'" : $default;
1777- }
1778-
1779- $ret[] = update_sql('ALTER TABLE {'. $table .'} ALTER COLUMN '. $field .' SET DEFAULT '. $default);
1780-}
1781-
1782-/**
1783- * Set a field to have no default value.
1784- *
1785- * @param $ret
1786- * Array to which query results will be added.
1787- * @param $table
1788- * The table to be altered.
1789- * @param $field
1790- * The field to be altered.
1791- */
1792-function db_field_set_no_default(&$ret, $table, $field) {
1793- $ret[] = update_sql('ALTER TABLE {'. $table .'} ALTER COLUMN '. $field .' DROP DEFAULT');
1794-}
1795-
1796-/**
1797 * Add a primary key.
1798 *
1799 * @param $ret
1800@@ -748,7 +647,7 @@ function db_field_set_no_default(&$ret,
1801 * @param $fields
1802 * Fields for the primary key.
1803 */
1804-function db_add_primary_key(&$ret, $table, $fields) {
1805+function db_pgsql_add_primary_key(&$ret, $table, $fields) {
1806 $ret[] = update_sql('ALTER TABLE {'. $table .'} ADD PRIMARY KEY ('.
1807 implode(',', $fields) .')');
1808 }
1809@@ -761,7 +660,7 @@ function db_add_primary_key(&$ret, $tabl
1810 * @param $table
1811 * The table to be altered.
1812 */
1813-function db_drop_primary_key(&$ret, $table) {
1814+function db_pgsql_drop_primary_key(&$ret, $table) {
1815 $ret[] = update_sql('ALTER TABLE {'. $table .'} DROP CONSTRAINT {'. $table .'}_pkey');
1816 }
1817
1818@@ -777,7 +676,7 @@ function db_drop_primary_key(&$ret, $tab
1819 * @param $fields
1820 * An array of field names.
1821 */
1822-function db_add_unique_key(&$ret, $table, $name, $fields) {
1823+function db_pgsql_add_unique_key(&$ret, $table, $name, $fields) {
1824 $name = '{'. $table .'}_'. $name .'_key';
1825 $ret[] = update_sql('ALTER TABLE {'. $table .'} ADD CONSTRAINT '.
1826 $name .' UNIQUE ('. implode(',', $fields) .')');
1827@@ -793,7 +692,7 @@ function db_add_unique_key(&$ret, $table
1828 * @param $name
1829 * The name of the key.
1830 */
1831-function db_drop_unique_key(&$ret, $table, $name) {
1832+function db_pgsql_drop_unique_key(&$ret, $table, $name) {
1833 $name = '{'. $table .'}_'. $name .'_key';
1834 $ret[] = update_sql('ALTER TABLE {'. $table .'} DROP CONSTRAINT '. $name);
1835 }
1836@@ -810,7 +709,7 @@ function db_drop_unique_key(&$ret, $tabl
1837 * @param $fields
1838 * An array of field names.
1839 */
1840-function db_add_index(&$ret, $table, $name, $fields) {
1841+function db_pgsql_add_index(&$ret, $table, $name, $fields) {
1842 $ret[] = update_sql(_db_create_index_sql($table, $name, $fields));
1843 }
1844
1845@@ -824,7 +723,7 @@ function db_add_index(&$ret, $table, $na
1846 * @param $name
1847 * The name of the index.
1848 */
1849-function db_drop_index(&$ret, $table, $name) {
1850+function db_pgsql_drop_index(&$ret, $table, $name) {
1851 $name = '{'. $table .'}_'. $name .'_idx';
1852 $ret[] = update_sql('DROP INDEX '. $name);
1853 }
1854@@ -891,7 +790,7 @@ function db_drop_index(&$ret, $table, $n
1855 * table along with changing the field. The format is the same as a
1856 * table specification but without the 'fields' element.
1857 */
1858-function db_change_field(&$ret, $table, $field, $field_new, $spec, $new_keys = array()) {
1859+function db_pgsql_change_field(&$ret, $table, $field, $field_new, $spec, $new_keys = array()) {
1860 $ret[] = update_sql('ALTER TABLE {'. $table .'} RENAME "'. $field .'" TO "'. $field .'_old"');
1861 $not_null = isset($spec['not null']) ? $spec['not null'] : FALSE;
1862 unset($spec['not null']);