Simple database--jsondb based on JSON data format implementation

Source: Internet
Author: User
Tags eval

Created a project on GitHub: Https://github.com/ThinkerCodeChina/jsonDB
/** +-----------------------------------------* JSONDB data Model built based on JSON data format +---------------------------------------- -* @description to JSON data retrieval, deletion, query and update * @author Bang (web programmer) [email protected] * @date June 28, 2014 * @version0.1 * @blog HTTP://BL og.csdn.net/thinkercode/+-----------------------------------------* Simple 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 at the same time the library provides a strong where retrieval condition, data sorting, * Limit query condition restrictions and other basic database functions.

With JSONDB, you can maintain a library/table or multiple libraries/tables with light * slack. Without the need for additional JSON-based maintenance of data *, in order to simplify SQL operations after the library is intact, the JSONDB core module expands the coherent operation model, simplifying the operation of JSONDB and the probability of a SQL sentence error. +-----------------------------------------* The current version number is insufficient: * 1. Cannot support query Field Operation * 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. Causes the LIMIT clause to weaken * 5. Write where condition is must use () include Where Condition * 6. Unable to select Deep Field as Return field * 7. No error or exception resolution * 8. External extensions +-------------------------are not supported ----------------* JSONDB Use: * JSONDB will provide a specific manual, including a large number of demo samples * Jsondb There are a lot of skills that can be used and not included in the manual * Hope that powerful netizens together to explore the potential use of JSONDB skills. and share to everyone * assume that the use of found problems can be sent at any time mail, or through blog messages and other ways to explore together * at the same time, due to their limited strength, expect to launch a netizen to expand Jsondb. * The ability to dedicate a share of power in future front-end applications +-----------------------------------------*/function (window,undefined) {_jsondb = Window.jsondb, _history = Null,dbcore = {},database = {},dbexpand = {};/** * [JSONDB Initialize the model. Supports definition of local database and data table] * @param mixed data * @param string dbName database name * @return jsondb */var jsondb = function (data,dbname) { 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;} IfThis.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 (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] + '-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 Demo sample download: Jsondb.rar


Simple database--jsondb based on JSON data format implementation

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.