Class: TrainPlugins::Pgsql::Connection
- Inherits:
-
Train::Plugins::Transport::BaseConnection
- Object
- Train::Plugins::Transport::BaseConnection
- TrainPlugins::Pgsql::Connection
- Defined in:
- lib/train-pgsql/connection.rb
Instance Method Summary collapse
- #close ⇒ Object
- #connect ⇒ Object
- #connection ⇒ Object
-
#initialize(options) ⇒ Connection
constructor
A new instance of Connection.
- #platform ⇒ Object
- #run_command_via_connection(query) ⇒ Object
- #uri ⇒ Object
Constructor Details
#initialize(options) ⇒ Connection
Returns a new instance of Connection.
7 8 9 10 |
# File 'lib/train-pgsql/connection.rb', line 7 def initialize() super() enable_cache(:command) end |
Instance Method Details
#close ⇒ Object
18 19 20 21 22 23 24 25 |
# File 'lib/train-pgsql/connection.rb', line 18 def close return if @connection.nil? logger.info format('[Pgsql] Closed connection to %s:%d', @options[:host], @options[:port]) @connection.finish ensure @connection = nil end |
#connect ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/train-pgsql/connection.rb', line 55 def connect logger.info format('[Pgsql] Opening connection to %s:%d', @options[:host], @options[:port]) @connection = PG::Connection.new( host: @options[:host], port: @options[:port], user: @options[:user], password: @options[:password], dbname: @options[:database] ) @connection end |
#connection ⇒ Object
51 52 53 |
# File 'lib/train-pgsql/connection.rb', line 51 def connection @connection ||= connect end |
#platform ⇒ Object
12 13 14 15 16 |
# File 'lib/train-pgsql/connection.rb', line 12 def platform Train::Platforms.name('pgsql').in_family('sql') force_platform!('pgsql', release: TrainPlugins::Pgsql::VERSION) end |
#run_command_via_connection(query) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/train-pgsql/connection.rb', line 31 def run_command_via_connection(query) stdout = '' stderr = '' exit_status = 0 logger.debug format('[Pgsql] Sending command (%s) to %s:%d', query, @options[:host], @options[:port]) begin connection.exec(query) do |result| stdout = { 'fields' => result.fields, 'values' => result.values } # stdout = result.values end rescue PG::Error => e stderr = e. end CommandResult.new(stdout, stderr, exit_status) end |
#uri ⇒ Object
27 28 29 |
# File 'lib/train-pgsql/connection.rb', line 27 def uri "pgsql://#{[:user]}@#{@options[:host]}:#{@options[:port]}/#{@options[:database]}" end |