From 45f9379fee3207190cdc28568f96ec5bfabb0e4e Mon Sep 17 00:00:00 2001 From: Peter Serwylo Date: Wed, 28 Sep 2016 22:57:40 +1000 Subject: [PATCH] Added helper function for debugging SQL queries during development It is often helpful during debugging to be able to dump the contents of an SQL result `Cursor` to the debug watch list. This is difficult to do under normal circumstances. This adds a utility method really only designed to be used during interactive debugging, which will do its best to build a `Map` for each row in the `Cursor`. This can then be used to test queries while the debugger is paused. --- .../main/java/org/fdroid/fdroid/Utils.java | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/app/src/main/java/org/fdroid/fdroid/Utils.java b/app/src/main/java/org/fdroid/fdroid/Utils.java index c39b0ab1a..eca09c6bd 100644 --- a/app/src/main/java/org/fdroid/fdroid/Utils.java +++ b/app/src/main/java/org/fdroid/fdroid/Utils.java @@ -20,10 +20,12 @@ package org.fdroid.fdroid; import android.content.Context; import android.content.pm.PackageManager; +import android.database.Cursor; import android.graphics.Bitmap; import android.net.Uri; import android.support.annotation.NonNull; import android.support.annotation.Nullable; +import android.support.annotation.RequiresApi; import android.text.Editable; import android.text.Html; import android.text.TextUtils; @@ -49,6 +51,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.math.BigInteger; +import java.nio.charset.Charset; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.security.cert.Certificate; @@ -56,9 +59,13 @@ import java.security.cert.CertificateEncodingException; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; +import java.util.ArrayList; import java.util.Date; import java.util.Formatter; +import java.util.HashMap; +import java.util.List; import java.util.Locale; +import java.util.Map; public final class Utils { @@ -541,4 +548,52 @@ public final class Utils { return versionName; } + /** + * Useful for debugging during development, so that arbitrary queries can be made, and their + * results inspected in the debugger. + */ + @SuppressWarnings("unused") + @RequiresApi(api = 11) + public static List> dumpCursor(Cursor cursor) { + List> data = new ArrayList<>(); + + if (cursor == null) { + return data; + } + + cursor.moveToFirst(); + while (!cursor.isAfterLast()) { + Map row = new HashMap<>(cursor.getColumnCount()); + for (String col : cursor.getColumnNames()) { + int i = cursor.getColumnIndex(col); + switch (cursor.getType(i)) { + case Cursor.FIELD_TYPE_NULL: + row.put(col, null); + break; + + case Cursor.FIELD_TYPE_INTEGER: + row.put(col, Integer.toString(cursor.getInt(i))); + break; + + case Cursor.FIELD_TYPE_FLOAT: + row.put(col, Double.toString(cursor.getFloat(i))); + break; + + case Cursor.FIELD_TYPE_STRING: + row.put(col, cursor.getString(i)); + break; + + case Cursor.FIELD_TYPE_BLOB: + row.put(col, new String(cursor.getBlob(i), Charset.defaultCharset())); + break; + } + } + data.add(row); + cursor.moveToNext(); + } + + cursor.close(); + return data; + } + }