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

Database.selectSQL: cache getColumn* methods (fixes #602) #604

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.HashMap;
import java.util.Map;
import net.sqlcipher.Cursor;
import net.sqlcipher.database.SQLiteDatabase;
import net.sqlcipher.database.SQLiteException;
Expand Down Expand Up @@ -979,45 +981,57 @@ public JSArray selectSQL(String statement, ArrayList<Object> values) throws Exce
}
try {
c = (Cursor) _db.query(statement, values.toArray(new Object[0]));
while (c.moveToNext()) {
JSObject row = new JSObject();
for (int i = 0; i < c.getColumnCount(); i++) {
String colName = c.getColumnName(i);
int index = c.getColumnIndex(colName);
int type = c.getType(i);
switch (type) {
case FIELD_TYPE_STRING:
row.put(colName, c.getString(index));
break;
case FIELD_TYPE_INTEGER:
row.put(colName, c.getLong(index));
break;
case FIELD_TYPE_FLOAT:
row.put(colName, c.getDouble(index));
break;
case FIELD_TYPE_BLOB:
byte[] blobVal = c.getBlob(index);
JSArray arr = this._uSqlite.ByteArrayToJSArray(blobVal);
row.put(colName, arr);
/* if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
row.put(colName, Base64.getEncoder().encodeToString(c.getBlob(index)));
} else {

if (c != null && c.moveToFirst()) { // Use moveToFirst() and do-while for potential performance gain

// Cache column information
int columnCount = c.getColumnCount();
String[] colNames = new String[columnCount];
int[] colIndices = new int[columnCount];
int[] colTypes = new int[columnCount];

for (int i = 0; i < columnCount; i++) {
colNames[i] = c.getColumnName(i);
colIndices[i] = c.getColumnIndex(colNames[i]);
colTypes[i] = c.getType(i);
}

do {
JSObject row = new JSObject();
for (int i = 0; i < columnCount; i++) {
String colName = colNames[i];
int index = colIndices[i];
int type = colTypes[i]; // No need to call getType again

switch (type) {
case FIELD_TYPE_STRING:
row.put(colName, c.getString(index));
break;
case FIELD_TYPE_INTEGER:
row.put(colName, c.getLong(index));
break;
case FIELD_TYPE_FLOAT:
row.put(colName, c.getDouble(index));
break;
case FIELD_TYPE_BLOB:
byte[] blobVal = c.getBlob(index);
JSArray arr = this._uSqlite.ByteArrayToJSArray(blobVal);
row.put(colName, arr);
break;
case FIELD_TYPE_NULL:
row.put(colName, JSONObject.NULL);
}
*/
break;
case FIELD_TYPE_NULL:
row.put(colName, JSONObject.NULL);
break;
default:
break;
break;
default:
break; // Consider logging unknown types for debugging
}
}
}
retArray.put(row);
retArray.put(row);
} while (c.moveToNext());
}

return retArray;
} catch (Exception e) {
throw new Exception("in selectSQL cursor " + e.getMessage());
throw new Exception("Error in selectSQL cursor: " + e.getMessage(), e); // Include the original exception for better debugging
} finally {
if (c != null) c.close();
}
Expand Down