Created
July 28, 2009 21:15
-
-
Save kara-ryli/157673 to your computer and use it in GitHub Desktop.
A wrapper class for HTML5 client-side storage that makes the methods more easy to document.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * SQLiteDB | |
| * | |
| * A wrapper class to make sane the HTML 5 client-side storage API. | |
| * Makes your code a little bit more self-documenting. | |
| */ | |
| var SQLiteDB = (function () { | |
| var db = function(args) { | |
| if ( SQLiteDB.available ) { this.initialize.apply(this, args); } | |
| else { throw new Error("database not available"); } | |
| }; | |
| db.prototype = { | |
| /** | |
| * Creates an instance of SQLiteDB | |
| * | |
| * @constructor | |
| * @param {String id} the ID of the database. | |
| * @param {String name} a user-friendly name of the database. | |
| * @param {String version} optional. The required version of the database. | |
| * @param {Number maxSize} optional. Maximum size of the database in bytes. | |
| */ | |
| initialize: function(id, name, version, maxSize) { | |
| if ( ! version) { version = ""; } | |
| this.db_ = window.openDatabase(id, version, name, maxSize); | |
| }, | |
| /** | |
| * Executes a transaction on the database. | |
| * | |
| * @param {Array|SQLiteDB.Statement statements} a statement or array of statements to execute | |
| * @param {Function onError} a function to call if the transaction fails. | |
| * @param {Function onSuccess} a function to call if the transaction succeeds. | |
| */ | |
| transaction: function(statements, onError, onSuccess) { | |
| this.db_.transaction(function(t) { | |
| var i, l, statement; | |
| if ( statements.length ) { | |
| for (i = 0, l = statements.length; i < l; i += 1) { | |
| statement = statements[i]; | |
| t.executeSql(statement.sql, statement.args, statement.onSuccess, statement.onFalure); | |
| } | |
| } | |
| else if ( statements.sql ){ | |
| t.executeSql(statements.sql, statements.args, statements.onSuccess, statements.onFalure); | |
| } | |
| }, onError, onSuccess); | |
| }, | |
| /** | |
| * Returns the current DB version. | |
| * | |
| * @returns {String} the DB version | |
| */ | |
| version: function() { return this.db_.version; }, | |
| /** | |
| * Returns the current DB version. | |
| * | |
| * @param {String version} the version to check | |
| * @returns {Boolean} whether the current DB version matches the supplied version | |
| */ | |
| isVersion: function(version) { return this.version() === version; } | |
| }; | |
| // Pre-defined error constants for failure handlers | |
| /** | |
| * Other non-database-related error. | |
| * | |
| * @const | |
| */ | |
| db.NON_DB_ERROR = 0; | |
| /** | |
| * Other database-related error. | |
| * | |
| * @const | |
| */ | |
| db.UNKNOWN_DB_ERROR = 1; | |
| /** | |
| * The version of the database is not the version that | |
| * you requested. | |
| * | |
| * @const | |
| */ | |
| db.INCORRECT_VERSON = 2; | |
| /** | |
| * Data set too large. There are limits in place on the | |
| * maximum result size that can be returned by a single | |
| * query. If you see this error, you should either use | |
| * the LIMIT and OFFSET constraints in the query to reduce | |
| * the number of results returned or rewrite the query to | |
| * return a more specific subset of the results. | |
| * | |
| * @const | |
| */ | |
| db.RESULTS_TO_LARGE = 3; | |
| /** | |
| * Storage limit exceeded. Either the space available for | |
| * storage is exhausted or the user declined to allow the | |
| * database to grow beyond the existing limit. | |
| * | |
| * @const | |
| */ | |
| db.EXCEEDS_STORAGE_LIMIT = 4; | |
| /** | |
| * Lock contention error. If the first query in a transaction | |
| * does not modify data, the transaction takes a read-write | |
| * lock for reading. It then upgrades that lock to a writer | |
| * lock if a subsequent query attempts to modify data. If another | |
| * query takes a writer lock ahead of it, any reads prior to | |
| * that point are untrustworthy, so the entire transaction must | |
| * be repeated. If you receive this error, you should retry the | |
| * transaction. | |
| * | |
| * @const | |
| */ | |
| db.LOCK_CONTENTION_ERROR = 5; | |
| /** | |
| * Constraint failure. This occurs when an INSERT, UPDATE, or | |
| * REPLACE query results in an empty set because a constraint on | |
| * a table could not be met. For example, you might receive this | |
| * error if it would cause two rows to contain the same non-null | |
| * value in a column marked as the primary key or marked with the | |
| * UNIQUE constraint. | |
| * | |
| * @static | |
| * @returns {Boolean} whether or not SQLiteDB is supported | |
| */ | |
| db.CONSTRAINT_FAILURE = 6; | |
| /** | |
| * Whether or not the browser supports SQLiteDB | |
| * | |
| * @const | |
| */ | |
| db.available = function() { return !!window.openDatabase; }; | |
| /** | |
| * SQLiteDB.Statement | |
| * | |
| * A convenience class for generating statements | |
| * | |
| * @param {String sql} the query to execute. | |
| * @param {Array args} the arguments to replace "?" in `sql`. | |
| * @param {Function onSuccess} executed on a successful query. | |
| * @param {Function onFailure} executed on a failed query. | |
| */ | |
| db.Statement = function(sql, args, onSuccess, onFailure) { | |
| this.sql = sql; | |
| this.args = args; | |
| this.onSuccess = onSuccess; | |
| this.onFalure = onFailure; | |
| }; | |
| return db; | |
| }()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment