Created
December 17, 2013 00:14
-
-
Save composite/7997653 to your computer and use it in GitHub Desktop.
Dataset Prototype
(WebSQL rquired but will be replaced to IndexedDB.)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (function(un){ | |
| var versions = {} | |
| , defer = function(context){ | |
| this.fn = {}; | |
| this.context = context; | |
| this.set = function(key, fn){defer.isF(fn) ? this.fn[key] = fn : defer.noF;}; | |
| this.get = function(key){return defer.isF(this.fn[key]) ? this.fn[key] : defer.noF;}; | |
| this.one = function(key){var fn = this.get(key); delete this.fn[key]; return fn;}; | |
| this.is = function(key){return key in this.fn;}; | |
| }, keygen = function(){ | |
| return 'FN#DS$' + Math.ceil(Math.abs(Math.random()*(new Date()))); | |
| }; | |
| defer.isF = function(fn){return typeof(fn) === 'function' || fn instanceof Function;}; | |
| defer.noF = function(){}; | |
| var DataSet = function(id, cols){ | |
| var that = this, fnkey = this.next = keygen(); | |
| this.id = id || 'DataSet#' + +(new Date()); | |
| this.cols = cols; | |
| this.defer = new defer(this); | |
| this.db = window.openDatabase('__DATASET_DB__', '1.0', 'DB for DataSet Framework.', 10*1024*1024); | |
| this.db.transaction(function(tx){ | |
| tx.executeSql('CREATE TABLE IF NOT EXISTS ' + that.id + ' ('+that.cols.join(',')+')'); | |
| that.defer.one(fnkey).call(that); | |
| }); | |
| }; | |
| DataSet.prototype = { | |
| select: function(){ | |
| var that = this, fnkey = this.next = keygen(); | |
| this.db.transaction(function(tx){ | |
| tx.executeSql('SELECT * FROM ' + that.id + '', [], function (tx, results) { | |
| that.defer.one(fnkey).call(that, results); | |
| }, DataSet.onerror); | |
| }); | |
| return this; | |
| }, | |
| insert: function(){ | |
| var that = this, args = arguments, slots = [], fnkey = this.next = keygen(); | |
| [].forEach.call(args, function(){slots.push('?')}); | |
| if(this.cols.length != args.length){ | |
| throw new Error('cols and arguments are not match.') | |
| } | |
| this.db.transaction(function(tx){ | |
| var sql; | |
| tx.executeSql(sql='INSERT INTO ' + that.id + ' ('+that.cols.join(',')+') VALUES ('+slots.join(',')+')', args, function (tx, results) { | |
| that.defer.one(fnkey).call(that, results); | |
| }, DataSet.onerror); | |
| console.log(sql);console.log(args); | |
| }); | |
| return this; | |
| }, | |
| then: function(fn){ | |
| console.log('SET : '+this.next); | |
| this.defer.set(this.next, fn); | |
| return this; | |
| } | |
| }; | |
| Object.defineProperties(DataSet.prototype, { | |
| }); | |
| DataSet.onerror = function(tx, error){ | |
| if(window.console && console.error){ | |
| console.error(error); | |
| } | |
| }; | |
| window.DataSet = DataSet; | |
| })(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var ds = new DataSet('test', ['code', 'name', 'date']); | |
| ds.then(function(){ | |
| console.log('DB complete.'); | |
| }).select().then(function(result){ | |
| console.log(result.rows.length); | |
| }).insert(~~(Math.random()*1024),new Date().toLocaleString(), new Date()) | |
| .then(function(){ | |
| console.log('DB insert.'); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment