Sha256: f4ee6ad81e8928e53de659b692800eb8c7431e5740e818e2ed6b29ef1502f415

Contents?: true

Size: 1.84 KB

Versions: 1

Compression:

Stored size: 1.84 KB

Contents

# frozen_string_literal: true

require_relative "abstract"
require "cgi"

module Hanami
  module Model
    module Sql
      module Consoles
        # PostgreSQL adapter
        #
        # @since 0.7.0
        # @api private
        class Postgresql < Abstract
          # @since 0.7.0
          # @api private
          COMMAND = "psql"

          # @since 0.7.0
          # @api private
          PASSWORD = "PGPASSWORD"

          # @since 0.7.0
          # @api private
          def connection_string
            configure_password
            concat(command, host, database, port, username)
          end

          private

          # @since 0.7.0
          # @api private
          def command
            COMMAND
          end

          # @since 0.7.0
          # @api private
          def host
            " -h #{query['host'] || @uri.host}"
          end

          # @since 0.7.0
          # @api private
          def database
            " -d #{database_name}"
          end

          # @since 0.7.0
          # @api private
          def port
            port = query["port"] || @uri.port
            " -p #{port}" if port
          end

          # @since 0.7.0
          # @api private
          def username
            username = query["user"] || @uri.user
            " -U #{username}" if username
          end

          # @since 0.7.0
          # @api private
          def configure_password
            password = query["password"] || @uri.password
            ENV[PASSWORD] = CGI.unescape(query["password"] || @uri.password) if password
          end

          # @since 1.1.0
          # @api private
          def query
            return {} if @uri.query.nil? || @uri.query.empty?

            parsed_query = @uri.query.split("&").map { |a| a.split("=") }
            @query ||= Hash[parsed_query]
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hanami-model-1.3.3 lib/hanami/model/sql/consoles/postgresql.rb