Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * WebkitSQLiteAdaptor
- * ===================
- * Sqlite implementation for Lawnchair.
- *
- */
- var WebkitSQLiteAdaptor = function(options) {
- for (var i in LawnchairAdaptorHelpers) {
- this[i] = LawnchairAdaptorHelpers[i];
- }
- this.init(options);
- };
- WebkitSQLiteAdaptor.prototype = {
- init:function(options) {
- var that = this;
- var merge = that.merge;
- var opts = (typeof arguments[0] == 'string') ? {table:options} : options;
- // default properties
- this.name = merge('Lawnchair', opts.name );
- this.version = merge('1.0', opts.version );
- this.table = merge('field', opts.table );
- this.display = merge('shed', opts.display );
- this.max = merge(65536, opts.max );
- this.db = merge(null, opts.db );
- // default sqlite callbacks
- this.onError = function(){};
- this.onData = function(){};
- if("onError" in opts) {
- this.onError = opts.onError;
- }
- // error out on shit browsers
- if (!window.openDatabase)
- throw('Lawnchair, "This browser does not support sqlite storage."');
- // instantiate the store
- this.db = openDatabase(this.name, this.version, this.display, this.max);
- // create a default database and table if one does not exist
- this.db.transaction(function(tx) {
- tx.executeSql("SELECT COUNT(*) FROM " + that.table, [], function(){}, function(tx, error) {
- that.db.transaction(function(tx) {
- tx.executeSql("CREATE TABLE "+ that.table + " (id NVARCHAR(32) UNIQUE PRIMARY KEY, value TEXT, timestamp REAL)", [], function(){}, that.onError);
- });
- });
- });
- },
- save:function(obj, callback) {
- var that = this;
- var update = function(id, obj, callback) {
- that.db.transaction(function(t) {
- t.executeSql(
- "UPDATE " + that.table + " SET value=?, timestamp=? WHERE id=?",
- [that.serialize(obj), that.now(), id],
- function() {
- if (callback != undefined) {
- obj.key = id;
- callback(obj);
- }
- },
- that.onError
- );
- });
- };
- var insert = function(obj, callback) {
- that.db.transaction(function(t) {
- var id = (obj.key == undefined) ? that.uuid() : obj.key;
- delete(obj.key);
- t.executeSql(
- "INSERT INTO " + that.table + " (id, value,timestamp) VALUES (?,?,?)",
- [id, that.serialize(obj), that.now()],
- function() {
- if (callback != undefined) {
- obj.key = id;
- callback(obj);
- }
- },
- that.onError
- );
- });
- };
- if (obj.key == undefined) {
- insert(obj, callback);
- } else {
- this.get(obj.key, function(r) {
- var isUpdate = (r != null);
- if (isUpdate) {
- var id = obj.key;
- delete(obj.key);
- update(id, obj, callback);
- } else {
- insert(obj, callback);
- }
- });
- }
- },
- get:function(key, callback) {
- var that = this;
- this.db.transaction(function(t) {
- t.executeSql(
- "SELECT value FROM " + that.table + " WHERE id = ?",
- [key],
- function(tx, results) {
- if (results.rows.length == 0) {
- callback(null);
- } else {
- var o = that.deserialize(results.rows.item(0).value);
- o.key = key;
- callback(o);
- }
- },
- this.onError
- );
- });
- },
- all:function(callback) {
- var cb = this.terseToVerboseCallback(callback);
- var that = this;
- this.db.transaction(function(t) {
- t.executeSql("SELECT * FROM " + that.table, [], function(tx, results) {
- if (results.rows.length == 0 ) {
- cb([]);
- } else {
- var r = [];
- for (var i = 0, l = results.rows.length; i < l; i++) {
- var raw = results.rows.item(i).value;
- var obj = that.deserialize(raw);
- obj.key = results.rows.item(i).id;
- r.push(obj);
- }
- cb(r);
- }
- },
- that.onError);
- });
- },
- remove:function(keyOrObj, callback) {
- var that = this;
- this.db.transaction(function(t) {
- t.executeSql(
- "DELETE FROM " + that.table + " WHERE id = ?",
- [(typeof keyOrObj == 'string') ? keyOrObj : keyOrObj.key],
- callback || that.onData,
- that.onError
- );
- });
- },
- nuke:function(callback) {
- var that = this;
- this.db.transaction(function(tx) {
- tx.executeSql(
- "DELETE FROM " + that.table,
- [],
- callback || that.onData,
- that.onError
- );
- });
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement