// imapd/ImapdDb.cc
// This file is part of Decimail; see http://decimail.org
// (C) 2004-2007 Philip Endecott
 
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
#include "ImapdDb.hh"


namespace Imapd {

ImapdDb::ImapdDb(void):

  q_get_completions
    (*this, "select realname || ' <' || email || '>' from realnames "
            "where realname ilike $1 || '%' or email ilike $1 || '%' "
            "group by realname,email order by count(*) desc "
            "limit $2"),

  q_get_create_action
    (*this, "select create_action from u_actions where $1 like mailbox_pattern"),
  q_get_delete_action
    (*this, "select delete_action from u_actions where $1 like mailbox_pattern"),
  q_get_rename_action
    (*this, "select rename_action from u_actions where $1 like mailbox_pattern"),
  q_get_copy_action
    (*this, "select copy_action from u_actions where $1 like mailbox_pattern"),

  q_get_deleted_messages
    (*this, "select msg_id from u_imap_message_flags where flag='\\\\Deleted' order by msg_id"),
  q_create_todelete
    (*this, "create temporary table todelete (msg_id integer not null primary key) on commit drop"),
  q_insert_todelete
    (*this, "insert into todelete(msg_id) values ($1)"),
  q_delete_todelete
    (*this, "delete from messages where msg_id in (select msg_id from todelete)"),

  q_get_mailbox_names
    (*this, "select distinct substring(mailbox_name from $2||'\\/?') from mailboxes "
            "where mailbox_name ~ $2 and username=$1"),
  q_get_subscribed_mailbox_names
    (*this, "select distinct substring(mailbox_name from $2||'\\/?') from imap_subscribed_mailboxes "
            "where mailbox_name ~ $2 and username=$1"),

  create_u_messages
    (*this, "create or replace temporary view u_messages as "
            "select * from messages where owner=$1"),
  create_u_recipients
    (*this, "create or replace temporary view u_recipients as "
            "select * from recipients "
            "where msg_id in (select msg_id from messages where owner=$1)"),
  create_u_actions
    (*this, "create or replace temporary view u_actions as "
            "select * from actions where username=$1"),
  create_u_imap_mailbox_flags
    (*this, "create or replace temporary view u_imap_mailbox_flags as "
            "select * from imap_mailbox_flags where username=$1"),
  create_u_imap_message_flags
    (*this, "create or replace temporary view u_imap_message_flags as "
            "select * from imap_message_flags where username=$1")
{
}

void ImapdDb::create_user_views(string username)
{
  create_u_messages.runonce(username);
  create_u_recipients.runonce(username);
  create_u_actions.runonce(username);
  create_u_imap_mailbox_flags.runonce(username);
  create_u_imap_message_flags.runonce(username);
}


};

