JsonDB, a simple database implemented based on json Data Format
/** + Response * jsonDB Data Model Based on json data format + ----------------------------------------------- * @ description searches, deletes, queries, and updates json data * @ author yuyin (web programmer ape) thinkercode@sina.com * @ date June 28, 2014 * @ version0.1 * @ blog http://blog.csdn.net/thinkercode/ + ------------------------------------------- * Introduction: * jsonDB is a js class library, is a data * model built based on the json data format to add, delete, modify, and query json data. JsonDB is built based on an application requirement of HTML5 * local storage. You can use SQL to add, delete, modify, and query json data. In addition, this class library provides powerful where search conditions and data sorting, * limit query condition restrictions and other basic database functions. With jsonDB, you can easily maintain one database/table or multiple databases/tables without the need to maintain json data. After this class library is complete, you can simplify SQL operations, based on the jsonDB core * core module, the coherent operation model is extended to simplify jsonDB operations and the probability of SQL * sentence errors. + ----------------------------------------- * Current version Insufficiency: * 1. the query field operation is not supported * 2. sorting of unselected query fields is not supported * 3. only sorting of a single field is supported, and the combined sorting operation cannot be performed * 4. the update and delete statements do not support the order by clause, which weakens the limit clause function. * 5. to write the where condition, you must use () to include the where condition * 6. you cannot select a deep field as the returned field * 7. no error or exception solution * 8. external extensions + ------------------------------------------- * use of jsonDB: * jsonDB provides a detailed user manual, there are a lot of examples * jsonDB and many other useful skills are not included in the manual. * It is expected that powerful netizens will explore the potential use skills of jsonDB together, and share it with you * if it is found in use You can send emails at any time, or share comments via blogs. * In view of your limited strength, we hope to launch netizens to expand jsonDB together, * contribute to future front-end applications + ----------------------------------------------- */(function (window, undefined) {_ jsonDB = window. jsonDB, _ history = null, DBCore = {}, Database = {}, DBExpand = {};/*** [jsonDB initialization model, support defining local databases and data tables] * @ param mixed data * @ param string dbName database name * @ return jsonDB */var jsonDB = function (data, dbName) {// create a database or data table if (data) {dbName = d BName | 'json _ db'; eval ('database. '+ dbName +' = data');} return jsonDB. fn. init ();} jsonDB. fn = jsonDB. prototype = {// initialization plug-in init: function (alias) {if (alias) {_ history = window [alias]; window [alias] = jsonDB ();} return this;}, query: function (SQL) {var type = SQL. match (/^ (\ w +)/); switch (type [0]) {case 'select': case 'delete': case 'update ': return eval ('dbcore. fn. '+ type [0] +' (SQL + "") '); break; default: ret Urn false; break ;}, insert: function (data, dbName) {if (data) {dbName = dbName | 'json _ db'; eval ('database. '+ dbName + '. push (data) ');} return this;}, findAll: function (dbName) {if (dbName) {return eval ('database. '+ dbName) ;}return Database ;};/*** [DBExpand Database Core Function extensions] */DBExpand = DBExpand. prototype = {sqlParam: {fields: '*', table: 'json _ db', where: null, order: null, limit: null,}, add: function (data) {Return this. insert (data, this. sqlParam. table) ;}, select: function () {var SQL = 'select' + this. sqlParam. fields + 'from' + this. sqlParam. table; if (this. sqlParam. where) {SQL + = 'where' + this. sqlParam. where;} if (this. sqlParam. order) {SQL + = 'ORDER BY' + this. sqlParam. order;} if (this. sqlParam. limit) {SQL + = 'limit' + this. sqlParam. limit;} this. clear (); return this. query (SQL) ;}, update: function (data) {if (data. le Ngth <1) {return false;} var SQL = 'update' + this. sqlParam. table + 'set' + data; if (this. sqlParam. where) {SQL + = 'where' + this. sqlParam. where;} if (this. sqlParam. limit) {SQL + = 'limit' + this. sqlParam. limit;} this. clear (); return this. query (SQL) ;}, delete: function () {if (this. sqlParam. where. length <1) {return false;} var SQL = 'delete from' + this. sqlParam. table; if (this. sqlParam. where) {SQL + = 'where' + this. sqlPa Ram. where;} if (this. sqlParam. limit) {SQL + = 'limit' + this. sqlParam. limit;} this. clear (); return this. query (SQL) ;}, drop: function (dbName) {// create a database or data table if (data) {dbName = dbName | 'json _ db '; eval ('database. '+ dbName +' = null');} return this;}, field: function (fields) {if (typeof fields = 'object') {this. sqlParam. fields = fields. join (',');} else {this. sqlParam. fields = fields;} return this;}, table: function (t Able) {this. sqlParam. table = table; return this;}, where: function (where) {this. sqlParam. where = '(' + where + ')'; return this;}, order: function (order) {this. sqlParam. order = order; return this;}, limit: function (limit) {this. sqlParam. limit = limit; return this;}, clear: function () {this. sqlParam. fields = '*'; this. sqlParam. where = null; this. sqlParam. order = null; this. sqlParam. limit = null;}/*** [DBCore Database Core] */DBCore. fn = DBCore. prototype = {SqlRegExp: {fields: '([a-z0-9 _ \, \. \ s \ *] *? \ S +) ', from:' (from \ s + ([a-z0-9 _ \.] +) \ s *)? ', Where :'(?: Where \ s + (\ (. * \) \ s *)? ', Order :'(?: Order \ s + by \ s + ([a-z0-9 _ \, \.] + ))? \ S + (asc | desc | ascnum | descnum )? \ S * ', limit :'(?: Limit \ s + ([0-9 \,] + ))? ', Set:' (set \ s + (.*?) \ S +) ', table:' ([a-z0-9 _ \.] *?) \ S *)? ',}, Select: function (SQL) {var params = {fields: ["*"], from: "json_db", where: "", orderby: [], order: "asc", limit: []}, SqlRegExp = this. sqlRegExp, reg = '^ (select) \ s +' + SqlRegExp. fields + SqlRegExp. from + SqlRegExp. where + SqlRegExp. order + SqlRegExp. limit, sqlReg = new RegExp (reg, 'I'), sqlFields = SQL. match (sqlReg), options = {fields: sqlFields [2]. replace ('',''). split (','), from: (sqlFields [4] = undef Ined )? 'Json _ db': sqlFields [4], where: (sqlFields [5] = undefined )? "True": sqlFields [5]. replace (/([^ \> \ <\! \ =]) = ([^ \ >\< \! \ =])/Gi, '$1 = $2 '). replace (/\ sand \ s/gi ,'&&'). replace (/\ sor \ s/gi, '| '). replace (/'/gi, ''), orderby: (sqlFields [6] = undefined )? []: SqlFields [6]. replace ('',''). split (','), order: (sqlFields [7] = undefined )? "Asc": sqlFields [7], limit: (sqlFields [8] = undefined )? []: SqlFields [8]. replace ('',''). split (',')}; for (I in options) {params [I] = options [I];} var result = []; result = this. filter (params, function (data) {if (options. fields. length = 0 | options. fields [0] = "*") {return data;} var result = {}; for (var I in options. fields) {result [options. fields [I] = data [options. fields [I];} return result;}); result = this. orderBy (result, options); result = this. limit (result, o Ptions); return result;}, update: function (SQL) {var params = {from: "json_db", where: "", limit: [], set: []}, SqlRegExp = this. sqlRegExp, reg = '^ (update) \ s +' + SqlRegExp. table + SqlRegExp. set + SqlRegExp. where + SqlRegExp. limit, sqlReg = new RegExp (reg, 'I'), sqlFields = SQL. match (sqlReg), options = {from: (sqlFields [3] = undefined )? 'Json _ db': sqlFields [3], set: (sqlFields [5] = undefined )? []: SqlFields [5]. replace ('',''). split (','), where: (sqlFields [6] = undefined )? "True": sqlFields [6]. replace (/([^ \> \ <\! \ =]) = ([^ \ >\< \! \ =])/Gi, '$1 = $2 '). replace (/\ sand \ s/gi ,'&&'). replace (/\ sor \ s/gi, '| '). replace (/'/gi, ''), limit: (sqlFields [7] = undefined )? []: SqlFields [7]. replace ('',''). split (',')}; for (I in options) {params [I] = options [I];} var jsonData = eval ('database. '+ options. from), tally = 0, extent = this. extent (params), setLen = options. set. length, affected_rows = 0; if (setLen <1) {return false;} options. where = options. where | "true"; for (var I in jsonData) {with (jsonData [I]) {if (eval (options. where) {if (affected_rows> = extent. start & tally <ext Ent. stop) {for (var j = 0; j <setLen; ++ j) {eval (options. set [j]) ;}++ tally;} else if (tally = extent. stop) {return tally ;}++ affected_rows ;}}return tally ;}, delete: function (SQL) {var params = {from: "json_db", where: "", limit: []}, SqlRegExp = this. sqlRegExp, reg = '^ (delete) \ s +' + SqlRegExp. from + SqlRegExp. where + SqlRegExp. limit, sqlReg = new RegExp (reg, 'I'), sqlFields = SQL. match (sqlReg), options = {from: (sqlF Ields [3] = undefined )? 'Json _ db': sqlFields [3], where: (sqlFields [4] = undefined )? "True": sqlFields [4]. replace (/([^ \> \ <\! \ =]) = ([^ \ >\< \! \ =])/Gi, '$1 = $2 '). replace (/\ sand \ s/gi ,'&&'). replace (/\ sor \ s/gi, '| '). replace (/'/gi, ''), limit: (sqlFields [5] = undefined )? []: SqlFields [5]. replace ('',''). split (',')}; for (I in options) {params [I] = options [I];} var jsonData = eval ('database. '+ options. from + '. concat () '), tally = 0, extent = this. extent (params), affected_rows = 0; options. where = options. where | "true"; for (var I in jsonData) {with (jsonData [I]) {if (eval (options. where) {if (affected_rows> = extent. start & tally <extent. stop) {eval ('database. '+ options. from +'. Splice (I-tally, 1) '); ++ tally;} else if (tally = extent. stop) {return tally ;}++ affected_rows ;}}return tally ;}, filter: function (options, callback) {var jsonData = eval ('database. '+ options. from), result = [], index = 0; options. where = options. where | "true"; for (var I in jsonData) {with (jsonData [I]) {if (eval (options. where) {if (callback) {result [index ++] = callback (jsonData [I]);} else {result [index ++] = jso NData [I] ;}}} return result ;}, orderBy: function (result, options) {if (options. orderby. length = 0) {return result;} result. sort (function (a, B) {switch (options. order. toLowerCase () {case "desc": return (eval ('a. '+ options. orderby [0] + '<B. '+ options. orderby [0])? 1:-1; case "asc": return (eval ('a. '+ options. orderby [0] + '> B. '+ options. orderby [0])? 1:-1; case "descnum": return (eval ('a. '+ options. orderby [0] + '-B. '+ options. orderby [0]); case "ascnum": return (eval ('B. '+ options. orderby [0] + '-. '+ options. orderby [0]) ;}}); return result ;}, limit: function (result, options) {switch (options. limit. length) {case 0: return result; case 1: return result. splice (0, options. limit [0]); case 2: return result. splice (options. limit [0], options. limit [1]) ;}}, extent: function (options) {switch (options. limit. length) {case 0: return {start: 0, stop: 9e + 99}; case 1: return {start: 0, stop: options. limit [0]}; case 2: return {start: options. limit [0], stop: options. limit [1] };}} window. jsonDB = jsonDB; // append Extension function jsonDB. fn = jsonDB. prototype = extend (jsonDB. fn, DBExpand); // method of merging objects function extend () {var paramsLen = arguments. length; if (paramsLen <1) {return false;} var target = arguments [0]; for (var I = 1; I <paramsLen; ++ I) {for (var j in arguments [I]) {target [j] = arguments [I] [j] ;}} return target ;}) (window );
Download source code and example: jsonDB.rar