Skip to content

Commit

Permalink
Fix parse after parentheses
Browse files Browse the repository at this point in the history
  • Loading branch information
violet-dev committed Sep 15, 2024
1 parent 88f112a commit 2442462
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
17 changes: 11 additions & 6 deletions violet/lib/component/hitomi/hitomi.dart
Original file line number Diff line number Diff line change
Expand Up @@ -322,15 +322,17 @@ class QueryTranslator {
where += parseParentheses(token, negative);
where += parseExpression();
where += nextToken();
if (hasMoreTokens()) {
where += parseLogicalExpression();
}
} else if (token == ')') {
return token;
} else {
where += parseTitle(token, negative);
}

if (hasMoreTokens() && lookAhead() != ')') {
String logicalOp = parseLogicalOperator();
where += logicalOp + parseExpression();
where += parseLogicalExpression();
}

return where;
Expand Down Expand Up @@ -393,18 +395,21 @@ class QueryTranslator {
return negative ? "Title NOT LIKE '%$token%'" : "Title LIKE '%$token%'";
}

String parseLogicalOperator() {
String parseLogicalExpression() {
String next = lookAhead();
late String op;
if (next.toLowerCase() == 'or') {
nextToken();
return ' OR ';
op = 'OR';
} else {
op = 'AND';
}
return ' AND ';
return ' $op ${parseExpression()}';
}

String nextToken() => tokens[index++];
String lookAhead() => index < tokens.length ? tokens[index] : '';
bool hasMoreTokens() => index < tokens.length - 1;
bool hasMoreTokens() => index < tokens.length;

static String findColumnByTag(String tag) {
switch (tag) {
Expand Down
2 changes: 1 addition & 1 deletion violet/test/query_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ void main() {
expect(result1,
'SELECT * FROM HitomiColumnModel WHERE Tags LIKE \'%|female:sole female|%\' AND NOT (Tags LIKE \'%|female:mother|%\' AND Tags LIKE \'%|female:milf|%\') AND ExistOnHitomi=1');
expect(result2,
'SELECT * FROM HitomiColumnModel WHERE (Language LIKE \'%korean%\' OR Language LIKE \'%n/a%\') AND ExistOnHitomi=1');
'SELECT * FROM HitomiColumnModel WHERE (Language LIKE \'%korean%\' OR Language LIKE \'%n/a%\') AND (Tags LIKE \'%|female:sole female|%\') IS NOT 1 AND ExistOnHitomi=1');
});
});
}

0 comments on commit 2442462

Please sign in to comment.