基於json資料格式實現的簡單資料庫——jsonDB

來源:互聯網
上載者:User

基於json資料格式實現的簡單資料庫——jsonDB

/** +----------------------------------------- * jsonDB 基於json資料格式構建的資料模型 +----------------------------------------- * @description對json資料檢索,刪除,查詢和更新 * @author戚銀(web程式猿)thinkercode@sina.com * @date 2014年6月28日 * @version0.1 * @blog http://blog.csdn.net/thinkercode/ +----------------------------------------- * 簡介: * jsonDB是js的一個類庫,是基於json資料格式構建的資料 * 模型實現對json資料增刪改查。jsonDB的構建源自於HTML5 * 本機存放區的一個應用需求,可以通過sql對json資料進行增 * 刪改查,同時該類庫提供強大的where檢索條件,資料排序, * limit查詢條件限制等資料庫準系統。通過jsonDB可以輕 * 松維護一個庫/表或多個庫/表,而無需額外實現json的資料 * 的維護等,在該類庫完善以後為簡化sql操作,基於jsonDB核 * 心模組擴充了連貫操作模型,簡化對jsonDB的操作以及sql語 * 句出錯的機率。 +----------------------------------------- * 目前的版本的不足: * 1.無法支援查詢欄位運算 * 2.不支援對沒有選取的查詢欄位排序 * 3.只支援單個欄位排序,無法進行組合排序操作 * 4.update、delete語句不支援order by子句,導致limit子句功能弱化 * 5.編寫where條件是必須使用()包含where條件 * 6.無法選取深層次的欄位作為返回欄位 * 7.沒有錯誤或異常解決方案 * 8.不支援外部擴充 +----------------------------------------- * jsonDB的使用: * jsonDB會提供一個詳細的使用手冊,其中包含大量樣本 * jsonDB還有很多可以使用的技巧並沒有一一包含在手冊中 * 期望強大的網友一起挖掘jsonDB的潛在提示,並分享給大家 * 如果使用中發現問題可以隨時發送郵件,或者通過部落格留言等方式一起探討 * 同時鑒於自己實力有限,期望發動網友一起擴充jsonDB, * 能在未來前端應用中奉獻一份力量 +----------------------------------------- */(function(window,undefined){_jsonDB = window.jsonDB,_history = null,DBCore = {},Database = {},DBExpand = {};/** * [jsonDB 初始化模型,支援定義本機資料庫和資料表] * @param  mixed data 資料 * @param  string dbName資料庫名字 * @return jsonDB */var jsonDB = function(data,dbName){//建立資料庫或者資料表if(data){dbName = dbName || 'json_db';eval('Database.'+ dbName +'= data');}return jsonDB.fn.init();}jsonDB.fn = jsonDB.prototype = {//初始化外掛程式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 :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 資料庫核心功能擴充] */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){//建立資料庫或者資料表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 資料庫核心] */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] == undefined) ? '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,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(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 < 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,'$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++] = 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:options.limit[0]};case 2: return {start:options.limit[0], stop:options.limit[1]};}}}window.jsonDB = jsonDB;//追加擴充功能jsonDB.fn = jsonDB.prototype =extend(jsonDB.fn,DBExpand);//合并對象方法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);


源碼與樣本下載:jsonDB.rar


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.