Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
172 lines (128 sloc) 5.03 KB
/*
* 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) 2011 IBM Corporation
*/
requireClass ../common/Weinre
requireClass ../common/Native
requireClass ../common/IDGenerator
requireClass ./SqlStepper
//-----------------------------------------------------------------------------
class WiDatabaseImpl
if (!window.openDatabase) return
window.openDatabase = wrappedOpenDatabase
//-----------------------------------------------------------------------------
static method getDatabases
var result = []
for (var id in id2db) {
result.push(id2db[id])
}
return result
//-----------------------------------------------------------------------------
function logSqlError(sqlError)
console.log("SQL Error " + sqlError.code + ": " + sqlError.message)
//-----------------------------------------------------------------------------
function getTableNames_step_1()
this.executeSql("SELECT name FROM sqlite_master WHERE type='table'");
//-----------------------------------------------------------------------------
function getTableNames_step_2(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)
}
Weinre.WeinreTargetCommands.sendClientCallback(this.callback, [result])
//-----------------------------------------------------------------------------
method getDatabaseTableNames( databaseId, callback)
var db = dbById(databaseId)
if (!db) return
var stepper = SqlStepper([
getTableNames_step_1,
getTableNames_step_2
])
stepper.callback = callback
stepper.run(db, logSqlError)
//-----------------------------------------------------------------------------
function executeSQL_step_1()
this.executeSql(this.query);
//-----------------------------------------------------------------------------
function executeSQL_step_2(resultSet)
var columnNames = []
var values = []
var rows = resultSet.rows
for (var i=0; i<rows.length; i++) {
var row = rows.item(i)
if (i == 0) {
for (var propName in row) {
columnNames.push(propName)
}
}
for (var j=0; j<columnNames.length; j++) {
values.push(row[columnNames[j]])
}
}
Weinre.wi.DatabaseNotify.sqlTransactionSucceeded(this.txid, columnNames, values)
//-----------------------------------------------------------------------------
function executeSQL_error(sqlError)
var error = {
code: sqlError.code,
message: sqlError.message
}
Weinre.wi.DatabaseNotify.sqlTransactionFailed(this.txid, error)
//-----------------------------------------------------------------------------
method executeSQL(/*int*/ databaseId, /*string*/ query, callback)
var db = dbById(databaseId)
if (!db) return
var txid = Weinre.targetDescription.channel + "-" + IDGenerator.next()
var stepper = SqlStepper([
executeSQL_step_1,
executeSQL_step_2,
])
stepper.txid = txid
stepper.query = query
stepper.callback = callback
stepper.run(db, executeSQL_error)
if (callback) {
Weinre.WeinreTargetCommands.sendClientCallback(callback, [true, txid])
}
//-----------------------------------------------------------------------------
function wrappedOpenDatabase(name, version, displayName, estimatedSize, creationCallback)
var db = Native.openDatabase(name, version, displayName, estimatedSize, creationCallback)
dbAdd(db, name, version)
return db
//-----------------------------------------------------------------------------
init
var id2db = {}
var name2db = {}
//-----------------------------------------------------------------------------
function dbById(id)
var record = id2db[id]
if (!record) return null
return record.db
//-----------------------------------------------------------------------------
function dbRecordById(id)
return id2db[id]
//-----------------------------------------------------------------------------
function dbRecordByName(name)
return name2db[name]
//-----------------------------------------------------------------------------
function dbAdd(db, name, version)
var record = dbRecordByName(name)
if (record) return record
record = {}
record.id = IDGenerator.next()
record.domain = window.location.origin
record.name = name
record.version = version
record.db = db
id2db[record.id] = record
name2db[name] = record
var payload = {}
payload.id = record.id
payload.domain = record.domain
payload.name = name
payload.version = version
Weinre.WeinreExtraTargetEvents.databaseOpened(payload)
Jump to Line
Something went wrong with that request. Please try again.