Class: SQLStatement::Insert

Inherits:
Hash
  • Object
show all
Defined in:
lib/sql/statement.rb

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

Instance Method Summary collapse

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

#targettableObject

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

#placeheldObject

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_sObject

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