home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 February / maximum-cd-2011-02.iso / DiscContents / digsby_setup85.exe / lib / plugins / twitter / res / sql.js < prev    next >
Encoding:
JavaScript  |  2009-08-25  |  2.7 KB  |  99 lines

  1.  
  2. // tiny HTML5 ORM
  3.  
  4. function SQLDesc(tableName, columns)
  5. {
  6.     this.tableName = tableName;
  7.     this.columns = columns;
  8.     this.allowedAttrs = makeAttrsSet(columns);
  9.  
  10.     this.insertPlaceholders = '(';
  11.  
  12.     var i;
  13.     for (i = 0; i < columns.length - 1; ++i)
  14.         this.insertPlaceholders += '?, ';
  15.     this.insertPlaceholders += '?)';
  16.  
  17.     this.typeCoercions = {};
  18.     for (i = 0; i < columns.length; ++i) {
  19.         var type = columns[i][1];
  20.         if (type.toLowerCase() === 'boolean') {
  21.             columns[i][1] = 'integer';
  22.             this.typeCoercions[columns[i][0]] = Number;
  23.         }
  24.     }
  25. }
  26.  
  27.  
  28. SQLDesc.prototype = {
  29.     toString: function() {
  30.         return '[SQLDesc ' + this.tableName + ']';
  31.     },
  32.  
  33.     insertTransform: function(columnName, obj) {
  34.         return columnName in this.typeCoercions ?
  35.             new this.typeCoercions[columnName](obj) :
  36.             obj;
  37.     },
  38.  
  39.     createTableStatement: function () {
  40.         var types = [];
  41.         $.each(this.columns, function (i, column) {
  42.             var name = column[0], type = column[1];
  43.             types.push([name, type].join(' '));
  44.         });
  45.         var typesString = types.join(', ');
  46.  
  47.         return "CREATE TABLE IF NOT EXISTS " + this.tableName + " (" + typesString + ")";
  48.     },
  49.  
  50.     ensureTableExists: function (tx, successCb, errorCb) {
  51.         tx.executeSql(this.createTableStatement(), [], function(tx, result) {
  52.             if (successCb)
  53.                 successCb(tx, result);
  54.         }, function (tx, error) {
  55.             if (errorCb)
  56.                 errorCb(tx, error);
  57.         });
  58.     },
  59.  
  60.     fromJSON: function (json, override) {
  61.         var allowed = this.allowedAttrs;
  62.         var obj = {};
  63.  
  64.         for (var key in json) {
  65.             var value = (override ? override[key] : undefined) || json[key];
  66.             if (allowed[key])
  67.                 obj[key] = value;
  68.         }
  69.  
  70.         return obj;
  71.     },
  72.  
  73.     insertOrReplace: function (tx, obj) {
  74.         var vals = [];
  75.  
  76.         var cols = this.columns;
  77.         for (var i = 0; i < cols.length; ++i) {
  78.             var columnName = cols[i][0];
  79.             var val = this.insertTransform(columnName, obj[columnName]);
  80.             vals.push(val === undefined ? null : val);
  81.         }
  82.  
  83.         var statement = 'INSERT OR REPLACE INTO ' + this.tableName + ' VALUES ' + this.insertPlaceholders;
  84.         tx.executeSql(statement, vals);
  85.     }
  86. };
  87.  
  88. /**
  89.  * makes a {columnName: true} hash set for a list of column names and types
  90.  */
  91. function makeAttrsSet(columns) {
  92.     var attrsSet = {};
  93.     for (var i = 0; i < columns.length; ++i)
  94.         attrsSet[columns[i][0]] = true;
  95.     return attrsSet;
  96. }
  97.  
  98.  
  99.