Search in sources :

Example 16 with DbConnection

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

the class DbMailItem method resolveSharedIndex.

public static void resolveSharedIndex(Mailbox mbox, PendingDelete info) throws ServiceException {
    if (info.sharedIndex == null || info.sharedIndex.isEmpty()) {
        return;
    }
    List<Integer> indexIDs = new ArrayList<Integer>(info.sharedIndex);
    DbConnection conn = mbox.getOperationConnection();
    PreparedStatement stmt = null;
    ResultSet rs = null;
    try {
        for (int i = 0; i < indexIDs.size(); i += Db.getINClauseBatchSize()) {
            int count = Math.min(Db.getINClauseBatchSize(), indexIDs.size() - i);
            stmt = conn.prepareStatement("SELECT index_id FROM " + getMailItemTableName(mbox) + " WHERE " + IN_THIS_MAILBOX_AND + DbUtil.whereIn("index_id", count));
            int pos = 1;
            pos = setMailboxId(stmt, mbox, pos);
            for (int index = i; index < i + count; index++) {
                stmt.setInt(pos++, indexIDs.get(index));
            }
            rs = stmt.executeQuery();
            while (rs.next()) {
                info.sharedIndex.remove(rs.getInt(1));
            }
            rs.close();
            rs = null;
            stmt.close();
            stmt = null;
        }
        info.indexIds.addAll(info.sharedIndex);
        info.sharedIndex.clear();
    } catch (SQLException e) {
        throw ServiceException.FAILURE("resolving shared index entries", e);
    } finally {
        DbPool.closeResults(rs);
        DbPool.closeStatement(stmt);
    }
}
Also used : SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) DbConnection(com.zimbra.cs.db.DbPool.DbConnection)

Example 17 with DbConnection

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

the class DbMailItem method getLeafNodes.

public static PendingDelete getLeafNodes(Mailbox mbox, QueryParams params) throws ServiceException {
    DbConnection conn = mbox.getOperationConnection();
    PreparedStatement stmt = null;
    ResultSet rs = null;
    try {
        StringBuilder buf = new StringBuilder();
        buf.append("SELECT " + LEAF_NODE_FIELDS + " FROM " + getMailItemTableName(mbox) + " WHERE " + IN_THIS_MAILBOX_AND + "type NOT IN " + FOLDER_TYPES);
        String whereClause = params.getWhereClause();
        if (!StringUtil.isNullOrEmpty(whereClause)) {
            buf.append(" AND ").append(whereClause);
        }
        String limitClause = params.getLimitClause();
        if (!StringUtil.isNullOrEmpty(limitClause)) {
            buf.append(" ").append(limitClause);
        }
        stmt = conn.prepareStatement(buf.toString());
        // Assume we're dealing with a very large result set.
        Db.getInstance().enableStreaming(stmt);
        int pos = 1;
        pos = setMailboxId(stmt, mbox, pos);
        PendingDelete info = accumulateDeletionInfo(mbox, stmt);
        stmt = null;
        return info;
    } catch (SQLException e) {
        throw ServiceException.FAILURE("fetching list of items to delete", e);
    } finally {
        DbPool.closeResults(rs);
        DbPool.closeStatement(stmt);
    }
}
Also used : SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) DbConnection(com.zimbra.cs.db.DbPool.DbConnection) PendingDelete(com.zimbra.cs.mailbox.MailItem.PendingDelete)

Example 18 with DbConnection

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

the class DbMailItem method update.

public void update(MailItem item, Metadata metadata) throws ServiceException {
    String name = item.getName().isEmpty() ? null : item.getName();
    checkNamingConstraint(mailbox, item.getFolderId(), name, item.getId());
    DbConnection conn = mailbox.getOperationConnection();
    PreparedStatement stmt = null;
    try {
        stmt = conn.prepareStatement("UPDATE " + getMailItemTableName(item) + " SET type = ?, imap_id = ?, index_id = ?, parent_id = ?, date = ?, size = ?, flags = ?," + "  blob_digest = ?, sender = ?, recipients = ?, subject = ?, name = ?," + "  metadata = ?, mod_metadata = ?, change_date = ?, mod_content = ?, locator = ?" + " WHERE " + IN_THIS_MAILBOX_AND + "id = ?");
        int pos = 1;
        stmt.setByte(pos++, item.getType().toByte());
        if (item.getImapUid() >= 0) {
            stmt.setInt(pos++, item.getImapUid());
        } else {
            stmt.setNull(pos++, Types.INTEGER);
        }
        if (item.getIndexStatus() == MailItem.IndexStatus.NO) {
            stmt.setNull(pos++, Types.INTEGER);
        } else {
            stmt.setInt(pos++, item.getIndexId());
        }
        // messages in virtual conversations are stored with a null parent_id
        if (item.getParentId() <= 0) {
            stmt.setNull(pos++, Types.INTEGER);
        } else {
            stmt.setInt(pos++, item.getParentId());
        }
        stmt.setInt(pos++, (int) (item.getDate() / 1000));
        stmt.setLong(pos++, item.getSize());
        stmt.setLong(pos++, item.getInternalFlagBitmask());
        stmt.setString(pos++, item.getDigest());
        stmt.setString(pos++, item.getSortSender());
        stmt.setString(pos++, item.getSortRecipients());
        stmt.setString(pos++, item.getSortSubject());
        stmt.setString(pos++, name);
        stmt.setString(pos++, checkMetadataLength(metadata.toString()));
        stmt.setInt(pos++, mailbox.getOperationChangeID());
        stmt.setInt(pos++, mailbox.getOperationTimestamp());
        stmt.setInt(pos++, item.getSavedSequence());
        stmt.setString(pos++, item.getLocator());
        pos = setMailboxId(stmt, mailbox, pos);
        stmt.setInt(pos++, item.getId());
        stmt.executeUpdate();
        if (mailbox.isItemModified(item, Change.FLAGS)) {
            DbTag.updateTagReferences(mailbox, item.getId(), item.getType(), item.getInternalFlagBitmask(), item.isUnread(), item.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(item.getName(), e);
        } else {
            throw ServiceException.FAILURE("Failed to update item mbox=" + mailbox.getId() + ",id=" + item.getId(), e);
        }
    } finally {
        DbPool.closeStatement(stmt);
    }
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) DbConnection(com.zimbra.cs.db.DbPool.DbConnection)

Example 19 with DbConnection

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

the class DbMailItem method reparentChildren.

public static void reparentChildren(MailItem oldParent, MailItem newParent) throws ServiceException {
    if (oldParent == newParent) {
        return;
    }
    Mailbox mbox = oldParent.getMailbox();
    if (mbox != newParent.getMailbox()) {
        throw MailServiceException.WRONG_MAILBOX();
    }
    DbConnection conn = mbox.getOperationConnection();
    PreparedStatement stmt = null;
    try {
        String relation = (oldParent instanceof VirtualConversation ? "id = ?" : "parent_id = ?");
        stmt = conn.prepareStatement("UPDATE " + getMailItemTableName(oldParent) + " SET parent_id = ?, mod_metadata = ?, change_date = ?" + " WHERE " + IN_THIS_MAILBOX_AND + relation);
        int pos = 1;
        if (newParent instanceof VirtualConversation) {
            stmt.setNull(pos++, Types.INTEGER);
        } else {
            stmt.setInt(pos++, newParent.getId());
        }
        stmt.setInt(pos++, mbox.getOperationChangeID());
        stmt.setInt(pos++, mbox.getOperationTimestamp());
        pos = setMailboxId(stmt, mbox, pos);
        stmt.setInt(pos++, oldParent instanceof VirtualConversation ? ((VirtualConversation) oldParent).getMessageId() : oldParent.getId());
        stmt.executeUpdate();
    } catch (SQLException e) {
        throw ServiceException.FAILURE("writing new parent for children of item " + oldParent.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) VirtualConversation(com.zimbra.cs.mailbox.VirtualConversation)

Example 20 with DbConnection

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

the class DbMailItem method getCalendarItems.

/**
     * Return all of the Invite records within the range start&lt;=Invites&lt;end.  IE "Give me all the
     * invites between 7:00 and 9:00" will return you everything from 7:00 to 8:59:59.99
     * @param start     start time of range, in milliseconds. {@code -1} means to leave the start time unconstrained.
     * @param end       end time of range, in milliseconds. {@code -1} means to leave the end time unconstrained.
     * @param folderId
     * @return list of invites
     */
public static List<UnderlyingData> getCalendarItems(Mailbox mbox, MailItem.Type type, long start, long end, int folderId, int[] excludeFolderIds) throws ServiceException {
    DbConnection conn = mbox.getOperationConnection();
    PreparedStatement stmt = null;
    ResultSet rs = null;
    try {
        stmt = calendarItemStatement(conn, DB_FIELDS, mbox, type, start, end, folderId, excludeFolderIds);
        rs = stmt.executeQuery();
        List<UnderlyingData> result = new ArrayList<UnderlyingData>();
        while (rs.next()) {
            result.add(constructItem(rs));
        }
        return result;
    } catch (SQLException e) {
        throw ServiceException.FAILURE("fetching calendar items for mailbox " + mbox.getId(), 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) ArrayList(java.util.ArrayList) 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