class PG::LogicalReplication::CommandBuilder

Constants

VALUE_TYPES_BY_KEY

Attributes

connection[RW]

Public Class Methods

new(connection) click to toggle source
# File lib/pg/logical_replication/command_builder.rb, line 8
def initialize(connection)
  @connection = connection
end

Public Instance Methods

command_with_options(sql, keyword, options) click to toggle source
# File lib/pg/logical_replication/command_builder.rb, line 12
def command_with_options(sql, keyword, options)
  raise CommandBuilderError, "Unrecognized keyword #{keyword}" unless ["WITH", "SET"].include?(keyword)
  if options.empty?
    case keyword
    when "WITH"
      return sql
    when "SET"
      raise CommandBuilderError, "Keyword SET requires options"
    end
  end
  "#{sql} #{keyword} (#{parameters_list(options)})"
end

Private Instance Methods

parameters_list(options) click to toggle source
# File lib/pg/logical_replication/command_builder.rb, line 27
def parameters_list(options)
  options.to_a.map do |k, v|
    "#{connection.quote_ident(k.to_s)} = #{safe_value(k.to_s, v)}"
  end.join(", ")
end
quote_string(s) click to toggle source
# File lib/pg/logical_replication/command_builder.rb, line 57
def quote_string(s)
  "#{connection.escape_literal(s)}"
end
safe_value(key, value) click to toggle source
# File lib/pg/logical_replication/command_builder.rb, line 46
def safe_value(key, value)
  case VALUE_TYPES_BY_KEY[key]
  when "string"
    value == "NONE" ? "NONE" : quote_string(value)
  when "bool"
    value ? "true" : "false"
  else
    raise PG::LogicalReplication::CommandBuilderError, "Option type for key '#{key}' not defined"
  end
end