Sha256: 317609167d26885b8c1025e69ea555a2377d65f03958b1b269966fd3f8737a34

Contents?: true

Size: 682 Bytes

Versions: 3

Compression:

Stored size: 682 Bytes

Contents

class Baza::SqlQueries::GenericUpdate
  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

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

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

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

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
baza-0.0.38 lib/baza/sql_queries/generic_update.rb
baza-0.0.37 lib/baza/sql_queries/generic_update.rb
baza-0.0.36 lib/baza/sql_queries/generic_update.rb