Skip to content

Commit 23b6e5c

Browse files
committed
Fix like escaping for h2 databases
1 parent ca5f84a commit 23b6e5c

1 file changed

Lines changed: 5 additions & 4 deletions

File tree

cloudnet/src/main/java/de/dytanic/cloudnet/database/sql/SQLDatabase.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,8 @@ public List<JsonDocument> get(String fieldName, Object fieldValue) {
169169
Preconditions.checkNotNull(fieldValue);
170170

171171
return this.databaseProvider.executeQuery(
172-
String.format("SELECT %s FROM `%s` WHERE %s LIKE ?", TABLE_COLUMN_VALUE, this.name, TABLE_COLUMN_VALUE),
172+
String.format("SELECT %s FROM `%s` WHERE %s LIKE ? ESCAPE '$'", TABLE_COLUMN_VALUE, this.name,
173+
TABLE_COLUMN_VALUE),
173174
resultSet -> {
174175
List<JsonDocument> jsonDocuments = new ArrayList<>();
175176
while (resultSet.next()) {
@@ -178,7 +179,7 @@ public List<JsonDocument> get(String fieldName, Object fieldValue) {
178179

179180
return jsonDocuments;
180181
},
181-
"%\"" + fieldName + "\":" + JsonDocument.GSON.toJson(fieldValue).replaceAll("([_%\\[])", "[$1]") + "%"
182+
"%\"" + fieldName + "\":" + JsonDocument.GSON.toJson(fieldValue).replaceAll("([_%])", "\\$$1") + "%"
182183
);
183184
}
184185

@@ -200,8 +201,8 @@ public List<JsonDocument> get(JsonDocument filters) {
200201
while (iterator.hasNext()) {
201202
item = iterator.next();
202203

203-
stringBuilder.append(TABLE_COLUMN_VALUE).append(" LIKE ?");
204-
collection.add("%\"" + item + "\":" + filters.get(item).toString().replaceAll("([_%\\[])", "[$1]") + "%");
204+
stringBuilder.append(TABLE_COLUMN_VALUE).append(" LIKE ? ESCAPE '$'");
205+
collection.add("%\"" + item + "\":" + filters.get(item).toString().replaceAll("([_%])", "\\$$1") + "%");
205206

206207
if (iterator.hasNext()) {
207208
stringBuilder.append(" and ");

0 commit comments

Comments
 (0)