Class: MultiInsert::Query
- Inherits:
-
Object
- Object
- MultiInsert::Query
- 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
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
Instance Method Summary collapse
-
#execute ⇒ nil | Array<Integer> | Array<Array<String | Number | Boolean>>
Execute the query, and return eventual results.
-
#initialize(table, columns, values, opts = {}) ⇒ Query
constructor
Create an insert query against the specified table and columns, with the specified values.
-
#on_conflict(column = nil) ⇒ Query::OnConflict
Begin a conflict clause.
-
#on_conflict_sql(sql) ⇒ Query
Handle a conflict with raw SQL.
-
#returning(columns) ⇒ Query
Add a returning clause to the query.
-
#returning_id ⇒ Query
Add a returning clause to the query, returning IDs.
-
#to_s ⇒ String
Equivalent to ‘to_sql`.
-
#to_sql ⇒ String
Convert the query to raw SQL.
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.
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
#opts ⇒ Object (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
#execute ⇒ nil | 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.
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.
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.
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.
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_id ⇒ Query
Add a returning clause to the query, returning IDs.
The IDs will be returned as a flat array.
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_s ⇒ String
Equivalent to ‘to_sql`.
110 111 112 |
# File 'lib/multi_insert/query.rb', line 110 def to_s to_sql end |
#to_sql ⇒ String
Convert the query to raw SQL.
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 |