pg 1.2.3 → 1.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data/CHANGELOG.md +986 -0
- data/Gemfile +23 -0
- data/README-Windows.rdoc +1 -1
- data/README.ja.md +300 -0
- data/README.md +327 -0
- data/Rakefile +123 -144
- 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 +16 -5
- data/ext/errorcodes.rb +0 -0
- data/ext/errorcodes.txt +5 -5
- data/ext/extconf.rb +259 -33
- data/ext/gvl_wrappers.c +17 -2
- data/ext/gvl_wrappers.h +56 -0
- data/ext/pg.c +89 -63
- data/ext/pg.h +31 -8
- data/ext/pg_binary_decoder.c +232 -1
- data/ext/pg_binary_encoder.c +428 -1
- data/ext/pg_cancel_connection.c +360 -0
- data/ext/pg_coder.c +148 -36
- data/ext/pg_connection.c +1365 -817
- data/ext/pg_copy_coder.c +360 -38
- data/ext/pg_errors.c +1 -1
- data/ext/pg_record_coder.c +56 -25
- data/ext/pg_result.c +187 -76
- data/ext/pg_text_decoder.c +32 -11
- data/ext/pg_text_encoder.c +65 -33
- data/ext/pg_tuple.c +84 -61
- data/ext/pg_type_map.c +44 -10
- data/ext/pg_type_map_all_strings.c +17 -3
- data/ext/pg_type_map_by_class.c +54 -27
- data/ext/pg_type_map_by_column.c +74 -31
- data/ext/pg_type_map_by_mri_type.c +48 -19
- data/ext/pg_type_map_by_oid.c +61 -27
- data/ext/pg_type_map_in_ruby.c +55 -21
- data/ext/pg_util.c +2 -2
- data/lib/pg/basic_type_map_based_on_result.rb +67 -0
- data/lib/pg/basic_type_map_for_queries.rb +206 -0
- data/lib/pg/basic_type_map_for_results.rb +104 -0
- data/lib/pg/basic_type_registry.rb +311 -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/cancel_connection.rb +53 -0
- data/lib/pg/coder.rb +18 -14
- data/lib/pg/connection.rb +894 -91
- data/lib/pg/exceptions.rb +20 -1
- data/lib/pg/text_decoder/date.rb +21 -0
- data/lib/pg/text_decoder/inet.rb +9 -0
- data/lib/pg/text_decoder/json.rb +17 -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 +13 -0
- data/lib/pg/text_encoder/inet.rb +31 -0
- data/lib/pg/text_encoder/json.rb +17 -0
- data/lib/pg/text_encoder/numeric.rb +9 -0
- data/lib/pg/text_encoder/timestamp.rb +24 -0
- data/lib/pg/version.rb +4 -0
- data/lib/pg.rb +109 -39
- 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/misc/yugabyte/Dockerfile +9 -0
- data/misc/yugabyte/docker-compose.yml +28 -0
- data/misc/yugabyte/pg-test.rb +45 -0
- data/pg.gemspec +38 -0
- data/ports/patches/krb5/1.21.3/0001-Allow-static-linking-krb5-library.patch +30 -0
- data/ports/patches/openssl/3.5.1/0001-aarch64-mingw.patch +21 -0
- data/ports/patches/postgresql/17.5/0001-Use-workaround-of-__builtin_setjmp-only-on-MINGW-on-.patch +42 -0
- data/ports/patches/postgresql/17.5/0001-libpq-Process-buffered-SSL-read-bytes-to-support-rec.patch +52 -0
- data/rakelib/pg_gem_helper.rb +64 -0
- data/rakelib/task_extension.rb +46 -0
- data/sample/array_insert.rb +20 -0
- data/sample/async_api.rb +102 -0
- data/sample/async_copyto.rb +39 -0
- data/sample/async_mixed.rb +56 -0
- data/sample/check_conn.rb +21 -0
- data/sample/copydata.rb +71 -0
- data/sample/copyfrom.rb +81 -0
- data/sample/copyto.rb +19 -0
- data/sample/cursor.rb +21 -0
- data/sample/disk_usage_report.rb +177 -0
- data/sample/issue-119.rb +94 -0
- data/sample/losample.rb +69 -0
- data/sample/minimal-testcase.rb +17 -0
- data/sample/notify_wait.rb +72 -0
- data/sample/pg_statistics.rb +285 -0
- data/sample/replication_monitor.rb +222 -0
- data/sample/test_binary_values.rb +33 -0
- data/sample/wal_shipper.rb +434 -0
- data/sample/warehouse_partitions.rb +311 -0
- data.tar.gz.sig +0 -0
- metadata +139 -213
- metadata.gz.sig +0 -0
- data/.gemtest +0 -0
- data/ChangeLog +0 -0
- data/History.rdoc +0 -578
- data/Manifest.txt +0 -73
- data/README.ja.rdoc +0 -13
- data/README.rdoc +0 -213
- data/Rakefile.cross +0 -299
- data/lib/pg/basic_type_mapping.rb +0 -522
- data/lib/pg/binary_decoder.rb +0 -23
- data/lib/pg/constants.rb +0 -12
- data/lib/pg/text_decoder.rb +0 -46
- data/lib/pg/text_encoder.rb +0 -59
- data/spec/data/expected_trace.out +0 -26
- data/spec/data/random_binary_data +0 -0
- data/spec/helpers.rb +0 -380
- data/spec/pg/basic_type_mapping_spec.rb +0 -630
- data/spec/pg/connection_spec.rb +0 -1949
- data/spec/pg/connection_sync_spec.rb +0 -41
- data/spec/pg/result_spec.rb +0 -681
- data/spec/pg/tuple_spec.rb +0 -333
- data/spec/pg/type_map_by_class_spec.rb +0 -138
- data/spec/pg/type_map_by_column_spec.rb +0 -226
- 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 -1123
- data/spec/pg_spec.rb +0 -50
data/ext/pg_type_map_in_ruby.c
CHANGED
@@ -19,6 +19,33 @@ typedef struct {
|
|
19
19
|
VALUE self;
|
20
20
|
} t_tmir;
|
21
21
|
|
22
|
+
static size_t
|
23
|
+
pg_tmir_memsize( const void *_this )
|
24
|
+
{
|
25
|
+
const t_tmir *this = (const t_tmir *)_this;
|
26
|
+
return sizeof(*this);
|
27
|
+
}
|
28
|
+
|
29
|
+
static void
|
30
|
+
pg_tmir_compact( void *_this )
|
31
|
+
{
|
32
|
+
t_tmir *this = (t_tmir *)_this;
|
33
|
+
pg_typemap_compact(&this->typemap);
|
34
|
+
pg_gc_location(this->self);
|
35
|
+
}
|
36
|
+
|
37
|
+
static const rb_data_type_t pg_tmir_type = {
|
38
|
+
"PG::TypeMapInRuby",
|
39
|
+
{
|
40
|
+
pg_typemap_mark,
|
41
|
+
RUBY_TYPED_DEFAULT_FREE,
|
42
|
+
pg_tmir_memsize,
|
43
|
+
pg_tmir_compact,
|
44
|
+
},
|
45
|
+
&pg_typemap_type,
|
46
|
+
0,
|
47
|
+
RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED | PG_RUBY_TYPED_FROZEN_SHAREABLE,
|
48
|
+
};
|
22
49
|
|
23
50
|
/*
|
24
51
|
* call-seq:
|
@@ -34,33 +61,37 @@ typedef struct {
|
|
34
61
|
static VALUE
|
35
62
|
pg_tmir_fit_to_result( VALUE self, VALUE result )
|
36
63
|
{
|
37
|
-
t_tmir *this =
|
64
|
+
t_tmir *this = RTYPEDDATA_DATA( self );
|
38
65
|
t_typemap *default_tm;
|
39
66
|
t_typemap *p_new_typemap;
|
40
67
|
VALUE sub_typemap;
|
41
68
|
VALUE new_typemap;
|
42
69
|
|
43
70
|
if( rb_respond_to(self, s_id_fit_to_result) ){
|
71
|
+
t_typemap *tm;
|
72
|
+
UNUSED(tm);
|
44
73
|
new_typemap = rb_funcall( self, s_id_fit_to_result, 1, result );
|
45
74
|
|
46
75
|
if ( !rb_obj_is_kind_of(new_typemap, rb_cTypeMap) ) {
|
76
|
+
/* TypedData_Get_Struct() raises "wrong argument type", which is misleading,
|
77
|
+
* so we better raise our own message */
|
47
78
|
rb_raise( rb_eTypeError, "wrong return type from fit_to_result: %s expected kind of PG::TypeMap",
|
48
79
|
rb_obj_classname( new_typemap ) );
|
49
80
|
}
|
50
|
-
|
81
|
+
TypedData_Get_Struct(new_typemap, t_typemap, &pg_typemap_type, tm);
|
51
82
|
} else {
|
52
83
|
new_typemap = self;
|
53
84
|
}
|
54
85
|
|
55
|
-
/* Ensure that the default type map fits
|
56
|
-
default_tm =
|
86
|
+
/* Ensure that the default type map fits equally. */
|
87
|
+
default_tm = RTYPEDDATA_DATA( this->typemap.default_typemap );
|
57
88
|
sub_typemap = default_tm->funcs.fit_to_result( this->typemap.default_typemap, result );
|
58
89
|
|
59
90
|
if( sub_typemap != this->typemap.default_typemap ){
|
60
91
|
new_typemap = rb_obj_dup( new_typemap );
|
61
92
|
}
|
62
93
|
|
63
|
-
p_new_typemap =
|
94
|
+
p_new_typemap = RTYPEDDATA_DATA(new_typemap);
|
64
95
|
p_new_typemap->default_typemap = sub_typemap;
|
65
96
|
return new_typemap;
|
66
97
|
}
|
@@ -95,8 +126,8 @@ pg_tmir_result_value( t_typemap *p_typemap, VALUE result, int tuple, int field )
|
|
95
126
|
static VALUE
|
96
127
|
pg_tmir_typecast_result_value( VALUE self, VALUE result, VALUE tuple, VALUE field )
|
97
128
|
{
|
98
|
-
t_tmir *this =
|
99
|
-
t_typemap *default_tm =
|
129
|
+
t_tmir *this = RTYPEDDATA_DATA( self );
|
130
|
+
t_typemap *default_tm = RTYPEDDATA_DATA( this->typemap.default_typemap );
|
100
131
|
return default_tm->funcs.typecast_result_value( default_tm, result, NUM2INT(tuple), NUM2INT(field) );
|
101
132
|
}
|
102
133
|
|
@@ -113,15 +144,15 @@ pg_tmir_typecast_result_value( VALUE self, VALUE result, VALUE tuple, VALUE fiel
|
|
113
144
|
static VALUE
|
114
145
|
pg_tmir_fit_to_query( VALUE self, VALUE params )
|
115
146
|
{
|
116
|
-
t_tmir *this =
|
147
|
+
t_tmir *this = RTYPEDDATA_DATA( self );
|
117
148
|
t_typemap *default_tm;
|
118
149
|
|
119
150
|
if( rb_respond_to(self, s_id_fit_to_query) ){
|
120
151
|
rb_funcall( self, s_id_fit_to_query, 1, params );
|
121
152
|
}
|
122
153
|
|
123
|
-
/* Ensure that the default type map fits
|
124
|
-
default_tm =
|
154
|
+
/* Ensure that the default type map fits equally. */
|
155
|
+
default_tm = RTYPEDDATA_DATA( this->typemap.default_typemap );
|
125
156
|
default_tm->funcs.fit_to_query( this->typemap.default_typemap, params );
|
126
157
|
|
127
158
|
return self;
|
@@ -137,7 +168,7 @@ pg_tmir_query_param( t_typemap *p_typemap, VALUE param_value, int field )
|
|
137
168
|
if ( NIL_P(coder) ){
|
138
169
|
return NULL;
|
139
170
|
} else if( rb_obj_is_kind_of(coder, rb_cPG_Coder) ) {
|
140
|
-
return
|
171
|
+
return RTYPEDDATA_DATA(coder);
|
141
172
|
} else {
|
142
173
|
rb_raise( rb_eTypeError, "wrong return type from typecast_query_param: %s expected nil or kind of PG::Coder",
|
143
174
|
rb_obj_classname( coder ) );
|
@@ -161,8 +192,8 @@ pg_tmir_query_param( t_typemap *p_typemap, VALUE param_value, int field )
|
|
161
192
|
static VALUE
|
162
193
|
pg_tmir_typecast_query_param( VALUE self, VALUE param_value, VALUE field )
|
163
194
|
{
|
164
|
-
t_tmir *this =
|
165
|
-
t_typemap *default_tm =
|
195
|
+
t_tmir *this = RTYPEDDATA_DATA( self );
|
196
|
+
t_typemap *default_tm = RTYPEDDATA_DATA( this->typemap.default_typemap );
|
166
197
|
t_pg_coder *p_coder = default_tm->funcs.typecast_query_param( default_tm, param_value, NUM2INT(field) );
|
167
198
|
|
168
199
|
return p_coder ? p_coder->coder_obj : Qnil;
|
@@ -179,6 +210,9 @@ pg_tmir_typecast_query_param( VALUE self, VALUE param_value, VALUE field )
|
|
179
210
|
* This method is called, when a type map is used for decoding copy data,
|
180
211
|
* before the value is casted.
|
181
212
|
*
|
213
|
+
* Should return the expected number of columns or 0 if the number of columns is unknown.
|
214
|
+
* This number is only used for memory pre-allocation.
|
215
|
+
*
|
182
216
|
*/
|
183
217
|
static VALUE pg_tmir_fit_to_copy_get_dummy( VALUE self ){}
|
184
218
|
#endif
|
@@ -186,7 +220,7 @@ static VALUE pg_tmir_fit_to_copy_get_dummy( VALUE self ){}
|
|
186
220
|
static int
|
187
221
|
pg_tmir_fit_to_copy_get( VALUE self )
|
188
222
|
{
|
189
|
-
t_tmir *this =
|
223
|
+
t_tmir *this = RTYPEDDATA_DATA( self );
|
190
224
|
t_typemap *default_tm;
|
191
225
|
VALUE num_columns = INT2NUM(0);
|
192
226
|
|
@@ -198,8 +232,8 @@ pg_tmir_fit_to_copy_get( VALUE self )
|
|
198
232
|
rb_raise( rb_eTypeError, "wrong return type from fit_to_copy_get: %s expected kind of Integer",
|
199
233
|
rb_obj_classname( num_columns ) );
|
200
234
|
}
|
201
|
-
/* Ensure that the default type map fits
|
202
|
-
default_tm =
|
235
|
+
/* Ensure that the default type map fits equally. */
|
236
|
+
default_tm = RTYPEDDATA_DATA( this->typemap.default_typemap );
|
203
237
|
default_tm->funcs.fit_to_copy_get( this->typemap.default_typemap );
|
204
238
|
|
205
239
|
return NUM2INT(num_columns);;
|
@@ -239,8 +273,8 @@ pg_tmir_copy_get( t_typemap *p_typemap, VALUE field_str, int fieldno, int format
|
|
239
273
|
static VALUE
|
240
274
|
pg_tmir_typecast_copy_get( VALUE self, VALUE field_str, VALUE fieldno, VALUE format, VALUE enc )
|
241
275
|
{
|
242
|
-
t_tmir *this =
|
243
|
-
t_typemap *default_tm =
|
276
|
+
t_tmir *this = RTYPEDDATA_DATA( self );
|
277
|
+
t_typemap *default_tm = RTYPEDDATA_DATA( this->typemap.default_typemap );
|
244
278
|
int enc_idx = rb_to_encoding_index( enc );
|
245
279
|
|
246
280
|
return default_tm->funcs.typecast_copy_get( default_tm, field_str, NUM2INT(fieldno), NUM2INT(format), enc_idx );
|
@@ -252,7 +286,7 @@ pg_tmir_s_allocate( VALUE klass )
|
|
252
286
|
t_tmir *this;
|
253
287
|
VALUE self;
|
254
288
|
|
255
|
-
self =
|
289
|
+
self = TypedData_Make_Struct( klass, t_tmir, &pg_tmir_type, this );
|
256
290
|
|
257
291
|
this->typemap.funcs.fit_to_result = pg_tmir_fit_to_result;
|
258
292
|
this->typemap.funcs.fit_to_query = pg_tmir_fit_to_query;
|
@@ -260,7 +294,7 @@ pg_tmir_s_allocate( VALUE klass )
|
|
260
294
|
this->typemap.funcs.typecast_result_value = pg_tmir_result_value;
|
261
295
|
this->typemap.funcs.typecast_query_param = pg_tmir_query_param;
|
262
296
|
this->typemap.funcs.typecast_copy_get = pg_tmir_copy_get;
|
263
|
-
this->typemap.default_typemap
|
297
|
+
RB_OBJ_WRITE(self, &this->typemap.default_typemap, pg_typemap_all_strings);
|
264
298
|
this->self = self;
|
265
299
|
|
266
300
|
return self;
|
@@ -268,7 +302,7 @@ pg_tmir_s_allocate( VALUE klass )
|
|
268
302
|
|
269
303
|
|
270
304
|
void
|
271
|
-
init_pg_type_map_in_ruby()
|
305
|
+
init_pg_type_map_in_ruby(void)
|
272
306
|
{
|
273
307
|
s_id_fit_to_result = rb_intern("fit_to_result");
|
274
308
|
s_id_fit_to_query = rb_intern("fit_to_query");
|
data/ext/pg_util.c
CHANGED
@@ -91,7 +91,7 @@ base64_decode( char *out, const char *in, unsigned int len)
|
|
91
91
|
*out_ptr++ = (b << 4) | (c >> 2);
|
92
92
|
*out_ptr++ = (c << 6) | d;
|
93
93
|
} else if (in_ptr < iend_ptr){
|
94
|
-
|
94
|
+
b = c = d = 0xff;
|
95
95
|
while ((a = base64_decode_table[*in_ptr++]) == 0xff && in_ptr < iend_ptr) {}
|
96
96
|
if (in_ptr < iend_ptr){
|
97
97
|
while ((b = base64_decode_table[*in_ptr++]) == 0xff && in_ptr < iend_ptr) {}
|
@@ -116,7 +116,7 @@ base64_decode( char *out, const char *in, unsigned int len)
|
|
116
116
|
}
|
117
117
|
|
118
118
|
|
119
|
-
return (char*)out_ptr - out;
|
119
|
+
return (int)((char*)out_ptr - out);
|
120
120
|
}
|
121
121
|
|
122
122
|
/*
|
@@ -0,0 +1,67 @@
|
|
1
|
+
# -*- ruby -*-
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'pg' unless defined?( PG )
|
5
|
+
|
6
|
+
# Simple set of rules for type casting common PostgreSQL types from Ruby
|
7
|
+
# to PostgreSQL.
|
8
|
+
#
|
9
|
+
# OIDs of supported type casts are not hard-coded in the sources, but are retrieved from the
|
10
|
+
# PostgreSQL's +pg_type+ table in PG::BasicTypeMapBasedOnResult.new .
|
11
|
+
#
|
12
|
+
# This class works equal to PG::BasicTypeMapForResults, but does not define decoders for
|
13
|
+
# the given result OIDs, but encoders. So it can be used to type cast field values based on
|
14
|
+
# the type OID retrieved by a separate SQL query.
|
15
|
+
#
|
16
|
+
# PG::TypeMapByOid#build_column_map(result) can be used to generate a result independent
|
17
|
+
# PG::TypeMapByColumn type map, which can subsequently be used to cast query bind parameters
|
18
|
+
# or #put_copy_data fields.
|
19
|
+
#
|
20
|
+
# Example:
|
21
|
+
# conn.exec( "CREATE TEMP TABLE copytable (t TEXT, i INT, ai INT[])" )
|
22
|
+
#
|
23
|
+
# # Retrieve table OIDs per empty result set.
|
24
|
+
# res = conn.exec( "SELECT * FROM copytable LIMIT 0" )
|
25
|
+
# # Build a type map for common ruby to database type encoders.
|
26
|
+
# btm = PG::BasicTypeMapBasedOnResult.new(conn)
|
27
|
+
# # Build a PG::TypeMapByColumn with encoders suitable for copytable.
|
28
|
+
# tm = btm.build_column_map( res )
|
29
|
+
# row_encoder = PG::TextEncoder::CopyRow.new type_map: tm
|
30
|
+
#
|
31
|
+
# conn.copy_data( "COPY copytable FROM STDIN", row_encoder ) do |res|
|
32
|
+
# conn.put_copy_data ['a', 123, [5,4,3]]
|
33
|
+
# end
|
34
|
+
# This inserts a single row into copytable with type casts from ruby to
|
35
|
+
# database types using text format.
|
36
|
+
#
|
37
|
+
# Very similar with binary format:
|
38
|
+
#
|
39
|
+
# conn.exec( "CREATE TEMP TABLE copytable (t TEXT, i INT, blob bytea, created_at timestamp)" )
|
40
|
+
# # Retrieve table OIDs per empty result set in binary format.
|
41
|
+
# res = conn.exec_params( "SELECT * FROM copytable LIMIT 0", [], 1 )
|
42
|
+
# # Build a type map for common ruby to database type encoders.
|
43
|
+
# btm = PG::BasicTypeMapBasedOnResult.new(conn)
|
44
|
+
# # Build a PG::TypeMapByColumn with encoders suitable for copytable.
|
45
|
+
# tm = btm.build_column_map( res )
|
46
|
+
# row_encoder = PG::BinaryEncoder::CopyRow.new type_map: tm
|
47
|
+
#
|
48
|
+
# conn.copy_data( "COPY copytable FROM STDIN WITH (FORMAT binary)", row_encoder ) do |res|
|
49
|
+
# conn.put_copy_data ['a', 123, "\xff\x00".b, Time.now]
|
50
|
+
# end
|
51
|
+
#
|
52
|
+
# This inserts a single row into copytable with type casts from ruby to
|
53
|
+
# database types using binary copy and value format.
|
54
|
+
# Binary COPY is faster than text format but less portable and less readable and pg offers fewer en-/decoders of database types.
|
55
|
+
#
|
56
|
+
class PG::BasicTypeMapBasedOnResult < PG::TypeMapByOid
|
57
|
+
include PG::BasicTypeRegistry::Checker
|
58
|
+
|
59
|
+
def initialize(connection_or_coder_maps, registry: nil)
|
60
|
+
@coder_maps = build_coder_maps(connection_or_coder_maps, registry: registry)
|
61
|
+
|
62
|
+
# Populate TypeMapByOid hash with encoders
|
63
|
+
@coder_maps.each_format(:encoder).flat_map{|f| f.coders }.each do |coder|
|
64
|
+
add_coder(coder)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,206 @@
|
|
1
|
+
# -*- ruby -*-
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'pg' unless defined?( PG )
|
5
|
+
|
6
|
+
# Simple set of rules for type casting common Ruby types to PostgreSQL.
|
7
|
+
#
|
8
|
+
# OIDs of supported type casts are not hard-coded in the sources, but are retrieved from the
|
9
|
+
# PostgreSQL's pg_type table in PG::BasicTypeMapForQueries.new .
|
10
|
+
#
|
11
|
+
# Query params are type casted based on the class of the given value.
|
12
|
+
#
|
13
|
+
# Higher level libraries will most likely not make use of this class, but use their
|
14
|
+
# own derivation of PG::TypeMapByClass or another set of rules to choose suitable
|
15
|
+
# encoders and decoders for the values to be sent.
|
16
|
+
#
|
17
|
+
# Example:
|
18
|
+
# conn = PG::Connection.new
|
19
|
+
# # Assign a default ruleset for type casts of input and output values.
|
20
|
+
# conn.type_map_for_queries = PG::BasicTypeMapForQueries.new(conn)
|
21
|
+
# # Execute a query. The Integer param value is typecasted internally by PG::BinaryEncoder::Int8.
|
22
|
+
# # The format of the parameter is set to 0 (text) and the OID of this parameter is set to 20 (int8).
|
23
|
+
# res = conn.exec_params( "SELECT $1", [5] )
|
24
|
+
class PG::BasicTypeMapForQueries < PG::TypeMapByClass
|
25
|
+
# Helper class for submission of binary strings into bytea columns.
|
26
|
+
#
|
27
|
+
# Since PG::BasicTypeMapForQueries chooses the encoder to be used by the class of the submitted value,
|
28
|
+
# it's necessary to send binary strings as BinaryData.
|
29
|
+
# That way they're distinct from text strings.
|
30
|
+
# Please note however that PG::BasicTypeMapForResults delivers bytea columns as plain String
|
31
|
+
# with binary encoding.
|
32
|
+
#
|
33
|
+
# conn.type_map_for_queries = PG::BasicTypeMapForQueries.new(conn)
|
34
|
+
# conn.exec("CREATE TEMP TABLE test (data bytea)")
|
35
|
+
# bd = PG::BasicTypeMapForQueries::BinaryData.new("ab\xff\0cd")
|
36
|
+
# conn.exec_params("INSERT INTO test (data) VALUES ($1)", [bd])
|
37
|
+
class BinaryData < String
|
38
|
+
end
|
39
|
+
|
40
|
+
class UndefinedEncoder < RuntimeError
|
41
|
+
end
|
42
|
+
|
43
|
+
include PG::BasicTypeRegistry::Checker
|
44
|
+
|
45
|
+
# Create a new type map for query submission
|
46
|
+
#
|
47
|
+
# Options:
|
48
|
+
# * +registry+: Custom type registry, nil for default global registry
|
49
|
+
# * +if_undefined+: Optional +Proc+ object which is called, if no type for an parameter class is not defined in the registry.
|
50
|
+
# The +Proc+ object is called with the name and format of the missing type.
|
51
|
+
# Its return value is not used.
|
52
|
+
def initialize(connection_or_coder_maps, registry: nil, if_undefined: nil)
|
53
|
+
@coder_maps = build_coder_maps(connection_or_coder_maps, registry: registry)
|
54
|
+
@array_encoders_by_klass = array_encoders_by_klass
|
55
|
+
@encode_array_as = :array
|
56
|
+
@if_undefined = if_undefined || UndefinedDefault
|
57
|
+
init_encoders
|
58
|
+
end
|
59
|
+
|
60
|
+
class UndefinedDefault
|
61
|
+
def self.call(oid_name, format)
|
62
|
+
raise UndefinedEncoder, "no encoder defined for type #{oid_name.inspect} format #{format}"
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
private_constant :UndefinedDefault
|
67
|
+
|
68
|
+
# Change the mechanism that is used to encode ruby array values
|
69
|
+
#
|
70
|
+
# Possible values:
|
71
|
+
# * +:array+ : Encode the ruby array as a PostgreSQL array.
|
72
|
+
# The array element type is inferred from the class of the first array element. This is the default.
|
73
|
+
# * +:json+ : Encode the ruby array as a JSON document.
|
74
|
+
# * +:record+ : Encode the ruby array as a composite type row.
|
75
|
+
# * <code>"_type"</code> : Encode the ruby array as a particular PostgreSQL type.
|
76
|
+
# All PostgreSQL array types are supported.
|
77
|
+
# If there's an encoder registered for the elements +type+, it will be used.
|
78
|
+
# Otherwise a string conversion (by +value.to_s+) is done.
|
79
|
+
def encode_array_as=(pg_type)
|
80
|
+
case pg_type
|
81
|
+
when :array
|
82
|
+
when :json
|
83
|
+
when :record
|
84
|
+
when /\A_/
|
85
|
+
else
|
86
|
+
raise ArgumentError, "invalid pg_type #{pg_type.inspect}"
|
87
|
+
end
|
88
|
+
|
89
|
+
@encode_array_as = pg_type
|
90
|
+
|
91
|
+
init_encoders
|
92
|
+
end
|
93
|
+
|
94
|
+
attr_reader :encode_array_as
|
95
|
+
|
96
|
+
private
|
97
|
+
|
98
|
+
def init_encoders
|
99
|
+
coders.each { |kl, c| self[kl] = nil } # Clear type map
|
100
|
+
populate_encoder_list
|
101
|
+
@textarray_encoder = coder_by_name(0, :encoder, '_text')
|
102
|
+
end
|
103
|
+
|
104
|
+
def coder_by_name(format, direction, name)
|
105
|
+
check_format_and_direction(format, direction)
|
106
|
+
@coder_maps.map_for(format, direction).coder_by_name(name)
|
107
|
+
end
|
108
|
+
|
109
|
+
def undefined(name, format)
|
110
|
+
@if_undefined.call(name, format)
|
111
|
+
end
|
112
|
+
|
113
|
+
def populate_encoder_list
|
114
|
+
DEFAULT_TYPE_MAP.each do |klass, selector|
|
115
|
+
if Array === selector
|
116
|
+
format, name, oid_name = selector
|
117
|
+
coder = coder_by_name(format, :encoder, name).dup
|
118
|
+
if coder
|
119
|
+
if oid_name
|
120
|
+
oid_coder = coder_by_name(format, :encoder, oid_name)
|
121
|
+
if oid_coder
|
122
|
+
coder.oid = oid_coder.oid
|
123
|
+
else
|
124
|
+
undefined(oid_name, format)
|
125
|
+
end
|
126
|
+
else
|
127
|
+
coder.oid = 0
|
128
|
+
end
|
129
|
+
self[klass] = coder
|
130
|
+
else
|
131
|
+
undefined(name, format)
|
132
|
+
end
|
133
|
+
else
|
134
|
+
|
135
|
+
case @encode_array_as
|
136
|
+
when :array
|
137
|
+
self[klass] = selector
|
138
|
+
when :json
|
139
|
+
self[klass] = PG::TextEncoder::JSON.new
|
140
|
+
when :record
|
141
|
+
self[klass] = PG::TextEncoder::Record.new type_map: self
|
142
|
+
when /\A_/
|
143
|
+
coder = coder_by_name(0, :encoder, @encode_array_as)
|
144
|
+
if coder
|
145
|
+
self[klass] = coder
|
146
|
+
else
|
147
|
+
undefined(@encode_array_as, format)
|
148
|
+
end
|
149
|
+
else
|
150
|
+
raise ArgumentError, "invalid pg_type #{@encode_array_as.inspect}"
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
def array_encoders_by_klass
|
157
|
+
DEFAULT_ARRAY_TYPE_MAP.inject({}) do |h, (klass, (format, name))|
|
158
|
+
h[klass] = coder_by_name(format, :encoder, name)
|
159
|
+
h
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
def get_array_type(value)
|
164
|
+
elem = value
|
165
|
+
while elem.kind_of?(Array)
|
166
|
+
elem = elem.first
|
167
|
+
end
|
168
|
+
@array_encoders_by_klass[elem.class] ||
|
169
|
+
elem.class.ancestors.lazy.map{|ancestor| @array_encoders_by_klass[ancestor] }.find{|a| a } ||
|
170
|
+
@textarray_encoder
|
171
|
+
end
|
172
|
+
|
173
|
+
begin
|
174
|
+
PG.require_bigdecimal_without_warning
|
175
|
+
has_bigdecimal = true
|
176
|
+
rescue LoadError
|
177
|
+
end
|
178
|
+
|
179
|
+
DEFAULT_TYPE_MAP = PG.make_shareable({
|
180
|
+
TrueClass => [1, 'bool', 'bool'],
|
181
|
+
FalseClass => [1, 'bool', 'bool'],
|
182
|
+
# We use text format and no type OID for numbers, because setting the OID can lead
|
183
|
+
# to unnecessary type conversions on server side.
|
184
|
+
Integer => [0, 'int8'],
|
185
|
+
Float => [0, 'float8'],
|
186
|
+
Time => [0, 'timestamptz'],
|
187
|
+
# We use text format and no type OID for IPAddr, because setting the OID can lead
|
188
|
+
# to unnecessary inet/cidr conversions on the server side.
|
189
|
+
IPAddr => [0, 'inet'],
|
190
|
+
Hash => [0, 'json'],
|
191
|
+
Array => :get_array_type,
|
192
|
+
BinaryData => [1, 'bytea'],
|
193
|
+
}.merge(has_bigdecimal ? {BigDecimal => [0, 'numeric']} : {}))
|
194
|
+
private_constant :DEFAULT_TYPE_MAP
|
195
|
+
|
196
|
+
DEFAULT_ARRAY_TYPE_MAP = PG.make_shareable({
|
197
|
+
TrueClass => [0, '_bool'],
|
198
|
+
FalseClass => [0, '_bool'],
|
199
|
+
Integer => [0, '_int8'],
|
200
|
+
String => [0, '_text'],
|
201
|
+
Float => [0, '_float8'],
|
202
|
+
Time => [0, '_timestamptz'],
|
203
|
+
IPAddr => [0, '_inet'],
|
204
|
+
}.merge(has_bigdecimal ? {BigDecimal => [0, '_numeric']} : {}))
|
205
|
+
private_constant :DEFAULT_ARRAY_TYPE_MAP
|
206
|
+
end
|
@@ -0,0 +1,104 @@
|
|
1
|
+
# -*- ruby -*-
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'pg' unless defined?( PG )
|
5
|
+
|
6
|
+
# Simple set of rules for type casting common PostgreSQL types to Ruby.
|
7
|
+
#
|
8
|
+
# OIDs of supported type casts are not hard-coded in the sources, but are retrieved from the
|
9
|
+
# PostgreSQL's +pg_type+ table in PG::BasicTypeMapForResults.new .
|
10
|
+
#
|
11
|
+
# Result values are type casted based on the type OID of the given result column.
|
12
|
+
#
|
13
|
+
# Higher level libraries will most likely not make use of this class, but use their
|
14
|
+
# own set of rules to choose suitable encoders and decoders.
|
15
|
+
#
|
16
|
+
# Example:
|
17
|
+
# conn = PG::Connection.new
|
18
|
+
# # Assign a default ruleset for type casts of output values.
|
19
|
+
# conn.type_map_for_results = PG::BasicTypeMapForResults.new(conn)
|
20
|
+
# # Execute a query.
|
21
|
+
# res = conn.exec_params( "SELECT $1::INT", ['5'] )
|
22
|
+
# # Retrieve and cast the result value. Value format is 0 (text) and OID is 20. Therefore typecasting
|
23
|
+
# # is done by PG::TextDecoder::Integer internally for all value retrieval methods.
|
24
|
+
# res.values # => [[5]]
|
25
|
+
#
|
26
|
+
# PG::TypeMapByOid#build_column_map(result) can be used to generate
|
27
|
+
# a result independent PG::TypeMapByColumn type map, which can subsequently be used
|
28
|
+
# to cast #get_copy_data fields:
|
29
|
+
#
|
30
|
+
# For the following table:
|
31
|
+
# conn.exec( "CREATE TABLE copytable AS VALUES('a', 123, '{5,4,3}'::INT[])" )
|
32
|
+
#
|
33
|
+
# # Retrieve table OIDs per empty result set.
|
34
|
+
# res = conn.exec( "SELECT * FROM copytable LIMIT 0" )
|
35
|
+
# # Build a type map for common database to ruby type decoders.
|
36
|
+
# btm = PG::BasicTypeMapForResults.new(conn)
|
37
|
+
# # Build a PG::TypeMapByColumn with decoders suitable for copytable.
|
38
|
+
# tm = btm.build_column_map( res )
|
39
|
+
# row_decoder = PG::TextDecoder::CopyRow.new type_map: tm
|
40
|
+
#
|
41
|
+
# conn.copy_data( "COPY copytable TO STDOUT", row_decoder ) do |res|
|
42
|
+
# while row=conn.get_copy_data
|
43
|
+
# p row
|
44
|
+
# end
|
45
|
+
# end
|
46
|
+
# This prints the rows with type casted columns:
|
47
|
+
# ["a", 123, [5, 4, 3]]
|
48
|
+
#
|
49
|
+
# Very similar with binary format:
|
50
|
+
#
|
51
|
+
# conn.exec( "CREATE TABLE copytable AS VALUES('a', 123, '2023-03-19 18:39:44'::TIMESTAMP)" )
|
52
|
+
#
|
53
|
+
# # Retrieve table OIDs per empty result set in binary format.
|
54
|
+
# res = conn.exec_params( "SELECT * FROM copytable LIMIT 0", [], 1 )
|
55
|
+
# # Build a type map for common database to ruby type decoders.
|
56
|
+
# btm = PG::BasicTypeMapForResults.new(conn)
|
57
|
+
# # Build a PG::TypeMapByColumn with decoders suitable for copytable.
|
58
|
+
# tm = btm.build_column_map( res )
|
59
|
+
# row_decoder = PG::BinaryDecoder::CopyRow.new type_map: tm
|
60
|
+
#
|
61
|
+
# conn.copy_data( "COPY copytable TO STDOUT WITH (FORMAT binary)", row_decoder ) do |res|
|
62
|
+
# while row=conn.get_copy_data
|
63
|
+
# p row
|
64
|
+
# end
|
65
|
+
# end
|
66
|
+
# This prints the rows with type casted columns:
|
67
|
+
# ["a", 123, 2023-03-19 18:39:44 UTC]
|
68
|
+
#
|
69
|
+
# See also PG::BasicTypeMapBasedOnResult for the encoder direction and PG::BasicTypeRegistry for the definition of additional types.
|
70
|
+
class PG::BasicTypeMapForResults < PG::TypeMapByOid
|
71
|
+
include PG::BasicTypeRegistry::Checker
|
72
|
+
|
73
|
+
class WarningTypeMap < PG::TypeMapInRuby
|
74
|
+
def initialize(typenames)
|
75
|
+
@already_warned = {}
|
76
|
+
@typenames_by_oid = typenames
|
77
|
+
end
|
78
|
+
|
79
|
+
def typecast_result_value(result, _tuple, field)
|
80
|
+
format = result.fformat(field)
|
81
|
+
oid = result.ftype(field)
|
82
|
+
unless @already_warned.dig(format, oid)
|
83
|
+
warn "Warning: no type cast defined for type #{@typenames_by_oid[oid].inspect} format #{format} with oid #{oid}. Please cast this type explicitly to TEXT to be safe for future changes."
|
84
|
+
unless frozen?
|
85
|
+
@already_warned[format] ||= {}
|
86
|
+
@already_warned[format][oid] = true
|
87
|
+
end
|
88
|
+
end
|
89
|
+
super
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def initialize(connection_or_coder_maps, registry: nil)
|
94
|
+
@coder_maps = build_coder_maps(connection_or_coder_maps, registry: registry)
|
95
|
+
|
96
|
+
# Populate TypeMapByOid hash with decoders
|
97
|
+
@coder_maps.each_format(:decoder).flat_map{|f| f.coders }.each do |coder|
|
98
|
+
add_coder(coder)
|
99
|
+
end
|
100
|
+
|
101
|
+
typenames = @coder_maps.typenames_by_oid
|
102
|
+
self.default_type_map = WarningTypeMap.new(typenames)
|
103
|
+
end
|
104
|
+
end
|