1417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughespackage SQLite;
2417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes
3417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughesimport java.util.Vector;
4417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes
5417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes/**
6417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * Class representing an SQLite result set as
7417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * returned by the
8417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * <A HREF="Database.html#get_table(java.lang.String)">Database.get_table</A>
9417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * convenience method.
10417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * <BR><BR>
11417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * Example:<BR>
12417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes *
13417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * <PRE>
14417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes *   ...
15417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes *   SQLite.Database db = new SQLite.Database();
16417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes *   db.open("db", 0);
17417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes *   System.out.print(db.get_table("select * from TEST"));
18417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes *   ...
19417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * </PRE>
20417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * Example output:<BR>
21417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes *
22417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * <PRE>
23417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes *   id|firstname|lastname|
24417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes *   0|John|Doe|
25417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes *   1|Speedy|Gonzales|
26417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes *   ...
27417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes * </PRE>
28417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes */
29417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes
30417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughespublic class TableResult implements Callback {
31417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes
32417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes    /**
33417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes     * Number of columns in the result set.
34417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes     */
35417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes
36417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes    public int ncolumns;
37417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes
38417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes    /**
39417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes     * Number of rows in the result set.
40417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes     */
41417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes
42417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes    public int nrows;
43417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes
44417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes    /**
45417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes     * Column names of the result set.
46417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes     */
47417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes
48417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes    public String column[];
49417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes
50417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes    /**
51417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes     * Types of columns of the result set or null.
52417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes     */
53417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes
54417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes    public String types[];
55417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes
56417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes    /**
57417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes     * Rows of the result set. Each row is stored as a String array.
58417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes     */
59417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes
60417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes    public Vector rows;
61417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes
62417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes    /**
637a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes     * Maximum number of rows to hold in the table.
647a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes     */
657a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes
667a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes    public int maxrows = 0;
677a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes
687a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes    /**
697a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes     * Flag to indicate Maximum number of rows condition.
707a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes     */
717a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes
727a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes    public boolean atmaxrows;
737a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes
747a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes    /**
75417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes     * Create an empty result set.
76417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes     */
77417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes
78417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes    public TableResult() {
797a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	clear();
807a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes    }
817a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes
827a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes    /**
837a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes     * Create an empty result set with maximum number of rows.
847a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes     */
857a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes
867a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes    public TableResult(int maxrows) {
877a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	this.maxrows = maxrows;
887a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	clear();
89417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes    }
90417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes
91417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes    /**
92417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes     * Clear result set.
93417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes     */
94417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes
95417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes    public void clear() {
967a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	column = new String[0];
977a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	types = null;
987a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	rows = new Vector();
997a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	ncolumns = nrows = 0;
1007a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	atmaxrows = false;
101417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes    }
102417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes
103417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes    /**
104417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes     * Callback method used while the query is executed.
105417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes     */
106417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes
107417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes    public void columns(String coldata[]) {
1087a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	column = coldata;
1097a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	ncolumns = column.length;
110417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes    }
111417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes
112417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes    /**
113417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes     * Callback method used while the query is executed.
114417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes     */
115417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes
116417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes    public void types(String types[]) {
1177a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	this.types = types;
118417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes    }
119417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes
120417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes    /**
121417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes     * Callback method used while the query is executed.
122417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes     */
123417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes
124417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes    public boolean newrow(String rowdata[]) {
1257a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	if (rowdata != null) {
1267a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	    if (maxrows > 0 && nrows >= maxrows) {
1277a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes		atmaxrows = true;
1287a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes		return true;
1297a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	    }
1307a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	    rows.addElement(rowdata);
1317a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	    nrows++;
1327a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	}
1337a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	return false;
134417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes    }
135417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes
136417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes    /**
137417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes     * Make String representation of result set.
138417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes     */
139417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes
140417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes    public String toString() {
1417a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	StringBuffer sb = new StringBuffer();
1427a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	int i;
1437a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	for (i = 0; i < ncolumns; i++) {
1447a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	    sb.append(column[i] == null ? "NULL" : column[i]);
1457a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	    sb.append('|');
1467a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	}
1477a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	sb.append('\n');
1487a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	for (i = 0; i < nrows; i++) {
1497a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	    int k;
1507a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	    String row[] = (String[]) rows.elementAt(i);
1517a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	    for (k = 0; k < ncolumns; k++) {
1527a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes		sb.append(row[k] == null ? "NULL" : row[k]);
1537a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes		sb.append('|');
1547a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	    }
1557a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	    sb.append('\n');
1567a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	}
1577a647e8547e57ca573541be55b3728ef7ce376feElliott Hughes	return sb.toString();
158417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes    }
159417deb1db112103aff04231b6ca79772ff7d3a21Elliott Hughes}
160