PageRenderTime 57ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/ext/protocols/couchdb/Result.hx

http://caffeine-hx.googlecode.com/
Haxe | 219 lines | 117 code | 21 blank | 81 comment | 21 complexity | e710e25b04b5cf5e1ad02b0cf309e575 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 protocols.couchdb;
  28. import formats.json.JsonObject;
  29. import formats.json.JsonArray;
  30. /**
  31. A Result set for a query, much like that in any relational database.
  32. **/
  33. class Result extends Document {
  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("ok")) {
  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. for(i in 0...l) {
  121. if(a.get(i) != null && a.getString(i) != "null") {
  122. var dr = new Row(this.database, a.getJsonObject(i));
  123. dr.setDatabase(database);
  124. rv.add(dr);
  125. }
  126. }
  127. return rv;
  128. }
  129. public function getRowCount() : Int {
  130. if(rowCount == null) {
  131. if(!has("rows")) {
  132. rowCount = 0;
  133. }
  134. else {
  135. var a = getJsonArray("rows");
  136. rowCount = a.length;
  137. }
  138. }
  139. return rowCount;
  140. }
  141. /**
  142. Returns each row as a string representation of the Json object. Useful
  143. for debugging the result set, to see if the objects you are expecting
  144. from the query actually exist.
  145. **/
  146. public function getStrings() : List<String>
  147. {
  148. var rv = new List<String>();
  149. if(!ok)
  150. return rv;
  151. var a = getJsonArray("rows");
  152. var l = a.length;
  153. for(i in 0...l) {
  154. if(a.get(i) != null && a.getString(i) != "null") {
  155. rv.add(Std.string(a.getJsonObject(i)));
  156. }
  157. }
  158. return rv;
  159. }
  160. /**
  161. This will be true if there was no error during the query.
  162. **/
  163. public function isOk() {
  164. return ok;
  165. }
  166. /**
  167. Only available if the view has a reduce function, but is
  168. also stored as the first row.value
  169. **/
  170. public function getValue() : Dynamic {
  171. return value;
  172. }
  173. /**
  174. The view this result set was generated from.
  175. **/
  176. public function getView(?foo:String) : View {
  177. return this.view;
  178. }
  179. /**
  180. The error code
  181. **/
  182. public function getErrorId() : String {
  183. return errId;
  184. }
  185. /**
  186. Error message
  187. **/
  188. public function getErrorReason() : String {
  189. return errReason;
  190. }
  191. /**
  192. The number of rows in the result set. Also available as the
  193. property 'rowCount'.
  194. **/
  195. public function numRows() : Int {
  196. return rowCount;
  197. }
  198. }