VariablesParseState.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. "use strict";
  2. /**
  3. * Class generates unused ids
  4. * @class VariablesParseState
  5. * @namespace mungedb-aggregate.pipeline.expressions
  6. * @module mungedb-aggregate
  7. * @constructor
  8. **/
  9. var Variables = require('./Variables'),
  10. VariablesIdGenerator = require('./VariablesIdGenerator');
  11. var VariablesParseState = module.exports = function VariablesParseState(idGenerator){
  12. if(!idGenerator || idGenerator.constructor !== VariablesIdGenerator) {
  13. throw new Error("idGenerator is required and must be of type VariablesIdGenerator");
  14. }
  15. this._idGenerator = idGenerator;
  16. this._variables = {}; //Note: The c++ type was StringMap<Variables::Id>
  17. }, klass = VariablesParseState, base = Object, proto = klass.prototype = Object.create(base.prototype, {constructor:{value:klass}});
  18. /**
  19. * Assigns a named variable a unique Id. This differs from all other variables, even
  20. * others with the same name.
  21. *
  22. * The special variables ROOT and CURRENT are always implicitly defined with CURRENT
  23. * equivalent to ROOT. If CURRENT is explicitly defined by a call to this function, it
  24. * breaks that equivalence.
  25. *
  26. * NOTE: Name validation is responsibility of caller.
  27. **/
  28. proto.defineVariable = function generateId(name) {
  29. // caller should have validated before hand by using Variables::uassertValidNameForUserWrite
  30. if(name === 'ROOT') {
  31. throw new Error("mError 17275: Can't redefine ROOT");
  32. }
  33. var id = this._idGenerator.generateId();
  34. this._variables[name] = id;
  35. return id;
  36. };
  37. /**
  38. * Returns the current Id for a variable. uasserts if the variable isn't defined.
  39. * @method getVariable
  40. * @param name {String} The name of the variable
  41. **/
  42. proto.getVariable = function getIdCount(name) {
  43. var found = this._variables[name];
  44. if(typeof found === 'number') return found;
  45. if(name !== "ROOT" && name !== "CURRENT") {
  46. throw new Error("uError 17276: Use of undefined variable " + name);
  47. }
  48. return Variables.ROOT_ID;
  49. };