--- ad-o.install	2008-04-20 00:14:59.000000000 +0530
+++ ad.install	2008-05-12 10:56:18.000000000 +0530
@@ -8,263 +8,129 @@
  */
 
 function ad_install() {
-  switch ($GLOBALS['db_type']) {
-    case 'pgsql':
-     /* The ad table stores administrative information about each ad.  The
-      * actual ad itself can be found in the appropriate ad type table.
-      */
-     db_query("CREATE TABLE {ads} (
-       aid INT NOT NULL UNIQUE DEFAULT '0' PRIMARY KEY,
-       uid INT NOT NULL DEFAULT '0',
- 
-       adstatus VARCHAR(255) NOT NULL DEFAULT '',
-       adtype VARCHAR(255) NOT NULL DEFAULT '',
-
-       redirect VARCHAR(255) NOT NULL DEFAULT '',
-
-       autoactivate INT NOT NULL DEFAULT '0',
-       autoactivated INT NOT NULL DEFAULT '0',
-       autoexpire INT NOT NULL DEFAULT '0',
-       autoexpired INT NOT NULL DEFAULT '0',
-
-       activated INT NOT NULL DEFAULT '0',
-       maxviews INT NOT NULL DEFAULT '0',
-       maxclicks INT NOT NULL DEFAULT '0',
-       expired INT NOT NULL DEFAULT '0'
-     );");
-
-     /**
-      * Every ad can have one or more owners.
-      */
-     db_query("CREATE TABLE {ad_owners} (
-       oid SERIAL NOT NULL PRIMARY KEY,
-       aid INT NOT NULL DEFAULT '0',
-       uid INT NOT NULL DEFAULT '0'
-     );");
-
-     /**
-      * Permissions can be granted to each owner of each ad.  The same owner
-      * can own multiple ads, and can have different permissions for each ad.
-      */
-     db_query("CREATE TABLE {ad_permissions} (
-       oid INT NOT NULL DEFAULT '0' PRIMARY KEY,
-       permissions TEXT NULL DEFAULT ''
-     );");
-
-     /**
-      * This table counts each time a given action occurs on an ad.  Actions
-      * include when the ad is viewed, clicked, enabled and disabled.
-      * Statistics are collected at an hourly granularity.
-      *
-      * The source column is used for tracking statistics for externally 
-      * hosted ads.
-      *
-      * Actions:
-      *  'view', 'click', 'enable', 'disable'
-      */
-     db_query("CREATE TABLE {ad_statistics} (
-       sid SERIAL NOT NULL PRIMARY KEY,
-       aid INT NOT NULL DEFAULT '0',
-
-       date INT NOT NULL DEFAULT '0',
-       action VARCHAR(255) NOT NULL DEFAULT '',
-       adgroup VARCHAR(255) NULL DEFAULT '',
-       hostid VARCHAR(32) NULL DEFAULT '',
-       count INT NOT NULL DEFAULT '0'
-     );");
-
-     /**
-      * The ad_clicks table tracks when a given advertisement was clicked, 
-      * who clicked it (uid if any and IP address), and what page they were
-      * on when they clicked it.
-      */
-      db_query("CREATE TABLE {ad_clicks} (
-        cid SERIAL NOT NULL PRIMARY KEY,
-        aid INT NOT NULL DEFAULT '0',
-        uid INT NOT NULL DEFAULT '0',
-
-        status INT NOT NULL DEFAULT '0',
- 
-        hostname varchar(128) NOT NULL DEFAULT '',
-        user_agent varchar(255) NOT NULL DEFAULT '',
-        adgroup varchar(255) NOT NULL DEFAULT '',
-        hostid varchar(32) NOT NULL DEFAULT '',
-        url varchar(255) DEFAULT '',
-        timestamp INT NOT NULL DEFAULT '0',
-      );");
-
-      /**
-       * The ad_hosts table is used to configure users that can display ads
-       * remotely. 
-       */
-      db_query("CREATE TABLE {ad_hosts} (
-        uid INT NOT NULL DEFAULT '0' PRIMARY KEY,
- 
-        hostid varchar(32) DEFAULT '',
-        status INT NOT NULL DEFAULT '0',
-        description TEXT NOT NULL DEFAULT ''
-      );");
-      break;
-
-    case 'mysql':
-    case 'mysqli':
-    default:
-
-      /* The ad table stores administrative information about each ad.  The
-       * actual ad itself can be found in the appropriate ad type table.
-       */
-      db_query("CREATE TABLE {ads} (
-        aid INT(10) UNSIGNED NOT NULL DEFAULT '0',
-        uid INT(10) UNSIGNED NOT NULL DEFAULT '0',
-  
-        adstatus VARCHAR(255) NOT NULL DEFAULT '',
-        adtype VARCHAR(255) NOT NULL DEFAULT '',
-
-        redirect VARCHAR(255) NOT NULL DEFAULT '',
-
-        autoactivate INT UNSIGNED NOT NULL DEFAULT '0',
-        autoactivated INT UNSIGNED NOT NULL DEFAULT '0',
-        autoexpire INT UNSIGNED NOT NULL DEFAULT '0',
-        autoexpired INT UNSIGNED NOT NULL DEFAULT '0',
-
-        activated INT UNSIGNED NOT NULL DEFAULT '0',
-        maxviews INT UNSIGNED NOT NULL DEFAULT '0',
-        maxclicks INT UNSIGNED NOT NULL DEFAULT '0',
-        expired INT UNSIGNED NOT NULL DEFAULT '0',
-
-        PRIMARY KEY  (aid),
-        KEY (uid),
-        KEY (autoactivate),
-        KEY (autoexpire)
-      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ ");
-
-      /**
-       * Every ad can have one or more owners.
-       */
-      db_query("CREATE TABLE {ad_owners} (
-        oid INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
-        aid INT(10) UNSIGNED NOT NULL DEFAULT '0',
-        uid INT(10) UNSIGNED NOT NULL DEFAULT '0',
-
-        PRIMARY KEY  (oid),
-        KEY  (aid),
-        KEY  (uid)
-      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ ");
-
-      /**
-       * Permissions can be granted to each owner of each ad.  The same owner
-       * can own multiple ads, and can have different permissions for each ad.
-       */
-      db_query("CREATE TABLE {ad_permissions} (
-        oid INT(11) UNSIGNED NOT NULL DEFAULT '0',
-        permissions LONGTEXT NULL,
-        PRIMARY KEY  (oid)
-      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ ");
-
-      /**
-       * This table counts each time a given action occurs on an ad.  Actions
-       * include when the ad is viewed, clicked, enabled and disabled.
-       * Statistics are collected at an hourly granularity.
-       *
-       * The source column is used for tracking statistics for externally 
-       * hosted ads.
-       *
-       * Actions:
-       *  'view', 'click', 'enable', 'disable'
-       */
-      db_query("CREATE TABLE {ad_statistics} (
-        sid INT UNSIGNED NOT NULL AUTO_INCREMENT,
-        aid INT UNSIGNED NOT NULL DEFAULT '0',
-
-        date INT(10) UNSIGNED NOT NULL DEFAULT '0',
-        action VARCHAR(255) NOT NULL DEFAULT '',
-        adgroup VARCHAR(255) NULL DEFAULT '',
-        hostid VARCHAR(32) NULL DEFAULT '',
-        count INT(11) UNSIGNED NOT NULL DEFAULT '0',
-
-        PRIMARY KEY  (sid),
-        KEY (aid),
-        KEY (date),
-        KEY (action),
-        KEY (adgroup),
-        KEY (hostid)
-      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ ");
-
-      /**
-       * The ad_clicks table tracks when a given advertisement was clicked, 
-       * who clicked it (uid if any and IP address), and what page they were
-       * on when they clicked it.
-       */
-      db_query("CREATE TABLE {ad_clicks} (
-        cid INT UNSIGNED NOT NULL AUTO_INCREMENT,
-        aid INT UNSIGNED NOT NULL DEFAULT '0',
-        uid int(10) UNSIGNED NOT NULL DEFAULT '0',
-
-        status INT(2) NOT NULL DEFAULT '0',
-
-        hostname varchar(128) NOT NULL DEFAULT '',
-        user_agent varchar(255) NOT NULL DEFAULT '',
-        adgroup varchar(255) NOT NULL DEFAULT '',
-        hostid varchar(32) NOT NULL DEFAULT '',
-        url varchar(255) DEFAULT '',
-        timestamp INT(11) UNSIGNED NOT NULL DEFAULT '0',
-
-        PRIMARY KEY  (cid),
-        KEY  (aid),
-        KEY  (status),
-        KEY  (hostname),
-        KEY  (user_agent),
-        KEY  (adgroup),
-        KEY  (hostid),
-        KEY  (url)
-      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ ");
-
-      /**
-       * The ad_hosts table is used to configure users that can display ads
-       * remotely. 
-       */
-      db_query("CREATE TABLE {ad_hosts} (
-        uid INT UNSIGNED NOT NULL DEFAULT '0',
-
-        hostid varchar(32) DEFAULT '',
-
-        status INT(2) UNSIGNED NOT NULL DEFAULT '0',
-        description TEXT NULL,
-
-        PRIMARY KEY  (uid),
-        KEY  (status),
-        KEY  (hostid)
-      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ ");
-  }
+	drupal_install_schema(ad);
+}
 
-  drupal_set_message(t('The necessary ad module tables have been created.'));
+function ad_schema(){
+  $schema['ads'] = array(
+    'fields' => array(
+      'aid'    => array('type' => 'int','size' => 'small', 'unsigned' => TRUE, 'not null' => TRUE,'default' => 0),
+      'uid'    => array('type' => 'int','size' => 'small','unsigned' => TRUE, 'not null' => TRUE,'default' => 0),
+      'adstatus'    => array('type' => 'varchar','length'=>255, 'not null' => TRUE, 'default' => ''),
+      'adtype' => array('type' => 'varchar','length'=>255, 'not null' => TRUE, 'default' => ''),
+      'redirect' => array('type' => 'varchar','length' =>255 ,'not null' => TRUE, 'default' => ''),
+      'autoactivate'    => array('type' => 'int','size'=>'small', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
+      'autoactivated' => array('type' => 'int', 'size'=>'small', 'not null' => TRUE, 'default' => 0,'unsigned' => TRUE),
+      'autoexpired' => array('type' => 'int', 'size'=>'small', 'not null' => TRUE, 'default' => 0),
+      'autoexpire' => array('type' =>'int' , 'size' =>'small' , 'not null' => TRUE ,'default' => 0),      
+      'activated' => array('type' => 'int', 'size'=>'small', 'not null' => TRUE, 'default' => 0, 'unsigned' => TRUE),
+      'maxviews'    => array('type' => 'int','size'=>'small', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
+      'maxclicks' => array('type' => 'int', 'size'=>'small', 'not null' => TRUE, 'default' => 0, 'unsigned' => TRUE),
+      'expired' => array('type' => 'int', 'size'=>'small', 'not null' => TRUE, 'default' => 0, 'unsigned' => TRUE)
+    ),
+    'primary key' => array('aid'),
+    'indexes' => array(
+      'uid' => array('uid'),
+      'autoactivate' => array('autoactivate'),
+      'autoexpired' => array('autoexpired'),
+    ),
+ );
+   	$schema['ad_owners'] = array(
+    'fields' => array(
+      'oid'    => array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE),
+      'uid'    => array('type' => 'int','size'=>'small','unsigned' => TRUE, 'not null' => TRUE,'default' => 0),
+      'aid'    => array('type' => 'int', 'not null' => TRUE, 'default' => 0,'unsigned' => TRUE),
+    ),
+    'primary key' => array('oid'),
+    'indexes' => array(
+      'uid' => array('uid'),
+      'aid' => array('aid'),
+    ),
+   );
+   	$schema['ad_permissions'] = array(
+    'fields' => array(
+      'oid'    => array('type' => 'int','size'=>'small', 'unsigned' => TRUE, 'not null' => TRUE,'default' => 0),
+      'permissions'    => array('type' => 'text','size'=> 'big'),
+    ),
+    'primary key' => array('oid'),
+   );
+   	$schema['ad_statistics'] = array(
+  	  'fields' => array(
+    	  'sid'    => array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE),
+	      'aid'    => array('type' => 'int','size' =>'small','not null' => TRUE, 'default' => 0,'unsigned' => TRUE),    	  
+	      'date' => array('type' => 'int','size' => 'small','not null' => TRUE,'default' => 0,'unsigned' => TRUE),
+        'action' => array('type' => 'varchar','length'=>255, 'not null' => TRUE, 'default' => ''),
+        'adgroup' => array('type' => 'varchar','length'=>255, 'default' => ''),	      
+        'hostid' => array('type' => 'varchar','length'=>32, 'default' => ''),
+	      'count'    => array('type' => 'int','size' =>'small','not null' => TRUE, 'default' => 0,'unsigned' => TRUE),    	  
+    	),
+      'primary key' => array('sid'),
+      'indexes' => array(
+        'aid' => array('aid'),
+        'date' => array('date'),
+        'action' => array('action'),
+        'adgroup' => array('adgroup'),
+        'hostid' => array('hostid'),      
+      ),
+    );
+   	$schema['ad_clicks'] = array(
+  	  'fields' => array(
+    	  'cid'    => array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE),
+	      'aid'    => array('type' => 'int','size' =>'small','not null' => TRUE, 'default' => 0,'unsigned' => TRUE),    	  
+	      'uid' => array('type' => 'int','size' => 'small','not null' => TRUE,'default' => 0,'unsigned' => TRUE),
+        'status' => array('type' => 'int','size'=>'small', 'not null' => TRUE, 'default' =>0),
+        'hostname' => array('type' => 'varchar','length'=>128, 'not null' => TRUE, 'default' => ''),	      
+        'user_agent' => array('type' => 'varchar','length'=>255,'not null' => TRUE, 'default' => ''),
+	      'adgroup'    => array('type' => 'varchar','length' =>255,'not null' => TRUE, 'default' => ''),
+	      'hostid'    => array('type' => 'varchar','length' =>32,'not null' => TRUE, 'default' => ''),    	  
+ 	      'url'    => array('type' => 'varchar','length' =>255, 'default' => ''),    	  	          	  
+	      'timestamp'    => array('type' => 'int','size' => 'small' , 'not null' => TRUE, 'default' => 0,'unsigned' => TRUE),    	   	      
+    	),
+      'primary key' => array('cid'),
+      'indexes' => array(
+        'aid' => array('aid'),
+        'status' => array('status'),
+        'hostname' => array('hostname'),
+        'user_agent' => array('user_agent'),
+        'adgroup' => array('adgroup'),
+        'hostid' => array('hostid'),      
+        'url' => array('url'),                            
+      ),
+    );
+   	$schema['ad_hosts'] = array(
+  	  'fields' => array(
+	      'uid' => array('type' => 'int','size' => 'small','not null' => TRUE,'default' => 0,'unsigned' => TRUE),
+        'status' => array('type' => 'int','size'=>'small', 'not null' => TRUE, 'default' =>0),
+	      'hostid'    => array('type' => 'varchar','length' =>32,'not null' => TRUE, 'default' => ''),    	  
+        'description'    => array('type' => 'text','size'=> 'big'),	      
+    	),
+      'primary key' => array('uid'),
+      'indexes' => array(
+        'status' => array('status'),
+        'hostid' => array('hostid'),      
+      ),
+    );
+    return $schema;
 }
 
 /**
  * Allow complete uninstallation of the ad module.
  */
 function ad_uninstall() {
-  // Delete all ad content.
   $result = db_query("SELECT nid FROM {node} WHERE type = 'ad'");
+  
   while ($nid = db_result($result)) {
     node_delete($nid);
     variable_del("ad_autoactivate_warning_$nid");
   }
-
-  // Drop all ad module tables.
-  db_query('DROP TABLE {ad_clicks}');
-  db_query('DROP TABLE {ad_hosts}');
-  db_query('DROP TABLE {ad_owners}');
-  db_query('DROP TABLE {ad_permissions}');
-  db_query('DROP TABLE {ad_statistics}');
-  db_query('DROP TABLE {ads}');
-
-  // Delete all remaining ad module variables.
+  
+  drupal_uninstall_schema('ad');
+  
   $variables = array('ad_cron_timestamp', 'ad_link_target', 'ad_cache', 'ad_cache_file', 'adserve', 'ad_group_vid', 'ad_groups', 'ad_validate_url', 'ad_display');
   foreach ($variables as $variable) {
     variable_del($variable);
   }
-  // TODO: "ad_block_quantity_$delta"
+
 }
 
 /**
