<?php
// $Id: privatemsg.install,v 1.1.2.2 2006/08/11 17:59:26 mindless Exp $

function privatemsg_install() {
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      db_query("CREATE TABLE {privatemsg} (
          id            int(10) unsigned NOT NULL auto_increment primary key,
          author        int(10) unsigned NOT NULL,
          recipient     int(10) unsigned NOT NULL,
          subject       varchar(64) NOT NULL,
          message       text NOT NULL,
          timestamp     int(11) unsigned NOT NULL,
          newmsg        tinyint unsigned NOT NULL,
          hostname      varchar(255) NOT NULL,
          folder        int(10) unsigned NOT NULL DEFAULT '0',
          author_del    tinyint unsigned NOT NULL,
          recipient_del tinyint unsigned NOT NULL,
          format        int(4) NOT NULL DEFAULT '0',
          key (recipient),
          key (folder)
        ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
      db_query("CREATE TABLE {privatemsg_folder} (
          fid           int(10) unsigned NOT NULL auto_increment primary key,
          uid           int(10) unsigned NOT NULL,
          name          varchar(255) NOT NULL
        ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
      // Sent messages
      db_query("INSERT INTO {privatemsg_folder} VALUES (1, 0, 'Sent')");
      db_query("CREATE TABLE {privatemsg_archive} (
          id            int(10) unsigned NOT NULL auto_increment primary key,
          author        int(10) unsigned NOT NULL,
          recipient     int(10) unsigned NOT NULL,
          subject       VARCHAR(64) NOT NULL,
          message       text NOT NULL,
          timestamp     int(11) unsigned NOT NULL,
          hostname      varchar(255) NOT NULL,
          folder        int(10) unsigned NOT NULL,
          format        int(4) NOT NULL DEFAULT '0',
          key (recipient)
        ) /*!40100 DEFAULT CHARACTER SET utf8 */;");
      break;
    case 'pgsql':
      db_query("CREATE TABLE {privatemsg} (
          id            SERIAL,
          author        integer NOT NULL,
          recipient     integer NOT NULL,
          subject       varchar(64) NOT NULL,
          message       text NOT NULL,
          timestamp     integer NOT NULL,
          newmsg        smallint NOT NULL,
          hostname      varchar(255) NOT NULL,
          format        smallint NOT NULL DEFAULT '0',
          folder        integer NOT NULL DEFAULT '0',
          author_del    smallint NOT NULL DEFAULT '0',
          recipient_del smallint NOT NULL DEFAULT '0',
          PRIMARY KEY (id)
        )");
      db_query("CREATE INDEX {privatemsg_folder_index} ON {privatemsg}(folder)");
      db_query("CREATE TABLE {privatemsg_folder} (
          fid           SERIAL,
          uid           integer NOT NULL,
          name          varchar(255) not null,
          PRIMARY KEY (fid)
        )");
      db_query("INSERT INTO {privatemsg_folder} (uid, name) VALUES (0, 'Sent')");
      db_query("CREATE TABLE {privatemsg_archive} (
          id            SERIAL,
          author        integer NOT NULL,
          recipient     integer NOT NULL,
          subject       varchar(64) NOT NULL,
          message       text NOT NULL,
          timestamp     integer NOT NULL,
          hostname      varchar(255) NOT NULL,
          format        smallint NOT NULL DEFAULT '0',
          folder        integer NOT NULL,
          PRIMARY KEY (id)
        )");
      db_query("CREATE INDEX {privatemsg_archive_recipient} ON {privatemsg_archive}(recipient)");
      db_query("create or replace function unix_timestamp(timestamp with time zone)
        returns int as '
        declare
           date alias for " .'$1'. ";
           timezero timestamp;
           offset interval;
        begin
           timezero := timestamp ''1970-1-1 00:00'' at time zone ''utc'';
           offset := date-timezero;

           return (extract(''days'' from offset)*86400+
                   extract(''hours'' from offset)*3600+
                   extract(''minutes'' from offset)*60+
                   extract(''seconds'' from offset))::int;
        end;
        ' language 'plpgsql'");
      db_query("create or replace function unix_timestamp(timestamp without time zone)
        returns int as '
        declare
           date alias for " .'$1'. ";
           timezero timestamp;
           offset interval;
        begin
           timezero := timestamp ''1970-1-1 00:00'' at time zone ''utc'';
           offset := date-timezero;

           return (extract(''days'' from offset)*86400+
                   extract(''hours'' from offset)*3600+
                   extract(''minutes'' from offset)*60+
                   extract(''seconds'' from offset))::int;
        end;
        ' language 'plpgsql'");
      break;
  }
}

/* Upgrade on mysql from versions before 22-May-2003:
   Create privatemsg_archive/privatemsg_folder tables and insert one row, shown above^
   ALTER TABLE privatemsg ADD folder int(10) unsigned NOT NULL;
   ALTER TABLE privatemsg ADD author_del tinyint unsigned NOT NULL;
   ALTER TABLE privatemsg ADD recipient_del tinyint unsigned NOT NULL;
   ALTER TABLE privatemsg ADD INDEX(folder);
   ALTER TABLE privatemsg CHANGE hostname hostname varchar(255) NOT NULL;
   Continue with steps below, but skip ALTER line for privatemsg_archive..
 *
 * Upgrade on mysql from versions before 29-Apr-2005:
   ALTER TABLE privatemsg CHANGE new newmsg tinyint UNSIGNED NOT NULL;
   ALTER TABLE privatemsg ADD format int(4) NOT NULL DEFAULT '0';
   ALTER TABLE privatemsg_archive ADD format int(4) NOT NULL DEFAULT '0';
   UPDATE privatemsg SET format=1;
   UPDATE privatemsg_archive SET format=1;
 */

function privatemsg_update_1() {
  return _system_update_utf8(array('privatemsg', 'privatemsg_archive', 'privatemsg_folder'));
}
?>
