--- ad2/image/ad_image.install	2007-04-29 06:48:18.000000000 +1000
+++ ad/image/ad_image.install	2007-05-09 16:47:38.000000000 +1000
@@ -9,6 +9,33 @@
 
 function ad_image_install() {
   switch ($GLOBALS['db_type']) {
+    case 'pgsql':
+      /**
+       * The ad_image_format table provides format guidelines for a given group
+       * of image ads.
+       */
+      db_query("CREATE TABLE {ad_image_format} (
+        gid INT NOT NULL UNIQUE PRIMARY KEY,
+
+        min_width INT NOT NULL DEFAULT '0',
+        max_width INT NOT NULL DEFAULT '0',
+        min_height INT NOT NULL DEFAULT '0',
+        max_height INT NOT NULL DEFAULT '0'
+	);");
+
+      /**
+       * The ad_image table stores information about each image ad.
+       */
+      db_query("CREATE TABLE {ad_image} (
+        aid INT NOT NULL DEFAULT '0' UNIQUE,
+        fid INT NOT NULL DEFAULT '0',
+
+        url VARCHAR(255) NOT NULL DEFAULT '',
+        tooltip VARCHAR(255) NOT NULL DEFAULT '',
+        width INT NOT NULL DEFAULT '0',
+        height INT NOT NULL DEFAULT '0'
+	);");
+      break;
     case 'mysql':
     case 'mysqli':
     default:
@@ -49,6 +76,9 @@ function ad_image_install() {
 function ad_image_update_1() {
   $ret = array();
   switch ($GLOBALS['db_type']) {
+    case 'pgsql':
+      $ret[] = update_sql("ALTER TABLE {ad_image} ADD tooltip VARCHAR(255) NOT NULL DEFAULT ''");
+      break;
     default:
       $ret[] = update_sql("ALTER TABLE {ad_image} ADD tooltip VARCHAR(255) NOT NULL DEFAULT '' AFTER url");
   }

--- ad2/ad.install	2007-04-05 04:08:51.000000000 +1000
+++ ad/ad.install	2007-05-11 15:22:56.000000000 +1000
@@ -9,6 +9,119 @@
 
 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',
+        gid 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'
+	);");
+
+      /**
+       * All ads are assigned to at least one group.  The ad_groups table
+       * defines these groups.  It is possible for a group to be comprised of
+       * multiple ad_types.  Ads are generally displayed by group, so for
+       * example you may call a group 'header', and then use it to randomly
+       * display ads in your website's header area.
+       */
+      db_query("CREATE TABLE {ad_groups} (
+        gid SERIAL NOT NULL PRIMARY KEY,
+        name VARCHAR(255) NOT NULL DEFAULT '' UNIQUE,
+        description TEXT NOT NULL DEFAULT ''
+        );");
+
+      /**
+       * All ads must be assigned to a group.  To get started, we create the
+       * default group to which all new ads will be assigned.  The default 
+       * group can not be deleted or modified.  If a non-default group is 
+       * deleted, ads in that group are assigned to the default group.
+       */
+      db_query("INSERT INTO {ad_groups} (gid,name, description) VALUES(1, 'default', 'All new advertisements will be added to this default group until you create your own custom advertisement groups.');");
+
+      /**
+       * 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 '',
+        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',
+
+        hostname varchar(128) 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 '',
+        description TEXT NOT NULL DEFAULT ''
+	);");
+      break;
     case 'mysql':
     case 'mysqli':
     default:
@@ -158,6 +271,12 @@ function ad_install() {
 function ad_update_1() {
   $ret = array();
   switch ($GLOBALS['db_type']) {
+    case 'pgsql':
+      $ret[] = update_sql("ALTER TABLE {ads} ADD activated INT NOT NULL DEFAULT '0';");
+      $ret[] = update_sql("ALTER TABLE {ads} ADD maxviews INT NOT NULL DEFAULT '0';");
+      $ret[] = update_sql("ALTER TABLE {ads} ADD maxclicks INT NOT NULL DEFAULT '0';");
+      $ret[] = update_sql("ALTER TABLE {ads} ADD expired INT NOT NULL DEFAULT '0';");
+      break;
     case 'mysql':
     case 'mysqli':
     default:
@@ -176,6 +295,11 @@ function ad_update_1() {
 function ad_update_2() {
   $ret = array();
   switch ($GLOBALS['db_type']) {
+    case 'pgsql':
+      $ret[] = update_sql("DROP TABLE {ad_notifications}");
+      $ret[] = update_sql("ALTER TABLE {ads} DROP expire_notified");
+      $ret[] = update_sql("ALTER TABLE {ads} DROP renew_notified");
+      break;
     default:
       $ret[] = update_sql("DROP TABLE {ad_notifications}");
       $ret[] = update_sql("ALTER TABLE {ads} DROP expire_notified");

--- ad2/ad.module	2007-05-04 06:10:09.000000000 +1000
+++ ad/ad.module	2007-05-09 16:54:58.000000000 +1000
@@ -1166,7 +1166,14 @@ function ad_is_owner($aid, $account = NU
  */
 function ad_owners_add($aid, $uid, $default = array('access statistics', 'access click history', 'manage status')) {
   $node = node_load(array('nid' => $aid));
-  db_query('LOCK TABLES {ad_owners} WRITE');
+  switch ($GLOBALS['db_type']) {
+    case 'pgsql':
+      db_query('START TRANSACTION;');
+      break;
+    default:
+      db_query('LOCK TABLES {ad_owners} WRITE');
+      break;
+  }
   if (!db_result(db_query('SELECT oid FROM {ad_owners} WHERE uid = %d AND aid = %d', $uid, $aid))) {
     db_query('INSERT INTO {ad_owners} (aid, uid) VALUES(%d, %d)', $aid, $uid);
     $rc = db_affected_rows() ? 1 : 0;
@@ -1183,12 +1190,26 @@ function ad_owners_add($aid, $uid, $defa
     }
 
     $oid = db_result(db_query("SELECT oid FROM {ad_owners} WHERE aid = %d and uid = %d", $aid, $uid));
-    db_query('LOCK TABLES {ad_permissions} WRITE');
+    switch ($GLOBALS['db_type']) {
+      case 'pgsql':
+        db_query('START TRANSACTION;');
+        break;
+      default:
+        db_query('LOCK TABLES {ad_permissions} WRITE');
+        break;
+    }
     db_query('DELETE FROM {ad_permissions} WHERE oid = %d', $oid);
     db_query("INSERT INTO {ad_permissions} VALUES(%d, '%s')", $oid, implode('|,|', $permissions));
     module_invoke_all('adowners', 'add', $node, array('oid' => $oid, 'uid' => $uid, 'aid' => $aid));
   }
-  db_query('UNLOCK TABLES');
+  switch ($GLOBALS['db_type']) {
+    case 'pgsql':
+      db_query('COMMIT;');
+      break;
+    default:
+      db_query('UNLOCK TABLES');
+      break;
+  }
   return $rc;
 }
 
@@ -1272,10 +1293,24 @@ function ad_owner_permissions_submit($fo
       $perms[] = $permission;
     }
   }
-  db_query('LOCK TABLES {ad_permissions} WRITE');
+  switch ($GLOBALS['db_type']) {
+    case 'pgsql':
+      db_query('START TRANSACTION;');
+      break;
+    default:
+      db_query('LOCK TABLES {ad_permissions} WRITE');
+      break;
+  }
   db_query('DELETE FROM {ad_permissions} WHERE oid = %d', $form_values['oid']);
   db_query("INSERT INTO {ad_permissions} VALUES(%d, '%s')", $form_values['oid'], implode('|,|', $perms));
-  db_query('UNLOCK TABLES');
+  switch ($GLOBALS['db_type']) {
+    case 'pgsql':
+      db_query('COMMIT;');
+      break;
+    default:
+      db_query('UNLOCK TABLES');
+      break;
+  }
   drupal_set_message(t('The permissions have been saved.'));
   return "node/$form_values[aid]/adowners";
 }

--- ad2/notify/ad_notify.install	2007-05-01 23:29:52.000000000 +1000
+++ ad/notify/ad_notify.install	2007-05-11 15:25:15.000000000 +1000
@@ -8,6 +8,37 @@
  */
 function ad_notify_install() {
   switch ($GLOBALS['db_type']) {
+    case 'pgsql':
+    /**
+     * Notifications can be granted to each owner of each ad.  The same owner
+     * can own multiple ads, and can have different notifications for each
+     * ad.  Notifications are defined by their type and an offset in seconds.
+     * For example, 'day, 0' would send a notification at the start of 
+     * every day, and 'expire, -86400' would send a notification one day
+     * before the ad expires.
+     */
+    db_query("CREATE TABLE {ad_notify} (
+      notid SERIAL NOT NULL PRIMARY KEY,
+      aid INT NOT NULL DEFAULT '0',
+      oid INT NOT NULL DEFAULT '0',
+
+      event VARCHAR(255) NOT NULL DEFAULT '',
+      delay INT NOT NULL DEFAULT '0',
+      queued INT NOT NULL DEFAULT '0',
+      time INT NOT NULL DEFAULT '0',
+      sent INT NOT NULL DEFAULT '0',
+      counter INT NOT NULL DEFAULT '0',
+      locked INT NOT NULL DEFAULT '0',
+      expire INT NOT NULL DEFAULT '0',
+      status INT NOT NULL DEFAULT '0',
+
+      address VARCHAR(255) NOT NULL DEFAULT '',
+      subject VARCHAR(255) NOT NULL DEFAULT '',
+      body TEXT NOT NULL DEFAULT '',
+
+      UNIQUE (oid, event, delay)
+      );");
+      break;
     case 'mysql':
     case 'mysqli':
     default:

--- ad2/text/ad_text.install	2007-04-05 04:12:29.000000000 +1000
+++ ad/text/ad_text.install	2007-05-11 15:25:52.000000000 +1000
@@ -9,6 +9,19 @@
 
 function ad_text_install() {
   switch ($GLOBALS['db_type']) {
+    case 'pgsql':
+
+      /**
+       * The ad_text table stores each line of the actual text ad.
+       */
+      db_query("CREATE TABLE {ad_text} (
+        aid INT NOT NULL DEFAULT '0' PRIMARY KEY,
+
+        url VARCHAR(255) NOT NULL DEFAULT '',
+        adheader VARCHAR(255) NOT NULL DEFAULT '',
+        adbody TEXT NOT NULL DEFAULT ''
+        );");
+      break;
     case 'mysql':
     case 'mysqli':
     default:

