Search in sources :

Example 1 with LockableDatabase

use of com.fsck.k9.mailstore.LockableDatabase in project k-9 by k9mail.

the class EmailProvider method getAccountStats.

private Cursor getAccountStats(String accountUuid, String[] columns, final String selection, final String[] selectionArgs) {
    Account account = getAccount(accountUuid);
    LockableDatabase database = getDatabase(account);
    // Use default projection if none was given
    String[] sourceProjection = (columns == null) ? STATS_DEFAULT_PROJECTION : columns;
    // Create SQL query string
    final StringBuilder sql = new StringBuilder();
    sql.append("SELECT ");
    // Append projection for the database query
    // e.g. "SUM(read=0) AS unread_count, SUM(flagged) AS flagged_count"
    boolean first = true;
    for (String columnName : sourceProjection) {
        if (!first) {
            sql.append(',');
        } else {
            first = false;
        }
        if (StatsColumns.UNREAD_COUNT.equals(columnName)) {
            sql.append("SUM(" + MessageColumns.READ + "=0) AS " + StatsColumns.UNREAD_COUNT);
        } else if (StatsColumns.FLAGGED_COUNT.equals(columnName)) {
            sql.append("SUM(" + MessageColumns.FLAGGED + ") AS " + StatsColumns.FLAGGED_COUNT);
        } else {
            throw new IllegalArgumentException("Column name not allowed: " + columnName);
        }
    }
    // Table selection
    sql.append(" FROM messages");
    if (containsAny(selection, FOLDERS_COLUMNS)) {
        sql.append(" JOIN folders ON (folders.id = messages.folder_id)");
    }
    // WHERE clause
    sql.append(" WHERE (deleted = 0 AND empty = 0)");
    if (!TextUtils.isEmpty(selection)) {
        sql.append(" AND (");
        sql.append(selection);
        sql.append(")");
    }
    // Query the database and return the result cursor
    try {
        return database.execute(false, new DbCallback<Cursor>() {

            @Override
            public Cursor doDbWork(SQLiteDatabase db) throws WrappedException, UnavailableStorageException {
                return db.rawQuery(sql.toString(), selectionArgs);
            }
        });
    } catch (UnavailableStorageException e) {
        throw new RuntimeException("Storage not available", e);
    } catch (MessagingException e) {
        throw new RuntimeException("messaging exception", e);
    }
}
Also used : Account(com.fsck.k9.Account) WrappedException(com.fsck.k9.mailstore.LockableDatabase.WrappedException) MessagingException(com.fsck.k9.mail.MessagingException) UnavailableStorageException(com.fsck.k9.mailstore.UnavailableStorageException) EmailProviderCacheCursor(com.fsck.k9.cache.EmailProviderCacheCursor) Cursor(android.database.Cursor) LockableDatabase(com.fsck.k9.mailstore.LockableDatabase) SQLiteDatabase(android.database.sqlite.SQLiteDatabase)

Example 2 with LockableDatabase

use of com.fsck.k9.mailstore.LockableDatabase in project k-9 by k9mail.

the class EmailProvider method getMessages.

protected Cursor getMessages(String accountUuid, final String[] projection, final String selection, final String[] selectionArgs, final String sortOrder) {
    Account account = getAccount(accountUuid);
    LockableDatabase database = getDatabase(account);
    try {
        return database.execute(false, new DbCallback<Cursor>() {

            @Override
            public Cursor doDbWork(SQLiteDatabase db) throws WrappedException, UnavailableStorageException {
                String where;
                if (TextUtils.isEmpty(selection)) {
                    where = InternalMessageColumns.DELETED + " = 0 AND " + InternalMessageColumns.EMPTY + " = 0";
                } else {
                    where = "(" + selection + ") AND " + InternalMessageColumns.DELETED + " = 0 AND " + InternalMessageColumns.EMPTY + " = 0";
                }
                final Cursor cursor;
                if (Utility.arrayContainsAny(projection, (Object[]) FOLDERS_COLUMNS)) {
                    StringBuilder query = new StringBuilder();
                    query.append("SELECT ");
                    boolean first = true;
                    for (String columnName : projection) {
                        if (!first) {
                            query.append(",");
                        } else {
                            first = false;
                        }
                        if (MessageColumns.ID.equals(columnName)) {
                            query.append("m.");
                            query.append(MessageColumns.ID);
                            query.append(" AS ");
                            query.append(MessageColumns.ID);
                        } else {
                            query.append(columnName);
                        }
                    }
                    query.append(" FROM messages m " + "JOIN threads t ON (t.message_id = m.id) " + "LEFT JOIN folders f ON (m.folder_id = f.id) " + "WHERE ");
                    query.append(SqlQueryBuilder.addPrefixToSelection(FIXUP_MESSAGES_COLUMNS, "m.", where));
                    query.append(" ORDER BY ");
                    query.append(SqlQueryBuilder.addPrefixToSelection(FIXUP_MESSAGES_COLUMNS, "m.", sortOrder));
                    cursor = db.rawQuery(query.toString(), selectionArgs);
                } else {
                    cursor = db.query(MESSAGES_TABLE, projection, where, selectionArgs, null, null, sortOrder);
                }
                return cursor;
            }
        });
    } catch (UnavailableStorageException e) {
        throw new RuntimeException("Storage not available", e);
    } catch (MessagingException e) {
        throw new RuntimeException("messaging exception", e);
    }
}
Also used : Account(com.fsck.k9.Account) WrappedException(com.fsck.k9.mailstore.LockableDatabase.WrappedException) LockableDatabase(com.fsck.k9.mailstore.LockableDatabase) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) MessagingException(com.fsck.k9.mail.MessagingException) UnavailableStorageException(com.fsck.k9.mailstore.UnavailableStorageException) EmailProviderCacheCursor(com.fsck.k9.cache.EmailProviderCacheCursor) Cursor(android.database.Cursor)

Example 3 with LockableDatabase

use of com.fsck.k9.mailstore.LockableDatabase in project k-9 by k9mail.

the class EmailProvider method getThread.

protected Cursor getThread(String accountUuid, final String[] projection, final String threadId, final String sortOrder) {
    Account account = getAccount(accountUuid);
    LockableDatabase database = getDatabase(account);
    try {
        return database.execute(false, new DbCallback<Cursor>() {

            @Override
            public Cursor doDbWork(SQLiteDatabase db) throws WrappedException, UnavailableStorageException {
                StringBuilder query = new StringBuilder();
                query.append("SELECT ");
                boolean first = true;
                for (String columnName : projection) {
                    if (!first) {
                        query.append(",");
                    } else {
                        first = false;
                    }
                    if (MessageColumns.ID.equals(columnName)) {
                        query.append("m." + MessageColumns.ID + " AS " + MessageColumns.ID);
                    } else {
                        query.append(columnName);
                    }
                }
                query.append(" FROM " + THREADS_TABLE + " t JOIN " + MESSAGES_TABLE + " m " + "ON (m." + MessageColumns.ID + " = t." + ThreadColumns.MESSAGE_ID + ") ");
                if (Utility.arrayContainsAny(projection, (Object[]) FOLDERS_COLUMNS)) {
                    query.append("LEFT JOIN " + FOLDERS_TABLE + " f " + "ON (m." + MessageColumns.FOLDER_ID + " = f." + FolderColumns.ID + ") ");
                }
                query.append("WHERE " + ThreadColumns.ROOT + " = ? AND " + InternalMessageColumns.DELETED + " = 0 AND " + InternalMessageColumns.EMPTY + " = 0");
                query.append(" ORDER BY ");
                query.append(SqlQueryBuilder.addPrefixToSelection(FIXUP_MESSAGES_COLUMNS, "m.", sortOrder));
                return db.rawQuery(query.toString(), new String[] { threadId });
            }
        });
    } catch (UnavailableStorageException e) {
        throw new RuntimeException("Storage not available", e);
    } catch (MessagingException e) {
        throw new RuntimeException("messaging exception", e);
    }
}
Also used : Account(com.fsck.k9.Account) WrappedException(com.fsck.k9.mailstore.LockableDatabase.WrappedException) LockableDatabase(com.fsck.k9.mailstore.LockableDatabase) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) MessagingException(com.fsck.k9.mail.MessagingException) UnavailableStorageException(com.fsck.k9.mailstore.UnavailableStorageException) EmailProviderCacheCursor(com.fsck.k9.cache.EmailProviderCacheCursor) Cursor(android.database.Cursor)

Example 4 with LockableDatabase

use of com.fsck.k9.mailstore.LockableDatabase in project k-9 by k9mail.

the class LockableDatabase method delete.

/**
     * @param recreate
     *            <code>true</code> if the DB should be recreated after delete
     * @throws UnavailableStorageException
     */
private void delete(final boolean recreate) throws UnavailableStorageException {
    lockWrite();
    try {
        try {
            mDb.close();
        } catch (Exception e) {
            Timber.d("Exception caught in DB close: %s", e.getMessage());
        }
        final StorageManager storageManager = getStorageManager();
        try {
            final File attachmentDirectory = storageManager.getAttachmentDirectory(uUid, mStorageProviderId);
            final File[] attachments = attachmentDirectory.listFiles();
            for (File attachment : attachments) {
                if (attachment.exists()) {
                    boolean attachmentWasDeleted = attachment.delete();
                    if (!attachmentWasDeleted) {
                        Timber.d("Attachment was not deleted!");
                    }
                }
            }
            if (attachmentDirectory.exists()) {
                boolean attachmentDirectoryWasDeleted = attachmentDirectory.delete();
                if (!attachmentDirectoryWasDeleted) {
                    Timber.d("Attachment directory was not deleted!");
                }
            }
        } catch (Exception e) {
            Timber.d("Exception caught in clearing attachments: %s", e.getMessage());
        }
        try {
            deleteDatabase(storageManager.getDatabase(uUid, mStorageProviderId));
        } catch (Exception e) {
            Timber.i(e, "LockableDatabase: delete(): Unable to delete backing DB file");
        }
        if (recreate) {
            openOrCreateDataspace();
        } else {
            // stop waiting for mount/unmount events
            getStorageManager().removeListener(mStorageListener);
        }
    } finally {
        unlockWrite();
    }
}
Also used : File(java.io.File) SQLiteException(android.database.sqlite.SQLiteException) MessagingException(com.fsck.k9.mail.MessagingException)

Example 5 with LockableDatabase

use of com.fsck.k9.mailstore.LockableDatabase in project k-9 by k9mail.

the class LockableDatabase method switchProvider.

/**
     * @param newProviderId
     *            Never <code>null</code>.
     * @throws MessagingException
     */
public void switchProvider(final String newProviderId) throws MessagingException {
    if (newProviderId.equals(mStorageProviderId)) {
        Timber.v("LockableDatabase: Ignoring provider switch request as they are equal: %s", newProviderId);
        return;
    }
    final String oldProviderId = mStorageProviderId;
    lockWrite(oldProviderId);
    try {
        lockWrite(newProviderId);
        try {
            try {
                mDb.close();
            } catch (Exception e) {
                Timber.i(e, "Unable to close DB on local store migration");
            }
            final StorageManager storageManager = getStorageManager();
            File oldDatabase = storageManager.getDatabase(uUid, oldProviderId);
            // create new path
            prepareStorage(newProviderId);
            // move all database files
            FileHelper.moveRecursive(oldDatabase, storageManager.getDatabase(uUid, newProviderId));
            // move all attachment files
            FileHelper.moveRecursive(storageManager.getAttachmentDirectory(uUid, oldProviderId), storageManager.getAttachmentDirectory(uUid, newProviderId));
            // remove any remaining old journal files
            deleteDatabase(oldDatabase);
            mStorageProviderId = newProviderId;
            // re-initialize this class with the new Uri
            openOrCreateDataspace();
        } finally {
            unlockWrite(newProviderId);
        }
    } finally {
        unlockWrite(oldProviderId);
    }
}
Also used : File(java.io.File) SQLiteException(android.database.sqlite.SQLiteException) MessagingException(com.fsck.k9.mail.MessagingException)

Aggregations

MessagingException (com.fsck.k9.mail.MessagingException)7 Account (com.fsck.k9.Account)5 Cursor (android.database.Cursor)4 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)4 EmailProviderCacheCursor (com.fsck.k9.cache.EmailProviderCacheCursor)4 LockableDatabase (com.fsck.k9.mailstore.LockableDatabase)4 WrappedException (com.fsck.k9.mailstore.LockableDatabase.WrappedException)4 UnavailableStorageException (com.fsck.k9.mailstore.UnavailableStorageException)4 SQLiteException (android.database.sqlite.SQLiteException)3 File (java.io.File)2 Context (android.content.Context)1