pg 0.18.1 → 1.5.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- checksums.yaml.gz.sig +0 -0
- data/.appveyor.yml +42 -0
- data/.gems +6 -0
- data/.github/workflows/binary-gems.yml +117 -0
- data/.github/workflows/source-gem.yml +141 -0
- data/.gitignore +22 -0
- data/.hgsigs +34 -0
- data/.hgtags +41 -0
- data/.irbrc +23 -0
- data/.pryrc +23 -0
- data/.tm_properties +21 -0
- data/.travis.yml +49 -0
- data/BSDL +2 -2
- data/Gemfile +17 -0
- data/History.md +901 -0
- data/Manifest.txt +8 -21
- data/README-Windows.rdoc +17 -28
- data/README.ja.md +300 -0
- data/README.md +286 -0
- data/Rakefile +43 -131
- data/Rakefile.cross +89 -70
- data/certs/ged.pem +24 -0
- data/certs/kanis@comcard.de.pem +20 -0
- data/certs/larskanis-2022.pem +26 -0
- data/certs/larskanis-2023.pem +24 -0
- data/certs/larskanis-2024.pem +24 -0
- data/ext/errorcodes.def +113 -0
- data/ext/errorcodes.rb +1 -1
- data/ext/errorcodes.txt +36 -2
- data/ext/extconf.rb +128 -55
- data/ext/gvl_wrappers.c +8 -0
- data/ext/gvl_wrappers.h +44 -33
- data/ext/pg.c +228 -202
- data/ext/pg.h +108 -99
- data/ext/pg_binary_decoder.c +164 -16
- data/ext/pg_binary_encoder.c +249 -22
- data/ext/pg_coder.c +189 -44
- data/ext/pg_connection.c +1889 -1195
- data/ext/pg_copy_coder.c +398 -42
- data/ext/pg_errors.c +1 -1
- data/ext/pg_record_coder.c +522 -0
- data/ext/pg_result.c +729 -234
- data/ext/pg_text_decoder.c +635 -52
- data/ext/pg_text_encoder.c +294 -130
- data/ext/pg_tuple.c +572 -0
- data/ext/pg_type_map.c +64 -23
- data/ext/pg_type_map_all_strings.c +21 -7
- data/ext/pg_type_map_by_class.c +59 -27
- data/ext/pg_type_map_by_column.c +86 -43
- data/ext/pg_type_map_by_mri_type.c +50 -21
- data/ext/pg_type_map_by_oid.c +62 -29
- data/ext/pg_type_map_in_ruby.c +59 -28
- data/ext/{util.c → pg_util.c} +13 -13
- data/ext/{util.h → pg_util.h} +3 -3
- data/lib/pg/basic_type_map_based_on_result.rb +67 -0
- data/lib/pg/basic_type_map_for_queries.rb +202 -0
- data/lib/pg/basic_type_map_for_results.rb +104 -0
- data/lib/pg/basic_type_registry.rb +303 -0
- data/lib/pg/binary_decoder/date.rb +9 -0
- data/lib/pg/binary_decoder/timestamp.rb +26 -0
- data/lib/pg/binary_encoder/timestamp.rb +20 -0
- data/lib/pg/coder.rb +36 -13
- data/lib/pg/connection.rb +813 -74
- data/lib/pg/exceptions.rb +16 -2
- data/lib/pg/result.rb +24 -7
- data/lib/pg/text_decoder/date.rb +18 -0
- data/lib/pg/text_decoder/inet.rb +9 -0
- data/lib/pg/text_decoder/json.rb +14 -0
- data/lib/pg/text_decoder/numeric.rb +9 -0
- data/lib/pg/text_decoder/timestamp.rb +30 -0
- data/lib/pg/text_encoder/date.rb +12 -0
- data/lib/pg/text_encoder/inet.rb +28 -0
- data/lib/pg/text_encoder/json.rb +14 -0
- data/lib/pg/text_encoder/numeric.rb +9 -0
- data/lib/pg/text_encoder/timestamp.rb +24 -0
- data/lib/pg/tuple.rb +30 -0
- data/lib/pg/type_map_by_column.rb +3 -2
- data/lib/pg/version.rb +4 -0
- data/lib/pg.rb +106 -41
- data/misc/openssl-pg-segfault.rb +31 -0
- data/misc/postgres/History.txt +9 -0
- data/misc/postgres/Manifest.txt +5 -0
- data/misc/postgres/README.txt +21 -0
- data/misc/postgres/Rakefile +21 -0
- data/misc/postgres/lib/postgres.rb +16 -0
- data/misc/ruby-pg/History.txt +9 -0
- data/misc/ruby-pg/Manifest.txt +5 -0
- data/misc/ruby-pg/README.txt +21 -0
- data/misc/ruby-pg/Rakefile +21 -0
- data/misc/ruby-pg/lib/ruby/pg.rb +16 -0
- data/pg.gemspec +34 -0
- data/rakelib/task_extension.rb +46 -0
- data/sample/array_insert.rb +1 -1
- data/sample/async_api.rb +4 -8
- data/sample/async_copyto.rb +1 -1
- data/sample/async_mixed.rb +1 -1
- data/sample/check_conn.rb +1 -1
- data/sample/copydata.rb +71 -0
- data/sample/copyfrom.rb +1 -1
- data/sample/copyto.rb +1 -1
- data/sample/cursor.rb +1 -1
- data/sample/disk_usage_report.rb +6 -15
- data/sample/issue-119.rb +2 -2
- data/sample/losample.rb +1 -1
- data/sample/minimal-testcase.rb +2 -2
- data/sample/notify_wait.rb +1 -1
- data/sample/pg_statistics.rb +6 -15
- data/sample/replication_monitor.rb +9 -18
- data/sample/test_binary_values.rb +1 -1
- data/sample/wal_shipper.rb +2 -2
- data/sample/warehouse_partitions.rb +8 -17
- data.tar.gz.sig +0 -0
- metadata +135 -207
- metadata.gz.sig +0 -0
- data/ChangeLog +0 -5378
- data/History.rdoc +0 -297
- data/README.ja.rdoc +0 -14
- data/README.rdoc +0 -161
- data/lib/pg/basic_type_mapping.rb +0 -399
- data/lib/pg/constants.rb +0 -11
- data/lib/pg/text_decoder.rb +0 -42
- data/lib/pg/text_encoder.rb +0 -27
- data/spec/data/expected_trace.out +0 -26
- data/spec/data/random_binary_data +0 -0
- data/spec/helpers.rb +0 -355
- data/spec/pg/basic_type_mapping_spec.rb +0 -251
- data/spec/pg/connection_spec.rb +0 -1459
- data/spec/pg/result_spec.rb +0 -449
- data/spec/pg/type_map_by_class_spec.rb +0 -138
- data/spec/pg/type_map_by_column_spec.rb +0 -222
- data/spec/pg/type_map_by_mri_type_spec.rb +0 -136
- data/spec/pg/type_map_by_oid_spec.rb +0 -149
- data/spec/pg/type_map_in_ruby_spec.rb +0 -164
- data/spec/pg/type_map_spec.rb +0 -22
- data/spec/pg/type_spec.rb +0 -665
- data/spec/pg_spec.rb +0 -50
@@ -1,399 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
|
3
|
-
require 'pg' unless defined?( PG )
|
4
|
-
|
5
|
-
module PG::BasicTypeRegistry
|
6
|
-
# An instance of this class stores the coders that should be used for a given wire format (text or binary)
|
7
|
-
# and type cast direction (encoder or decoder).
|
8
|
-
class CoderMap
|
9
|
-
# Hash of text types that don't require quotation, when used within composite types.
|
10
|
-
# type.name => true
|
11
|
-
DONT_QUOTE_TYPES = %w[
|
12
|
-
int2 int4 int8
|
13
|
-
float4 float8
|
14
|
-
oid
|
15
|
-
bool
|
16
|
-
date timestamp timestamptz
|
17
|
-
].inject({}){|h,e| h[e] = true; h }
|
18
|
-
|
19
|
-
def initialize(result, coders_by_name, format, arraycoder)
|
20
|
-
coder_map = {}
|
21
|
-
|
22
|
-
_ranges, nodes = result.partition { |row| row['typinput'] == 'range_in' }
|
23
|
-
leaves, nodes = nodes.partition { |row| row['typelem'].to_i == 0 }
|
24
|
-
arrays, nodes = nodes.partition { |row| row['typinput'] == 'array_in' }
|
25
|
-
|
26
|
-
# populate the enum types
|
27
|
-
_enums, leaves = leaves.partition { |row| row['typinput'] == 'enum_in' }
|
28
|
-
# enums.each do |row|
|
29
|
-
# coder_map[row['oid'].to_i] = OID::Enum.new
|
30
|
-
# end
|
31
|
-
|
32
|
-
# populate the base types
|
33
|
-
leaves.find_all { |row| coders_by_name.key?(row['typname']) }.each do |row|
|
34
|
-
coder = coders_by_name[row['typname']].dup
|
35
|
-
coder.oid = row['oid'].to_i
|
36
|
-
coder.name = row['typname']
|
37
|
-
coder.format = format
|
38
|
-
coder_map[coder.oid] = coder
|
39
|
-
end
|
40
|
-
|
41
|
-
_records_by_oid = result.group_by { |row| row['oid'] }
|
42
|
-
|
43
|
-
# populate composite types
|
44
|
-
# nodes.each do |row|
|
45
|
-
# add_oid row, records_by_oid, coder_map
|
46
|
-
# end
|
47
|
-
|
48
|
-
if arraycoder
|
49
|
-
# populate array types
|
50
|
-
arrays.each do |row|
|
51
|
-
elements_coder = coder_map[row['typelem'].to_i]
|
52
|
-
next unless elements_coder
|
53
|
-
|
54
|
-
coder = arraycoder.new
|
55
|
-
coder.oid = row['oid'].to_i
|
56
|
-
coder.name = row['typname']
|
57
|
-
coder.format = format
|
58
|
-
coder.elements_type = elements_coder
|
59
|
-
coder.needs_quotation = !DONT_QUOTE_TYPES[elements_coder.name]
|
60
|
-
coder_map[coder.oid] = coder
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
|
-
# populate range types
|
65
|
-
# ranges.find_all { |row| coder_map.key? row['rngsubtype'].to_i }.each do |row|
|
66
|
-
# subcoder = coder_map[row['rngsubtype'].to_i]
|
67
|
-
# range = OID::Range.new subcoder
|
68
|
-
# coder_map[row['oid'].to_i] = range
|
69
|
-
# end
|
70
|
-
|
71
|
-
@coders = coder_map.values
|
72
|
-
@coders_by_name = @coders.inject({}){|h, t| h[t.name] = t; h }
|
73
|
-
@coders_by_oid = @coders.inject({}){|h, t| h[t.oid] = t; h }
|
74
|
-
@typenames_by_oid = result.inject({}){|h, t| h[t['oid'].to_i] = t['typname']; h }
|
75
|
-
end
|
76
|
-
|
77
|
-
attr_reader :coders
|
78
|
-
attr_reader :coders_by_oid
|
79
|
-
attr_reader :coders_by_name
|
80
|
-
attr_reader :typenames_by_oid
|
81
|
-
|
82
|
-
def coder_by_name(name)
|
83
|
-
@coders_by_name[name]
|
84
|
-
end
|
85
|
-
|
86
|
-
def coder_by_oid(oid)
|
87
|
-
@coders_by_oid[oid]
|
88
|
-
end
|
89
|
-
end
|
90
|
-
|
91
|
-
private
|
92
|
-
|
93
|
-
def supports_ranges?(connection)
|
94
|
-
connection.server_version >= 90200
|
95
|
-
end
|
96
|
-
|
97
|
-
def build_coder_maps(connection)
|
98
|
-
if supports_ranges?(connection)
|
99
|
-
result = connection.exec <<-SQL
|
100
|
-
SELECT t.oid, t.typname, t.typelem, t.typdelim, t.typinput, r.rngsubtype
|
101
|
-
FROM pg_type as t
|
102
|
-
LEFT JOIN pg_range as r ON oid = rngtypid
|
103
|
-
SQL
|
104
|
-
else
|
105
|
-
result = connection.exec <<-SQL
|
106
|
-
SELECT t.oid, t.typname, t.typelem, t.typdelim, t.typinput
|
107
|
-
FROM pg_type as t
|
108
|
-
SQL
|
109
|
-
end
|
110
|
-
|
111
|
-
[
|
112
|
-
[0, :encoder, PG::TextEncoder::Array],
|
113
|
-
[0, :decoder, PG::TextDecoder::Array],
|
114
|
-
[1, :encoder, nil],
|
115
|
-
[1, :decoder, nil],
|
116
|
-
].inject([]) do |h, (format, direction, arraycoder)|
|
117
|
-
h[format] ||= {}
|
118
|
-
h[format][direction] = CoderMap.new result, CODERS_BY_NAME[format][direction], format, arraycoder
|
119
|
-
h
|
120
|
-
end
|
121
|
-
end
|
122
|
-
|
123
|
-
ValidFormats = { 0 => true, 1 => true }
|
124
|
-
ValidDirections = { :encoder => true, :decoder => true }
|
125
|
-
|
126
|
-
def check_format_and_direction(format, direction)
|
127
|
-
raise(ArgumentError, "Invalid format value %p" % format) unless ValidFormats[format]
|
128
|
-
raise(ArgumentError, "Invalid direction %p" % direction) unless ValidDirections[direction]
|
129
|
-
end
|
130
|
-
protected :check_format_and_direction
|
131
|
-
|
132
|
-
# The key of this hash maps to the `typname` column from the table.
|
133
|
-
# encoder_map is then dynamically built with oids as the key and Type
|
134
|
-
# objects as values.
|
135
|
-
CODERS_BY_NAME = []
|
136
|
-
|
137
|
-
# Register an OID type named +name+ with a typecasting encoder and decoder object in
|
138
|
-
# +type+. +name+ should correspond to the `typname` column in
|
139
|
-
# the `pg_type` table.
|
140
|
-
def self.register_type(format, name, encoder_class, decoder_class)
|
141
|
-
CODERS_BY_NAME[format] ||= { encoder: {}, decoder: {} }
|
142
|
-
CODERS_BY_NAME[format][:encoder][name] = encoder_class.new(name: name, format: format) if encoder_class
|
143
|
-
CODERS_BY_NAME[format][:decoder][name] = decoder_class.new(name: name, format: format) if decoder_class
|
144
|
-
end
|
145
|
-
|
146
|
-
# Alias the +old+ type to the +new+ type.
|
147
|
-
def self.alias_type(format, new, old)
|
148
|
-
CODERS_BY_NAME[format][:encoder][new] = CODERS_BY_NAME[format][:encoder][old]
|
149
|
-
CODERS_BY_NAME[format][:decoder][new] = CODERS_BY_NAME[format][:decoder][old]
|
150
|
-
end
|
151
|
-
|
152
|
-
register_type 0, 'int2', PG::TextEncoder::Integer, PG::TextDecoder::Integer
|
153
|
-
alias_type 0, 'int4', 'int2'
|
154
|
-
alias_type 0, 'int8', 'int2'
|
155
|
-
alias_type 0, 'oid', 'int2'
|
156
|
-
|
157
|
-
# register_type 0, 'numeric', OID::Decimal.new
|
158
|
-
register_type 0, 'text', PG::TextEncoder::String, PG::TextDecoder::String
|
159
|
-
alias_type 0, 'varchar', 'text'
|
160
|
-
alias_type 0, 'char', 'text'
|
161
|
-
alias_type 0, 'bpchar', 'text'
|
162
|
-
alias_type 0, 'xml', 'text'
|
163
|
-
|
164
|
-
# # FIXME: why are we keeping these types as strings?
|
165
|
-
# alias_type 'tsvector', 'text'
|
166
|
-
# alias_type 'interval', 'text'
|
167
|
-
# alias_type 'macaddr', 'text'
|
168
|
-
# alias_type 'uuid', 'text'
|
169
|
-
#
|
170
|
-
# register_type 'money', OID::Money.new
|
171
|
-
# There is no PG::TextEncoder::Bytea, because it's simple and more efficient to send bytea-data
|
172
|
-
# in binary format, either with PG::BinaryEncoder::Bytea or in Hash param format.
|
173
|
-
register_type 0, 'bytea', nil, PG::TextDecoder::Bytea
|
174
|
-
register_type 0, 'bool', PG::TextEncoder::Boolean, PG::TextDecoder::Boolean
|
175
|
-
# register_type 'bit', OID::Bit.new
|
176
|
-
# register_type 'varbit', OID::Bit.new
|
177
|
-
#
|
178
|
-
register_type 0, 'float4', PG::TextEncoder::Float, PG::TextDecoder::Float
|
179
|
-
alias_type 0, 'float8', 'float4'
|
180
|
-
|
181
|
-
register_type 0, 'timestamp', PG::TextEncoder::TimestampWithoutTimeZone, PG::TextDecoder::TimestampWithoutTimeZone
|
182
|
-
register_type 0, 'timestamptz', PG::TextEncoder::TimestampWithTimeZone, PG::TextDecoder::TimestampWithTimeZone
|
183
|
-
register_type 0, 'date', PG::TextEncoder::Date, PG::TextDecoder::Date
|
184
|
-
# register_type 'time', OID::Time.new
|
185
|
-
#
|
186
|
-
# register_type 'path', OID::Text.new
|
187
|
-
# register_type 'point', OID::Point.new
|
188
|
-
# register_type 'polygon', OID::Text.new
|
189
|
-
# register_type 'circle', OID::Text.new
|
190
|
-
# register_type 'hstore', OID::Hstore.new
|
191
|
-
# register_type 'json', OID::Json.new
|
192
|
-
# register_type 'citext', OID::Text.new
|
193
|
-
# register_type 'ltree', OID::Text.new
|
194
|
-
#
|
195
|
-
# register_type 'cidr', OID::Cidr.new
|
196
|
-
# alias_type 'inet', 'cidr'
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
register_type 1, 'int2', PG::BinaryEncoder::Int2, PG::BinaryDecoder::Integer
|
201
|
-
register_type 1, 'int4', PG::BinaryEncoder::Int4, PG::BinaryDecoder::Integer
|
202
|
-
register_type 1, 'int8', PG::BinaryEncoder::Int8, PG::BinaryDecoder::Integer
|
203
|
-
alias_type 1, 'oid', 'int2'
|
204
|
-
|
205
|
-
register_type 1, 'text', PG::BinaryEncoder::String, PG::BinaryDecoder::String
|
206
|
-
alias_type 1, 'varchar', 'text'
|
207
|
-
alias_type 1, 'char', 'text'
|
208
|
-
alias_type 1, 'bpchar', 'text'
|
209
|
-
alias_type 1, 'xml', 'text'
|
210
|
-
|
211
|
-
register_type 1, 'bytea', PG::BinaryEncoder::Bytea, PG::BinaryDecoder::Bytea
|
212
|
-
register_type 1, 'bool', PG::BinaryEncoder::Boolean, PG::BinaryDecoder::Boolean
|
213
|
-
register_type 1, 'float4', nil, PG::BinaryDecoder::Float
|
214
|
-
register_type 1, 'float8', nil, PG::BinaryDecoder::Float
|
215
|
-
end
|
216
|
-
|
217
|
-
# Simple set of rules for type casting common PostgreSQL types to Ruby.
|
218
|
-
#
|
219
|
-
# OIDs of supported type casts are not hard-coded in the sources, but are retrieved from the
|
220
|
-
# PostgreSQL's pg_type table in PG::BasicTypeMapForResults.new .
|
221
|
-
#
|
222
|
-
# Result values are type casted based on the type OID of the given result column.
|
223
|
-
#
|
224
|
-
# Higher level libraries will most likely not make use of this class, but use their
|
225
|
-
# own set of rules to choose suitable encoders and decoders.
|
226
|
-
#
|
227
|
-
# Example:
|
228
|
-
# conn = PG::Connection.new
|
229
|
-
# # Assign a default ruleset for type casts of input and output values.
|
230
|
-
# conn.type_mapping = PG::BasicTypeMapping.new(conn)
|
231
|
-
# # Execute a query.
|
232
|
-
# res = conn.exec_params( "SELECT $1::INT", ['5'] )
|
233
|
-
# # Retrieve and cast the result value. Value format is 0 (text) and OID is 20. Therefore typecasting
|
234
|
-
# # is done by PG::TextDecoder::Integer internally for all value retrieval methods.
|
235
|
-
# res.values # => [[5]]
|
236
|
-
#
|
237
|
-
# PG::TypeMapByOid#fit_to_result(result, false) can be used to generate
|
238
|
-
# a result independent PG::TypeMapByColumn type map, which can subsequently be used
|
239
|
-
# to cast #get_copy_data fields. See also PG::BasicTypeMapBasedOnResult .
|
240
|
-
#
|
241
|
-
class PG::BasicTypeMapForResults < PG::TypeMapByOid
|
242
|
-
include PG::BasicTypeRegistry
|
243
|
-
|
244
|
-
class WarningTypeMap < PG::TypeMapInRuby
|
245
|
-
def initialize(typenames)
|
246
|
-
@already_warned = Hash.new{|h, k| h[k] = {} }
|
247
|
-
@typenames_by_oid = typenames
|
248
|
-
end
|
249
|
-
|
250
|
-
def typecast_result_value(result, _tuple, field)
|
251
|
-
format = result.fformat(field)
|
252
|
-
oid = result.ftype(field)
|
253
|
-
unless @already_warned[format][oid]
|
254
|
-
STDERR.puts "Warning: no type cast defined for type #{@typenames_by_oid[format][oid].inspect} with oid #{oid}. Please cast this type explicitly to TEXT to be safe for future changes."
|
255
|
-
@already_warned[format][oid] = true
|
256
|
-
end
|
257
|
-
super
|
258
|
-
end
|
259
|
-
end
|
260
|
-
|
261
|
-
def initialize(connection)
|
262
|
-
@coder_maps = build_coder_maps(connection)
|
263
|
-
|
264
|
-
# Populate TypeMapByOid hash with decoders
|
265
|
-
@coder_maps.map{|f| f[:decoder].coders }.flatten.each do |coder|
|
266
|
-
add_coder(coder)
|
267
|
-
end
|
268
|
-
|
269
|
-
typenames = @coder_maps.map{|f| f[:decoder].typenames_by_oid }
|
270
|
-
self.default_type_map = WarningTypeMap.new(typenames)
|
271
|
-
end
|
272
|
-
end
|
273
|
-
|
274
|
-
# Simple set of rules for type casting common PostgreSQL types from Ruby
|
275
|
-
# to PostgreSQL.
|
276
|
-
#
|
277
|
-
# OIDs of supported type casts are not hard-coded in the sources, but are retrieved from the
|
278
|
-
# PostgreSQL's pg_type table in PG::BasicTypeMapBasedOnResult.new .
|
279
|
-
#
|
280
|
-
# This class works equal to PG::BasicTypeMapForResults, but does not define decoders for
|
281
|
-
# the given result OIDs, but encoders. So it can be used to type cast field values based on
|
282
|
-
# the type OID retrieved by a separate SQL query.
|
283
|
-
#
|
284
|
-
# PG::TypeMapByOid#build_column_map(result) can be used to generate a result independent
|
285
|
-
# PG::TypeMapByColumn type map, which can subsequently be used to cast query bind parameters
|
286
|
-
# or #put_copy_data fields.
|
287
|
-
#
|
288
|
-
# Example:
|
289
|
-
# conn.exec( "CREATE TEMP TABLE copytable (t TEXT, i INT, ai INT[])" )
|
290
|
-
#
|
291
|
-
# # Retrieve table OIDs per empty result set.
|
292
|
-
# res = conn.exec( "SELECT * FROM copytable LIMIT 0" )
|
293
|
-
# tm = basic_type_mapping.build_column_map( res )
|
294
|
-
# row_encoder = PG::TextEncoder::CopyRow.new type_map: tm
|
295
|
-
#
|
296
|
-
# conn.copy_data( "COPY copytable FROM STDIN", row_encoder ) do |res|
|
297
|
-
# conn.put_copy_data ['a', 123, [5,4,3]]
|
298
|
-
# end
|
299
|
-
class PG::BasicTypeMapBasedOnResult < PG::TypeMapByOid
|
300
|
-
include PG::BasicTypeRegistry
|
301
|
-
|
302
|
-
def initialize(connection)
|
303
|
-
@coder_maps = build_coder_maps(connection)
|
304
|
-
|
305
|
-
# Populate TypeMapByOid hash with encoders
|
306
|
-
@coder_maps.map{|f| f[:encoder].coders }.flatten.each do |coder|
|
307
|
-
add_coder(coder)
|
308
|
-
end
|
309
|
-
end
|
310
|
-
end
|
311
|
-
|
312
|
-
# Simple set of rules for type casting common Ruby types to PostgreSQL.
|
313
|
-
#
|
314
|
-
# OIDs of supported type casts are not hard-coded in the sources, but are retrieved from the
|
315
|
-
# PostgreSQL's pg_type table in PG::BasicTypeMapForQueries.new .
|
316
|
-
#
|
317
|
-
# Query params are type casted based on the MRI internal type of the given value.
|
318
|
-
#
|
319
|
-
# Higher level libraries will most likely not make use of this class, but use their
|
320
|
-
# own set of rules to choose suitable encoders and decoders.
|
321
|
-
#
|
322
|
-
# Example:
|
323
|
-
# conn = PG::Connection.new
|
324
|
-
# # Assign a default ruleset for type casts of input and output values.
|
325
|
-
# conn.type_mapping_for_queries = PG::BasicTypeMapForQueries.new(conn)
|
326
|
-
# # Execute a query. The Integer param value is typecasted internally by PG::BinaryEncoder::Int8.
|
327
|
-
# # The format of the parameter is set to 1 (binary) and the OID of this parameter is set to 20 (int8).
|
328
|
-
# res = conn.exec_params( "SELECT $1", [5] )
|
329
|
-
class PG::BasicTypeMapForQueries < PG::TypeMapByClass
|
330
|
-
include PG::BasicTypeRegistry
|
331
|
-
|
332
|
-
def initialize(connection)
|
333
|
-
@coder_maps = build_coder_maps(connection)
|
334
|
-
|
335
|
-
populate_encoder_list
|
336
|
-
@array_encoders_by_klass = array_encoders_by_klass
|
337
|
-
@anyarray_encoder = coder_by_name(0, :encoder, '_any')
|
338
|
-
end
|
339
|
-
|
340
|
-
private
|
341
|
-
|
342
|
-
def coder_by_name(format, direction, name)
|
343
|
-
check_format_and_direction(format, direction)
|
344
|
-
@coder_maps[format][direction].coder_by_name(name)
|
345
|
-
end
|
346
|
-
|
347
|
-
def populate_encoder_list
|
348
|
-
DEFAULT_TYPE_MAP.each do |klass, selector|
|
349
|
-
if Array === selector
|
350
|
-
format, name, oid_name = selector
|
351
|
-
coder = coder_by_name(format, :encoder, name).dup
|
352
|
-
if oid_name
|
353
|
-
coder.oid = coder_by_name(format, :encoder, oid_name).oid
|
354
|
-
else
|
355
|
-
coder.oid = 0
|
356
|
-
end
|
357
|
-
self[klass] = coder
|
358
|
-
else
|
359
|
-
self[klass] = selector
|
360
|
-
end
|
361
|
-
end
|
362
|
-
end
|
363
|
-
|
364
|
-
def array_encoders_by_klass
|
365
|
-
DEFAULT_ARRAY_TYPE_MAP.inject({}) do |h, (klass, (format, name))|
|
366
|
-
h[klass] = coder_by_name(format, :encoder, name)
|
367
|
-
h
|
368
|
-
end
|
369
|
-
end
|
370
|
-
|
371
|
-
def get_array_type(value)
|
372
|
-
elem = value
|
373
|
-
while elem.kind_of?(Array)
|
374
|
-
elem = elem.first
|
375
|
-
end
|
376
|
-
@array_encoders_by_klass[elem.class] ||
|
377
|
-
elem.class.ancestors.lazy.map{|ancestor| @array_encoders_by_klass[ancestor] }.find{|a| a } ||
|
378
|
-
@anyarray_encoder
|
379
|
-
end
|
380
|
-
|
381
|
-
DEFAULT_TYPE_MAP = {
|
382
|
-
TrueClass => [1, 'bool', 'bool'],
|
383
|
-
FalseClass => [1, 'bool', 'bool'],
|
384
|
-
# We use text format and no type OID for numbers, because setting the OID can lead
|
385
|
-
# to unnecessary type conversions on server side.
|
386
|
-
Integer => [0, 'int8'],
|
387
|
-
Float => [0, 'float8'],
|
388
|
-
Array => :get_array_type,
|
389
|
-
}
|
390
|
-
|
391
|
-
DEFAULT_ARRAY_TYPE_MAP = {
|
392
|
-
TrueClass => [0, '_bool'],
|
393
|
-
FalseClass => [0, '_bool'],
|
394
|
-
Integer => [0, '_int8'],
|
395
|
-
String => [0, '_text'],
|
396
|
-
Float => [0, '_float8'],
|
397
|
-
}
|
398
|
-
|
399
|
-
end
|
data/lib/pg/constants.rb
DELETED
data/lib/pg/text_decoder.rb
DELETED
@@ -1,42 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
|
3
|
-
module PG
|
4
|
-
module TextDecoder
|
5
|
-
class Date < SimpleDecoder
|
6
|
-
ISO_DATE = /\A(\d{4})-(\d\d)-(\d\d)\z/
|
7
|
-
|
8
|
-
def decode(string, tuple=nil, field=nil)
|
9
|
-
if string =~ ISO_DATE
|
10
|
-
Time.new $1.to_i, $2.to_i, $3.to_i
|
11
|
-
else
|
12
|
-
string
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
class TimestampWithoutTimeZone < SimpleDecoder
|
18
|
-
ISO_DATETIME_WITHOUT_TIMEZONE = /\A(\d{4})-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)(\.\d+)?\z/
|
19
|
-
|
20
|
-
def decode(string, tuple=nil, field=nil)
|
21
|
-
if string =~ ISO_DATETIME_WITHOUT_TIMEZONE
|
22
|
-
Time.new $1.to_i, $2.to_i, $3.to_i, $4.to_i, $5.to_i, "#{$6}#{$7}".to_r
|
23
|
-
else
|
24
|
-
string
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
class TimestampWithTimeZone < SimpleDecoder
|
30
|
-
ISO_DATETIME_WITH_TIMEZONE = /\A(\d{4})-(\d\d)-(\d\d) (\d\d):(\d\d):(\d\d)(\.\d+)?([-\+]\d\d)\z/
|
31
|
-
|
32
|
-
def decode(string, tuple=nil, field=nil)
|
33
|
-
if string =~ ISO_DATETIME_WITH_TIMEZONE
|
34
|
-
Time.new $1.to_i, $2.to_i, $3.to_i, $4.to_i, $5.to_i, "#{$6}#{$7}".to_r, "#{$8}:00"
|
35
|
-
else
|
36
|
-
string
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end # module PG
|
42
|
-
|
data/lib/pg/text_encoder.rb
DELETED
@@ -1,27 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
|
3
|
-
module PG
|
4
|
-
module TextEncoder
|
5
|
-
class Date < SimpleEncoder
|
6
|
-
STRFTIME_ISO_DATE = "%Y-%m-%d".freeze
|
7
|
-
def encode(value)
|
8
|
-
value.respond_to?(:strftime) ? value.strftime(STRFTIME_ISO_DATE) : value
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
class TimestampWithoutTimeZone < SimpleEncoder
|
13
|
-
STRFTIME_ISO_DATETIME_WITHOUT_TIMEZONE = "%Y-%m-%d %H:%M:%S.%N".freeze
|
14
|
-
def encode(value)
|
15
|
-
value.respond_to?(:strftime) ? value.strftime(STRFTIME_ISO_DATETIME_WITHOUT_TIMEZONE) : value
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
class TimestampWithTimeZone < SimpleEncoder
|
20
|
-
STRFTIME_ISO_DATETIME_WITH_TIMEZONE = "%Y-%m-%d %H:%M:%S.%N %:z".freeze
|
21
|
-
def encode(value)
|
22
|
-
value.respond_to?(:strftime) ? value.strftime(STRFTIME_ISO_DATETIME_WITH_TIMEZONE) : value
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end # module PG
|
27
|
-
|
@@ -1,26 +0,0 @@
|
|
1
|
-
To backend> Msg Q
|
2
|
-
To backend> "SELECT 1 AS one"
|
3
|
-
To backend> Msg complete, length 21
|
4
|
-
From backend> T
|
5
|
-
From backend (#4)> 28
|
6
|
-
From backend (#2)> 1
|
7
|
-
From backend> "one"
|
8
|
-
From backend (#4)> 0
|
9
|
-
From backend (#2)> 0
|
10
|
-
From backend (#4)> 23
|
11
|
-
From backend (#2)> 4
|
12
|
-
From backend (#4)> -1
|
13
|
-
From backend (#2)> 0
|
14
|
-
From backend> D
|
15
|
-
From backend (#4)> 11
|
16
|
-
From backend (#2)> 1
|
17
|
-
From backend (#4)> 1
|
18
|
-
From backend (1)> 1
|
19
|
-
From backend> C
|
20
|
-
From backend (#4)> 11
|
21
|
-
From backend> "SELECT"
|
22
|
-
From backend> Z
|
23
|
-
From backend (#4)> 5
|
24
|
-
From backend> Z
|
25
|
-
From backend (#4)> 5
|
26
|
-
From backend> T
|
Binary file
|