Search in sources :

Example 26 with DbConnection

use of com.zimbra.cs.db.DbPool.DbConnection in project zm-mailbox by Zimbra.

the class DbImapFolder method getImapFolder.

public static ImapFolder getImapFolder(Mailbox mbox, DataSource ds, int itemId) throws ServiceException {
    DbConnection conn = null;
    PreparedStatement stmt = null;
    ResultSet rs = null;
    try {
        conn = DbPool.getConnection(mbox);
        stmt = conn.prepareStatement("SELECT local_path, remote_path, uid_validity" + " FROM " + getTableName(mbox) + " WHERE " + DbMailItem.IN_THIS_MAILBOX_AND + "item_id = ? ");
        int pos = DbMailItem.setMailboxId(stmt, mbox, 1);
        stmt.setInt(pos, itemId);
        rs = stmt.executeQuery();
        if (!rs.next())
            return null;
        String localPath = rs.getString("local_path");
        String remotePath = rs.getString("remote_path");
        Long uidValidity = rs.getLong("uid_validity");
        if (rs.wasNull()) {
            uidValidity = null;
        }
        return new ImapFolder(ds, itemId, remotePath, localPath, uidValidity);
    } catch (SQLException e) {
        throw ServiceException.FAILURE("Unable to get IMAP folder data", e);
    } finally {
        DbPool.closeResults(rs);
        DbPool.closeStatement(stmt);
        DbPool.quietClose(conn);
    }
}
Also used : ImapFolder(com.zimbra.cs.datasource.imap.ImapFolder) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) DbConnection(com.zimbra.cs.db.DbPool.DbConnection)

Example 27 with DbConnection

use of com.zimbra.cs.db.DbPool.DbConnection in project zm-mailbox by Zimbra.

the class DbImapFolder method deleteImapData.

/**
     * Deletes all IMAP message data for the given mailbox/data source.
     */
public static void deleteImapData(Mailbox mbox, String dataSourceId) throws ServiceException {
    ZimbraLog.datasource.info("Deleting IMAP data for DataSource %s", dataSourceId);
    if (StringUtil.isNullOrEmpty(dataSourceId))
        return;
    DbConnection conn = null;
    PreparedStatement stmt = null;
    try {
        // Note: data in imap_message gets deleted implicitly by the
        // foreign key cascading delete
        conn = DbPool.getConnection(mbox);
        stmt = conn.prepareStatement("DELETE FROM " + getTableName(mbox) + " WHERE " + DbMailItem.IN_THIS_MAILBOX_AND + "data_source_id = ?");
        int pos = 1;
        pos = DbMailItem.setMailboxId(stmt, mbox, pos);
        stmt.setString(pos++, dataSourceId);
        stmt.executeUpdate();
        conn.commit();
    } catch (SQLException e) {
        throw ServiceException.FAILURE("Unable to delete IMAP data", e);
    } finally {
        DbPool.closeStatement(stmt);
        DbPool.quietClose(conn);
    }
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) DbConnection(com.zimbra.cs.db.DbPool.DbConnection)

Example 28 with DbConnection

use of com.zimbra.cs.db.DbPool.DbConnection in project zm-mailbox by Zimbra.

the class DbImapFolder method deleteImapFolder.

/**
     * Deletes IMAP folder and message data for the given folder.
     */
public static void deleteImapFolder(Mailbox mbox, DataSource ds, ImapFolder folder) throws ServiceException {
    ZimbraLog.datasource.info("Deleting IMAP data for %s in %s", folder, ds);
    DbConnection conn = null;
    PreparedStatement stmt = null;
    try {
        // Note: data in imap_message gets deleted implicitly by the
        // foreign key cascading delete
        conn = DbPool.getConnection(mbox);
        stmt = conn.prepareStatement("DELETE FROM " + getTableName(mbox) + " WHERE " + DbMailItem.IN_THIS_MAILBOX_AND + "data_source_id = ? and item_id = ?");
        int pos = 1;
        pos = DbMailItem.setMailboxId(stmt, mbox, pos);
        stmt.setString(pos++, ds.getId());
        stmt.setInt(pos++, folder.getItemId());
        stmt.executeUpdate();
        conn.commit();
    } catch (SQLException e) {
        throw ServiceException.FAILURE("Unable to delete IMAP folder", e);
    } finally {
        DbPool.closeStatement(stmt);
        DbPool.quietClose(conn);
    }
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) DbConnection(com.zimbra.cs.db.DbPool.DbConnection)

Example 29 with DbConnection

use of com.zimbra.cs.db.DbPool.DbConnection in project zm-mailbox by Zimbra.

the class DbImapFolder method createImapFolder.

public static ImapFolder createImapFolder(Mailbox mbox, DataSource ds, int itemId, String localPath, String remotePath, long uidValidity) throws ServiceException {
    DbConnection conn = null;
    PreparedStatement stmt = null;
    try {
        conn = DbPool.getConnection(mbox);
        ZimbraLog.datasource.debug("createImapFolder: itemId = %d, localPath = %s, remotePath = %s, uidValidity = %d", itemId, localPath, remotePath, uidValidity);
        stmt = conn.prepareStatement("INSERT INTO " + getTableName(mbox) + " (" + DbMailItem.MAILBOX_ID + "item_id, data_source_id, local_path, remote_path, uid_validity) " + "VALUES (" + DbMailItem.MAILBOX_ID_VALUE + "?, ?, ?, ?, ?)");
        int pos = 1;
        pos = DbMailItem.setMailboxId(stmt, mbox, pos);
        stmt.setInt(pos++, itemId);
        stmt.setString(pos++, ds.getId());
        stmt.setString(pos++, localPath);
        stmt.setString(pos++, remotePath);
        stmt.setLong(pos++, uidValidity);
        stmt.executeUpdate();
        conn.commit();
        return new ImapFolder(ds, itemId, remotePath, localPath, uidValidity);
    } catch (SQLException e) {
        throw ServiceException.FAILURE("Unable to store IMAP message data", e);
    } finally {
        DbPool.closeStatement(stmt);
        DbPool.quietClose(conn);
    }
}
Also used : ImapFolder(com.zimbra.cs.datasource.imap.ImapFolder) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) DbConnection(com.zimbra.cs.db.DbPool.DbConnection)

Example 30 with DbConnection

use of com.zimbra.cs.db.DbPool.DbConnection in project zm-mailbox by Zimbra.

the class DbImapMessage method getMovedMessages.

public static List<ImapMessage> getMovedMessages(Mailbox mbox, DataSource ds, int folderId) throws ServiceException {
    List<ImapMessage> msgs = new ArrayList<ImapMessage>();
    DbConnection conn = null;
    PreparedStatement stmt = null;
    ResultSet rs = null;
    try {
        conn = DbPool.getConnection();
        stmt = conn.prepareStatement(String.format("SELECT imap.uid, imap.item_id, imap.flags as tflags, mi.unread, mi.flags " + " FROM %s imap JOIN %s mi " + " ON imap.mailbox_id = mi.mailbox_id AND imap.item_id = mi.id " + " WHERE imap.mailbox_id = %d AND imap.imap_folder_id = %d AND mi.folder_id != %d", getTableName(mbox), DbMailItem.getMailItemTableName(mbox), mbox.getId(), folderId, folderId));
        rs = stmt.executeQuery();
        while (rs.next()) {
            long uid = rs.getLong("uid");
            int itemId = rs.getInt("item_id");
            int flags = rs.getInt("flags");
            int unread = rs.getInt("unread");
            int tflags = rs.getInt("tflags");
            flags = unread > 0 ? (flags | Flag.BITMASK_UNREAD) : (flags & ~Flag.BITMASK_UNREAD);
            msgs.add(new ImapMessage(ds, -1, itemId, tflags, uid, flags));
        }
        rs.close();
        stmt.close();
    } catch (SQLException e) {
        throw ServiceException.FAILURE("Unable to get IMAP message data", e);
    } finally {
        DbPool.closeResults(rs);
        DbPool.closeStatement(stmt);
        DbPool.quietClose(conn);
    }
    return msgs;
}
Also used : ImapMessage(com.zimbra.cs.datasource.imap.ImapMessage) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) DbConnection(com.zimbra.cs.db.DbPool.DbConnection)

Aggregations

DbConnection (com.zimbra.cs.db.DbPool.DbConnection)218 PreparedStatement (java.sql.PreparedStatement)165 SQLException (java.sql.SQLException)162 ResultSet (java.sql.ResultSet)85 Mailbox (com.zimbra.cs.mailbox.Mailbox)71 ArrayList (java.util.ArrayList)36 ServiceException (com.zimbra.common.service.ServiceException)23 UnderlyingData (com.zimbra.cs.mailbox.MailItem.UnderlyingData)18 HashMap (java.util.HashMap)10 Metadata (com.zimbra.cs.mailbox.Metadata)9 IOException (java.io.IOException)9 MailItem (com.zimbra.cs.mailbox.MailItem)8 TypedIdList (com.zimbra.cs.mailbox.util.TypedIdList)8 AccountServiceException (com.zimbra.cs.account.AccountServiceException)7 Folder (com.zimbra.cs.mailbox.Folder)7 List (java.util.List)6 DbVolume (com.zimbra.cs.db.DbVolume)5 PendingDelete (com.zimbra.cs.mailbox.MailItem.PendingDelete)5 VirtualConversation (com.zimbra.cs.mailbox.VirtualConversation)5 CreateVolume (com.zimbra.cs.redolog.op.CreateVolume)5