Update verification of batch compatible statements using regex

This commit is contained in:
김선우
2025-08-24 10:13:04 +09:00
parent bf1473dc08
commit 9f6eb8bc92
2 changed files with 157 additions and 43 deletions

View File

@@ -11,6 +11,7 @@ import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.ReentrantLock;
import java.util.regex.Pattern;
import tech.turso.annotations.Nullable;
import tech.turso.annotations.SkipNullableCheck;
import tech.turso.core.TursoResultSet;
@@ -18,6 +19,20 @@ import tech.turso.core.TursoStatement;
public class JDBC4Statement implements Statement {
private static final Pattern BATCH_COMPATIBLE_PATTERN =
Pattern.compile(
"^\\s*"
+ // Leading whitespace
"(?:/\\*.*?\\*/\\s*)*"
+ // Optional C-style comments
"(?:--[^\\n]*\\n\\s*)*"
+ // Optional SQL line comments
"(?:"
+ // Start of keywords group
"INSERT|UPDATE|DELETE"
+ ")\\b",
Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
private final JDBC4Connection connection;
@Nullable protected TursoStatement statement = null;
@@ -254,6 +269,7 @@ public class JDBC4Statement implements Statement {
batchCommands.clear();
}
// TODO: let's make this batch operation atomic
@Override
public int[] executeBatch() throws SQLException {
ensureOpen();
@@ -310,31 +326,14 @@ public class JDBC4Statement implements Statement {
return updateCounts;
}
/**
* Checks if a SQL statement is compatible with batch execution. Only INSERT, UPDATE, DELETE, and
* DDL statements are allowed in batch. SELECT and other query statements are not allowed.
*
* @param sql The SQL statement to check
* @return true if the statement is batch-compatible, false otherwise
*/
private boolean isBatchCompatibleStatement(String sql) {
boolean isBatchCompatibleStatement(String sql) {
if (sql == null || sql.trim().isEmpty()) {
return false;
}
// Trim and convert to uppercase for case-insensitive comparison
String trimmedSql = sql.trim().toUpperCase();
// Check if it starts with batch-compatible keywords
return trimmedSql.startsWith("INSERT")
|| trimmedSql.startsWith("UPDATE")
|| trimmedSql.startsWith("DELETE")
|| trimmedSql.startsWith("CREATE")
|| trimmedSql.startsWith("DROP")
|| trimmedSql.startsWith("ALTER")
|| trimmedSql.startsWith("TRUNCATE")
|| trimmedSql.startsWith("REPLACE")
|| trimmedSql.startsWith("MERGE");
// Check if the SQL matches batch-compatible patterns (DML and DDL)
// This will return false for SELECT, EXPLAIN, PRAGMA, SHOW, etc.
return BATCH_COMPATIBLE_PATTERN.matcher(sql).find();
}
@Override