Class: SQB::Insert
Direct Known Subclasses
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
- #record(&block) ⇒ Object
- #to_sql ⇒ Object
-
#value(hash) ⇒ Object
(also: #values)
Set a value to be inserted.
Methods inherited from Base
Constructor Details
This class inherits a constructor from SQB::Base
Instance Method Details
#record(&block) ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/sqb/insert.rb', line 39 def record(&block) @record = {} block.call @records ||= [] @records << @record @record = nil end |
#to_sql ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/sqb/insert.rb', line 6 def to_sql [].tap do |query| values_sql = values.map { |rec| "(" + rec.join(', ') + ")" }.join(', ') query << "#{mysql_verb} INTO" query << escape_and_join(@options[:database_name], @table_name) if values_sql.empty? raise NoValuesError, "No values have been specified. Use `value` to add values to the query." end query << "(#{columns.join(', ')})" query << "VALUES" query << values_sql end.join(' ') end |
#value(hash) ⇒ Object Also known as: values
Set a value to be inserted
24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/sqb/insert.rb', line 24 def value(hash) if @record.nil? record = (@local_record ||= {}) else record = @record end hash.each do |key, value| record[key] = value end self end |