JsonQuery.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dojox.data.util.JsonQuery"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dojox.data.util.JsonQuery"] = true;
  8. dojo.provide("dojox.data.util.JsonQuery");
  9. // this is a mixin to convert object attribute queries to
  10. // JSONQuery/JSONPath syntax to be sent to the server.
  11. dojo.declare("dojox.data.util.JsonQuery", null, {
  12. useFullIdInQueries: false,
  13. _toJsonQuery: function(args, jsonQueryPagination){
  14. var first = true;
  15. var self = this;
  16. function buildQuery(path, query){
  17. var isDataItem = query.__id;
  18. if(isDataItem){
  19. // it is a reference to a persisted object, need to make it a query by id
  20. var newQuery = {};
  21. newQuery[self.idAttribute] = self.useFullIdInQueries ? query.__id : query[self.idAttribute];
  22. query = newQuery;
  23. }
  24. for(var i in query){
  25. // iterate through each property, adding them to the overall query
  26. var value = query[i];
  27. var newPath = path + (/^[a-zA-Z_][\w_]*$/.test(i) ? '.' + i : '[' + dojo._escapeString(i) + ']');
  28. if(value && typeof value == "object"){
  29. buildQuery(newPath, value);
  30. }else if(value!="*"){ // full wildcards can be ommitted
  31. jsonQuery += (first ? "" : "&") + newPath +
  32. ((!isDataItem && typeof value == "string" && args.queryOptions && args.queryOptions.ignoreCase) ? "~" : "=") +
  33. (self.simplifiedQuery ? encodeURIComponent(value) : dojo.toJson(value));
  34. first = false;
  35. }
  36. }
  37. }
  38. // performs conversion of Dojo Data query objects and sort arrays to JSONQuery strings
  39. if(args.query && typeof args.query == "object"){
  40. // convert Dojo Data query objects to JSONQuery
  41. var jsonQuery = "[?(";
  42. buildQuery("@", args.query);
  43. if(!first){
  44. // use ' instead of " for quoting in JSONQuery, and end with ]
  45. jsonQuery += ")]";
  46. }else{
  47. jsonQuery = "";
  48. }
  49. args.queryStr = jsonQuery.replace(/\\"|"/g,function(t){return t == '"' ? "'" : t;});
  50. }else if(!args.query || args.query == '*'){
  51. args.query = "";
  52. }
  53. var sort = args.sort;
  54. if(sort){
  55. // if we have a sort order, add that to the JSONQuery expression
  56. args.queryStr = args.queryStr || (typeof args.query == 'string' ? args.query : "");
  57. first = true;
  58. for(i = 0; i < sort.length; i++){
  59. args.queryStr += (first ? '[' : ',') + (sort[i].descending ? '\\' : '/') + "@[" + dojo._escapeString(sort[i].attribute) + "]";
  60. first = false;
  61. }
  62. args.queryStr += ']';
  63. }
  64. // this is optional because with client side paging JSONQuery doesn't yield the total count
  65. if(jsonQueryPagination && (args.start || args.count)){
  66. // pagination
  67. args.queryStr = (args.queryStr || (typeof args.query == 'string' ? args.query : "")) +
  68. '[' + (args.start || '') + ':' + (args.count ? (args.start || 0) + args.count : '') + ']';
  69. }
  70. if(typeof args.queryStr == 'string'){
  71. args.queryStr = args.queryStr.replace(/\\"|"/g,function(t){return t == '"' ? "'" : t;});
  72. return args.queryStr;
  73. }
  74. return args.query;
  75. },
  76. jsonQueryPagination: true,
  77. fetch: function(args){
  78. this._toJsonQuery(args, this.jsonQueryPagination);
  79. return this.inherited(arguments);
  80. },
  81. isUpdateable: function(){
  82. return true;
  83. },
  84. matchesQuery: function(item,request){
  85. request._jsonQuery = request._jsonQuery || dojox.json.query(this._toJsonQuery(request));
  86. return request._jsonQuery([item]).length;
  87. },
  88. clientSideFetch: function(/*Object*/ request,/*Array*/ baseResults){
  89. request._jsonQuery = request._jsonQuery || dojox.json.query(this._toJsonQuery(request));
  90. // we use client side paging function here instead of JSON Query because we must also determine the total count
  91. return this.clientSidePaging(request, request._jsonQuery(baseResults));
  92. },
  93. querySuperSet: function(argsSuper,argsSub){
  94. if(!argsSuper.query){
  95. return argsSub.query;
  96. }
  97. return this.inherited(arguments);
  98. }
  99. });
  100. }