Skip to content

Commit e60f6c9

Browse files
committed
Merge pull request #3 from aef/master
First step to Rails 3 compatibility
2 parents 4458c66 + 9c946ad commit e60f6c9

File tree

3 files changed

+195
-0
lines changed

3 files changed

+195
-0
lines changed

lib/pg.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# This is a compatibility layer for using the pure Ruby postgres-pr instead of
2+
# the C interface of postgres.
3+
4+
require 'postgres-pr/postgres-compat'

lib/postgres-pr/pg-compat.rb

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
# This is a compatibility layer for using the pure Ruby postgres-pr instead of
2+
# the C interface of postgres.
3+
4+
require 'rexml/syncenumerator'
5+
require 'postgres-pr/connection'
6+
7+
class PGconn
8+
PQTRANS_IDLE = 0 #(connection idle)
9+
PQTRANS_INTRANS = 2 #(idle, within transaction block)
10+
PQTRANS_INERROR = 3 #(idle, within failed transaction)
11+
PQTRANS_UNKNOWN = 4 #(cannot determine status)
12+
13+
class << self
14+
alias connect new
15+
end
16+
17+
def initialize(host, port, options, tty, database, user, auth)
18+
uri =
19+
if host.nil?
20+
nil
21+
elsif host[0] != ?/
22+
"tcp://#{ host }:#{ port }"
23+
else
24+
"unix:#{ host }/.s.PGSQL.#{ port }"
25+
end
26+
@host = host
27+
@db = database
28+
@user = user
29+
@conn = PostgresPR::Connection.new(database, user, auth, uri)
30+
end
31+
32+
def close
33+
@conn.close
34+
end
35+
36+
attr_reader :host, :db, :user
37+
38+
def query(sql)
39+
PGresult.new(@conn.query(sql))
40+
end
41+
42+
alias exec query
43+
44+
def transaction_status
45+
@conn.transaction_status
46+
end
47+
48+
def self.escape(str)
49+
str.gsub("'","''").gsub("\\", "\\\\\\\\")
50+
end
51+
52+
def escape(str)
53+
self.class.escape(str)
54+
end
55+
56+
def notice_processor
57+
@conn.notice_processor
58+
end
59+
60+
def notice_processor=(np)
61+
@conn.notice_processor = np
62+
end
63+
64+
def self.quote_ident(name)
65+
%("#{name}")
66+
end
67+
68+
end
69+
70+
class PGresult
71+
include Enumerable
72+
73+
EMPTY_QUERY = 0
74+
COMMAND_OK = 1
75+
TUPLES_OK = 2
76+
COPY_OUT = 3
77+
COPY_IN = 4
78+
BAD_RESPONSE = 5
79+
NONFATAL_ERROR = 6
80+
FATAL_ERROR = 7
81+
82+
def each(&block)
83+
@result.each(&block)
84+
end
85+
86+
def [](index)
87+
@result[index]
88+
end
89+
90+
def initialize(res)
91+
@res = res
92+
@fields = @res.fields.map {|f| f.name}
93+
@result = []
94+
@res.rows.each do |row|
95+
@result << REXML::SyncEnumerator.new(fields, row).map {|name, value| [name, value]}
96+
end
97+
end
98+
99+
# TODO: status, getlength, cmdstatus
100+
101+
attr_reader :result, :fields
102+
103+
def num_tuples
104+
@result.size
105+
end
106+
107+
alias :ntuples :num_tuples
108+
109+
def num_fields
110+
@fields.size
111+
end
112+
113+
alias :nfields :num_fields
114+
115+
def fieldname(index)
116+
@fields[index]
117+
end
118+
119+
def fieldnum(name)
120+
@fields.index(name)
121+
end
122+
123+
def type(index)
124+
# TODO: correct?
125+
@res.fields[index].type_oid
126+
end
127+
128+
alias :ftype :type
129+
130+
def size(index)
131+
raise
132+
# TODO: correct?
133+
@res.fields[index].typlen
134+
end
135+
136+
def getvalue(tup_num, field_num)
137+
@result[tup_num][field_num]
138+
end
139+
140+
def status
141+
if num_tuples > 0
142+
TUPLES_OK
143+
else
144+
COMMAND_OK
145+
end
146+
end
147+
148+
def cmdstatus
149+
@res.cmd_tag || ''
150+
end
151+
152+
# free the result set
153+
def clear
154+
@res = @fields = @result = nil
155+
end
156+
157+
# Returns the number of rows affected by the SQL command
158+
def cmdtuples
159+
case @res.cmd_tag
160+
when nil
161+
return nil
162+
when /^INSERT\s+(\d+)\s+(\d+)$/, /^(DELETE|UPDATE|MOVE|FETCH)\s+(\d+)$/
163+
$2.to_i
164+
else
165+
nil
166+
end
167+
end
168+
169+
alias :cmd_tuples :cmdtuples
170+
171+
end
172+
173+
class PGError < Exception
174+
end

lib/postgres-pr/postgres-compat.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
require 'postgres-pr/connection'
55

66
class PGconn
7+
PQTRANS_IDLE = 0 #(connection idle)
8+
PQTRANS_INTRANS = 2 #(idle, within transaction block)
9+
PQTRANS_INERROR = 3 #(idle, within failed transaction)
10+
PQTRANS_UNKNOWN = 4 #(cannot determine status)
11+
712
class << self
813
alias connect new
914
end
@@ -43,6 +48,10 @@ def self.escape(str)
4348
str.gsub("'","''").gsub("\\", "\\\\\\\\")
4449
end
4550

51+
def escape(str)
52+
self.class.escape(str)
53+
end
54+
4655
def notice_processor
4756
@conn.notice_processor
4857
end
@@ -91,10 +100,14 @@ def num_tuples
91100
@result.size
92101
end
93102

103+
alias :ntuples :num_tuples
104+
94105
def num_fields
95106
@fields.size
96107
end
97108

109+
alias :nfields :num_fields
110+
98111
def fieldname(index)
99112
@fields[index]
100113
end
@@ -107,6 +120,8 @@ def type(index)
107120
# TODO: correct?
108121
@res.fields[index].type_oid
109122
end
123+
124+
alias :ftype :type
110125

111126
def size(index)
112127
raise
@@ -146,6 +161,8 @@ def cmdtuples
146161
nil
147162
end
148163
end
164+
165+
alias :cmd_tuples :cmdtuples
149166

150167
end
151168

0 commit comments

Comments
 (0)