Home‎ > ‎

Asynchronous Web SQL Database

posted 24 Jul 2011, 21:22 by Bevan Coleman
The 'current' database (it will be replaced) that implimented in most mobile browers is very much asynchronous. Given that this is on a JavaScript platform it makes a lot of sense for it to be this way, but it does mean that it works is a very non SQL way.
Most people are used to an atomic database such as SQL Server, MySql, SQLite, etc... were one would expect that an INSERT followed by an SELECT (for the same data) would return the newly entered data. However as the database (which is still SQLite) has been wrapped in an Javacript style asynchronous mathods you can not be sure that is the case (the INSERT may not finish untill AFTER teh select has run).
The solution to this is to use the provide 'success' callbacks on all the database methods to insure that the SELECT (in my example) is not run until after the INSERT. For simple situations this works fine (and is a fairly standard JavaScript patten). However when you have a number of SQL queries that are dependant on each other the nesting can get quite deep and it does make the code hard to read.
After a bit of looking I've remember jQuery adding a new Defered Object sub-system in v1.5. What this allows is syntax like so;
var dfd = $.Deferred().DoInsert.done(DoSelect).done(DoSomethingelse);
This allows the same effect as using nestted callbacks, but without the depth and in a mucher easier to read syntax.