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

Change of PhoneGap SQLitePlugin API to be closer to the W3 SQL API #24

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ all: coffee

coffee:
mkdir -p build
coffee -p src/pgsqlite_plugin.js.coffee > build/pgsqlite_plugin.js
coffee -p src/lawnchair_pgsqlite_plugin_adapter.js.coffee > build/lawnchair_pgsqlite_plugin_adapter.js
coffee -p src/pgsqlite_plugin.coffee > build/pgsqlite_plugin.js
coffee -p src/lawnchair_pgsqlite_plugin_adapter.coffee > build/lawnchair_pgsqlite_plugin_adapter.js

clean:
rm -f build/pgsqlite_plugin.js
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,10 @@ General Usage
db.executeSql('DROP TABLE IF EXISTS test_table');
db.executeSql('CREATE TABLE IF NOT EXISTS test_table (id integer primary key, data text, data_num integer)');
db.transaction(function(tx) {
return tx.executeSql(["INSERT INTO test_table (data, data_num) VALUES (?,?)", "test", 100], function(res) {
return tx.executeSql("INSERT INTO test_table (data, data_num) VALUES (?,?)", ["test", 100], function(res) {
console.log("insertId: " + res.insertId + " -- probably 1");
console.log("rowsAffected: " + res.rowsAffected + " -- should be 1");
return db.executeSql("select count(id) as cnt from test_table;", function(res) {
return db.executeSql("select count(id) as cnt from test_table;", [], function(res) {
console.log("rows.length: " + res.rows.length + " -- should be 1");
return console.log("rows[0].cnt: " + res.rows[0].cnt + " -- should be 1");
});
Expand Down
21 changes: 10 additions & 11 deletions build/lawnchair_pgsqlite_plugin_adapter.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

/*
PGSQLitePlugin Lawnchair Adapter
(c) 2011 Joe Noon <[email protected]>
Expand Down Expand Up @@ -32,7 +31,7 @@
};
db = options.db || this.name;
this.db = new PGSQLitePlugin("" + db + ".sqlite3");
this.db.executeSql(sql, success, fail);
this.db.executeSql(sql, [], success, fail);
},
keys: function(callback) {
var cb, sql, success, that;
Expand All @@ -42,7 +41,7 @@
success = function(res) {
cb.call(that, res.rows);
};
this.db.executeSql(sql, success, fail);
this.db.executeSql(sql, [], success, fail);
return this;
},
save: function(obj, callback) {
Expand All @@ -62,7 +61,7 @@
delete obj.key;
val.unshift(JSON.stringify(obj));
sql = exists ? up : ins;
db.executeSql([sql].concat(val), success, fail);
db.executeSql(sql, val, success, fail);
});
return this;
},
Expand Down Expand Up @@ -117,7 +116,7 @@
sql = obj.key in ids_hash ? up : ins;
delete obj.key;
val.unshift(JSON.stringify(obj));
t.executeSql([sql].concat(val), success, fail);
t.executeSql(sql, val, success, fail);
};
for (_k = 0, _len3 = objs.length; _k < _len3; _k++) {
obj = objs[_k];
Expand All @@ -132,7 +131,7 @@
};
if (keys.length > 0) {
exists_sql = ["SELECT id FROM " + this.name + " WHERE id IN (" + marks + ")"].concat(keys);
db.executeSql(exists_sql, exists_success);
db.executeSql(exists_sql, [], exists_success);
} else {
exists_success({
rows: []
Expand Down Expand Up @@ -180,7 +179,7 @@
if (!is_array) r = r[0];
if (cb) that.lambda(cb).call(that, r);
};
this.db.executeSql(sql, success, fail);
this.db.executeSql(sql, [], success, fail);
return this;
},
exists: function(key, cb) {
Expand All @@ -190,7 +189,7 @@
success = function(res) {
if (cb) that.fn("exists", cb).call(that, res.rows.length > 0);
};
this.db.executeSql(sql, success, fail);
this.db.executeSql(sql, [], success, fail);
return this;
},
all: function(callback) {
Expand Down Expand Up @@ -218,7 +217,7 @@
})();
cb.call(that, r);
};
this.db.executeSql(sql, success, fail);
this.db.executeSql(sql, [], success, fail);
return this;
},
remove: function(keyOrObj, cb) {
Expand All @@ -231,7 +230,7 @@
success = function() {
if (cb) that.lambda(cb).call(that);
};
this.db.executeSql(sql, success, fail);
this.db.executeSql(sql, [], success, fail);
return this;
},
nuke: function(cb) {
Expand All @@ -243,7 +242,7 @@
if (cb) that.lambda(cb).call(that);
db.executeSql("VACUUM");
};
this.db.executeSql(sql, success, fail);
this.db.executeSql(sql, [], success, fail);
return this;
}
};
Expand Down
8 changes: 4 additions & 4 deletions build/pgsqlite_plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@
delete callbacks[ref];
};

PGSQLitePlugin.prototype.executeSql = function(sql, success, error) {
PGSQLitePlugin.prototype.executeSql = function(sql, params, success, error) {
var opts;
if (!sql) throw new Error("Cannot executeSql without a query");
opts = getOptions({
query: [].concat(sql || []),
query: [sql].concat(params || []),
path: this.dbPath
}, success, error);
PhoneGap.exec("PGSQLitePlugin.backgroundExecuteSql", opts);
Expand Down Expand Up @@ -109,9 +109,9 @@
this.executes = [];
}

PGSQLitePluginTransaction.prototype.executeSql = function(sql, success, error) {
PGSQLitePluginTransaction.prototype.executeSql = function(sql, params, success, error) {
this.executes.push(getOptions({
query: [].concat(sql || []),
query: [sql].concat(params || []),
path: this.dbPath
}, success, error));
};
Expand Down
20 changes: 10 additions & 10 deletions src/lawnchair_pgsqlite_plugin_adapter.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pgsqlite_plugin =
# open a connection and create the db if it doesn't exist
db = options.db || @name
@db = new PGSQLitePlugin("#{db}.sqlite3")
@db.executeSql sql, success, fail
@db.executeSql sql, [], success, fail
return

keys: (callback) ->
Expand All @@ -36,7 +36,7 @@ pgsqlite_plugin =
success = (res) ->
cb.call(that, res.rows)
return
@db.executeSql sql, success, fail
@db.executeSql sql, [], success, fail
this

save: (obj, callback) ->
Expand All @@ -55,7 +55,7 @@ pgsqlite_plugin =
delete obj.key
val.unshift(JSON.stringify(obj))
sql = if exists then up else ins
db.executeSql [ sql ].concat(val), success, fail
db.executeSql sql, val, success, fail
return
this

Expand Down Expand Up @@ -111,7 +111,7 @@ pgsqlite_plugin =
sql = if obj.key of ids_hash then up else ins
delete obj.key
val.unshift(JSON.stringify(obj))
t.executeSql [ sql ].concat(val), success, fail
t.executeSql sql, val, success, fail
return
return

Expand All @@ -126,7 +126,7 @@ pgsqlite_plugin =
if keys.length > 0
# the case where there is at least one object with an existing key
exists_sql = [ "SELECT id FROM #{@name} WHERE id IN (#{marks})" ].concat(keys)
db.executeSql exists_sql, exists_success
db.executeSql exists_sql, [], exists_success
else
# the case where every object is new, so we don't need to do a select query first
exists_success({ rows: [] })
Expand Down Expand Up @@ -156,7 +156,7 @@ pgsqlite_plugin =
that.lambda(cb).call(that, r) if cb
return

@db.executeSql sql, success, fail
@db.executeSql sql, [], success, fail
this

exists: (key, cb) ->
Expand All @@ -165,7 +165,7 @@ pgsqlite_plugin =
success = (res) ->
that.fn("exists", cb).call(that, res.rows.length > 0) if cb
return
@db.executeSql sql, success, fail
@db.executeSql sql, [], success, fail
this

all: (callback) ->
Expand All @@ -181,7 +181,7 @@ pgsqlite_plugin =
obj
cb.call(that, r)
return
@db.executeSql sql, success, fail
@db.executeSql sql, [], success, fail
this

remove: (keyOrObj, cb) ->
Expand All @@ -193,7 +193,7 @@ pgsqlite_plugin =
success = () ->
that.lambda(cb).call(that) if cb
return
@db.executeSql sql, success, fail
@db.executeSql sql, [], success, fail
this

nuke: (cb) ->
Expand All @@ -205,7 +205,7 @@ pgsqlite_plugin =
# clean up the db
db.executeSql "VACUUM"
return
@db.executeSql sql, success, fail
@db.executeSql sql, [], success, fail
this

PGSQLitePlugin.lawnchair_adapter = pgsqlite_plugin
Expand Down
8 changes: 4 additions & 4 deletions src/pgsqlite_plugin.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ class root.PGSQLitePlugin
delete callbacks[ref]
return

executeSql: (sql, success, error) ->
executeSql: (sql, params, success, error) ->
throw new Error "Cannot executeSql without a query" unless sql
opts = getOptions({ query: [].concat(sql || []), path: @dbPath }, success, error)
opts = getOptions({ query: [sql].concat(params || []), path: @dbPath }, success, error)
PhoneGap.exec("PGSQLitePlugin.backgroundExecuteSql", opts)
return

Expand Down Expand Up @@ -78,8 +78,8 @@ class root.PGSQLitePluginTransaction
constructor: (@dbPath) ->
@executes = []

executeSql: (sql, success, error) ->
@executes.push getOptions({ query: [].concat(sql || []), path: @dbPath }, success, error)
executeSql: (sql, params, success, error) ->
@executes.push getOptions({ query: [sql].concat(params || []), path: @dbPath }, success, error)
return

complete: (success, error) ->
Expand Down