Skip to content
This repository has been archived by the owner on Nov 16, 2021. It is now read-only.

Commit

Permalink
Merge pull request #1 from brodysoft/master
Browse files Browse the repository at this point in the history
Merge Updates from head
  • Loading branch information
RikshaDriver committed Mar 21, 2014
2 parents 427b2c7 + fbf3116 commit 1592d24
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 36 deletions.
12 changes: 4 additions & 8 deletions Lawnchair-adapter/Lawnchair-sqlitePlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,21 @@ Lawnchair.adapter('webkit-sqlite', (function () {
// public methods
return {

//valid: function() { return !!(window.openDatabase) },
//valid: function() { return !!(window.my_openDatabase) },
valid: function() { return !!(sqlitePlugin.openDatabase) },

init: function (options, callback) {
var that = this
, cb = that.fn(that.name, callback)
, dbname = options.db || this.name
, bgType = options.bgType || 1
, create = "CREATE TABLE IF NOT EXISTS " + this.name + " (id NVARCHAR(32) UNIQUE PRIMARY KEY, value TEXT, timestamp REAL)"
, win = function(){ return cb.call(that, that); }
// open a connection and create the db if it doesn't exist
//this.db = openDatabase(this.name, '1.0.0', this.name, 65536)
//this.db = my_openDatabase(this.name, '1.0.0', this.name, 65536)
//this.db = window.my_openDatabase("Database", "1.0", "PhoneGap Demo", 200000);
this.db = sqlitePlugin.openDatabase(this.name, '1.0.0', this.name, 65536)
//this.db = sqlitePlugin.openDatabase("Database", "1.0", "PhoneGap Demo", 200000);
this.db = sqlitePlugin.openDatabase({name:dbname,bgType:bgType})
this.db.transaction(function (t) {
t.executeSql(create, [], win, fail)
})
},
},

keys: function (callback) {
var cb = this.lambda(callback)
Expand Down
12 changes: 4 additions & 8 deletions Lawnchair-adapter/test-www/Lawnchair-sqlitePlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,21 @@ Lawnchair.adapter('webkit-sqlite', (function () {
// public methods
return {

//valid: function() { return !!(window.openDatabase) },
//valid: function() { return !!(window.my_openDatabase) },
valid: function() { return !!(sqlitePlugin.openDatabase) },

init: function (options, callback) {
var that = this
, cb = that.fn(that.name, callback)
, dbname = options.db || this.name
, bgType = options.bgType || 1
, create = "CREATE TABLE IF NOT EXISTS " + this.name + " (id NVARCHAR(32) UNIQUE PRIMARY KEY, value TEXT, timestamp REAL)"
, win = function(){ return cb.call(that, that); }
// open a connection and create the db if it doesn't exist
//this.db = openDatabase(this.name, '1.0.0', this.name, 65536)
//this.db = my_openDatabase(this.name, '1.0.0', this.name, 65536)
//this.db = window.my_openDatabase("Database", "1.0", "PhoneGap Demo", 200000);
this.db = sqlitePlugin.openDatabase(this.name, '1.0.0', this.name, 65536)
//this.db = sqlitePlugin.openDatabase("Database", "1.0", "PhoneGap Demo", 200000);
this.db = sqlitePlugin.openDatabase({name:dbname,bgType:bgType})
this.db.transaction(function (t) {
t.executeSql(create, [], win, fail)
})
},
},

keys: function (callback) {
var cb = this.lambda(callback)
Expand Down
49 changes: 31 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Cordova/PhoneGap SQLitePlugin
# Cordova/PhoneGap SQLitePlugin - a reference implementation

Native interface to sqlite in a Cordova/PhoneGap plugin for Android & iOS, working to follow the HTML5 Web SQL API as close as possible.

Expand All @@ -8,21 +8,22 @@ License for iOS version: MIT only

## Status

- Android & iOS versions are working with Cordova 3.0 tooling
- New feature development is on hold until the version for Windows Phone 8 is integrated and working with fail-safe nested transactions
- Please use the [Cordova-SQLitePlugin forum](http://groups.google.com/group/Cordova-SQLitePlugin) for community support
- Commercial support is available for SQLCipher integration

## Announcements

- New location: https://github.com/lite4cordova/Cordova-SQLitePlugin
- Forum renamed to: [lite4cordova forum](http://groups.google.com/group/lite4cordova)
- Forum renamed to: [Cordova-SQLitePlugin forum](http://groups.google.com/group/Cordova-SQLitePlugin)
- New location: https://github.com/brodysoft/Cordova-SQLitePlugin
- Android & iOS versions are combined again in this project.
- Android & iOS versions are working with Cordova 3.0 tooling.
- iOS version can now be built with either ARC or MRC.
- WP8 version working with Cordova 3.0 tooling at: [lite4cordova / Cordova-SQLitePlugin-WP-2013.12](https://github.com/lite4cordova/Cordova-SQLitePlugin-WP-2013.12)
- WP8 version working with Cordova 3.0 tooling at: [brodybits / Cordova-SQLitePlugin-WP-2014.01](https://github.com/brodybits/Cordova-SQLitePlugin-WP-2014.01)

## Highlights

- Drop-in replacement for HTML5 SQL API, the only change is window.openDatabase() --> sqlitePlugin.openDatabase()
- Works with Cordova 3.0 tooling
- Drop-in replacement for HTML5 SQL API, the only change should be window.openDatabase() --> sqlitePlugin.openDatabase()
- Fail-safe nested transactions with batch processing optimizations
- As described in [this posting](http://brodyspark.blogspot.com/2012/12/cordovaphonegap-sqlite-plugins-offer.html):
- Keeps sqlite database in a user data location that is known, can be reconfigured, and iOS will be backed up by iCloud.
Expand All @@ -34,21 +35,30 @@ License for iOS version: MIT only
- [Get It Done app](http://getitdoneapp.com/) by [marcucio.com](http://marcucio.com/)
- [Larkwire](http://www.larkwire.com/) (iOS version): Learn bird songs the fun way

## Known limitations
## Known issues

- Problem with Android 4.4.
- Issue buiding with Android SDK < 16
- For iOS version: There is a memory leak if you use this version with background processing disabled. As a workaround, the iOS version has background processing enabled by default.

## Other limitations

- 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 described in **Usage**.
- For Android version, there is an issue with background processing that affects transaction error handling and may affect nested transactions.
- For Android below SDK 11:
- the data that is stored is of type 'TEXT' regardless of the schema
- `rowsAffected` is not returned for INSERT or DELETE statement
- For iOS version: There is a memory leak if you use this version with background processing disabled. A solution is planned very soon. As a workaround, the iOS version has background processing enabled by default.
- Background processing model could be improved with one background thread per database connection.
- For iOS, iCloud backup is NOT optional and should be.
- Missing db creation callback

## Other versions

- Pre-populated database support for Android & iOS: https://github.com/RikshaDriver/Cordova-PrePopulated-SQLitePlugin
- Windows Phone 8+ version: https://github.com/marcucio/Cordova-WP-SqlitePlugin
- iOS enhancements, with extra fixes for console log messages: https://github.com/mineshaftgap/Cordova-SQLitePlugin
- Original iOS version with a different API: https://github.com/davibe/Phonegap-SQLitePlugin
- Original version for iOS, with a different API: https://github.com/davibe/Phonegap-SQLitePlugin

## Using with SQLCipher

Expand Down Expand Up @@ -295,7 +305,7 @@ Make a change like this to index.html (or use the sample code) verify proper ins

# Support

## Logging issues
## Reporting issues

If you have an issue with the plugin please check the following first:
- You are using the latest version of the Plugin Javascript & platform-specific Java or Objective-C source from this repository.
Expand All @@ -309,13 +319,11 @@ If you still cannot get something to work:
- if the issue is with *adding* data to a table, that the test program includes the statements you used to open the database and create the table;
- if the issue is with *retrieving* data from a table, that the test program includes the statements you used to open the database, create the table, and enter the data you are trying to retrieve.

Then you can log the issue at [lite4cordova / Cordova-SQLitePlugin / issues](https://github.com/lite4cordova/Cordova-SQLitePlugin/issues) or post it to the [lite4cordova forum](http://groups.google.com/group/lite4cordova).

**NOTE:** If you have any questions please use the community forum as described below.
Then you can post the issue to the [Cordova-SQLitePlugin forum](http://groups.google.com/group/Cordova-SQLitePlugin).

## Community forum

If you have any questions about the plugin please post it to the [lite4cordova forum](http://groups.google.com/group/lite4cordova).
If you have any questions about the plugin please post it to the [Cordova-SQLitePlugin forum](http://groups.google.com/group/Cordova-SQLitePlugin).

## Support priorities

Expand All @@ -328,7 +336,7 @@ If you have any questions about the plugin please post it to the [lite4cordova f

## Professional support

Coming soon
Available for integration with SQLCipher.

# Unit test(s)

Expand Down Expand Up @@ -364,13 +372,18 @@ In this example, you would be using/creating the database at: *Documents/kvstore

Using the `db` option you can create multiple stores in one sqlite file. (There will be one table per store.)

recipes = new Lawnchair {db: "cookbook", name: "recipes", ...}
recipes = new Lawnchair {db: "cookbook", name: "recipes", ...}
ingredients = new Lawnchair {db: "cookbook", name: "ingredients", ...}

It also supports bgType argument:

users = new Lawnchair {name: "users", bgType: 1, ...}


# Contributing

- Testimonials of apps that are using this plugin would be especially helpful.
- Reporting issues to the [lite4cordova forum](http://groups.google.com/group/lite4cordova) can help improve the quality of this plugin.
- Reporting issues to the [Cordova-SQLitePlugin forum](http://groups.google.com/group/Cordova-SQLitePlugin) can help improve the quality of this plugin.
- Patches with bug fixes are helpful, especially when submitted with test code.
- Other enhancements are very welcome, especially with test code. Increase of complexity should be avoided.
- All contributions may be reused by [@brodybits (Chris Brody)](https://github.com/brodybits) under another license in the future. Efforts will be taken to give credit for major contributions but it will not be guaranteed.
Expand Down
22 changes: 21 additions & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,26 @@
<clobbers target="SQLitePlugin" />
</js-module>

<!-- amazon-fireos -->
<platform name="amazon-fireos">
<config-file target="AndroidManifest.xml" parent="/manifest/application">
<activity android:name="org.pgsqlite.SQLitePlugin"
android:label="@string/app_name">
<intent-filter>
</intent-filter>
</activity>
</config-file>

<!-- Cordova >= 3.0.0 -->
<config-file target="res/xml/config.xml" parent="/*">
<feature name="SQLitePlugin">
<param name="android-package" value="org.pgsqlite.SQLitePlugin"/>
</feature>
</config-file>

<source-file src="src/android/org/pgsqlite/SQLitePlugin.java" target-dir="src/org/pgsqlite"/>
</platform>

<!-- android -->
<platform name="android">
<config-file target="AndroidManifest.xml" parent="/manifest/application">
Expand All @@ -24,7 +44,7 @@
</config-file>

<!-- Cordova >= 3.0.0 -->
<config-file target="config.xml" parent="/*">
<config-file target="res/xml/config.xml" parent="/*">
<feature name="SQLitePlugin">
<param name="android-package" value="org.pgsqlite.SQLitePlugin"/>
</feature>
Expand Down
4 changes: 4 additions & 0 deletions src/android/org/pgsqlite/SQLitePlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,10 @@ private void openDatabase(String dbname, String password)

if(!dbfile.exists()) copyPrepopulatedDatabase(completeDBName, dbfile);

if (!dbfile.exists()) {
dbfile.getParentFile().mkdirs();
}

Log.v("info", "Open sqlite db: " + dbfile.getAbsolutePath());

SQLiteDatabase mydb = SQLiteDatabase.openOrCreateDatabase(dbfile, null);
Expand Down
29 changes: 28 additions & 1 deletion src/ios/SQLitePlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
*/

#import "SQLitePlugin.h"
#include <regex.h>


//LIBB64
typedef enum
Expand Down Expand Up @@ -129,6 +131,29 @@ static int base64_encode_blockend(char* code_out,

//LIBB64---END

static void sqlite_regexp(sqlite3_context* context, int argc, sqlite3_value** values) {
int ret;
regex_t regex;
char* reg = (char*)sqlite3_value_text(values[0]);
char* text = (char*)sqlite3_value_text(values[1]);

if ( argc != 2 || reg == 0 || text == 0) {
sqlite3_result_error(context, "SQL function regexp() called with invalid arguments.\n", -1);
return;
}

ret = regcomp(&regex, reg, REG_EXTENDED | REG_NOSUB);
if ( ret != 0 ) {
sqlite3_result_error(context, "error compiling regular expression", -1);
return;
}

ret = regexec(&regex, text , 0, NULL, 0);
regfree(&regex);

sqlite3_result_int(context, (ret != REG_NOMATCH));
}


@implementation SQLitePlugin

Expand Down Expand Up @@ -192,9 +217,11 @@ -(void)open: (CDVInvokedUrlCommand*)command
}
else {
// Extra for SQLCipher:
// const char *key = [[options objectForKey:@"key"] UTF8String];
// const char *key = [@"your_key_here" UTF8String];
// if(key != NULL) sqlite3_key(db, key, strlen(key));

sqlite3_create_function(db, "regexp", 2, SQLITE_ANY, NULL, &sqlite_regexp, NULL, NULL);

// Attempt to read the SQLite master table (test for SQLCipher version):
if(sqlite3_exec(db, (const char*)"SELECT count(*) FROM sqlite_master;", NULL, NULL, NULL) == SQLITE_OK) {
dbPointer = [NSValue valueWithPointer:db];
Expand Down

0 comments on commit 1592d24

Please sign in to comment.