Class: PostgresPR::Connection

### Home Classes Methods

### In Files

- lib/postgres-pr/connection.rb

### Parent

Object

### Namespace

- CLASS PostgresPR::Connection::Result

### Methods

- ::new
- #close
- #query
- #transaction_status

### Files

- 1q

### Class/Module Index

Quicksearch

- PostgresPR
- PostgresPR::Authentification
- PostgresPR::AuthentificationClearTextPassword
- PostgresPR::AuthentificationCryptPassword
- PostgresPR::AuthentificationKerberosV4
- PostgresPR::AuthentificationKerberosV5
- PostgresPR::AuthentificationMD5Password
- PostgresPR::AuthentificationOk
- PostgresPR::AuthentificationSCMCredential
- PostgresPR::BackendKeyData
- PostgresPR::CommandComplete
- PostgresPR::Connection
- PostgresPR::Connection::Result
- PostgresPR::CopyInResponse
- PostgresPR::CopyOutResponse
- PostgresPR::DataRow
- PostgresPR::DumpError
- PostgresPR::EmptyQueryResponse
- PostgresPR::ErrorResponse
- PostgresPR::Message
- PostgresPR::NoticeErrorMixin
- PostgresPR::NoticeResponse
- PostgresPR::ParameterStatus
- PostgresPR::Parse
- PostgresPR::ParseComplete
- PostgresPR::ParseError
- PostgresPR::PasswordMessage
- PostgresPR::Query
- PostgresPR::ReadyForQuery
- PostgresPR::RowDescription
- PostgresPR::RowDescription::FieldInfo
- PostgresPR::SSLRequest
- PostgresPR::SaltedAuthentificationMixin
- PostgresPR::StartupMessage
- PostgresPR::Terminate
- PostgresPR::UnknownAuthType
- PostgresPR::UnknownMessageType
- Buffer
- Buffer::EOF
- Buffer::Error
- Postgres
- Postgres::Conversion
- Postgres::Conversion::ConversionError
- BinaryReaderMixin
- BinaryWriterMixin
- ByteOrder
- IO
- PGError
- PGconn
- PGresult
- TC_Conversion

No matching classes.

# PostgresPR::Connection

### Constants

DEFAULT_HOST
DEFAULT_PATH
DEFAULT_PORT
DEFAULT_URI

### Attributes

notice_processor[RW]

A block which is called with the NoticeResponse object as parameter.

### Public Class Methods

new(database, user, password=nil, uri = nil) click to toggle source

    # File lib/postgres-pr/connection.rb, line 46
    def initialize(database, user, password=nil, uri = nil)
      uri ||= DEFAULT_URI

      @transaction_status = nil
      @params = {}
      establish_connection(uri)

      @conn << StartupMessage.new(PROTO_VERSION, 'user' => user, 'database' => database).dump

      loop do
        msg = Message.read(@conn)

        case msg
        when AuthentificationClearTextPassword
          raise ArgumentError, "no password specified" if password.nil?
          @conn << PasswordMessage.new(password).dump

        when AuthentificationCryptPassword
          raise ArgumentError, "no password specified" if password.nil?
          @conn << PasswordMessage.new(password.crypt(msg.salt)).dump

        when AuthentificationMD5Password
          raise ArgumentError, "no password specified" if password.nil?
          require 'digest/md5'

          m = Digest::MD5.hexdigest(password + user)
          m = Digest::MD5.hexdigest(m + msg.salt)
          m = 'md5' + m
          @conn << PasswordMessage.new(m).dump

        when AuthentificationKerberosV4, AuthentificationKerberosV5, AuthentificationSCMCredential
          raise "unsupported authentification"

        when AuthentificationOk
        when ErrorResponse
          raise msg.field_values.join("\t")
        when NoticeResponse
          @notice_processor.call(msg) if @notice_processor
        when ParameterStatus
          @params[msg.key] = msg.value
        when BackendKeyData
          # TODO
          #p msg
        when ReadyForQuery
          @transaction_status = msg.backend_transaction_status_indicator
          break
        else
          raise "unhandled message type"
        end
      end
    end

### Public Instance Methods

close() click to toggle source

    # File lib/postgres-pr/connection.rb, line 98
    def close
      raise "connection already closed" if @conn.nil?
      @conn.shutdown
      @conn = nil
    end

query(sql) click to toggle source

    # File lib/postgres-pr/connection.rb, line 111
    def query(sql)
      @conn << Query.dump(sql)

      result = Result.new
      errors = []

      loop do
        msg = Message.read(@conn)
        case msg
        when DataRow
          result.rows << msg.columns
        when CommandComplete
          result.cmd_tag = msg.cmd_tag
        when ReadyForQuery
          @transaction_status = msg.backend_transaction_status_indicator
          break
        when RowDescription
          result.fields = msg.fields
        when CopyInResponse
        when CopyOutResponse
        when EmptyQueryResponse
        when ErrorResponse
          # TODO
          errors << msg
        when NoticeResponse
          @notice_processor.call(msg) if @notice_processor
        else
          # TODO
        end
      end

      raise errors.map{|e| e.field_values.join("\t") }.join("\n") unless errors.empty?

      result
    end

transaction_status() click to toggle source

Returns one of the following statuses:

    PQTRANS_IDLE    = 0 (connection idle)
    PQTRANS_INTRANS = 2 (idle, within transaction block)
    PQTRANS_INERROR = 3 (idle, within failed transaction)
    PQTRANS_UNKNOWN = 4 (cannot determine status)

Not yet implemented is:

    PQTRANS_ACTIVE  = 1 (command in progress)

    # File lib/postgres-pr/connection.rb, line 33
    def transaction_status
      case @transaction_status
      when II
        0
      when TT
        2
      when EE
        3
      else
        4
      end
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.