Class: SQLStatement::Insert
Overview
Insert values directly into a table. This class does not support the use of SelectParts, 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.
507 508 509 |
# File 'lib/sql/statement.rb', line 507 def initialize table @targettable=table end |
Instance Attribute Details
#targettable ⇒ Object
The name of the table to insert into.
511 512 513 |
# File 'lib/sql/statement.rb', line 511 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)
524 525 526 |
# File 'lib/sql/statement.rb', line 524 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.
514 515 516 517 518 |
# File 'lib/sql/statement.rb', line 514 def to_s "INSERT INTO #{@targettable.to_sqlpart} ("+ keys.collect{|x| x.to_sqlpart}.join(", ")+")"+ "VALUES ("+values.collect{|x| x.to_sqlpart}.join(", ")+")" end |