Skip to content

Commit

Permalink
change query analyser to return array of parameters for CONTAINS oper…
Browse files Browse the repository at this point in the history
…ator (hyperledger-archives#2728)

Signed-off-by: andrew-coleman <[email protected]>
  • Loading branch information
andrew-coleman authored and mbwhite committed Nov 15, 2017
1 parent 273e8b9 commit c644771
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
24 changes: 23 additions & 1 deletion packages/composer-common/lib/query/queryanalyzer.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,27 @@ class QueryAnalyzer {
// Pop the scope name off again.
parameters.scopes.pop();

const result = [left, right];
// if the left is a string, it is the name of a property
// and we infer the type of the right from the model
// if the right is a parameter
let result = [];
if (typeof left === 'string' && (right instanceof Array && right.length > 0)) {
if(right[0].type === null) {
right[0].type = this.getParameterType(left, parameters);
}
result = result.concat(right);
}

// if the right is a string, it is the name of a property
// and we infer the type of the left from the model
// if the left is a parameter
if (typeof right === 'string' && (left instanceof Array && left.length > 0)) {
if(left[0].type === null) {
left[0].type = this.getParameterType(right, parameters);
}
result = result.concat(left);
}

LOG.exit(method, result);
return result;
}
Expand Down Expand Up @@ -442,6 +462,8 @@ class QueryAnalyzer {
LOG.entry(method, ast, parameters);
const result = ast.elements.map((element) => {
return this.visit(element, parameters);
}).filter((element) => {
return !(Array.isArray(element) && element.length === 0);
});
LOG.exit(method, result);
return result;
Expand Down
22 changes: 16 additions & 6 deletions packages/composer-common/test/query/queryanalyzer.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ describe('QueryAnalyzer', () => {
queryAnalyzer = new QueryAnalyzer( mockQuery );
const result = queryAnalyzer.visit(mockQuery, {});
result.should.not.be.null;
result.length.should.equal(2);
result.length.should.equal(0);
});

it('should process select with a contains and an array value', () => {
Expand All @@ -315,7 +315,7 @@ describe('QueryAnalyzer', () => {
queryAnalyzer = new QueryAnalyzer( mockQuery );
const result = queryAnalyzer.visit(mockQuery, {});
result.should.not.be.null;
result.length.should.equal(2);
result.length.should.equal(0);
});

it('should process select with a contains and a parameter value', () => {
Expand All @@ -325,7 +325,17 @@ describe('QueryAnalyzer', () => {
queryAnalyzer = new QueryAnalyzer( mockQuery );
const result = queryAnalyzer.visit(mockQuery, {});
result.should.not.be.null;
result.length.should.equal(2);
result.length.should.equal(1);
});

it('should process select with a contains and a reversed parameter value', () => {
const ast = parser.parse('SELECT org.acme.TestAsset WHERE (_$inputStringValue CONTAINS stringValues)', { startRule: 'SelectStatement' });
const select = new Select(mockQuery, ast);
mockQuery.getSelect.returns(select);
queryAnalyzer = new QueryAnalyzer( mockQuery );
const result = queryAnalyzer.visit(mockQuery, {});
result.should.not.be.null;
result.length.should.equal(1);
});

it('should process select with a contains and a nested expression', () => {
Expand All @@ -335,7 +345,7 @@ describe('QueryAnalyzer', () => {
queryAnalyzer = new QueryAnalyzer( mockQuery );
const result = queryAnalyzer.visit(mockQuery, {});
result.should.not.be.null;
result.length.should.equal(2);
result.length.should.equal(0);
});

it('should process select with a contains and a nested expression with a parameter value', () => {
Expand All @@ -345,7 +355,7 @@ describe('QueryAnalyzer', () => {
queryAnalyzer = new QueryAnalyzer( mockQuery );
const result = queryAnalyzer.visit(mockQuery, {});
result.should.not.be.null;
result.length.should.equal(2);
result.length.should.equal(1);
});

it('should process select with a contains and a nested expression with a reversed parameter value', () => {
Expand All @@ -355,7 +365,7 @@ describe('QueryAnalyzer', () => {
queryAnalyzer = new QueryAnalyzer( mockQuery );
const result = queryAnalyzer.visit(mockQuery, {});
result.should.not.be.null;
result.length.should.equal(2);
result.length.should.equal(1);
});

it('should throw for a select with a contains without a property name', () => {
Expand Down

0 comments on commit c644771

Please sign in to comment.