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.
27 28 29 30 31 |
# File 'lib/db/model/statement/insert.rb', line 27 def initialize(source, fields, values) @source = source @fields = fields @values = values end |
Instance Method Details
#call(context) ⇒ Object
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/db/model/statement/insert.rb', line 49 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
60 61 62 63 64 65 66 67 68 69 |
# File 'lib/db/model/statement/insert.rb', line 60 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
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/db/model/statement/insert.rb', line 33 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 |