Class: Lexicon::Common::Psql

Inherits:
Object
  • Object
show all
Defined in:
lib/lexicon/common/psql.rb

Instance Method Summary collapse

Constructor Details

#initialize(url:, executor:) ⇒ Psql

Returns a new instance of Psql.

Parameters:



8
9
10
11
# File 'lib/lexicon/common/psql.rb', line 8

def initialize(url:, executor:)
  @url = url
  @executor = executor
end

Instance Method Details

#execute(command, search_path:) ⇒ Object

Parameters:

  • command (String)
  • search_path (String, Array<String>)


15
16
17
18
19
20
21
22
# 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) ⇒ Object

Parameters:

  • command (String)


25
26
27
28
29
# 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:) ⇒ Object

Parameters:

  • file (Pathname)
  • search_path (String, Array<String>)


33
34
35
36
37
# 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