Skip to content

Commit

Permalink
Prevent backtick escaping on function calls and array indexes keys
Browse files Browse the repository at this point in the history
  • Loading branch information
brantburnett committed Feb 26, 2018
1 parent 6aa16c2 commit a908c0e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
23 changes: 21 additions & 2 deletions app/index-definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,25 @@ function ensureEscaped(identifier) {
}
}

/**
* Ensures that the N1QL identifier is escaped with backticks, unless it's a
* function call or array subquery
*
* @param {!string} identifier
* @return {!string}
*/
function normalizeIndexKey(identifier) {
identifier = identifier.trim().replace(/\s{2,}/g, ' ');

if (identifier.match(/^(\(?\s*DISTINCT|ALL)\s*\(?\s*ARRAY|\(/i)) {
// Contains parentheses or starts with ALL ARRAY or DISTINCT ARRAY
// Don't escape
return identifier;
} else {
return ensureEscaped(identifier);
}
}

/**
* Represents an index
* @property {!string} name
Expand Down Expand Up @@ -68,8 +87,8 @@ export class IndexDefinition {
definition.index_key = !obj.index_key ?
[] :
_.isArray(obj.index_key) ?
obj.index_key.map(ensureEscaped) :
_.compact([ensureEscaped(obj.index_key)]);
obj.index_key.map(normalizeIndexKey) :
_.compact([normalizeIndexKey(obj.index_key)]);

definition.condition =
IndexDefinition.normalizeCondition(obj.condition);
Expand Down
6 changes: 6 additions & 0 deletions example/beer-sample/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,9 @@ index_key:
- ibu
condition: (`type` = 'beer')
num_replica: 0
---
name: BreweriesByAddress
index_key:
- (distinct (array `p` for `p` in `address` end))
condition: (`type` = 'brewery')
num_replica: 0

0 comments on commit a908c0e

Please sign in to comment.