Class: PostgresPR::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/postgres-pr/connection.rb

Defined Under Namespace

Classes: Result

Constant Summary collapse

DEFAULT_PORT =
5432
DEFAULT_HOST =
'localhost'
DEFAULT_PATH =
'/tmp'
DEFAULT_URI =
if RUBY_PLATFORM.include?('win')
  'tcp://' + DEFAULT_HOST + ':' + DEFAULT_PORT.to_s 
else
  'unix:' + File.join(DEFAULT_PATH, '.s.PGSQL.' + DEFAULT_PORT.to_s)  
end

Instance Method Summary collapse

Constructor Details

#initialize(database, user, password = nil, uri = nil) ⇒ Connection

sync



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/postgres-pr/connection.rb', line 19

def initialize(database, user, password=nil, uri = nil)
  uri ||= DEFAULT_URI

  raise unless @mutex.nil?

  @mutex = Mutex.new

  @mutex.synchronize {
    @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 "authentification failed"
      when NoticeResponse
        # TODO
      when ParameterStatus
        @params[msg.key] = msg.value
      when BackendKeyData
        # TODO
        #p msg
      when ReadyForQuery
        # TODO: use transaction status
        break
      else
        raise "unhandled message type"
      end
    end
  }
end

Instance Method Details

#query(sql) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/postgres-pr/connection.rb', line 83

def query(sql)
  @mutex.synchronize {
    @conn << Query.dump(sql)

    result = Result.new
    errors = []

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

    raise errors.map{|e| e.inspect}.join("   ") unless errors.empty?

    result
  }
end