Skip to content

Commit 4ee4155

Browse files
authored
Merge pull request #1194 from seuros/sqlite3
adapter fixes for db statements and sqlite3
2 parents 59ba043 + 749aade commit 4ee4155

File tree

2 files changed

+31
-8
lines changed

2 files changed

+31
-8
lines changed

lib/arjdbc/abstract/database_statements.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ def preprocess_query(sql)
9696
sql
9797
end
9898

99-
def raw_execute(sql, name, binds = [], prepare: false, async: false, allow_retry: false, materialize_transactions: true)
99+
def raw_execute(sql, name, binds = [], prepare: false, async: false, allow_retry: false, materialize_transactions: true, batch: false)
100100
log(sql, name, async: async) do
101101
with_raw_connection(allow_retry: allow_retry, materialize_transactions: materialize_transactions) do |conn|
102102
result = conn.execute(sql)

lib/arjdbc/sqlite3/adapter.rb

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -223,17 +223,29 @@ def supports_lazy_transactions?
223223

224224
# REFERENTIAL INTEGRITY ====================================
225225

226+
def execute_batch(statements, name = nil, **kwargs)
227+
if statements.is_a?(Array)
228+
# SQLite JDBC doesn't support multiple statements in one execute
229+
# Execute each statement separately
230+
statements.each do |sql|
231+
raw_execute(sql, name, **kwargs.merge(batch: true))
232+
end
233+
else
234+
raw_execute(statements, name, batch: true, **kwargs)
235+
end
236+
end
237+
226238
def disable_referential_integrity # :nodoc:
227239
old_foreign_keys = query_value("PRAGMA foreign_keys")
228240
old_defer_foreign_keys = query_value("PRAGMA defer_foreign_keys")
229241

230242
begin
231-
execute("PRAGMA defer_foreign_keys = ON")
232-
execute("PRAGMA foreign_keys = OFF")
243+
raw_execute("PRAGMA defer_foreign_keys = ON", "SCHEMA", allow_retry: false, materialize_transactions: false)
244+
raw_execute("PRAGMA foreign_keys = OFF", "SCHEMA", allow_retry: false, materialize_transactions: false)
233245
yield
234246
ensure
235-
execute("PRAGMA defer_foreign_keys = #{old_defer_foreign_keys}")
236-
execute("PRAGMA foreign_keys = #{old_foreign_keys}")
247+
raw_execute("PRAGMA defer_foreign_keys = #{old_defer_foreign_keys}", "SCHEMA", allow_retry: false, materialize_transactions: false)
248+
raw_execute("PRAGMA foreign_keys = #{old_foreign_keys}", "SCHEMA", allow_retry: false, materialize_transactions: false)
237249
end
238250
end
239251

@@ -809,7 +821,17 @@ def configure_connection
809821
DEFAULT_PRAGMAS.merge(pragmas).each do |pragma, value|
810822
if ::SQLite3::Pragmas.respond_to?(pragma)
811823
stmt = ::SQLite3::Pragmas.public_send(pragma, value)
812-
raw_execute(stmt, "SCHEMA")
824+
# Skip pragma execution if we're inside a transaction and it's not allowed
825+
begin
826+
raw_execute(stmt, "SCHEMA")
827+
rescue => e
828+
if e.message.include?("Safety level may not be changed inside a transaction")
829+
# Log warning and skip this pragma
830+
warn "Cannot set pragma '#{pragma}' inside a transaction, skipping"
831+
else
832+
raise
833+
end
834+
end
813835
else
814836
warn "Unknown SQLite pragma: #{pragma}"
815837
end
@@ -863,7 +885,7 @@ def jdbc_connection_class
863885
include ArJdbc::Abstract::ConnectionManagement
864886
include ArJdbc::Abstract::DatabaseStatements
865887
include ArJdbc::Abstract::StatementCache
866-
include ArJdbc::Abstract::TransactionSupport
888+
# Don't include TransactionSupport - use Rails' SQLite3::DatabaseStatements instead
867889

868890

869891
##
@@ -972,7 +994,8 @@ def initialize_type_map(m)
972994

973995
# because the JDBC driver doesn't like multiple SQL statements in one JDBC statement
974996
def combine_multi_statements(total_sql)
975-
total_sql
997+
# For Rails 8 compatibility - join multiple statements with semicolon
998+
total_sql.is_a?(Array) ? total_sql.join(";\n") : total_sql
976999
end
9771000

9781001
def type_map

0 commit comments

Comments
 (0)