Index: demo.info
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/demo/demo.info,v
retrieving revision 1.1.2.1
diff -u -r1.1.2.1 demo.info
--- demo.info	18 Jun 2007 23:06:36 -0000	1.1.2.1
+++ demo.info	29 Jul 2007 21:53:00 -0000
@@ -1,6 +1,6 @@
 ; $Id: demo.info,v 1.1.2.1 2007/06/18 23:06:36 dww Exp $
 name = Demo Site
-description = "Resets the site for demonstration purposes."
-dependencies = dba drupal
+description = "Create snapshots and reset the site for demonstration purposes."
+dependencies = drupal
 package = Development
 
Index: demo.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/demo/demo.module,v
retrieving revision 1.1.2.9
diff -u -r1.1.2.9 demo.module
--- demo.module	20 Jul 2007 21:14:27 -0000	1.1.2.9
+++ demo.module	29 Jul 2007 22:16:26 -0000
@@ -219,6 +219,8 @@
 }
 
 function demo_dump_submit($form_id, $values) {
+  require_once drupal_get_path('module', 'demo') .'/sql_dump.inc';
+
   // Write .info file
   $info = demo_set_info($values);
   if (!$info) {
@@ -227,14 +229,7 @@
   
   // 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');
 }
@@ -284,6 +279,8 @@
 }
 
 function demo_reset($filename = 'demo_site', $verbose = true) {
+  require_once drupal_get_path('module', 'demo') .'/sql_dump.inc';
+
   // Load any database information in front of reset.
   $demo_dump_cron = variable_get('demo_dump_cron', $filename);
   $fileconfig = demo_get_fileconfig($filename);
@@ -291,11 +288,11 @@
   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');
+      $tables = demo_get_db_tables();
       
       // Drop those tables.
       foreach ($tables as $table) {
-        module_invoke('dba', 'drop_table', $table, false);
+        db_query("DROP TABLE %s", $table);
       }
       
       // Load data from snapshot
@@ -399,7 +396,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']);
--- D:/htdocs/test/drupal/drupal-5.0/sites/all/modules/demo/sql_dump.inc
+++ D:/htdocs/test/drupal/drupal-5.0/sites/all/modules/demo/sql_dump.inc
@@ -0,0 +1,172 @@
+<?php
+// $Id$
+
+/**
+ * Return all tables in active MySQL database as array.
+ */
+function demo_get_db_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;
+}
+
+function demo_dump_db($filename) {
+  switch ($GLOBALS['db_type']) {
+  	case 'mysql':
+  	case 'mysqli':
+      return _demo_mysql_dump_db($filename);
+
+  	case 'pgsql':
+      // @todo Postgres support should utilize pg_dump. See phpPgAdmin's
+      // dbexport.php for how to do it.
+      drupal_set_message(t('PostgreSQL support not implemented yet.'), 'error');
+      return FALSE;
+  }
+}
+
+function _demo_mysql_dump_db($filename) {
+  // Make sure we have permission to save our backup file.
+  if (file_check_directory(dirname($filename), FILE_CREATE_DIRECTORY) && ($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_get_db_tables() as $table) {
+      fwrite($fp, _demo_mysql_dump_table($table));
+    }
+
+    fclose($fp);
+    return TRUE;
+  }
+
+  return FALSE;
+}
+
+/**
+ * Return name of active MySQL 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 MySQL table.
+ */
+function _demo_mysql_dump_table($table) {
+  $output = "--\n";
+  $output .= "-- Table structure for table '$table'\n";
+  $output .= "--\n\n";
+
+  $output .= _demo_mysql_show_create_table($table) .";\n\n";
+
+  $output .= "--\n";
+  $output .= "-- Dumping data for table '$table'\n";
+  $output .= "--\n\n";
+  
+  // Get table fields.
+  $fields = _demo_mysql_table_fields($table);
+  $num_fields = count($fields);
+
+  // Dump table data.
+  $result = db_query("SELECT * FROM %s", $table);
+  while ($row = db_fetch_array($result)) {
+    $line = "INSERT INTO $table VALUES(";
+    $i = 0;
+    foreach ($row as $value) {
+      if (!isset($value) || is_null($value)) {
+        $line .= 'NULL';
+      } else if (!empty($value) && ($fields[$i]->Type == 'longblob' || $fields[$i]->Type == 'blob')) {
+        $line .= '0x'. bin2hex($value);
+      }
+      else if (is_numeric($value)) {
+        $line .= $value;
+      }
+      else {
+        // Escape backslashes first
+        $value = str_replace('\\', '\\\\', $value);
+        // Escape newlines and carriage returns
+        $value = str_replace("\n", "\\n", $value);
+        $value = str_replace("\r", "\\r", $value);
+        // Escape single quotes
+        // This works in Oracle, PostGreSQL and MySQL for sure.
+        $value = str_replace("'", "''", $value);
+        
+        $line .= "'". $value ."'";
+      }
+      $line .= (++$i < $num_fields) ? ', ' : ");\n";
+    }
+    $output .= $line;
+  }
+  return $output;
+}
+
+/**
+ * Return CREATE TABLE definition.
+ */
+function _demo_mysql_show_create_table($table) {
+  $create = db_fetch_array(db_query("SHOW CREATE TABLE %s", $table));
+  return $create['Create Table'];
+}
+
+/**
+ * Return list of table fields.
+ */
+function _demo_mysql_table_fields($table) {
+  $fields = array();
+  $result = db_query("DESCRIBE %s", $table);
+  while ($field = db_fetch_object($result)) {
+    $fields[] = $field;
+  }
+  return $fields;
+}
+


