Search in sources :

Example 51 with DbConnection

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

the class DbMailItem method getByName.

public static UnderlyingData getByName(Mailbox mbox, int folderId, String name, MailItem.Type type) 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 {
        stmt = conn.prepareStatement("SELECT " + DB_FIELDS + " FROM " + getMailItemTableName(mbox, "mi") + " WHERE " + IN_THIS_MAILBOX_AND + "folder_id = ? AND " + typeIn(type) + " AND " + Db.equalsSTRING("name"));
        int pos = 1;
        pos = setMailboxId(stmt, mbox, pos);
        stmt.setInt(pos++, folderId);
        stmt.setString(pos++, name);
        rs = stmt.executeQuery();
        if (!rs.next()) {
            throw MailItem.noSuchItem(-1, type);
        }
        UnderlyingData data = constructItem(rs);
        MailItem.Type resultType = MailItem.Type.of(data.type);
        if (!MailItem.isAcceptableType(type, resultType)) {
            throw MailItem.noSuchItem(data.id, type);
        }
        if (resultType == MailItem.Type.CONVERSATION) {
            completeConversation(mbox, conn, data);
        }
        return data;
    } catch (SQLException e) {
        throw ServiceException.FAILURE("fetching item by name ('" + name + "' in folder " + folderId + ")", e);
    } finally {
        DbPool.closeResults(rs);
        DbPool.closeStatement(stmt);
    }
}
Also used : MailItem(com.zimbra.cs.mailbox.MailItem) 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 52 with DbConnection

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

the class DbMailItem method addToCalendarItemTable.

public static void addToCalendarItemTable(CalendarItem calItem) throws ServiceException {
    Mailbox mbox = calItem.getMailbox();
    long start = calItem.getStartTime();
    long end = calItem.getEndTime();
    Timestamp startTs = new Timestamp(start);
    Timestamp endTs = getEnd(start, end);
    DbConnection conn = mbox.getOperationConnection();
    PreparedStatement stmt = null;
    try {
        String mailbox_id = DebugConfig.disableMailboxGroups ? "" : "mailbox_id, ";
        stmt = conn.prepareStatement("INSERT INTO " + getCalendarItemTableName(mbox) + " (" + mailbox_id + "uid, item_id, start_time, end_time)" + " VALUES (" + (DebugConfig.disableMailboxGroups ? "" : "?, ") + "?, ?, ?, ?)");
        int pos = 1;
        pos = setMailboxId(stmt, mbox, pos);
        stmt.setString(pos++, calItem.getUid());
        stmt.setInt(pos++, calItem.getId());
        stmt.setTimestamp(pos++, startTs);
        stmt.setTimestamp(pos++, endTs);
        stmt.executeUpdate();
    } catch (SQLException e) {
        throw ServiceException.FAILURE("writing invite to calendar item table: UID=" + calItem.getUid(), e);
    } finally {
        DbPool.closeStatement(stmt);
    }
}
Also used : Mailbox(com.zimbra.cs.mailbox.Mailbox) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Timestamp(java.sql.Timestamp) DbConnection(com.zimbra.cs.db.DbPool.DbConnection)

Example 53 with DbConnection

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

the class DbMailbox method setSyncCutoff.

public static void setSyncCutoff(Mailbox mbox, int cutoff) throws ServiceException {
    DbConnection conn = mbox.getOperationConnection();
    PreparedStatement stmt = null;
    try {
        stmt = conn.prepareStatement("UPDATE " + qualifyZimbraTableName(mbox, TABLE_MAILBOX) + " SET tracking_sync = ? WHERE id = ? AND tracking_sync < ?");
        stmt.setInt(1, cutoff);
        stmt.setInt(2, mbox.getId());
        stmt.setInt(3, cutoff);
        stmt.executeUpdate();
    } catch (SQLException e) {
        throw ServiceException.FAILURE("turning on sync tracking for mailbox " + mbox.getId(), e);
    } finally {
        DbPool.closeStatement(stmt);
    }
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) DbConnection(com.zimbra.cs.db.DbPool.DbConnection)

Example 54 with DbConnection

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

the class DbMailbox method getConfig.

public static String getConfig(Mailbox mbox, String section) throws ServiceException {
    DbConnection conn = mbox.getOperationConnection();
    PreparedStatement stmt = null;
    ResultSet rs = null;
    try {
        stmt = conn.prepareStatement("SELECT metadata FROM " + qualifyZimbraTableName(mbox, TABLE_METADATA) + " WHERE " + DbMailItem.IN_THIS_MAILBOX_AND + "section = ?");
        int pos = 1;
        if (!DebugConfig.disableMailboxGroups) {
            stmt.setInt(pos++, mbox.getId());
        }
        stmt.setString(pos++, section);
        rs = stmt.executeQuery();
        if (rs.next()) {
            return rs.getString(1);
        }
        return null;
    } catch (SQLException e) {
        throw ServiceException.FAILURE("getting metadata section '" + section + "' in mailbox " + mbox.getId(), 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)

Example 55 with DbConnection

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

the class DbMailbox method updateVersion.

public static void updateVersion(Mailbox mbox, MailboxVersion version) throws ServiceException {
    DbConnection conn = mbox.getOperationConnection();
    PreparedStatement stmt = null;
    try {
        stmt = conn.prepareStatement("UPDATE " + qualifyZimbraTableName(mbox, TABLE_MAILBOX) + " SET version = ?" + (DebugConfig.disableMailboxGroups ? "" : " WHERE id = ?"));
        int pos = 1;
        stmt.setString(pos++, version == null ? null : version.toString());
        pos = DbMailItem.setMailboxId(stmt, mbox, pos++);
        stmt.executeUpdate();
    } catch (SQLException e) {
        throw ServiceException.FAILURE("setting mailbox version to '" + version + "' in mailbox " + mbox.getId(), e);
    } finally {
        DbPool.closeStatement(stmt);
    }
}
Also used : SQLException(java.sql.SQLException) 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