Sha256: 94a333750daee42f61b251978521566baa4955d29c546eafef9c5e5304c5a47f

Contents?: true

Size: 1.26 KB

Versions: 15

Compression:

Stored size: 1.26 KB

Contents

class Baza::SqlQueries::MysqlUpsert
  def initialize(args)
    @db = args.fetch(:db)
    @table_name = args.fetch(:table_name)
    @updates = args.fetch(:updates)
    @terms = args.fetch(:terms)
  end

  def execute
    procedure_name = "baza_upsert_#{SecureRandom.hex(5)}"

    sql = "CREATE PROCEDURE `#{@db.escape_table(procedure_name)}` () BEGIN\n"
    sql << "\tIF EXISTS(#{select_query}) THEN\n"
    sql << "\t\t#{update_sql};\n"
    sql << "\tELSE\n"
    sql << "\t\t#{insert_sql};\n"
    sql << "\tEND IF;\n"
    sql << "END;\n"

    @db.query(sql)

    begin
      @db.query("CALL `#{@db.escape_table(procedure_name)}`")
    ensure
      @db.query("DROP PROCEDURE `#{@db.escape_table(procedure_name)}`")
    end
  end

private

  def select_query
    sql = ""
    sql << "SELECT * FROM #{@db.sep_table}#{@db.escape_table(@table_name)}#{@db.sep_table} WHERE"

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

    sql
  end

  def update_sql
    @db.update(@table_name, @updates, @terms, return_sql: true)
  end

  def insert_sql
    @db.insert(@table_name, @updates.merge(@terms), return_sql: true)
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
baza-0.0.35 lib/baza/sql_queries/mysql_upsert.rb
baza-0.0.34 lib/baza/sql_queries/mysql_upsert.rb
baza-0.0.33 lib/baza/sql_queries/mysql_upsert.rb
baza-0.0.32 lib/baza/sql_queries/mysql_upsert.rb
baza-0.0.31 lib/baza/sql_queries/mysql_upsert.rb
baza-0.0.30 lib/baza/sql_queries/mysql_upsert.rb
baza-0.0.29 lib/baza/sql_queries/mysql_upsert.rb
baza-0.0.28 lib/baza/sql_queries/mysql_upsert.rb
baza-0.0.27 lib/baza/sql_queries/mysql_upsert.rb
baza-0.0.26 lib/baza/sql_queries/mysql_upsert.rb
baza-0.0.25 lib/baza/sql_queries/mysql_upsert.rb
baza-0.0.24 lib/baza/sql_queries/mysql_upsert.rb
baza-0.0.23 lib/baza/sql_queries/mysql_upsert.rb
baza-0.0.22 lib/baza/sql_queries/mysql_upsert.rb
baza-0.0.21 lib/baza/sql_queries/mysql_upsert.rb