Skip to content

Commit 36e9bb9

Browse files
committed
fix(android): Fix "Null" Strings in Android for INSERT and UPDATE statements. Close #27
1 parent ac84c2b commit 36e9bb9

File tree

1 file changed

+28
-10
lines changed

1 file changed

+28
-10
lines changed

android/src/main/java/dog/craftz/sqlite_2/RNSqlite2Module.java

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,15 @@ public void run() {
8282
for (int i = 0; i < numQueries; i++) {
8383
ReadableArray sqlQuery = queries.getArray(i);
8484
String sql = sqlQuery.getString(0);
85-
String[] bindArgs = convertParamsToStringArray(sqlQuery.getArray(1));
85+
ReadableArray queryArgs = sqlQuery.getArray(1);
8686
try {
8787
if (isSelect(sql)) {
88-
results[i] = doSelectInBackgroundAndPossiblyThrow(sql, bindArgs, db);
88+
results[i] = doSelectInBackgroundAndPossiblyThrow(sql, queryArgs, db);
8989
} else { // update/insert/delete
9090
if (readOnly) {
9191
results[i] = new SQLitePLuginResult(EMPTY_ROWS, EMPTY_COLUMNS, 0, 0, new ReadOnlyException());
9292
} else {
93-
results[i] = doUpdateInBackgroundAndPossiblyThrow(sql, bindArgs, db);
93+
results[i] = doUpdateInBackgroundAndPossiblyThrow(sql, queryArgs, db);
9494
}
9595
}
9696
} catch (Throwable e) {
@@ -110,15 +110,33 @@ public void run() {
110110
}
111111

112112
// do a update/delete/insert operation
113-
private SQLitePLuginResult doUpdateInBackgroundAndPossiblyThrow(String sql, String[] bindArgs,
114-
SQLiteDatabase db) {
113+
private SQLitePLuginResult doUpdateInBackgroundAndPossiblyThrow(String sql, ReadableArray queryArgs, SQLiteDatabase db) throws IllegalArgumentException {
115114
debug("\"run\" query: %s", sql);
116115
SQLiteStatement statement = null;
117116
try {
118117
statement = db.compileStatement(sql);
119118
debug("compiled statement");
120-
if (bindArgs != null) {
121-
statement.bindAllArgsAsStrings(bindArgs);
119+
// Bind all of the arguments
120+
for (int i = 0; i < queryArgs.size(); i++) {
121+
ReadableType type = queryArgs.getType(i);
122+
switch (type) {
123+
case Null:
124+
statement.bindNull(i + 1);
125+
break;
126+
case Boolean:
127+
final long b = queryArgs.getBoolean(i) ? 1 : 0;
128+
statement.bindLong(i + 1, b);
129+
break;
130+
case Number:
131+
final double f = queryArgs.getDouble(i);
132+
statement.bindDouble(i + 1, f);
133+
break;
134+
case String:
135+
statement.bindString(i + 1, unescapeBlob(queryArgs.getString(i)));
136+
break;
137+
default:
138+
throw new IllegalArgumentException("Unexpected data type given");
139+
}
122140
}
123141
debug("bound args");
124142
if (isInsert(sql)) {
@@ -145,11 +163,11 @@ private SQLitePLuginResult doUpdateInBackgroundAndPossiblyThrow(String sql, Stri
145163
}
146164

147165
// do a select operation
148-
private SQLitePLuginResult doSelectInBackgroundAndPossiblyThrow(String sql, String[] bindArgs,
149-
SQLiteDatabase db) {
166+
private SQLitePLuginResult doSelectInBackgroundAndPossiblyThrow(String sql, ReadableArray queryArgs, SQLiteDatabase db) {
150167
debug("\"all\" query: %s", sql);
151168
Cursor cursor = null;
152169
try {
170+
String[] bindArgs = convertParamsToStringArray(queryArgs);
153171
cursor = db.rawQuery(sql, bindArgs);
154172
int numRows = cursor.getCount();
155173
if (numRows == 0) {
@@ -177,7 +195,7 @@ private SQLitePLuginResult doSelectInBackgroundAndPossiblyThrow(String sql, Stri
177195
private Object getValueFromCursor(Cursor cursor, int index, int columnType) {
178196
switch (columnType) {
179197
case Cursor.FIELD_TYPE_FLOAT:
180-
return cursor.getFloat(index);
198+
return cursor.getDouble(index);
181199
case Cursor.FIELD_TYPE_INTEGER:
182200
return cursor.getInt(index);
183201
case Cursor.FIELD_TYPE_BLOB:

0 commit comments

Comments
 (0)