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