PageRenderTime 56ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/ext3/chx/protocols/memedb/Result.hx

http://caffeine-hx.googlecode.com/
Haxe | 221 lines | 119 code | 21 blank | 81 comment | 21 complexity | af54d4373197fa16329705b616e92773 MD5 | raw file
Possible License(s): GPL-2.0, Apache-2.0, BSD-3-Clause, LGPL-2.1
  1. /*
  2. * Copyright (c) 2008, The Caffeine-hx project contributors
  3. * Original author : Russell Weir
  4. * Contributors:
  5. * All rights reserved.
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * - Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * - Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE CAFFEINE-HX PROJECT CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. * ARE DISCLAIMED. IN NO EVENT SHALL THE CAFFEINE-HX PROJECT CONTRIBUTORS
  19. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  20. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  21. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  22. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  23. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  24. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  25. * THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. package chx.protocols.memedb;
  28. import chx.formats.json.JSONObject;
  29. import chx.formats.json.JSONArray;
  30. /**
  31. A Result set for a query, much like that in any relational database.
  32. **/
  33. class Result extends JSONDocument {
  34. // only for debugging.
  35. static var SAVE_OUTPUT_REQUEST : Bool = false;
  36. public var rowCount(getRowCount, null) : Null<Int>;
  37. public var offset(default, null) : Int;
  38. public var ok(default,null) : Bool;
  39. /** only available if the view has a reduce function, but is
  40. also stored as the first row.value **/
  41. public var value(default,null) : Dynamic;
  42. private var view : View;
  43. private var result : Dynamic;
  44. private var errId : String;
  45. private var errReason : String;
  46. private var outputRequest : String;
  47. public function new(d: Database, v : View, t : Transaction) {
  48. /**
  49. Reduced result:
  50. {"ok":true,"result":491}
  51. Mapped result:
  52. {
  53. "offset":0,
  54. "total_rows":10,
  55. "rows":[{"id":"0","key":"0","value":?}, ...]
  56. }
  57. **/
  58. super();
  59. if(SAVE_OUTPUT_REQUEST)
  60. this.outputRequest = untyped t.http.requestText;
  61. this.database = d;
  62. this.view = v;
  63. if(!t.isOk()) {
  64. try {
  65. this.data = t.getObject();
  66. }
  67. catch(e:Dynamic) { this.data = {}; };
  68. this.errId = optString("error","unknown error");
  69. this.errReason = optString("reason","unknown error");
  70. this.ok = false;
  71. this.rowCount = 0;
  72. this.offset = 0;
  73. }
  74. else {
  75. this.data = t.getObject();
  76. this.errId = "";
  77. this.errReason = "";
  78. this.ok = true;
  79. // a reduced set
  80. if(has("result")) {
  81. // I assume here that the result field will _not_ be JSONObject.
  82. // therefore, the success of this try is a fatal error on my part.
  83. try {
  84. getJSONObject("result");
  85. throw "Unhandled exception. Contact developers.";
  86. } catch(e : Dynamic) {}
  87. this.rowCount = 1;
  88. var v = get("result");
  89. value = v;
  90. this.data =
  91. {
  92. rows : [{id: null, key:null, value: v}]
  93. };
  94. }
  95. else {
  96. // this is not correct in any view that defines a startkey or endkey, it
  97. // returns the total rows in the database
  98. // this.rowCount = getInt("total_rows");
  99. // the Null<rowCount> will not be updated unless it is requested
  100. this.rowCount = null;
  101. this.offset = optInt("offset",0);
  102. }
  103. }
  104. }
  105. /**
  106. Retrieves a list of Rows that matched this View.
  107. **/
  108. public function getRows() : List<Row>
  109. { //{"total_rows":1000,"offset":0,"rows":[
  110. //{"id":"0","key":"0","value":{"rev":"2922574358"}}, ...
  111. var rv = new List<Row>();
  112. if(!ok)
  113. return rv;
  114. var a : JSONArray;
  115. //try {
  116. a = getJSONArray("rows");
  117. //} catch(e : Dynamic) { return rv; }
  118. trace(Std.string(a));
  119. var l = a.length;
  120. trace(l);
  121. for(i in 0...l) {
  122. if(a.get(i) != null && a.getString(i) != "null") {
  123. var dr = new Row(this.database, a.getJSONObject(i));
  124. //trace(Std.string(dr));
  125. dr.setDatabase(database);
  126. rv.add(dr);
  127. }
  128. }
  129. return rv;
  130. }
  131. public function getRowCount() : Int {
  132. if(rowCount == null) {
  133. if(!has("rows")) {
  134. rowCount = 0;
  135. }
  136. else {
  137. var a = getJSONArray("rows");
  138. rowCount = a.length;
  139. }
  140. }
  141. return rowCount;
  142. }
  143. /**
  144. Returns each row as a string representation of the Json object. Useful
  145. for debugging the result set, to see if the objects you are expecting
  146. from the query actually exist.
  147. **/
  148. public function getStrings() : List<String>
  149. {
  150. var rv = new List<String>();
  151. if(!ok)
  152. return rv;
  153. var a = getJSONArray("rows");
  154. var l = a.length;
  155. for(i in 0...l) {
  156. if(a.get(i) != null && a.getString(i) != "null") {
  157. rv.add(Std.string(a.getJSONObject(i)));
  158. }
  159. }
  160. return rv;
  161. }
  162. /**
  163. This will be true if there was no error during the query.
  164. **/
  165. public function isOk() {
  166. return ok;
  167. }
  168. /**
  169. Only available if the view has a reduce function, but is
  170. also stored as the first row.value
  171. **/
  172. public function getValue() : Dynamic {
  173. return value;
  174. }
  175. /**
  176. The view this result set was generated from.
  177. **/
  178. public function getView(?foo:String) : View {
  179. return this.view;
  180. }
  181. /**
  182. The error code
  183. **/
  184. public function getErrorId() : String {
  185. return errId;
  186. }
  187. /**
  188. Error message
  189. **/
  190. public function getErrorReason() : String {
  191. return errReason;
  192. }
  193. /**
  194. The number of rows in the result set. Also available as the
  195. property 'rowCount'.
  196. **/
  197. public function numRows() : Int {
  198. return rowCount;
  199. }
  200. }