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);
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
Aggregations