Created
January 15, 2014 03:23
-
-
Save composite/8430218 to your computer and use it in GitHub Desktop.
Database class for Web SQL Database
(but will not active?)
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
| /** | |
| * Web SQL Database 를 사용하기 쉽게 정의한 클래스 | |
| * | |
| * 초기화: | |
| * var db = new Database('dbname', 10*1024*1024); | |
| * 단일 질의: (현재 트랜잭션이 특성상 한꺼번에 수행 못하는 문제 있음.) | |
| * db.query('select * from table').then(successFunc); | |
| * 배치 질의: (한꺼번에 수행할 시 반드시 추천.) | |
| * db.begin() | |
| * .query('insert into table values (?)', 1) | |
| * .query('insert into table values (?)', 2) | |
| * .query('insert into table values (?)', 3) | |
| * .execute(doneFunc, progressFunc) | |
| * .then(afterExecFunc); | |
| * 프라미스 이벤트 처리: | |
| * .then(function(resultSet){ | |
| * for(var i=0;i<resultSet.rows.length;i++){ | |
| * var row = resultSet.rows.item(i); | |
| * // TODO : 각 행마다 처리 | |
| * } | |
| * }); | |
| * then 메소드 호출은 선택사항. | |
| * 단, then 호출 안하고 다음 사항으로 넘어갈 시 | |
| * 이전 쿼리 성공 이벤트를 정의하지 못하게 됨. (일회성 이벤트) | |
| * | |
| * @param {string} dbname DB 이름 (필수) | |
| * @param {integer} size DB 저장소 크기 (선택) | |
| */ | |
| var Database = (function(DB, un){ | |
| 'use strict'; | |
| if(!DB){ | |
| return function(){throw new Error('Not supported DB feature.')}; | |
| } | |
| /** | |
| * 해당 클래스에 프라미즈 패턴을 가능케 하는 내부 클래스 | |
| * @param {object} context 프라미즈 패턴 기준 개체 | |
| */ | |
| var 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;}; | |
| }, | |
| /** | |
| * 일회성 프라미즈 패턴을 인식하는 키를 생성 | |
| * @return {string} | |
| */ | |
| keygen = function(){ | |
| return 'FN#DS$' + Math.ceil(Math.abs(Math.random()*(new Date()))); | |
| }; | |
| /** | |
| * 함수 여부 | |
| * @param {object} fn 임의의 개체 | |
| * @return {boolean} | |
| */ | |
| defer.isF = function(fn){return typeof(fn) === 'function' || fn instanceof Function;}; | |
| /** | |
| * 빈 함수 정의 | |
| * @return {function} | |
| */ | |
| defer.noF = function(){}; | |
| /** | |
| * Web SQL Database 클래스 본문 | |
| * @param {string} dbname DB 이름 | |
| * @param {integer} size DB 저장소 크기 | |
| */ | |
| function SQL(dbname, size){ | |
| var that = this, fnkey = this.next = keygen(); | |
| this.defer = new defer(this); | |
| this.db = DB(dbname, '1.0', 'Data Store for NodePlatform.', isNaN(size) ? 10*1024*1024 : size); | |
| this.db.transaction(function(tx){ | |
| that.defer.one(fnkey).call(that); | |
| }); | |
| } | |
| SQL.prototype = { | |
| query: function(){ | |
| var that = this, fnkey = this.next = keygen() | |
| ,q = [].shift.call(arguments), arg = [].slice.call(arguments); | |
| if(!this.isTran){ | |
| this.db.transaction(function(tx){ | |
| tx.executeSql(q , arg, function (tx, results) { | |
| that.defer.one(fnkey).apply(that, [results, q].concat(arg)); | |
| }, SQL.onerror); | |
| }); | |
| }else{ | |
| (function(q, arg){ | |
| that.tranq.push(function(tx, count, max, done, proc){ | |
| tx.executeSql(q , arg, function (tx, results) { | |
| that.defer.one(fnkey).apply(that, [results, q].concat(arg)); | |
| if(count == max){ | |
| done.call(tx, max); | |
| }else{ | |
| proc.call(tx, count, max); | |
| } | |
| }, SQL.onerror); | |
| }); | |
| })(q, arg); | |
| } | |
| return this; | |
| }, | |
| begin: function(){ | |
| this.isTran = true; | |
| this.tranq = []; | |
| return this; | |
| }, | |
| execute: function(done, proc){ | |
| var that = this, fnkey = this.next = keygen() | |
| this.db.transaction(function(tx){ | |
| var db = this, count = 0, max = that.tranq.length; | |
| done = defer.isF(done) ? done : defer.noF; | |
| proc = defer.isF(proc) ? proc : defer.noF; | |
| that.tranq.forEach(function(fn){ | |
| count++; | |
| fn.call(db, tx, count, max, done, proc); | |
| }); | |
| that.defer.one(fnkey).apply(that); | |
| }); | |
| this.isTran = false; | |
| return this; | |
| }, | |
| then: function(fn){ | |
| this.defer.set(this.next, fn); | |
| return this; | |
| } | |
| }; | |
| Object.defineProperties(SQL.prototype, { | |
| tranq: { | |
| value: [], | |
| writable: true | |
| }, | |
| isTran: { | |
| value: false, | |
| writable: true | |
| }, | |
| }); | |
| SQL.onerror = function(tx, error){ | |
| if(window.console && console.error){ | |
| console.error(error); | |
| } | |
| }; | |
| return SQL; | |
| })(window.openDatabase); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment