Skip to content

Commit 9039199

Browse files
committed
Add some new pieces of sqlite3 adapter from 7
1 parent 161d83b commit 9039199

File tree

1 file changed

+88
-5
lines changed

1 file changed

+88
-5
lines changed

lib/arjdbc/sqlite3/adapter.rb

Lines changed: 88 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,15 @@ module SQLite3
7979
boolean: { name: "boolean" },
8080
json: { name: "json" },
8181
}
82+
83+
class StatementPool < ConnectionAdapters::StatementPool # :nodoc:
84+
alias reset clear
8285

83-
# DIFFERENCE: class_attribute in original adapter is moved down to our section which is a class
84-
# since we cannot define it here in the module (original source this is a class).
86+
private
87+
def dealloc(stmt)
88+
stmt.close unless stmt.closed?
89+
end
90+
end
8591

8692
def initialize(connection, logger, connection_options, config)
8793
super(connection, logger, config)
@@ -101,7 +107,7 @@ def supports_transaction_isolation?
101107
end
102108

103109
def supports_partial_index?
104-
database_version >= "3.9.0"
110+
true
105111
end
106112

107113
def supports_expression_index?
@@ -144,6 +150,34 @@ def supports_insert_on_conflict?
144150
alias supports_insert_conflict_target? supports_insert_on_conflict?
145151

146152
# DIFFERENCE: active?, reconnect!, disconnect! handles by arjdbc core
153+
def supports_concurrent_connections?
154+
!@memory_database
155+
end
156+
157+
def active?
158+
!@raw_connection.closed?
159+
end
160+
161+
def reconnect!(restore_transactions: false)
162+
@lock.synchronize do
163+
if active?
164+
@connection.rollback rescue nil
165+
else
166+
connect
167+
end
168+
169+
super
170+
end
171+
end
172+
alias :reset! :reconnect!
173+
174+
# Disconnects from the database if already connected. Otherwise, this
175+
# method does nothing.
176+
def disconnect!
177+
super
178+
@connection.close rescue nil
179+
end
180+
147181

148182
def supports_index_sort_order?
149183
true
@@ -233,8 +267,8 @@ def primary_keys(table_name) # :nodoc:
233267
pks.sort_by { |f| f["pk"] }.map { |f| f["name"] }
234268
end
235269

236-
def remove_index(table_name, column_name = nil, **options) # :nodoc:
237-
return if options[:if_exists] && !index_exists?(table_name, column_name, **options)
270+
def remove_index(table_name, column_name = nil, **options) # :nodoc:
271+
return if options[:if_exists] && !index_exists?(table_name, column_name, **options)
238272

239273
index_name = index_name_for_remove(table_name, column_name, options)
240274

@@ -271,6 +305,16 @@ def remove_column(table_name, column_name, type = nil, **options) #:nodoc:
271305
end
272306
end
273307

308+
def remove_columns(table_name, *column_names, type: nil, **options) # :nodoc:
309+
alter_table(table_name) do |definition|
310+
column_names.each do |column_name|
311+
definition.remove_column column_name
312+
end
313+
column_names = column_names.map(&:to_s)
314+
definition.foreign_keys.delete_if { |fk| column_names.include?(fk.column) }
315+
end
316+
end
317+
274318
def change_column_default(table_name, column_name, default_or_changes) #:nodoc:
275319
default = extract_new_default_value(default_or_changes)
276320

@@ -380,6 +424,34 @@ def table_structure(table_name)
380424
end
381425
alias column_definitions table_structure
382426

427+
def extract_value_from_default(default)
428+
case default
429+
when /^null$/i
430+
nil
431+
# Quoted types
432+
when /^'(.*)'$/m
433+
$1.gsub("''", "'")
434+
# Quoted types
435+
when /^"(.*)"$/m
436+
$1.gsub('""', '"')
437+
# Numeric types
438+
when /\A-?\d+(\.\d*)?\z/
439+
$&
440+
else
441+
# Anything else is blank or some function
442+
# and we can't know the value of that, so return nil.
443+
nil
444+
end
445+
end
446+
447+
def extract_default_function(default_value, default)
448+
default if has_default_function?(default_value, default)
449+
end
450+
451+
def has_default_function?(default_value, default)
452+
!default_value && %r{\w+\(.*\)|CURRENT_TIME|CURRENT_DATE|CURRENT_TIMESTAMP}.match?(default)
453+
end
454+
383455
# See: https://www.sqlite.org/lang_altertable.html
384456
# SQLite has an additional restriction on the ALTER TABLE statement
385457
def invalid_alter_table_type?(type, options)
@@ -564,6 +636,17 @@ def arel_visitor
564636
Arel::Visitors::SQLite.new(self)
565637
end
566638

639+
def build_statement_pool
640+
StatementPool.new(self.class.type_cast_config_to_integer(@config[:statement_limit]))
641+
end
642+
643+
def connect
644+
@connection = ::SQLite3::Database.new(
645+
@config[:database].to_s,
646+
@config.merge(results_as_hash: true)
647+
)
648+
end
649+
567650
def configure_connection
568651
execute("PRAGMA foreign_keys = ON", "SCHEMA")
569652
end

0 commit comments

Comments
 (0)