10
10
require "active_record/connection_adapters/statement_pool"
11
11
require "active_record/connection_adapters/sqlite3/explain_pretty_printer"
12
12
require "active_record/connection_adapters/sqlite3/quoting"
13
+ require "active_record/connection_adapters/sqlite3/database_statements"
13
14
require "active_record/connection_adapters/sqlite3/schema_creation"
14
15
require "active_record/connection_adapters/sqlite3/schema_definitions"
15
16
require "active_record/connection_adapters/sqlite3/schema_dumper"
@@ -64,6 +65,7 @@ module SQLite3
64
65
# DIFFERENCE: FQN
65
66
include ::ActiveRecord ::ConnectionAdapters ::SQLite3 ::Quoting
66
67
include ::ActiveRecord ::ConnectionAdapters ::SQLite3 ::SchemaStatements
68
+ include ::ActiveRecord ::ConnectionAdapters ::SQLite3 ::DatabaseStatements
67
69
68
70
NATIVE_DATABASE_TYPES = {
69
71
primary_key : "integer PRIMARY KEY AUTOINCREMENT NOT NULL" ,
@@ -81,19 +83,28 @@ module SQLite3
81
83
}
82
84
83
85
class StatementPool < ConnectionAdapters ::StatementPool # :nodoc:
84
- alias reset clear
85
-
86
86
private
87
87
def dealloc ( stmt )
88
88
stmt . close unless stmt . closed?
89
89
end
90
90
end
91
91
92
92
def initialize ( connection , logger , connection_options , config )
93
+ @memory_database = config [ :database ] == ":memory:"
93
94
super ( connection , logger , config )
94
95
configure_connection
95
96
end
96
97
98
+ def self . database_exists? ( config )
99
+ config = config . symbolize_keys
100
+ if config [ :database ] == ":memory:"
101
+ true
102
+ else
103
+ database_file = defined? ( Rails . root ) ? File . expand_path ( config [ :database ] , Rails . root ) : config [ :database ]
104
+ File . exist? ( database_file )
105
+ end
106
+ end
107
+
97
108
def supports_ddl_transactions?
98
109
true
99
110
end
@@ -158,18 +169,10 @@ def active?
158
169
!@raw_connection . closed?
159
170
end
160
171
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
172
+ def reconnect!
173
+ super
174
+ connect if @connection . closed?
171
175
end
172
- alias :reset! :reconnect!
173
176
174
177
# Disconnects from the database if already connected. Otherwise, this
175
178
# method does nothing.
@@ -178,7 +181,6 @@ def disconnect!
178
181
@connection . close rescue nil
179
182
end
180
183
181
-
182
184
def supports_index_sort_order?
183
185
true
184
186
end
@@ -216,48 +218,8 @@ def disable_referential_integrity # :nodoc:
216
218
end
217
219
end
218
220
219
- #--
220
- # DATABASE STATEMENTS ======================================
221
- #++
222
-
223
- READ_QUERY = ActiveRecord ::ConnectionAdapters ::AbstractAdapter . build_read_query_regexp (
224
- :pragma
225
- ) # :nodoc:
226
- private_constant :READ_QUERY
227
-
228
- def write_query? ( sql ) # :nodoc:
229
- !READ_QUERY . match? ( sql )
230
- end
231
-
232
- def explain ( arel , binds = [ ] )
233
- sql = "EXPLAIN QUERY PLAN #{ to_sql ( arel , binds ) } "
234
- # DIFFERENCE: FQN
235
- ::ActiveRecord ::ConnectionAdapters ::SQLite3 ::ExplainPrettyPrinter . new . pp ( exec_query ( sql , "EXPLAIN" , [ ] ) )
236
- end
237
-
238
- # DIFFERENCE: implemented in ArJdbc::Abstract::DatabaseStatements
239
- #def exec_query(sql, name = nil, binds = [], prepare: false)
240
-
241
- # DIFFERENCE: implemented in ArJdbc::Abstract::DatabaseStatements
242
- #def exec_delete(sql, name = "SQL", binds = [])
243
-
244
- def last_inserted_id ( result )
245
- @connection . last_insert_row_id
246
- end
247
-
248
- # DIFFERENCE: implemented in ArJdbc::Abstract::DatabaseStatements
249
- #def execute(sql, name = nil) #:nodoc:
250
-
251
- def begin_db_transaction #:nodoc:
252
- log ( "begin transaction" , 'TRANSACTION' ) { @connection . transaction }
253
- end
254
-
255
- def commit_db_transaction #:nodoc:
256
- log ( "commit transaction" , 'TRANSACTION' ) { @connection . commit }
257
- end
258
-
259
- def exec_rollback_db_transaction #:nodoc:
260
- log ( "rollback transaction" , 'TRANSACTION' ) { @connection . rollback }
221
+ def all_foreign_keys_valid? # :nodoc:
222
+ execute ( "PRAGMA foreign_key_check" ) . blank?
261
223
end
262
224
263
225
# SCHEMA STATEMENTS ========================================
@@ -275,6 +237,7 @@ def remove_index(table_name, column_name = nil, **options) # :nodoc:
275
237
exec_query "DROP INDEX #{ quote_column_name ( index_name ) } "
276
238
end
277
239
240
+
278
241
# Renames a table.
279
242
#
280
243
# Example:
@@ -366,18 +329,6 @@ def foreign_keys(table_name)
366
329
end
367
330
end
368
331
369
- def insert_fixtures_set ( fixture_set , tables_to_delete = [ ] )
370
- disable_referential_integrity do
371
- transaction ( requires_new : true ) do
372
- tables_to_delete . each { |table | delete "DELETE FROM #{ quote_table_name ( table ) } " , "Fixture Delete" }
373
-
374
- fixture_set . each do |table_name , rows |
375
- rows . each { |row | insert_fixture ( row , table_name ) }
376
- end
377
- end
378
- end
379
- end
380
-
381
332
def build_insert_sql ( insert ) # :nodoc:
382
333
sql = +"INSERT #{ insert . into } #{ insert . values_list } "
383
334
@@ -392,18 +343,14 @@ def build_insert_sql(insert) # :nodoc:
392
343
sql
393
344
end
394
345
395
- def shared_cache?
396
- config [ :properties ] && config [ :properties ] [ :shared_cache ] == true
346
+ def shared_cache? # :nodoc:
347
+ @ config. fetch ( :flags , 0 ) . anybits? ( :: SQLite3 :: Constants :: Open :: SHAREDCACHE )
397
348
end
398
349
399
350
def get_database_version # :nodoc:
400
351
SQLite3Adapter ::Version . new ( query_value ( "SELECT sqlite_version(*)" ) )
401
352
end
402
353
403
- def build_truncate_statement ( table_name )
404
- "DELETE FROM #{ quote_table_name ( table_name ) } "
405
- end
406
-
407
354
def check_version
408
355
if database_version < "3.8.0"
409
356
raise "Your version of SQLite (#{ database_version } ) is too old. Active Record supports SQLite >= 3.8."
@@ -553,6 +500,7 @@ def copy_table_indexes(from, to, rename = {})
553
500
options = { name : name . gsub ( /(^|_)(#{ from } )_/ , "\\ 1#{ to } _" ) , internal : true }
554
501
options [ :unique ] = true if index . unique
555
502
options [ :where ] = index . where if index . where
503
+ options [ :order ] = index . orders if index . orders
556
504
add_index ( to , columns , **options )
557
505
end
558
506
end
@@ -803,11 +751,15 @@ def self.jdbc_connection_class
803
751
804
752
# because the JDBC driver doesn't like multiple SQL statements in one JDBC statement
805
753
def combine_multi_statements ( total_sql )
806
- if total_sql . length == 1
807
- total_sql . first
808
- else
809
- total_sql
810
- end
754
+ total_sql
755
+ end
756
+
757
+ # combine
758
+ def write_query? ( sql ) # :nodoc:
759
+ return sql . any? { |stmt | super ( stmt ) } if sql . kind_of? Array
760
+ !READ_QUERY . match? ( sql )
761
+ rescue ArgumentError # Invalid encoding
762
+ !READ_QUERY . match? ( sql . b )
811
763
end
812
764
813
765
def initialize_type_map ( m = type_map )
0 commit comments