• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package SQLite;
2 
3 import 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 
30 public 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      * Maximum number of rows to hold in the table.
64      */
65 
66     public int maxrows = 0;
67 
68     /**
69      * Flag to indicate Maximum number of rows condition.
70      */
71 
72     public boolean atmaxrows;
73 
74     /**
75      * Create an empty result set.
76      */
77 
TableResult()78     public TableResult() {
79 	clear();
80     }
81 
82     /**
83      * Create an empty result set with maximum number of rows.
84      */
85 
TableResult(int maxrows)86     public TableResult(int maxrows) {
87 	this.maxrows = maxrows;
88 	clear();
89     }
90 
91     /**
92      * Clear result set.
93      */
94 
clear()95     public void clear() {
96 	column = new String[0];
97 	types = null;
98 	rows = new Vector();
99 	ncolumns = nrows = 0;
100 	atmaxrows = false;
101     }
102 
103     /**
104      * Callback method used while the query is executed.
105      */
106 
columns(String coldata[])107     public void columns(String coldata[]) {
108 	column = coldata;
109 	ncolumns = column.length;
110     }
111 
112     /**
113      * Callback method used while the query is executed.
114      */
115 
types(String types[])116     public void types(String types[]) {
117 	this.types = types;
118     }
119 
120     /**
121      * Callback method used while the query is executed.
122      */
123 
newrow(String rowdata[])124     public boolean newrow(String rowdata[]) {
125 	if (rowdata != null) {
126 	    if (maxrows > 0 && nrows >= maxrows) {
127 		atmaxrows = true;
128 		return true;
129 	    }
130 	    rows.addElement(rowdata);
131 	    nrows++;
132 	}
133 	return false;
134     }
135 
136     /**
137      * Make String representation of result set.
138      */
139 
toString()140     public String toString() {
141 	StringBuffer sb = new StringBuffer();
142 	int i;
143 	for (i = 0; i < ncolumns; i++) {
144 	    sb.append(column[i] == null ? "NULL" : column[i]);
145 	    sb.append('|');
146 	}
147 	sb.append('\n');
148 	for (i = 0; i < nrows; i++) {
149 	    int k;
150 	    String row[] = (String[]) rows.elementAt(i);
151 	    for (k = 0; k < ncolumns; k++) {
152 		sb.append(row[k] == null ? "NULL" : row[k]);
153 		sb.append('|');
154 	    }
155 	    sb.append('\n');
156 	}
157 	return sb.toString();
158     }
159 }
160