Created
May 17, 2019 21:28
-
-
Save ahmetozalp/20277f635a70fb28609d6af742d94653 to your computer and use it in GitHub Desktop.
webSql.ts
This file contains 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
const SqlService = function (this: any) { | |
this.db = null; | |
this.stack = { | |
queries: [], | |
binds: [], | |
}; | |
this.timeout = 1000; | |
this.isWaiting = false; | |
this.wait = null; | |
this.RN = false; | |
this.console = function(type: any, value: string, message: string) { | |
switch (type) { | |
case 'required': | |
console.warn(value + 'is reqiured.'); | |
break; | |
case 'deprecated': | |
console.warn(value + ' is deprecated. + ' + message); | |
break; | |
default: | |
break; | |
} | |
}; | |
this.rightNow = function() { | |
this.RN = true; | |
return this; | |
}; | |
this.init = function(opt: { dbObject: any; id: any; timeout: any; }) { | |
if (typeof opt !== 'object') | |
this.console('deprecated', 'init(id, object)', 'Use init({id: \'\', dbObject: object}) instead.'); | |
else { | |
if (!opt.dbObject) | |
this.console('required', 'dbObject'); | |
if (!opt.id) | |
this.console('required', 'id'); | |
} | |
var object = opt.dbObject; | |
var id = opt.id; | |
this.timeout = opt.timeout || this.timeout; | |
this.db = object.openDatabase(id + ".db", "1.1", "Database", 200000); | |
}; | |
this.execute = function(sql: any, value: any, type: string) { | |
var self = this; | |
type = type || "array"; | |
return new Promise(function(resolve, reject) { | |
var reset = function() { | |
self.stack.queries = []; | |
self.stack.binds = []; | |
self.isWaiting = false; | |
}; | |
var exec = function(doResult: boolean) { | |
var queries = self.stack.queries; | |
var binds = self.stack.binds; | |
reset(); | |
if (queries.length < 1) | |
return; | |
self.db.transaction(function(tx: { executeSql: (arg0: any, arg1: any, arg2: (tx: any, res: any) => void, arg3: (tx: any, error: any) => void) => void; }) { | |
var tmpQ, tmpB, execCount = 0, errors: any | never[] = []; | |
for (var i = 0; i < queries.length; i++) { | |
tmpQ = queries[i]; | |
tmpB = binds[i]; | |
tx.executeSql(tmpQ, tmpB, function (tx, res) { | |
execCount++; | |
if (execCount === queries.length && doResult === true) { | |
switch(type) { | |
case 'array': | |
var list = []; | |
for (var i = 0; i < res.rows.length; i++) | |
list.push(res.rows.item(i)); | |
resolve(list); | |
break; | |
case 'object': | |
resolve(res.rows.item(0)); | |
break; | |
default: | |
resolve(res); | |
break; | |
} | |
} | |
}, function(tx, error) { | |
execCount++; | |
errors.push(error || tx); | |
if (execCount === queries.length && doResult === true) | |
reject(errors); | |
}); | |
} | |
}); | |
}; | |
if (self.RN === true || self.timeout === 0) { | |
self.RN = false; | |
if (self.wait !== null) | |
clearTimeout(self.wait); | |
exec(false); | |
self.stack.queries.push(sql); | |
self.stack.binds.push(value); | |
return exec(true); | |
} | |
self.stack.queries.push(sql); | |
self.stack.binds.push(value); | |
if (self.isWaiting === true) { | |
return; | |
} | |
self.isWaiting = true; | |
self.wait = setTimeout(exec, self.timeout); | |
}); | |
}; | |
this.select = function(table: string, field: string, where: string, values: any, order: string) { | |
var self = this; | |
var deferred = new Promise(function(resolve, reject) { | |
field = field || "*"; | |
var list = []; | |
if (where && values) { | |
var sql = 'SELECT ' + field + ' FROM ' + table + ' WHERE ' + where; | |
} else { | |
var sql = 'SELECT ' + field + ' FROM ' + table; | |
} | |
if (order) | |
sql += " ORDER BY " + order; | |
self.rightNow().execute(sql, values || []).then(resolve, reject); | |
}); | |
return deferred; | |
}; | |
this.insert = function(table: string, row: string[], values: never[] | string[][]) { | |
var self = this; | |
var deferred = new Promise(function(resolve, reject) { | |
var sql = 'INSERT INTO ' + table + ' ('; | |
for (var i = 0; i < row.length; i++) sql += row[i] + ","; | |
sql = sql.slice(0, sql.length - 1); | |
sql += ') VALUES '; | |
if (typeof values[0] == "object" && values[0]) { | |
for (var i = 0; i < values.length; i++) { | |
sql += '('; | |
for (var j = 0; j < values[i].length; j++) | |
sql += ( j != values[i].length - 1 ) ? "'"+values[i][j]+"'," : "'"+values[i][j]+"'),"; | |
} | |
sql = sql.slice(0, sql.length - 1); | |
values = []; | |
} else { | |
sql += "("; | |
for (var i = 0; i < values.length; i++) sql += "?,"; | |
sql = sql.slice(0, sql.length - 1); | |
sql += ")"; | |
} | |
self.execute(sql, values || [], "popup").then(resolve, reject); | |
}); | |
return deferred; | |
}; | |
this.delete = function(table: string, where: string, values: any) { | |
var self = this; | |
var deferred = new Promise(function(resolve, reject) { | |
if (where && values) | |
var sql = 'DELETE FROM ' + table + ' WHERE ' + where; | |
else | |
var sql = 'DELETE FROM ' + table; | |
self.execute(sql, values || [], "popup").then(resolve, reject); | |
}); | |
return deferred; | |
}; | |
this.update = function(table: string, row: string[], values: any[], where: string, wValues: any[]) { | |
var self = this; | |
var deferred = new Promise(function(resolve, reject) { | |
var sql = 'UPDATE ' + table + ' SET '; | |
for (var i = 0; i < values.length; i++) sql += row[i] + "=?,"; | |
sql = sql.slice(0, sql.length - 1); | |
if (where && wValues) { | |
sql += " WHERE " + where; | |
for (var i = 0; i < wValues.length; i++) | |
values.push(wValues[i]); | |
} | |
self.execute(sql, values || [], "popup").then(resolve, reject); | |
}); | |
return deferred; | |
}; | |
this.query = function(sql: any) { | |
let self = this; | |
let deferred = new Promise(function(resolve, reject) { | |
let list:any[] = []; | |
self.execute(sql, [], "popup", true).then(function (res: { rows: { length: number; item: (arg0: number) => void; }; }) { | |
for (let i = 0; i < res.rows.length; i++){ | |
list.push(res.rows.item(i)); | |
} | |
resolve(list); | |
}, reject); | |
}); | |
return deferred; | |
}; | |
}; | |
module.exports = SqlService; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment