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