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