Simple database--jsondb based on JSON data format implementation

Source: Internet
Author: User

/** +-----------------------------------------* JSONDB data Model built based on JSON data format +---------------------------------------- -* @description to JSON data retrieval, deletion, query and update * @author Bang (Web program Ape) [email protected] * @date June 28, 2014 * @version0.1 * @blog http ://blog.csdn.net/thinkercode/+-----------------------------------------* Introduction: * JSONDB is a class library of JS, is based on the JSON data format built data * Model implementation of JSON data additions and deletions to change. The JSONDB is built from an application requirement of HTML5 * local storage, which can be used to add/revise JSON data through SQL, and the library provides a strong where retrieval condition, data sorting, and limit query condition restrictions and other basic database functions. With JSONDB, you can maintain a library/table or multiple libraries/tables, without the need for additional JSON data * maintenance, to simplify SQL operations after the class library is complete, and to simplify operations on the JSONDB core module, simplifying the operation of the jsondb and the SQL language * The probability of an error in the sentence. +-----------------------------------------* The current version of the insufficient: * 1. Query field operations cannot be supported * 2. Do not support ordering of query fields that are not selected * 3. Only single field sorting is supported, no combined sort operation * 4. The update, DELETE statement does not support the ORDER BY clause, which causes the limit clause to weaken * 5. Writing a Where condition is required to include the Where condition * 6. Unable to select Deep Field as Return field * 7. No error or exception solution * 8. The use of external extensions +-----------------------------------------* JSONDB is not supported: * JSONDB will provide a detailed user manual with a large number of examples * Jsondb There are a lot of tricks that can be used and not one by one included in the manual * expect powerful netizens to explore the potential use of jsondb together and share to everyone * if you find problems in use can alwayse-mail, or through blog comments and other ways to explore * at the same time, in view of their limited strength, expect to launch a netizen to expand JSONDB, * can be in the future front-end application to dedicate a force +-----------------------------------------* * ( function (window,undefined) {_jsondb = Window.jsondb,_history = Null,dbcore = {},database = {},dbexpand = {};/** * [JsonDB] Start model, support definition of local database and data table] * @param mixed data * @param string dbname database name * @return jsondb */var jsondb = function (data,dbn AME) {//CREATE DATABASE or datasheet if (data) {dbName = DbName | | ' json_db '; eval (' Database. ') + dbName + ' = Data ');} return JsonDB.fn.init ();} Jsondb.fn = Jsondb.prototype = {//initialize 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 ' up ' Date ': Return eval (' Dbcore.fn. ') + type[0] + ' (sql+ ""), Break;default:return 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 extension] */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.length<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.sqlParam.where;} if (this.sqlParam.limit) {sql + = ' limit ' +this.sqlparam.limit;} This.clear (); return this.query (SQL);},drop:function (dbName) {//CREATE DATABASE or datasheet 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 (table) {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: ' (?: o Rder\\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] = = undefined)? ' json_db ': Sqlfields[4],where: (sqlfields[5] = = undefined)? "True": Sqlfields[5].replace (/([^\>\<\!\=]) = ([^\>\<\!\=])/gi, ' $ = = $ '). Replace (/\sand\s/gi, ' & & '). Replace (/\sor\s/gi, ' | | '). Replace (/'/gi, '), by: (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] = = "*") {RET Urn 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,options); 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 (r eg, ' 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, ' $ = = $ '). 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,affect Ed_rows = 0;if (setlen<1) {return false;} Options.where = Options.where | | "True"; for (var i-Jsondata) {with (Jsondata[i]) {if (eval (options.where)) {if (Affected_rows >= Extent.start & & Tally < Extent.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: (sqlfields[3] = = undefined)? ' json_db ': Sqlfields[3],where: (sqlfields[4] = = undefined)? "True": Sqlfields[4].replace (/([^\>\<\!\=])= ([^\>\<\!\=])/gi, ' $ = = $ '). 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;opt Ions.where = Options.where | | "True"; for (var i-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-Jsondata) {with (Jsondata[i]) {if (eval (options.where)) {if (callback) {result[index++] = callback ( Jsondata[i]);} else{result[index++] = Jsondata[i];}}} Return Result;},ordErby:function (result,options) {if (options.orderby.length = = 0) {return result;} Result.sort (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] + '-A. ' + 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:o Ptions.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);//Merge Object method 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);


source code and sample download: Jsondb.rar


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.