Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | package org.mozilla.gecko.background.db; |
michael@0 | 6 | |
michael@0 | 7 | import android.database.Cursor; |
michael@0 | 8 | |
michael@0 | 9 | /** |
michael@0 | 10 | * A utility for dumping a cursor the debug log. |
michael@0 | 11 | * <p> |
michael@0 | 12 | * <b>For debugging only!</p> |
michael@0 | 13 | */ |
michael@0 | 14 | public class CursorDumper { |
michael@0 | 15 | protected static String fixedWidth(int width, String s) { |
michael@0 | 16 | if (s == null) { |
michael@0 | 17 | return spaces(width); |
michael@0 | 18 | } |
michael@0 | 19 | int length = s.length(); |
michael@0 | 20 | if (width == length) { |
michael@0 | 21 | return s; |
michael@0 | 22 | } |
michael@0 | 23 | if (width > length) { |
michael@0 | 24 | return s + spaces(width - length); |
michael@0 | 25 | } |
michael@0 | 26 | return s.substring(0, width); |
michael@0 | 27 | } |
michael@0 | 28 | |
michael@0 | 29 | protected static String spaces(int i) { |
michael@0 | 30 | return " ".substring(0, i); |
michael@0 | 31 | } |
michael@0 | 32 | |
michael@0 | 33 | protected static String dashes(int i) { |
michael@0 | 34 | return "-------------------------------------".substring(0, i); |
michael@0 | 35 | } |
michael@0 | 36 | |
michael@0 | 37 | /** |
michael@0 | 38 | * Dump a cursor to the debug log, ignoring any log level settings. |
michael@0 | 39 | * <p> |
michael@0 | 40 | * The position in the cursor is maintained. Caller is responsible for opening |
michael@0 | 41 | * and closing cursor. |
michael@0 | 42 | * |
michael@0 | 43 | * @param cursor |
michael@0 | 44 | * to dump. |
michael@0 | 45 | */ |
michael@0 | 46 | public static void dumpCursor(Cursor cursor) { |
michael@0 | 47 | dumpCursor(cursor, 18, "records"); |
michael@0 | 48 | } |
michael@0 | 49 | |
michael@0 | 50 | /** |
michael@0 | 51 | * Dump a cursor to the debug log, ignoring any log level settings. |
michael@0 | 52 | * <p> |
michael@0 | 53 | * The position in the cursor is maintained. Caller is responsible for opening |
michael@0 | 54 | * and closing cursor. |
michael@0 | 55 | * |
michael@0 | 56 | * @param cursor |
michael@0 | 57 | * to dump. |
michael@0 | 58 | * @param columnWidth |
michael@0 | 59 | * how many characters per cursor column. |
michael@0 | 60 | * @param tags |
michael@0 | 61 | * a descriptor, printed like "(10 tags)", in the header row. |
michael@0 | 62 | */ |
michael@0 | 63 | protected static void dumpCursor(Cursor cursor, int columnWidth, String tags) { |
michael@0 | 64 | int originalPosition = cursor.getPosition(); |
michael@0 | 65 | try { |
michael@0 | 66 | String[] columnNames = cursor.getColumnNames(); |
michael@0 | 67 | int columnCount = cursor.getColumnCount(); |
michael@0 | 68 | |
michael@0 | 69 | for (int i = 0; i < columnCount; ++i) { |
michael@0 | 70 | System.out.print(fixedWidth(columnWidth, columnNames[i]) + " | "); |
michael@0 | 71 | } |
michael@0 | 72 | System.out.println("(" + cursor.getCount() + " " + tags + ")"); |
michael@0 | 73 | for (int i = 0; i < columnCount; ++i) { |
michael@0 | 74 | System.out.print(dashes(columnWidth) + " | "); |
michael@0 | 75 | } |
michael@0 | 76 | System.out.println(""); |
michael@0 | 77 | if (!cursor.moveToFirst()) { |
michael@0 | 78 | System.out.println("EMPTY"); |
michael@0 | 79 | return; |
michael@0 | 80 | } |
michael@0 | 81 | |
michael@0 | 82 | cursor.moveToFirst(); |
michael@0 | 83 | while (!cursor.isAfterLast()) { |
michael@0 | 84 | for (int i = 0; i < columnCount; ++i) { |
michael@0 | 85 | System.out.print(fixedWidth(columnWidth, cursor.getString(i)) + " | "); |
michael@0 | 86 | } |
michael@0 | 87 | System.out.println(""); |
michael@0 | 88 | cursor.moveToNext(); |
michael@0 | 89 | } |
michael@0 | 90 | for (int i = 0; i < columnCount-1; ++i) { |
michael@0 | 91 | System.out.print(dashes(columnWidth + 3)); |
michael@0 | 92 | } |
michael@0 | 93 | System.out.print(dashes(columnWidth + 3 - 1)); |
michael@0 | 94 | System.out.println(""); |
michael@0 | 95 | } finally { |
michael@0 | 96 | cursor.moveToPosition(originalPosition); |
michael@0 | 97 | } |
michael@0 | 98 | } |
michael@0 | 99 | } |