Search in sources :

Example 61 with DbConnection

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

the class DbMailItem method saveDate.

public static void saveDate(MailItem item) throws ServiceException {
    Mailbox mbox = item.getMailbox();
    DbConnection conn = mbox.getOperationConnection();
    PreparedStatement stmt = null;
    try {
        stmt = conn.prepareStatement("UPDATE " + getMailItemTableName(mbox) + " SET date = ?, mod_metadata = ?, change_date = ? WHERE " + IN_THIS_MAILBOX_AND + "id = ?");
        int pos = 1;
        stmt.setInt(pos++, (int) (item.getDate() / 1000));
        stmt.setInt(pos++, mbox.getOperationChangeID());
        stmt.setInt(pos++, mbox.getOperationTimestamp());
        pos = setMailboxId(stmt, mbox, pos);
        stmt.setInt(pos++, item.getId());
        stmt.executeUpdate();
    } catch (SQLException e) {
        throw ServiceException.FAILURE("setting IMAP UID for item " + item.getId(), e);
    } finally {
        DbPool.closeStatement(stmt);
    }
}
Also used : Mailbox(com.zimbra.cs.mailbox.Mailbox) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) DbConnection(com.zimbra.cs.db.DbPool.DbConnection)

Example 62 with DbConnection

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

the class DbMailItem method icopy.

public static void icopy(MailItem source, UnderlyingData data, boolean shared) throws ServiceException {
    Mailbox mbox = source.getMailbox();
    if (data == null || data.id <= 0 || data.folderId <= 0 || data.parentId == 0) {
        throw ServiceException.FAILURE("invalid data for DB item i-copy", null);
    }
    checkNamingConstraint(mbox, data.folderId, source.getName(), data.id);
    DbConnection conn = mbox.getOperationConnection();
    PreparedStatement stmt = null;
    try {
        String table = getMailItemTableName(mbox);
        String mailbox_id = DebugConfig.disableMailboxGroups ? "" : "mailbox_id, ";
        String flags;
        if (!shared) {
            flags = "flags";
        } else if (Db.supports(Db.Capability.BITWISE_OPERATIONS)) {
            flags = "flags | " + Flag.BITMASK_COPIED;
        } else {
            flags = "CASE WHEN " + Db.getInstance().bitAND("flags", String.valueOf(Flag.BITMASK_COPIED)) + " <> 0 THEN flags ELSE flags + " + Flag.BITMASK_COPIED + " END";
        }
        stmt = conn.prepareStatement("INSERT INTO " + table + "(" + mailbox_id + " id, type, parent_id, folder_id, prev_folders, index_id, imap_id, date, size, locator, blob_digest," + " unread, flags, tag_names, sender, subject, name, metadata, mod_metadata, change_date, mod_content) " + "SELECT " + mailbox_id + " ?, type, parent_id, ?, ?, ?, ?, date, size, ?, blob_digest," + " unread, " + flags + ", tag_names, sender, subject, name, metadata, ?, ?, ? FROM " + table + " WHERE " + IN_THIS_MAILBOX_AND + "id = ?");
        int pos = 1;
        // ID
        stmt.setInt(pos++, data.id);
        // FOLDER_ID
        stmt.setInt(pos++, data.folderId);
        stmt.setString(pos++, data.getPrevFolders());
        if (data.indexId == MailItem.IndexStatus.NO.id()) {
            stmt.setNull(pos++, Types.INTEGER);
        } else {
            stmt.setInt(pos++, data.indexId);
        }
        // IMAP_ID
        stmt.setInt(pos++, data.imapId);
        stmt.setString(pos++, data.locator);
        // MOD_METADATA
        stmt.setInt(pos++, mbox.getOperationChangeID());
        // CHANGE_DATE
        stmt.setInt(pos++, mbox.getOperationTimestamp());
        // MOD_CONTENT
        stmt.setInt(pos++, mbox.getOperationChangeID());
        pos = setMailboxId(stmt, mbox, pos);
        stmt.setInt(pos++, source.getId());
        stmt.executeUpdate();
        stmt.close();
        boolean needsTag = shared && !source.isTagged(Flag.FlagInfo.COPIED);
        if (needsTag || source.getParentId() > 0) {
            boolean altersMODSEQ = source.getParentId() > 0;
            String updateChangeID = (altersMODSEQ ? ", mod_metadata = ?, change_date = ?" : "");
            stmt = conn.prepareStatement("UPDATE " + table + " SET parent_id = NULL, flags = " + flags + updateChangeID + " WHERE " + IN_THIS_MAILBOX_AND + "id = ?");
            pos = 1;
            if (altersMODSEQ) {
                stmt.setInt(pos++, mbox.getOperationChangeID());
                stmt.setInt(pos++, mbox.getOperationTimestamp());
            }
            pos = setMailboxId(stmt, mbox, pos);
            stmt.setInt(pos++, source.getId());
            stmt.executeUpdate();
            stmt.close();
        }
        if (source instanceof Message && source.getParentId() <= 0) {
            changeOpenTargets(source, data.id);
        }
        MailItem.Type type = MailItem.Type.of(data.type);
        DbTag.storeTagReferences(mbox, data.id, type, data.getFlags() | (shared ? Flag.BITMASK_COPIED : 0), data.unreadCount > 0);
        DbTag.storeTagReferences(mbox, data.id, type, data.getTags());
    } catch (SQLException e) {
        // catch item_id uniqueness constraint violation and return failure
        if (Db.errorMatches(e, Db.Error.DUPLICATE_ROW)) {
            throw MailServiceException.ALREADY_EXISTS(data.id, e);
        } else {
            throw ServiceException.FAILURE("i-copying " + source.getType() + ": " + source.getId(), e);
        }
    } finally {
        DbPool.closeStatement(stmt);
    }
}
Also used : MailItem(com.zimbra.cs.mailbox.MailItem) Mailbox(com.zimbra.cs.mailbox.Mailbox) ImapMessage(com.zimbra.cs.imap.ImapMessage) Message(com.zimbra.cs.mailbox.Message) Pop3Message(com.zimbra.cs.pop3.Pop3Message) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) DbConnection(com.zimbra.cs.db.DbPool.DbConnection)

Example 63 with DbConnection

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

the class DbMailItem method openConversation.

public static void openConversation(String hash, MailItem item) throws ServiceException {
    Mailbox mbox = item.getMailbox();
    DbConnection conn = mbox.getOperationConnection();
    PreparedStatement stmt = null;
    try {
        String command = Db.supports(Db.Capability.REPLACE_INTO) ? "REPLACE" : "INSERT";
        String mailbox_id = DebugConfig.disableMailboxGroups ? "" : "mailbox_id, ";
        stmt = conn.prepareStatement(command + " INTO " + getConversationTableName(item) + "(" + mailbox_id + "hash, conv_id)" + " VALUES (" + (DebugConfig.disableMailboxGroups ? "" : "?, ") + "?, ?)");
        int pos = 1;
        pos = setMailboxId(stmt, mbox, pos);
        stmt.setString(pos++, hash);
        stmt.setInt(pos++, item.getId());
        stmt.executeUpdate();
    } catch (SQLException e) {
        if (Db.errorMatches(e, Db.Error.DUPLICATE_ROW)) {
            try {
                DbPool.closeStatement(stmt);
                stmt = conn.prepareStatement("UPDATE " + getConversationTableName(item) + " SET conv_id = ? WHERE " + IN_THIS_MAILBOX_AND + "hash = ?");
                int pos = 1;
                stmt.setInt(pos++, item.getId());
                pos = setMailboxId(stmt, mbox, pos);
                stmt.setString(pos++, hash);
                stmt.executeUpdate();
            } catch (SQLException nested) {
                throw ServiceException.FAILURE("updating open conversation association for hash " + hash, nested);
            }
        } else {
            throw ServiceException.FAILURE("writing open conversation association for hash " + hash, e);
        }
    } finally {
        DbPool.closeStatement(stmt);
    }
}
Also used : Mailbox(com.zimbra.cs.mailbox.Mailbox) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) DbConnection(com.zimbra.cs.db.DbPool.DbConnection)

Example 64 with DbConnection

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

the class DbMailItem method getBy.

private static UnderlyingData getBy(LookupBy by, Mailbox mbox, String key, MailItem.Type type, boolean fromDumpster) throws ServiceException {
    if (Mailbox.isCachedType(type)) {
        throw ServiceException.INVALID_REQUEST("folders and tags must be retrieved from cache", null);
    }
    DbConnection conn = mbox.getOperationConnection();
    PreparedStatement stmt = null;
    ResultSet rs = null;
    try {
        String keyColumn = LookupBy.uuid.equals(by) ? "uuid" : "id";
        stmt = conn.prepareStatement("SELECT " + DB_FIELDS + " FROM " + getMailItemTableName(mbox, "mi", fromDumpster) + " WHERE " + IN_THIS_MAILBOX_AND + keyColumn + " = ?");
        int pos = 1;
        pos = setMailboxId(stmt, mbox, pos);
        stmt.setString(pos++, key);
        rs = stmt.executeQuery();
        if (!rs.next()) {
            if (LookupBy.uuid.equals(by)) {
                throw MailItem.noSuchItemUuid(key, type);
            } else {
                int id = Integer.parseInt(key);
                throw MailItem.noSuchItem(id, type);
            }
        }
        UnderlyingData data = constructItem(rs, fromDumpster);
        if (!MailItem.isAcceptableType(type, MailItem.Type.of(data.type))) {
            if (LookupBy.uuid.equals(by)) {
                throw MailItem.noSuchItemUuid(key, type);
            } else {
                int id = Integer.parseInt(key);
                throw MailItem.noSuchItem(id, type);
            }
        }
        if (!fromDumpster && data.type == MailItem.Type.CONVERSATION.toByte()) {
            completeConversation(mbox, conn, data);
        }
        return data;
    } catch (SQLException e) {
        if (!fromDumpster) {
            throw ServiceException.FAILURE("fetching item " + key, e);
        } else {
            throw ServiceException.FAILURE("fetching item " + key + " from dumpster", e);
        }
    } finally {
        DbPool.closeResults(rs);
        DbPool.closeStatement(stmt);
    }
}
Also used : SQLException(java.sql.SQLException) UnderlyingData(com.zimbra.cs.mailbox.MailItem.UnderlyingData) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) DbConnection(com.zimbra.cs.db.DbPool.DbConnection)

Example 65 with DbConnection

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

the class HSQLDB method clearDatabase.

/**
     * Deletes all records from all tables.
     * @param zimbraServerDir the directory that contains the ZimbraServer project
     * @throws Exception
     */
public static void clearDatabase(String zimbraServerDir) throws Exception {
    zimbraServerDir = Strings.nullToEmpty(zimbraServerDir);
    DbConnection conn = DbPool.getConnection();
    try {
        execute(conn, zimbraServerDir + "src/db/hsqldb/clear.sql");
    } finally {
        DbPool.quietClose(conn);
    }
}
Also used : 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