jsonsql.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * JsonSQL
  3. * By: Trent Richardson [http://trentrichardson.com]
  4. * Version 0.1
  5. * Last Modified: 1/1/2008
  6. *
  7. * Copyright 2008 Trent Richardson
  8. *
  9. * Licensed under the Apache License, Version 2.0 (the "License");
  10. * you may not use this file except in compliance with the License.
  11. * You may obtain a copy of the License at
  12. *
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. * Unless required by applicable law or agreed to in writing, software
  16. * distributed under the License is distributed on an "AS IS" BASIS,
  17. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. * See the License for the specific language governing permissions and
  19. * limitations under the License.
  20. */
  21. window.jsonsql = {
  22. query: function(sql,json){
  23. 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);
  24. var ops = {
  25. fields: returnfields[2].replace(' ','').split(','),
  26. from: returnfields[3].replace(' ',''),
  27. where: (returnfields[4] == undefined)? "true":returnfields[4],
  28. orderby: (returnfields[5] == undefined)? []:returnfields[5].replace(' ','').split(','),
  29. order: (returnfields[6] == undefined)? "asc":returnfields[6],
  30. limit: (returnfields[7] == undefined)? []:returnfields[7].replace(' ','').split(',')
  31. };
  32. return this.parse(json, ops);
  33. },
  34. parse: function(json,ops){
  35. var o = { fields:["*"], from:"json", where:"", orderby:[], order: "asc", limit:[] };
  36. for(i in ops) o[i] = ops[i];
  37. var result = [];
  38. result = this.returnFilter(json,o);
  39. result = this.returnOrderBy(result,o.orderby,o.order);
  40. result = this.returnLimit(result,o.limit);
  41. return result;
  42. },
  43. returnFilter: function(json,jsonsql_o){
  44. var jsonsql_scope = eval(jsonsql_o.from);
  45. var jsonsql_result = [];
  46. var jsonsql_rc = 0;
  47. if(jsonsql_o.where == "")
  48. jsonsql_o.where = "true";
  49. for(var jsonsql_i in jsonsql_scope){
  50. with(jsonsql_scope[jsonsql_i]){
  51. if(eval(jsonsql_o.where)){
  52. jsonsql_result[jsonsql_rc++] = this.returnFields(jsonsql_scope[jsonsql_i],jsonsql_o.fields);
  53. }
  54. }
  55. }
  56. return jsonsql_result;
  57. },
  58. returnFields: function(scope,fields){
  59. if(fields.length == 0)
  60. fields = ["*"];
  61. if(fields[0] == "*")
  62. return scope;
  63. var returnobj = {};
  64. for(var i in fields)
  65. returnobj[fields[i]] = scope[fields[i]];
  66. return returnobj;
  67. },
  68. returnOrderBy: function(result,orderby,order){
  69. if(orderby.length == 0)
  70. return result;
  71. result.sort(function(a,b){
  72. switch(order.toLowerCase()){
  73. case "desc": return (eval('a.'+ orderby[0] +' < b.'+ orderby[0]))? 1:-1;
  74. case "asc": return (eval('a.'+ orderby[0] +' > b.'+ orderby[0]))? 1:-1;
  75. case "descnum": return (eval('a.'+ orderby[0] +' - b.'+ orderby[0]));
  76. case "ascnum": return (eval('b.'+ orderby[0] +' - a.'+ orderby[0]));
  77. }
  78. });
  79. return result;
  80. },
  81. returnLimit: function(result,limit){
  82. switch(limit.length){
  83. case 0: return result;
  84. case 1: return result.splice(0,limit[0]);
  85. case 2: return result.splice(limit[0]-1,limit[1]);
  86. }
  87. }
  88. };