Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.j256.ormlite.android;

import java.lang.reflect.Array;
import java.lang.reflect.Method;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -213,12 +215,18 @@ public String toString() {
* Execute some SQL on the database and return the number of rows changed.
*/
static int execSql(SQLiteDatabase db, String label, String finalSql, Object[] argArray) throws SQLException {
int result = -1;
try {
db.execSQL(finalSql, argArray);
result = exeSqlCompatibly(db, finalSql, argArray);
} catch (android.database.SQLException e) {
throw new SQLException("Problems executing " + label + " Android statement: " + finalSql, e);
}
int result;

// reflected sql method returns will >= 0
Comment thread
TYZRPVX marked this conversation as resolved.
Outdated
if (result >= 0) {
return result;
}

SQLiteStatement stmt = null;
try {
// ask sqlite how many rows were just changed
Expand All @@ -236,6 +244,41 @@ static int execSql(SQLiteDatabase db, String label, String finalSql, Object[] ar
return result;
}

/**
* Use reflection first, when reflection error, use origin method without return code
* @return modified rows count
*/
private static int exeSqlCompatibly(SQLiteDatabase db, String sql, Object[] bindArgs) {
Comment thread
TYZRPVX marked this conversation as resolved.
Outdated
try {
int result = invokeExecSqlMethod(db, sql, bindArgs);
return result;
} catch (android.database.SQLException e) {
Comment thread
TYZRPVX marked this conversation as resolved.
Outdated
db.execSQL(sql, bindArgs);
return -1;
Comment thread
TYZRPVX marked this conversation as resolved.
Outdated
}
}

private static int invokeExecSqlMethod(SQLiteDatabase db, String sql, Object[] bindArgs) throws android.database.SQLException {
Comment thread
TYZRPVX marked this conversation as resolved.
Outdated
try {
Object result = hiddenExecSqlMethod().invoke(db, sql, bindArgs);
Comment thread
TYZRPVX marked this conversation as resolved.
Outdated
logger.trace("invokeExecSqlMethod result: {}", result);
return (int) result;
} catch (Exception e) {
throw new android.database.SQLException("reflect error", e);
}
}

private static Method hiddenExecSqlMethod() {
Comment thread
TYZRPVX marked this conversation as resolved.
Class<SQLiteDatabase> clz = SQLiteDatabase.class;
Class<?> objArrClass = Array.newInstance(Object.class, 0).getClass();
Comment thread
TYZRPVX marked this conversation as resolved.
Outdated
try {
Method executeSqlMethod = clz.getMethod("executeSql", String.class, objArrClass);
Comment thread
TYZRPVX marked this conversation as resolved.
Outdated
return executeSqlMethod;
} catch (NoSuchMethodException e) {
return null;
}
}

private void isInPrep() throws SQLException {
if (cursor != null) {
throw new SQLException("Query already run. Cannot add argument values.");
Expand Down