Module: ActiveRecord::Import::PostgreSQLAdapter
- Includes:
- ImportSupport, OnDuplicateKeyUpdateSupport
- Included in:
- ConnectionAdapters::PostgreSQLAdapter
- Defined in:
- lib/activerecord-import/adapters/postgresql_adapter.rb
Constant Summary collapse
- MIN_VERSION_FOR_UPSERT =
90500
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.
-
#insert_many(sql, values, *args) ⇒ Object
:nodoc:.
- #next_value_for_sequence(sequence_name) ⇒ Object
-
#post_sql_statements(table_name, options) ⇒ Object
:nodoc:.
- #sql_for_conflict_target(args = {}) ⇒ Object
- #sql_for_default_conflict_target(table_name) ⇒ Object
-
#sql_for_on_duplicate_key_ignore(table_name, *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(table_name, arr) ⇒ Object
:nodoc:.
-
#sql_for_on_duplicate_key_update_as_hash(table_name, hsh) ⇒ Object
:nodoc:.
- #support_setting_primary_key_of_imported_objects? ⇒ Boolean
- #supports_on_duplicate_key_ignore?(current_version = self.postgresql_version) ⇒ Boolean
- #supports_on_duplicate_key_update?(current_version = self.postgresql_version) ⇒ Boolean
Methods included from ImportSupport
Instance Method Details
#add_column_for_on_duplicate_key_update(column, options = {}) ⇒ Object
Add a column to be updated on duplicate key update
37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/activerecord-import/adapters/postgresql_adapter.rb', line 37 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
125 126 127 |
# File 'lib/activerecord-import/adapters/postgresql_adapter.rb', line 125 def duplicate_key_update_error?(exception)# :nodoc: exception.is_a?(ActiveRecord::StatementInvalid) && exception.to_s.include?('duplicate key') end |
#insert_many(sql, values, *args) ⇒ Object
:nodoc:
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/activerecord-import/adapters/postgresql_adapter.rb', line 7 def insert_many( sql, values, *args ) # :nodoc: number_of_inserts = 1 base_sql,post_sql = if sql.is_a?( String ) [ sql, '' ] elsif sql.is_a?( Array ) [ sql.shift, sql.join( ' ' ) ] end sql2insert = base_sql + values.join( ',' ) + post_sql ids = select_values( sql2insert, *args ) ActiveRecord::Base.connection.query_cache.clear [number_of_inserts,ids] end |
#next_value_for_sequence(sequence_name) ⇒ Object
24 25 26 |
# File 'lib/activerecord-import/adapters/postgresql_adapter.rb', line 24 def next_value_for_sequence(sequence_name) %{nextval('#{sequence_name}')} end |
#post_sql_statements(table_name, options) ⇒ Object
:nodoc:
28 29 30 31 32 33 34 |
# File 'lib/activerecord-import/adapters/postgresql_adapter.rb', line 28 def post_sql_statements( table_name, ) # :nodoc: unless [:primary_key].blank? super(table_name, ) << ("RETURNING #{[:primary_key]}") else super(table_name, ) end end |
#sql_for_conflict_target(args = {}) ⇒ Object
112 113 114 115 116 117 118 |
# File 'lib/activerecord-import/adapters/postgresql_adapter.rb', line 112 def sql_for_conflict_target( args={} ) if constraint_name = args[:constraint_name] "ON CONSTRAINT #{constraint_name} " elsif conflict_target = args[:conflict_target] '(' << Array( conflict_target ).join( ', ' ) << ') ' end end |
#sql_for_default_conflict_target(table_name) ⇒ Object
120 121 122 |
# File 'lib/activerecord-import/adapters/postgresql_adapter.rb', line 120 def sql_for_default_conflict_target( table_name ) "(#{primary_key( table_name )}) " end |
#sql_for_on_duplicate_key_ignore(table_name, *args) ⇒ Object
Returns a generated ON CONFLICT DO NOTHING statement given the passed in args.
52 53 54 55 56 57 58 |
# File 'lib/activerecord-import/adapters/postgresql_adapter.rb', line 52 def sql_for_on_duplicate_key_ignore( table_name, *args ) # :nodoc: arg = args.first if arg.is_a?( Hash ) conflict_target = sql_for_conflict_target( arg ) end " 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.
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/activerecord-import/adapters/postgresql_adapter.rb', line 62 def sql_for_on_duplicate_key_update( table_name, *args ) # :nodoc: arg = args.first if arg.is_a?( Array ) || arg.is_a?( String ) arg = { :columns => arg } end return unless arg.is_a?( Hash ) sql = " ON CONFLICT " conflict_target = sql_for_conflict_target( arg ) columns = arg.fetch( :columns, [] ) if columns.respond_to?( :empty? ) && columns.empty? return sql << "#{conflict_target}DO NOTHING" end conflict_target ||= sql_for_default_conflict_target( table_name ) unless conflict_target raise ArgumentError, 'Expected :conflict_target or :constraint_name to be specified' end sql << "#{conflict_target}DO UPDATE SET " if columns.is_a?( Array ) sql << sql_for_on_duplicate_key_update_as_array( table_name, columns ) elsif columns.is_a?( Hash ) sql << sql_for_on_duplicate_key_update_as_hash( table_name, columns ) elsif columns.is_a?( String ) sql << columns else raise ArgumentError, 'Expected :columns to be an Array or Hash' end sql end |
#sql_for_on_duplicate_key_update_as_array(table_name, arr) ⇒ Object
:nodoc:
95 96 97 98 99 100 101 |
# File 'lib/activerecord-import/adapters/postgresql_adapter.rb', line 95 def sql_for_on_duplicate_key_update_as_array( table_name, arr ) # :nodoc: results = arr.map do |column| qc = quote_column_name( column ) "#{qc}=EXCLUDED.#{qc}" end results.join( ',' ) end |
#sql_for_on_duplicate_key_update_as_hash(table_name, hsh) ⇒ Object
:nodoc:
103 104 105 106 107 108 109 110 |
# File 'lib/activerecord-import/adapters/postgresql_adapter.rb', line 103 def sql_for_on_duplicate_key_update_as_hash( table_name, hsh ) # :nodoc: results = hsh.map do |column1, column2| qc1 = quote_column_name( column1 ) qc2 = quote_column_name( column2 ) "#{qc1}=EXCLUDED.#{qc2}" end results.join( ',' ) end |
#support_setting_primary_key_of_imported_objects? ⇒ Boolean
137 138 139 |
# File 'lib/activerecord-import/adapters/postgresql_adapter.rb', line 137 def support_setting_primary_key_of_imported_objects? true end |
#supports_on_duplicate_key_ignore?(current_version = self.postgresql_version) ⇒ Boolean
133 134 135 |
# File 'lib/activerecord-import/adapters/postgresql_adapter.rb', line 133 def supports_on_duplicate_key_ignore?(current_version=self.postgresql_version) supports_on_duplicate_key_update?(current_version) end |
#supports_on_duplicate_key_update?(current_version = self.postgresql_version) ⇒ Boolean
129 130 131 |
# File 'lib/activerecord-import/adapters/postgresql_adapter.rb', line 129 def supports_on_duplicate_key_update?(current_version=self.postgresql_version) current_version >= MIN_VERSION_FOR_UPSERT end |