Path: blob/trunk/third_party/closure/goog/db/indexeddb.js
2868 views
// Copyright 2011 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314/**15* @fileoverview Wrapper for an IndexedDB database.16*17*/181920goog.provide('goog.db.IndexedDb');2122goog.require('goog.db.Error');23goog.require('goog.db.ObjectStore');24goog.require('goog.db.Transaction');25goog.require('goog.events.Event');26goog.require('goog.events.EventHandler');27goog.require('goog.events.EventTarget');28293031/**32* Creates an IDBDatabase wrapper object. The database object has methods for33* setting the version to change the structure of the database and for creating34* transactions to get or modify the stored records. Should not be created35* directly, call {@link goog.db.openDatabase} to set up the connection.36*37* @param {!IDBDatabase} db Underlying IndexedDB database object.38* @constructor39* @extends {goog.events.EventTarget}40* @final41*/42goog.db.IndexedDb = function(db) {43goog.db.IndexedDb.base(this, 'constructor');4445/**46* Underlying IndexedDB database object.47*48* @type {!IDBDatabase}49* @private50*/51this.db_ = db;5253/**54* Internal event handler that listens to IDBDatabase events.55* @type {!goog.events.EventHandler<!goog.db.IndexedDb>}56* @private57*/58this.eventHandler_ = new goog.events.EventHandler(this);5960this.eventHandler_.listen(61this.db_, goog.db.IndexedDb.EventType.ABORT,62goog.bind(this.dispatchEvent, this, goog.db.IndexedDb.EventType.ABORT));63this.eventHandler_.listen(64this.db_, goog.db.IndexedDb.EventType.ERROR, this.dispatchError_);65this.eventHandler_.listen(66this.db_, goog.db.IndexedDb.EventType.VERSION_CHANGE,67this.dispatchVersionChange_);68this.eventHandler_.listen(69this.db_, goog.db.IndexedDb.EventType.CLOSE,70goog.bind(this.dispatchEvent, this, goog.db.IndexedDb.EventType.CLOSE));71};72goog.inherits(goog.db.IndexedDb, goog.events.EventTarget);737475/**76* True iff the database connection is open.77*78* @type {boolean}79* @private80*/81goog.db.IndexedDb.prototype.open_ = true;828384/**85* Dispatches a wrapped error event based on the given event.86*87* @param {Event} ev The error event given to the underlying IDBDatabase.88* @private89*/90goog.db.IndexedDb.prototype.dispatchError_ = function(ev) {91this.dispatchEvent({92type: goog.db.IndexedDb.EventType.ERROR,93errorCode: /** @type {IDBRequest} */ (ev.target).error.severity94});95};969798/**99* Dispatches a wrapped version change event based on the given event.100*101* @param {Event} ev The version change event given to the underlying102* IDBDatabase.103* @private104*/105goog.db.IndexedDb.prototype.dispatchVersionChange_ = function(ev) {106this.dispatchEvent(107new goog.db.IndexedDb.VersionChangeEvent(ev.oldVersion, ev.newVersion));108};109110111/**112* Closes the database connection. Metadata queries can still be made after this113* method is called, but otherwise this wrapper should not be used further.114*/115goog.db.IndexedDb.prototype.close = function() {116if (this.open_) {117this.db_.close();118this.open_ = false;119}120};121122123/**124* @return {boolean} Whether a connection is open and the database can be used.125*/126goog.db.IndexedDb.prototype.isOpen = function() {127return this.open_;128};129130131/**132* @return {string} The name of this database.133*/134goog.db.IndexedDb.prototype.getName = function() {135return this.db_.name;136};137138139/**140* @return {number} The current database version.141*/142goog.db.IndexedDb.prototype.getVersion = function() {143// TODO(bradfordcsmith): drop Number() call once closure compiler's externs144// are updated145return Number(this.db_.version);146};147148149/**150* @return {DOMStringList} List of object stores in this database.151*/152goog.db.IndexedDb.prototype.getObjectStoreNames = function() {153return this.db_.objectStoreNames;154};155156157/**158* Creates an object store in this database. Can only be called inside a159* {@link goog.db.UpgradeNeededCallback}.160*161* @param {string} name Name for the new object store.162* @param {!IDBObjectStoreParameters=} opt_params Options object.163* The available options are:164* keyPath, which is a string and determines what object attribute165* to use as the key when storing objects in this object store; and166* autoIncrement, which is a boolean, which defaults to false and determines167* whether the object store should automatically generate keys for stored168* objects. If keyPath is not provided and autoIncrement is false, then all169* insert operations must provide a key as a parameter.170* @return {!goog.db.ObjectStore} The newly created object store.171* @throws {goog.db.Error} If there's a problem creating the object store.172*/173goog.db.IndexedDb.prototype.createObjectStore = function(name, opt_params) {174try {175return new goog.db.ObjectStore(176this.db_.createObjectStore(name, opt_params));177} catch (ex) {178throw goog.db.Error.fromException(ex, 'creating object store ' + name);179}180};181182183/**184* Deletes an object store. Can only be called inside a185* {@link goog.db.UpgradeNeededCallback}.186*187* @param {string} name Name of the object store to delete.188* @throws {goog.db.Error} If there's a problem deleting the object store.189*/190goog.db.IndexedDb.prototype.deleteObjectStore = function(name) {191try {192this.db_.deleteObjectStore(name);193} catch (ex) {194throw goog.db.Error.fromException(ex, 'deleting object store ' + name);195}196};197198199/**200* Creates a new transaction.201*202* @param {!Array<string>} storeNames A list of strings that contains the203* transaction's scope, the object stores that this transaction can operate204* on.205* @param {goog.db.Transaction.TransactionMode=} opt_mode The mode of the206* transaction. If not present, the default is READ_ONLY.207* @return {!goog.db.Transaction} The wrapper for the newly created transaction.208* @throws {goog.db.Error} If there's a problem creating the transaction.209*/210goog.db.IndexedDb.prototype.createTransaction = function(storeNames, opt_mode) {211try {212// IndexedDB on Chrome 22+ requires that opt_mode not be passed rather than213// be explicitly passed as undefined.214var transaction = opt_mode ? this.db_.transaction(storeNames, opt_mode) :215this.db_.transaction(storeNames);216return new goog.db.Transaction(transaction, this);217} catch (ex) {218throw goog.db.Error.fromException(ex, 'creating transaction');219}220};221222223/** @override */224goog.db.IndexedDb.prototype.disposeInternal = function() {225goog.db.IndexedDb.base(this, 'disposeInternal');226this.eventHandler_.dispose();227};228229230/**231* Event types fired by a database.232*233* @enum {string} The event types for the web socket.234*/235goog.db.IndexedDb.EventType = {236237/**238* Fired when a transaction is aborted and the event bubbles to its database.239*/240ABORT: 'abort',241242/**243* Fired when the database connection is forcibly closed by the browser,244* without an explicit call to IDBDatabase#close. This behavior is not in the245* spec yet but will be added since it is necessary, see246* https://www.w3.org/Bugs/Public/show_bug.cgi?id=22540.247*/248CLOSE: 'close',249250/**251* Fired when a transaction has an error.252*/253ERROR: 'error',254255/**256* Fired when someone (possibly in another window) is attempting to modify the257* structure of the database. Since a change can only be made when there are258* no active database connections, this usually means that the database should259* be closed so that the other client can make its changes.260*/261VERSION_CHANGE: 'versionchange'262};263264265266/**267* Event representing a (possibly attempted) change in the database structure.268*269* At time of writing, no Chrome versions support oldVersion or newVersion. See270* http://crbug.com/153122.271*272* @param {number} oldVersion The previous version of the database.273* @param {number} newVersion The version the database is being or has been274* updated to.275* @constructor276* @extends {goog.events.Event}277* @final278*/279goog.db.IndexedDb.VersionChangeEvent = function(oldVersion, newVersion) {280goog.db.IndexedDb.VersionChangeEvent.base(281this, 'constructor', goog.db.IndexedDb.EventType.VERSION_CHANGE);282283/**284* The previous version of the database.285* @type {number}286*/287this.oldVersion = oldVersion;288289/**290* The version the database is being or has been updated to.291* @type {number}292*/293this.newVersion = newVersion;294};295goog.inherits(goog.db.IndexedDb.VersionChangeEvent, goog.events.Event);296297298