Skip to content

Commit 478ef58

Browse files
committed
Added validation for operators and non raw column names
1 parent aba6336 commit 478ef58

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

src/main/java/org/javawebstack/orm/query/QueryColumn.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
package org.javawebstack.orm.query;
22

33
import org.javawebstack.orm.TableInfo;
4+
import org.javawebstack.orm.exception.ORMQueryException;
45

56
import java.util.Arrays;
7+
import java.util.regex.Pattern;
68
import java.util.stream.Collectors;
79

810
public class QueryColumn {
911

12+
private static final Pattern NAME_PATTERN = Pattern.compile("[A-Za-z0-9_-]+");
13+
1014
private final String name;
1115
private final boolean raw;
1216

@@ -15,6 +19,8 @@ public QueryColumn(String name) {
1519
}
1620

1721
public QueryColumn(String name, boolean raw) {
22+
if(!raw)
23+
validateName(name);
1824
this.name = name;
1925
this.raw = raw;
2026
}
@@ -37,4 +43,9 @@ public String toString(TableInfo info) {
3743
return Arrays.stream((info != null ? info.getColumnName(name) : name).split("\\.")).map(s -> "`" + s + "`").collect(Collectors.joining("."));
3844
}
3945

46+
private static void validateName(String name) {
47+
if(!NAME_PATTERN.matcher(name).matches())
48+
throw new ORMQueryException("Invalid column name '" + name + "' (Use raw in case you know what you're doing)");
49+
}
50+
4051
}

src/main/java/org/javawebstack/orm/query/QueryCondition.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,41 @@
11
package org.javawebstack.orm.query;
22

3+
import org.javawebstack.orm.exception.ORMQueryException;
4+
5+
import java.util.Arrays;
6+
import java.util.List;
7+
import java.util.Locale;
8+
39
public class QueryCondition implements QueryElement {
410

11+
private static final List<String> VALID_OPERATORS = Arrays.asList(
12+
"=",
13+
"<=>",
14+
"!=",
15+
"<>",
16+
"<=",
17+
">=",
18+
"<",
19+
">",
20+
"is null",
21+
"is not null",
22+
"is",
23+
"is not",
24+
"in",
25+
"not in",
26+
"like",
27+
"not like"
28+
);
29+
530
private final Object left;
631
private final String operator;
732
private final Object right;
833
private final boolean not;
934

1035
public QueryCondition(Object left, String operator, Object right, boolean not) {
36+
validateOperator(operator);
1137
this.left = left;
12-
this.operator = operator; // TODO Validate and throw exception
38+
this.operator = operator;
1339
this.right = right;
1440
this.not = not;
1541
}
@@ -38,4 +64,9 @@ public boolean isNot() {
3864
return not;
3965
}
4066

67+
private static void validateOperator(String operator) {
68+
if(!VALID_OPERATORS.contains(operator.toLowerCase(Locale.ROOT)))
69+
throw new ORMQueryException("The given operator '" + operator + "' is invalid or not supported");
70+
}
71+
4172
}

0 commit comments

Comments
 (0)