postgres-pr 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/postgres-pr/connection.rb +13 -4
- data/lib/postgres-pr/message.rb +2 -0
- data/lib/postgres.rb +53 -0
- metadata +3 -2
@@ -48,35 +48,44 @@ class Connection
|
|
48
48
|
}
|
49
49
|
end
|
50
50
|
|
51
|
+
class Result
|
52
|
+
attr_accessor :rows, :fields
|
53
|
+
def initialize(rows=[], fields=[])
|
54
|
+
@rows, @fields = rows, fields
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
51
58
|
def query(sql)
|
52
59
|
@mutex.synchronize {
|
53
60
|
@conn << Query.dump(sql)
|
54
61
|
|
55
|
-
|
62
|
+
result = Result.new
|
56
63
|
|
57
64
|
loop do
|
58
65
|
msg = Message.read(@conn)
|
59
66
|
case msg
|
60
67
|
when DataRow
|
61
|
-
rows << msg.columns
|
68
|
+
result.rows << msg.columns
|
62
69
|
when CommandComplete
|
63
70
|
when ReadyForQuery
|
64
71
|
break
|
65
72
|
when RowDescription
|
66
|
-
|
73
|
+
result.fields = msg.fields
|
67
74
|
when CopyInResponse
|
68
75
|
when CopyOutResponse
|
69
76
|
when EmptyQueryResponse
|
77
|
+
p "EMPTY!"
|
70
78
|
when ErrorResponse
|
71
79
|
p msg
|
72
80
|
raise
|
73
81
|
when NoticeResponse
|
82
|
+
p msg
|
74
83
|
# TODO
|
75
84
|
else
|
76
85
|
raise
|
77
86
|
end
|
78
87
|
end
|
79
|
-
|
88
|
+
result
|
80
89
|
}
|
81
90
|
end
|
82
91
|
|
data/lib/postgres-pr/message.rb
CHANGED
@@ -399,6 +399,7 @@ end
|
|
399
399
|
|
400
400
|
class RowDescription < Message
|
401
401
|
register_message_type ?T
|
402
|
+
fields :fields
|
402
403
|
|
403
404
|
class FieldInfo < Struct.new(:name, :oid, :attr_nr, :type_oid, :typlen, :atttypmod, :formatcode); end
|
404
405
|
|
@@ -430,6 +431,7 @@ class RowDescription < Message
|
|
430
431
|
f.typlen = buffer.read_int16_network
|
431
432
|
f.atttypmod = buffer.read_int32_network
|
432
433
|
f.formatcode = buffer.read_int16_network
|
434
|
+
f
|
433
435
|
}
|
434
436
|
end
|
435
437
|
end
|
data/lib/postgres.rb
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
# This is a compatibility layer for using the pure Ruby postgres-pr instead of
|
2
|
+
# the C interface of postgres.
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'postgres.so'
|
6
|
+
rescue LoadError
|
7
|
+
require 'postgres-pr/connection'
|
8
|
+
class PGconn
|
9
|
+
class << self
|
10
|
+
alias connect new
|
11
|
+
end
|
12
|
+
|
13
|
+
def initialize(host, port, options, tty, database, user, auth)
|
14
|
+
uri =
|
15
|
+
if host[0] != ?/
|
16
|
+
"tcp://#{ host }:#{ port }"
|
17
|
+
else
|
18
|
+
"unix:#{ host }/.s.PGSQL.#{ port }"
|
19
|
+
end
|
20
|
+
|
21
|
+
@db = database
|
22
|
+
@conn = Connection.new(database, user, auth, uri)
|
23
|
+
end
|
24
|
+
|
25
|
+
attr_reader :db
|
26
|
+
|
27
|
+
def query(sql)
|
28
|
+
PGresult.new(@conn.query(sql))
|
29
|
+
end
|
30
|
+
|
31
|
+
alias exec query
|
32
|
+
end
|
33
|
+
|
34
|
+
class PGresult
|
35
|
+
def initialize(res)
|
36
|
+
@res = res
|
37
|
+
end
|
38
|
+
|
39
|
+
def fields
|
40
|
+
@res.fields.map {|f| f.name}
|
41
|
+
end
|
42
|
+
|
43
|
+
def result
|
44
|
+
@res.rows.map {|row| row.map {|c| c || ""} }
|
45
|
+
end
|
46
|
+
|
47
|
+
include Enumerable
|
48
|
+
|
49
|
+
def each(&block)
|
50
|
+
result.each(&block)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
metadata
CHANGED
@@ -3,7 +3,7 @@ rubygems_version: 0.8.1
|
|
3
3
|
specification_version: 1
|
4
4
|
name: postgres-pr
|
5
5
|
version: !ruby/object:Gem::Version
|
6
|
-
version: 0.0
|
6
|
+
version: 0.1.0
|
7
7
|
date: 2004-11-18
|
8
8
|
summary: A pure Ruby interface to the PostgreSQL database
|
9
9
|
require_paths:
|
@@ -27,8 +27,9 @@ required_ruby_version: !ruby/object:Gem::Version::Requirement
|
|
27
27
|
platform: ruby
|
28
28
|
files:
|
29
29
|
- lib/postgres-pr
|
30
|
-
- lib/binary_writer.rb
|
31
30
|
- lib/byteorder.rb
|
31
|
+
- lib/postgres.rb
|
32
|
+
- lib/binary_writer.rb
|
32
33
|
- lib/binary_reader.rb
|
33
34
|
- lib/buffer.rb
|
34
35
|
- lib/postgres-pr/typeconv
|