Class: Update

Inherits:
SqlStatement show all
Defined in:
lib/update.rb

Instance Attribute Summary

Attributes inherited from SqlStatement

#tables, #to_sql

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from SqlStatement

#and, #and_with_or_conditions, #initialize, #or, #where

Constructor Details

This class inherits a constructor from SqlStatement

Class Method Details

.[](table) ⇒ Object

call-seq: Update -> an_update

Returns an Update instance with the SQL initialized to ‘update [table] ’

Update[:table1].to_sql       #=> "update table1"


8
9
10
11
12
# File 'lib/update.rb', line 8

def [](table)
  update = self.new("update #{table.to_sql}")
  update.tables = [table]
  update
end

Instance Method Details

#[](hash) ⇒ Object

call-seq: update -> an_update

Returns an Update instance with the set values appended to the SQL statement.

update = Update[:table1].set[:column1=>'book', :column2=>10]
update.to_sql       #=> "update table1 set column1 = 'book, column2 = 10"


30
31
32
33
34
35
36
37
38
# File 'lib/update.rb', line 30

def [](hash)
  @to_sql += " set "
  set_args = []
  hash.each_pair do |col, val|
    set_args << "#{col.to_sql}=#{val.to_sql}"
  end
  @to_sql += set_args.sort.join(', ')
  self
end

#setObject

call-seq: update.set -> update

Returns self. Unnecessary and only available to mimic SQL statements.

Update[:table1].set.to_sql       #=> "update table1"


20
21
22
# File 'lib/update.rb', line 20

def set
  self
end