Class: DataMapper::Queries::InsertStatement
- Inherits:
-
Object
- Object
- DataMapper::Queries::InsertStatement
- Defined in:
- lib/data_mapper/queries/insert_statement.rb
Direct Known Subclasses
Instance Method Summary collapse
-
#initialize(database, instance) ⇒ InsertStatement
constructor
A new instance of InsertStatement.
-
#to_sql ⇒ Object
The only thing this method is responsible for is generating the insert statement.
Constructor Details
#initialize(database, instance) ⇒ InsertStatement
Returns a new instance of InsertStatement.
6 7 8 |
# File 'lib/data_mapper/queries/insert_statement.rb', line 6 def initialize(database, instance) @database, @instance = database, instance end |
Instance Method Details
#to_sql ⇒ Object
The only thing this method is responsible for is generating the insert statement. It is the database adapters responsibility to get the last inserted id
12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/data_mapper/queries/insert_statement.rb', line 12 def to_sql table = @database[@instance.class] keys = [] values = [] @instance.dirty_attributes.each_pair { |k,v| keys << table[k].to_sql; values << v } # Formatting is a bit off here, but it looks nicer in the log this way. sql = "INSERT INTO #{table.to_sql} (#{keys.join(', ')}) \ VALUES (#{values.map { |v| @database.quote_value(v) }.join(', ')})" end |