Code coverage report for src/cypherquery.js

Statements: 82.28% (65 / 79)      Branches: 68.75% (33 / 48)      Functions: 100% (6 / 6)      Lines: 82.28% (65 / 79)      Ignored: none     

All files » src/ » cypherquery.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124  1       1 1     1 4914 4914   4914   4914   4914     1 522 522 522     522 522 522     522 1518 1518   18 18   1500 1500         1500       1500 1500       1500 1500   1226   1226   274   1500     522   522     1 1 1 1 1   1 1008     1 521 521   521 521 521     1 4 4   2 2   2     4     1 397   397     397 227 397   427   397     1   1     1  
 
Iif (typeof window === 'object') {
  var _ = window._;
  var helpers = window.Neo4jMapper.helpers;
} else {
  var _ = require('underscore');
  var helpers = require('./helpers');
}
 
var CypherQuery = function CypherQuery(query, parameters) {
  this.statements = [];
  Iif (typeof query === 'string')
    this.query = query;
  else Iif (typeof query === 'object')
    this.statements = query;
  Iif (parameters)
    this.parameters = parameters;
  this.cypher = _.extend(CypherQuery.prototype.cypher);
}
 
CypherQuery.prototype.statementsToString = function(options) {
  var s = '';
  var chopLength = 15;
  var defaultOptions = {
    niceFormat: true
  };
  Eif (this.statements) {
    Eif (typeof options !== 'object')
      options = {};
    else
      _.defaults(options, defaultOptions);
    for (var i=0; i < this.statements.length; i++) {
      var queryFragment = this.statements[i];
      if (typeof queryFragment === 'string') {
        // if we have just a string, we add the string to final query, no manipulation
        s += queryFragment;
        continue;
      }
      var attribute = Object.keys(this.statements[i])[0];
      Iif ((typeof queryFragment === 'object') && (typeof queryFragment.toQueryString === 'function')) {
        queryFragment = queryFragment.toQueryString();
        s += queryFragment;
        continue;
      }
      var forQuery = this.statements[i][attribute];
      // do we have an object with a .toQueryString() method?
 
      // remove underscore from attribute, e.g. ORDER_BY -> ORDER BY
      attribute = attribute.replace(/([A-Z]{1})\_([A-Z]{1})/g, '$1 $2');
      Iif (options.niceFormat) {
        // extend attribute-string with whitespace
        attribute = attribute + Array(chopLength - attribute.length).join(' ');
      }
      Eif (forQuery !== null) {
        if (typeof forQuery === 'string') {
          // remove dupliacted statement fragment identifier, e.g. START START … -> START …
          forQuery = forQuery.trim().replace(new RegExp('^'+attribute+'\\s+', 'i'), '');
          // remove trailing semicolon
          forQuery = forQuery.replace(/\;$/, '');
        } else {
          forQuery = String(forQuery);
        }
        s += '\n'+attribute+' '+forQuery+' ';
      }
    }
    s = s.trim().replace(/;+$/,'');
  }
  return s + ';';
}
 
CypherQuery.prototype.query = '';         // the cypher query string
CypherQuery.prototype.parameters = null;
CypherQuery.prototype.statements = null;
CypherQuery.prototype.cypher = {};
CypherQuery.prototype.useParameters = true;
 
CypherQuery.prototype.hasParameters = function() {
  return ((this.parameters) && (typeof this.parameters === 'object') && (Object.keys(this.parameters).length > 0));
}
 
CypherQuery.prototype.toCypher = function(options) {
  var s = '';
  Iif (this.query)
    s = this.query;
  else Eif (this.statements.length > 0)
    s = this.statementsToString(options);
  return s;
}
 
CypherQuery.prototype.toString = function(options) {
  var s = this.toCypher(options);
  if ((s)&&(this.hasParameters())) {
    // replace identifiers with values to present a good equivalent
    for (var key in this.parameters) {
      var value = this.parameters[key];
      // TODO: better check that we detect placeholders
      s = s.replace('{'+key+'}', helpers.valueToStringForCypherQuery(value, "'"));
    };
  }
  return s;
}
 
CypherQuery.prototype.addParameters = function(parameters) {
  Iif (typeof parameters !== 'object')
    throw Error('parameter(s) as argument must be an object, e.g. { key: "value" }')
  Iif (this.useParameters === null)
    this.useParameters = true;
  // reset parameters
  if ((!this.hasParameters()) || ((parameters !== null) && (Object.keys(parameters).length === 0)))
    this.parameters = {};
  for (var attr in parameters) {
    // replace undefined values with null because we can't work with undefined values in neo4j
    this.parameters[attr] = (parameters[attr] === undefined) ? null : parameters[attr];
  }
  return this;
}
 
CypherQuery.prototype.addParameter = CypherQuery.prototype.addParameters;
 
Iif (typeof window === 'object') {
  window.Neo4jMapper.CypherQuery = CypherQuery;
} else {
  module.exports = exports = CypherQuery;
}