Class: MultiInsert::Query

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

Overview

A MultiInsert query.

Most of the time, you do not need to instanciate it yourself.

Defined Under Namespace

Classes: OnConflict

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table, columns, values, opts = {}) ⇒ Query

Create an insert query against the specified table and columns, with the specified values. The following options are supported:

  • time (true) Whether to insert created_at and updated_at.

Parameters:

  • table (String | Symbol)

    The table to be used for insertion.

  • columns (Array<String | Symbol>)

    The columns to be inserted.

  • values (Array<Array<String | Number | Boolean>>)

    The values to be inserted.

  • opts (Hash<Object>) (defaults to: {})

    Options.



56
57
58
59
60
# File 'lib/multi_insert/query.rb', line 56

def initialize(table, columns, values, opts = {})
  @table = table.to_sym
  @opts = opts
  @sql_insert = ::MultiInsert::QueryBuilder.insert(table, columns, values, opts)
end

Instance Attribute Details

#optsObject (readonly)

Returns the value of attribute opts.



46
47
48
# File 'lib/multi_insert/query.rb', line 46

def opts
  @opts
end

Instance Method Details

#executenil | Array<Integer> | Array<Array<String | Number | Boolean>>

Execute the query, and return eventual results.

Result may be:

  • Nil if no returning clause was present.

  • An array of IDs if the returning_id helper was called.

  • An array of rows otherwise.

Returns:

  • (nil | Array<Integer> | Array<Array<String | Number | Boolean>>)


122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/multi_insert/query.rb', line 122

def execute
  result = nil
  ActiveRecord::Base.connection_pool.with_connection do |con|
    result = con.execute(to_sql)
  end
  if @sql_returning.nil?
    nil
  else
    if @returning_flat
      result.values.map{|r| r.first}
    else
      result
    end
  end
end

#on_conflict(column = nil) ⇒ Query::OnConflict

Begin a conflict clause.

Parameters:

  • column (String | Symbol) (defaults to: nil)

    The column to watch for conflicts.

Returns:



86
87
88
# File 'lib/multi_insert/query.rb', line 86

def on_conflict(column = nil)
  ::MultiInsert::Query::OnConflict.new(self, column)
end

#on_conflict_sql(sql) ⇒ Query

Handle a conflict with raw SQL

You should probably use the friendly helper method instead.

Parameters:

  • sql (String)

    An SQL expression starting with “ON CONFLICT”.

Returns:



95
96
97
98
# File 'lib/multi_insert/query.rb', line 95

def on_conflict_sql(sql)
  @sql_on_conflict = sql
  self
end

#returning(columns) ⇒ Query

Add a returning clause to the query.

Parameters:

  • columns (Array<Symbol | String>)

    The columns to return.

Returns:



66
67
68
69
70
# File 'lib/multi_insert/query.rb', line 66

def returning(columns)
  @sql_returning = ::MultiInsert::QueryBuilder.returning(columns)
  @returning_flat = false
  self
end

#returning_idQuery

Add a returning clause to the query, returning IDs.

The IDs will be returned as a flat array.

Returns:



76
77
78
79
80
# File 'lib/multi_insert/query.rb', line 76

def returning_id
  @sql_returning = ::MultiInsert::QueryBuilder.returning([:id])
  @returning_flat = true
  self
end

#to_sString

Equivalent to ‘to_sql`.

Returns:

  • (String)

    An SQL query.



110
111
112
# File 'lib/multi_insert/query.rb', line 110

def to_s
  to_sql
end

#to_sqlString

Convert the query to raw SQL.

Returns:

  • (String)

    An SQL query.



103
104
105
# File 'lib/multi_insert/query.rb', line 103

def to_sql
  [@sql_insert, @sql_on_conflict, @sql_returning].reject(&:nil?).join(' ')
end