Class: DB::Model::Statement::Insert
- Inherits:
-
Object
- Object
- DB::Model::Statement::Insert
- Defined in:
- lib/db/model/statement/insert.rb
Instance Method Summary collapse
- #call(context) ⇒ Object
-
#initialize(source, fields, values) ⇒ Insert
constructor
A new instance of Insert.
- #to_a(context) ⇒ Object
- #to_sql(context) ⇒ Object
Constructor Details
#initialize(source, fields, values) ⇒ Insert
Returns a new instance of Insert.
10 11 12 13 14 |
# File 'lib/db/model/statement/insert.rb', line 10 def initialize(source, fields, values) @source = source @fields = fields @values = values end |
Instance Method Details
#call(context) ⇒ Object
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/db/model/statement/insert.rb', line 32 def call(context) to_sql(context).call do |connection| result = connection.next_result keys = result.field_names.map(&:to_sym) result.each do |row| yield(keys.zip(row).to_h) end end end |
#to_a(context) ⇒ Object
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/db/model/statement/insert.rb', line 43 def to_a(context) to_sql(context).call do |connection| result = connection.next_result keys = result.field_names.map(&:to_sym) result.map do |row| @source.new(context, keys.zip(row).to_h) end end end |
#to_sql(context) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/db/model/statement/insert.rb', line 16 def to_sql(context) statement = context.query("INSERT INTO") statement.identifier(@source.type) statement.clause("(") @fields.append_to(statement) statement.clause(") VALUES") @values.append_to(statement) statement.clause("RETURNING *") return statement end |