pg 1.6.0.rc1-x86_64-linux

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