WhereMatchExpression.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. "use strict";
  2. /**
  3. *
  4. * This documentation was automatically generated. Please update when you touch this function.
  5. * @method WhereMatchExpression
  6. * @param WHERE
  7. *
  8. */
  9. // Autogenerated by cport.py on 2013-09-17 14:37
  10. var WhereMatchExpression = module.exports = function() {
  11. this._func = 0;
  12. }, klass = WhereMatchExpression, base = Object, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  13. // DEPENDENCIES
  14. var errors = require("../../Errors.js"),
  15. ErrorCodes = errors.ErrorCodes;
  16. // File: expression_where.cpp lines: 55-55
  17. // string _code;
  18. proto._code = undefined;
  19. // File: expression_where.cpp lines: 59-59
  20. // ScriptingFunction _func;
  21. proto._func = undefined;
  22. // File: expression_where.cpp lines: 54-54
  23. // string _ns;
  24. proto._ns = undefined;
  25. // File: expression_where.cpp lines: 58-58
  26. // auto_ptr<Scope> _scope;
  27. proto._scope = undefined;
  28. // File: expression_where.cpp lines: 56-56
  29. // BSONObj _userScope;
  30. proto._userScope = undefined;
  31. // File: expression_where.cpp lines: 55-55
  32. // string _code;
  33. proto._code = undefined;
  34. // File: expression_where.cpp lines: 59-59
  35. // ScriptingFunction _func;
  36. proto._func = undefined;
  37. // File: expression_where.cpp lines: 54-54
  38. // string _ns;
  39. proto._ns = undefined;
  40. // File: expression_where.cpp lines: 58-58
  41. // auto_ptr<Scope> _scope;
  42. proto._scope = undefined;
  43. // File: expression_where.cpp lines: 56-56
  44. // BSONObj _userScope;
  45. proto._userScope = undefined;
  46. /**
  47. *
  48. * This documentation was automatically generated. Please update when you touch this function.
  49. * @method debugString
  50. * @param debug
  51. * @param level
  52. *
  53. */
  54. proto.debugString = function debugString( debug, level){
  55. // File: expression_where.cpp lines: 113-124
  56. /*
  57. proto._debugAddSpace( debug, level );
  58. debug = debug + "$where\n";
  59. proto._debugAddSpace( debug, level + 1 );
  60. debug = debug + "ns: " + proto._ns + "\n";
  61. proto._debugAddSpace( debug, level + 1 );
  62. debug = debug + "code: " + proto._code + "\n";
  63. proto._debugAddSpace( debug, level + 1 );
  64. debug = debug + "scope: " + proto._userScope + "\n";
  65. console.log(debug);
  66. */
  67. };
  68. /**
  69. *
  70. * This documentation was automatically generated. Please update when you touch this function.
  71. * @method equivalent
  72. * @param other
  73. *
  74. */
  75. proto.equivalent = function equivalent(other){
  76. // File: expression_where.cpp lines: 127-134
  77. if ( this.matchType() != other.matchType() ) return false;
  78. var realOther = new WhereMatchExpression(other);
  79. return (this._ns == realOther._ns && this._code == realOther._code && this._userScope == realOther._userScope);
  80. };
  81. /**
  82. *
  83. * This documentation was automatically generated. Please update when you touch this function.
  84. * @method init
  85. * @param string ns
  86. * @param theCode
  87. * @param scope
  88. *
  89. */
  90. proto.init = function init(ns, theCode, scope){
  91. // File: expression_where.cpp lines: 62-86
  92. if ( ns.size() === 0 )
  93. return {code:ErrorCodes.BAD_VALUE, description:"ns for $where cannot be empty"};
  94. if ( theCode.size() === 0 )
  95. return {code:ErrorCodes.BAD_VALUE, description:"code for $where cannot be empty"};
  96. this._ns = ns;
  97. this._code = theCode.toString();
  98. this._userScope = scope.getOwned();
  99. // we don't have authentication yet
  100. // var userToken = ClientBasic::getCurrent().getAuthorizationSession().getAuthenticatedUserNamesToken();
  101. this._scope = globalScriptEngine.getPooledScope( this._ns.substr(0, this._ns.indexOf('.')), "where" + userToken );
  102. this._func = this._scope.createFunction( this._code.c_str() );
  103. if ( !this._func )
  104. return {code:ErrorCodes.BAD_VALUE, description:"$where compile error"};
  105. return {code:ErrorCodes.OK};
  106. };
  107. /**
  108. *
  109. * This documentation was automatically generated. Please update when you touch this function.
  110. * @method matches
  111. * @param doc
  112. * @param details
  113. *
  114. */
  115. proto.matches = function matches(doc /*, details */){
  116. // File: expression_where.cpp lines: 89-110
  117. verify( this._func );
  118. if ( ! this._userScope.isEmpty() ) {
  119. this._scope.init( this._userScope );
  120. }
  121. this._scope.setObject("obj", doc);
  122. this._scope.setBoolean("fullObject", true); // this is a hack b/c fullObject used to be relevant
  123. err = this._scope.invoke(this._func, 0, obj, 1000 * 60, false);
  124. if ( err == -3 ) { // INVOKE_ERROR
  125. throw new Error("16812 error on invocation of $where function:\n" + this._scope.getError().name);
  126. }
  127. else if ( err != 0 ) { // ! INVOKE_SUCCESS
  128. throw new Error("16813 unknown error in invocation of $where function");
  129. }
  130. return this._scope.getBoolean("__returnValue") != 0;
  131. };
  132. /**
  133. *
  134. * WhereMatchExpression never matches a single element
  135. * @method matchesSingleElement
  136. * @param e
  137. *
  138. */
  139. proto.matchesSingleElement = function matchesSingleElement(e){
  140. // File: expression_where.cpp lines: 40-41
  141. return false;
  142. };
  143. /**
  144. *
  145. * Create a new WhereMatchExpression and initialize it with our property values
  146. * @method shallowClone
  147. *
  148. */
  149. proto.shallowClone = function shallowClone(){
  150. // File: expression_where.cpp lines: 44-47
  151. e = new WhereMatchExpression();
  152. e.init(this._ns, this._code, this._userScope);
  153. return e;
  154. };