Class: SQLStatement::Insert
Overview
Insert values directly into a table. This class does not support the ability to merge a Select object, rather it behaves like a hash of column names (as symbols) to values. You can get the “slice” functionality by merging hashes into the statement using Hash#merge!
.
Instance Attribute Summary collapse
-
#targettable ⇒ Object
The name of the table to insert into.
Instance Method Summary collapse
-
#initialize(table) ⇒ Insert
constructor
A new instance of Insert.
-
#placeheld ⇒ Object
Returns the objects corresponding to ?‘s in the SQL code, in the proper order.
-
#to_s ⇒ Object
Returns the SQL code to execute.
Constructor Details
#initialize(table) ⇒ Insert
Returns a new instance of Insert.
468 469 470 |
# File 'lib/sql/statement.rb', line 468 def initialize table @targettable=table end |
Instance Attribute Details
#targettable ⇒ Object
The name of the table to insert into.
472 473 474 |
# File 'lib/sql/statement.rb', line 472 def targettable @targettable end |
Instance Method Details
#placeheld ⇒ Object
Returns the objects corresponding to ?‘s in the SQL code, in the proper order. Insert into the database using the statement:
i=Insert.new(:tablename)
#add values
dbh.do(i.to_s,*i.placeheld)
485 486 487 |
# File 'lib/sql/statement.rb', line 485 def placeheld values.collect{|x| x.placeheld}.flatten end |
#to_s ⇒ Object
Returns the SQL code to execute. May contain ?‘s as placeholders for values.
475 476 477 478 479 |
# File 'lib/sql/statement.rb', line 475 def to_s "INSERT INTO #{@targettable.to_sqlpart} ("+ keys.collect{|x| x.to_sqlpart}.join(", ")+")"+ "VALUES ("+values.collect{|x| x.to_sqlpart}.join(", ")+")" end |