### Eclipse Workspace Patch 1.0
#P Drupal FHead (MSSQL)
Index: modules/locale/locale.install
===================================================================
RCS file: /cvs/drupal/drupal/modules/locale/locale.install,v
retrieving revision 1.7
diff -u -r1.7 locale.install
--- modules/locale/locale.install	14 Nov 2006 06:20:40 -0000	1.7
+++ modules/locale/locale.install	1 Dec 2006 00:21:01 -0000
@@ -73,7 +73,33 @@
       db_query("CREATE INDEX {locales_target}_plural_idx ON {locales_target} (plural)");
       db_query("CREATE INDEX {locales_source}_source_idx ON {locales_source} (source)");
       break;
+    case 'mssql':
+      db_query("CREATE TABLE {locales_meta} (
+        locale varchar(12) NOT NULL default '',
+        name varchar(64) NOT NULL default '',
+        enabled int NOT NULL default '0',
+        isdefault int NOT NULL default '0',
+        plurals int NOT NULL default '0',
+        formula varchar(128) NOT NULL default '',
+        PRIMARY KEY (locale)
+      );");
+      db_query("CREATE TABLE {locales_source} (
+        lid int NOT NULL identity(1,1),
+        location varchar(255) NOT NULL default '',
+        source varchar(max) NOT NULL,
+        PRIMARY KEY (lid)
+      );");
+      db_query("CREATE TABLE {locales_target} (
+        lid int NOT NULL default '0',
+        translation varchar(max) NOT NULL,
+        locale varchar(12) NOT NULL default '',
+        plid int NOT NULL default '0',
+        plural int NOT NULL default '0',
+      );");
+      break;
   }
+  
+  
   db_query("INSERT INTO {locales_meta} (locale, name, enabled, isdefault) VALUES ('en', 'English', '1', '1')");
 }
 
Index: includes/install.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/install.inc,v
retrieving revision 1.28
diff -u -r1.28 install.inc
--- includes/install.inc	28 Nov 2006 03:32:03 -0000	1.28
+++ includes/install.inc	1 Dec 2006 00:21:00 -0000
@@ -144,7 +144,7 @@
 function drupal_detect_database_types() {
   $databases = array();
 
-  foreach (array('mysql', 'mysqli', 'pgsql') as $type) {
+  foreach (array('mysql', 'mysqli', 'pgsql', 'mssql') as $type) {
     if (file_exists('./includes/install.'. $type .'.inc')) {
       include_once './includes/install.'. $type .'.inc';
       $function = $type .'_is_available';
@@ -305,7 +305,7 @@
   // The system module is a special case; we can't bootstrap until it's
   // installed, so we can't use the normal installation function.
   $module_list = array_diff($module_list, array('system'));
-
+  require_once "./profiles/$profile/$profile.profile";
   $system_path = dirname(drupal_get_filename('module', 'system', NULL));
   require_once './' . $system_path . '/system.install';
   module_invoke('system', 'install');
@@ -319,7 +319,7 @@
   // Install schemas for profile and all its modules.
   module_rebuild_cache();
   drupal_install_modules($module_list);
-
+  
   // And now, run the profile's install function.
   $function = $profile .'_install';
   if (function_exists($function)) {
@@ -337,17 +337,17 @@
  */
 function drupal_install_modules($module_list = array()) {
   $enable_modules = array();
-
+  
   foreach ($module_list as $module) {
     if (drupal_get_installed_schema_version($module, TRUE) == SCHEMA_UNINSTALLED) {
       module_load_install($module);
       module_invoke($module, 'install');
-      $versions = drupal_get_schema_versions($module);
+      $versions = drupal_get_schema_versions($module); 
       drupal_set_installed_schema_version($module, $versions ? max($versions) : SCHEMA_INSTALLED);
       $enable_modules[] = $module;
     }
   }
-
+  
   module_enable($enable_modules);
 }
 
Index: modules/search/search.install
===================================================================
RCS file: /cvs/drupal/drupal/modules/search/search.install,v
retrieving revision 1.6
diff -u -r1.6 search.install
--- modules/search/search.install	1 Sep 2006 07:40:08 -0000	1.6
+++ modules/search/search.install	1 Dec 2006 00:21:01 -0000
@@ -59,6 +59,26 @@
         PRIMARY KEY (word)
       )");
       break;
+    case 'mssql':
+      db_query("CREATE TABLE {search_dataset} (
+        sid int  NOT NULL default '0',
+        type varchar(16) default NULL,
+        data varchar(1024) NOT NULL
+      );");
+      db_query("CREATE TABLE {search_index} (
+        word varchar(50) NOT NULL default '',
+        sid int  NOT NULL default '0',
+        type varchar(16) default NULL,
+        fromsid int  NOT NULL default '0',
+        fromtype varchar(16) default NULL,
+        score float default NULL
+      );");
+      db_query("CREATE TABLE {search_total} (
+        word varchar(50) NOT NULL default '',
+        count float default NULL,
+        PRIMARY KEY (word)
+      );");
+      break;
   }
 }
 
Index: modules/profile/profile.install
===================================================================
RCS file: /cvs/drupal/drupal/modules/profile/profile.install,v
retrieving revision 1.8
diff -u -r1.8 profile.install
--- modules/profile/profile.install	28 Nov 2006 14:37:44 -0000	1.8
+++ modules/profile/profile.install	1 Dec 2006 00:21:01 -0000
@@ -64,6 +64,29 @@
       db_query("CREATE INDEX {profile_values}_uid_idx ON {profile_values} (uid)");
       db_query("CREATE INDEX {profile_values}_fid_idx ON {profile_values} (fid)");
       break;
+    case 'mssql':
+      db_query("CREATE TABLE {profile_fields} (
+        fid int NOT NULL identity(1,1),
+        title varchar(255) default NULL,
+        name varchar(128) unique default NULL,
+        explanation TEXT default NULL,
+        category varchar(255) default NULL,
+        page varchar(255) default NULL,
+        type varchar(128) default NULL,
+        weight int DEFAULT '0' NOT NULL,
+        required int DEFAULT '0' NOT NULL,
+        register int DEFAULT '0' NOT NULL,
+        visibility int DEFAULT '0' NOT NULL,
+        autocomplete int DEFAULT '0' NOT NULL,
+        options text,
+        PRIMARY KEY (fid)
+      );");
+      db_query("CREATE TABLE {profile_values} (
+        fid int  default '0',
+        uid int  default '0',
+        value text
+      );");
+      break;
   }
 }
 
Index: modules/book/book.install
===================================================================
RCS file: /cvs/drupal/drupal/modules/book/book.install,v
retrieving revision 1.6
diff -u -r1.6 book.install
--- modules/book/book.install	1 Sep 2006 07:40:08 -0000	1.6
+++ modules/book/book.install	1 Dec 2006 00:21:00 -0000
@@ -29,6 +29,15 @@
       db_query("CREATE INDEX {book}_nid_idx ON {book} (nid)");
       db_query("CREATE INDEX {book}_parent_idx ON {book} (parent)");
       break;
+    case 'mssql':
+      db_query("CREATE TABLE {book} (
+                vid int  NOT NULL default '0',
+                nid int  NOT NULL default '0',
+                parent int NOT NULL default '0',
+                weight int NOT NULL default '0',
+                PRIMARY KEY (vid)
+              );");
+      break;
   }
 }
 
Index: modules/contact/contact.install
===================================================================
RCS file: /cvs/drupal/drupal/modules/contact/contact.install,v
retrieving revision 1.5
diff -u -r1.5 contact.install
--- modules/contact/contact.install	1 Sep 2006 07:40:08 -0000	1.5
+++ modules/contact/contact.install	1 Dec 2006 00:21:01 -0000
@@ -31,6 +31,17 @@
         UNIQUE (category)
       )");
       break;
+    case 'mssql':
+      db_query("CREATE TABLE {contact} (
+        cid int NOT NULL default '0',
+        category varchar(255) unique NOT NULL default '',
+        recipients varchar(1024) NOT NULL default '',
+        reply varchar(1024) NOT NULL default '',
+        weight int NOT NULL default '0',
+        selected int NOT NULL default '0',
+        PRIMARY KEY (cid)
+      );");
+      break;
   }
 }
 
Index: modules/poll/poll.install
===================================================================
RCS file: /cvs/drupal/drupal/modules/poll/poll.install,v
retrieving revision 1.7
diff -u -r1.7 poll.install
--- modules/poll/poll.install	1 Sep 2006 07:40:08 -0000	1.7
+++ modules/poll/poll.install	1 Dec 2006 00:21:01 -0000
@@ -64,6 +64,27 @@
       )");
       db_query("CREATE INDEX {poll_choices}_nid_idx ON {poll_choices} (nid)");
       break;
+    case 'mssql':
+      db_query("CREATE TABLE {poll} (
+          nid int  NOT NULL default '0',
+          runtime int NOT NULL default '0',
+          active int  NOT NULL default '0',
+          PRIMARY KEY (nid)
+        );");
+      db_query("CREATE TABLE {poll_votes} (
+        nid int  NOT NULL,
+        uid int  NOT NULL default 0,
+        hostname varchar(128) NOT NULL default ''
+      );");
+      db_query("CREATE TABLE {poll_choices} (
+        chid int  NOT NULL identity(1,1),
+        nid int  NOT NULL default '0',
+        chtext varchar(128) NOT NULL default '',
+        chvotes int NOT NULL default '0',
+        chorder int NOT NULL default '0',
+        PRIMARY KEY (chid)
+      );");
+      break;
   }
 }
 
Index: modules/aggregator/aggregator.install
===================================================================
RCS file: /cvs/drupal/drupal/modules/aggregator/aggregator.install,v
retrieving revision 1.8
diff -u -r1.8 aggregator.install
--- modules/aggregator/aggregator.install	26 Sep 2006 14:19:00 -0000	1.8
+++ modules/aggregator/aggregator.install	1 Dec 2006 00:21:00 -0000
@@ -113,6 +113,50 @@
       db_query("CREATE INDEX {aggregator_item}_fid_idx ON {aggregator_item} (fid)");
 
       break;
+    
+    case 'mssql':
+      db_query("CREATE TABLE aggregator_category (
+                cid int NOT NULL identity(1,1),
+                title varchar(255) unique NOT NULL default '',
+                description varchar(1024) NOT NULL,
+                block int NOT NULL default '0',
+                PRIMARY KEY (cid)
+              );");
+      db_query("CREATE TABLE aggregator_category_feed (
+                fid int NOT NULL default '0',
+                cid int NOT NULL default '0',
+                PRIMARY KEY (fid,cid)
+              );");
+      db_query("CREATE TABLE aggregator_category_item (
+                iid int NOT NULL default '0',
+                cid int NOT NULL default '0',
+                PRIMARY KEY (iid,cid)
+              );");
+      db_query("CREATE TABLE aggregator_feed (
+                fid int NOT NULL identity(1,1),
+                title varchar(255) unique NOT NULL default '',
+                url varchar(255) NOT NULL default '',
+                refresh int NOT NULL default '0',
+                checked int NOT NULL default '0',
+                link varchar(255) unique NOT NULL default '',
+                description varchar(1024) NOT NULL,
+                image varchar(1024) NOT NULL,
+                etag varchar(255) NOT NULL default '',
+                modified int NOT NULL default '0',
+                block int NOT NULL default '0',
+                PRIMARY KEY (fid)
+              );");
+      db_query("CREATE TABLE aggregator_item (
+                iid int NOT NULL identity(1,1),
+                fid int NOT NULL default '0',
+                title varchar(255) NOT NULL default '',
+                link varchar(255) NOT NULL default '',
+                author varchar(255) NOT NULL default '',
+                description varchar(1024) NOT NULL,
+                timestamp int default NULL,
+                PRIMARY KEY (iid)
+              );");
+      break;
   }
 }
 
Index: modules/drupal/drupal.install
===================================================================
RCS file: /cvs/drupal/drupal/modules/drupal/drupal.install,v
retrieving revision 1.5
diff -u -r1.5 drupal.install
--- modules/drupal/drupal.install	1 Sep 2006 07:40:08 -0000	1.5
+++ modules/drupal/drupal.install	1 Dec 2006 00:21:01 -0000
@@ -53,6 +53,29 @@
         PRIMARY KEY (cid,name)
       )");
       break;
+    case 'mssql':
+      db_query("CREATE TABLE client (
+        cid int  NOT NULL identity(1,1),
+        link varchar(255) NOT NULL default '',
+        name varchar(128) NOT NULL default '',
+        mail varchar(128) NOT NULL default '',
+        slogan varchar(1024) NOT NULL,
+        mission varchar(1024) NOT NULL,
+        users int NOT NULL default '0',
+        nodes int NOT NULL default '0',
+        version varchar(35) NOT NULL default'',
+        created int NOT NULL default '0',
+        changed int NOT NULL default '0',
+        PRIMARY KEY (cid)
+      );
+      ");
+      db_query("CREATE TABLE client_system (
+        cid int NOT NULL default '0',
+        name varchar(255) NOT NULL default '',
+        type varchar(255) NOT NULL default '',
+        PRIMARY KEY (cid,name)
+      );");
+      break;
   }
 }
 
Index: modules/system/system.install
===================================================================
RCS file: /cvs/drupal/drupal/modules/system/system.install,v
retrieving revision 1.49
diff -u -r1.49 system.install
--- modules/system/system.install	28 Nov 2006 20:52:51 -0000	1.49
+++ modules/system/system.install	1 Dec 2006 00:21:04 -0000
@@ -189,7 +189,7 @@
         INDEX expire (expire)
       ) /*!40100 DEFAULT CHARACTER SET UTF8 */ ");
       db_query("CREATE TABLE {cache_page} (
-        cid varchar(255) BINARY NOT NULL default '',
+        cid varchar(255) NOT NULL default '',
         data longblob,
         expire int NOT NULL default '0',
         created int NOT NULL default '0',
@@ -1010,8 +1010,350 @@
       )");
       db_query("CREATE INDEX {watchdog}_type_idx ON {watchdog} (type)");
       break;
+    case 'mssql':
+      db_query("CREATE TABLE {access} (
+          aid int NOT NULL identity(1,1),
+          mask varchar(255) NOT NULL default '',
+          type varchar(255) NOT NULL default '',
+          status int NOT NULL default '0',
+          PRIMARY KEY (aid)
+        );");
+      db_query("CREATE TABLE {authmap} (
+        aid int  NOT NULL identity(1,1),
+        uid int NOT NULL default '0',
+        authname varchar(128) unique NOT NULL default '',
+        module varchar(128) NOT NULL default '',
+        PRIMARY KEY (aid)
+      );");
+      db_query("CREATE TABLE {blocks} (
+        module varchar(64) DEFAULT '' NOT NULL,
+        delta varchar(32) NOT NULL default '0',
+        theme varchar(255) NOT NULL default '',
+        status int DEFAULT '0' NOT NULL,
+        weight int DEFAULT '0' NOT NULL,
+        region varchar(64) DEFAULT 'left' NOT NULL,
+        custom int DEFAULT '0' NOT NULL,
+        throttle int DEFAULT '0' NOT NULL,
+        visibility int DEFAULT '0' NOT NULL,
+        pages text DEFAULT '' NOT NULL
+      );");
+      db_query("CREATE TABLE {boxes} (
+        bid int NOT NULL identity(1,1),
+        title varchar(64) NOT NULL default '',
+        body varchar(1024),
+        info varchar(128) unique NOT NULL default '',
+        format int NOT NULL default '0',
+        PRIMARY KEY (bid),
+      );");
+      db_query("CREATE TABLE {cache} (
+        cid varchar(255) NOT NULL default '',
+        data text,
+        expire int NOT NULL default '0',
+        created int NOT NULL default '0',
+        headers text,
+        PRIMARY KEY (cid),
+      );");
+      db_query("CREATE TABLE {cache_filter} (
+        cid varchar(255) NOT NULL default '',
+        data text,
+        expire int NOT NULL default '0',
+        created int NOT NULL default '0',
+        headers text,
+        PRIMARY KEY (cid),
+      );");
+      db_query("CREATE TABLE {cache_menu} (
+        cid varchar(255) NOT NULL default '',
+        data text,
+        expire int NOT NULL default '0',
+        created int NOT NULL default '0',
+        headers text,
+        PRIMARY KEY (cid),
+      );");;
+      db_query("CREATE TABLE {cache_page} (
+        cid varchar(255) NOT NULL default '',
+        data text,
+        expire int NOT NULL default '0',
+        created int NOT NULL default '0',
+        headers text,
+        PRIMARY KEY (cid),
+      );");
+      db_query("CREATE TABLE {comments} (
+        cid int NOT NULL default '0',
+        pid int NOT NULL default '0',
+        nid int NOT NULL default '0',
+        uid int NOT NULL default '0',
+        subject varchar(64) NOT NULL default '',
+        comment varchar(1024) NOT NULL,
+        hostname varchar(128) NOT NULL default '',
+        timestamp int NOT NULL default '0',
+        score int NOT NULL default '0',
+        status int  NOT NULL default '0',
+        format int NOT NULL default '0',
+        thread varchar(255) NOT NULL,
+        users varchar(1024),
+        name varchar(60) NULL default NULL,
+        mail varchar(64) NULL default NULL,
+        homepage varchar(255) NULL default NULL,
+        PRIMARY KEY (cid)
+      );");
+      db_query("CREATE TABLE {node_comment_statistics} (
+        nid int  NOT NULL default '0',
+        last_comment_timestamp int NOT NULL default '0',
+        last_comment_name varchar(60) NULL default NULL,
+        last_comment_uid int NOT NULL default '0',
+        comment_count int  NOT NULL default '0',
+        PRIMARY KEY (nid)
+      );");
+      db_query("CREATE TABLE {files} (
+        fid int  NOT NULL default 0,
+        nid int  NOT NULL default 0,
+        filename varchar(255) NOT NULL default '',
+        filepath varchar(255) NOT NULL default '',
+        filemime varchar(255) NOT NULL default '',
+        filesize int  NOT NULL default 0,
+        PRIMARY KEY (fid)
+      );
+      ");
+      db_query("CREATE TABLE {file_revisions} (
+        fid int  NOT NULL default 0,
+        vid int  NOT NULL default 0,
+        description varchar(255) NOT NULL default '',
+        list int  NOT NULL default 0,
+        PRIMARY KEY (fid, vid)
+      );");
+      db_query("CREATE TABLE {filter_formats} (
+        format int NOT NULL identity(1,1),
+        name varchar(255) unique NOT NULL default '',
+        roles varchar(255) NOT NULL default '',
+        cache int NOT NULL default '0',
+        PRIMARY KEY (format)
+      );");
+      db_query("CREATE TABLE {filters} (
+        format int NOT NULL default '0',
+        module varchar(64) NOT NULL default '',
+        delta int DEFAULT '0' NOT NULL,
+        weight int DEFAULT '0' NOT NULL
+      );");
+      db_query("CREATE TABLE {flood} (
+        event varchar(64) NOT NULL default '',
+        hostname varchar(128) NOT NULL default '',
+        timestamp int NOT NULL default '0'
+      );");
+      db_query("CREATE TABLE {history} (
+        uid int NOT NULL default '0',
+        nid int NOT NULL default '0',
+        timestamp int NOT NULL default '0',
+        PRIMARY KEY (uid,nid)
+      );");
+      db_query("CREATE TABLE {menu} (
+        mid int  NOT NULL default '0',
+        pid int  NOT NULL default '0',
+        path varchar(255) NOT NULL default '',
+        title varchar(255) NOT NULL default '',
+        description varchar(255) NOT NULL default '',
+        weight int NOT NULL default '0',
+        type int  NOT NULL default '0',
+        PRIMARY KEY (mid)
+      );");
+      db_query("CREATE TABLE {node} (
+        nid int  NOT NULL default '0',
+        vid int unique NOT NULL default '0',
+        type varchar(32) NOT NULL default '',
+        title varchar(128) NOT NULL default '',
+        uid int NOT NULL default '0',
+        status int NOT NULL default '1',
+        created int NOT NULL default '0',
+        changed int NOT NULL default '0',
+        comment int NOT NULL default '0',
+        promote int NOT NULL default '0',
+        moderate int NOT NULL default '0',
+        sticky int NOT NULL default '0',
+        PRIMARY KEY  (nid, vid)
+      );");
+      db_query("CREATE TABLE {node_access} (
+        nid int  NOT NULL default '0',
+        gid int  NOT NULL default '0',
+        realm varchar(255) NOT NULL default '',
+        grant_view int  NOT NULL default '0',
+        grant_update int  NOT NULL default '0',
+        grant_delete int  NOT NULL default '0',
+        PRIMARY KEY (nid,gid,realm)
+      );");
+      db_query("CREATE TABLE {node_revisions} (
+        nid int  NOT NULL,
+        vid int  NOT NULL,
+        uid int NOT NULL default '0',
+        title varchar(128) NOT NULL default '',
+        body varchar(1024) NOT NULL default '',
+        teaser varchar(1024) NOT NULL default '',
+        log varchar(1024) NOT NULL default '',
+        timestamp int NOT NULL default '0',
+        format int NOT NULL default '0',
+        PRIMARY KEY  (vid),
+      );");
+      db_query("CREATE TABLE {node_type} (
+        type varchar(32) NOT NULL,
+        name varchar(255) NOT NULL default '',
+        module varchar(255) NOT NULL,
+        description varchar(1024) NOT NULL default '',
+        help varchar(1024) NOT NULL default '',
+        has_title tinyint NOT NULL,
+        title_label varchar(255) NOT NULL default '',
+        has_body tinyint NOT NULL,
+        body_label varchar(255) NOT NULL default '',
+        min_word_count int NOT NULL,
+        custom int NOT NULL DEFAULT '0',
+        modified int NOT NULL DEFAULT '0',
+        locked int NOT NULL DEFAULT '0',
+        orig_type varchar(255) NOT NULL default '',
+        PRIMARY KEY (type))");
+      db_query("CREATE TABLE {url_alias} (
+        pid int  NOT NULL identity(1,1),
+        src varchar(128) NOT NULL default '',
+        dst varchar(128) unique NOT NULL default '',
+        PRIMARY KEY (pid)
+      );");
+      db_query("CREATE TABLE {permission} (
+        rid int  NOT NULL default '0',
+        perm varchar(1024),
+        tid int  NOT NULL default '0'
+      );");
+      db_query("CREATE TABLE {role} (
+        rid int  NOT NULL identity(1,1),
+        name varchar(32) unique NOT NULL default '',
+        PRIMARY KEY (rid)
+      );");
+      db_query("CREATE TABLE {blocks_roles} (
+        module varchar(64) NOT NULL,
+        delta varchar(32) NOT NULL,
+        rid int NOT NULL,
+        PRIMARY KEY (module, delta, rid)
+      );");
+      db_query("CREATE TABLE {sessions} (
+        uid int  NOT NULL,
+        sid varchar(32) NOT NULL default '',
+        hostname varchar(128) NOT NULL default '',
+        timestamp int NOT NULL default '0',
+        cache int NOT NULL default '0',
+        session varchar(1024),
+        PRIMARY KEY (sid)
+      );");
+      db_query("CREATE TABLE {sequences} (
+        name varchar(255) NOT NULL default '',
+        id int  NOT NULL default '0',
+        PRIMARY KEY (name)
+      );");
+      db_query("CREATE TABLE {node_counter} (
+        nid int NOT NULL default '0',
+        totalcount int  NOT NULL default '0',
+        daycount int NOT NULL default '0',
+        timestamp int  NOT NULL default '0',
+        PRIMARY KEY (nid)
+      );");
+      db_query("CREATE TABLE {system} (
+        filename varchar(255) NOT NULL default '',
+        name varchar(255) NOT NULL default '',
+        type varchar(255) NOT NULL default '',
+        description varchar(255) NOT NULL default '',
+        status char NOT NULL default '0',
+        throttle int DEFAULT '0' NOT NULL,
+        bootstrap int NOT NULL default '0',
+        schema_version smallint NOT NULL default -1,
+        weight int NOT NULL default '0',
+        PRIMARY KEY (filename)
+      );");
+      db_query("CREATE TABLE {term_data} (
+        tid int  NOT NULL default '0',
+        vid int  NOT NULL default '0',
+        name varchar(255) NOT NULL default '',
+        description varchar(1024),
+        weight int NOT NULL default '0',
+        PRIMARY KEY (tid)
+      );");
+      db_query("CREATE TABLE {term_hierarchy} (
+        tid int  NOT NULL default '0',
+        parent int  NOT NULL default '0',
+        PRIMARY KEY (tid, parent)
+      );");
+      db_query("CREATE TABLE {term_node} (
+      nid int  NOT NULL default '0',
+      tid int  NOT NULL default '0',
+      PRIMARY KEY (tid,nid)
+      );");
+      db_query("CREATE TABLE {term_relation} (
+        tid1 int  NOT NULL default '0',
+        tid2 int  NOT NULL default '0'
+      );");
+      db_query("CREATE TABLE {term_synonym} (
+        tid int  NOT NULL default '0',
+        name varchar(255) NOT NULL default ''
+      );");
+      db_query("CREATE TABLE {users} (
+        uid int  NOT NULL default '0',
+        name varchar(60) unique NOT NULL default '',
+        pass varchar(32) NOT NULL default '',
+        mail varchar(64) default '',
+        mode int NOT NULL default '0',
+        sort int default '0',
+        threshold int default '0',
+        theme varchar(255) NOT NULL default '',
+        signature varchar(255) NOT NULL default '',
+        created int NOT NULL default '0',
+        access int NOT NULL default '0',
+        login int NOT NULL default '0',
+        status int NOT NULL default '0',
+        timezone varchar(8) NULL default NULL,
+        language varchar(12) NOT NULL default '',
+        picture varchar(255) NOT NULL DEFAULT '',
+        init varchar(64) default '',
+        data varchar(1024) NULL,
+        PRIMARY KEY (uid)
+      );");
+      db_query("CREATE TABLE {users_roles} (
+        uid int  NOT NULL default '0',
+        rid int  NOT NULL default '0',
+        PRIMARY KEY (uid, rid)
+      );");
+      db_query("CREATE TABLE {variable} (
+        name varchar(48) NOT NULL default '',
+        value text NOT NULL,
+        PRIMARY KEY (name)
+      );");
+      db_query("CREATE TABLE {vocabulary} (
+        vid int  NOT NULL default '',
+        name varchar(255) NOT NULL default '',
+        description varchar(1024),
+        help varchar(255) NOT NULL default '',
+        relations int  NOT NULL default '0',
+        hierarchy int  NOT NULL default '0',
+        multiple int  NOT NULL default '0',
+        required int  NOT NULL default '0',
+        tags int  NOT NULL default '0',
+        module varchar(255) NOT NULL default '',
+        weight int NOT NULL default '0',
+        PRIMARY KEY (vid)
+      );");
+      db_query("CREATE TABLE {vocabulary_node_types} (
+        vid int  NOT NULL DEFAULT '0',
+        type varchar(32) NOT NULL DEFAULT '',
+        PRIMARY KEY (vid, type)
+      );");
+      db_query("CREATE TABLE {watchdog} (
+        wid int NOT NULL identity(1,1),
+        uid int NOT NULL default '0',
+        type varchar(16) NOT NULL default '',
+        message varchar(1024) NOT NULL,
+        severity int  NOT NULL default '0',
+        link varchar(255) NOT NULL default '',
+        location varchar(128) NOT NULL default '',
+        referer varchar(128) NOT NULL default '',
+        hostname varchar(128) NOT NULL default '',
+        timestamp int NOT NULL default '0',
+        PRIMARY KEY (wid)
+      );");
+      break;
   }
-
+  
   db_query("INSERT INTO {system} (filename, name, type, description, status, throttle, bootstrap, schema_version) VALUES ('themes/engines/phptemplate/phptemplate.engine', 'phptemplate', 'theme_engine', '', 1, 0, 0, 0)");
   db_query("INSERT INTO {system} (filename, name, type, description, status, throttle, bootstrap, schema_version) VALUES ('themes/garland/page.tpl.php', 'garland', 'theme', 'themes/engines/phptemplate/phptemplate.engine', 1, 0, 0, 0)");
 
Index: modules/statistics/statistics.install
===================================================================
RCS file: /cvs/drupal/drupal/modules/statistics/statistics.install,v
retrieving revision 1.7
diff -u -r1.7 statistics.install
--- modules/statistics/statistics.install	7 Nov 2006 22:27:07 -0000	1.7
+++ modules/statistics/statistics.install	1 Dec 2006 00:21:01 -0000
@@ -37,6 +37,20 @@
       )");
       db_query("CREATE INDEX {accesslog}_accesslog_timestamp_idx ON {accesslog} (timestamp)");
       break;
+    case 'mssql':
+      db_query("CREATE TABLE {accesslog} (
+        aid int NOT NULL identity(1,1),
+        sid varchar(32) NOT NULL default '',
+        title varchar(255) default NULL,
+        path varchar(255) default NULL,
+        url varchar(255) default NULL,
+        hostname varchar(128) default NULL,
+        uid int  default '0',
+        timer int  NOT NULL default '0',
+        timestamp int  NOT NULL default '0',
+        PRIMARY KEY (aid)
+      );");
+      break;
   }
 }
 
Index: modules/forum/forum.install
===================================================================
RCS file: /cvs/drupal/drupal/modules/forum/forum.install,v
retrieving revision 1.6
diff -u -r1.6 forum.install
--- modules/forum/forum.install	1 Sep 2006 07:40:08 -0000	1.6
+++ modules/forum/forum.install	1 Dec 2006 00:21:01 -0000
@@ -27,6 +27,14 @@
       db_query("CREATE INDEX {forum}_nid_idx ON {forum} (nid)");
       db_query("CREATE INDEX {forum}_tid_idx ON {forum} (tid)");
       break;
+    case 'mssql':
+      db_query("CREATE TABLE {forum} (
+        nid int  NOT NULL default '0',
+        vid int  NOT NULL default '0',
+        tid int  NOT NULL default '0',
+        PRIMARY KEY (vid)
+      );");
+      break;
   }
 }
 
Index: includes/install.mssql.inc
===================================================================
RCS file: includes/install.mssql.inc
diff -N includes/install.mssql.inc
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ includes/install.mssql.inc	1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,139 @@
+<?php
+// $Id: $
+
+// MS SQL specific install functions
+
+/**
+ * Check if MS SQL is available.
+ *
+ * @return
+ *  TRUE/FALSE
+ */
+function mssql_is_available() {
+  return function_exists('mssql_connect');
+}
+
+/**
+ * Check if we can connect to MS SQL.
+ *
+ * @return
+ *  TRUE/FALSE
+ */
+function drupal_test_mssql($url, &$success) {
+  if (!mssql_is_available()) {
+    drupal_set_message('PHP MS SQL support not enabled.', 'error');
+    return FALSE;
+  }
+
+  $url = parse_url($url);
+
+  // Decode url-encoded information in the db connection string.
+  $url['user'] = urldecode($url['user']);
+  $url['pass'] = urldecode($url['pass']);
+  $url['host'] = stripslashes(urldecode($url['host']));
+  $url['path'] = urldecode($url['path']);
+
+  // Allow for non-standard MS SQL port.
+  if (isset($url['port'])) {
+     $url['host'] = $url['host'] .','. $url['port'];
+  }
+
+  // Test connecting to the database.
+  $connection = mssql_connect($url['host'], $url['user'], $url['pass']);
+  if (!$connection) {
+    drupal_set_message(st('Failure to connect to your MS SQL database server. MS SQL reports the following message: %error.<ul><li>Are you sure you have the correct username and password?</li><li>Are you sure that you have typed the correct database hostname?</li><li>Are you sure that the database server is running?</li></ul>For more help, see the <a href="http://drupal.org/node/258">Installation and upgrading handbook</a>. If you are unsure what these terms mean you should probably contact your hosting provider.', array('%error' => $php_errormsg)), 'error');
+    return FALSE;
+  }
+
+  // Test selecting the database.
+  if (!mssql_select_db(substr($url['path'], 1))) {
+    drupal_set_message(st('We were able to connect to the MS SQL database server (which means your username and password are valid) but not able to select your database. MS SQL reports the following message: %error.<ul><li>Are you sure you have the correct database name?</li><li>Are you sure the database exists?</li><li>Are you sure the username has permission to access the database?</li></ul>For more help, see the <a href="http://drupal.org/node/258">Installation and upgrading handbook</a>. If you are unsure what these terms mean you should probably contact your hosting provider.', array('%error' => mssql_get_last_message())), 'error');
+    return FALSE;
+  }
+
+  $success = array('CONNECT');
+
+  // Test CREATE.
+  $query = 'CREATE TABLE drupal_install_test (id int NULL)';
+  $result = mssql_query($query);
+  if ($result === FALSE) {
+    drupal_set_message(st('We were unable to create a test table on your MS SQL database server with the command %query. MS SQL reports the following message: %error.<ul><li>Are you sure the configured username has the necessary MS SQL permissions to create tables in the database?</li></ul>For more help, see the <a href="http://drupal.org/node/258">Installation and upgrading handbook</a>. If you are unsure what these terms mean you should probably contact your hosting provider.', array('%query' => $query, '%error' => $php_errormsg)), 'error');
+    return FALSE;
+  }
+  $err = FALSE;
+  $success[] = 'SELECT';
+  $success[] = 'CREATE';
+
+  // Test INSERT.
+  $query = 'INSERT INTO drupal_install_test (id) VALUES (1)';
+  $result = mssql_query($query);
+  if ($result === FALSE) {
+    drupal_set_message(st('We were unable to insert a value into a test table on your MS SQL database server. We tried inserting a value with the command %query and MS SQL reported the following error: %error.', array('%query' => $query, '%error' => $php_errormsg)), 'error');
+    $err = TRUE;
+  }
+  else {
+    $success[] = 'INSERT';
+  }
+
+  // Test UPDATE.
+  $query = 'UPDATE drupal_install_test SET id = 2';
+  $result = mssql_query($query);
+  if ($result === FALSE) {
+    drupal_set_message(st('We were unable to update a value in a test table on your MS SQL database server. We tried updating a value with the command %query and MS SQL reported the following error: %error.', array('%query' => $query, '%error' => $php_errormsg)), 'error');
+    $err = TRUE;
+  }
+  else {
+    $success[] = 'UPDATE';
+  }
+
+  // Test LOCK.
+  $query = 'BEGIN TRANSACTION';
+  $result = mssql_query($query);
+  if ($result === FALSE) {
+    drupal_set_message(st('We were unable to lock a test table on your MS SQL database server. We tried locking a table with the command %query and MS SQL reported the following error: %error.', array('%query' => $query, '%error' => $php_errormsg)), 'error');
+    $err = TRUE;
+  }
+  else {
+    $success[] = 'LOCK';
+  }
+
+  // Test UNLOCK.
+  $query = 'COMMIT TRANSACTION';
+  $result = mssql_query($query);
+  if ($result === FALSE) {
+    drupal_set_message(st('We were unable to unlock a test table on your MS SQL database server. We tried unlocking a table with the command %query and MS SQL reported the following error: %error.', array('%query' => $query, '%error' => $php_errormsg)), 'error');
+    $err = TRUE;
+  }
+  else {
+    $success[] = 'UNLOCK';
+  }
+
+  // Test DELETE.
+  $query = 'DELETE FROM drupal_install_test';
+  $result = mssql_query($query);
+  if ($result === FALSE) {
+    drupal_set_message(st('We were unable to delete a value from a test table on your MS SQL database server. We tried deleting a value with the command %query and MS SQL reported the following error: %error.', array('%query' => $query, '%error' => $php_errormsg)), 'error');
+    $err = TRUE;
+  }
+  else {
+    $success[] = 'DELETE';
+  }
+
+  // Test DROP.
+  $query = 'DROP TABLE drupal_install_test';
+  $result = mssql_query($query);
+  if ($result === FALSE) {
+    drupal_set_message(st('We were unable to drop a test table from your MS SQL database server. We tried dropping a table with the command %query and MS SQL reported the following error %error.', array('%query' => $query, '%error' => $php_errormsg)), 'error');
+    $err = TRUE;
+  }
+  else {
+    $success[] = 'DROP';
+  }
+
+  if ($err) {
+    return FALSE;
+  }
+
+  mssql_close($connection);
+  return TRUE;
+}
Index: includes/database.mssql.inc
===================================================================
RCS file: includes/database.mssql.inc
diff -N includes/database.mssql.inc
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ includes/database.mssql.inc	1 Jan 1970 00:00:00 -0000
@@ -0,0 +1,480 @@
+<?php
+// $Id: $
+
+/**
+ * @file
+ * Database interface code for Microsoft SQL database servers.
+ * 
+ * $connection = mssql_connect('\\\\.\pipe\MSSQL$SQLEXPRESS\sql\query', 'sa', 'pw');
+ */
+
+/**
+ * @ingroup database
+ * @{
+ */
+
+
+/**
+ * Report database status.
+ */
+function db_status_report($phase) {
+  $t = get_t();
+
+  $version = db_version();
+
+  $form['mysql'] = array(
+    'title' => $t('MS SQL database'),
+    'value' => ($phase == 'runtime') ? l($version, 'admin/logs/status/sql') : $version,
+  );
+
+  /*if (version_compare($version, DRUPAL_MINIMUM_MSSQL) < 0) {
+    $form['mysql']['severity'] = REQUIREMENT_ERROR;
+    $form['mysql']['description'] = $t('Your MS SQL Server is too old. Drupal requires at least MS SQL %version.', array('%version' => DRUPAL_MINIMUM_MSSQL));
+  }*/
+
+  return $form;
+}
+
+/**
+ * Returns the version of the database server currently in use.
+ *
+ * @return Database server version
+ */
+function db_version() {
+  list($version) = explode('-', 'version');
+  return $version;
+}
+
+/**
+ * Initialize a database connection.
+ *
+ * Note that you can change the mysql_connect() call to mysql_pconnect() if you
+ * want to use persistent connections. This is not recommended on shared hosts,
+ * and might require additional database/webserver tuning. It can increase
+ * performance, however, when the overhead to connect to your database is high
+ * (e.g. your database and web server live on different machines).
+ */
+function db_connect($url) {
+  // Check if MS SQL support is present in PHP
+  if (!function_exists('mssql_connect')) {
+    drupal_maintenance_theme();
+    drupal_set_title('PHP MS SQL support not enabled');
+    print theme('maintenance_page', '<p>We were unable to use the MS SQL database because the MS SQL extension for PHP is not installed. Check your <code>PHP.ini</code> to see how you can enable it.</p>
+<p>For more help, see the <a href="http://drupal.org/node/258">Installation and upgrading handbook</a>. If you are unsure what these terms mean you should probably contact your hosting provider.</p>');
+    exit;
+  }
+
+  $url = parse_url($url);
+
+  // Decode url-encoded information in the db connection string
+  $url['user'] = urldecode($url['user']);
+  // Test if database url has a password.
+  if(isset($url['pass'])) {
+    $url['pass'] = urldecode($url['pass']);
+  }
+  else {
+    $url['pass'] = '';
+  }
+  $url['host'] = stripslashes(urldecode($url['host']));
+  $url['path'] = urldecode($url['path']);
+
+  // Allow for non-standard MS SQL port.
+  if (isset($url['port'])) {
+     $url['host'] = $url['host'] .','. $url['port'];
+  }
+
+  // Allow large texts for MS SQL.
+  ini_set("mssql.textlimit", 16000000);
+  ini_set("mssql.textsize", 16000000);
+
+  // TODO: Put back @.
+  $connection = mssql_connect($url['host'], $url['user'], $url['pass']);
+  if (!$connection) {
+    // Redirect to installer if using default DB credentials
+    if ($url['user'] == 'username' && $url['pass'] == 'password') {
+      include_once 'includes/install.inc';
+      install_goto('install.php');
+    }
+
+    // Show error screen otherwise
+    drupal_maintenance_theme();
+    drupal_set_header('HTTP/1.1 503 Service Unavailable');
+    drupal_set_title('Unable to connect to database server');
+    print theme('maintenance_page', '<p>If you still have to install Drupal, proceed to the <a href="'. base_path() .'install.php">installation page</a>.</p>
+<p>If you have already finished installed Drupal, this either means that the username and password information in your <code>settings.php</code> file is incorrect or that we can\'t connect to the MS SQL database server. This could mean your hosting provider\'s database server is down.</p>
+<p>The MS SQL error was: '. theme('placeholder', mssql_get_last_message()) .'.</p>
+<p>Currently, the username is '. theme('placeholder', $url['user']) .' and the database server is '. theme('placeholder', $url['host']) .'.</p>
+<ul>
+  <li>Are you sure you have the correct username and password?</li>
+  <li>Are you sure that you have typed the correct hostname?</li>
+  <li>Are you sure that the database server is running?</li>
+</ul>
+<p>For more help, see the <a href="http://drupal.org/node/258">Installation and upgrading handbook</a>. If you are unsure what these terms mean you should probably contact your hosting provider.</p>');
+    exit;
+  }
+
+  if (!mssql_select_db(substr($url['path'], 1))) {
+    drupal_maintenance_theme();
+    drupal_set_title('Unable to select database');
+    print theme('maintenance_page', '<p>We were able to connect to the MS SQL database server (which means your username and password are okay) but not able to select the database.</p>
+<p>The MS SQL error was: '. theme('placeholder', mssql_get_last_message()) .'.</p>
+<p>Currently, the database is '. theme('placeholder', substr($url['path'], 1)) .'. The username is '. theme('placeholder', $url['user']) .' and the database server is '. theme('placeholder', $url['host']) .'.</p>
+<ul>
+  <li>Are you sure you have the correct database name?</li>
+  <li>Are you sure the database exists?</li>
+  <li>Are you sure the username has permission to access the database?</li>
+</ul>
+<p>For more help, see the <a href="http://drupal.org/node/258">Installation and upgrading handbook</a>. If you are unsure what these terms mean you should probably contact your hosting provider.</p>');
+    exit;
+  }
+
+  // TODO: Document this. Not sure what this does.
+  mssql_query('SET TEXTSIZE 524287;');
+
+  return $connection;
+}
+
+/**
+ * Helper function for db_query().
+ */
+function _db_query($query, $debug = 0) {
+  global $active_db, $queries;
+
+  if (variable_get('dev_query', 0)) {
+    list($usec, $sec) = explode(' ', microtime());
+    $timer = (float)$usec + (float)$sec;
+  }
+
+  // LENGTH is called DATALENGTH in MS SQL.
+  // TODO: Preg search instead of strpos.
+  if(strpos($query,"LENGTH")){
+    $pattern = '/(.+)LENGTH(.+)/i';
+    $query = preg_replace($pattern, '${1}DATALENGTH$2', $query); 
+  }
+  // No LIMIT in MS SQL. We use SELECT TOP n ... FROM ...
+  // TODO: Preg search instead of strpos.
+  if(strpos($query,'LIMIT')){
+    $pattern = '/(.+)LIMIT (\d+), (\d+)/i';
+    $query_new = preg_replace($pattern, '${1}', $query);
+    $limit = preg_replace($pattern, '$3', $query);
+    $query = str_replace('SELECT', 'SELECT TOP '.$limit, $query_new);
+  }
+  // TODO: Not sure what this is. 
+  // TODO: Preg search instead of strpos so we don't replace valid instances of 'relevance'.
+  if(strpos($query,'relevance')){
+    $query = str_replace('relevance', '_relevance', $query_new);
+  }
+  
+  $result = @mssql_query($query, $active_db);
+  
+  if(!$result) {
+    watchdog('mssql', mssql_get_last_message($active_db), WATCHDOG_ERROR);
+  }
+  if (variable_get('dev_query', 0)) {
+    $bt = debug_backtrace();
+    $query = $bt[2]['function'] . "\n" . $query;
+    list($usec, $sec) = explode(' ', microtime());
+    $stop = (float)$usec + (float)$sec;
+    $diff = $stop - $timer;
+    $queries[] = array($query, $diff);
+  }
+
+  if ($debug) {
+    print '<p>query: '. $query .'<br />error:'. mssql_get_last_message() .'</p>';
+  }
+
+  // TODO: if (!mssql_errno($active_db)) {
+    return $result;
+  /*}
+  else {
+    trigger_error(check_plain(mssql_get_last_message() ."\nquery: ". $query), E_USER_WARNING);
+    return FALSE;
+  }*/
+}
+
+/**
+ * Fetch one result row from the previous query as an object.
+ *
+ * @param $result
+ *   A database query result resource, as returned from db_query().
+ * @return
+ *   An object representing the next row of the result. The attributes of this
+ *   object are the table fields selected by the query.
+ */
+function db_fetch_object($result) {
+  if ($result) {
+    return mssql_fetch_object($result);
+  }
+}
+
+/**
+ * Fetch one result row from the previous query as an array.
+ *
+ * @param $result
+ *   A database query result resource, as returned from db_query().
+ * @return
+ *   An associative array representing the next row of the 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) {
+  if ($result) {
+    return mssql_fetch_array($result, MSSQL_ASSOC);
+  }
+}
+
+/**
+ * Determine how many result rows were found by the preceding query.
+ *
+ * @param $result
+ *   A database query result resource, as returned from db_query().
+ * @return
+ *   The number of result rows.
+ */
+function db_num_rows($result) {
+  if ($result) {
+    return mssql_num_rows($result);
+  }
+}
+
+/**
+ * Return an individual result field from the previous query.
+ *
+ * Only use this function if exactly one field is being selected; otherwise,
+ * use db_fetch_object() or db_fetch_array().
+ *
+ * @param $result
+ *   A database query result resource, as returned from db_query().
+ * @param $row
+ *   The index of the row whose result is needed.
+ * @return
+ *   The resulting field.
+ */
+function db_result($result, $row = 0) {
+  if ($result && mssql_num_rows($result) > $row) {
+    mssql_data_seek($result, $row);
+    $row_result = mssql_fetch_row($result);
+    return $row_result[0];
+  }
+}
+
+/**
+ * Determine whether the previous query caused an error.
+ */
+function db_error() {
+  return mssql_get_last_message();
+}
+
+/**
+ * Return a new unique ID in the given sequence.
+ *
+ * For compatibility reasons, Drupal does not use auto-numbered fields in its
+ * database tables. Instead, this function is used to return a new unique ID
+ * of the type requested. If necessary, a new sequence with the given name
+ * will be created.
+ */
+function db_next_id($name) {
+  $name = db_prefix_tables($name);
+  db_lock_table('sequences');
+  $id = db_result(db_query("SELECT id FROM {sequences} WHERE name = '%s'", $name)) + 1;
+  if (db_num_rows(db_query("SELECT id FROM {sequences} WHERE name = '%s'", $name)) == 0) {
+    db_query("INSERT INTO {sequences} VALUES ('%s', %d)", $name, $id);
+  }
+  else {
+    db_query("UPDATE {sequences} SET id = %d WHERE name = '%s'", $id, $name);
+  }
+  db_unlock_tables();
+
+  return $id;
+}
+
+/**
+ * Determine the number of rows changed by the preceding query.
+ */
+function db_affected_rows() {
+  global $active_db;
+  $result = mssql_query('SELECT @@ROWCOUNT', $active_db);
+  list($affected) = mssql_fetch_row($result);
+  return $affected;
+}
+
+/**
+ * Runs a limited-range query in the active database.
+ *
+ * Use this as a substitute for db_query() when a subset of the query is to be
+ * returned.
+ * 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.
+ *
+ * Note that if you need to know how many results were returned, you should do
+ * a SELECT COUNT(*) on the temporary table afterwards. db_num_rows() and
+ * db_affected_rows() do not give consistent result across different database
+ * types in this case.
+ *
+ * @param $query
+ *   A string containing an SQL query.
+ * @param ...
+ *   A variable number of arguments which are substituted into the query
+ *   using printf() syntax. The query arguments can be enclosed in one
+ *   array instead.
+ *   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.
+ *
+ * @param $from
+ *   The first result row to return.
+ * @param $count
+ *   The maximum number of result rows to return.
+ * @return
+ *   A database query result resource, or FALSE if the query was not executed
+ *   correctly.
+ */
+function db_query_range($query) {
+  $args = func_get_args();
+  $count = array_pop($args);
+  $from = array_pop($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);
+  $query .= ' LIMIT '. (int)$from .', '. (int)$count;
+  return _db_query($query);
+}
+
+/**
+ * Runs a SELECT query and stores its results in a temporary table.
+ *
+ * Use this as a substitute for db_query() when the results need to stored
+ * in a temporary table. Temporary tables exist for the duration of the page
+ * request.
+ * 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.
+ *
+ * Note that if you need to know how many results were returned, you should do
+ * a SELECT COUNT(*) on the temporary table afterwards. db_num_rows() and
+ * db_affected_rows() do not give consistent result across different database
+ * types in this case.
+ *
+ * @param $query
+ *   A string containing a normal SELECT SQL query.
+ * @param ...
+ *   A variable number of arguments which are substituted into the query
+ *   using printf() syntax. The query arguments can be enclosed in one
+ *   array instead.
+ *   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.
+ *
+ * @param $table
+ *   The name of the temporary table to select into. This name will not be
+ *   prefixed as there is no risk of collision.
+ * @return
+ *   A database query result resource, or FALSE if the query was not executed
+ *   correctly.
+ */
+function db_query_temporary($query) {
+  $args = func_get_args();
+  $tablename = array_pop($args);
+  array_shift($args);
+
+  $query = preg_replace('/^SELECT/i', 'CREATE TEMPORARY TABLE '. $tablename .' SELECT', 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);
+}
+
+/**
+ * Returns a properly formatted Binary Large OBject value.
+ *
+ * @param $data
+ *   Data to encode.
+ * @return
+ *  Encoded data.
+ */
+function db_encode_blob($data) {
+  global $active_db;
+  return "'" . base64_encode($data) . "'";
+}
+
+/**
+ * Returns text from a Binary Large Object value.
+ *
+ * @param $data
+ *   Data to decode.
+ * @return
+ *  Decoded data.
+ */
+function db_decode_blob($data) {
+  return base64_decode($data);
+}
+
+/**
+ * Prepare user input for use in a database query, preventing SQL injection attacks.
+ */
+function db_escape_string($text) {
+  // TODO: Use proper function here for db escaping.
+  // Above 'TODO' solved by below. Leaving not until
+  // thuroughly tested.
+  // Souvent22
+  // Taken from php.net 
+  // vollmer at ampache dot org
+  /* De MagicQuotes */
+  $text = stripslashes($text);
+  $text = str_replace("'","''",$text);
+  $text = str_replace("\0","[NULL]",$text);
+  return $text;
+}
+
+/**
+ * Lock a table.
+ */
+function db_lock_table($table) {
+  db_query('SET TRANSACTION ISOLATION LEVEL SERIALIZABLE');
+  db_query('BEGIN TRANSACTION');
+}
+
+/**
+ * Unlock all locked tables.
+ */
+function db_unlock_tables() {
+  db_query('COMMIT');
+}
+
+/**
+ * Check if a table exists.
+ */
+function db_table_exists($table) {
+  return db_num_rows(db_query("SHOW TABLES LIKE '{" . db_escape_table($table) . "}'"));
+}
+
+/**
+ * Wraps the given table.field entry with a DISTINCT(). The wrapper is added to
+ * the SELECT list entry of the given query and the resulting query is returned.
+ * This function only applies the wrapper if a DISTINCT doesn't already exist in
+ * the query.
+ *
+ * @param $table Table containing the field to set as DISTINCT
+ * @param $field Field to set as DISTINCT
+ * @param $query Query to apply the wrapper to
+ * @return SQL query with the DISTINCT wrapper surrounding the given table.field.
+ */
+function db_distinct_field($table, $field, $query) {
+  $field_to_select = 'DISTINCT('. $table .'.'. $field .')';
+  // (?<!text) is a negative look-behind (no need to rewrite queries that already use DISTINCT).
+  return preg_replace('/(SELECT.*)('. $table .'\.)?(?<!DISTINCT\()(?<!DISTINCT\('. $table .'\.)'. $field .'(.*FROM )/AUsi', '\1'. $field_to_select .'\3', $query);
+}
+
+/**
+ * @} End of "ingroup database".
+ */
