| |
| /* |
| * weinre is available under *either* the terms of the modified BSD license *or* the |
| * MIT License (2008). See http://opensource.org/licenses/alphabetical for full text. |
| * |
| * Copyright (c) 2010, 2011 IBM Corporation |
| */ |
| |
| requireClass ../common/Binding |
| |
| //----------------------------------------------------------------------------- |
| class SqlStepper(steps) |
| if (!(this instanceof SqlStepper)) return new SqlStepper(steps) |
| |
| this.__context = {} |
| |
| var context = this.__context |
| context.steps = steps |
| |
| //----------------------------------------------------------------------------- |
| method run(db, errorCallback) |
| var context = this.__context |
| |
| if (context.hasBeenRun) |
| throw new Ex(arguments, "stepper has already been run") |
| |
| context.hasBeenRun = true |
| |
| context.db = db |
| context.errorCallback = errorCallback |
| context.nextStep = 0 |
| |
| context.ourErrorCallback = new Binding(this, ourErrorCallback) |
| context.runStep = new Binding(this, runStep) |
| this.executeSql = new Binding(this, executeSql) |
| |
| db.transaction(context.runStep) |
| |
| //----------------------------------------------------------------------------- |
| function executeSql(statement, data) |
| var context = this.__context |
| |
| context.tx.executeSql(statement, data, context.runStep, context.ourErrorCallback) |
| |
| //----------------------------------------------------------------------------- |
| function ourErrorCallback(tx, sqlError) |
| var context = this.__context |
| |
| context.errorCallback.call(this, sqlError) |
| |
| //----------------------------------------------------------------------------- |
| function runStep(tx, resultSet) |
| var context = this.__context |
| |
| if (context.nextStep >= context.steps.length) return |
| |
| context.tx = tx |
| |
| context.currentStep = context.nextStep |
| context.nextStep++ |
| |
| var step = context.steps[context.currentStep] |
| |
| step.call(this, resultSet) |
| |
| //----------------------------------------------------------------------------- |
| static method example(db, id) |
| function step1() { |
| this.executeSql("SELECT name FROM sqlite_master WHERE type='table'") |
| } |
| |
| function step2(resultSet) { |
| var rows = resultSet.rows |
| var result = [] |
| for (var i=0; i<rows.length; i++) { |
| var name = rows.item(i).name |
| if (name == "__WebKitDatabaseInfoTable__") continue |
| |
| result.push(name) |
| } |
| |
| console.log("[" + this.id + "] table names: " + result.join(", ")) |
| } |
| |
| function errorCb(sqlError) { |
| |
| console.log("[" + this.id + "] sql error:" + sqlError.code + ": " + sqlError.message) |
| } |
| |
| var stepper = new SqlStepper([step1, step2]) |
| stepper.id = id |
| stepper.run(db, errorCb) |