ComparisonMatchExpression.js 4.4 KB

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