Class: Lexicon::Common::Psql
- Inherits:
-
Object
- Object
- Lexicon::Common::Psql
- Defined in:
- lib/lexicon/common/psql.rb
Instance Method Summary collapse
- #execute(command, search_path:) ⇒ Object
- #execute_raw(command) ⇒ Object
-
#initialize(url:, executor:) ⇒ Psql
constructor
A new instance of Psql.
- #load_sql(file, search_path:) ⇒ Object
Constructor Details
#initialize(url:, executor:) ⇒ Psql
Returns a new instance of Psql.
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
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
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
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 |