Advertisement
patrickc

Untitled

Dec 10th, 2011
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * WebkitSQLiteAdaptor
  3.  * ===================
  4.  * Sqlite implementation for Lawnchair.
  5.  *
  6.  */
  7. var WebkitSQLiteAdaptor = function(options) {
  8.     for (var i in LawnchairAdaptorHelpers) {
  9.         this[i] = LawnchairAdaptorHelpers[i];
  10.     }
  11.     this.init(options);
  12. };
  13.  
  14.  
  15. WebkitSQLiteAdaptor.prototype = {
  16.     init:function(options) {
  17.         var that = this;
  18.         var merge = that.merge;
  19.         var opts = (typeof arguments[0] == 'string') ? {table:options} : options;
  20.  
  21.         // default properties
  22.         this.name       = merge('Lawnchair', opts.name      );
  23.         this.version    = merge('1.0',       opts.version   );
  24.         this.table      = merge('field',     opts.table     );
  25.         this.display    = merge('shed',      opts.display   );
  26.         this.max        = merge(65536,       opts.max       );
  27.         this.db         = merge(null,        opts.db        );
  28.  
  29.         // default sqlite callbacks
  30.         this.onError = function(){};
  31.         this.onData  = function(){};
  32.  
  33.         if("onError" in opts) {
  34.             this.onError = opts.onError;
  35.         }
  36.  
  37.         // error out on shit browsers
  38.         if (!window.openDatabase)
  39.             throw('Lawnchair, "This browser does not support sqlite storage."');
  40.  
  41.         // instantiate the store
  42.         this.db = openDatabase(this.name, this.version, this.display, this.max);
  43.  
  44.         // create a default database and table if one does not exist
  45.         this.db.transaction(function(tx) {
  46.             tx.executeSql("SELECT COUNT(*) FROM " + that.table, [], function(){}, function(tx, error) {
  47.                 that.db.transaction(function(tx) {
  48.                     tx.executeSql("CREATE TABLE "+ that.table + " (id NVARCHAR(32) UNIQUE PRIMARY KEY, value TEXT, timestamp REAL)", [], function(){}, that.onError);
  49.                 });
  50.             });
  51.         });
  52.     },
  53.     save:function(obj, callback) {
  54.         var that = this;
  55.    
  56.         var update = function(id, obj, callback) {
  57.             that.db.transaction(function(t) {
  58.                 t.executeSql(
  59.                     "UPDATE " + that.table + " SET value=?, timestamp=? WHERE id=?",
  60.                     [that.serialize(obj), that.now(), id],
  61.                     function() {
  62.                         if (callback != undefined) {
  63.                             obj.key = id;
  64.                             callback(obj);
  65.                         }
  66.                     },
  67.                     that.onError
  68.                 );
  69.             });
  70.         };
  71.         var insert = function(obj, callback) {
  72.             that.db.transaction(function(t) {
  73.                 var id = (obj.key == undefined) ? that.uuid() : obj.key;
  74.                 delete(obj.key);
  75.                 t.executeSql(
  76.                     "INSERT INTO " + that.table + " (id, value,timestamp) VALUES (?,?,?)",
  77.                     [id, that.serialize(obj), that.now()],
  78.                     function() {
  79.                         if (callback != undefined) {
  80.                             obj.key = id;
  81.                             callback(obj);
  82.                         }
  83.                     },
  84.                     that.onError
  85.                 );
  86.             });
  87.         };
  88.         if (obj.key == undefined) {
  89.             insert(obj, callback);
  90.         } else {
  91.             this.get(obj.key, function(r) {
  92.                 var isUpdate = (r != null);
  93.    
  94.                 if (isUpdate) {
  95.                     var id = obj.key;
  96.                     delete(obj.key);
  97.                     update(id, obj, callback);
  98.                 } else {
  99.                     insert(obj, callback);
  100.                 }
  101.             });
  102.         }
  103.     },
  104.     get:function(key, callback) {
  105.         var that = this;
  106.         this.db.transaction(function(t) {
  107.             t.executeSql(
  108.                 "SELECT value FROM " + that.table + " WHERE id = ?",
  109.                 [key],
  110.                 function(tx, results) {
  111.                     if (results.rows.length == 0) {
  112.                         callback(null);
  113.                     } else {
  114.                         var o = that.deserialize(results.rows.item(0).value);
  115.                         o.key = key;
  116.                         callback(o);
  117.                     }
  118.                 },
  119.                 this.onError
  120.             );
  121.         });
  122.     },
  123.     all:function(callback) {
  124.         var cb = this.terseToVerboseCallback(callback);
  125.         var that = this;
  126.         this.db.transaction(function(t) {
  127.             t.executeSql("SELECT * FROM " + that.table, [], function(tx, results) {
  128.                 if (results.rows.length == 0 ) {
  129.                     cb([]);
  130.                 } else {
  131.                     var r = [];
  132.                     for (var i = 0, l = results.rows.length; i < l; i++) {
  133.                         var raw = results.rows.item(i).value;
  134.                         var obj = that.deserialize(raw);
  135.                         obj.key = results.rows.item(i).id;
  136.                         r.push(obj);
  137.                     }
  138.                     cb(r);
  139.                 }
  140.             },
  141.             that.onError);
  142.         });
  143.     },
  144.     remove:function(keyOrObj, callback) {
  145.         var that = this;
  146.         this.db.transaction(function(t) {
  147.             t.executeSql(
  148.                 "DELETE FROM " + that.table + " WHERE id = ?",
  149.                 [(typeof keyOrObj == 'string') ? keyOrObj : keyOrObj.key],
  150.                 callback || that.onData,
  151.                 that.onError
  152.             );
  153.         });
  154.     },
  155.     nuke:function(callback) {
  156.         var that = this;
  157.         this.db.transaction(function(tx) {
  158.             tx.executeSql(
  159.                 "DELETE FROM " + that.table,
  160.                 [],
  161.                 callback || that.onData,
  162.                 that.onError
  163.             );
  164.         });
  165.     }
  166. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement