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