Index: demo.info
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/demo/demo.info,v
retrieving revision 1.1.2.3
diff -u -r1.1.2.3 demo.info
--- demo.info	10 Sep 2007 20:12:14 -0000	1.1.2.3
+++ demo.info	12 Sep 2007 12:20:24 -0000
@@ -1,6 +1,5 @@
 ; $Id: demo.info,v 1.1.2.3 2007/09/10 20:12:14 smk Exp $
 name = "Demo Site"
 description = "Create snapshots and reset the site for demonstration or testing purposes."
-dependencies = dba
 package = Development
 
Index: demo.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/demo/demo.module,v
retrieving revision 1.1.2.10
diff -u -r1.1.2.10 demo.module
--- demo.module	10 Sep 2007 20:12:14 -0000	1.1.2.10
+++ demo.module	12 Sep 2007 12:20:36 -0000
@@ -226,22 +226,28 @@
 }
 
 function demo_dump_submit($form_id, $values) {
+  global $db_type;
+
   // Write .info file
   $info = demo_set_info($values);
   if (!$info) {
     return false;
   }
   
+  // Include database specific functions
+  switch ($db_type) {
+  	case 'mysqli':
+      $engine = 'mysql';
+  		break;
+  	default:
+      $engine = $db_type;
+  		break;
+  }
+  require_once drupal_get_path('module', 'demo') ."/database_{$engine}_dump.inc";
+
   // Perform dump
   $fileconfig = demo_get_fileconfig($info['filename']);
-  module_invoke('dba', 'auto_backup',
-    $fileconfig['path'] . $fileconfig['site'],
-    $fileconfig['sql'],
-    array(),
-    false,
-    false,
-    false
-  );
+  demo_dump_db($fileconfig['sqlfile']);
   
   drupal_goto('admin/settings/demo/manage');
 }
@@ -297,12 +303,9 @@
 
   if (file_exists($fileconfig['sqlfile'])) {
     if ($fp = fopen($fileconfig['sqlfile'], 'r')) {
-      // Fetch list of all tables of this installation (dba deals with prefixes).
-      $tables = module_invoke('dba', 'get_tables');
-      
-      // Drop those tables.
-      foreach ($tables as $table) {
-        module_invoke('dba', 'drop_table', $table, false);
+      // Drop all tables.
+      foreach (demo_enum_tables() as $table) {
+        db_query("DROP TABLE %s", $table);
       }
       
       // Load data from snapshot
@@ -406,7 +409,7 @@
     }
     if (count($info['modules']) > 1) {
       // Remove required core modules and obvious modules from module list.
-      $info['modules'] = array_diff($info['modules'], array('block', 'filter', 'node', 'system', 'user', 'watchdog', 'demo', 'dba'));
+      $info['modules'] = array_diff($info['modules'], array('block', 'filter', 'node', 'system', 'user', 'watchdog', 'demo'));
       // Sort module list alphabetically.
       sort($info['modules']);
       $option['#description'] .= t('Modules: ') . implode(', ', $info['modules']);
@@ -482,6 +485,61 @@
 }
 
 /**
+ * Returns a list of tables in the active database.
+ *
+ * Only returns tables whose prefix matches the configured one (or ones, if
+ * there are multiple).
+ */
+function demo_enum_tables() {
+  global $db_prefix;
+
+  $tables = array();
+
+  if (is_array($db_prefix)) {
+    // Create a regular expression for table prefix matching.
+    $rx = '/^('. implode('|', array_filter($db_prefix)) .')/';
+  }
+  else if ($db_prefix != '') {
+    $rx = '/^'. $db_prefix .'/';
+  }
+
+  switch ($GLOBALS['db_type']) {
+  	case 'mysql':
+  	case 'mysqli':
+      $result = db_query("SHOW TABLES");
+      break;
+  	case 'pgsql':
+      $result = db_query("SELECT table_name FROM information_schema.tables WHERE table_schema = '%s'", 'public');
+      break;
+  }
+
+  while ($table = db_fetch_array($result)) {
+    $table = reset($table);
+    if (is_array($db_prefix)) {
+      // Check if table matches any configured prefix.
+      if (preg_match($rx, $table)) {
+        // One arbitrary prefix matched, now let's find out which one.
+        $plain_table = preg_replace($rx, '', $table);
+        $table_prefix = substr($table, 0, strlen($table) - strlen($plain_table));
+        if ($db_prefix[$plain_table] == $table_prefix) {
+          $tables[] = $table;
+        }
+      }
+    }
+    else if ($db_prefix != '') {
+      if (preg_match($rx, $table)) {
+        $tables[] = $table;
+      }
+    }
+    else {
+      $tables[] = $table;
+    }
+  }
+
+  return $tables;
+}
+
+/**
  * Implementation of hook_cron().
  */
 function demo_cron() {
--- D:/htdocs/test/drupal/drupal-5.0/sites/all/modules/demo/database_mysql_dump.inc
+++ D:/htdocs/test/drupal/drupal-5.0/sites/all/modules/demo/database_mysql_dump.inc
@@ -0,0 +1,133 @@
+<?php
+// $Id$
+
+/**
+ * Dump active database.
+ */
+function demo_dump_db($filename) {
+  // Make sure we have permission to save our backup file.
+  if (!file_check_directory(dirname($filename), FILE_CREATE_DIRECTORY)) {
+    return FALSE;
+  }
+
+  if ($fp = fopen($filename, 'wb')) {
+    $database = _demo_get_database();
+
+    $header  = "-- Demo.module database dump\n";
+    $header .= "-- http://drupal.org/project/demo\n";
+    $header .= "--\n";
+    $header .= "-- Database: $database\n";
+    $header .= "-- Date: ". format_date(time(), 'large') ."\n\n";
+    fwrite($fp, $header);
+
+    foreach (demo_enum_tables() as $table) {
+      fwrite($fp, _demo_dump_table($table));
+    }
+
+    fclose($fp);
+    return TRUE;
+  }
+
+  return FALSE;
+}
+
+/**
+ * Returns the name of the active database.
+ */
+function _demo_get_database() {
+  $database = array_keys(db_fetch_array(db_query('SHOW TABLES')));
+  $database = preg_replace('/^Tables_in_/', '', $database[0]);
+  return $database;
+}
+
+/**
+ * Dump a table.
+ *
+ * This code has been stolen from the phpMyAdmin project.
+ *
+ * @todo Support extended inserts (much faster).
+ */
+function _demo_dump_table($table) {
+  $output = "--\n";
+  $output .= "-- Table structure for table '$table'\n";
+  $output .= "--\n\n";
+
+  $output .= _demo_show_create_table($table) .";\n\n";
+
+  $output .= "--\n";
+  $output .= "-- Dumping data for table '$table'\n";
+  $output .= "--\n\n";
+  
+  // Dump table data.
+  $result = db_query("SELECT * FROM %s", $table);
+
+  // Get table fields.
+  $fields = _demo_get_fields($result);
+  $num_fields = count($fields);
+
+  // Escape backslashes, PHP code, special chars
+  $search = array('\\', "'", "\x00", "\x0a", "\x0d", "\x1a");
+  $replace = array('\\\\', "''", '\0', '\n', '\r', '\Z');
+
+  while ($row = db_fetch_array($result)) {
+    $i = 0;
+    $values = array();
+    foreach ($row as $value) {
+      // NULL
+      if (!isset($value) || is_null($value)) {
+        $values[] = 'NULL';
+      }
+      // A number
+      // timestamp is numeric on some MySQL 4.1, BLOBs are sometimes numeric
+      else if ($fields['meta'][$i]->numeric
+        && $fields['meta'][$i]->type != 'timestamp'
+        && !($fields['meta'][$i]->blob)) {
+        $values[] = $value;
+      }
+      // A binary field
+      // Note: with mysqli, under MySQL 4.1.3, we get the flag
+      // "binary" for those field types (I don't know why)
+      else if (stristr($fields['flags'][$i], 'binary')
+        && $fields['meta'][$i]->type != 'datetime'
+        && $fields['meta'][$i]->type != 'date'
+        && $fields['meta'][$i]->type != 'time'
+        && $fields['meta'][$i]->type != 'timestamp') {
+        // Empty blobs need to be different, but '0' is also empty :-(
+        if (empty($value) && $value != '0') {
+            $values[] = "''";
+        } else {
+            $values[] = '0x' . bin2hex($value);
+        }
+      }
+      // Something else -> treat as a string
+      else {
+        $values[] = "'". str_replace($search, $replace, $value) ."'";
+      }
+      $i++;
+    }
+    $output .= "INSERT INTO `$table` VALUES (". implode(', ', $values) . ");\n";
+  }
+  return $output;
+}
+
+/**
+ * Return CREATE TABLE definition.
+ */
+function _demo_show_create_table($table) {
+  $create = db_fetch_array(db_query("SHOW CREATE TABLE %s", $table));
+  return $create['Create Table'];
+}
+
+/**
+ * Return table fields and their properties.
+ */
+function _demo_get_fields($result) {
+  $fields = array();
+  $num_fields = mysql_num_fields($result);
+  for ($i = 0; $i < $num_fields; $i++) {
+    $fields['meta'][] = mysql_fetch_field($result, $i);
+    $fields['flags'][] = mysql_field_flags($result, $i);
+  }
+  return $fields;
+}
+

--- D:/htdocs/test/drupal/drupal-5.0/sites/all/modules/demo/database_pgsql_dump.inc
+++ D:/htdocs/test/drupal/drupal-5.0/sites/all/modules/demo/database_pgsql_dump.inc
@@ -0,0 +1,14 @@
+<?php
+// $Id$
+
+/**
+ * Dump active database.
+ *
+ * @todo Postgres should utilize pg_dump. See phpPgAdmin's dbexport.php
+ * for how to do it.
+ */
+function demo_dump_db($filename) {
+  drupal_set_message(t('PostgreSQL support not implemented yet.'), 'error');
+  return FALSE;
+}
+


