Skip to content

Commit

Permalink
perf: CoreStatement uses optimize regex for generated key matches
Browse files Browse the repository at this point in the history
  • Loading branch information
schlosna authored Mar 12, 2024
1 parent 3b83760 commit 95b8efa
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/main/java/org/sqlite/core/CoreStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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();");
}
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/org/sqlite/StatementTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 95b8efa

Please sign in to comment.