Module: ActiveRecord::Import::SQLite3Adapter
- Includes:
- ImportSupport
- Included in:
- ConnectionAdapters::SQLite3Adapter
- Defined in:
- lib/activerecord-import/adapters/sqlite3_adapter.rb
Constant Summary collapse
- MIN_VERSION_FOR_IMPORT =
"3.7.11".freeze
- SQLITE_LIMIT_COMPOUND_SELECT =
500
Instance Method Summary collapse
- #affected_rows ⇒ Object
-
#insert_many(sql, values, *args) ⇒ Object
sql
can be a single string or an array. - #next_value_for_sequence(sequence_name) ⇒ Object
- #pre_sql_statements(options) ⇒ Object
- #support_setting_primary_key_of_imported_objects? ⇒ Boolean
-
#supports_import?(current_version = sqlite_version) ⇒ Boolean
Override our conformance to ActiveRecord::Import::ImportSupport interface to ensure that we only support import in supported version of SQLite.
Instance Method Details
#affected_rows ⇒ Object
59 60 61 62 |
# File 'lib/activerecord-import/adapters/sqlite3_adapter.rb', line 59 def affected_rows result = execute('SELECT changes();') result.first[0] end |
#insert_many(sql, values, *args) ⇒ Object
sql
can be a single string or an array. If it is an array all elements that are in position >= 1 will be appended to the final SQL.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/activerecord-import/adapters/sqlite3_adapter.rb', line 20 def insert_many(sql, values, *args) # :nodoc: number_of_inserts = 0 ids = [] base_sql, post_sql = if sql.is_a?( String ) [sql, ''] elsif sql.is_a?( Array ) [sql.shift, sql.join( ' ' )] end value_sets = ::ActiveRecord::Import::ValueSetsRecordsParser.parse(values, max_records: SQLITE_LIMIT_COMPOUND_SELECT) transaction(requires_new: true) do value_sets.each do |value_set| number_of_inserts += 1 sql2insert = base_sql + value_set.join( ',' ) + post_sql last_insert_id = insert( sql2insert, *args ) first_insert_id = last_insert_id - affected_rows + 1 ids.concat((first_insert_id..last_insert_id).to_a) end end [number_of_inserts, ids] end |
#next_value_for_sequence(sequence_name) ⇒ Object
55 56 57 |
# File 'lib/activerecord-import/adapters/sqlite3_adapter.rb', line 55 def next_value_for_sequence(sequence_name) %{nextval('#{sequence_name}')} end |
#pre_sql_statements(options) ⇒ Object
46 47 48 49 50 51 52 53 |
# File 'lib/activerecord-import/adapters/sqlite3_adapter.rb', line 46 def pre_sql_statements( ) sql = [] # Options :recursive and :on_duplicate_key_ignore are mutually exclusive if ([:ignore] || [:on_duplicate_key_ignore]) && ![:recursive] sql << "OR IGNORE" end sql + super end |
#support_setting_primary_key_of_imported_objects? ⇒ Boolean
64 65 66 |
# File 'lib/activerecord-import/adapters/sqlite3_adapter.rb', line 64 def support_setting_primary_key_of_imported_objects? true end |
#supports_import?(current_version = sqlite_version) ⇒ Boolean
Override our conformance to ActiveRecord::Import::ImportSupport interface to ensure that we only support import in supported version of SQLite. Which INSERT statements with multiple value sets was introduced in 3.7.11.
10 11 12 13 14 15 16 |
# File 'lib/activerecord-import/adapters/sqlite3_adapter.rb', line 10 def supports_import?(current_version = sqlite_version) if current_version >= MIN_VERSION_FOR_IMPORT true else false end end |