ComparisonMatchExpression.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. "use strict";
  2. LeafMatchExpression = require('LeafMatchExpression');
  3. Value = require('../Value');
  4. // Autogenerated by cport.py on 2013-09-17 14:37
  5. var ComparisonMatchExpression = module.exports = function ComparisonMatchExpression( type ){
  6. this._matchType = type;
  7. }, klass = ComparisonMatchExpression, base = LeafMatchExpression, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  8. // File: expression_leaf.h lines: 88-88
  9. // BSONElement _rhs;
  10. proto._rhs = undefined;
  11. /**
  12. *
  13. * This documentation was automatically generated. Please update when you touch this function.
  14. * @method debugString
  15. * @param
  16. *
  17. */
  18. proto.debugString = function debugString( /* StringBuilder& debug, int level */ ){
  19. // File: expression_leaf.cpp lines: 135-154
  20. var retStr = this._debugAddSpace( level ) + this.path() + " ";
  21. switch (this._matchType){
  22. case 'LT':
  23. retStr += '$lt';
  24. break;
  25. case 'LTE':
  26. retStr += '$lte';
  27. break;
  28. case 'EQ':
  29. retStr += '==';
  30. break;
  31. case 'GT':
  32. retStr += '$gt';
  33. break;
  34. case 'GTE':
  35. retStr += '$gte';
  36. break;
  37. default:
  38. retStr += "Unknown comparison!";
  39. break;
  40. }
  41. retStr += this._rhs.toString();
  42. if (this.getTag() !== null ) {
  43. retStr += this.getTag().debugString();
  44. }
  45. return retStr + '\n';
  46. };
  47. /**
  48. *
  49. * This documentation was automatically generated. Please update when you touch this function.
  50. * @method equivalent
  51. * @param
  52. *
  53. */
  54. proto.equivalent = function equivalent( other ) {// const MatchExpression* other
  55. // File: expression_leaf.cpp lines: 53-61
  56. // bool ComparisonMatchExpression::equivalent( const MatchExpression* other ) const {
  57. // if ( other->matchType() != matchType() )
  58. // return false;
  59. // const ComparisonMatchExpression* realOther =
  60. // static_cast<const ComparisonMatchExpression*>( other );
  61. //
  62. // return
  63. // path() == realOther->path() &&
  64. // _rhs.valuesEqual( realOther->_rhs );
  65. // }
  66. if (other._matchType != this._matchType) { return false; }
  67. return this.path() == other.path() && Values.compare(this._rhs,other._rhs);
  68. };
  69. /**
  70. *
  71. * This documentation was automatically generated. Please update when you touch this function.
  72. * @method getData
  73. * @param
  74. *
  75. */
  76. proto.getData = function getData( /* */ ){
  77. // File: expression_leaf.h lines: 85-84
  78. return this._rhs;
  79. };
  80. /**
  81. *
  82. * This documentation was automatically generated. Please update when you touch this function.
  83. * @method getRHS
  84. * @param
  85. *
  86. */
  87. proto.getRHS = function getRHS( /* */ ){
  88. // File: expression_leaf.h lines: 79-78
  89. return this._rhs;
  90. };
  91. /**
  92. *
  93. * This documentation was automatically generated. Please update when you touch this function.
  94. * @method init
  95. * @param
  96. *
  97. */
  98. proto.init = function init( path,rhs ) { // const StringData& path, const BSONElement& rhs
  99. // File: expression_leaf.cpp lines: 65-87
  100. this._rhs = rhs;
  101. if ( rhs === {}) { return {'code':'BAD_VALUE', 'desc':'Need a real operand'};}
  102. if ( rhs === undefined ) { return {'code':'BAD_VALUE', 'desc':'Cannot compare to undefined'};}
  103. if (!(this._matchType in {"LT":1, "LTE":1, "EQ":1, "GT":1, "GTE":1})) {
  104. return {'code':'BAD_VALUE', 'desc':'Bad match type for ComparisonMatchExpression'};
  105. }
  106. return this.initPath( path );
  107. };
  108. /**
  109. *
  110. * This documentation was automatically generated. Please update when you touch this function.
  111. * @method matchesSingleElement
  112. * @param
  113. *
  114. */
  115. proto.matchesSingleElement = function matchesSingleElement( e ){ // const BSONElement& e
  116. // File: expression_leaf.cpp lines: 91-132
  117. if( typeof(e) != typeof(this._rhs) ){
  118. if ((e === null || e === undefined) && (this._rhs ===null || this._rhs === undefined)) {
  119. return ["EQ","LTE","GTE"].indexOf(this._matchType) != -1;
  120. }
  121. if (this._rhs instanceof MaxKey || this._rhs instanceof MinKey) {
  122. return this._matchType != "EQ";
  123. }
  124. return false;
  125. }
  126. if( this._rhs instanceof Array) {
  127. if( this._matchType != 'EQ') {
  128. return false;
  129. }
  130. }
  131. var x = Value.compare( e, this._rhs );
  132. switch( this._matchType ) {
  133. case "LT":
  134. return x == -1;
  135. break;
  136. case "LTE":
  137. return x <= 0;
  138. break;
  139. case "EQ":
  140. return x === 0;
  141. break;
  142. case "GT":
  143. return x == 1;
  144. break;
  145. case "GTE":
  146. return x >= 0;
  147. break;
  148. default:
  149. throw new Error("Invalid comparison type evaluated.");
  150. break;
  151. }
  152. return false;
  153. };