mirror of
https://github.com/moparisthebest/PhoneGap-SQLitePlugin-Android
synced 2024-11-21 08:25:01 -05:00
Fix Android version to handle multiple databases (#2)
This commit is contained in:
parent
938bce69b8
commit
ca74669116
@ -44,7 +44,7 @@ do ->
|
||||
console.log "SQLitePlugin::executePragmaStatement"
|
||||
pcb = success
|
||||
|
||||
cordova.exec (-> 1), error, "SQLitePlugin", "executePragmaStatement", [ "A-1", statement ]
|
||||
cordova.exec (-> 1), error, "SQLitePlugin", "executePragmaStatement", [ @dbPath, statement ]
|
||||
return
|
||||
|
||||
SQLitePluginCallback =
|
||||
@ -81,6 +81,7 @@ do ->
|
||||
transaction_callback_queue[@trans_id] = new Object()
|
||||
return
|
||||
|
||||
# XXX FUTURE handle tx CBs under SQLitePluginCallback object:
|
||||
SQLitePluginTransaction.queryCompleteCallback = (transId, queryId, result) ->
|
||||
console.log "SQLitePluginTransaction.queryCompleteCallback"
|
||||
query = null
|
||||
@ -198,10 +199,13 @@ do ->
|
||||
if error
|
||||
errorcb = (res) ->
|
||||
error txself, res
|
||||
|
||||
transaction_callback_queue[@trans_id]["success"] = successcb
|
||||
transaction_callback_queue[@trans_id]["error"] = errorcb
|
||||
cordova.exec null, null, "SQLitePlugin", "executeSqlBatch", transaction_queue[@trans_id]
|
||||
|
||||
cordova.exec null, null, "SQLitePlugin", "executeSqlBatch", [ @dbPath, transaction_queue[@trans_id] ]
|
||||
|
||||
# XXX FUTURE all CBs under SQLitePluginCallback
|
||||
# required for callbacks:
|
||||
root.SQLitePluginTransaction = SQLitePluginTransaction
|
||||
root.SQLitePluginCallback = SQLitePluginCallback
|
||||
|
@ -50,7 +50,7 @@
|
||||
pcb = success;
|
||||
cordova.exec((function() {
|
||||
return 1;
|
||||
}), error, "SQLitePlugin", "executePragmaStatement", ["A-1", statement]);
|
||||
}), error, "SQLitePlugin", "executePragmaStatement", [this.dbPath, statement]);
|
||||
};
|
||||
SQLitePluginCallback = {
|
||||
p1: function(id, result) {
|
||||
@ -230,7 +230,7 @@
|
||||
}
|
||||
transaction_callback_queue[this.trans_id]["success"] = successcb;
|
||||
transaction_callback_queue[this.trans_id]["error"] = errorcb;
|
||||
return cordova.exec(null, null, "SQLitePlugin", "executeSqlBatch", transaction_queue[this.trans_id]);
|
||||
return cordova.exec(null, null, "SQLitePlugin", "executeSqlBatch", [this.dbPath, transaction_queue[this.trans_id]]);
|
||||
};
|
||||
root.SQLitePluginTransaction = SQLitePluginTransaction;
|
||||
root.SQLitePluginCallback = SQLitePluginCallback;
|
||||
|
@ -20,17 +20,21 @@ import android.content.Context;
|
||||
import android.database.Cursor;
|
||||
import android.database.sqlite.*;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
public class SQLitePlugin extends Plugin {
|
||||
|
||||
// Data Definition Language
|
||||
SQLiteDatabase myDb = null; // Database object
|
||||
/**
|
||||
* Multiple database map.
|
||||
*/
|
||||
HashMap<String, SQLiteDatabase> myDbMap;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
public SQLitePlugin() {
|
||||
myDbMap = new HashMap<String, SQLiteDatabase>();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -54,15 +58,20 @@ public class SQLitePlugin extends Plugin {
|
||||
"database", 5000000);
|
||||
//this.openDatabase(args.getString(0), args.getString(1),
|
||||
// args.getString(2), args.getLong(3));
|
||||
}
|
||||
else if (action.equals("executePragmaStatement"))
|
||||
}
|
||||
// XXX TODO:
|
||||
else if (action.equals("close")) {
|
||||
Log.v("error", "NOT IMPLEMENTED"); // XXX TODO
|
||||
}
|
||||
else if (action.equals("executePragmaStatement"))
|
||||
{
|
||||
String id = args.getString(0);
|
||||
String dbName = args.getString(0);
|
||||
String query = args.getString(1);
|
||||
Cursor myCursor = this.myDb.rawQuery(query, null);
|
||||
|
||||
Cursor myCursor = this.getDatabase(dbName).rawQuery(query, null);
|
||||
this.processPragmaResults(myCursor, id);
|
||||
}
|
||||
else if (action.equals("executeSqlBatch"))
|
||||
}
|
||||
else if (action.equals("executeSqlBatch"))
|
||||
{
|
||||
String[] queries = null;
|
||||
String[] queryIDs = null;
|
||||
@ -71,18 +80,21 @@ public class SQLitePlugin extends Plugin {
|
||||
JSONArray jsonArr = null;
|
||||
int paramLen = 0;
|
||||
JSONArray[] jsonparams = null;
|
||||
|
||||
if (args.isNull(0)) {
|
||||
|
||||
String dbName = args.getString(0);
|
||||
JSONArray txargs = args.getJSONArray(1);
|
||||
|
||||
if (txargs.isNull(0)) {
|
||||
queries = new String[0];
|
||||
} else {
|
||||
int len = args.length();
|
||||
int len = txargs.length();
|
||||
queries = new String[len];
|
||||
queryIDs = new String[len];
|
||||
jsonparams = new JSONArray[len];
|
||||
|
||||
for (int i = 0; i < len; i++)
|
||||
{
|
||||
a = args.getJSONObject(i);
|
||||
a = txargs.getJSONObject(i);
|
||||
queries[i] = a.getString("query");
|
||||
queryIDs[i] = a.getString("query_id");
|
||||
trans_id = a.getString("trans_id");
|
||||
@ -92,7 +104,7 @@ public class SQLitePlugin extends Plugin {
|
||||
}
|
||||
}
|
||||
if(trans_id != null)
|
||||
this.executeSqlBatch(queries, jsonparams, queryIDs, trans_id);
|
||||
this.executeSqlBatch(dbName, queries, jsonparams, queryIDs, trans_id);
|
||||
else
|
||||
Log.v("error", "null trans_id");
|
||||
}
|
||||
@ -119,10 +131,12 @@ public class SQLitePlugin extends Plugin {
|
||||
*/
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
/** XXX TODO :
|
||||
if (this.myDb != null) {
|
||||
this.myDb.close();
|
||||
this.myDb = null;
|
||||
}
|
||||
**/
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
@ -141,28 +155,31 @@ public class SQLitePlugin extends Plugin {
|
||||
* @param size
|
||||
* The size in bytes
|
||||
*/
|
||||
public void openDatabase(String db, String version, String display_name,
|
||||
private void openDatabase(String db, String version, String display_name,
|
||||
long size) {
|
||||
|
||||
// If database is open, then close it
|
||||
if (this.myDb != null) {
|
||||
this.myDb.close();
|
||||
}
|
||||
|
||||
this.myDb = this.cordova.getActivity().getApplicationContext().openOrCreateDatabase(db + ".db", Context.MODE_PRIVATE, null);
|
||||
SQLiteDatabase myDb = this.cordova.getActivity().getApplicationContext().openOrCreateDatabase(db + ".db", Context.MODE_PRIVATE, null);
|
||||
myDbMap.put(db, myDb);
|
||||
}
|
||||
|
||||
public void executeSqlBatch(String[] queryarr, JSONArray[] jsonparams, String[] queryIDs, String tx_id) {
|
||||
private SQLiteDatabase getDatabase(String dbName) {
|
||||
return myDbMap.get(dbName);
|
||||
}
|
||||
|
||||
private void executeSqlBatch(String dbName, String[] queryarr, JSONArray[] jsonparams, String[] queryIDs, String tx_id) {
|
||||
SQLiteDatabase myDb = this.getDatabase(dbName); // XXX TODO check for null
|
||||
|
||||
try {
|
||||
this.myDb.beginTransaction();
|
||||
myDb.beginTransaction();
|
||||
|
||||
String query = "";
|
||||
String query_id = "";
|
||||
int len = queryarr.length;
|
||||
|
||||
for (int i = 0; i < len; i++) {
|
||||
query = queryarr[i];
|
||||
query_id = queryIDs[i];
|
||||
if (query.toLowerCase().startsWith("insert") && jsonparams != null) {
|
||||
SQLiteStatement myStatement = this.myDb.compileStatement(query);
|
||||
SQLiteStatement myStatement = myDb.compileStatement(query);
|
||||
for (int j = 0; j < jsonparams[i].length(); j++) {
|
||||
if (jsonparams[i].get(j) instanceof Float || jsonparams[i].get(j) instanceof Double ) {
|
||||
myStatement.bindDouble(j + 1, jsonparams[i].getDouble(j));
|
||||
@ -189,13 +206,13 @@ public class SQLitePlugin extends Plugin {
|
||||
}
|
||||
}
|
||||
|
||||
Cursor myCursor = this.myDb.rawQuery(query, params);
|
||||
Cursor myCursor = myDb.rawQuery(query, params);
|
||||
|
||||
this.processResults(myCursor, query_id, tx_id);
|
||||
myCursor.close();
|
||||
}
|
||||
}
|
||||
this.myDb.setTransactionSuccessful();
|
||||
myDb.setTransactionSuccessful();
|
||||
}
|
||||
catch (SQLiteException ex) {
|
||||
ex.printStackTrace();
|
||||
@ -207,7 +224,7 @@ public class SQLitePlugin extends Plugin {
|
||||
this.sendJavascript("SQLitePluginTransaction.txErrorCallback('" + tx_id + "', '"+ex.getMessage()+"');");
|
||||
}
|
||||
finally {
|
||||
this.myDb.endTransaction();
|
||||
myDb.endTransaction();
|
||||
Log.v("executeSqlBatch", tx_id);
|
||||
this.sendJavascript("SQLitePluginTransaction.txCompleteCallback('" + tx_id + "');");
|
||||
}
|
||||
@ -221,11 +238,12 @@ public class SQLitePlugin extends Plugin {
|
||||
* @param tx_id
|
||||
* Transaction id
|
||||
*/
|
||||
public void processResults(Cursor cur, String query_id, String tx_id) {
|
||||
|
||||
private void processResults(Cursor cur, String query_id, String tx_id)
|
||||
{
|
||||
String result = "[]";
|
||||
// If query result has rows
|
||||
|
||||
// XXX TODO use results2string() and do test:
|
||||
if (cur.moveToFirst()) {
|
||||
JSONArray fullresult = new JSONArray();
|
||||
String key = "";
|
||||
@ -280,8 +298,15 @@ public class SQLitePlugin extends Plugin {
|
||||
|
||||
}
|
||||
|
||||
public void processPragmaResults(Cursor cur, String id) {
|
||||
private void processPragmaResults(Cursor cur, String id)
|
||||
{
|
||||
String result = this.results2string(cur);
|
||||
|
||||
this.sendJavascript(" SQLitePluginCallback.p1('" + id + "', " + result + ");");
|
||||
}
|
||||
|
||||
private String results2string(Cursor cur)
|
||||
{
|
||||
String result = "[]";
|
||||
|
||||
// If query result has rows
|
||||
@ -297,7 +322,6 @@ public class SQLitePlugin extends Plugin {
|
||||
for (int i = 0; i < colCount; ++i) {
|
||||
key = cur.getColumnName(i);
|
||||
|
||||
// XXX TBD factor out:
|
||||
// for old Android SDK remove lines from HERE:
|
||||
if(android.os.Build.VERSION.SDK_INT >= 11)
|
||||
{
|
||||
@ -337,7 +361,6 @@ public class SQLitePlugin extends Plugin {
|
||||
result = fullresult.toString();
|
||||
}
|
||||
|
||||
this.sendJavascript(" SQLitePluginCallback.p1('" + id + "', " + result + ");");
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user