2

I'm trying to write a way to synchronously execute a set of sqlite queries within a WebDB environment. I've come up with what I think amounts to a synchronous routine, but I'm not sure how to test it. Here's what I have:

var db = openDatabase("test1", "1.0", "test", 5 * 1024 * 1024);

function synchronousSql(tx, sqlStack, callback) {
    if (sqlStack.length !== 0) {
        var q = sqlStack.shift();
        console.log(+new Date() + ' ' + q);
        tx.executeSql(q, [], synchronousSql(tx, sqlStack, callback), null);
    } else {
        callback();
    }
}

var seq = [
    'drop table if exists table1',
    'drop table if exists table2',
    'drop table if exists table3',
    'drop table if exists table4',
    'drop table if exists table5',
    'create table table1(id integer, value text)',
    'create table table2(id integer, value text)',
    'create table table3(id integer, value text)',
    'create table table4(id integer, value text)',
    'create table table5(id integer, value text)',
    'drop table if exists table1',
    'drop table if exists table2',
    'drop table if exists table3',
    'drop table if exists table4',
    'drop table if exists table5'
    ];

db.transaction(function(tx) {
    synchronousSql(tx, seq, function() {
        console.log(+new Date() + ' - from synchronousSql callback');
    });
}, null, function() {
     console.log(+new Date() + ' - from transaction callback');   
});

On paper (I think) it should work. Looking at it, I think my alert message will pop up when the final statement is executed, not necessarily when it returns, but I'm not sure how to fix that. I tried specifying a callback in my arguments for the synchronousSql function but that meant I had to call it recursively with a callback, and if I used an empty anonymous function it seemed to overwrite the desired callback.

So I guess two questions: first, is this really synchronous; and second, how can I implement a final callback to run on the last callback of the stack?

edit: updated the code to a more recent version. First question still stands, though: is this truly synchronous?

tmountjr
  • 1,423
  • 2
  • 22
  • 38

1 Answers1

0

Put an alert() and a setTimeout() in your code to test whether they all execute at the same time or get separated. That's usually a pretty good way to test it. If all code gets executed at the same time (async) then you'll instantly have 10 alerts to hide - else you get one per x ms.

On your second question: just check whether the stack is empty. You'll probably have to pass a third variable through your function though. Unless you define the variable on the global scope, it won't get overwritten.

Tom van der Woerdt
  • 29,532
  • 7
  • 72
  • 105
  • This is at best a comment, no? – jcolebrand Nov 30 '11 at 22:40
  • It's the answer that can help him do the actual tests himself. I'd consider it an answer. – Tom van der Woerdt Nov 30 '11 at 22:42
  • So I took a slightly different approach: http://jsfiddle.net/tmountjr/fhkEK/10/ Each time the routine runs it logs the timestamp to the console window. The final callback (when the stack is empty) shows a later timestamp than any of the other callbacks. So...synchronous? – tmountjr Nov 30 '11 at 22:51
  • @mounty did that tell you what you intended? I know what it did, but I'm curious if you're happy with your results. ~ Basically, what happened is that each request was fired off synchronously, yes, because your code is synchronous. However, since you're not waiting on a response from the database before continuing as tho the requests were asynchronous. – jcolebrand Nov 30 '11 at 22:53
  • @jcolebrand If nothing else the second question was answered. I'm not quite sure where I would have put the `setTimeout` and `alert` statements in the first suggestion, though. I did update the example to use the second suggestion: http://jsfiddle.net/tmountjr/fhkEK/11/ – tmountjr Nov 30 '11 at 22:59
  • pretty much where you put the console.log statements to dump the timestamp. – jcolebrand Nov 30 '11 at 23:54
  • Correct - if you make each function call last 1s, you'll know whether it's doing them synchronously or not. – Tom van der Woerdt Dec 01 '11 at 10:30
  • So thanks in part to the suggestions here, I think it is running asynchronously. I even added a before and after timestamp, and a query runs only after the one preceding it has finished running. Thanks! – tmountjr Dec 01 '11 at 20:23