Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

All statements and queries are closed in finally statements #132

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
125 changes: 64 additions & 61 deletions src/android-native/src/main/java/io/liteglue/SQLitePlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -699,84 +699,87 @@ private JSONObject executeSqlStatementNDK(String query, JSONArray paramsAsJson,

boolean hasRows;

SQLiteStatement myStatement = mydb.prepareStatement(query);

SQLiteStatement myStatement = null;
try {
for (int i = 0; i < paramsAsJson.length(); ++i) {
if (paramsAsJson.isNull(i)) {
myStatement.bindNull(i + 1);
} else {
Object p = paramsAsJson.get(i);
if (p instanceof Float || p instanceof Double)
myStatement.bindDouble(i + 1, paramsAsJson.getDouble(i));
else if (p instanceof Number)
myStatement.bindLong(i + 1, paramsAsJson.getLong(i));
else
myStatement.bindTextNativeString(i + 1, paramsAsJson.getString(i));
try {
myStatement = mydb.prepareStatement(query);
for (int i = 0; i < paramsAsJson.length(); ++i) {
if (paramsAsJson.isNull(i)) {
myStatement.bindNull(i + 1);
} else {
Object p = paramsAsJson.get(i);
if (p instanceof Float || p instanceof Double)
myStatement.bindDouble(i + 1, paramsAsJson.getDouble(i));
else if (p instanceof Number)
myStatement.bindLong(i + 1, paramsAsJson.getLong(i));
else
myStatement.bindTextNativeString(i + 1, paramsAsJson.getString(i));
}
}

hasRows = myStatement.step();
} catch (Exception ex) {
ex.printStackTrace();
String errorMessage = ex.getMessage();
Log.v("executeSqlBatch", "SQLitePlugin.executeSql[Batch](): Error=" + errorMessage);


throw ex;
}

hasRows = myStatement.step();
} catch (Exception ex) {
ex.printStackTrace();
String errorMessage = ex.getMessage();
Log.v("executeSqlBatch", "SQLitePlugin.executeSql[Batch](): Error=" + errorMessage);
// If query result has rows
if (hasRows) {
JSONArray rowsArrayResult = new JSONArray();
String key;
int colCount = myStatement.getColumnCount();

// cleanup statement and throw the exception:
myStatement.dispose();
throw ex;
}
// Build up JSON result object for each row
do {
JSONObject row = new JSONObject();
try {
for (int i = 0; i < colCount; ++i) {
key = myStatement.getColumnName(i);

// If query result has rows
if (hasRows) {
JSONArray rowsArrayResult = new JSONArray();
String key;
int colCount = myStatement.getColumnCount();
switch (myStatement.getColumnType(i)) {
case SQLColumnType.NULL:
row.put(key, JSONObject.NULL);
break;

case SQLColumnType.REAL:
row.put(key, myStatement.getColumnDouble(i));
break;

case SQLColumnType.INTEGER:
row.put(key, myStatement.getColumnLong(i));
break;

case SQLColumnType.BLOB:
case SQLColumnType.TEXT:
default: // (just in case)
row.put(key, myStatement.getColumnTextNativeString(i));
}

// Build up JSON result object for each row
do {
JSONObject row = new JSONObject();
try {
for (int i = 0; i < colCount; ++i) {
key = myStatement.getColumnName(i);

switch (myStatement.getColumnType(i)) {
case SQLColumnType.NULL:
row.put(key, JSONObject.NULL);
break;

case SQLColumnType.REAL:
row.put(key, myStatement.getColumnDouble(i));
break;

case SQLColumnType.INTEGER:
row.put(key, myStatement.getColumnLong(i));
break;

case SQLColumnType.BLOB:
case SQLColumnType.TEXT:
default: // (just in case)
row.put(key, myStatement.getColumnTextNativeString(i));
}

}
rowsArrayResult.put(row);

rowsArrayResult.put(row);
} catch (JSONException e) {
e.printStackTrace();
}
} while (myStatement.step());

try {
rowsResult.put("rows", rowsArrayResult);
} catch (JSONException e) {
e.printStackTrace();
}
} while (myStatement.step());

try {
rowsResult.put("rows", rowsArrayResult);
} catch (JSONException e) {
e.printStackTrace();
}
} finally {
if (myStatement != null) {
myStatement.dispose();
}
}

myStatement.dispose();

return rowsResult;
}
}
Expand Down
Loading