1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | var jsonsql = {
|
23 |
|
24 | query: function(sql,json){
|
25 |
|
26 | var returnfields = sql.match(/^(select)\s+([a-z0-9_\,\.\s\*]+)\s+from\s+([a-z0-9_\.]+)(?: where\s+\((.+)\))?\s*(?:order\sby\s+([a-z0-9_\,]+))?\s*(asc|desc|ascnum|descnum)?\s*(?:limit\s+([0-9_\,]+))?/i);
|
27 |
|
28 | var ops = {
|
29 | fields: returnfields[2].replace(' ','').split(','),
|
30 | from: returnfields[3].replace(' ',''),
|
31 | where: (returnfields[4] == undefined)? "true":returnfields[4],
|
32 | orderby: (returnfields[5] == undefined)? []:returnfields[5].replace(' ','').split(','),
|
33 | order: (returnfields[6] == undefined)? "asc":returnfields[6],
|
34 | limit: (returnfields[7] == undefined)? []:returnfields[7].replace(' ','').split(',')
|
35 | };
|
36 |
|
37 | return this.parse(json, ops);
|
38 | },
|
39 |
|
40 | parse: function(json,ops){
|
41 | var o = { fields:["*"], from:"json", where:"", orderby:[], order: "asc", limit:[] };
|
42 | for(i in ops) o[i] = ops[i];
|
43 |
|
44 | var result = [];
|
45 | result = this.returnFilter(json,o);
|
46 | result = this.returnOrderBy(result,o.orderby,o.order);
|
47 | result = this.returnLimit(result,o.limit);
|
48 |
|
49 | return result;
|
50 | },
|
51 |
|
52 | returnFilter: function(json,jsonsql_o){
|
53 |
|
54 | var jsonsql_scope = eval(jsonsql_o.from);
|
55 | var jsonsql_result = [];
|
56 | var jsonsql_rc = 0;
|
57 |
|
58 | if(jsonsql_o.where == "")
|
59 | jsonsql_o.where = "true";
|
60 |
|
61 | for(var jsonsql_i in jsonsql_scope){
|
62 | with(jsonsql_scope[jsonsql_i]){
|
63 | if(eval(jsonsql_o.where)){
|
64 | jsonsql_result[jsonsql_rc++] = this.returnFields(jsonsql_scope[jsonsql_i],jsonsql_o.fields);
|
65 | }
|
66 | }
|
67 | }
|
68 |
|
69 | return jsonsql_result;
|
70 | },
|
71 |
|
72 | returnFields: function(scope,fields){
|
73 | if(fields.length == 0)
|
74 | fields = ["*"];
|
75 |
|
76 | if(fields[0] == "*")
|
77 | return scope;
|
78 |
|
79 | var returnobj = {};
|
80 | for(var i in fields)
|
81 | returnobj[fields[i]] = scope[fields[i]];
|
82 |
|
83 | return returnobj;
|
84 | },
|
85 |
|
86 | returnOrderBy: function(result,orderby,order){
|
87 | if(orderby.length == 0)
|
88 | return result;
|
89 |
|
90 | result.sort(function(a,b){
|
91 | switch(order.toLowerCase()){
|
92 | case "desc": return (eval('a.'+ orderby[0] +' < b.'+ orderby[0]))? 1:-1;
|
93 | case "asc": return (eval('a.'+ orderby[0] +' > b.'+ orderby[0]))? 1:-1;
|
94 | case "descnum": return (eval('a.'+ orderby[0] +' - b.'+ orderby[0]));
|
95 | case "ascnum": return (eval('b.'+ orderby[0] +' - a.'+ orderby[0]));
|
96 | }
|
97 | });
|
98 |
|
99 | return result;
|
100 | },
|
101 |
|
102 | returnLimit: function(result,limit){
|
103 | switch(limit.length){
|
104 | case 0: return result;
|
105 | case 1: return result.splice(0,limit[0]);
|
106 | case 2: return result.splice(limit[0]-1,limit[1]);
|
107 | }
|
108 | }
|
109 |
|
110 | }; |
\ | No newline at end of file |