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 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

Instance Method Summary collapse

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

#targettableObject

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

#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)



524
525
526
# File 'lib/sql/statement.rb', line 524

def placeheld
   values.collect{|x| x.placeheld}.flatten
end

#to_sObject

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