New, optional interface to open sqlite plugin db

This commit is contained in:
Chris Brody 2012-12-17 22:23:40 +01:00
parent c3bec6fa08
commit ed46dd7952
4 changed files with 87 additions and 15 deletions

View File

@ -221,11 +221,35 @@ do ->
cordova.exec null, null, "SQLitePlugin", "executeSqlBatch", [ @dbPath, transaction_queue[@trans_id] ] cordova.exec null, null, "SQLitePlugin", "executeSqlBatch", [ @dbPath, transaction_queue[@trans_id] ]
return return
SQLiteFactory =
opendb: ->
if arguments.length < 1 then return null
first = arguments[0]
dbname = "DB"
okcb = null
errorcb = null
if first.constructor == String
dbname = first
if arguments.length >= 5
okcb = arguments[4]
if arguments.length > 5 then errorcb = arguments[5]
else
dbname = first['name']
if arguments.length >= 2
okcb = arguments[1]
if arguments.length > 2 then errorcb = arguments[2]
new SQLitePlugin dbname, okcb, errorcb
# Required for callbacks: # Required for callbacks:
root.SQLitePluginCallback = SQLitePluginCallback root.SQLitePluginCallback = SQLitePluginCallback
root.SQLitePluginTransactionCB = SQLitePluginTransactionCB root.SQLitePluginTransactionCB = SQLitePluginTransactionCB
root.sqlitePlugin = root.sqlitePlugin =
openDatabase: (dbPath, version, displayName, estimatedSize, creationCallback, errorCallback) -> #openDatabase: (dbPath, version, displayName, estimatedSize, creationCallback, errorCallback) ->
new SQLitePlugin(dbPath, creationCallback, errorCallback) # new SQLitePlugin(dbPath, creationCallback, errorCallback)
openDatabase: SQLiteFactory.opendb

View File

@ -1,5 +1,5 @@
(function() { (function() {
var SQLitePlugin, SQLitePluginCallback, SQLitePluginTransaction, SQLitePluginTransactionCB, get_unique_id, pcb, root, transaction_callback_queue, transaction_queue; var SQLiteFactory, SQLitePlugin, SQLitePluginCallback, SQLitePluginTransaction, SQLitePluginTransactionCB, get_unique_id, pcb, root, transaction_callback_queue, transaction_queue;
root = this; root = this;
SQLitePlugin = function(dbPath, openSuccess, openError) { SQLitePlugin = function(dbPath, openSuccess, openError) {
console.log("SQLitePlugin"); console.log("SQLitePlugin");
@ -224,11 +224,33 @@
transaction_callback_queue[this.trans_id]["error"] = errorcb; transaction_callback_queue[this.trans_id]["error"] = errorcb;
cordova.exec(null, null, "SQLitePlugin", "executeSqlBatch", [this.dbPath, transaction_queue[this.trans_id]]); cordova.exec(null, null, "SQLitePlugin", "executeSqlBatch", [this.dbPath, transaction_queue[this.trans_id]]);
}; };
SQLiteFactory = {
opendb: function() {
var dbname, errorcb, first, okcb;
if (arguments.length < 1) return null;
first = arguments[0];
dbname = "DB";
okcb = null;
errorcb = null;
if (first.constructor === String) {
dbname = first;
if (arguments.length >= 5) {
okcb = arguments[4];
if (arguments.length > 5) errorcb = arguments[5];
}
} else {
dbname = first['name'];
if (arguments.length >= 2) {
okcb = arguments[1];
if (arguments.length > 2) errorcb = arguments[2];
}
}
return new SQLitePlugin(dbname, okcb, errorcb);
}
};
root.SQLitePluginCallback = SQLitePluginCallback; root.SQLitePluginCallback = SQLitePluginCallback;
root.SQLitePluginTransactionCB = SQLitePluginTransactionCB; root.SQLitePluginTransactionCB = SQLitePluginTransactionCB;
return root.sqlitePlugin = { return root.sqlitePlugin = {
openDatabase: function(dbPath, version, displayName, estimatedSize, creationCallback, errorCallback) { openDatabase: SQLiteFactory.opendb
return new SQLitePlugin(dbPath, creationCallback, errorCallback);
}
}; };
})(); })();

View File

@ -1,4 +1,4 @@
# Cordova/PhoneGap SQLitePlugin - Android version # Cordova/PhoneGap sqlite plugin - Android version
Native interface to sqlite in a Cordova/PhoneGap plugin, working to follow the HTML5 Web SQL API as close as possible. Native interface to sqlite in a Cordova/PhoneGap plugin, working to follow the HTML5 Web SQL API as close as possible.
@ -10,6 +10,10 @@ License for this version: MIT or Apache
## Announcements ## Announcements
- New, optional interface to open a database like:
var db = window.sqlitePlugin.openDatabase({name: "DB"});
- [Improvements in the form of PRAGMAs & multiple database files (bug fix)](http://brodyspark.blogspot.com/2012/12/improvements-to-phonegap-sqliteplugin.html). - [Improvements in the form of PRAGMAs & multiple database files (bug fix)](http://brodyspark.blogspot.com/2012/12/improvements-to-phonegap-sqliteplugin.html).
- The Android version is now maintained in this location as [announced here](http://brodyspark.blogspot.com/2012/12/phonegap-sqliteplugin-for-ios-android.html). - The Android version is now maintained in this location as [announced here](http://brodyspark.blogspot.com/2012/12/phonegap-sqliteplugin-for-ios-android.html).
@ -20,27 +24,49 @@ License for this version: MIT or Apache
- batch processing optimizations - batch processing optimizations
- No 5MB maximum, more information at: http://www.sqlite.org/limits.html - No 5MB maximum, more information at: http://www.sqlite.org/limits.html
This SQLitePlugin can also be used with SQLCipher to provide encryption. This was already described on my old blog: This sqlite plugin can also be used with SQLCipher to provide encryption. This was already described on my old blog:
- [Android version with rebuilding SQLCipher from source](http://mobileapphelp.blogspot.com/2012/08/rebuilding-sqlitesqlcipher-for-android.html) - [Android version with rebuilding SQLCipher from source](http://mobileapphelp.blogspot.com/2012/08/rebuilding-sqlitesqlcipher-for-android.html)
- [Android version tested with SQLCipher for database encryption](http://mobileapphelp.blogspot.com/2012/08/trying-sqlcipher-with-cordova.html), working with a few changes to SQLitePlugin.java - [Android version tested with SQLCipher for database encryption](http://mobileapphelp.blogspot.com/2012/08/trying-sqlcipher-with-cordova.html), working with a few changes to SQLitePlugin.java
Updated instructions will be posted on my [new blog](http://brodyspark.blogspot.com/) sometime in the near future. Updated instructions will be posted on my [new blog](http://brodyspark.blogspot.com/) sometime in the near future.
## Apps using Cordova/PhoneGap SQLitePlugin (Android version) ## Apps using Cordova/PhoneGap sqlite plugin (Android version)
- [Get It Done app](http://getitdoneapp.com/) by [marcucio.com](http://marcucio.com/) - [Get It Done app](http://getitdoneapp.com/) by [marcucio.com](http://marcucio.com/)
- Upcoming (under development): Arbiter disastery recovery app by [LMN Solutions](http://lmnsolutions.com/) - Upcoming (under development): Arbiter disastery recovery app by [LMN Solutions](http://lmnsolutions.com/)
I would like to gather some more real-world examples, please send to chris.brody@gmail.com and I will post them. I would like to gather some more real-world examples, please send to chris.brody@gmail.com and I will post them.
## Known limitations ## known limitations
- `rowsAffected` field in the response to UPDATE and DELETE is not working - `rowsAffected` field in the response to UPDATE and DELETE is not working
- The db version, display name, and size parameter values are not supported and ignored. - The db version, display name, and size parameter values are not supported and will be ignored.
- The sqlite plugin will not work before the callback for the "deviceready" event has been fired, as in the following example:
// Wait for Cordova to load
document.addEventListener("deviceready", onDeviceReady, false);
// Cordova is ready
function onDeviceReady() {
var db = window.sqlitePlugin.openDatabase({name: "DB"});
// ...
}
Usage Usage
===== =====
The idea is to emulate the HTML5 SQL API as closely as possible. The only major change is to use window.sqlitePlugin.openDatabase() (or sqlitePlugin.openDatabase()) instead of window.openDatabase(). If you see any other major change please report it, it is probably a bug. The idea is to emulate the HTML5 SQL API as closely as possible. The only major change is to use window.sqlitePlugin.openDatabase() (or sqlitePlugin.openDatabase()) instead of window.openDatabase(). If you see any other major change and it is not reported under **known limitations** please report it, it is probably a bug.
## Opening a database
There are two options to open a database:
- Recommended:
var db = window.sqlitePlugin.openDatabase({name: "DB"});
- Classical:
var db = window.sqlitePlugin.openDatabase("Database", "1.0", "Demo", -1);
## Sample with PRAGMA feature ## Sample with PRAGMA feature
@ -53,7 +79,7 @@ This is a pretty strong test: first we create a table and add a single entry, th
// Cordova is ready // Cordova is ready
// //
function onDeviceReady() { function onDeviceReady() {
var db = window.sqlitePlugin.openDatabase("Database", "1.0", "Demo", -1); var db = window.sqlitePlugin.openDatabase({name: "DB"});
db.transaction(function(tx) { db.transaction(function(tx) {
tx.executeSql('DROP TABLE IF EXISTS test_table'); tx.executeSql('DROP TABLE IF EXISTS test_table');
@ -61,7 +87,7 @@ This is a pretty strong test: first we create a table and add a single entry, th
// demonstrate PRAGMA: // demonstrate PRAGMA:
db.executePragmaStatement("pragma table_info (test_table);", function(res) { db.executePragmaStatement("pragma table_info (test_table);", function(res) {
alert("PRAGMA res: " + JSON.stringify(res)); console.log("PRAGMA res: " + JSON.stringify(res));
}); });
tx.executeSql("INSERT INTO test_table (data, data_num) VALUES (?,?)", ["test", 100], function(tx, res) { tx.executeSql("INSERT INTO test_table (data, data_num) VALUES (?,?)", ["test", 100], function(tx, res) {

View File

@ -20,7 +20,7 @@
test("db transaction test", function() { test("db transaction test", function() {
var db = window.sqlitePlugin.openDatabase("Database", "1.0", "Demo", -1); var db = window.sqlitePlugin.openDatabase({name: "Database"});
ok(!!db, "db object"); ok(!!db, "db object");