Skip to content

Commit

Permalink
cordova-sqlite-ext 0.10.5 - merge release
Browse files Browse the repository at this point in the history
Merge branch 'cordova-sqlite-legacy-express-core' into cordova-sqlite-ext-master

Part of openDatabase in nextTick as a hack to solve problem with
workaround solution to storesafe#666 &
pre-populated database on Windows

Some additional test & doc fixes
  • Loading branch information
Christopher J. Brody committed Jun 14, 2017
2 parents 51da4df + 8016cdd commit 80b87a8
Show file tree
Hide file tree
Showing 19 changed files with 496 additions and 275 deletions.
10 changes: 10 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# Changes

## cordova-sqlite-ext 0.10.5

- Part of openDatabase in nextTick as a hack to solve problem with workaround solution to litehelpers/Cordova-sqlite-storage#666 & pre-populated database on Windows
- Completely remove engine constraints from package.json in this version branch

###### cordova-sqlite-legacy-express-core 1.0.0

- Workaround solution to BUG litehelpers/Cordova-sqlite-storage#666 (hanging transaction in case of location reload/change)
- selfTest simulate scenario & test solution to BUG litehelpers/Cordova-sqlite-storage#666 (also includes string test and test of effects of location reload/change in this version branch, along with another internal check)

## cordova-sqlite-ext 0.10.4

### cordova-sqlite-storage 1.5.2
Expand Down
5 changes: 4 additions & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ MIT or Apache 2.0

MIT only

based on Phonegap-SQLitePlugin by @davibe (Davide Bertola <dade@dadeb.it>) and @joenoon (Joe Noon <joenoon@gmail.com>)

## Windows version

MIT or Apache 2.0
Expand Down Expand Up @@ -44,4 +46,5 @@ Based on <http://libb64.sourceforge.net/> by Chris Venter, public domain)

### SQLite3

public domain

Public domain
73 changes: 40 additions & 33 deletions README.md

Large diffs are not rendered by default.

147 changes: 123 additions & 24 deletions SQLitePlugin.coffee.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
# applications that repeatedly open and close the database.
# [BUG #210] TODO: better to abort and clean up the pending transaction state.
# XXX TBD this will be renamed and include some more per-db state.
# NOTE: In case txLocks is renamed or replaced the selfTest has to be adapted as well.
txLocks = {}

## utility functions:
Expand Down Expand Up @@ -83,7 +84,7 @@
SQLitePlugin = (openargs, openSuccess, openError) ->
# console.log "SQLitePlugin openargs: #{JSON.stringify openargs}"

# _should_ already be checked by openDatabase:
# SHOULD already be checked by openDatabase:
if !(openargs and openargs['name'])
throw newSQLError "Cannot create a SQLitePlugin db instance without a db name"

Expand Down Expand Up @@ -114,8 +115,9 @@
SQLitePlugin::databaseFeatures = isSQLitePluginDatabase: true

# Keep track of state of open db connections
# XXX TBD this will be moved and renamed or
# combined with txLocks.
# XXX FUTURE TBD this *may* be moved and renamed,
# or even combined with txLocks if possible.
# NOTE: In case txLocks is renamed or replaced the selfTest has to be adapted as well.
SQLitePlugin::openDBs = {}

SQLitePlugin::addTransaction = (t) ->
Expand All @@ -126,14 +128,17 @@
}
txLocks[@dbname].queue.push t
if @dbname of @openDBs && @openDBs[@dbname] isnt DB_STATE_INIT
# XXX TODO: only when queue has length of 1 [and test it!!]
# FUTURE TBD: rename startNextTransaction to something like
# triggerTransactionQueue
# ALT TBD: only when queue has length of 1 (and test)??
@startNextTransaction()

else
if @dbname of @openDBs
console.log 'new transaction is waiting for open operation'
else
# XXX TBD TODO: in this case (which should not happen), should abort and discard the transaction.
# XXX SHOULD NOT GET HERE.
# FUTURE TBD TODO: in this exceptional case abort and discard the transaction.
console.log 'database is closed, new transaction is [stuck] waiting until db is opened again!'
return

Expand Down Expand Up @@ -208,7 +213,11 @@
success @
return

# STOP HERE in this case:
return

else
# openDatabase part 1:
console.log 'OPEN database: ' + @dbname

opensuccesscb = =>
Expand All @@ -218,7 +227,7 @@
#if !@openDBs[@dbname] then call open error cb, and abort pending tx if any
if !@openDBs[@dbname]
console.log 'database was closed during open operation'
# XXX TODO [BUG #210] (and test!!):
# XXX TODO (WITH TEST) ref BUG litehelpers/Cordova-sqlite-storage#210:
# if !!error then error newSQLError 'database closed during open operation'
# @abortAllPendingTransactions()

Expand All @@ -243,27 +252,51 @@
# store initial DB state:
@openDBs[@dbname] = DB_STATE_INIT

# openDatabase part 2 in next tick:
nextTick =>
# As a WORKAROUND SOLUTION to BUG litehelpers/Cordova-sqlite-storage#666:
# If the database was never opened on the JavaScript side
# start an extra ROLLBACK statement to abort any pending transaction
# (does not matter whether it succeeds or fails here).
# FUTURE TBD a better solution would be to send a special signal or parameter
# if the database was never opened on the JavaScript side.
if not txLocks[@dbname]
myfn = (tx) ->
tx.addStatement 'ROLLBACK'
return
@addTransaction new SQLitePluginTransaction @, myfn, null, null, false, false

cordova.exec opensuccesscb, openerrorcb, "SQLitePlugin", "open", [ @openargs ]

return

SQLitePlugin::close = (success, error) ->
if @dbname of @openDBs
if txLocks[@dbname] && txLocks[@dbname].inProgress
# XXX TBD: wait for current tx then close (??)
# FUTURE TBD TODO ref BUG litehelpers/Cordova-sqlite-storage#210:
# Wait for current tx to finish then close,
# then abort any other pending transactions
# (and cleanup any other internal resources).
# (This would need testing!!)
console.log 'cannot close: transaction is in progress'
error newSQLError 'database cannot be closed while a transaction is in progress'
return

console.log 'CLOSE database: ' + @dbname

# XXX [BUG #209] closing one db handle disables other handles to same db
# NOTE: closing one db handle disables other handles to same db
# FUTURE TBD TODO ref litehelpers/Cordova-sqlite-storage#210:
# Add a dispose method to simply invalidate the
# current database object ("this")
delete @openDBs[@dbname]

if txLocks[@dbname] then console.log 'closing db with transaction queue length: ' + txLocks[@dbname].queue.length
else console.log 'closing db with no transaction lock state'

# XXX [BUG #210] TODO: when closing or deleting a db, abort any pending transactions [and test it!!]
# XXX TODO BUG litehelpers/Cordova-sqlite-storage#210:
# abort all pending transactions (with error callback)
# when closing a database (needs testing!!)
# (and cleanup any other internal resources)

cordova.exec success, error, "SQLitePlugin", "close", [ { path: @dbname } ]

Expand Down Expand Up @@ -656,6 +689,12 @@
new SQLitePlugin openargs, okcb, errorcb

deleteDatabase: (first, success, error) ->
# XXX TODO BUG litehelpers/Cordova-sqlite-storage#367:
# abort all pending transactions (with error callback)
# when deleting a database
# (and cleanup any other internal resources)
# NOTE: This should properly close the database
# (at least on the JavaScript side) before deleting.
args = {}

if first.constructor == String
Expand Down Expand Up @@ -695,7 +734,10 @@

args.dblocation = dblocation

# XXX [BUG #210] TODO: when closing or deleting a db, abort any pending transactions (with error callback)
# XXX TODO BUG litehelpers/Cordova-sqlite-storage#367 (repeated here):
# abort all pending transactions (with error callback)
# when deleting a database
# (and cleanup any other internal resources)
delete SQLitePlugin::openDBs[args.path]
cordova.exec success, error, "SQLitePlugin", "delete", [ args ]

Expand All @@ -706,11 +748,11 @@

start: (successcb, errorcb) ->
SQLiteFactory.deleteDatabase {name: SelfTest.DBNAME, location: 'default'},
(-> SelfTest.start2(successcb, errorcb)),
(-> SelfTest.start2(successcb, errorcb))
(-> SelfTest.step1(successcb, errorcb)),
(-> SelfTest.step1(successcb, errorcb))
return

start2: (successcb, errorcb) ->
step1: (successcb, errorcb) ->
SQLiteFactory.openDatabase {name: SelfTest.DBNAME, location: 'default'}, (db) ->
check1 = false
db.transaction (tx) ->
Expand All @@ -731,36 +773,78 @@

if resutSet.rows.item(0).upperText isnt 'TEST'
return SelfTest.finishWithError errorcb,
"Incorrect resutSet.rows.item(0).upperText value: #{resutSet.rows.item(0).data} (expected: 'TEST')"
"Incorrect resutSet.rows.item(0).upperText value: #{resutSet.rows.item(0).upperText} (expected: 'TEST')"

check1 = true
return

, (sql_err) ->
SelfTest.finishWithError errorcb, "SQL error: #{sql_err}"
return
, (ignored, tx_sql_err) ->
return SelfTest.finishWithError errorcb, "TX SQL error: #{tx_sql_err}"

, (tx_err) ->
SelfTest.finishWithError errorcb, "TRANSACTION error: #{tx_err}"
return

, (tx_err) ->
return SelfTest.finishWithError errorcb, "TRANSACTION error: #{tx_err}"

, () ->
# tx success:
if !check1
return SelfTest.finishWithError errorcb,
'Did not get expected upperText result data'

# DELETE INTERNAL STATE to simulate the effects of location refresh or change:
delete db.openDBs[SelfTest.DBNAME]
delete txLocks[SelfTest.DBNAME]
# SIMULATE SCENARIO IN BUG litehelpers/Cordova-sqlite-storage#666:
db.executeSql 'BEGIN', null, (ignored) -> nextTick -> # (nextTick needed for Windows)
# DELETE INTERNAL STATE to simulate the effects of location refresh or change:
delete db.openDBs[SelfTest.DBNAME]
delete txLocks[SelfTest.DBNAME]
nextTick ->
# VERIFY INTERNAL STATE IS DELETED:
db.transaction (tx2) ->
tx2.executeSql 'SELECT 1'
return
, (tx_err) ->
# EXPECTED RESULT:
if !tx_err
return SelfTest.finishWithError errorcb, 'Missing error object'
SelfTest.step2 successcb, errorcb
return
, () ->
# NOT EXPECTED:
return SelfTest.finishWithError errorcb, 'Missing error object'
return
return

SelfTest.start3 successcb, errorcb
return
return

, (open_err) ->
SelfTest.finishWithError errorcb, "Open database error: #{open_err}"
return

step2: (successcb, errorcb) ->
SQLiteFactory.openDatabase {name: SelfTest.DBNAME, location: 'default'}, (db) ->
# TX SHOULD SUCCEED to demonstrate solution to BUG litehelpers/Cordova-sqlite-storage#666:
db.transaction (tx) ->
tx.executeSql 'SELECT ? AS myResult', [null], (ignored, resutSet) ->
if !resutSet.rows
return SelfTest.finishWithError errorcb, 'Missing resutSet.rows'
if !resutSet.rows.length
return SelfTest.finishWithError errorcb, 'Missing resutSet.rows.length'
if resutSet.rows.length isnt 1
return SelfTest.finishWithError errorcb,
"Incorrect resutSet.rows.length value: #{resutSet.rows.length} (expected: 1)"
SelfTest.step3 successcb, errorcb
return
return
, (txError) ->
# NOT EXPECTED:
return SelfTest.finishWithError errorcb, "UNEXPECTED TRANSACTION ERROR: #{txError}"
return
, (open_err) ->
SelfTest.finishWithError errorcb, "Open database error: #{open_err}"
return

start3: (successcb, errorcb) ->
step3: (successcb, errorcb) ->
SQLiteFactory.openDatabase {name: SelfTest.DBNAME, location: 'default'}, (db) ->
db.sqlBatch [
'CREATE TABLE TestTable(id integer primary key autoincrement unique, data);'
Expand Down Expand Up @@ -877,11 +961,23 @@
# CLEANUP & FINISH:
db.close () ->
SQLiteFactory.deleteDatabase {name: SelfTest.DBNAME, location: 'default'}, successcb, (cleanup_err)->
# TBD IGNORE THIS ERROR on Windows (and WP8):
if /Windows /.test(navigator.userAgent) or /IEMobile/.test(navigator.userAgent)
console.log "IGNORE CLEANUP (DELETE) ERROR: #{JSON.stringify cleanup_err} (Windows/WP8)"
successcb()
return
SelfTest.finishWithError errorcb, "Cleanup error: #{cleanup_err}"

, (close_err) ->
# TBD IGNORE THIS ERROR on Windows (and WP8):
if /Windows /.test(navigator.userAgent) or /IEMobile/.test(navigator.userAgent)
console.log "IGNORE close ERROR: #{JSON.stringify close_err} (Windows/WP8)"
SQLiteFactory.deleteDatabase {name: SelfTest.DBNAME, location: 'default'}, successcb, successcb
return
SelfTest.finishWithError errorcb, "close error: #{close_err}"

# FUTURE TODO: return

, (select_err) ->
SelfTest.finishWithError errorcb, "SELECT error: #{select_err}"

Expand All @@ -893,8 +989,11 @@
return

finishWithError: (errorcb, message) ->
console.log "selfTest ERROR with message: #{message}"
SQLiteFactory.deleteDatabase {name: SelfTest.DBNAME, location: 'default'}, ->
errorcb newSQLError message
# FUTURE TODO: return
# FUTURE TODO log err2
, (err2)-> errorcb newSQLError "Cleanup error: #{err2} for error: #{message}"
return

Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cordova-sqlite-ext",
"version": "0.10.4",
"version": "0.10.5",
"description": "Native interface to SQLite for PhoneGap/Cordova with extra features",
"cordova": {
"id": "cordova-sqlite-ext",
Expand All @@ -10,7 +10,6 @@
"windows"
]
},
"engines": {},
"repository": {
"type": "git",
"url": "https://github.com/litehelpers/cordova-sqlite-ext.git"
Expand Down
4 changes: 2 additions & 2 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
<plugin xmlns="http://www.phonegap.com/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android"
id="cordova-sqlite-ext"
version="0.10.4">
version="0.10.5">

<name>Cordova sqlite storage plugin with extras</name>
<name>Cordova sqlite storage plugin with extra features</name>

<license>MIT</license>

Expand Down
2 changes: 0 additions & 2 deletions spec/www/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@

<!-- [Cordova] source file(s): -->
<script src="cordova.js"></script>
<!-- Needed for Cordova pre-3.0: -->
<script src="SQLitePlugin.js"></script>

<!-- spec file(s): -->
<script src="spec/browser-check-startup.js"></script>
Expand Down
4 changes: 4 additions & 0 deletions spec/www/spec/basic-db-tx-sql-storage-results.js
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,8 @@ var mytests = function() {
addColumnTest();
} else {
createdb.close(addColumnTest, function(e) {
// XXX TBD IGNORE close error on Windows:
if (isWindows) return addColumnTest();
// ERROR RESULT (NOT EXPECTED):
expect(false).toBe(true);
expect(e).toBeDefined();
Expand Down Expand Up @@ -966,6 +968,8 @@ var mytests = function() {
tableRenameTest();
} else {
createdb.close(tableRenameTest, function(e) {
// XXX TBD IGNORE close error on Windows:
if (isWindows) return tableRenameTest();
// ERROR RESULT (NOT EXPECTED):
expect(false).toBe(true);
expect(e).toBeDefined();
Expand Down
6 changes: 3 additions & 3 deletions spec/www/spec/browser-check-startup.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

var MYTIMEOUT = 12000;

var isAndroid = /Android/.test(navigator.userAgent);
var isWP8 = /IEMobile/.test(navigator.userAgent); // Matches WP(7/8/8.1)
var isWindows = /Windows /.test(navigator.userAgent); // Windows 8.1/Windows Phone 8.1/Windows 10
var isAndroid = !isWindows && /Android/.test(navigator.userAgent);
var isMac = /Macintosh/.test(navigator.userAgent);

window.hasBrowser = true;
// XXX TODO rename to something like window.hasWebKitWebSQL here and
// in actual test scripts
// XXX FUTURE TODO rename to something like window.hasWebKitWebSQL here
// and in actual test scripts
window.hasWebKitBrowser = (!isWindows && !isWP8 && !isMac && (isAndroid || !(window.webkit && window.webkit.messageHandlers)));

describe('check startup', function() {
Expand Down
Loading

0 comments on commit 80b87a8

Please sign in to comment.