class Lexicon::Common::Psql

Attributes

executor[R]

@return [ShellExecutor]

url[R]

@return [String]

Public Class Methods

new(url:, executor:) click to toggle source

@param [String] url @param [ShellExecutor] executor

# File lib/lexicon/common/psql.rb, line 8
def initialize(url:, executor:)
  @url = url
  @executor = executor
end

Public Instance Methods

execute(command, search_path:) click to toggle source

@param [String] command @param [String, Array<String>] search_path

# File lib/lexicon/common/psql.rb, line 15
      def execute(command, search_path:)
        command = <<~SQL
          SET search_path TO #{Array(search_path).join(', ')};
          #{command}
        SQL

        execute_raw(command)
      end
execute_raw(command) click to toggle source

@param [String] command

# File lib/lexicon/common/psql.rb, line 25
      def execute_raw(command)
        @executor.execute <<~BASH
          psql '#{url}' --quiet -c #{Shellwords.escape(command)}
        BASH
      end
load_sql(file, search_path:) click to toggle source

@param [Pathname] file @param [String, Array<String>] search_path

# File lib/lexicon/common/psql.rb, line 33
      def load_sql(file, search_path:)
        @executor.execute <<~BASH
          echo 'SET SEARCH_PATH TO #{Array(search_path).join(', ')};' | cat - #{file} | psql '#{url}'
        BASH
      end