Skip to content

Commit ea4639f

Browse files
Add support for executing rawQuery with Object varargs as binding parameters
1 parent 2de69e1 commit ea4639f

File tree

5 files changed

+76
-10
lines changed

5 files changed

+76
-10
lines changed

sqlcipher/src/androidTest/java/net/zetetic/database/sqlcipher_cts/AndroidSQLCipherTestCase.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,18 @@ public abstract class AndroidSQLCipherTestCase {
2323
protected SQLiteDatabase database;
2424
protected String DATABASE_NAME = "database_test.db";
2525
protected String TAG = getClass().getSimpleName();
26+
protected File databasePath = null;
2627
protected Context context = null;
2728

2829
@Before
29-
public void setUp() throws Exception {
30+
public void setUp() {
3031
context = InstrumentationRegistry.getInstrumentation().getTargetContext();
3132
System.loadLibrary("sqlcipher");
32-
File databasePath = context.getDatabasePath(DATABASE_NAME);
33+
databasePath = context.getDatabasePath(DATABASE_NAME);
3334
databasePath.mkdirs();
3435
if (databasePath.exists()) {
3536
databasePath.delete();
3637
}
37-
database = SQLiteDatabase.openOrCreateDatabase(databasePath, null);
38-
assertNotNull(database);
3938
}
4039

4140
@After

sqlcipher/src/androidTest/java/net/zetetic/database/sqlcipher_cts/SQLCipherDatabaseTest.java

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import net.zetetic.database.sqlcipher.SQLiteDatabase;
77
import net.zetetic.database.sqlcipher.SQLiteDatabaseHook;
88

9+
import org.junit.Before;
910
import org.junit.Test;
1011

1112
import java.io.File;
@@ -18,12 +19,6 @@ public class SQLCipherDatabaseTest extends AndroidSQLCipherTestCase {
1819
@Test
1920
public void testConnectionWithPassword(){
2021
int a = 0, b = 0;
21-
database.close();
22-
database = null;
23-
File databasePath = context.getDatabasePath("foo.db");
24-
if(databasePath.exists()){
25-
databasePath.delete();
26-
}
2722
database = SQLiteDatabase.openOrCreateDatabase(databasePath, "foo", null, null);
2823
database.execSQL("create table t1(a,b);");
2924
database.execSQL("insert into t1(a,b) values(?,?)", new Object[]{1, 2});
@@ -35,4 +30,20 @@ public void testConnectionWithPassword(){
3530
assertThat(a, is(1));
3631
assertThat(b, is(2));
3732
}
33+
34+
@Test
35+
public void insertDataQueryByObjectParams(){
36+
float a = 1.25f, a1 = 0.0f;
37+
double b = 2.00123d, b1 = 0.0d;
38+
database = SQLiteDatabase.openOrCreateDatabase(databasePath, "foo", null, null);
39+
database.execSQL("create table t1(a,b);");
40+
database.execSQL("insert into t1(a,b) values(?,?)", new Object[]{a, b});
41+
Cursor cursor = database.rawQuery("select * from t1 where a = ? and b = ?;", a, b);
42+
if(cursor != null && cursor.moveToFirst()){
43+
a1 = cursor.getFloat(0);
44+
b1 = cursor.getDouble(1);
45+
}
46+
assertThat(a1, is(a));
47+
assertThat(b1, is(b));
48+
}
3849
}

sqlcipher/src/main/java/net/zetetic/database/sqlcipher/SQLiteDatabase.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1435,6 +1435,26 @@ public Cursor rawQuery(String sql, String[] selectionArgs) {
14351435
return rawQueryWithFactory(null, sql, selectionArgs, null, null);
14361436
}
14371437

1438+
/**
1439+
* Runs the provided SQL and returns a {@link Cursor} over the result set.
1440+
*
1441+
* @param sql the SQL query. The SQL string must not be ; terminated
1442+
* @param bindingArgs You may include ?s in where clause in the query,
1443+
* which will be replaced by the values from bindingArgs. The
1444+
* values will be bound as Objects.
1445+
* @return A {@link Cursor} object, which is positioned before the first entry. Note that
1446+
* {@link Cursor}s are not synchronized, see the documentation for more details.
1447+
*/
1448+
public Cursor rawQuery(String sql, Object... bindingArgs) {
1449+
acquireReference();
1450+
try {
1451+
SQLiteDirectCursorDriver driver = new SQLiteDirectCursorDriver(this, sql, null, null);
1452+
return driver.query(mCursorFactory, bindingArgs);
1453+
} finally {
1454+
releaseReference();
1455+
}
1456+
}
1457+
14381458
/**
14391459
* Runs the provided SQL and returns a {@link Cursor} over the result set.
14401460
*

sqlcipher/src/main/java/net/zetetic/database/sqlcipher/SQLiteDirectCursorDriver.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,26 @@ public Cursor query(CursorFactory factory, String[] selectionArgs) {
6464
return cursor;
6565
}
6666

67+
public Cursor query(CursorFactory factory, Object... bindingArgs){
68+
final SQLiteQuery query = new SQLiteQuery(mDatabase, mSql, mCancellationSignal);
69+
final Cursor cursor;
70+
try {
71+
query.bindAllArgs(bindingArgs);
72+
73+
if (factory == null) {
74+
cursor = new SQLiteCursor(this, mEditTable, query);
75+
} else {
76+
cursor = factory.newCursor(mDatabase, this, mEditTable, query);
77+
}
78+
} catch (RuntimeException ex) {
79+
query.close();
80+
throw ex;
81+
}
82+
83+
mQuery = query;
84+
return cursor;
85+
}
86+
6787
public void cursorClosed() {
6888
// Do nothing
6989
}

sqlcipher/src/main/java/net/zetetic/database/sqlcipher/SQLiteProgram.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,22 @@ public void bindAllArgsAsStrings(String[] bindArgs) {
206206
}
207207
}
208208

209+
/**
210+
* Given a varargs of Object bindArgs, this method binds all of them in one single call.
211+
*
212+
* @param bindArgs the varargs of bind args, none of which must be null.
213+
*/
214+
public void bindAllArgs(Object... bindArgs){
215+
if (bindArgs != null) {
216+
for (int i = bindArgs.length; i != 0; i--) {
217+
if (bindArgs[i - 1] == null) {
218+
throw new IllegalArgumentException("the bind value at index " + i + " is null");
219+
}
220+
bind(i, bindArgs[i - 1]);
221+
}
222+
}
223+
}
224+
209225
@Override
210226
protected void onAllReferencesReleased() {
211227
clearBindings();

0 commit comments

Comments
 (0)