pg 1.3.0.rc2-x64-mingw-ucrt

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