home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2012 January / maximum-cd-2012-01.iso / DiscContents / digsby_setup.exe / res / html / sql.js < prev    next >
Encoding:
JavaScript  |  2009-06-18  |  2.8 KB  |  100 lines

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