Path: blob/trunk/javascript/atoms/html5/database.js
2884 views
// Licensed to the Software Freedom Conservancy (SFC) under one1// or more contributor license agreements. See the NOTICE file2// distributed with this work for additional information3// regarding copyright ownership. The SFC licenses this file4// to you under the Apache License, Version 2.0 (the5// "License"); you may not use this file except in compliance6// with the License. You may obtain a copy of the License at7//8// http://www.apache.org/licenses/LICENSE-2.09//10// Unless required by applicable law or agreed to in writing,11// software distributed under the License is distributed on an12// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY13// KIND, either express or implied. See the License for the14// specific language governing permissions and limitations15// under the License.1617/**18* @fileoverview Atoms for executing SQL queries on web client database.19*20*/2122goog.provide('bot.storage.database');23goog.provide('bot.storage.database.ResultSet');2425goog.require('bot');26goog.require('bot.Error');27goog.require('bot.ErrorCode');282930/**31* Opens the database to access its contents. This function will create the32* database if it does not exist. For details,33* @see http://www.w3.org/TR/webdatabase/#databases34*35* @param {string} databaseName The name of the database.36* @param {string=} opt_version The expected database version to be opened;37* defaults to the empty string.38* @param {string=} opt_displayName The name to be displayed to the user;39* defaults to the databaseName.40* @param {number=} opt_size The estimated initial quota size of the database;41* default value is 5MB.42* @param {!Window=} opt_window The window associated with the database;43* defaults to the main window.44* @return {!Database} The object to access the web database.45*46*/47bot.storage.database.openOrCreate = function(databaseName, opt_version,48opt_displayName, opt_size, opt_window) {49var version = opt_version || '';50var displayName = opt_displayName || (databaseName + 'name');51var size = opt_size || 5 * 1024 * 1024;52var win = opt_window || bot.getWindow();5354return win.openDatabase(databaseName, version, displayName, size);55};565758/**59* It executes a single SQL query on a given web database storage.60*61* @param {string} databaseName The name of the database.62* @param {string} query The SQL statement.63* @param {!Array.<*>} args Arguments needed for the SQL statement.64* @param {!function(!SQLTransaction, !bot.storage.database.ResultSet)}65* queryResultCallback Callback function to be invoked on successful query66* statement execution.67* @param {!function(!SQLError)} txErrorCallback68* Callback function to be invoked on transaction (commit) failure.69* @param {!function()=} opt_txSuccessCallback70* Callback function to be invoked on successful transaction execution.71* @param {function(!SQLTransaction, !SQLError)=} opt_queryErrorCallback72* Callback function to be invoked on successful query statement execution.73* @see http://www.w3.org/TR/webdatabase/#executing-sql-statements74*/75bot.storage.database.executeSql = function(databaseName, query, args,76queryResultCallback, txErrorCallback, opt_txSuccessCallback,77opt_queryErrorCallback) {7879var db;8081try {82db = bot.storage.database.openOrCreate(databaseName);83} catch (e) {84throw new bot.Error(bot.ErrorCode.UNKNOWN_ERROR, e.message);85}8687var queryCallback = function(tx, result) {88var wrappedResult = new bot.storage.database.ResultSet(result);89queryResultCallback(tx, wrappedResult);90};9192var transactionCallback = function(tx) {93tx.executeSql(query, args, queryCallback, opt_queryErrorCallback);94};9596db.transaction(transactionCallback, txErrorCallback,97opt_txSuccessCallback);98};99100101102/**103* A wrapper of the SQLResultSet object returned by the SQL statement.104*105* @param {!SQLResultSet} sqlResultSet The original SQLResultSet object.106* @constructor107*/108bot.storage.database.ResultSet = function(sqlResultSet) {109110/**111* The database rows returned from the SQL query.112* @type {!Array.<*>}113*/114this.rows = [];115for (var i = 0; i < sqlResultSet.rows.length; i++) {116this.rows[i] = sqlResultSet.rows.item(i);117}118119/**120* The number of rows that were changed by the SQL statement121* @type {number}122*/123this.rowsAffected = sqlResultSet.rowsAffected;124125/**126* The row ID of the row that the SQLResultSet object's SQL statement127* inserted into the database, if the statement inserted a row; else128* it is assigned to -1. Originally, accessing insertId attribute of129* a SQLResultSet object returns the exception INVALID_ACCESS_ERR130* if no rows are inserted.131* @type {number}132*/133this.insertId = -1;134try {135this.insertId = sqlResultSet.insertId;136} catch (error) {137// If accessing sqlResultSet.insertId results in INVALID_ACCESS_ERR138// exception, this.insertId will be assigned to -1.139}140};141142143