Skip to content

Commit

Permalink
Fix parsing of mix of AND/OR with parentheses in SQL
Browse files Browse the repository at this point in the history
Resolves: #6834
  • Loading branch information
luigidellaquila committed Jul 21, 2017
1 parent cbf45af commit 88c03c3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -280,21 +280,25 @@ private Object extractConditionItem(final boolean iAllowOperator, final int iExp
final int lastPosition = parserIsEnded() ? parserText.length() : parserGetCurrentPosition();

if (word.length() > 0 && word.charAt(0) == OStringSerializerHelper.EMBEDDED_BEGIN) {
braces++;
if (uWord.startsWith("(SELECT") || uWord.startsWith("(TRAVERSE")) {
braces++;

// SUB-CONDITION
parserSetCurrentPosition(lastPosition - word.length() + 1);
// SUB-CONDITION
parserSetCurrentPosition(lastPosition - word.length() + 1);

final Object subCondition = extractConditions(null);
final Object subCondition = extractConditions(null);

if (!parserSkipWhiteSpaces() || parserGetCurrentChar() == ')') {
braces--;
parserMoveCurrentPosition(+1);
}
if (subCondition instanceof OSQLFilterCondition) {
((OSQLFilterCondition) subCondition).inBraces = true;
if (!parserSkipWhiteSpaces() || parserGetCurrentChar() == ')') {
braces--;
parserMoveCurrentPosition(+1);
}
if (subCondition instanceof OSQLFilterCondition) {
((OSQLFilterCondition) subCondition).inBraces = true;
}
result[i] = subCondition;
} else {
return new OSQLPredicate(word.substring(1, word.length() - 1)).getRootCondition();
}
result[i] = subCondition;
} else if (word.charAt(0) == OStringSerializerHelper.LIST_BEGIN) {
// COLLECTION OF ELEMENTS
parserSetCurrentPosition(lastPosition - getLastWordLength());
Expand Down Expand Up @@ -434,4 +438,6 @@ protected Set<String> computePrefetchFieldList(final OSQLFilterCondition iCondit

return iFields;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -1640,14 +1640,33 @@ public void testFilterListsOfMaps() {
db.command(new OCommandSQL("create class " + className)).execute();
db.command(new OCommandSQL("create property " + className + ".tagz embeddedmap")).execute();
db.command(new OCommandSQL("insert into " + className + " set tagz = {}")).execute();
db.command(new OCommandSQL("update " + className + " SET tagz.foo = [{name:'a', surname:'b'}, {name:'c', surname:'d'}]")).execute();
db.command(new OCommandSQL("update " + className + " SET tagz.foo = [{name:'a', surname:'b'}, {name:'c', surname:'d'}]"))
.execute();

List<ODocument> results = db.query(new OSQLSynchQuery<ODocument>("select tagz.values()[0][name = 'a'] as t from "+className));
List<ODocument> results = db.query(new OSQLSynchQuery<ODocument>("select tagz.values()[0][name = 'a'] as t from " + className));
Assert.assertEquals(results.size(), 1);
Map map = results.get(0).field("t");
Assert.assertEquals(map.get("surname"), "b");
}

@Test
public void testAndOrParentheses() {
//issue #6834

String className = "testAndOrParentheses";

db.command(new OCommandSQL("create class " + className)).execute();
db.command(new OCommandSQL("insert into " + className + " set name = 'foo'")).execute();

List<ODocument> results = db.query(new OSQLSynchQuery<ODocument>(
" select * from " + className + " where ( (1=1) or (1 in (select 1 from " + className + ")) ) and 1=1 "));
Assert.assertEquals(results.size(), 1);
results = db.query(new OSQLSynchQuery<ODocument>(
" select * from " + className + " where ( (1=1) or (1 in (select 1 from " + className + ")) ) and 1=2 "));
Assert.assertEquals(results.size(), 0);

}

private long indexUsages(ODatabaseDocumentTx db) {
final long oldIndexUsage;
try {
Expand Down

0 comments on commit 88c03c3

Please sign in to comment.