forked from paulocaldeira17/angular-websql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathangular-websql.min.js
More file actions
7 lines (7 loc) · 3.47 KB
/
angular-websql.min.js
File metadata and controls
7 lines (7 loc) · 3.47 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},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)},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)},selectAll:function(a){return this.executeQuery("SELECT * FROM `"+a+"`; ",[])},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},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,[])},dropTable:function(a){return this.executeQuery("DROP TABLE IF EXISTS `"+a+"`; ",[])}}}catch(g){console.error(g)}}}}]);