stmt = conn.prepareStatement("SELECT " + DB_FIELDS + " FROM " + table + " WHERE " + (DebugConfig.enableMailboxGroup ? "mailbox_id = ? AND " : "") + "type IN " + FOLDER_AND_TAG_TYPES);
stmt = conn.prepareStatement("SELECT " + DB_FIELDS + " FROM " + table + " WHERE " + IN_THIS_MAILBOX_AND + "type IN " + FOLDER_AND_TAG_TYPES);
    public static Mailbox.MailboxData getFoldersAndTags(Mailbox mbox, Map<Integer, UnderlyingData> folderData, Map<Integer, UnderlyingData> tagData, boolean reload)    throws ServiceException {        boolean fetchFolders = folderData != null;        boolean fetchTags    = tagData != null;        if (!fetchFolders && !fetchTags && !reload)            return null;        Connection conn = mbox.getOperationConnection();        PreparedStatement stmt = null;        ResultSet rs = null;        try {            String table = getMailItemTableName(mbox.getId(), "mi");            stmt = conn.prepareStatement("SELECT " + DB_FIELDS +                    " FROM " + table + " WHERE " +                    (DebugConfig.enableMailboxGroup ? "mailbox_id = ? AND " : "") +                    "type IN " + FOLDER_AND_TAG_TYPES);            if (DebugConfig.enableMailboxGroup)                stmt.setInt(1, mbox.getId());            rs = stmt.executeQuery();            while (rs.next()) {                UnderlyingData data = constructItem(rs);                if (fetchFolders && MailItem.isAcceptableType(MailItem.TYPE_FOLDER, data.type))                    folderData.put(data.id, data);                else if (fetchTags && MailItem.isAcceptableType(MailItem.TYPE_TAG, data.type))                    tagData.put(data.id, data);                rs.getInt(CI_UNREAD);                reload |= rs.wasNull();            }            if (!reload)                return null;            rs.close();            stmt.close();            // going to recalculate counts, so discard any existing counts...            if (fetchFolders)                for (UnderlyingData data : folderData.values())                    data.size = data.unreadCount = 0;            if (fetchTags)                for (UnderlyingData data : tagData.values())                    data.size = data.unreadCount = 0;            Mailbox.MailboxData mbd = new Mailbox.MailboxData();            stmt = conn.prepareStatement("SELECT folder_id, type, tags, COUNT(*), SUM(unread), SUM(size)" +                    " FROM " + table + " WHERE " +                    (DebugConfig.enableMailboxGroup ? "mailbox_id = ? AND " : "") +                    "type NOT IN " + NON_SEARCHABLE_TYPES +                    " GROUP BY folder_id, type, tags");            if (DebugConfig.enableMailboxGroup)                stmt.setInt(1, mbox.getId());            rs = stmt.executeQuery();            while (rs.next()) {                byte type  = rs.getByte(2);                int count  = rs.getInt(4);                int unread = rs.getInt(5);                long size  = rs.getLong(6);                if (type == MailItem.TYPE_CONTACT)                    mbd.contacts += count;                mbd.size += size;                if (fetchFolders) {	                UnderlyingData data = folderData.get(rs.getInt(1));                    assert(data != null);                    data.unreadCount += unread;                    data.size += count;                }                if (fetchTags) {                    long tags = rs.getLong(3);                    for (int i = 0; tags != 0 && i < MailItem.MAX_TAG_COUNT - 1; i++) {                        if ((tags & (1L << i)) != 0) {        	                UnderlyingData data = tagData.get(i + MailItem.TAG_ID_OFFSET);        	                if (data != null)        	                    data.unreadCount += unread;                            // could track cumulative count if desired...                            tags &= ~(1L << i);                        }                    }                }            }            return mbd;        } catch (SQLException e) {        	throw ServiceException.FAILURE("fetching folder data for mailbox " + mbox.getId(), e);        } finally {        	DbPool.closeResults(rs);            DbPool.closeStatement(stmt);        }    }