Class Insert
In: lib/insert.rb
Parent: SqlStatement

Methods

[]   []   into   values  

Public Class methods

Returns an Insert instance with the SQL initialized to ‘insert into [table] ‘

   Insert[:table1].to_sql       #=> "insert into table1 "

[Source]

    # File lib/insert.rb, line 17
17:     def [](table)
18:       self.new("insert into #{table.to_sql}")
19:     end

Returns the Insert class. Unnecessary and only available to mimic SQL statements.

   Insert.into       #=> Insert

[Source]

    # File lib/insert.rb, line 8
 8:     def into
 9:       self
10:     end

Public Instance methods

Returns an Insert instance with the columns appended to the SQL statement.

   Insert.into[:table1][:column1, :column2].to_sql       #=> "insert into table1 (column1, column2)"

[Source]

    # File lib/insert.rb, line 27
27:   def [](*columns)
28:     @to_sql += " (#{columns.join(', ')})"
29:     self
30:   end

If a block is given: Ignores any parameters given to the method. Executes the block then calls to_sql on the result. Returns an Insert instance with the result of the block‘s execution appended to the SQL statement.

   insert = Insert.into[:table1][:column1].values { Select['book'] }
   insert.to_sql      #=> "insert into table1 (column1) select 'book'"

If no block is given: Returns an Insert instance with the args appended to the SQL statement as values

   insert = Insert.into[:table1][:column1, :column2].values(10, 'book')
   insert.to_sql      #=> "insert into table1 (column1, column2) values (10, 'book')"

[Source]

    # File lib/insert.rb, line 48
48:   def values(*args)
49:     @to_sql += case
50:       when block_given? then " #{yield.to_sql}"
51:       else " values (#{args.to_sql})"
52:     end
53:     self
54:   end

[Validate]