1*fd76c71bSTreehugger Robot package SQLite; 2*fd76c71bSTreehugger Robot 3*fd76c71bSTreehugger Robot /** 4*fd76c71bSTreehugger Robot * Class to represent compiled SQLite3 statement. 5*fd76c71bSTreehugger Robot * 6*fd76c71bSTreehugger Robot * Note, that all native methods of this class are 7*fd76c71bSTreehugger Robot * not synchronized, i.e. it is up to the caller 8*fd76c71bSTreehugger Robot * to ensure that only one thread is in these 9*fd76c71bSTreehugger Robot * methods at any one time. 10*fd76c71bSTreehugger Robot */ 11*fd76c71bSTreehugger Robot 12*fd76c71bSTreehugger Robot public class Stmt { 13*fd76c71bSTreehugger Robot 14*fd76c71bSTreehugger Robot /** 15*fd76c71bSTreehugger Robot * Internal handle for the SQLite3 statement. 16*fd76c71bSTreehugger Robot */ 17*fd76c71bSTreehugger Robot 18*fd76c71bSTreehugger Robot private long handle = 0; 19*fd76c71bSTreehugger Robot 20*fd76c71bSTreehugger Robot /** 21*fd76c71bSTreehugger Robot * Internal last error code for prepare()/step() methods. 22*fd76c71bSTreehugger Robot */ 23*fd76c71bSTreehugger Robot 24*fd76c71bSTreehugger Robot protected int error_code = 0; 25*fd76c71bSTreehugger Robot 26*fd76c71bSTreehugger Robot /** 27*fd76c71bSTreehugger Robot * Prepare the next SQL statement for the Stmt instance. 28*fd76c71bSTreehugger Robot * @return true when the next piece of the SQL statement sequence 29*fd76c71bSTreehugger Robot * has been prepared, false on end of statement sequence. 30*fd76c71bSTreehugger Robot */ 31*fd76c71bSTreehugger Robot prepare()32*fd76c71bSTreehugger Robot public native boolean prepare() throws SQLite.Exception; 33*fd76c71bSTreehugger Robot 34*fd76c71bSTreehugger Robot /** 35*fd76c71bSTreehugger Robot * Perform one step of compiled SQLite3 statement. 36*fd76c71bSTreehugger Robot * 37*fd76c71bSTreehugger Robot * Example:<BR> 38*fd76c71bSTreehugger Robot * <PRE> 39*fd76c71bSTreehugger Robot * ... 40*fd76c71bSTreehugger Robot * try { 41*fd76c71bSTreehugger Robot * Stmt s = db.prepare("select * from x; select * from y;"); 42*fd76c71bSTreehugger Robot * s.bind(...); 43*fd76c71bSTreehugger Robot * ... 44*fd76c71bSTreehugger Robot * s.bind(...); 45*fd76c71bSTreehugger Robot * while (s.step(cb)) { 46*fd76c71bSTreehugger Robot * Object o = s.value(...); 47*fd76c71bSTreehugger Robot * ... 48*fd76c71bSTreehugger Robot * } 49*fd76c71bSTreehugger Robot * // s.reset() for re-execution or 50*fd76c71bSTreehugger Robot * // s.prepare() for the next piece of SQL 51*fd76c71bSTreehugger Robot * while (s.prepare()) { 52*fd76c71bSTreehugger Robot * s.bind(...); 53*fd76c71bSTreehugger Robot * ... 54*fd76c71bSTreehugger Robot * s.bind(...); 55*fd76c71bSTreehugger Robot * while (s.step(cb)) { 56*fd76c71bSTreehugger Robot * Object o = s.value(...); 57*fd76c71bSTreehugger Robot * ... 58*fd76c71bSTreehugger Robot * } 59*fd76c71bSTreehugger Robot * } 60*fd76c71bSTreehugger Robot * } catch (SQLite.Exception e) { 61*fd76c71bSTreehugger Robot * s.close(); 62*fd76c71bSTreehugger Robot * } 63*fd76c71bSTreehugger Robot * </PRE> 64*fd76c71bSTreehugger Robot * 65*fd76c71bSTreehugger Robot * @return true when row data is available, false on end 66*fd76c71bSTreehugger Robot * of result set. 67*fd76c71bSTreehugger Robot */ 68*fd76c71bSTreehugger Robot step()69*fd76c71bSTreehugger Robot public native boolean step() throws SQLite.Exception; 70*fd76c71bSTreehugger Robot 71*fd76c71bSTreehugger Robot /** 72*fd76c71bSTreehugger Robot * Close the compiled SQLite3 statement. 73*fd76c71bSTreehugger Robot */ 74*fd76c71bSTreehugger Robot close()75*fd76c71bSTreehugger Robot public native void close() throws SQLite.Exception; 76*fd76c71bSTreehugger Robot 77*fd76c71bSTreehugger Robot /** 78*fd76c71bSTreehugger Robot * Reset the compiled SQLite3 statement without 79*fd76c71bSTreehugger Robot * clearing parameter bindings. 80*fd76c71bSTreehugger Robot */ 81*fd76c71bSTreehugger Robot reset()82*fd76c71bSTreehugger Robot public native void reset() throws SQLite.Exception; 83*fd76c71bSTreehugger Robot 84*fd76c71bSTreehugger Robot /** 85*fd76c71bSTreehugger Robot * Clear all bound parameters of the compiled SQLite3 statement. 86*fd76c71bSTreehugger Robot */ 87*fd76c71bSTreehugger Robot clear_bindings()88*fd76c71bSTreehugger Robot public native void clear_bindings() throws SQLite.Exception; 89*fd76c71bSTreehugger Robot 90*fd76c71bSTreehugger Robot /** 91*fd76c71bSTreehugger Robot * Bind positional integer value to compiled SQLite3 statement. 92*fd76c71bSTreehugger Robot * @param pos parameter index, 1-based 93*fd76c71bSTreehugger Robot * @param value value of parameter 94*fd76c71bSTreehugger Robot */ 95*fd76c71bSTreehugger Robot bind(int pos, int value)96*fd76c71bSTreehugger Robot public native void bind(int pos, int value) throws SQLite.Exception; 97*fd76c71bSTreehugger Robot 98*fd76c71bSTreehugger Robot /** 99*fd76c71bSTreehugger Robot * Bind positional long value to compiled SQLite3 statement. 100*fd76c71bSTreehugger Robot * @param pos parameter index, 1-based 101*fd76c71bSTreehugger Robot * @param value value of parameter 102*fd76c71bSTreehugger Robot */ 103*fd76c71bSTreehugger Robot bind(int pos, long value)104*fd76c71bSTreehugger Robot public native void bind(int pos, long value) throws SQLite.Exception; 105*fd76c71bSTreehugger Robot 106*fd76c71bSTreehugger Robot /** 107*fd76c71bSTreehugger Robot * Bind positional double value to compiled SQLite3 statement. 108*fd76c71bSTreehugger Robot * @param pos parameter index, 1-based 109*fd76c71bSTreehugger Robot * @param value value of parameter 110*fd76c71bSTreehugger Robot */ 111*fd76c71bSTreehugger Robot bind(int pos, double value)112*fd76c71bSTreehugger Robot public native void bind(int pos, double value) throws SQLite.Exception; 113*fd76c71bSTreehugger Robot 114*fd76c71bSTreehugger Robot /** 115*fd76c71bSTreehugger Robot * Bind positional byte array to compiled SQLite3 statement. 116*fd76c71bSTreehugger Robot * @param pos parameter index, 1-based 117*fd76c71bSTreehugger Robot * @param value value of parameter, may be null 118*fd76c71bSTreehugger Robot */ 119*fd76c71bSTreehugger Robot bind(int pos, byte[] value)120*fd76c71bSTreehugger Robot public native void bind(int pos, byte[] value) throws SQLite.Exception; 121*fd76c71bSTreehugger Robot 122*fd76c71bSTreehugger Robot /** 123*fd76c71bSTreehugger Robot * Bind positional String to compiled SQLite3 statement. 124*fd76c71bSTreehugger Robot * @param pos parameter index, 1-based 125*fd76c71bSTreehugger Robot * @param value value of parameter, may be null 126*fd76c71bSTreehugger Robot */ 127*fd76c71bSTreehugger Robot bind(int pos, String value)128*fd76c71bSTreehugger Robot public native void bind(int pos, String value) throws SQLite.Exception; 129*fd76c71bSTreehugger Robot 130*fd76c71bSTreehugger Robot /** 131*fd76c71bSTreehugger Robot * Bind positional SQL null to compiled SQLite3 statement. 132*fd76c71bSTreehugger Robot * @param pos parameter index, 1-based 133*fd76c71bSTreehugger Robot */ 134*fd76c71bSTreehugger Robot bind(int pos)135*fd76c71bSTreehugger Robot public native void bind(int pos) throws SQLite.Exception; 136*fd76c71bSTreehugger Robot 137*fd76c71bSTreehugger Robot /** 138*fd76c71bSTreehugger Robot * Bind positional zero'ed blob to compiled SQLite3 statement. 139*fd76c71bSTreehugger Robot * @param pos parameter index, 1-based 140*fd76c71bSTreehugger Robot * @param length byte size of zero blob 141*fd76c71bSTreehugger Robot */ 142*fd76c71bSTreehugger Robot bind_zeroblob(int pos, int length)143*fd76c71bSTreehugger Robot public native void bind_zeroblob(int pos, int length) 144*fd76c71bSTreehugger Robot throws SQLite.Exception; 145*fd76c71bSTreehugger Robot 146*fd76c71bSTreehugger Robot /** 147*fd76c71bSTreehugger Robot * Return number of parameters in compiled SQLite3 statement. 148*fd76c71bSTreehugger Robot * @return int number of parameters 149*fd76c71bSTreehugger Robot */ 150*fd76c71bSTreehugger Robot bind_parameter_count()151*fd76c71bSTreehugger Robot public native int bind_parameter_count() throws SQLite.Exception; 152*fd76c71bSTreehugger Robot 153*fd76c71bSTreehugger Robot /** 154*fd76c71bSTreehugger Robot * Return name of parameter in compiled SQLite3 statement. 155*fd76c71bSTreehugger Robot * @param pos parameter index, 1-based 156*fd76c71bSTreehugger Robot * @return String parameter name 157*fd76c71bSTreehugger Robot */ 158*fd76c71bSTreehugger Robot bind_parameter_name(int pos)159*fd76c71bSTreehugger Robot public native String bind_parameter_name(int pos) throws SQLite.Exception; 160*fd76c71bSTreehugger Robot 161*fd76c71bSTreehugger Robot /** 162*fd76c71bSTreehugger Robot * Return index of named parameter in compiled SQLite3 statement. 163*fd76c71bSTreehugger Robot * @param name of parameter 164*fd76c71bSTreehugger Robot * @return int index of parameter, 1-based 165*fd76c71bSTreehugger Robot */ 166*fd76c71bSTreehugger Robot bind_parameter_index(String name)167*fd76c71bSTreehugger Robot public native int bind_parameter_index(String name) 168*fd76c71bSTreehugger Robot throws SQLite.Exception; 169*fd76c71bSTreehugger Robot 170*fd76c71bSTreehugger Robot 171*fd76c71bSTreehugger Robot /** 172*fd76c71bSTreehugger Robot * Retrieve integer column from exec'ed SQLite3 statement. 173*fd76c71bSTreehugger Robot * @param col column number, 0-based 174*fd76c71bSTreehugger Robot * @return int column value 175*fd76c71bSTreehugger Robot */ 176*fd76c71bSTreehugger Robot column_int(int col)177*fd76c71bSTreehugger Robot public native int column_int(int col) throws SQLite.Exception; 178*fd76c71bSTreehugger Robot 179*fd76c71bSTreehugger Robot /** 180*fd76c71bSTreehugger Robot * Retrieve long column from exec'ed SQLite3 statement. 181*fd76c71bSTreehugger Robot * @param col column number, 0-based 182*fd76c71bSTreehugger Robot * @return long column value 183*fd76c71bSTreehugger Robot */ column_long(int col)184*fd76c71bSTreehugger Robot public native long column_long(int col) throws SQLite.Exception; 185*fd76c71bSTreehugger Robot 186*fd76c71bSTreehugger Robot /** 187*fd76c71bSTreehugger Robot * Retrieve double column from exec'ed SQLite3 statement. 188*fd76c71bSTreehugger Robot * @param col column number, 0-based 189*fd76c71bSTreehugger Robot * @return double column value 190*fd76c71bSTreehugger Robot */ column_double(int col)191*fd76c71bSTreehugger Robot public native double column_double(int col) throws SQLite.Exception; 192*fd76c71bSTreehugger Robot 193*fd76c71bSTreehugger Robot /** 194*fd76c71bSTreehugger Robot * Retrieve blob column from exec'ed SQLite3 statement. 195*fd76c71bSTreehugger Robot * @param col column number, 0-based 196*fd76c71bSTreehugger Robot * @return byte[] column value 197*fd76c71bSTreehugger Robot */ column_bytes(int col)198*fd76c71bSTreehugger Robot public native byte[] column_bytes(int col) throws SQLite.Exception; 199*fd76c71bSTreehugger Robot 200*fd76c71bSTreehugger Robot /** 201*fd76c71bSTreehugger Robot * Retrieve string column from exec'ed SQLite3 statement. 202*fd76c71bSTreehugger Robot * @param col column number, 0-based 203*fd76c71bSTreehugger Robot * @return String column value 204*fd76c71bSTreehugger Robot */ column_string(int col)205*fd76c71bSTreehugger Robot public native String column_string(int col) throws SQLite.Exception; 206*fd76c71bSTreehugger Robot 207*fd76c71bSTreehugger Robot /** 208*fd76c71bSTreehugger Robot * Retrieve column type from exec'ed SQLite3 statement. 209*fd76c71bSTreehugger Robot * @param col column number, 0-based 210*fd76c71bSTreehugger Robot * @return column type code, e.g. SQLite.Constants.SQLITE_INTEGER 211*fd76c71bSTreehugger Robot */ column_type(int col)212*fd76c71bSTreehugger Robot public native int column_type(int col) throws SQLite.Exception; 213*fd76c71bSTreehugger Robot 214*fd76c71bSTreehugger Robot /** 215*fd76c71bSTreehugger Robot * Retrieve number of columns of exec'ed SQLite3 statement. 216*fd76c71bSTreehugger Robot * @return int number of columns 217*fd76c71bSTreehugger Robot */ 218*fd76c71bSTreehugger Robot column_count()219*fd76c71bSTreehugger Robot public native int column_count() throws SQLite.Exception; 220*fd76c71bSTreehugger Robot 221*fd76c71bSTreehugger Robot /** 222*fd76c71bSTreehugger Robot * Retrieve column data as object from exec'ed SQLite3 statement. 223*fd76c71bSTreehugger Robot * @param col column number, 0-based 224*fd76c71bSTreehugger Robot * @return Object or null 225*fd76c71bSTreehugger Robot */ 226*fd76c71bSTreehugger Robot column(int col)227*fd76c71bSTreehugger Robot public Object column(int col) throws SQLite.Exception { 228*fd76c71bSTreehugger Robot switch (column_type(col)) { 229*fd76c71bSTreehugger Robot case Constants.SQLITE_INTEGER: 230*fd76c71bSTreehugger Robot return Long.valueOf(column_long(col)); // android-changed: performance 231*fd76c71bSTreehugger Robot case Constants.SQLITE_FLOAT: 232*fd76c71bSTreehugger Robot return new Double(column_double(col)); 233*fd76c71bSTreehugger Robot case Constants.SQLITE_BLOB: 234*fd76c71bSTreehugger Robot return column_bytes(col); 235*fd76c71bSTreehugger Robot case Constants.SQLITE3_TEXT: 236*fd76c71bSTreehugger Robot return column_string(col); 237*fd76c71bSTreehugger Robot } 238*fd76c71bSTreehugger Robot return null; 239*fd76c71bSTreehugger Robot } 240*fd76c71bSTreehugger Robot 241*fd76c71bSTreehugger Robot /** 242*fd76c71bSTreehugger Robot * Return table name of column of SQLite3 statement. 243*fd76c71bSTreehugger Robot * @param col column number, 0-based 244*fd76c71bSTreehugger Robot * @return String or null 245*fd76c71bSTreehugger Robot */ 246*fd76c71bSTreehugger Robot column_table_name(int col)247*fd76c71bSTreehugger Robot public native String column_table_name(int col) throws SQLite.Exception; 248*fd76c71bSTreehugger Robot 249*fd76c71bSTreehugger Robot /** 250*fd76c71bSTreehugger Robot * Return database name of column of SQLite3 statement. 251*fd76c71bSTreehugger Robot * @param col column number, 0-based 252*fd76c71bSTreehugger Robot * @return String or null 253*fd76c71bSTreehugger Robot */ 254*fd76c71bSTreehugger Robot column_database_name(int col)255*fd76c71bSTreehugger Robot public native String column_database_name(int col) throws SQLite.Exception; 256*fd76c71bSTreehugger Robot 257*fd76c71bSTreehugger Robot /** 258*fd76c71bSTreehugger Robot * Return declared column type of SQLite3 statement. 259*fd76c71bSTreehugger Robot * @param col column number, 0-based 260*fd76c71bSTreehugger Robot * @return String or null 261*fd76c71bSTreehugger Robot */ 262*fd76c71bSTreehugger Robot column_decltype(int col)263*fd76c71bSTreehugger Robot public native String column_decltype(int col) throws SQLite.Exception; 264*fd76c71bSTreehugger Robot 265*fd76c71bSTreehugger Robot /** 266*fd76c71bSTreehugger Robot * Return origin column name of column of SQLite3 statement. 267*fd76c71bSTreehugger Robot * @param col column number, 0-based 268*fd76c71bSTreehugger Robot * @return String or null 269*fd76c71bSTreehugger Robot */ 270*fd76c71bSTreehugger Robot column_origin_name(int col)271*fd76c71bSTreehugger Robot public native String column_origin_name(int col) throws SQLite.Exception; 272*fd76c71bSTreehugger Robot 273*fd76c71bSTreehugger Robot /** 274*fd76c71bSTreehugger Robot * Return statement status information. 275*fd76c71bSTreehugger Robot * @param op which counter to report 276*fd76c71bSTreehugger Robot * @param flg reset flag 277*fd76c71bSTreehugger Robot * @return counter 278*fd76c71bSTreehugger Robot */ 279*fd76c71bSTreehugger Robot status(int op, boolean flg)280*fd76c71bSTreehugger Robot public native int status(int op, boolean flg); 281*fd76c71bSTreehugger Robot 282*fd76c71bSTreehugger Robot /** 283*fd76c71bSTreehugger Robot * Destructor for object. 284*fd76c71bSTreehugger Robot */ 285*fd76c71bSTreehugger Robot finalize()286*fd76c71bSTreehugger Robot protected native void finalize(); 287*fd76c71bSTreehugger Robot 288*fd76c71bSTreehugger Robot /** 289*fd76c71bSTreehugger Robot * Internal native initializer. 290*fd76c71bSTreehugger Robot */ 291*fd76c71bSTreehugger Robot internal_init()292*fd76c71bSTreehugger Robot private static native void internal_init(); 293*fd76c71bSTreehugger Robot 294*fd76c71bSTreehugger Robot static { internal_init()295*fd76c71bSTreehugger Robot internal_init(); 296*fd76c71bSTreehugger Robot } 297*fd76c71bSTreehugger Robot } 298