-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathangular-websql.min.js
More file actions
7 lines (7 loc) · 6.38 KB
/
angular-websql.min.js
File metadata and controls
7 lines (7 loc) · 6.38 KB
1
2
3
4
5
6
7
/**
* angular-websql
* Helps you generate and run websql queries with angular services.
* © MIT License
* @version 1.0.2
*/
"use strict";angular.module("angular-websql",[]).factory("$webSql",["$q",function(a){return{openDatabase:function(b,c,d,e){try{var f=openDatabase(b,c,d,e);if("undefined"==typeof openDatabase)throw"Browser does not support web sql";return{executeQuery:function(b,c){var d=a.defer();return f.transaction(function(a){a.executeSql(b,c,function(a,b){d.resolve(b)},function(a,b){console.log("There has been an error: "+b.message),d.reject()})}),d.promise},executeQueries:function(b,c){var d=a.defer();return f.transaction(function(a){for(var e=0;e<b.length;++e)a.executeSql(b[e],c[e],function(a,b){},function(a,b){console.log("There has been an error: "+b.message),d.reject()})},function(a,b){console.log("There has been an error: "+b),d.reject()},function(a,b){d.resolve(b)}),d.promise},insert:function(a,b,c){var d="boolean"==typeof c&&c?"INSERT OR REPLACE":"INSERT";d+=" INTO `{tableName}` ({fields}) VALUES({values});";var e="",f="",g=[];for(var h in b)e+=Object.keys(b)[Object.keys(b).length-1]==h?"`"+h+"`":"`"+h+"`, ",f+=Object.keys(b)[Object.keys(b).length-1]==h?"?":"?, ",g.push(b[h]);return this.executeQuery(this.replace(d,{"{tableName}":a,"{fields}":e,"{values}":f}),g)},bulkInsert:function(b,c,d){var e=a.defer(),g=this;if(!(c instanceof Array))return this.insert(b,c,d);if("undefined"==typeof c||c.length<=0)e.reject();else{var h="boolean"==typeof d&&d?"INSERT OR REPLACE":"INSERT";h+=" INTO `{tableName}` ({fields}) VALUES({values});",f.transaction(function(a){for(var d in c){var f=c[d],i="",j="",k=[];for(var l in f)i+=Object.keys(f)[Object.keys(f).length-1]==l?"`"+l+"`":"`"+l+"`, ",j+=Object.keys(f)[Object.keys(f).length-1]==l?"?":"?, ",k.push(f[l]);h=g.replace(h,{"{tableName}":b,"{fields}":i,"{values}":j}),a.executeSql(h,k,function(a,b){},function(a,b){console.log("There has been an error: "+b.message),e.reject()})}},function(a,b){console.log("There has been an error: "+b),e.reject()},function(a,b){e.resolve(b)})}return e.promise},update:function(a,b,c){var d="UPDATE `{tableName}` SET {update} WHERE {where}; ",e="",f=[];for(var g in b)e+=Object.keys(b)[Object.keys(b).length-1]==g?"`"+g+"`= ?":"`"+g+"`= ?,",f.push(b[g]);var h=this.whereClause(c);return this.executeQuery(this.replace(d,{"{tableName}":a,"{update}":e,"{where}":h.w}),f.concat(h.p))},del:function(a,b){if(b)var c="DELETE FROM `{tableName}` WHERE {where}; ",d=this.whereClause(b);else var c="DELETE FROM `{tableName}`;",d={p:[]};return this.executeQuery(this.replace(c,{"{tableName}":a,"{where}":d.w}),d.p)},select:function(a,b){var c="SELECT * FROM `{tableName}` WHERE {where}; ",d=this.whereClause(b);return this.executeQuery(this.replace(c,{"{tableName}":a,"{where}":d.w}),d.p)},selectLimit:function(a,b,c){var d="SELECT * FROM `{tableName}` WHERE {where} LIMIT {limit}; ",e=this.whereClause(b);if(isNaN(c))throw"Limit must be a number.";return this.executeQuery(this.replace(d,{"{tableName}":a,"{where}":e.w,"{limit}":c}),e.p)},selectAll:function(a,b){var c="SELECT * FROM `{tableName}` ",d="";if("undefined"!=typeof b&&b.length>0){c+=" {extras} ";for(var e in b){d+=" "+b[e].operator+" ";for(var f in b[e].columns)d+="`"+b[e].columns[f]+"`,";d=d.substring(0,d.length-1)+" ","undefined"!=typeof b[e].postOperator&&(c+=b[e].postOperator+" ")}c+=";"}return this.executeQuery(this.replace(c,{"{tableName}":a,"{extras}":d}),[])},selectAllLimit:function(a,b){if(isNaN(b))throw"Limit must be a number.";return this.executeQuery("SELECT * FROM `"+a+"` LIMIT "+b+"; ",[])},selectOne:function(a){return this.selectAllLimit(a,1)},whereClause:function(a){var b="",c=[];for(var d in a)"undefined"==typeof a[d]||"object"==typeof a[d]||"string"!=typeof a[d]||a[d].match(/NULL/gi)?"undefined"!=typeof a[d]&&"object"!=typeof a[d]&&"number"==typeof a[d]?c.push(a[d]):"undefined"==typeof a[d].value||"object"!=typeof a[d]||"number"!=typeof a[d].value&&a[d].value.match(/NULL/gi)||c.push(a[d].value):c.push(a[d]),b+="object"==typeof a[d]?"undefined"==typeof a[d].union?"string"==typeof a[d].value&&a[d].value.match(/NULL/gi)?"`"+d+"` "+a[d].value:"undefined"!=typeof a[d].operator?"`"+d+"` "+a[d].operator+" ? ":"`"+d+"` = ?":"string"==typeof a[d].value&&a[d].value.match(/NULL/gi)?"`"+d+"` "+a[d].value+" "+a[d].union+" ":"undefined"!=typeof a[d].operator?"`"+d+"` "+a[d].operator+" ? "+a[d].union+" ":"`"+d+"` = ? "+a[d].union+" ":"string"==typeof a[d]&&a[d].match(/NULL/gi)?"`"+d+"` "+a[d]:"`"+d+"` = ?";return{w:b,p:c}},replace:function(a,b){for(var c in b)a=a.replace(new RegExp(c,"ig"),b[c]);return a},addColumns:function(a,b){var c=[],d=[];for(var e in b){var f="ALTER TABLE `{tableName}` ADD COLUMN {fields}; ",g="{type} {null}",h=[],i="`"+e+"` ";"undefined"==typeof b[e].null&&(b[e].null="NULL");for(var j in b[e])g=g.replace(new RegExp("{"+j+"}","ig"),b[e][j]);i+=g,"undefined"!=typeof b[e].default&&(i+=" DEFAULT "+b[e].default),"undefined"!=typeof b[e].primary&&(i+=" PRIMARY KEY"),"undefined"!=typeof b[e].auto_increment&&(i+=" AUTOINCREMENT"),Object.keys(b)[Object.keys(b).length-1]!=e&&(i+=","),"undefined"!=typeof b[e].primary&&b[e].primary&&h.push(e);var k={tableName:a,fields:i};for(var l in k)f=f.replace(new RegExp("{"+l+"}","ig"),k[l]);c.push(f),d.push([])}return this.executeQueries(c,d)},createTable:function(a,b){var c="CREATE TABLE IF NOT EXISTS `{tableName}` ({fields}); ",d=[],e="";for(var f in b){var g="{type} {null}";e+="`"+f+"` ","undefined"==typeof b[f].null&&(b[f].null="NULL");for(var h in b[f])g=g.replace(new RegExp("{"+h+"}","ig"),b[f][h]);e+=g,"undefined"!=typeof b[f].default&&(e+=" DEFAULT "+b[f].default),"undefined"!=typeof b[f].primary&&(e+=" PRIMARY KEY"),"undefined"!=typeof b[f].auto_increment&&(e+=" AUTOINCREMENT"),Object.keys(b)[Object.keys(b).length-1]!=f&&(e+=","),"undefined"!=typeof b[f].primary&&b[f].primary&&d.push(f)}var i={tableName:a,fields:e};for(var j in i)c=c.replace(new RegExp("{"+j+"}","ig"),i[j]);return this.executeQuery(c,[])},createOrAlterTable:function(a,b){var c=this;return c.select("sqlite_master",{type:{value:"table",union:"AND"},tbl_name:a}).then(function(d){if(d.rows.length<=0)return c.createTable(a,b);for(var e=0;e<d.rows.length;++e){var f=d.rows.item(e).sql,g=new RegExp("`[^`]+`","ig"),h=f.replace(/(CREATE TABLE `.*` \(|\))/gi,"").match(g),i={};for(var j in b)h.indexOf("`"+j+"`")==-1&&(i[j]=b[j]);return c.addColumns(a,i)}})},dropTable:function(a){return this.executeQuery("DROP TABLE IF EXISTS `"+a+"`; ",[])}}}catch(a){console.error(a)}}}}]);