Class: MultiInsert::Query::OnConflict

Inherits:
Object
  • Object
show all
Defined in:
lib/multi_insert/query.rb

Overview

A conflict close inside a query.

You never need to instanciate it yourself. Instead, use ‘Query#on_conflict`.

Instance Method Summary collapse

Constructor Details

#initialize(query, column) ⇒ OnConflict

Create an ON CONFLICT clause

Parameters:

  • query (Query)

    the query.

  • column (String | Symbol | nil)

    the column to watch for conflicts.



18
19
20
21
# File 'lib/multi_insert/query.rb', line 18

def initialize(query, column)
  @query = query
  @column = column
end

Instance Method Details

#do_nothingQuery

Ignore conflicting rows.

Returns:

  • (Query)

    the original query.



26
27
28
# File 'lib/multi_insert/query.rb', line 26

def do_nothing
  @query.on_conflict_sql(::MultiInsert::QueryBuilder.on_conflict_do_nothing(@column))
end

#do_update(values) ⇒ Query

Update the conflicting rows according to user-supplied rules.

The rules can be:

  • A single symbol or string denoting a column name. In this case, the matching column will be updated.

  • An array of strings or symbols. In this case, the matching columns will be updated.

  • An hash of values (Integers or Strings) or the symbol ‘:excluded`. The matching columns will be updated

to the supplied values, except when the value is ‘:excluded`. In that case, the matching columns will be set to the value to be inserted.

Parameters:

  • values (String | Symbol | Array<String | Symbol> | Hash<String | Symbol => String | Number | Boolean>)

    The user specified rules.

Returns:

  • (Query)

    The original query.



41
42
43
# File 'lib/multi_insert/query.rb', line 41

def do_update(values)
  @query.on_conflict_sql(::MultiInsert::QueryBuilder.on_conflict_do_update(@column, values, @query.opts))
end