1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-12-03 06:22:17 -05:00

Quick hack to log all SQL queries

This commit is contained in:
cketti 2013-07-11 04:02:50 +02:00
parent c0479fb59a
commit 6cf8789d04
4 changed files with 204 additions and 79 deletions

View File

@ -32,7 +32,6 @@ import android.content.ContentValues;
import android.content.Context; import android.content.Context;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.database.Cursor; import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException; import android.database.sqlite.SQLiteException;
import android.net.Uri; import android.net.Uri;
import android.util.Log; import android.util.Log;
@ -207,7 +206,7 @@ public class LocalStore extends Store implements Serializable {
} }
@Override @Override
public void doDbUpgrade(final SQLiteDatabase db) { public void doDbUpgrade(final LoggingSQLiteDatabase db) {
try { try {
upgradeDatabase(db); upgradeDatabase(db);
} catch (Exception e) { } catch (Exception e) {
@ -217,7 +216,7 @@ public class LocalStore extends Store implements Serializable {
} }
} }
private void upgradeDatabase(final SQLiteDatabase db) { private void upgradeDatabase(final LoggingSQLiteDatabase db) {
Log.i(K9.LOG_TAG, String.format("Upgrading database from version %d to version %d", Log.i(K9.LOG_TAG, String.format("Upgrading database from version %d to version %d",
db.getVersion(), DB_VERSION)); db.getVersion(), DB_VERSION));
@ -703,7 +702,7 @@ public class LocalStore extends Store implements Serializable {
} }
} }
private void update41Metadata(final SQLiteDatabase db, SharedPreferences prefs, int id, String name) { private void update41Metadata(final LoggingSQLiteDatabase db, SharedPreferences prefs, int id, String name) {
Folder.FolderClass displayClass = Folder.FolderClass.NO_CLASS; Folder.FolderClass displayClass = Folder.FolderClass.NO_CLASS;
@ -755,7 +754,7 @@ public class LocalStore extends Store implements Serializable {
return database.execute(false, new DbCallback<Long>() { return database.execute(false, new DbCallback<Long>() {
@Override @Override
public Long doDbWork(final SQLiteDatabase db) { public Long doDbWork(final LoggingSQLiteDatabase db) {
final File[] files = attachmentDirectory.listFiles(); final File[] files = attachmentDirectory.listFiles();
long attachmentLength = 0; long attachmentLength = 0;
for (File file : files) { for (File file : files) {
@ -776,7 +775,7 @@ public class LocalStore extends Store implements Serializable {
database.execute(false, new DbCallback<Void>() { database.execute(false, new DbCallback<Void>() {
@Override @Override
public Void doDbWork(final SQLiteDatabase db) throws WrappedException { public Void doDbWork(final LoggingSQLiteDatabase db) throws WrappedException {
db.execSQL("VACUUM"); db.execSQL("VACUUM");
return null; return null;
} }
@ -804,7 +803,7 @@ public class LocalStore extends Store implements Serializable {
// been deleted locally. They take up insignificant space // been deleted locally. They take up insignificant space
database.execute(false, new DbCallback<Void>() { database.execute(false, new DbCallback<Void>() {
@Override @Override
public Void doDbWork(final SQLiteDatabase db) { public Void doDbWork(final LoggingSQLiteDatabase db) {
// Delete entries from 'threads' table // Delete entries from 'threads' table
db.execSQL("DELETE FROM threads WHERE message_id IN " + db.execSQL("DELETE FROM threads WHERE message_id IN " +
"(SELECT id FROM messages WHERE deleted = 0 AND uid NOT LIKE 'Local%')"); "(SELECT id FROM messages WHERE deleted = 0 AND uid NOT LIKE 'Local%')");
@ -833,7 +832,7 @@ public class LocalStore extends Store implements Serializable {
public int getMessageCount() throws MessagingException { public int getMessageCount() throws MessagingException {
return database.execute(false, new DbCallback<Integer>() { return database.execute(false, new DbCallback<Integer>() {
@Override @Override
public Integer doDbWork(final SQLiteDatabase db) { public Integer doDbWork(final LoggingSQLiteDatabase db) {
Cursor cursor = null; Cursor cursor = null;
try { try {
cursor = db.rawQuery("SELECT COUNT(*) FROM messages", null); cursor = db.rawQuery("SELECT COUNT(*) FROM messages", null);
@ -849,7 +848,7 @@ public class LocalStore extends Store implements Serializable {
public int getFolderCount() throws MessagingException { public int getFolderCount() throws MessagingException {
return database.execute(false, new DbCallback<Integer>() { return database.execute(false, new DbCallback<Integer>() {
@Override @Override
public Integer doDbWork(final SQLiteDatabase db) { public Integer doDbWork(final LoggingSQLiteDatabase db) {
Cursor cursor = null; Cursor cursor = null;
try { try {
cursor = db.rawQuery("SELECT COUNT(*) FROM folders", null); cursor = db.rawQuery("SELECT COUNT(*) FROM folders", null);
@ -878,7 +877,7 @@ public class LocalStore extends Store implements Serializable {
try { try {
database.execute(false, new DbCallback < List <? extends Folder >> () { database.execute(false, new DbCallback < List <? extends Folder >> () {
@Override @Override
public List <? extends Folder > doDbWork(final SQLiteDatabase db) throws WrappedException { public List <? extends Folder > doDbWork(final LoggingSQLiteDatabase db) throws WrappedException {
Cursor cursor = null; Cursor cursor = null;
try { try {
@ -932,7 +931,7 @@ public class LocalStore extends Store implements Serializable {
private void pruneCachedAttachments(final boolean force) throws MessagingException { private void pruneCachedAttachments(final boolean force) throws MessagingException {
database.execute(false, new DbCallback<Void>() { database.execute(false, new DbCallback<Void>() {
@Override @Override
public Void doDbWork(final SQLiteDatabase db) throws WrappedException { public Void doDbWork(final LoggingSQLiteDatabase db) throws WrappedException {
if (force) { if (force) {
ContentValues cv = new ContentValues(); ContentValues cv = new ContentValues();
cv.putNull("content_uri"); cv.putNull("content_uri");
@ -999,7 +998,7 @@ public class LocalStore extends Store implements Serializable {
cv.put("visible_limit", Integer.toString(visibleLimit)); cv.put("visible_limit", Integer.toString(visibleLimit));
database.execute(false, new DbCallback<Void>() { database.execute(false, new DbCallback<Void>() {
@Override @Override
public Void doDbWork(final SQLiteDatabase db) throws WrappedException { public Void doDbWork(final LoggingSQLiteDatabase db) throws WrappedException {
db.update("folders", cv, null, null); db.update("folders", cv, null, null);
return null; return null;
} }
@ -1009,7 +1008,7 @@ public class LocalStore extends Store implements Serializable {
public ArrayList<PendingCommand> getPendingCommands() throws UnavailableStorageException { public ArrayList<PendingCommand> getPendingCommands() throws UnavailableStorageException {
return database.execute(false, new DbCallback<ArrayList<PendingCommand>>() { return database.execute(false, new DbCallback<ArrayList<PendingCommand>>() {
@Override @Override
public ArrayList<PendingCommand> doDbWork(final SQLiteDatabase db) throws WrappedException { public ArrayList<PendingCommand> doDbWork(final LoggingSQLiteDatabase db) throws WrappedException {
Cursor cursor = null; Cursor cursor = null;
try { try {
cursor = db.query("pending_commands", cursor = db.query("pending_commands",
@ -1049,7 +1048,7 @@ public class LocalStore extends Store implements Serializable {
cv.put("arguments", Utility.combine(command.arguments, ',')); cv.put("arguments", Utility.combine(command.arguments, ','));
database.execute(false, new DbCallback<Void>() { database.execute(false, new DbCallback<Void>() {
@Override @Override
public Void doDbWork(final SQLiteDatabase db) throws WrappedException { public Void doDbWork(final LoggingSQLiteDatabase db) throws WrappedException {
db.insert("pending_commands", "command", cv); db.insert("pending_commands", "command", cv);
return null; return null;
} }
@ -1062,7 +1061,7 @@ public class LocalStore extends Store implements Serializable {
public void removePendingCommand(final PendingCommand command) throws UnavailableStorageException { public void removePendingCommand(final PendingCommand command) throws UnavailableStorageException {
database.execute(false, new DbCallback<Void>() { database.execute(false, new DbCallback<Void>() {
@Override @Override
public Void doDbWork(final SQLiteDatabase db) throws WrappedException { public Void doDbWork(final LoggingSQLiteDatabase db) throws WrappedException {
db.delete("pending_commands", "id = ?", new String[] { Long.toString(command.mId) }); db.delete("pending_commands", "id = ?", new String[] { Long.toString(command.mId) });
return null; return null;
} }
@ -1072,7 +1071,7 @@ public class LocalStore extends Store implements Serializable {
public void removePendingCommands() throws UnavailableStorageException { public void removePendingCommands() throws UnavailableStorageException {
database.execute(false, new DbCallback<Void>() { database.execute(false, new DbCallback<Void>() {
@Override @Override
public Void doDbWork(final SQLiteDatabase db) throws WrappedException { public Void doDbWork(final LoggingSQLiteDatabase db) throws WrappedException {
db.delete("pending_commands", null, null); db.delete("pending_commands", null, null);
return null; return null;
} }
@ -1147,7 +1146,7 @@ public class LocalStore extends Store implements Serializable {
final ArrayList<LocalMessage> messages = new ArrayList<LocalMessage>(); final ArrayList<LocalMessage> messages = new ArrayList<LocalMessage>();
final int j = database.execute(false, new DbCallback<Integer>() { final int j = database.execute(false, new DbCallback<Integer>() {
@Override @Override
public Integer doDbWork(final SQLiteDatabase db) throws WrappedException { public Integer doDbWork(final LoggingSQLiteDatabase db) throws WrappedException {
Cursor cursor = null; Cursor cursor = null;
int i = 0; int i = 0;
try { try {
@ -1204,7 +1203,7 @@ public class LocalStore extends Store implements Serializable {
public AttachmentInfo getAttachmentInfo(final String attachmentId) throws UnavailableStorageException { public AttachmentInfo getAttachmentInfo(final String attachmentId) throws UnavailableStorageException {
return database.execute(false, new DbCallback<AttachmentInfo>() { return database.execute(false, new DbCallback<AttachmentInfo>() {
@Override @Override
public AttachmentInfo doDbWork(final SQLiteDatabase db) throws WrappedException { public AttachmentInfo doDbWork(final LoggingSQLiteDatabase db) throws WrappedException {
String name; String name;
String type; String type;
int size; int size;
@ -1245,7 +1244,7 @@ public class LocalStore extends Store implements Serializable {
public void createFolders(final List<LocalFolder> foldersToCreate, final int visibleLimit) throws UnavailableStorageException { public void createFolders(final List<LocalFolder> foldersToCreate, final int visibleLimit) throws UnavailableStorageException {
database.execute(true, new DbCallback<Void>() { database.execute(true, new DbCallback<Void>() {
@Override @Override
public Void doDbWork(final SQLiteDatabase db) throws WrappedException { public Void doDbWork(final LoggingSQLiteDatabase db) throws WrappedException {
for (LocalFolder folder : foldersToCreate) { for (LocalFolder folder : foldersToCreate) {
String name = folder.getName(); String name = folder.getName();
final LocalFolder.PreferencesHolder prefHolder = folder.new PreferencesHolder(); final LocalFolder.PreferencesHolder prefHolder = folder.new PreferencesHolder();
@ -1366,7 +1365,7 @@ public class LocalStore extends Store implements Serializable {
try { try {
database.execute(false, new DbCallback<Void>() { database.execute(false, new DbCallback<Void>() {
@Override @Override
public Void doDbWork(final SQLiteDatabase db) throws WrappedException { public Void doDbWork(final LoggingSQLiteDatabase db) throws WrappedException {
Cursor cursor = null; Cursor cursor = null;
try { try {
String baseQuery = "SELECT " + GET_FOLDER_COLS + " FROM folders "; String baseQuery = "SELECT " + GET_FOLDER_COLS + " FROM folders ";
@ -1440,7 +1439,7 @@ public class LocalStore extends Store implements Serializable {
public boolean exists() throws MessagingException { public boolean exists() throws MessagingException {
return database.execute(false, new DbCallback<Boolean>() { return database.execute(false, new DbCallback<Boolean>() {
@Override @Override
public Boolean doDbWork(final SQLiteDatabase db) throws WrappedException { public Boolean doDbWork(final LoggingSQLiteDatabase db) throws WrappedException {
Cursor cursor = null; Cursor cursor = null;
try { try {
cursor = db.rawQuery("SELECT id FROM folders " cursor = db.rawQuery("SELECT id FROM folders "
@ -1495,7 +1494,7 @@ public class LocalStore extends Store implements Serializable {
try { try {
return database.execute(false, new DbCallback<Integer>() { return database.execute(false, new DbCallback<Integer>() {
@Override @Override
public Integer doDbWork(final SQLiteDatabase db) throws WrappedException { public Integer doDbWork(final LoggingSQLiteDatabase db) throws WrappedException {
try { try {
open(OpenMode.READ_WRITE); open(OpenMode.READ_WRITE);
} catch (MessagingException e) { } catch (MessagingException e) {
@ -1528,7 +1527,7 @@ public class LocalStore extends Store implements Serializable {
try { try {
return database.execute(false, new DbCallback<Integer>() { return database.execute(false, new DbCallback<Integer>() {
@Override @Override
public Integer doDbWork(final SQLiteDatabase db) throws WrappedException { public Integer doDbWork(final LoggingSQLiteDatabase db) throws WrappedException {
int unreadMessageCount = 0; int unreadMessageCount = 0;
Cursor cursor = db.query("messages", new String[] { "SUM(read=0)" }, Cursor cursor = db.query("messages", new String[] { "SUM(read=0)" },
"folder_id = ? AND (empty IS NULL OR empty != 1) AND deleted = 0", "folder_id = ? AND (empty IS NULL OR empty != 1) AND deleted = 0",
@ -1559,7 +1558,7 @@ public class LocalStore extends Store implements Serializable {
try { try {
return database.execute(false, new DbCallback<Integer>() { return database.execute(false, new DbCallback<Integer>() {
@Override @Override
public Integer doDbWork(final SQLiteDatabase db) throws WrappedException { public Integer doDbWork(final LoggingSQLiteDatabase db) throws WrappedException {
int flaggedMessageCount = 0; int flaggedMessageCount = 0;
Cursor cursor = db.query("messages", new String[] { "SUM(flagged)" }, Cursor cursor = db.query("messages", new String[] { "SUM(flagged)" },
"folder_id = ? AND (empty IS NULL OR empty != 1) AND deleted = 0", "folder_id = ? AND (empty IS NULL OR empty != 1) AND deleted = 0",
@ -1644,7 +1643,7 @@ public class LocalStore extends Store implements Serializable {
try { try {
database.execute(false, new DbCallback<Void>() { database.execute(false, new DbCallback<Void>() {
@Override @Override
public Void doDbWork(final SQLiteDatabase db) throws WrappedException { public Void doDbWork(final LoggingSQLiteDatabase db) throws WrappedException {
try { try {
open(OpenMode.READ_WRITE); open(OpenMode.READ_WRITE);
} catch (MessagingException e) { } catch (MessagingException e) {
@ -1815,7 +1814,7 @@ public class LocalStore extends Store implements Serializable {
try { try {
database.execute(false, new DbCallback<Void>() { database.execute(false, new DbCallback<Void>() {
@Override @Override
public Void doDbWork(final SQLiteDatabase db) throws WrappedException { public Void doDbWork(final LoggingSQLiteDatabase db) throws WrappedException {
try { try {
open(OpenMode.READ_WRITE); open(OpenMode.READ_WRITE);
if (fp.contains(FetchProfile.Item.BODY)) { if (fp.contains(FetchProfile.Item.BODY)) {
@ -2020,7 +2019,7 @@ public class LocalStore extends Store implements Serializable {
private void populateHeaders(final List<LocalMessage> messages) throws UnavailableStorageException { private void populateHeaders(final List<LocalMessage> messages) throws UnavailableStorageException {
database.execute(false, new DbCallback<Void>() { database.execute(false, new DbCallback<Void>() {
@Override @Override
public Void doDbWork(final SQLiteDatabase db) throws WrappedException, UnavailableStorageException { public Void doDbWork(final LoggingSQLiteDatabase db) throws WrappedException, UnavailableStorageException {
Cursor cursor = null; Cursor cursor = null;
if (messages.isEmpty()) { if (messages.isEmpty()) {
return null; return null;
@ -2066,7 +2065,7 @@ public class LocalStore extends Store implements Serializable {
try { try {
return database.execute(false, new DbCallback<String>() { return database.execute(false, new DbCallback<String>() {
@Override @Override
public String doDbWork(final SQLiteDatabase db) throws WrappedException, UnavailableStorageException { public String doDbWork(final LoggingSQLiteDatabase db) throws WrappedException, UnavailableStorageException {
try { try {
open(OpenMode.READ_WRITE); open(OpenMode.READ_WRITE);
Cursor cursor = null; Cursor cursor = null;
@ -2100,7 +2099,7 @@ public class LocalStore extends Store implements Serializable {
try { try {
return database.execute(false, new DbCallback<LocalMessage>() { return database.execute(false, new DbCallback<LocalMessage>() {
@Override @Override
public LocalMessage doDbWork(final SQLiteDatabase db) throws WrappedException, UnavailableStorageException { public LocalMessage doDbWork(final LoggingSQLiteDatabase db) throws WrappedException, UnavailableStorageException {
try { try {
open(OpenMode.READ_WRITE); open(OpenMode.READ_WRITE);
LocalMessage message = new LocalMessage(uid, LocalFolder.this); LocalMessage message = new LocalMessage(uid, LocalFolder.this);
@ -2144,7 +2143,7 @@ public class LocalStore extends Store implements Serializable {
try { try {
return database.execute(false, new DbCallback<Message[]>() { return database.execute(false, new DbCallback<Message[]>() {
@Override @Override
public Message[] doDbWork(final SQLiteDatabase db) throws WrappedException, UnavailableStorageException { public Message[] doDbWork(final LoggingSQLiteDatabase db) throws WrappedException, UnavailableStorageException {
try { try {
open(OpenMode.READ_WRITE); open(OpenMode.READ_WRITE);
return LocalStore.this.getMessages( return LocalStore.this.getMessages(
@ -2206,7 +2205,7 @@ public class LocalStore extends Store implements Serializable {
try { try {
database.execute(false, new DbCallback<Void>() { database.execute(false, new DbCallback<Void>() {
@Override @Override
public Void doDbWork(final SQLiteDatabase db) throws WrappedException, UnavailableStorageException { public Void doDbWork(final LoggingSQLiteDatabase db) throws WrappedException, UnavailableStorageException {
try { try {
lDestFolder.open(OpenMode.READ_WRITE); lDestFolder.open(OpenMode.READ_WRITE);
for (Message message : msgs) { for (Message message : msgs) {
@ -2330,7 +2329,7 @@ public class LocalStore extends Store implements Serializable {
public Message storeSmallMessage(final Message message, final Runnable runnable) throws MessagingException { public Message storeSmallMessage(final Message message, final Runnable runnable) throws MessagingException {
return database.execute(true, new DbCallback<Message>() { return database.execute(true, new DbCallback<Message>() {
@Override @Override
public Message doDbWork(final SQLiteDatabase db) throws WrappedException, UnavailableStorageException { public Message doDbWork(final LoggingSQLiteDatabase db) throws WrappedException, UnavailableStorageException {
try { try {
appendMessages(new Message[] { message }); appendMessages(new Message[] { message });
final String uid = message.getUid(); final String uid = message.getUid();
@ -2366,7 +2365,7 @@ public class LocalStore extends Store implements Serializable {
try { try {
database.execute(true, new DbCallback<Void>() { database.execute(true, new DbCallback<Void>() {
@Override @Override
public Void doDbWork(final SQLiteDatabase db) throws WrappedException, UnavailableStorageException { public Void doDbWork(final LoggingSQLiteDatabase db) throws WrappedException, UnavailableStorageException {
for (Message message : messages) { for (Message message : messages) {
try { try {
message.destroy(); message.destroy();
@ -2382,7 +2381,7 @@ public class LocalStore extends Store implements Serializable {
} }
} }
private ThreadInfo getThreadInfo(SQLiteDatabase db, String messageId, boolean onlyEmpty) { private ThreadInfo getThreadInfo(LoggingSQLiteDatabase db, String messageId, boolean onlyEmpty) {
String sql = "SELECT t.id, t.message_id, t.root, t.parent " + String sql = "SELECT t.id, t.message_id, t.root, t.parent " +
"FROM messages m " + "FROM messages m " +
"LEFT JOIN threads t ON (t.message_id = m.id) " + "LEFT JOIN threads t ON (t.message_id = m.id) " +
@ -2431,7 +2430,7 @@ public class LocalStore extends Store implements Serializable {
final Map<String, String> uidMap = new HashMap<String, String>(); final Map<String, String> uidMap = new HashMap<String, String>();
database.execute(true, new DbCallback<Void>() { database.execute(true, new DbCallback<Void>() {
@Override @Override
public Void doDbWork(final SQLiteDatabase db) throws WrappedException, UnavailableStorageException { public Void doDbWork(final LoggingSQLiteDatabase db) throws WrappedException, UnavailableStorageException {
try { try {
for (Message message : messages) { for (Message message : messages) {
if (!(message instanceof MimeMessage)) { if (!(message instanceof MimeMessage)) {
@ -2599,7 +2598,7 @@ public class LocalStore extends Store implements Serializable {
try { try {
database.execute(false, new DbCallback<Void>() { database.execute(false, new DbCallback<Void>() {
@Override @Override
public Void doDbWork(final SQLiteDatabase db) throws WrappedException, UnavailableStorageException { public Void doDbWork(final LoggingSQLiteDatabase db) throws WrappedException, UnavailableStorageException {
try { try {
message.buildMimeRepresentation(); message.buildMimeRepresentation();
@ -2677,7 +2676,7 @@ public class LocalStore extends Store implements Serializable {
private void saveHeaders(final long id, final MimeMessage message) throws MessagingException { private void saveHeaders(final long id, final MimeMessage message) throws MessagingException {
database.execute(true, new DbCallback<Void>() { database.execute(true, new DbCallback<Void>() {
@Override @Override
public Void doDbWork(final SQLiteDatabase db) throws WrappedException, UnavailableStorageException { public Void doDbWork(final LoggingSQLiteDatabase db) throws WrappedException, UnavailableStorageException {
deleteHeaders(id); deleteHeaders(id);
for (String name : message.getHeaderNames()) { for (String name : message.getHeaderNames()) {
@ -2709,7 +2708,7 @@ public class LocalStore extends Store implements Serializable {
private void deleteHeaders(final long id) throws UnavailableStorageException { private void deleteHeaders(final long id) throws UnavailableStorageException {
database.execute(false, new DbCallback<Void>() { database.execute(false, new DbCallback<Void>() {
@Override @Override
public Void doDbWork(final SQLiteDatabase db) throws WrappedException, UnavailableStorageException { public Void doDbWork(final LoggingSQLiteDatabase db) throws WrappedException, UnavailableStorageException {
db.execSQL("DELETE FROM headers WHERE message_id = ?", new Object[] db.execSQL("DELETE FROM headers WHERE message_id = ?", new Object[]
{ id }); { id });
return null; return null;
@ -2729,7 +2728,7 @@ public class LocalStore extends Store implements Serializable {
try { try {
database.execute(true, new DbCallback<Void>() { database.execute(true, new DbCallback<Void>() {
@Override @Override
public Void doDbWork(final SQLiteDatabase db) throws WrappedException, UnavailableStorageException { public Void doDbWork(final LoggingSQLiteDatabase db) throws WrappedException, UnavailableStorageException {
try { try {
long attachmentId = -1; long attachmentId = -1;
Uri contentUri = null; Uri contentUri = null;
@ -2908,7 +2907,7 @@ public class LocalStore extends Store implements Serializable {
cv.put("uid", message.getUid()); cv.put("uid", message.getUid());
database.execute(false, new DbCallback<Void>() { database.execute(false, new DbCallback<Void>() {
@Override @Override
public Void doDbWork(final SQLiteDatabase db) throws WrappedException, UnavailableStorageException { public Void doDbWork(final LoggingSQLiteDatabase db) throws WrappedException, UnavailableStorageException {
db.update("messages", cv, "id = ?", new String[] db.update("messages", cv, "id = ?", new String[]
{ Long.toString(message.mId) }); { Long.toString(message.mId) });
return null; return null;
@ -2928,7 +2927,7 @@ public class LocalStore extends Store implements Serializable {
try { try {
database.execute(true, new DbCallback<Void>() { database.execute(true, new DbCallback<Void>() {
@Override @Override
public Void doDbWork(final SQLiteDatabase db) throws WrappedException, public Void doDbWork(final LoggingSQLiteDatabase db) throws WrappedException,
UnavailableStorageException { UnavailableStorageException {
for (Message message : messages) { for (Message message : messages) {
@ -2991,7 +2990,7 @@ public class LocalStore extends Store implements Serializable {
try { try {
database.execute(false, new DbCallback<Void>() { database.execute(false, new DbCallback<Void>() {
@Override @Override
public Void doDbWork(final SQLiteDatabase db) throws WrappedException { public Void doDbWork(final LoggingSQLiteDatabase db) throws WrappedException {
try { try {
// Get UIDs for all messages to delete // Get UIDs for all messages to delete
Cursor cursor = db.query("messages", new String[] { "uid" }, Cursor cursor = db.query("messages", new String[] { "uid" },
@ -3035,7 +3034,7 @@ public class LocalStore extends Store implements Serializable {
try { try {
database.execute(false, new DbCallback<Void>() { database.execute(false, new DbCallback<Void>() {
@Override @Override
public Void doDbWork(final SQLiteDatabase db) throws WrappedException, UnavailableStorageException { public Void doDbWork(final LoggingSQLiteDatabase db) throws WrappedException, UnavailableStorageException {
try { try {
// We need to open the folder first to make sure we've got it's id // We need to open the folder first to make sure we've got it's id
open(OpenMode.READ_ONLY); open(OpenMode.READ_ONLY);
@ -3073,7 +3072,7 @@ public class LocalStore extends Store implements Serializable {
open(OpenMode.READ_WRITE); open(OpenMode.READ_WRITE);
database.execute(false, new DbCallback<Void>() { database.execute(false, new DbCallback<Void>() {
@Override @Override
public Void doDbWork(final SQLiteDatabase db) throws WrappedException, UnavailableStorageException { public Void doDbWork(final LoggingSQLiteDatabase db) throws WrappedException, UnavailableStorageException {
Cursor attachmentsCursor = null; Cursor attachmentsCursor = null;
try { try {
String accountUuid = mAccount.getUuid(); String accountUuid = mAccount.getUuid();
@ -3117,7 +3116,7 @@ public class LocalStore extends Store implements Serializable {
try { try {
database.execute(false, new DbCallback<Void>() { database.execute(false, new DbCallback<Void>() {
@Override @Override
public Void doDbWork(final SQLiteDatabase db) throws WrappedException, UnavailableStorageException { public Void doDbWork(final LoggingSQLiteDatabase db) throws WrappedException, UnavailableStorageException {
Cursor messagesCursor = null; Cursor messagesCursor = null;
try { try {
messagesCursor = db.query("messages", new String[] messagesCursor = db.query("messages", new String[]
@ -3174,7 +3173,7 @@ public class LocalStore extends Store implements Serializable {
public void updateLastUid() throws MessagingException { public void updateLastUid() throws MessagingException {
Integer lastUid = database.execute(false, new DbCallback<Integer>() { Integer lastUid = database.execute(false, new DbCallback<Integer>() {
@Override @Override
public Integer doDbWork(final SQLiteDatabase db) { public Integer doDbWork(final LoggingSQLiteDatabase db) {
Cursor cursor = null; Cursor cursor = null;
try { try {
open(OpenMode.READ_ONLY); open(OpenMode.READ_ONLY);
@ -3199,7 +3198,7 @@ public class LocalStore extends Store implements Serializable {
public Long getOldestMessageDate() throws MessagingException { public Long getOldestMessageDate() throws MessagingException {
return database.execute(false, new DbCallback<Long>() { return database.execute(false, new DbCallback<Long>() {
@Override @Override
public Long doDbWork(final SQLiteDatabase db) { public Long doDbWork(final LoggingSQLiteDatabase db) {
Cursor cursor = null; Cursor cursor = null;
try { try {
open(OpenMode.READ_ONLY); open(OpenMode.READ_ONLY);
@ -3218,7 +3217,7 @@ public class LocalStore extends Store implements Serializable {
}); });
} }
private ThreadInfo doMessageThreading(SQLiteDatabase db, Message message) private ThreadInfo doMessageThreading(LoggingSQLiteDatabase db, Message message)
throws MessagingException { throws MessagingException {
long rootId = -1; long rootId = -1;
long parentId = -1; long parentId = -1;
@ -3327,7 +3326,7 @@ public class LocalStore extends Store implements Serializable {
try { try {
return database.execute(false, new DbCallback<List<Message>>() { return database.execute(false, new DbCallback<List<Message>>() {
@Override @Override
public List<Message> doDbWork(final SQLiteDatabase db) throws WrappedException { public List<Message> doDbWork(final LoggingSQLiteDatabase db) throws WrappedException {
try { try {
open(OpenMode.READ_WRITE); open(OpenMode.READ_WRITE);
} catch (MessagingException e) { } catch (MessagingException e) {
@ -3651,7 +3650,7 @@ public class LocalStore extends Store implements Serializable {
try { try {
database.execute(true, new DbCallback<Void>() { database.execute(true, new DbCallback<Void>() {
@Override @Override
public Void doDbWork(final SQLiteDatabase db) throws WrappedException, UnavailableStorageException { public Void doDbWork(final LoggingSQLiteDatabase db) throws WrappedException, UnavailableStorageException {
try { try {
if (flag == Flag.DELETED && set) { if (flag == Flag.DELETED && set) {
delete(); delete();
@ -3697,7 +3696,7 @@ public class LocalStore extends Store implements Serializable {
try { try {
database.execute(true, new DbCallback<Void>() { database.execute(true, new DbCallback<Void>() {
@Override @Override
public Void doDbWork(final SQLiteDatabase db) throws WrappedException, public Void doDbWork(final LoggingSQLiteDatabase db) throws WrappedException,
UnavailableStorageException { UnavailableStorageException {
String[] idArg = new String[] { Long.toString(mId) }; String[] idArg = new String[] { Long.toString(mId) };
@ -3750,7 +3749,7 @@ public class LocalStore extends Store implements Serializable {
try { try {
database.execute(true, new DbCallback<Void>() { database.execute(true, new DbCallback<Void>() {
@Override @Override
public Void doDbWork(final SQLiteDatabase db) throws WrappedException, public Void doDbWork(final LoggingSQLiteDatabase db) throws WrappedException,
UnavailableStorageException { UnavailableStorageException {
try { try {
LocalFolder localFolder = (LocalFolder) mFolder; LocalFolder localFolder = (LocalFolder) mFolder;
@ -3815,14 +3814,14 @@ public class LocalStore extends Store implements Serializable {
* Get ID of the the given message's parent if the parent is an empty message. * Get ID of the the given message's parent if the parent is an empty message.
* *
* @param db * @param db
* {@link SQLiteDatabase} instance to access the database. * {@link LoggingSQLiteDatabase} instance to access the database.
* @param messageId * @param messageId
* The database ID of the message to get the parent for. * The database ID of the message to get the parent for.
* *
* @return Message ID of the parent message if there exists a parent and it is empty. * @return Message ID of the parent message if there exists a parent and it is empty.
* Otherwise {@code -1}. * Otherwise {@code -1}.
*/ */
private long getEmptyThreadParent(SQLiteDatabase db, long messageId) { private long getEmptyThreadParent(LoggingSQLiteDatabase db, long messageId) {
Cursor cursor = db.rawQuery( Cursor cursor = db.rawQuery(
"SELECT m.id " + "SELECT m.id " +
"FROM threads t1 " + "FROM threads t1 " +
@ -3842,13 +3841,13 @@ public class LocalStore extends Store implements Serializable {
* Check whether or not a message has child messages in the thread structure. * Check whether or not a message has child messages in the thread structure.
* *
* @param db * @param db
* {@link SQLiteDatabase} instance to access the database. * {@link LoggingSQLiteDatabase} instance to access the database.
* @param messageId * @param messageId
* The database ID of the message to get the children for. * The database ID of the message to get the children for.
* *
* @return {@code true} if the message has children. {@code false} otherwise. * @return {@code true} if the message has children. {@code false} otherwise.
*/ */
private boolean hasThreadChildren(SQLiteDatabase db, long messageId) { private boolean hasThreadChildren(LoggingSQLiteDatabase db, long messageId) {
Cursor cursor = db.rawQuery( Cursor cursor = db.rawQuery(
"SELECT COUNT(t2.id) " + "SELECT COUNT(t2.id) " +
"FROM threads t1 " + "FROM threads t1 " +
@ -3867,11 +3866,11 @@ public class LocalStore extends Store implements Serializable {
* Delete a message from the 'messages' and 'threads' tables. * Delete a message from the 'messages' and 'threads' tables.
* *
* @param db * @param db
* {@link SQLiteDatabase} instance to access the database. * {@link LoggingSQLiteDatabase} instance to access the database.
* @param messageId * @param messageId
* The database ID of the message to delete. * The database ID of the message to delete.
*/ */
private void deleteMessageRow(SQLiteDatabase db, long messageId) { private void deleteMessageRow(LoggingSQLiteDatabase db, long messageId) {
String[] idArg = { Long.toString(messageId) }; String[] idArg = { Long.toString(messageId) };
// Delete the message // Delete the message
@ -4083,7 +4082,7 @@ public class LocalStore extends Store implements Serializable {
try { try {
database.execute(true, new DbCallback<Void>() { database.execute(true, new DbCallback<Void>() {
@Override @Override
public Void doDbWork(final SQLiteDatabase db) throws WrappedException, public Void doDbWork(final LoggingSQLiteDatabase db) throws WrappedException,
UnavailableStorageException { UnavailableStorageException {
selectionCallback.doDbWork(db, selection.toString(), selectionCallback.doDbWork(db, selection.toString(),
@ -4127,7 +4126,7 @@ public class LocalStore extends Store implements Serializable {
* Execute the SQL statement. * Execute the SQL statement.
* *
* @param db * @param db
* Use this {@link SQLiteDatabase} instance for your SQL statement. * Use this {@link LoggingSQLiteDatabase} instance for your SQL statement.
* @param selectionSet * @param selectionSet
* A partial selection string containing place holders for the argument list, e.g. * A partial selection string containing place holders for the argument list, e.g.
* {@code " IN (?,?,?)"} (starts with a space). * {@code " IN (?,?,?)"} (starts with a space).
@ -4135,12 +4134,12 @@ public class LocalStore extends Store implements Serializable {
* The current subset of the argument list. * The current subset of the argument list.
* @throws UnavailableStorageException * @throws UnavailableStorageException
*/ */
void doDbWork(SQLiteDatabase db, String selectionSet, String[] selectionArgs) void doDbWork(LoggingSQLiteDatabase db, String selectionSet, String[] selectionArgs)
throws UnavailableStorageException; throws UnavailableStorageException;
/** /**
* This will be executed after each invocation of * This will be executed after each invocation of
* {@link #doDbWork(SQLiteDatabase, String, String[])} (after the transaction has been * {@link #doDbWork(LoggingSQLiteDatabase, String, String[])} (after the transaction has been
* committed). * committed).
*/ */
void postDbWork(); void postDbWork();
@ -4181,7 +4180,7 @@ public class LocalStore extends Store implements Serializable {
} }
@Override @Override
public void doDbWork(SQLiteDatabase db, String selectionSet, String[] selectionArgs) public void doDbWork(LoggingSQLiteDatabase db, String selectionSet, String[] selectionArgs)
throws UnavailableStorageException { throws UnavailableStorageException {
db.update("messages", cv, "(empty IS NULL OR empty != 1) AND id" + selectionSet, db.update("messages", cv, "(empty IS NULL OR empty != 1) AND id" + selectionSet,
@ -4229,7 +4228,7 @@ public class LocalStore extends Store implements Serializable {
} }
@Override @Override
public void doDbWork(SQLiteDatabase db, String selectionSet, String[] selectionArgs) public void doDbWork(LoggingSQLiteDatabase db, String selectionSet, String[] selectionArgs)
throws UnavailableStorageException { throws UnavailableStorageException {
db.execSQL("UPDATE messages SET " + flagColumn + " = " + ((newState) ? "1" : "0") + db.execSQL("UPDATE messages SET " + flagColumn + " = " + ((newState) ? "1" : "0") +
@ -4281,7 +4280,7 @@ public class LocalStore extends Store implements Serializable {
} }
@Override @Override
public void doDbWork(SQLiteDatabase db, String selectionSet, String[] selectionArgs) public void doDbWork(LoggingSQLiteDatabase db, String selectionSet, String[] selectionArgs)
throws UnavailableStorageException { throws UnavailableStorageException {
if (threadedList) { if (threadedList) {

View File

@ -22,7 +22,7 @@ public class LockableDatabase {
* HibernateCallback. * HibernateCallback.
* *
* @param <T> * @param <T>
* Return value type for {@link #doDbWork(SQLiteDatabase)} * Return value type for {@link #doDbWork(LoggingSQLiteDatabase)}
*/ */
public static interface DbCallback<T> { public static interface DbCallback<T> {
/** /**
@ -33,7 +33,7 @@ public class LockableDatabase {
* @throws WrappedException * @throws WrappedException
* @throws UnavailableStorageException * @throws UnavailableStorageException
*/ */
T doDbWork(SQLiteDatabase db) throws WrappedException, UnavailableStorageException; T doDbWork(LoggingSQLiteDatabase db) throws WrappedException, UnavailableStorageException;
} }
public static interface SchemaDefinition { public static interface SchemaDefinition {
@ -42,7 +42,7 @@ public class LockableDatabase {
/** /**
* @param db Never <code>null</code>. * @param db Never <code>null</code>.
*/ */
void doDbUpgrade(SQLiteDatabase db); void doDbUpgrade(LoggingSQLiteDatabase db);
} }
/** /**
@ -106,7 +106,7 @@ public class LockableDatabase {
private String mStorageProviderId; private String mStorageProviderId;
private SQLiteDatabase mDb; private LoggingSQLiteDatabase mDb;
/** /**
* Reentrant read lock * Reentrant read lock
*/ */
@ -265,7 +265,7 @@ public class LockableDatabase {
* Never <code>null</code>. * Never <code>null</code>.
* *
* @param <T> * @param <T>
* @return Whatever {@link DbCallback#doDbWork(SQLiteDatabase)} returns. * @return Whatever {@link DbCallback#doDbWork(LoggingSQLiteDatabase)} returns.
* @throws UnavailableStorageException * @throws UnavailableStorageException
*/ */
public <T> T execute(final boolean transactional, final DbCallback<T> callback) throws UnavailableStorageException { public <T> T execute(final boolean transactional, final DbCallback<T> callback) throws UnavailableStorageException {
@ -370,13 +370,14 @@ public class LockableDatabase {
lockWrite(); lockWrite();
try { try {
final File databaseFile = prepareStorage(mStorageProviderId); final File databaseFile = prepareStorage(mStorageProviderId);
SQLiteDatabase db;
try { try {
if (StorageManager.InternalStorageProvider.ID.equals(mStorageProviderId)) { if (StorageManager.InternalStorageProvider.ID.equals(mStorageProviderId)) {
// internal storage // internal storage
mDb = application.openOrCreateDatabase(databaseFile.getName(), Context.MODE_PRIVATE, null); db = application.openOrCreateDatabase(databaseFile.getName(), Context.MODE_PRIVATE, null);
} else { } else {
// external storage // external storage
mDb = SQLiteDatabase.openOrCreateDatabase(databaseFile, null); db = SQLiteDatabase.openOrCreateDatabase(databaseFile, null);
} }
} catch (SQLiteException e) { } catch (SQLiteException e) {
// try to gracefully handle DB corruption - see issue 2537 // try to gracefully handle DB corruption - see issue 2537
@ -384,12 +385,14 @@ public class LockableDatabase {
databaseFile.delete(); databaseFile.delete();
if (StorageManager.InternalStorageProvider.ID.equals(mStorageProviderId)) { if (StorageManager.InternalStorageProvider.ID.equals(mStorageProviderId)) {
// internal storage // internal storage
mDb = application.openOrCreateDatabase(databaseFile.getName(), Context.MODE_PRIVATE, null); db = application.openOrCreateDatabase(databaseFile.getName(), Context.MODE_PRIVATE, null);
} else { } else {
// external storage // external storage
mDb = SQLiteDatabase.openOrCreateDatabase(databaseFile, null); db = SQLiteDatabase.openOrCreateDatabase(databaseFile, null);
} }
} }
mDb = new LoggingSQLiteDatabase(db);
if (mDb.getVersion() != mSchemaDefinition.getVersion()) { if (mDb.getVersion() != mSchemaDefinition.getVersion()) {
mSchemaDefinition.doDbUpgrade(mDb); mSchemaDefinition.doDbUpgrade(mDb);
} }

View File

@ -0,0 +1,123 @@
package com.fsck.k9.mail.store;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.SystemClock;
import android.util.Log;
public class LoggingSQLiteDatabase {
private static final String LOG_TAG = "k9-sql";
private SQLiteDatabase mDatabase;
public LoggingSQLiteDatabase(SQLiteDatabase database) {
mDatabase = database;
}
public int getVersion() {
return mDatabase.getVersion();
}
public void close() {
mDatabase.close();
}
public void beginTransaction() {
mDatabase.beginTransaction();
}
public void setTransactionSuccessful() {
mDatabase.setTransactionSuccessful();
}
public void endTransaction() {
mDatabase.endTransaction();
}
public void setVersion(int version) {
mDatabase.setVersion(version);
}
public void execSQL(String sql) {
long start = SystemClock.elapsedRealtime();
mDatabase.execSQL(sql);
long end = SystemClock.elapsedRealtime();
Log.d(LOG_TAG, "execSQL [" + (end - start) + "ms]: " + sql);
}
public void execSQL(String sql, Object[] args) {
long start = SystemClock.elapsedRealtime();
mDatabase.execSQL(sql, args);
long end = SystemClock.elapsedRealtime();
Log.d(LOG_TAG, "execSQL [" + (end - start) + "ms]: " + sql);
}
public Cursor rawQuery(String sql, String[] selectionArgs) {
long start = SystemClock.elapsedRealtime();
Cursor cursor = mDatabase.rawQuery(sql, selectionArgs);
long end = SystemClock.elapsedRealtime();
// DB might be queried lazily; make sure query is performed now
cursor.getCount();
long end2 = SystemClock.elapsedRealtime();
Log.d(LOG_TAG, "rawQuery [" + (end - start) + "ms + " + (end2 - end) + "ms]: " + sql);
return cursor;
}
public int update(String table, ContentValues values, String whereClause, String[] whereArgs) {
long start = SystemClock.elapsedRealtime();
int rows = mDatabase.update(table, values, whereClause, whereArgs);
long end = SystemClock.elapsedRealtime();
Log.d(LOG_TAG, "update [" + (end - start) + "ms]: " + table);
return rows;
}
public Cursor query(String table, String[] columns, String selection, String[] selectionArgs,
String groupBy, String having, String orderBy) {
long start = SystemClock.elapsedRealtime();
Cursor cursor = mDatabase.query(table, columns, selection, selectionArgs, groupBy, having,
orderBy);
long end = SystemClock.elapsedRealtime();
// DB might be queried lazily; make sure query is performed now
cursor.getCount();
long end2 = SystemClock.elapsedRealtime();
Log.d(LOG_TAG, "query [" + (end - start) + "ms + " + (end2 - end) + "ms]: " + table + "; where= " + selection);
return cursor;
}
public long insert(String table, String nullColumnHack, ContentValues values) {
long start = SystemClock.elapsedRealtime();
long id = mDatabase.insert(table, nullColumnHack, values);
long end = SystemClock.elapsedRealtime();
Log.d(LOG_TAG, "insert [" + (end - start) + "ms]: " + table);
return id;
}
public int delete(String table, String whereClause, String[] whereArgs) {
long start = SystemClock.elapsedRealtime();
int rows = mDatabase.delete(table, whereClause, whereArgs);
long end = SystemClock.elapsedRealtime();
Log.d(LOG_TAG, "delete [" + (end - start) + "ms]: " + table);
return rows;
}
public long replace(String table, String nullColumnHack, ContentValues initialValues) {
long start = SystemClock.elapsedRealtime();
long id = mDatabase.replace(table, nullColumnHack, initialValues);
long end = SystemClock.elapsedRealtime();
Log.d(LOG_TAG, "replace [" + (end - start) + "ms]: " + table);
return id;
}
}

View File

@ -15,6 +15,7 @@ import com.fsck.k9.mail.store.LocalStore;
import com.fsck.k9.mail.store.LockableDatabase; import com.fsck.k9.mail.store.LockableDatabase;
import com.fsck.k9.mail.store.LockableDatabase.DbCallback; import com.fsck.k9.mail.store.LockableDatabase.DbCallback;
import com.fsck.k9.mail.store.LockableDatabase.WrappedException; import com.fsck.k9.mail.store.LockableDatabase.WrappedException;
import com.fsck.k9.mail.store.LoggingSQLiteDatabase;
import com.fsck.k9.mail.store.UnavailableStorageException; import com.fsck.k9.mail.store.UnavailableStorageException;
import com.fsck.k9.search.SqlQueryBuilder; import com.fsck.k9.search.SqlQueryBuilder;
@ -26,7 +27,6 @@ import android.content.Context;
import android.content.UriMatcher; import android.content.UriMatcher;
import android.database.Cursor; import android.database.Cursor;
import android.database.CursorWrapper; import android.database.CursorWrapper;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri; import android.net.Uri;
/** /**
@ -300,7 +300,7 @@ public class EmailProvider extends ContentProvider {
try { try {
return database.execute(false, new DbCallback<Cursor>() { return database.execute(false, new DbCallback<Cursor>() {
@Override @Override
public Cursor doDbWork(SQLiteDatabase db) throws WrappedException, public Cursor doDbWork(LoggingSQLiteDatabase db) throws WrappedException,
UnavailableStorageException { UnavailableStorageException {
String where; String where;
@ -370,7 +370,7 @@ public class EmailProvider extends ContentProvider {
try { try {
return database.execute(false, new DbCallback<Cursor>() { return database.execute(false, new DbCallback<Cursor>() {
@Override @Override
public Cursor doDbWork(SQLiteDatabase db) throws WrappedException, public Cursor doDbWork(LoggingSQLiteDatabase db) throws WrappedException,
UnavailableStorageException { UnavailableStorageException {
StringBuilder query = new StringBuilder(); StringBuilder query = new StringBuilder();
@ -488,7 +488,7 @@ public class EmailProvider extends ContentProvider {
try { try {
return database.execute(false, new DbCallback<Cursor>() { return database.execute(false, new DbCallback<Cursor>() {
@Override @Override
public Cursor doDbWork(SQLiteDatabase db) throws WrappedException, public Cursor doDbWork(LoggingSQLiteDatabase db) throws WrappedException,
UnavailableStorageException { UnavailableStorageException {
StringBuilder query = new StringBuilder(); StringBuilder query = new StringBuilder();
@ -587,7 +587,7 @@ public class EmailProvider extends ContentProvider {
try { try {
return database.execute(false, new DbCallback<Cursor>() { return database.execute(false, new DbCallback<Cursor>() {
@Override @Override
public Cursor doDbWork(SQLiteDatabase db) throws WrappedException, public Cursor doDbWork(LoggingSQLiteDatabase db) throws WrappedException,
UnavailableStorageException { UnavailableStorageException {
return db.rawQuery(sql.toString(), selectionArgs); return db.rawQuery(sql.toString(), selectionArgs);