1package SQLite;
2
3import java.util.Vector;
4
5/**
6 * Class representing an SQLite result set as
7 * returned by the
8 * <A HREF="Database.html#get_table(java.lang.String)">Database.get_table</A>
9 * convenience method.
10 * <BR><BR>
11 * Example:<BR>
12 *
13 * <PRE>
14 *   ...
15 *   SQLite.Database db = new SQLite.Database();
16 *   db.open("db", 0);
17 *   System.out.print(db.get_table("select * from TEST"));
18 *   ...
19 * </PRE>
20 * Example output:<BR>
21 *
22 * <PRE>
23 *   id|firstname|lastname|
24 *   0|John|Doe|
25 *   1|Speedy|Gonzales|
26 *   ...
27 * </PRE>
28 */
29
30public class TableResult implements Callback {
31
32    /**
33     * Number of columns in the result set.
34     */
35
36    public int ncolumns;
37
38    /**
39     * Number of rows in the result set.
40     */
41
42    public int nrows;
43
44    /**
45     * Column names of the result set.
46     */
47
48    public String column[];
49
50    /**
51     * Types of columns of the result set or null.
52     */
53
54    public String types[];
55
56    /**
57     * Rows of the result set. Each row is stored as a String array.
58     */
59
60    public Vector rows;
61
62    /**
63     * Create an empty result set.
64     */
65
66    public TableResult() {
67    clear();
68    }
69
70    /**
71     * Clear result set.
72     */
73
74    public void clear() {
75    column = new String[0];
76    types = null;
77    rows = new Vector();
78    ncolumns = nrows = 0;
79    }
80
81    /**
82     * Callback method used while the query is executed.
83     */
84
85    public void columns(String coldata[]) {
86    column = coldata;
87    ncolumns = column.length;
88    }
89
90    /**
91     * Callback method used while the query is executed.
92     */
93
94    public void types(String types[]) {
95    this.types = types;
96    }
97
98    /**
99     * Callback method used while the query is executed.
100     */
101
102    public boolean newrow(String rowdata[]) {
103    if (rowdata != null) {
104        rows.addElement(rowdata);
105        nrows++;
106    }
107    return false;
108    }
109
110    /**
111     * Make String representation of result set.
112     */
113
114    public String toString() {
115    StringBuffer sb = new StringBuffer();
116    int i;
117    for (i = 0; i < ncolumns; i++) {
118        sb.append(column[i] == null ? "NULL" : column[i]);
119        sb.append('|');
120    }
121    sb.append('\n');
122    for (i = 0; i < nrows; i++) {
123        int k;
124        String row[] = (String[]) rows.elementAt(i);
125        for (k = 0; k < ncolumns; k++) {
126        sb.append(row[k] == null ? "NULL" : row[k]);
127        sb.append('|');
128        }
129        sb.append('\n');
130    }
131    return sb.toString();
132    }
133}
134