Skip to content

Commit

Permalink
(#54) - Implement db.readTransaction (in coffeescript)
Browse files Browse the repository at this point in the history
  • Loading branch information
nolanlawson committed Jul 7, 2014
1 parent b5a2e76 commit 0c6b65b
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 25 deletions.
24 changes: 20 additions & 4 deletions SQLitePlugin.coffee.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ License for common Javascript: MIT or Apache
root = @
- global constants:
READ_ONLY_REGEX = /^\s*(?:drop|delete|insert|update|create)\s/i
IOS_REGEX = /iP(?:ad|hone|od)/
- SQLitePlugin object is defined by a constructor function and prototype member functions:
SQLitePlugin = (openargs, openSuccess, openError) ->
Expand Down Expand Up @@ -40,7 +45,7 @@ License for common Javascript: MIT or Apache
@bg =
if !openargs.bgType
# default to true for iOS only (due to memory issue)
((navigator.userAgent.match(/iPad/i)) || (navigator.userAgent.match(/iPhone/i)))
IOS_REGEX.test(navigator.userAgent)
else
openargs.bgType == 1
Expand All @@ -59,7 +64,11 @@ License for common Javascript: MIT or Apache
return
SQLitePlugin::transaction = (fn, error, success) ->
@addTransaction new SQLitePluginTransaction(this, fn, error, success, true)
@addTransaction new SQLitePluginTransaction(this, fn, error, success, true, false)
return
SQLitePlugin::readTransaction = (fn, error, success) ->
@addTransaction new SQLitePluginTransaction(this, fn, error, success, true, true)
return
SQLitePlugin::startNextTransaction = ->
Expand Down Expand Up @@ -93,7 +102,7 @@ License for common Javascript: MIT or Apache
tx.executeSql(statement, params, mysuccess, myerror)
return
@addTransaction new SQLitePluginTransaction(this, myfn, myerror, mysuccess, false)
@addTransaction new SQLitePluginTransaction(this, myfn, myerror, mysuccess, false, false)
return
- Deprecated pragma API, use db.executeSql() instead:
Expand Down Expand Up @@ -128,7 +137,7 @@ License for common Javascript: MIT or Apache
###
Transaction batching object:
###
SQLitePluginTransaction = (db, fn, error, success, txlock) ->
SQLitePluginTransaction = (db, fn, error, success, txlock, readOnly) ->
if typeof(fn) != "function"
###
This is consistent with the implementation in Chrome -- it
Expand All @@ -143,6 +152,7 @@ License for common Javascript: MIT or Apache
@error = error
@success = success
@txlock = txlock
@readOnly = readOnly
@executes = []
if txlock
Expand All @@ -168,6 +178,12 @@ License for common Javascript: MIT or Apache
return
SQLitePluginTransaction::executeSql = (sql, values, success, error) ->
if @readOnly && READ_ONLY_REGEX.test(sql)
@handleStatementFailure(error, {message: 'invalid sql for a read-only transaction'})
return

qid = @executes.length
@executes.push
Expand Down
64 changes: 64 additions & 0 deletions test-www/www/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,70 @@
});
});

test(suiteName + "readTransaction should throw on modification", function() {
stop();
var db = openDatabase("Database-readonly", "1.0", "Demo", DEFAULT_SIZE);
db.transaction(function(tx) {
tx.executeSql('DROP TABLE IF EXISTS test_table');
tx.executeSql('CREATE TABLE IF NOT EXISTS test_table (foo text)');
tx.executeSql('INSERT INTO test_table VALUES ("bar")');
}, function () {}, function () {
db.readTransaction(function (tx) {
tx.executeSql('SELECT * from test_table', [], function (tx, res) {
equal(res.rows.length, 1);
equal(res.rows.item(0).foo, 'bar');
});
}, function () {}, function () {
var tasks;
var numDone = 0;
var failed = false;
function checkDone() {
if (++numDone === tasks.length) {
start();
}
}
function fail() {
if (!failed) {
failed = true;
start();
ok(false, 'readTransaction was supposed to fail');
}
}
// all of these should throw an error
tasks = [
function () {
db.readTransaction(function (tx) {
tx.executeSql('DELETE from test_table');
}, checkDone, fail);
},
function () {
db.readTransaction(function (tx) {
tx.executeSql('UPDATE test_table SET foo = "baz"');
}, checkDone, fail);
},
function () {
db.readTransaction(function (tx) {
tx.executeSql('INSERT INTO test_table VALUES ("baz")');
}, checkDone, fail);
},
function () {
db.readTransaction(function (tx) {
tx.executeSql('DROP TABLE test_table');
}, checkDone, fail);
},
function () {
db.readTransaction(function (tx) {
tx.executeSql('CREATE TABLE test_table2');
}, checkDone, fail);
}
];
for (var i = 0; i < tasks.length; i++) {
tasks[i]();
}
});
});
});

/**
test(suiteName + "PRAGMA & multiple databases", function() {
var db = openDatabase("DB1", "1.0", "Demo", DEFAULT_SIZE);
Expand Down
58 changes: 37 additions & 21 deletions www/SQLitePlugin.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0c6b65b

Please sign in to comment.