diff options
-rwxr-xr-x | SQL/mssql.initial.sql | 206 | ||||
-rw-r--r-- | program/include/main.inc | 4 | ||||
-rwxr-xr-x | program/include/rcube_db.inc | 32 | ||||
-rw-r--r-- | program/include/rcube_imap.inc | 6 | ||||
-rw-r--r-- | program/include/session.inc | 6 | ||||
-rw-r--r-- | program/steps/addressbook/save.inc | 6 | ||||
-rw-r--r-- | program/steps/mail/addcontact.inc | 2 |
7 files changed, 245 insertions, 17 deletions
diff --git a/SQL/mssql.initial.sql b/SQL/mssql.initial.sql new file mode 100755 index 000000000..59916e81f --- /dev/null +++ b/SQL/mssql.initial.sql @@ -0,0 +1,206 @@ +CREATE TABLE [dbo].[cache] (
+ [cache_id] [int] IDENTITY (1, 1) NOT NULL ,
+ [user_id] [int] NOT NULL ,
+ [session_id] [varchar] (32) COLLATE Latin1_General_CI_AI NULL ,
+ [cache_key] [varchar] (128) COLLATE Latin1_General_CI_AI NOT NULL ,
+ [created] [datetime] NOT NULL ,
+ [data] [text] COLLATE Latin1_General_CI_AI NOT NULL
+) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
+GO
+
+CREATE TABLE [dbo].[contacts] (
+ [contact_id] [int] IDENTITY (1, 1) NOT NULL ,
+ [user_id] [int] NOT NULL ,
+ [changed] [datetime] NOT NULL ,
+ [del] [char] (1) COLLATE Latin1_General_CI_AI NOT NULL ,
+ [name] [varchar] (128) COLLATE Latin1_General_CI_AI NOT NULL ,
+ [email] [varchar] (128) COLLATE Latin1_General_CI_AI NOT NULL ,
+ [firstname] [varchar] (128) COLLATE Latin1_General_CI_AI NOT NULL ,
+ [surname] [varchar] (128) COLLATE Latin1_General_CI_AI NOT NULL ,
+ [vcard] [text] COLLATE Latin1_General_CI_AI NULL
+) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
+GO
+
+CREATE TABLE [dbo].[identities] (
+ [identity_id] [int] IDENTITY (1, 1) NOT NULL ,
+ [user_id] [int] NOT NULL ,
+ [del] [char] (1) COLLATE Latin1_General_CI_AI NOT NULL ,
+ [standard] [char] (1) COLLATE Latin1_General_CI_AI NOT NULL ,
+ [name] [varchar] (128) COLLATE Latin1_General_CI_AI NOT NULL ,
+ [organization] [varchar] (128) COLLATE Latin1_General_CI_AI NOT NULL ,
+ [email] [varchar] (128) COLLATE Latin1_General_CI_AI NOT NULL ,
+ [reply-to] [varchar] (128) COLLATE Latin1_General_CI_AI NOT NULL ,
+ [bcc] [varchar] (128) COLLATE Latin1_General_CI_AI NOT NULL ,
+ [signature] [text] COLLATE Latin1_General_CI_AI NOT NULL
+) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
+GO
+
+CREATE TABLE [dbo].[messages] (
+ [message_id] [int] IDENTITY (1, 1) NOT NULL ,
+ [user_id] [int] NOT NULL ,
+ [del] [tinyint] NOT NULL ,
+ [cache_key] [varchar] (128) COLLATE Latin1_General_CI_AI NOT NULL ,
+ [created] [datetime] NOT NULL ,
+ [idx] [int] NOT NULL ,
+ [uid] [int] NOT NULL ,
+ [subject] [varchar] (255) COLLATE Latin1_General_CI_AI NOT NULL ,
+ [from] [varchar] (255) COLLATE Latin1_General_CI_AI NOT NULL ,
+ [to] [varchar] (255) COLLATE Latin1_General_CI_AI NOT NULL ,
+ [cc] [varchar] (255) COLLATE Latin1_General_CI_AI NOT NULL ,
+ [date] [datetime] NOT NULL ,
+ [size] [int] NOT NULL ,
+ [headers] [text] COLLATE Latin1_General_CI_AI NOT NULL ,
+ [structure] [text] COLLATE Latin1_General_CI_AI NULL
+) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
+GO
+
+CREATE TABLE [dbo].[session] (
+ [sess_id] [varchar] (32) COLLATE Latin1_General_CI_AI NOT NULL ,
+ [created] [datetime] NOT NULL ,
+ [changed] [datetime] NULL ,
+ [ip] [varchar] (15) COLLATE Latin1_General_CI_AI NOT NULL ,
+ [vars] [text] COLLATE Latin1_General_CI_AI NOT NULL
+) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
+GO
+
+CREATE TABLE [dbo].[users] (
+ [user_id] [int] IDENTITY (1, 1) NOT NULL ,
+ [username] [varchar] (128) COLLATE Latin1_General_CI_AI NOT NULL ,
+ [mail_host] [varchar] (128) COLLATE Latin1_General_CI_AI NOT NULL ,
+ [alias] [varchar] (128) COLLATE Latin1_General_CI_AI NOT NULL ,
+ [created] [datetime] NOT NULL ,
+ [last_login] [datetime] NULL ,
+ [language] [varchar] (5) COLLATE Latin1_General_CI_AI NOT NULL ,
+ [preferences] [text] COLLATE Latin1_General_CI_AI NOT NULL
+) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
+GO
+
+ALTER TABLE [dbo].[cache] WITH NOCHECK ADD
+ PRIMARY KEY CLUSTERED
+ (
+ [cache_id]
+ ) ON [PRIMARY]
+GO
+
+ALTER TABLE [dbo].[contacts] WITH NOCHECK ADD
+ CONSTRAINT [PK_contacts_contact_id] PRIMARY KEY CLUSTERED
+ (
+ [contact_id]
+ ) ON [PRIMARY]
+GO
+
+ALTER TABLE [dbo].[identities] WITH NOCHECK ADD
+ PRIMARY KEY CLUSTERED
+ (
+ [identity_id]
+ ) ON [PRIMARY]
+GO
+
+ALTER TABLE [dbo].[messages] WITH NOCHECK ADD
+ PRIMARY KEY CLUSTERED
+ (
+ [message_id]
+ ) ON [PRIMARY]
+GO
+
+ALTER TABLE [dbo].[session] WITH NOCHECK ADD
+ CONSTRAINT [PK_session_sess_id] PRIMARY KEY CLUSTERED
+ (
+ [sess_id]
+ ) ON [PRIMARY]
+GO
+
+ALTER TABLE [dbo].[users] WITH NOCHECK ADD
+ CONSTRAINT [PK_users_user_id] PRIMARY KEY CLUSTERED
+ (
+ [user_id]
+ ) ON [PRIMARY]
+GO
+
+ALTER TABLE [dbo].[cache] ADD
+ CONSTRAINT [DF_cache_user_id] DEFAULT ('0') FOR [user_id],
+ CONSTRAINT [DF_cache_session_id] DEFAULT (null) FOR [session_id],
+ CONSTRAINT [DF_cache_cache_key] DEFAULT ('') FOR [cache_key],
+ CONSTRAINT [DF_cache_created] DEFAULT (getdate()) FOR [created]
+GO
+
+ CREATE INDEX [IX_cache_user_id] ON [dbo].[cache]([user_id]) ON [PRIMARY]
+GO
+
+ CREATE INDEX [IX_cache_cache_key] ON [dbo].[cache]([cache_key]) ON [PRIMARY]
+GO
+
+ CREATE INDEX [IX_cache_session_id] ON [dbo].[cache]([session_id]) ON [PRIMARY]
+GO
+
+ALTER TABLE [dbo].[contacts] ADD
+ CONSTRAINT [DF_contacts_user_id] DEFAULT (0) FOR [user_id],
+ CONSTRAINT [DF_contacts_changed] DEFAULT (getdate()) FOR [changed],
+ CONSTRAINT [DF_contacts_del] DEFAULT ('0') FOR [del],
+ CONSTRAINT [DF_contacts_name] DEFAULT ('') FOR [name],
+ CONSTRAINT [DF_contacts_email] DEFAULT ('') FOR [email],
+ CONSTRAINT [DF_contacts_firstname] DEFAULT ('') FOR [firstname],
+ CONSTRAINT [DF_contacts_surname] DEFAULT ('') FOR [surname],
+ CONSTRAINT [CK_contacts_del] CHECK ([del] = '1' or [del] = '0')
+GO
+
+ CREATE INDEX [IX_contacts_user_id] ON [dbo].[contacts]([user_id]) ON [PRIMARY]
+GO
+
+ALTER TABLE [dbo].[identities] ADD
+ CONSTRAINT [DF_identities_user] DEFAULT ('0') FOR [user_id],
+ CONSTRAINT [DF_identities_del] DEFAULT ('0') FOR [del],
+ CONSTRAINT [DF_identities_standard] DEFAULT ('0') FOR [standard],
+ CONSTRAINT [DF_identities_name] DEFAULT ('') FOR [name],
+ CONSTRAINT [DF_identities_organization] DEFAULT ('') FOR [organization],
+ CONSTRAINT [DF_identities_email] DEFAULT ('') FOR [email],
+ CONSTRAINT [DF_identities_reply] DEFAULT ('') FOR [reply-to],
+ CONSTRAINT [DF_identities_bcc] DEFAULT ('') FOR [bcc],
+ CHECK ([standard] = '1' or [standard] = '0'),
+ CHECK ([del] = '1' or [del] = '0')
+GO
+
+ CREATE INDEX [IX_identities_user_id] ON [dbo].[identities]([user_id]) ON [PRIMARY]
+GO
+
+ALTER TABLE [dbo].[messages] ADD
+ CONSTRAINT [DF_messages_user_id] DEFAULT (0) FOR [user_id],
+ CONSTRAINT [DF_messages_del] DEFAULT (0) FOR [del],
+ CONSTRAINT [DF_messages_cache_key] DEFAULT ('') FOR [cache_key],
+ CONSTRAINT [DF_messages_created] DEFAULT (getdate()) FOR [created],
+ CONSTRAINT [DF_messages_idx] DEFAULT (0) FOR [idx],
+ CONSTRAINT [DF_messages_uid] DEFAULT (0) FOR [uid],
+ CONSTRAINT [DF_messages_subject] DEFAULT ('') FOR [subject],
+ CONSTRAINT [DF_messages_from] DEFAULT ('') FOR [from],
+ CONSTRAINT [DF_messages_to] DEFAULT ('') FOR [to],
+ CONSTRAINT [DF_messages_cc] DEFAULT ('') FOR [cc],
+ CONSTRAINT [DF_messages_date] DEFAULT (getdate()) FOR [date],
+ CONSTRAINT [DF_messages_size] DEFAULT (0) FOR [size]
+GO
+
+ CREATE INDEX [IX_messages_user_id] ON [dbo].[messages]([user_id]) ON [PRIMARY]
+GO
+
+ CREATE INDEX [IX_messages_cache_key] ON [dbo].[messages]([cache_key]) ON [PRIMARY]
+GO
+
+ CREATE INDEX [IX_messages_idx] ON [dbo].[messages]([idx]) ON [PRIMARY]
+GO
+
+ CREATE INDEX [IX_messages_uid] ON [dbo].[messages]([uid]) ON [PRIMARY]
+GO
+
+ALTER TABLE [dbo].[session] ADD
+ CONSTRAINT [DF_session_sess_id] DEFAULT ('') FOR [sess_id],
+ CONSTRAINT [DF_session_created] DEFAULT (getdate()) FOR [created],
+ CONSTRAINT [DF_session_ip] DEFAULT ('') FOR [ip]
+GO
+
+ALTER TABLE [dbo].[users] ADD
+ CONSTRAINT [DF_users_username] DEFAULT ('') FOR [username],
+ CONSTRAINT [DF_users_mail_host] DEFAULT ('') FOR [mail_host],
+ CONSTRAINT [DF_users_alias] DEFAULT ('') FOR [alias],
+ CONSTRAINT [DF_users_created] DEFAULT (getdate()) FOR [created],
+ CONSTRAINT [DF_users_language] DEFAULT ('en') FOR [language]
+GO
+
diff --git a/program/include/main.inc b/program/include/main.inc index 051d6e250..7458fe131 100644 --- a/program/include/main.inc +++ b/program/include/main.inc @@ -466,7 +466,7 @@ function rcmail_login($user, $pass, $host=NULL) // update user's record $DB->query("UPDATE ".get_table_name('users')." - SET last_login=now() + SET last_login=".$DB->now()." WHERE user_id=?", $user_id); } @@ -511,7 +511,7 @@ function rcmail_create_user($user, $host) $DB->query("INSERT INTO ".get_table_name('users')." (created, last_login, username, mail_host, alias, language) - VALUES (now(), now(), ?, ?, ?, ?)", + VALUES (".$DB->now().", ".$DB->now().", ?, ?, ?, ?)", $user, $host, $user_email, diff --git a/program/include/rcube_db.inc b/program/include/rcube_db.inc index 5b32798a7..a01b38dd6 100755 --- a/program/include/rcube_db.inc +++ b/program/include/rcube_db.inc @@ -292,13 +292,14 @@ class rcube_db switch($this->db_provider) { case 'pgsql': - // PostgreSQL uses sequences $result = &$this->db_handle->getOne("SELECT CURRVAL('$sequence')"); + + case 'mssql': + $result = &$this->db_handle->getOne("SELECT @@IDENTITY"); + if (DB::isError($result)) - { raise_error(array('code' => 500, 'type' => 'db', 'line' => __LINE__, 'file' => __FILE__, 'message' => $result->getMessage()), TRUE, FALSE); - } return $result; @@ -421,6 +422,25 @@ class rcube_db } + /* + * Return SQL function for current time and date + * + * @return string SQL function to use in query + * @access public + */ + function now() + { + switch($this->db_provider) + { + case 'mssql': + return "getdate()"; + + default: + return "now()"; + } + } + + /** * Return SQL statement to convert a field value into a unix timestamp * @@ -434,7 +454,9 @@ class rcube_db { case 'pgsql': return "EXTRACT (EPOCH FROM $field)"; - break; + + case 'mssql': + return "datediff(s, '1970-01-01 00:00:00', $field)"; default: return "UNIX_TIMESTAMP($field)"; @@ -456,7 +478,7 @@ class rcube_db case 'mysqli': case 'mysql': case 'sqlite': - return "FROM_UNIXTIME($timestamp)"; + return sprintf("FROM_UNIXTIME(%d)", $timestamp); default: return date("'Y-m-d H:i:s'", $timestamp); diff --git a/program/include/rcube_imap.inc b/program/include/rcube_imap.inc index 23636dc35..a11c74910 100644 --- a/program/include/rcube_imap.inc +++ b/program/include/rcube_imap.inc @@ -1764,7 +1764,7 @@ class rcube_imap { $this->db->query( "UPDATE ".get_table_name('cache')." - SET created=now(), + SET created=".$this->db->now().", data=? WHERE user_id=? AND cache_key=?", @@ -1778,7 +1778,7 @@ class rcube_imap $this->db->query( "INSERT INTO ".get_table_name('cache')." (created, user_id, cache_key, data) - VALUES (now(), ?, ?, ?)", + VALUES (".$this->db->now().", ?, ?, ?)", $_SESSION['user_id'], $key, $data); @@ -1967,7 +1967,7 @@ class rcube_imap $this->db->query( "INSERT INTO ".get_table_name('messages')." (user_id, del, cache_key, created, idx, uid, subject, ".$this->db->quoteIdentifier('from').", ".$this->db->quoteIdentifier('to').", cc, date, size, headers, structure) - VALUES (?, 0, ?, now(), ?, ?, ?, ?, ?, ?, ".$this->db->fromunixtime($headers->timestamp).", ?, ?, ?)", + VALUES (?, 0, ?, ".$this->db->now().", ?, ?, ?, ?, ?, ?, ".$this->db->fromunixtime($headers->timestamp).", ?, ?, ?)", $_SESSION['user_id'], $key, $index, diff --git a/program/include/session.inc b/program/include/session.inc index dc362f8f9..00ca29915 100644 --- a/program/include/session.inc +++ b/program/include/session.inc @@ -70,7 +70,7 @@ function sess_write($key, $vars) session_decode($vars); $DB->query("UPDATE ".get_table_name('session')." SET vars=?, - changed=now() + changed=".$DB->now()." WHERE sess_id=?", $vars, $key); @@ -79,7 +79,7 @@ function sess_write($key, $vars) { $DB->query("INSERT INTO ".get_table_name('session')." (sess_id, vars, ip, created, changed) - VALUES (?, ?, ?, now(), now())", + VALUES (?, ?, ?, ".$DB->now().", ".$DB->now().")", $key, $vars, $_SERVER['REMOTE_ADDR']); @@ -118,7 +118,7 @@ function sess_gc($maxlifetime) // get all expired sessions $sql_result = $DB->query("SELECT sess_id FROM ".get_table_name('session')." - WHERE ".$DB->unixtimestamp('now()')."-".$DB->unixtimestamp('changed')." > ?", + WHERE ".$DB->unixtimestamp($DB->now())."-".$DB->unixtimestamp('changed')." > ?", $maxlifetime); $a_exp_sessions = array(); diff --git a/program/steps/addressbook/save.inc b/program/steps/addressbook/save.inc index f6b8b3cc2..abea316e5 100644 --- a/program/steps/addressbook/save.inc +++ b/program/steps/addressbook/save.inc @@ -50,7 +50,7 @@ if (!empty($_POST['_cid'])) if (sizeof($a_write_sql)) { $DB->query("UPDATE $contacts_table - SET changed=now(), ".join(', ', $a_write_sql)." + SET changed=".$DB->now().", ".join(', ', $a_write_sql)." WHERE contact_id=? AND user_id=? AND del<>1", @@ -172,8 +172,8 @@ else if (sizeof($a_insert_cols)) { $DB->query("INSERT INTO $contacts_table - (user_id, changed, del, ".join(', ', $a_insert_cols).") - VALUES (?, now(), 0, ".join(', ', $a_insert_values).")", + (user_id, changed, del, ".join(', ', $a_insert_cols).") + VALUES (?, ".$DB->now().", 0, ".join(', ', $a_insert_values).")", $_SESSION['user_id']); $insert_id = $DB->insert_id(get_sequence_name('contacts')); diff --git a/program/steps/mail/addcontact.inc b/program/steps/mail/addcontact.inc index a0e1e9394..b040581aa 100644 --- a/program/steps/mail/addcontact.inc +++ b/program/steps/mail/addcontact.inc @@ -43,7 +43,7 @@ if (!empty($_GET['_address'])) { $DB->query("INSERT INTO ".get_table_name('contacts')." (user_id, changed, del, name, email) - VALUES (?, now(), 0, ?, ?)", + VALUES (?, ".$DB->now().", 0, ?, ?)", $_SESSION['user_id'], $contact['name'], $contact['mailto']); |