Index: includes/cache-install.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/cache-install.inc,v
retrieving revision 1.7
diff -u -r1.7 cache-install.inc
--- includes/cache-install.inc	30 Dec 2009 08:16:55 -0000	1.7
+++ includes/cache-install.inc	26 Feb 2010 06:58:18 -0000
@@ -41,8 +41,7 @@
     // subtle bugs, some of which would not be fixed unless the site
     // administrator cleared the cache manually.
     try {
-      if (function_exists('drupal_install_initialize_database')) {
-        drupal_install_initialize_database();
+      if (class_exists('Database')) {
         parent::clear($cid, $wildcard);
       }
     }
Index: includes/install.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/install.inc,v
retrieving revision 1.127
diff -u -r1.127 install.inc
--- includes/install.inc	3 Feb 2010 18:16:23 -0000	1.127
+++ includes/install.inc	26 Feb 2010 06:58:18 -0000
@@ -214,20 +214,16 @@
 function drupal_detect_database_types() {
   $databases = array();
 
-  // Initialize the database system if it has not been
-  // initialized yet. We do not initialize it earlier to make
-  // requirements check during the installation.
-  require_once DRUPAL_ROOT . '/includes/database/database.inc';
-
   // We define a driver as a directory in /includes/database that in turn
   // contains a database.inc file. That allows us to drop in additional drivers
   // without modifying the installer.
   // Because we have no registry yet, we need to also include the install.inc
   // file for the driver explicitly.
   require_once DRUPAL_ROOT . '/includes/database/database.inc';
+  spl_autoload_register('db_autoload');
   foreach (file_scan_directory(DRUPAL_ROOT . '/includes/database', '/^[a-z]*$/i', array('recurse' => FALSE)) as $file) {
-    include_once "{$file->uri}/install.inc";
-    include_once "{$file->uri}/database.inc";
+    //include_once "{$file->uri}/install.inc";
+    //include_once "{$file->uri}/database.inc";
     $drivers[$file->filename] = $file->uri;
   }
 
@@ -567,28 +563,6 @@
 }
 
 /**
- * Manually include all files for the active database.
- *
- * Because we have no registry yet, we need to manually include the
- * necessary database include files.
- */
-function drupal_install_initialize_database() {
-  static $included = FALSE;
-
-  if (!$included) {
-    $connection_info = Database::getConnectionInfo();
-    $driver = $connection_info['default']['driver'];
-    require_once DRUPAL_ROOT . '/includes/database/query.inc';
-    require_once DRUPAL_ROOT . '/includes/database/select.inc';
-    require_once DRUPAL_ROOT . '/includes/database/schema.inc';
-    foreach (glob(DRUPAL_ROOT . '/includes/database/' . $driver . '/*.inc') as $include_file) {
-      require_once $include_file;
-    }
-    $included = TRUE;
-  }
-}
-
-/**
  * Callback to install the system module.
  *
  * Separated from the installation of other modules so core system
@@ -597,7 +571,6 @@
 function drupal_install_system() {
   $system_path = dirname(drupal_get_filename('module', 'system', NULL));
   require_once DRUPAL_ROOT . '/' . $system_path . '/system.install';
-  drupal_install_initialize_database();
   module_invoke('system', 'install');
 
   $system_versions = drupal_get_schema_versions('system');
Index: includes/bootstrap.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/bootstrap.inc,v
retrieving revision 1.355
diff -u -r1.355 bootstrap.inc
--- includes/bootstrap.inc	25 Feb 2010 09:13:00 -0000	1.355
+++ includes/bootstrap.inc	26 Feb 2010 06:58:18 -0000
@@ -1934,6 +1934,10 @@
   // Set Drupal's watchdog as the logging callback.
   Database::setLoggingCallback('watchdog', WATCHDOG_NOTICE, WATCHDOG_ERROR);
 
+  // Register the Database-specific autoload routing after the registry-based
+  // ones, so that they take precedence.  This is here mostly as a fallback.
+  spl_autoload_register('db_autoload');
+
   // Register autoload functions so that we can access classes and interfaces.
   spl_autoload_register('drupal_autoload_class');
   spl_autoload_register('drupal_autoload_interface');
@@ -2374,7 +2378,7 @@
  */
 function _registry_check_code($type, $name = NULL) {
   static $lookup_cache, $cache_update_needed;
-
+//debug("Including $name from the registry");
   if ($type == 'class' && class_exists($name) || $type == 'interface' && interface_exists($name)) {
     return TRUE;
   }
Index: includes/update.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/update.inc,v
retrieving revision 1.38
diff -u -r1.38 update.inc
--- includes/update.inc	25 Feb 2010 09:42:39 -0000	1.38
+++ includes/update.inc	26 Feb 2010 06:58:18 -0000
@@ -99,9 +99,9 @@
   // Allow the database system to work even if the registry has not been
   // created yet.
   drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE);
-  drupal_install_initialize_database();
-  spl_autoload_unregister('drupal_autoload_class');
-  spl_autoload_unregister('drupal_autoload_interface');
+  //drupal_install_initialize_database();
+  //spl_autoload_unregister('drupal_autoload_class');
+  //spl_autoload_unregister('drupal_autoload_interface');
 
   // The new cache_bootstrap bin is required to bootstrap to
   // DRUPAL_BOOTSTRAP_SESSION, so create it here rather than in
Index: includes/install.core.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/install.core.inc,v
retrieving revision 1.2
diff -u -r1.2 install.core.inc
--- includes/install.core.inc	26 Feb 2010 06:39:13 -0000	1.2
+++ includes/install.core.inc	26 Feb 2010 06:58:18 -0000
@@ -278,6 +278,7 @@
     // Initialize the database system. Note that the connection
     // won't be initialized until it is actually requested.
     require_once DRUPAL_ROOT . '/includes/database/database.inc';
+    spl_autoload_register('db_autoload');
 
     // Verify the last completed task in the database, if there is one.
     $task = install_verify_completed_task();
@@ -349,7 +350,6 @@
       $install_state['tasks_performed'][] = $task_name;
       $install_state['installation_finished'] = empty($tasks_to_perform);
       if ($install_state['database_tables_exist'] && ($task['run'] == INSTALL_TASK_RUN_IF_NOT_COMPLETED || $install_state['installation_finished'])) {
-        drupal_install_initialize_database();
         variable_set('install_task', $install_state['installation_finished'] ? 'done' : $task_name);
       }
     }
@@ -1311,7 +1311,6 @@
 function install_bootstrap_full(&$install_state) {
   // Bootstrap newly installed Drupal, while preserving existing messages.
   $messages = isset($_SESSION['messages']) ? $_SESSION['messages'] : '';
-  drupal_install_initialize_database();
 
   drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
   $_SESSION['messages'] = $messages;
Index: includes/theme.maintenance.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/theme.maintenance.inc,v
retrieving revision 1.54
diff -u -r1.54 theme.maintenance.inc
--- includes/theme.maintenance.inc	25 Feb 2010 20:57:39 -0000	1.54
+++ includes/theme.maintenance.inc	26 Feb 2010 06:58:18 -0000
@@ -39,6 +39,7 @@
       // Because we are operating in a crippled environment, we need to
       // bootstrap just enough to allow hook invocations to work.
       require_once DRUPAL_ROOT . '/includes/database/database.inc';
+      spl_autoload_register('db_autoload');
       $module_list['system']['filename'] = 'modules/system/system.module';
       module_list(TRUE, FALSE, FALSE, $module_list);
       drupal_load('module', 'system');
Index: includes/database/database.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/database/database.inc,v
retrieving revision 1.97
diff -u -r1.97 database.inc
--- includes/database/database.inc	15 Feb 2010 22:12:27 -0000	1.97
+++ includes/database/database.inc	26 Feb 2010 06:58:19 -0000
@@ -791,7 +791,10 @@
   public function schema() {
     if (empty($this->schema)) {
       $class_type = 'DatabaseSchema_' . $this->driver();
-      $this->schema = new $class_type($this);
+      debug(spl_autoload_functions());
+      if (class_exists($class_type)) {
+        $this->schema = new $class_type($this);
+      }
     }
     return $this->schema;
   }
@@ -2056,6 +2059,83 @@
 
 
 /**
+ * Autoload callback for the database system.
+ *
+ * In most cases this function will not be needed, as the registry
+ * will find classes for us.  However, in some cases, such as the installer,
+ * the registry may not be available.  We therefore need to have this
+ * autoload implementation available so that the database system
+ * is not dependent on having a working database.
+ */
+function db_autoload($class) {
+  static $base_path = '';
+  static $checked = array();
+//dpm($class);
+if (!in_array($class, array('FirePHP'))) {
+  debug($class);
+}
+  static $files = array(
+    'query.inc' => array(
+      'QueryPlaceholderInterface',
+      'QueryConditionInterface', 'DatabaseCondition',
+      'Query', 'DeleteQuery', 'InsertQuery', 'UpdateQuery', 'MergeQuery', 'TruncateQuery',
+      'QueryAlterableInterface',
+    ),
+    'select.inc' => array('QueryAlterableInterface', 'SelectQueryInterface', 'SelectQuery', 'SelectQueryExtender'),
+    'database.inc' => array('DatabaseConnection'),
+    'log.inc' => array('DatabaseLog'),
+    'prefetch.inc' => array('DatabaseStatementPrefetch'),
+    'schema.inc' => array('DatabaseSchema'),
+  );
+
+  // If a class doesn't exist, it may get checked a second time
+  // by class_exists().  If so, just bail out now.
+  if (isset($checked[$class])) {
+    return;
+  }
+  $checked[$class] = TRUE;
+
+  if (empty($base_path)) {
+    $base_path = dirname(realpath(__FILE__));
+  }
+
+  // If there is an underscore in the class name, we know it's a
+  // driver-specific file so check for those.  If not, it's a generic.
+  // Note that we use require_once here instead of require because of a
+  // quirk in class_exists().  By default, class_exists() will try to
+  // autoload a class if it's not found.  However, we cannot tell
+  // at this point whether or not the class is going to exist, only
+  // the file that it would be in if it does exist.  That means we may
+  // try to include a file that was already included by another
+  // autoload call, which would break.  Using require_once() neatly
+  // avoids that issue.
+  if (strpos($class, '_') !== FALSE) {
+    list($base, $driver) = explode('_', $class);
+
+    // Drivers have an extra file, and may put their SelectQuery implementation
+    // in the main query file since it's so small.
+    $driver_files = $files;
+    $driver_files['query.inc'][] = 'SelectQuery';
+    $driver_files['install.inc'] = array('DatabaseTasks');
+
+    foreach ($driver_files as $file => $classes) {
+      if (in_array($base, $classes)) {
+        require_once "{$base_path}/{$driver}/{$file}";
+        return;
+      }
+    }
+  }
+  else {
+    foreach ($files as $file => $classes) {
+      if (in_array($class, $classes)) {
+        require_once $base_path . '/' . $file;
+        return;
+      }
+    }
+  }
+}
+
+/**
  * The following utility functions are simply convenience wrappers.
  * They should never, ever have any database-specific code in them.
  */
Index: modules/simpletest/drupal_web_test_case.php
===================================================================
RCS file: /cvs/drupal/drupal/modules/simpletest/drupal_web_test_case.php,v
retrieving revision 1.197
diff -u -r1.197 drupal_web_test_case.php
--- modules/simpletest/drupal_web_test_case.php	25 Feb 2010 16:03:47 -0000	1.197
+++ modules/simpletest/drupal_web_test_case.php	26 Feb 2010 06:58:19 -0000
@@ -571,6 +571,8 @@
     $this->originalPrefix = $db_prefix;
     $this->originalFileDirectory = file_directory_path();
 
+    spl_autoload_register('db_autoload');
+
     // Reset all statics so that test is performed with a clean environment.
     drupal_static_reset();
 
Index: modules/simpletest/simpletest.pages.inc
===================================================================
RCS file: /cvs/drupal/drupal/modules/simpletest/simpletest.pages.inc,v
retrieving revision 1.25
diff -u -r1.25 simpletest.pages.inc
--- modules/simpletest/simpletest.pages.inc	7 Feb 2010 05:36:57 -0000	1.25
+++ modules/simpletest/simpletest.pages.inc	26 Feb 2010 06:58:19 -0000
@@ -185,7 +185,9 @@
   // Get list of tests.
   $tests_list = array();
   foreach ($form_state['values'] as $class_name => $value) {
-    if (class_exists($class_name) && $value === 1) {
+  	// Since class_exists() will likely trigger an autoload lookup,
+  	// we do the fast check first.
+    if ($value === 1 && class_exists($class_name)) {
       $tests_list[] = $class_name;
     }
   }
Index: modules/system/system.install
===================================================================
RCS file: /cvs/drupal/drupal/modules/system/system.install,v
retrieving revision 1.449
diff -u -r1.449 system.install
--- modules/system/system.install	26 Feb 2010 06:39:13 -0000	1.449
+++ modules/system/system.install	26 Feb 2010 06:58:19 -0000
@@ -394,6 +394,7 @@
   // Create tables.
   $modules = array('system', 'filter', 'user', 'node');
   foreach ($modules as $module) {
+  	debug("Installing $module");
     drupal_install_schema($module);
     $versions = drupal_get_schema_versions($module);
     $version = $versions ? max($versions) : SCHEMA_INSTALLED;
