Class: Baza::SqlQueries::GenericUpdate

Inherits:
Object
  • Object
show all
Defined in:
lib/baza/sql_queries/generic_update.rb

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ GenericUpdate

Returns a new instance of GenericUpdate.



2
3
4
5
6
7
8
# File 'lib/baza/sql_queries/generic_update.rb', line 2

def initialize(args)
  @db = args.fetch(:db)
  @table_name = args.fetch(:table_name)
  @data = args.fetch(:data)
  @terms = args.fetch(:terms)
  @buffer = args[:buffer]
end

Instance Method Details

#executeObject



10
11
12
13
14
15
16
# File 'lib/baza/sql_queries/generic_update.rb', line 10

def execute
  if @buffer
    @buffer.query(to_sql)
  else
    @db.query(to_sql)
  end
end

#to_sqlObject



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/baza/sql_queries/generic_update.rb', line 18

def to_sql
  sql = "UPDATE #{@db.sep_col}#{@db.escape_table(@table_name)}#{@db.sep_col} SET "

  first = true
  @data.each do |key, value|
    sql << ", " unless first
    first = false if first
    sql << "#{@db.sep_col}#{@db.escape_column(key)}#{@db.sep_col} = #{@db.sqlval(value)}"
  end

  sql << " WHERE #{@db.sql_make_where(@terms)}" if @terms && !@terms.empty?
  sql
end