Module: ActiveRecord::Import::SQLite3Adapter
- Includes:
- ImportSupport, OnDuplicateKeyUpdateSupport
- Included in:
- ConnectionAdapters::SQLite3Adapter
- Defined in:
- lib/activerecord-import/adapters/sqlite3_adapter.rb
Constant Summary collapse
- MIN_VERSION_FOR_IMPORT =
"3.7.11".freeze
- MIN_VERSION_FOR_UPSERT =
"3.24.0".freeze
- SQLITE_LIMIT_COMPOUND_SELECT =
500
Instance Method Summary collapse
-
#add_column_for_on_duplicate_key_update(column, options = {}) ⇒ Object
Add a column to be updated on duplicate key update.
-
#duplicate_key_update_error?(exception) ⇒ Boolean
Return true if the statement is a duplicate key record error.
- #increment_locking_column!(results, locking_column) ⇒ Object
-
#insert_many(sql, values, _options = {}, *args) ⇒ Object
sql
can be a single string or an array. - #next_value_for_sequence(sequence_name) ⇒ Object
-
#post_sql_statements(table_name, options) ⇒ Object
:nodoc:.
- #pre_sql_statements(options) ⇒ Object
- #sql_for_conflict_target(args = {}) ⇒ Object
- #sql_for_default_conflict_target(primary_key) ⇒ Object
-
#sql_for_on_duplicate_key_ignore(*args) ⇒ Object
Returns a generated ON CONFLICT DO NOTHING statement given the passed in
args
. -
#sql_for_on_duplicate_key_update(_table_name, *args) ⇒ Object
Returns a generated ON CONFLICT DO UPDATE statement given the passed in
args
. -
#sql_for_on_duplicate_key_update_as_array(locking_column, arr) ⇒ Object
:nodoc:.
-
#sql_for_on_duplicate_key_update_as_hash(locking_column, hsh) ⇒ Object
:nodoc:.
-
#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.
- #supports_on_duplicate_key_update?(current_version = sqlite_version) ⇒ Boolean
Instance Method Details
#add_column_for_on_duplicate_key_update(column, options = {}) ⇒ Object
Add a column to be updated on duplicate key update
76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/activerecord-import/adapters/sqlite3_adapter.rb', line 76 def add_column_for_on_duplicate_key_update( column, = {} ) # :nodoc: arg = [:on_duplicate_key_update] if arg.is_a?( Hash ) columns = arg.fetch( :columns ) { arg[:columns] = [] } case columns when Array then columns << column.to_sym unless columns.include?( column.to_sym ) when Hash then columns[column.to_sym] = column.to_sym end elsif arg.is_a?( Array ) arg << column.to_sym unless arg.include?( column.to_sym ) end end |
#duplicate_key_update_error?(exception) ⇒ Boolean
Return true if the statement is a duplicate key record error
169 170 171 |
# File 'lib/activerecord-import/adapters/sqlite3_adapter.rb', line 169 def duplicate_key_update_error?(exception) # :nodoc: exception.is_a?(ActiveRecord::StatementInvalid) && exception.to_s.include?('duplicate key') end |
#increment_locking_column!(results, locking_column) ⇒ Object
173 174 175 176 177 |
# File 'lib/activerecord-import/adapters/sqlite3_adapter.rb', line 173 def increment_locking_column!(results, locking_column) if locking_column.present? results << "\"#{locking_column}\"=EXCLUDED.\"#{locking_column}\"+1" end end |
#insert_many(sql, values, _options = {}, *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.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/activerecord-import/adapters/sqlite3_adapter.rb', line 26 def insert_many( sql, values, = {}, *args ) # :nodoc: number_of_inserts = 0 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 insert( sql2insert, *args ) end end ActiveRecord::Import::Result.new([], number_of_inserts, [], []) end |
#next_value_for_sequence(sequence_name) ⇒ Object
71 72 73 |
# File 'lib/activerecord-import/adapters/sqlite3_adapter.rb', line 71 def next_value_for_sequence(sequence_name) %{nextval('#{sequence_name}')} end |
#post_sql_statements(table_name, options) ⇒ Object
:nodoc:
58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/activerecord-import/adapters/sqlite3_adapter.rb', line 58 def post_sql_statements( table_name, ) # :nodoc: sql = [] if supports_on_duplicate_key_update? # Options :recursive and :on_duplicate_key_ignore are mutually exclusive if ([:ignore] || [:on_duplicate_key_ignore]) && ![:on_duplicate_key_update] sql << sql_for_on_duplicate_key_ignore( [:on_duplicate_key_ignore] ) end end sql + super end |
#pre_sql_statements(options) ⇒ Object
49 50 51 52 53 54 55 56 |
# File 'lib/activerecord-import/adapters/sqlite3_adapter.rb', line 49 def pre_sql_statements( ) sql = [] # Options :recursive and :on_duplicate_key_ignore are mutually exclusive if !supports_on_duplicate_key_update? && ([:ignore] || [:on_duplicate_key_ignore]) sql << "OR IGNORE" end sql + super end |
#sql_for_conflict_target(args = {}) ⇒ Object
153 154 155 156 157 158 159 160 161 |
# File 'lib/activerecord-import/adapters/sqlite3_adapter.rb', line 153 def sql_for_conflict_target( args = {} ) conflict_target = args[:conflict_target] index_predicate = args[:index_predicate] if conflict_target.present? '(' << Array( conflict_target ).reject( &:blank? ).join( ', ' ) << ') '.tap do |sql| sql << "WHERE #{index_predicate} " if index_predicate end end end |
#sql_for_default_conflict_target(primary_key) ⇒ Object
163 164 165 166 |
# File 'lib/activerecord-import/adapters/sqlite3_adapter.rb', line 163 def sql_for_default_conflict_target( primary_key ) conflict_target = Array(primary_key).join(', ') "(#{conflict_target}) " if conflict_target.present? end |
#sql_for_on_duplicate_key_ignore(*args) ⇒ Object
Returns a generated ON CONFLICT DO NOTHING statement given the passed in args
.
91 92 93 94 95 |
# File 'lib/activerecord-import/adapters/sqlite3_adapter.rb', line 91 def sql_for_on_duplicate_key_ignore( *args ) # :nodoc: arg = args.first conflict_target = sql_for_conflict_target( arg ) if arg.is_a?( Hash ) " ON CONFLICT #{conflict_target}DO NOTHING" end |
#sql_for_on_duplicate_key_update(_table_name, *args) ⇒ Object
Returns a generated ON CONFLICT DO UPDATE statement given the passed in args
.
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/activerecord-import/adapters/sqlite3_adapter.rb', line 99 def sql_for_on_duplicate_key_update( _table_name, *args ) # :nodoc: arg, primary_key, locking_column = args arg = { columns: arg } if arg.is_a?( Array ) || arg.is_a?( String ) return unless arg.is_a?( Hash ) sql = ' ON CONFLICT ' conflict_target = sql_for_conflict_target( arg ) columns = arg.fetch( :columns, [] ) condition = arg[:condition] if columns.respond_to?( :empty? ) && columns.empty? return sql << "#{conflict_target}DO NOTHING" end conflict_target ||= sql_for_default_conflict_target( primary_key ) unless conflict_target raise ArgumentError, 'Expected :conflict_target to be specified' end sql << "#{conflict_target}DO UPDATE SET " if columns.is_a?( Array ) sql << sql_for_on_duplicate_key_update_as_array( locking_column, columns ) elsif columns.is_a?( Hash ) sql << sql_for_on_duplicate_key_update_as_hash( locking_column, columns ) elsif columns.is_a?( String ) sql << columns else raise ArgumentError, 'Expected :columns to be an Array or Hash' end sql << " WHERE #{condition}" if condition.present? sql end |
#sql_for_on_duplicate_key_update_as_array(locking_column, arr) ⇒ Object
:nodoc:
134 135 136 137 138 139 140 141 |
# File 'lib/activerecord-import/adapters/sqlite3_adapter.rb', line 134 def sql_for_on_duplicate_key_update_as_array( locking_column, arr ) # :nodoc: results = arr.map do |column| qc = quote_column_name( column ) "#{qc}=EXCLUDED.#{qc}" end increment_locking_column!(results, locking_column) results.join( ',' ) end |
#sql_for_on_duplicate_key_update_as_hash(locking_column, hsh) ⇒ Object
:nodoc:
143 144 145 146 147 148 149 150 151 |
# File 'lib/activerecord-import/adapters/sqlite3_adapter.rb', line 143 def sql_for_on_duplicate_key_update_as_hash( locking_column, hsh ) # :nodoc: results = hsh.map do |column1, column2| qc1 = quote_column_name( column1 ) qc2 = quote_column_name( column2 ) "#{qc1}=EXCLUDED.#{qc2}" end increment_locking_column!(results, locking_column) results.join( ',' ) 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.
12 13 14 15 16 17 18 |
# File 'lib/activerecord-import/adapters/sqlite3_adapter.rb', line 12 def supports_import?(current_version = sqlite_version) if current_version >= MIN_VERSION_FOR_IMPORT true else false end end |
#supports_on_duplicate_key_update?(current_version = sqlite_version) ⇒ Boolean
20 21 22 |
# File 'lib/activerecord-import/adapters/sqlite3_adapter.rb', line 20 def supports_on_duplicate_key_update?(current_version = sqlite_version) current_version >= MIN_VERSION_FOR_UPSERT end |