json 데이터 형식 을 기반 으로 한 간단 한 데이터베이스 - jsonDB
8568 단어 JavaScriptHTM5
/**
+-----------------------------------------
* jsonDB json
+-----------------------------------------
* @description json , ,
* @author (web ) [email protected]
* @date 2014 6 28
* @version 0.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(/([^\>\\\\= extent.start && tally < extent.stop){
for(var j=0;j\\= 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
원본 코드 와 예제 다운로드: jsonDB. rar
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
기초 정리 - 1문자 (String) 숫자 (Number) 불린 (Boolean) null undefined 심볼 (Symbol) 큰정수 (BigInt) 따옴표로 묶어 있어야 함 Not-A-Number - 숫자 데이터 / 숫자로 표...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.