1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/background/db/CursorDumper.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,99 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +package org.mozilla.gecko.background.db; 1.9 + 1.10 +import android.database.Cursor; 1.11 + 1.12 +/** 1.13 + * A utility for dumping a cursor the debug log. 1.14 + * <p> 1.15 + * <b>For debugging only!</p> 1.16 + */ 1.17 +public class CursorDumper { 1.18 + protected static String fixedWidth(int width, String s) { 1.19 + if (s == null) { 1.20 + return spaces(width); 1.21 + } 1.22 + int length = s.length(); 1.23 + if (width == length) { 1.24 + return s; 1.25 + } 1.26 + if (width > length) { 1.27 + return s + spaces(width - length); 1.28 + } 1.29 + return s.substring(0, width); 1.30 + } 1.31 + 1.32 + protected static String spaces(int i) { 1.33 + return " ".substring(0, i); 1.34 + } 1.35 + 1.36 + protected static String dashes(int i) { 1.37 + return "-------------------------------------".substring(0, i); 1.38 + } 1.39 + 1.40 + /** 1.41 + * Dump a cursor to the debug log, ignoring any log level settings. 1.42 + * <p> 1.43 + * The position in the cursor is maintained. Caller is responsible for opening 1.44 + * and closing cursor. 1.45 + * 1.46 + * @param cursor 1.47 + * to dump. 1.48 + */ 1.49 + public static void dumpCursor(Cursor cursor) { 1.50 + dumpCursor(cursor, 18, "records"); 1.51 + } 1.52 + 1.53 + /** 1.54 + * Dump a cursor to the debug log, ignoring any log level settings. 1.55 + * <p> 1.56 + * The position in the cursor is maintained. Caller is responsible for opening 1.57 + * and closing cursor. 1.58 + * 1.59 + * @param cursor 1.60 + * to dump. 1.61 + * @param columnWidth 1.62 + * how many characters per cursor column. 1.63 + * @param tags 1.64 + * a descriptor, printed like "(10 tags)", in the header row. 1.65 + */ 1.66 + protected static void dumpCursor(Cursor cursor, int columnWidth, String tags) { 1.67 + int originalPosition = cursor.getPosition(); 1.68 + try { 1.69 + String[] columnNames = cursor.getColumnNames(); 1.70 + int columnCount = cursor.getColumnCount(); 1.71 + 1.72 + for (int i = 0; i < columnCount; ++i) { 1.73 + System.out.print(fixedWidth(columnWidth, columnNames[i]) + " | "); 1.74 + } 1.75 + System.out.println("(" + cursor.getCount() + " " + tags + ")"); 1.76 + for (int i = 0; i < columnCount; ++i) { 1.77 + System.out.print(dashes(columnWidth) + " | "); 1.78 + } 1.79 + System.out.println(""); 1.80 + if (!cursor.moveToFirst()) { 1.81 + System.out.println("EMPTY"); 1.82 + return; 1.83 + } 1.84 + 1.85 + cursor.moveToFirst(); 1.86 + while (!cursor.isAfterLast()) { 1.87 + for (int i = 0; i < columnCount; ++i) { 1.88 + System.out.print(fixedWidth(columnWidth, cursor.getString(i)) + " | "); 1.89 + } 1.90 + System.out.println(""); 1.91 + cursor.moveToNext(); 1.92 + } 1.93 + for (int i = 0; i < columnCount-1; ++i) { 1.94 + System.out.print(dashes(columnWidth + 3)); 1.95 + } 1.96 + System.out.print(dashes(columnWidth + 3 - 1)); 1.97 + System.out.println(""); 1.98 + } finally { 1.99 + cursor.moveToPosition(originalPosition); 1.100 + } 1.101 + } 1.102 +}