class PgPartitions::SQL::If

Public Instance Methods

to_sql() click to toggle source
# File lib/pg_partitions/sql.rb, line 10
def to_sql
  if conditions.empty?
    raise ArgumentError, 'You must provide at least one condition'
  end

  lines = conditions.map do |opts|
    if opts.key? :if
      build_condition :if, opts
    elsif opts.key? :elsif
      build_condition :elsif, opts
    else opts.key? :else
      "ELSE\n  #{opts[:else]}"
    end
  end

  lines << 'END IF;'
  lines.join("\n") << "\n"
end

Private Instance Methods

build_condition(key, opts) click to toggle source
# File lib/pg_partitions/sql.rb, line 31
def build_condition(key, opts)
  then_sql = opts.fetch :then do
    "INSERT INTO #{opts.fetch(:insert)} VALUES(NEW.*) RETURNING * INTO result;"
  end

  "#{key.to_s.upcase} (#{opts[key]}) THEN\n  #{then_sql}"
end