pg 1.6.0.rc2-aarch64-linux

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.
Files changed (126) hide show
  1. checksums.yaml +7 -0
  2. checksums.yaml.gz.sig +0 -0
  3. data/BSDL +22 -0
  4. data/CHANGELOG.md +986 -0
  5. data/Contributors.rdoc +46 -0
  6. data/Gemfile +23 -0
  7. data/LICENSE +56 -0
  8. data/POSTGRES +23 -0
  9. data/README-OS_X.rdoc +68 -0
  10. data/README-Windows.rdoc +56 -0
  11. data/README.ja.md +300 -0
  12. data/README.md +287 -0
  13. data/Rakefile +185 -0
  14. data/certs/ged.pem +24 -0
  15. data/certs/kanis@comcard.de.pem +20 -0
  16. data/certs/larskanis-2022.pem +26 -0
  17. data/certs/larskanis-2023.pem +24 -0
  18. data/certs/larskanis-2024.pem +24 -0
  19. data/ext/errorcodes.def +1043 -0
  20. data/ext/errorcodes.rb +45 -0
  21. data/ext/errorcodes.txt +494 -0
  22. data/ext/extconf.rb +320 -0
  23. data/ext/gvl_wrappers.c +32 -0
  24. data/ext/gvl_wrappers.h +297 -0
  25. data/ext/pg.c +703 -0
  26. data/ext/pg.h +391 -0
  27. data/ext/pg_binary_decoder.c +460 -0
  28. data/ext/pg_binary_encoder.c +590 -0
  29. data/ext/pg_cancel_connection.c +360 -0
  30. data/ext/pg_coder.c +671 -0
  31. data/ext/pg_connection.c +4890 -0
  32. data/ext/pg_copy_coder.c +921 -0
  33. data/ext/pg_errors.c +95 -0
  34. data/ext/pg_record_coder.c +522 -0
  35. data/ext/pg_result.c +1764 -0
  36. data/ext/pg_text_decoder.c +1008 -0
  37. data/ext/pg_text_encoder.c +846 -0
  38. data/ext/pg_tuple.c +572 -0
  39. data/ext/pg_type_map.c +200 -0
  40. data/ext/pg_type_map_all_strings.c +130 -0
  41. data/ext/pg_type_map_by_class.c +271 -0
  42. data/ext/pg_type_map_by_column.c +356 -0
  43. data/ext/pg_type_map_by_mri_type.c +313 -0
  44. data/ext/pg_type_map_by_oid.c +390 -0
  45. data/ext/pg_type_map_in_ruby.c +333 -0
  46. data/ext/pg_util.c +149 -0
  47. data/ext/pg_util.h +65 -0
  48. data/ext/vc/pg.sln +26 -0
  49. data/ext/vc/pg_18/pg.vcproj +216 -0
  50. data/ext/vc/pg_19/pg_19.vcproj +209 -0
  51. data/lib/2.7/pg_ext.so +0 -0
  52. data/lib/3.0/pg_ext.so +0 -0
  53. data/lib/3.1/pg_ext.so +0 -0
  54. data/lib/3.2/pg_ext.so +0 -0
  55. data/lib/3.3/pg_ext.so +0 -0
  56. data/lib/3.4/pg_ext.so +0 -0
  57. data/lib/pg/basic_type_map_based_on_result.rb +67 -0
  58. data/lib/pg/basic_type_map_for_queries.rb +206 -0
  59. data/lib/pg/basic_type_map_for_results.rb +104 -0
  60. data/lib/pg/basic_type_registry.rb +311 -0
  61. data/lib/pg/binary_decoder/date.rb +9 -0
  62. data/lib/pg/binary_decoder/timestamp.rb +26 -0
  63. data/lib/pg/binary_encoder/timestamp.rb +20 -0
  64. data/lib/pg/cancel_connection.rb +53 -0
  65. data/lib/pg/coder.rb +107 -0
  66. data/lib/pg/connection.rb +1092 -0
  67. data/lib/pg/exceptions.rb +31 -0
  68. data/lib/pg/result.rb +43 -0
  69. data/lib/pg/text_decoder/date.rb +21 -0
  70. data/lib/pg/text_decoder/inet.rb +9 -0
  71. data/lib/pg/text_decoder/json.rb +17 -0
  72. data/lib/pg/text_decoder/numeric.rb +9 -0
  73. data/lib/pg/text_decoder/timestamp.rb +30 -0
  74. data/lib/pg/text_encoder/date.rb +13 -0
  75. data/lib/pg/text_encoder/inet.rb +31 -0
  76. data/lib/pg/text_encoder/json.rb +17 -0
  77. data/lib/pg/text_encoder/numeric.rb +9 -0
  78. data/lib/pg/text_encoder/timestamp.rb +24 -0
  79. data/lib/pg/tuple.rb +30 -0
  80. data/lib/pg/type_map_by_column.rb +16 -0
  81. data/lib/pg/version.rb +4 -0
  82. data/lib/pg.rb +144 -0
  83. data/misc/openssl-pg-segfault.rb +31 -0
  84. data/misc/postgres/History.txt +9 -0
  85. data/misc/postgres/Manifest.txt +5 -0
  86. data/misc/postgres/README.txt +21 -0
  87. data/misc/postgres/Rakefile +21 -0
  88. data/misc/postgres/lib/postgres.rb +16 -0
  89. data/misc/ruby-pg/History.txt +9 -0
  90. data/misc/ruby-pg/Manifest.txt +5 -0
  91. data/misc/ruby-pg/README.txt +21 -0
  92. data/misc/ruby-pg/Rakefile +21 -0
  93. data/misc/ruby-pg/lib/ruby/pg.rb +16 -0
  94. data/misc/yugabyte/Dockerfile +9 -0
  95. data/misc/yugabyte/docker-compose.yml +28 -0
  96. data/misc/yugabyte/pg-test.rb +45 -0
  97. data/pg.gemspec +38 -0
  98. data/ports/aarch64-linux/lib/libpq-ruby-pg.so.1 +0 -0
  99. data/ports/patches/krb5/1.21.3/0001-Allow-static-linking-krb5-library.patch +30 -0
  100. data/ports/patches/openssl/3.5.1/0001-aarch64-mingw.patch +21 -0
  101. data/ports/patches/postgresql/17.5/0001-Use-workaround-of-__builtin_setjmp-only-on-MINGW-on-.patch +42 -0
  102. data/ports/patches/postgresql/17.5/0001-libpq-Process-buffered-SSL-read-bytes-to-support-rec.patch +52 -0
  103. data/rakelib/pg_gem_helper.rb +64 -0
  104. data/rakelib/task_extension.rb +46 -0
  105. data/sample/array_insert.rb +20 -0
  106. data/sample/async_api.rb +102 -0
  107. data/sample/async_copyto.rb +39 -0
  108. data/sample/async_mixed.rb +56 -0
  109. data/sample/check_conn.rb +21 -0
  110. data/sample/copydata.rb +71 -0
  111. data/sample/copyfrom.rb +81 -0
  112. data/sample/copyto.rb +19 -0
  113. data/sample/cursor.rb +21 -0
  114. data/sample/disk_usage_report.rb +177 -0
  115. data/sample/issue-119.rb +94 -0
  116. data/sample/losample.rb +69 -0
  117. data/sample/minimal-testcase.rb +17 -0
  118. data/sample/notify_wait.rb +72 -0
  119. data/sample/pg_statistics.rb +285 -0
  120. data/sample/replication_monitor.rb +222 -0
  121. data/sample/test_binary_values.rb +33 -0
  122. data/sample/wal_shipper.rb +434 -0
  123. data/sample/warehouse_partitions.rb +311 -0
  124. data.tar.gz.sig +0 -0
  125. metadata +258 -0
  126. metadata.gz.sig +0 -0
data/ext/pg_coder.c ADDED
@@ -0,0 +1,671 @@
1
+ /*
2
+ * pg_coder.c - PG::Coder class extension
3
+ *
4
+ */
5
+
6
+ #include "pg.h"
7
+
8
+ VALUE rb_cPG_Coder;
9
+ VALUE rb_cPG_SimpleCoder;
10
+ VALUE rb_cPG_SimpleEncoder;
11
+ VALUE rb_cPG_SimpleDecoder;
12
+ VALUE rb_cPG_CompositeCoder;
13
+ VALUE rb_cPG_CompositeEncoder;
14
+ VALUE rb_cPG_CompositeDecoder;
15
+ VALUE rb_mPG_BinaryFormatting;
16
+ static ID s_id_encode;
17
+ static ID s_id_decode;
18
+ static ID s_id_CFUNC;
19
+
20
+ static VALUE
21
+ pg_coder_allocate( VALUE klass )
22
+ {
23
+ rb_raise( rb_eTypeError, "PG::Coder cannot be instantiated directly");
24
+ }
25
+
26
+ void
27
+ pg_coder_init_encoder( VALUE self )
28
+ {
29
+ t_pg_coder *this = RTYPEDDATA_DATA( self );
30
+ VALUE klass = rb_class_of(self);
31
+ if( rb_const_defined( klass, s_id_CFUNC ) ){
32
+ VALUE cfunc = rb_const_get( klass, s_id_CFUNC );
33
+ this->enc_func = RTYPEDDATA_DATA(cfunc);
34
+ } else {
35
+ this->enc_func = NULL;
36
+ }
37
+ this->dec_func = NULL;
38
+ RB_OBJ_WRITE(self, &this->coder_obj, self);
39
+ this->oid = 0;
40
+ this->format = 0;
41
+ this->flags = 0;
42
+ rb_iv_set( self, "@name", Qnil );
43
+ }
44
+
45
+ void
46
+ pg_coder_init_decoder( VALUE self )
47
+ {
48
+ t_pg_coder *this = RTYPEDDATA_DATA( self );
49
+ VALUE klass = rb_class_of(self);
50
+ this->enc_func = NULL;
51
+ if( rb_const_defined( klass, s_id_CFUNC ) ){
52
+ VALUE cfunc = rb_const_get( klass, s_id_CFUNC );
53
+ this->dec_func = RTYPEDDATA_DATA(cfunc);
54
+ } else {
55
+ this->dec_func = NULL;
56
+ }
57
+ RB_OBJ_WRITE(self, &this->coder_obj, self);
58
+ this->oid = 0;
59
+ this->format = 0;
60
+ this->flags = 0;
61
+ rb_iv_set( self, "@name", Qnil );
62
+ }
63
+
64
+ static size_t
65
+ pg_coder_memsize(const void *_this)
66
+ {
67
+ const t_pg_coder *this = (const t_pg_coder *)_this;
68
+ return sizeof(*this);
69
+ }
70
+
71
+ static size_t
72
+ pg_composite_coder_memsize(const void *_this)
73
+ {
74
+ const t_pg_composite_coder *this = (const t_pg_composite_coder *)_this;
75
+ return sizeof(*this);
76
+ }
77
+
78
+ void
79
+ pg_coder_compact(void *_this)
80
+ {
81
+ t_pg_coder *this = (t_pg_coder *)_this;
82
+ pg_gc_location(this->coder_obj);
83
+ }
84
+
85
+ static void
86
+ pg_composite_coder_compact(void *_this)
87
+ {
88
+ t_pg_composite_coder *this = (t_pg_composite_coder *)_this;
89
+ pg_coder_compact(&this->comp);
90
+ }
91
+
92
+ const rb_data_type_t pg_coder_type = {
93
+ "PG::Coder",
94
+ {
95
+ (RUBY_DATA_FUNC) NULL,
96
+ RUBY_TYPED_DEFAULT_FREE,
97
+ pg_coder_memsize,
98
+ pg_coder_compact,
99
+ },
100
+ 0,
101
+ 0,
102
+ // IMPORTANT: WB_PROTECTED objects must only use the RB_OBJ_WRITE()
103
+ // macro to update VALUE references, as to trigger write barriers.
104
+ RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED | PG_RUBY_TYPED_FROZEN_SHAREABLE,
105
+ };
106
+
107
+ static VALUE
108
+ pg_simple_encoder_allocate( VALUE klass )
109
+ {
110
+ t_pg_coder *this;
111
+ VALUE self = TypedData_Make_Struct( klass, t_pg_coder, &pg_coder_type, this );
112
+ pg_coder_init_encoder( self );
113
+ return self;
114
+ }
115
+
116
+ static const rb_data_type_t pg_composite_coder_type = {
117
+ "PG::CompositeCoder",
118
+ {
119
+ (RUBY_DATA_FUNC) NULL,
120
+ RUBY_TYPED_DEFAULT_FREE,
121
+ pg_composite_coder_memsize,
122
+ pg_composite_coder_compact,
123
+ },
124
+ &pg_coder_type,
125
+ 0,
126
+ RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED | PG_RUBY_TYPED_FROZEN_SHAREABLE,
127
+ };
128
+
129
+ static VALUE
130
+ pg_composite_encoder_allocate( VALUE klass )
131
+ {
132
+ t_pg_composite_coder *this;
133
+ VALUE self = TypedData_Make_Struct( klass, t_pg_composite_coder, &pg_composite_coder_type, this );
134
+ pg_coder_init_encoder( self );
135
+ this->elem = NULL;
136
+ this->needs_quotation = 1;
137
+ this->delimiter = ',';
138
+ this->dimensions = -1;
139
+ rb_iv_set( self, "@elements_type", Qnil );
140
+ return self;
141
+ }
142
+
143
+ static VALUE
144
+ pg_simple_decoder_allocate( VALUE klass )
145
+ {
146
+ t_pg_coder *this;
147
+ VALUE self = TypedData_Make_Struct( klass, t_pg_coder, &pg_coder_type, this );
148
+ pg_coder_init_decoder( self );
149
+ return self;
150
+ }
151
+
152
+ static VALUE
153
+ pg_composite_decoder_allocate( VALUE klass )
154
+ {
155
+ t_pg_composite_coder *this;
156
+ VALUE self = TypedData_Make_Struct( klass, t_pg_composite_coder, &pg_composite_coder_type, this );
157
+ pg_coder_init_decoder( self );
158
+ this->elem = NULL;
159
+ this->needs_quotation = 1;
160
+ this->delimiter = ',';
161
+ this->dimensions = -1;
162
+ rb_iv_set( self, "@elements_type", Qnil );
163
+ return self;
164
+ }
165
+
166
+ /*
167
+ * call-seq:
168
+ * coder.encode( value [, encoding] )
169
+ *
170
+ * Encodes the given Ruby object into string representation, without
171
+ * sending data to/from the database server.
172
+ *
173
+ * A nil value is passed through.
174
+ *
175
+ */
176
+ static VALUE
177
+ pg_coder_encode(int argc, VALUE *argv, VALUE self)
178
+ {
179
+ VALUE res;
180
+ VALUE intermediate = Qnil;
181
+ VALUE value;
182
+ int len, len2;
183
+ int enc_idx;
184
+ t_pg_coder *this = RTYPEDDATA_DATA(self);
185
+
186
+ if(argc < 1 || argc > 2){
187
+ rb_raise(rb_eArgError, "wrong number of arguments (%i for 1..2)", argc);
188
+ }else if(argc == 1){
189
+ enc_idx = rb_ascii8bit_encindex();
190
+ }else{
191
+ enc_idx = rb_to_encoding_index(argv[1]);
192
+ }
193
+ value = argv[0];
194
+
195
+ if( NIL_P(value) )
196
+ return Qnil;
197
+
198
+ if( !this->enc_func ){
199
+ rb_raise(rb_eRuntimeError, "no encoder function defined");
200
+ }
201
+
202
+ len = this->enc_func( this, value, NULL, &intermediate, enc_idx );
203
+
204
+ if( len == -1 ){
205
+ /* The intermediate value is a String that can be used directly. */
206
+ return intermediate;
207
+ }
208
+
209
+ res = rb_str_new(NULL, len);
210
+ PG_ENCODING_SET_NOCHECK(res, enc_idx);
211
+ len2 = this->enc_func( this, value, RSTRING_PTR(res), &intermediate, enc_idx );
212
+ if( len < len2 ){
213
+ rb_bug("%s: result length of first encoder run (%i) is less than second run (%i)",
214
+ rb_obj_classname( self ), len, len2 );
215
+ }
216
+ rb_str_set_len( res, len2 );
217
+
218
+ return res;
219
+ }
220
+
221
+ /*
222
+ * call-seq:
223
+ * coder.decode( string, tuple=nil, field=nil )
224
+ *
225
+ * Decodes the given string representation into a Ruby object, without
226
+ * sending data to/from the database server.
227
+ *
228
+ * A nil value is passed through and non String values are expected to have
229
+ * #to_str defined.
230
+ *
231
+ */
232
+ static VALUE
233
+ pg_coder_decode(int argc, VALUE *argv, VALUE self)
234
+ {
235
+ char *val;
236
+ int tuple = -1;
237
+ int field = -1;
238
+ VALUE res;
239
+ t_pg_coder *this = RTYPEDDATA_DATA(self);
240
+
241
+ if(argc < 1 || argc > 3){
242
+ rb_raise(rb_eArgError, "wrong number of arguments (%i for 1..3)", argc);
243
+ }else if(argc >= 3){
244
+ tuple = NUM2INT(argv[1]);
245
+ field = NUM2INT(argv[2]);
246
+ }
247
+
248
+ if( NIL_P(argv[0]) )
249
+ return Qnil;
250
+
251
+ if( this->format == 0 ){
252
+ val = StringValueCStr(argv[0]);
253
+ }else{
254
+ val = StringValuePtr(argv[0]);
255
+ }
256
+ if( !this->dec_func ){
257
+ rb_raise(rb_eRuntimeError, "no decoder function defined");
258
+ }
259
+
260
+ res = this->dec_func(this, val, RSTRING_LENINT(argv[0]), tuple, field, ENCODING_GET(argv[0]));
261
+
262
+ return res;
263
+ }
264
+
265
+ /*
266
+ * call-seq:
267
+ * coder.oid = Integer
268
+ *
269
+ * Specifies the type OID that is sent alongside with an encoded
270
+ * query parameter value.
271
+ *
272
+ * The default is +0+.
273
+ */
274
+ static VALUE
275
+ pg_coder_oid_set(VALUE self, VALUE oid)
276
+ {
277
+ t_pg_coder *this = RTYPEDDATA_DATA(self);
278
+ rb_check_frozen(self);
279
+ this->oid = NUM2UINT(oid);
280
+ return oid;
281
+ }
282
+
283
+ /*
284
+ * call-seq:
285
+ * coder.oid -> Integer
286
+ *
287
+ * The type OID that is sent alongside with an encoded
288
+ * query parameter value.
289
+ */
290
+ static VALUE
291
+ pg_coder_oid_get(VALUE self)
292
+ {
293
+ t_pg_coder *this = RTYPEDDATA_DATA(self);
294
+ return UINT2NUM(this->oid);
295
+ }
296
+
297
+ /*
298
+ * call-seq:
299
+ * coder.format = Integer
300
+ *
301
+ * Specifies the format code that is sent alongside with an encoded
302
+ * query parameter value.
303
+ *
304
+ * The default is +0+.
305
+ */
306
+ static VALUE
307
+ pg_coder_format_set(VALUE self, VALUE format)
308
+ {
309
+ t_pg_coder *this = RTYPEDDATA_DATA(self);
310
+ rb_check_frozen(self);
311
+ this->format = NUM2INT(format);
312
+ return format;
313
+ }
314
+
315
+ /*
316
+ * call-seq:
317
+ * coder.format -> Integer
318
+ *
319
+ * The format code that is sent alongside with an encoded
320
+ * query parameter value.
321
+ */
322
+ static VALUE
323
+ pg_coder_format_get(VALUE self)
324
+ {
325
+ t_pg_coder *this = RTYPEDDATA_DATA(self);
326
+ return INT2NUM(this->format);
327
+ }
328
+
329
+ /*
330
+ * call-seq:
331
+ * coder.flags = Integer
332
+ *
333
+ * Set coder specific bitwise OR-ed flags.
334
+ * See the particular en- or decoder description for available flags.
335
+ *
336
+ * The default is +0+.
337
+ */
338
+ static VALUE
339
+ pg_coder_flags_set(VALUE self, VALUE flags)
340
+ {
341
+ t_pg_coder *this = RTYPEDDATA_DATA(self);
342
+ rb_check_frozen(self);
343
+ this->flags = NUM2INT(flags);
344
+ return flags;
345
+ }
346
+
347
+ /*
348
+ * call-seq:
349
+ * coder.flags -> Integer
350
+ *
351
+ * Get current bitwise OR-ed coder flags.
352
+ */
353
+ static VALUE
354
+ pg_coder_flags_get(VALUE self)
355
+ {
356
+ t_pg_coder *this = RTYPEDDATA_DATA(self);
357
+ return INT2NUM(this->flags);
358
+ }
359
+
360
+ /*
361
+ * call-seq:
362
+ * coder.needs_quotation = Boolean
363
+ *
364
+ * Specifies whether the assigned #elements_type requires quotation marks to
365
+ * be transferred safely. Encoding with #needs_quotation=false is somewhat
366
+ * faster.
367
+ *
368
+ * The default is +true+. This option is ignored for decoding of values.
369
+ */
370
+ static VALUE
371
+ pg_coder_needs_quotation_set(VALUE self, VALUE needs_quotation)
372
+ {
373
+ t_pg_composite_coder *this = RTYPEDDATA_DATA(self);
374
+ rb_check_frozen(self);
375
+ this->needs_quotation = RTEST(needs_quotation);
376
+ return needs_quotation;
377
+ }
378
+
379
+ /*
380
+ * call-seq:
381
+ * coder.needs_quotation -> Boolean
382
+ *
383
+ * Specifies whether the assigned #elements_type requires quotation marks to
384
+ * be transferred safely.
385
+ */
386
+ static VALUE
387
+ pg_coder_needs_quotation_get(VALUE self)
388
+ {
389
+ t_pg_composite_coder *this = RTYPEDDATA_DATA(self);
390
+ return this->needs_quotation ? Qtrue : Qfalse;
391
+ }
392
+
393
+ /*
394
+ * call-seq:
395
+ * coder.delimiter = String
396
+ *
397
+ * Specifies the character that separates values within the composite type.
398
+ * The default is a comma.
399
+ * This must be a single one-byte character.
400
+ */
401
+ static VALUE
402
+ pg_coder_delimiter_set(VALUE self, VALUE delimiter)
403
+ {
404
+ t_pg_composite_coder *this = RTYPEDDATA_DATA(self);
405
+ rb_check_frozen(self);
406
+ StringValue(delimiter);
407
+ if(RSTRING_LEN(delimiter) != 1)
408
+ rb_raise( rb_eArgError, "delimiter size must be one byte");
409
+ this->delimiter = *RSTRING_PTR(delimiter);
410
+ return delimiter;
411
+ }
412
+
413
+ /*
414
+ * call-seq:
415
+ * coder.delimiter -> String
416
+ *
417
+ * The character that separates values within the composite type.
418
+ */
419
+ static VALUE
420
+ pg_coder_delimiter_get(VALUE self)
421
+ {
422
+ t_pg_composite_coder *this = RTYPEDDATA_DATA(self);
423
+ return rb_str_new(&this->delimiter, 1);
424
+ }
425
+
426
+ /*
427
+ * call-seq:
428
+ * coder.dimensions = Integer
429
+ * coder.dimensions = nil
430
+ *
431
+ * Set number of array dimensions to be encoded.
432
+ *
433
+ * This property ensures, that this number of dimensions is always encoded.
434
+ * If less dimensions than this number are in the given value, an ArgumentError is raised.
435
+ * If more dimensions than this number are in the value, the Array value is passed to the next encoder.
436
+ *
437
+ * Setting dimensions is especially useful, when a Record shall be encoded into an Array, since the Array encoder can not distinguish if the array shall be encoded as a higher dimension or as a record otherwise.
438
+ *
439
+ * The default is +nil+.
440
+ *
441
+ * See #dimensions
442
+ */
443
+ static VALUE
444
+ pg_coder_dimensions_set(VALUE self, VALUE dimensions)
445
+ {
446
+ t_pg_composite_coder *this = RTYPEDDATA_DATA(self);
447
+ rb_check_frozen(self);
448
+ if(!NIL_P(dimensions) && NUM2INT(dimensions) < 0)
449
+ rb_raise( rb_eArgError, "dimensions must be nil or >= 0");
450
+ this->dimensions = NIL_P(dimensions) ? -1 : NUM2INT(dimensions);
451
+ return dimensions;
452
+ }
453
+
454
+ /*
455
+ * call-seq:
456
+ * coder.dimensions -> Integer | nil
457
+ *
458
+ * Get number of enforced array dimensions or +nil+ if not set.
459
+ *
460
+ * See #dimensions=
461
+ */
462
+ static VALUE
463
+ pg_coder_dimensions_get(VALUE self)
464
+ {
465
+ t_pg_composite_coder *this = RTYPEDDATA_DATA(self);
466
+ return this->dimensions < 0 ? Qnil : INT2NUM(this->dimensions);
467
+ }
468
+
469
+ /*
470
+ * call-seq:
471
+ * coder.elements_type = coder
472
+ *
473
+ * Specifies the PG::Coder object that is used to encode or decode
474
+ * the single elementes of this composite type.
475
+ *
476
+ * If set to +nil+ all values are encoded and decoded as String objects.
477
+ */
478
+ static VALUE
479
+ pg_coder_elements_type_set(VALUE self, VALUE elem_type)
480
+ {
481
+ t_pg_composite_coder *this = RTYPEDDATA_DATA( self );
482
+
483
+ rb_check_frozen(self);
484
+ if ( NIL_P(elem_type) ){
485
+ this->elem = NULL;
486
+ } else if ( rb_obj_is_kind_of(elem_type, rb_cPG_Coder) ){
487
+ this->elem = RTYPEDDATA_DATA( elem_type );
488
+ } else {
489
+ rb_raise( rb_eTypeError, "wrong elements type %s (expected some kind of PG::Coder)",
490
+ rb_obj_classname( elem_type ) );
491
+ }
492
+
493
+ rb_iv_set( self, "@elements_type", elem_type );
494
+ return elem_type;
495
+ }
496
+
497
+ static const rb_data_type_t pg_coder_cfunc_type = {
498
+ "PG::Coder::CFUNC",
499
+ {
500
+ (RUBY_DATA_FUNC)NULL,
501
+ (RUBY_DATA_FUNC)NULL,
502
+ (size_t (*)(const void *))NULL,
503
+ },
504
+ 0,
505
+ 0,
506
+ RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED | PG_RUBY_TYPED_FROZEN_SHAREABLE,
507
+ };
508
+
509
+ VALUE
510
+ pg_define_coder( const char *name, void *func, VALUE base_klass, VALUE nsp )
511
+ {
512
+ VALUE cfunc_obj = TypedData_Wrap_Struct( rb_cObject, &pg_coder_cfunc_type, func );
513
+ VALUE coder_klass = rb_define_class_under( nsp, name, base_klass );
514
+ if( nsp==rb_mPG_BinaryEncoder || nsp==rb_mPG_BinaryDecoder )
515
+ rb_include_module( coder_klass, rb_mPG_BinaryFormatting );
516
+
517
+ if( nsp==rb_mPG_BinaryEncoder || nsp==rb_mPG_TextEncoder )
518
+ rb_define_method( coder_klass, "encode", pg_coder_encode, -1 );
519
+ if( nsp==rb_mPG_BinaryDecoder || nsp==rb_mPG_TextDecoder )
520
+ rb_define_method( coder_klass, "decode", pg_coder_decode, -1 );
521
+
522
+ rb_define_const( coder_klass, "CFUNC", rb_obj_freeze(cfunc_obj) );
523
+
524
+ RB_GC_GUARD(cfunc_obj);
525
+ return coder_klass;
526
+ }
527
+
528
+
529
+ static int
530
+ pg_text_enc_in_ruby(t_pg_coder *conv, VALUE value, char *out, VALUE *intermediate, int enc_idx)
531
+ {
532
+ int arity = rb_obj_method_arity(conv->coder_obj, s_id_encode);
533
+ if( arity == 1 ){
534
+ VALUE out_str = rb_funcall( conv->coder_obj, s_id_encode, 1, value );
535
+ StringValue( out_str );
536
+ *intermediate = rb_str_export_to_enc(out_str, rb_enc_from_index(enc_idx));
537
+ }else{
538
+ VALUE enc = rb_enc_from_encoding(rb_enc_from_index(enc_idx));
539
+ VALUE out_str = rb_funcall( conv->coder_obj, s_id_encode, 2, value, enc );
540
+ StringValue( out_str );
541
+ *intermediate = out_str;
542
+ }
543
+ return -1;
544
+ }
545
+
546
+ t_pg_coder_enc_func
547
+ pg_coder_enc_func(t_pg_coder *this)
548
+ {
549
+ if( this ){
550
+ if( this->enc_func ){
551
+ return this->enc_func;
552
+ }else{
553
+ return pg_text_enc_in_ruby;
554
+ }
555
+ }else{
556
+ /* no element encoder defined -> use std to_str conversion */
557
+ return pg_coder_enc_to_s;
558
+ }
559
+ }
560
+
561
+ static VALUE
562
+ pg_text_dec_in_ruby(t_pg_coder *this, const char *val, int len, int tuple, int field, int enc_idx)
563
+ {
564
+ VALUE string = pg_text_dec_string(this, val, len, tuple, field, enc_idx);
565
+ return rb_funcall( this->coder_obj, s_id_decode, 3, string, INT2NUM(tuple), INT2NUM(field) );
566
+ }
567
+
568
+ static VALUE
569
+ pg_bin_dec_in_ruby(t_pg_coder *this, const char *val, int len, int tuple, int field, int enc_idx)
570
+ {
571
+ VALUE string = pg_bin_dec_bytea(this, val, len, tuple, field, enc_idx);
572
+ return rb_funcall( this->coder_obj, s_id_decode, 3, string, INT2NUM(tuple), INT2NUM(field) );
573
+ }
574
+
575
+ t_pg_coder_dec_func
576
+ pg_coder_dec_func(t_pg_coder *this, int binary)
577
+ {
578
+ if( this ){
579
+ if( this->dec_func ){
580
+ return this->dec_func;
581
+ }else{
582
+ return binary ? pg_bin_dec_in_ruby : pg_text_dec_in_ruby;
583
+ }
584
+ }else{
585
+ /* no element decoder defined -> use std String conversion */
586
+ return binary ? pg_bin_dec_bytea : pg_text_dec_string;
587
+ }
588
+ }
589
+
590
+
591
+ void
592
+ init_pg_coder(void)
593
+ {
594
+ s_id_encode = rb_intern("encode");
595
+ s_id_decode = rb_intern("decode");
596
+ s_id_CFUNC = rb_intern("CFUNC");
597
+
598
+ /* Document-class: PG::Coder < Object
599
+ *
600
+ * This is the base class for all type cast encoder and decoder classes.
601
+ *
602
+ * It can be used for implicit type casts by a PG::TypeMap or to
603
+ * convert single values to/from their string representation by #encode
604
+ * and #decode.
605
+ *
606
+ * Ruby +nil+ values are not handled by encoders, but are always transmitted
607
+ * as SQL +NULL+ value. Vice versa SQL +NULL+ values are not handled by decoders,
608
+ * but are always returned as a +nil+ value.
609
+ */
610
+ rb_cPG_Coder = rb_define_class_under( rb_mPG, "Coder", rb_cObject );
611
+ rb_define_alloc_func( rb_cPG_Coder, pg_coder_allocate );
612
+ rb_define_method( rb_cPG_Coder, "oid=", pg_coder_oid_set, 1 );
613
+ rb_define_method( rb_cPG_Coder, "oid", pg_coder_oid_get, 0 );
614
+ rb_define_method( rb_cPG_Coder, "format=", pg_coder_format_set, 1 );
615
+ rb_define_method( rb_cPG_Coder, "format", pg_coder_format_get, 0 );
616
+ rb_define_method( rb_cPG_Coder, "flags=", pg_coder_flags_set, 1 );
617
+ rb_define_method( rb_cPG_Coder, "flags", pg_coder_flags_get, 0 );
618
+
619
+ /* define flags to be used with PG::Coder#flags= */
620
+ rb_define_const( rb_cPG_Coder, "TIMESTAMP_DB_UTC", INT2NUM(PG_CODER_TIMESTAMP_DB_UTC));
621
+ rb_define_const( rb_cPG_Coder, "TIMESTAMP_DB_LOCAL", INT2NUM(PG_CODER_TIMESTAMP_DB_LOCAL));
622
+ rb_define_const( rb_cPG_Coder, "TIMESTAMP_APP_UTC", INT2NUM(PG_CODER_TIMESTAMP_APP_UTC));
623
+ rb_define_const( rb_cPG_Coder, "TIMESTAMP_APP_LOCAL", INT2NUM(PG_CODER_TIMESTAMP_APP_LOCAL));
624
+ rb_define_const( rb_cPG_Coder, "FORMAT_ERROR_MASK", INT2NUM(PG_CODER_FORMAT_ERROR_MASK));
625
+ rb_define_const( rb_cPG_Coder, "FORMAT_ERROR_TO_RAISE", INT2NUM(PG_CODER_FORMAT_ERROR_TO_RAISE));
626
+ rb_define_const( rb_cPG_Coder, "FORMAT_ERROR_TO_STRING", INT2NUM(PG_CODER_FORMAT_ERROR_TO_STRING));
627
+ rb_define_const( rb_cPG_Coder, "FORMAT_ERROR_TO_PARTIAL", INT2NUM(PG_CODER_FORMAT_ERROR_TO_PARTIAL));
628
+
629
+ /*
630
+ * Name of the coder or the corresponding data type.
631
+ *
632
+ * This accessor is only used in PG::Coder#inspect .
633
+ */
634
+ rb_define_attr( rb_cPG_Coder, "name", 1, 1 );
635
+
636
+ /* Document-class: PG::SimpleCoder < PG::Coder */
637
+ rb_cPG_SimpleCoder = rb_define_class_under( rb_mPG, "SimpleCoder", rb_cPG_Coder );
638
+
639
+ /* Document-class: PG::SimpleEncoder < PG::SimpleCoder */
640
+ rb_cPG_SimpleEncoder = rb_define_class_under( rb_mPG, "SimpleEncoder", rb_cPG_SimpleCoder );
641
+ rb_define_alloc_func( rb_cPG_SimpleEncoder, pg_simple_encoder_allocate );
642
+ /* Document-class: PG::SimpleDecoder < PG::SimpleCoder */
643
+ rb_cPG_SimpleDecoder = rb_define_class_under( rb_mPG, "SimpleDecoder", rb_cPG_SimpleCoder );
644
+ rb_define_alloc_func( rb_cPG_SimpleDecoder, pg_simple_decoder_allocate );
645
+
646
+ /* Document-class: PG::CompositeCoder < PG::Coder
647
+ *
648
+ * This is the base class for all type cast classes of PostgreSQL types,
649
+ * that are made up of some sub type.
650
+ *
651
+ * See PG::TextEncoder::Array, PG::TextDecoder::Array, PG::BinaryEncoder::Array, PG::BinaryDecoder::Array, etc.
652
+ */
653
+ rb_cPG_CompositeCoder = rb_define_class_under( rb_mPG, "CompositeCoder", rb_cPG_Coder );
654
+ rb_define_method( rb_cPG_CompositeCoder, "elements_type=", pg_coder_elements_type_set, 1 );
655
+ rb_define_attr( rb_cPG_CompositeCoder, "elements_type", 1, 0 );
656
+ rb_define_method( rb_cPG_CompositeCoder, "needs_quotation=", pg_coder_needs_quotation_set, 1 );
657
+ rb_define_method( rb_cPG_CompositeCoder, "needs_quotation?", pg_coder_needs_quotation_get, 0 );
658
+ rb_define_method( rb_cPG_CompositeCoder, "delimiter=", pg_coder_delimiter_set, 1 );
659
+ rb_define_method( rb_cPG_CompositeCoder, "delimiter", pg_coder_delimiter_get, 0 );
660
+ rb_define_method( rb_cPG_CompositeCoder, "dimensions=", pg_coder_dimensions_set, 1 );
661
+ rb_define_method( rb_cPG_CompositeCoder, "dimensions", pg_coder_dimensions_get, 0 );
662
+
663
+ /* Document-class: PG::CompositeEncoder < PG::CompositeCoder */
664
+ rb_cPG_CompositeEncoder = rb_define_class_under( rb_mPG, "CompositeEncoder", rb_cPG_CompositeCoder );
665
+ rb_define_alloc_func( rb_cPG_CompositeEncoder, pg_composite_encoder_allocate );
666
+ /* Document-class: PG::CompositeDecoder < PG::CompositeCoder */
667
+ rb_cPG_CompositeDecoder = rb_define_class_under( rb_mPG, "CompositeDecoder", rb_cPG_CompositeCoder );
668
+ rb_define_alloc_func( rb_cPG_CompositeDecoder, pg_composite_decoder_allocate );
669
+
670
+ rb_mPG_BinaryFormatting = rb_define_module_under( rb_cPG_Coder, "BinaryFormatting");
671
+ }