Don't like ads? PRO users don't see any ads ;-)

Untitled

By: Bili on May 26th, 2012  |  syntax: JavaScript  |  size: 3.83 KB  |  hits: 29  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. (function () {
  2.   "use strict";
  3.  
  4.   var Statement, Database;
  5.  
  6.   // Alternative typeof implementation yielding more meaningful results,
  7.   // see http://javascriptweblog.wordpress.com/2011/08/08/fixing-the-javascript-typeof-operator/
  8.   function type(obj) {
  9.     var typeString;
  10.  
  11.     typeString = Object.prototype.toString.call(obj);
  12.     return typeString.substring(8, typeString.length - 1).toLowerCase();
  13.   }
  14.  
  15.   function throwSQLiteError(message, comException) {
  16.     var error = new Error(message);
  17.     error.resultCode = comException.number & 0xffff;
  18.     throw error;
  19.   }
  20.  
  21.   Statement = WinJS.Class.define(function (db, sql, args) {
  22.     try {
  23.       this.statement = db.connection.prepare(sql);
  24.     } catch (comException) {
  25.       throwSQLiteError('Error preparing an SQLite statement.', comException);
  26.     }
  27.  
  28.     if (args) {
  29.       this.bind(args);
  30.     }
  31.   }, {
  32.     bind: function (args) {
  33.       var index, resultCode;
  34.  
  35.       args.forEach(function (arg, i) {
  36.         index = i + 1;
  37.         switch (type(arg)) {
  38.           case 'number':
  39.             if (arg % 1 === 0) {
  40.               resultCode = this.statement.bindInt(index, arg);
  41.             } else {
  42.               resultCode = this.statement.bindDouble(index, arg);
  43.             }
  44.             break;
  45.           case 'string':
  46.             resultCode = this.statement.bindText(index, arg);
  47.             break;
  48.           case 'null':
  49.             resultCode = this.statement.bindNull(index);
  50.             break;
  51.           default:
  52.             throw new Error("Unsupported argument type.");
  53.         }
  54.         if (resultCode !== SQLite3.ResultCode.ok) {
  55.           throw new Error("Error " + resultCode + " when binding argument to SQL query.");
  56.         }
  57.       }, this);
  58.     },
  59.     run: function () {
  60.       this.statement.step();
  61.     },
  62.     all: function () {
  63.       var result = [];
  64.  
  65.       this.each(function (row) {
  66.         result.push(row);
  67.       });
  68.       return result;
  69.     },
  70.     each: function (callback) {
  71.       var row, i, len, name;
  72.  
  73.       while (this.statement.step() === SQLite3.ResultCode.row) {
  74.         row = {};
  75.         for (i = 0, len = this.statement.columnCount() ; i < len; i += 1) {
  76.           name = this.statement.columnName(i);
  77.           switch (this.statement.columnType(i)) {
  78.             case SQLite3.Datatype.integer:
  79.               row[name] = this.statement.columnInt(i);
  80.               break;
  81.             case SQLite3.Datatype.float:
  82.               row[name] = this.statement.columnDouble(i);
  83.               break;
  84.             case SQLite3.Datatype.text:
  85.               row[name] = this.statement.columnText(i);
  86.               break;
  87.             case SQLite3.Datatype["null"]:
  88.               row[name] = null;
  89.               break;
  90.           }
  91.         }
  92.         callback(row);
  93.       }
  94.     },
  95.     close: function () {
  96.       this.statement.close();
  97.     }
  98.   });
  99.  
  100.   Database = WinJS.Class.define(function (dbPath) {
  101.     try {
  102.       this.connection = SQLite3.Database(dbPath);
  103.     } catch (comException) {
  104.       throwSQLiteError('Error creating an SQLite database connection.', comException);
  105.     }
  106.   }, {
  107.     run: function (sql, args) {
  108.       var statement = new Statement(this, sql, args);
  109.  
  110.       statement.run();
  111.       statement.close();
  112.     },
  113.     all: function (sql, args) {
  114.       var rows, statement = new Statement(this, sql, args);
  115.  
  116.       rows = statement.all();
  117.       statement.close();
  118.       return rows;
  119.     },
  120.     each: function (sql, args, callback) {
  121.       if (!callback && type(args) === 'function') {
  122.         callback = args;
  123.         args = null;
  124.       }
  125.  
  126.       var statement = new Statement(this, sql, args);
  127.  
  128.       statement.each(callback);
  129.       statement.close();
  130.     },
  131.     close: function () {
  132.       this.connection.close();
  133.     }
  134. });
  135.  
  136. WinJS.Namespace.define('SQLite3JS', {
  137.   Database: Database
  138. });
  139.  
  140. }());