From 95b8efa0fac96ffd4ad231d01d1a13a1835175b7 Mon Sep 17 00:00:00 2001 From: David Schlosnagle Date: Mon, 11 Mar 2024 22:40:53 -0400 Subject: [PATCH] perf: CoreStatement uses optimize regex for generated key matches --- src/main/java/org/sqlite/core/CoreStatement.java | 6 +++--- src/test/java/org/sqlite/StatementTest.java | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/sqlite/core/CoreStatement.java b/src/main/java/org/sqlite/core/CoreStatement.java index a492f04caa..e74d399792 100644 --- a/src/main/java/org/sqlite/core/CoreStatement.java +++ b/src/main/java/org/sqlite/core/CoreStatement.java @@ -40,9 +40,9 @@ public abstract class CoreStatement implements Codes { // pattern for matching insert statements of the general format starting with INSERT or REPLACE. // CTEs used prior to the insert or replace keyword are also be permitted. - private final Pattern insertPattern = + private static final Pattern INSERT_PATTERN = Pattern.compile( - "^(with\\s+.+\\(.+?\\))*\\s*(insert|replace)", + "^\\s*(?:with\\s+.+\\(.+?\\))*\\s*(?:insert|replace)\\s*", Pattern.DOTALL | Pattern.CASE_INSENSITIVE); protected CoreStatement(SQLiteConnection c) { @@ -182,7 +182,7 @@ protected void clearGeneratedKeys() throws SQLException { */ public void updateGeneratedKeys() throws SQLException { clearGeneratedKeys(); - if (sql != null && insertPattern.matcher(sql.trim().toLowerCase()).find()) { + if (sql != null && INSERT_PATTERN.matcher(sql).find()) { generatedKeysStat = conn.createStatement(); generatedKeysRs = generatedKeysStat.executeQuery("SELECT last_insert_rowid();"); } diff --git a/src/test/java/org/sqlite/StatementTest.java b/src/test/java/org/sqlite/StatementTest.java index ebb50ec9dc..172a2f2fef 100644 --- a/src/test/java/org/sqlite/StatementTest.java +++ b/src/test/java/org/sqlite/StatementTest.java @@ -333,8 +333,8 @@ public void getGeneratedKeys() throws SQLException { // test INSERT with common table expression stat.executeUpdate( - "with colors as (select 'green' as color)\n" - + "insert into t1 (v) select color from colors;"); + " WITH colors as (select 'green' as color) \n" + + "INSERT into t1 (v) select color from colors;"); rs = stat.getGeneratedKeys(); assertThat(rs.next()).isTrue(); assertThat(rs.getInt(1)).isEqualTo(3);