yugabyte_ysql 0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (121) hide show
  1. checksums.yaml +7 -0
  2. data/.appveyor.yml +42 -0
  3. data/.gems +6 -0
  4. data/.gemtest +0 -0
  5. data/.github/workflows/binary-gems.yml +117 -0
  6. data/.github/workflows/source-gem.yml +143 -0
  7. data/.gitignore +24 -0
  8. data/.hgsigs +34 -0
  9. data/.hgtags +41 -0
  10. data/.irbrc +23 -0
  11. data/.pryrc +23 -0
  12. data/.tm_properties +21 -0
  13. data/.travis.yml +49 -0
  14. data/BSDL +22 -0
  15. data/Contributors.rdoc +46 -0
  16. data/Gemfile +18 -0
  17. data/History.md +901 -0
  18. data/LICENSE +56 -0
  19. data/Manifest.txt +73 -0
  20. data/POSTGRES +23 -0
  21. data/README-OS_X.rdoc +68 -0
  22. data/README-Windows.rdoc +56 -0
  23. data/README.ja.md +302 -0
  24. data/README.md +373 -0
  25. data/Rakefile +118 -0
  26. data/Rakefile.cross +299 -0
  27. data/certs/ged.pem +24 -0
  28. data/certs/kanis@comcard.de.pem +20 -0
  29. data/certs/larskanis-2022.pem +26 -0
  30. data/certs/larskanis-2023.pem +24 -0
  31. data/certs/larskanis-2024.pem +24 -0
  32. data/ext/errorcodes.def +1044 -0
  33. data/ext/errorcodes.rb +45 -0
  34. data/ext/errorcodes.txt +497 -0
  35. data/ext/extconf.rb +174 -0
  36. data/ext/gvl_wrappers.c +21 -0
  37. data/ext/gvl_wrappers.h +264 -0
  38. data/ext/pg.c +692 -0
  39. data/ext/pg.h +392 -0
  40. data/ext/pg_binary_decoder.c +308 -0
  41. data/ext/pg_binary_encoder.c +387 -0
  42. data/ext/pg_coder.c +624 -0
  43. data/ext/pg_connection.c +4681 -0
  44. data/ext/pg_copy_coder.c +917 -0
  45. data/ext/pg_errors.c +95 -0
  46. data/ext/pg_record_coder.c +522 -0
  47. data/ext/pg_result.c +1766 -0
  48. data/ext/pg_text_decoder.c +1005 -0
  49. data/ext/pg_text_encoder.c +827 -0
  50. data/ext/pg_tuple.c +572 -0
  51. data/ext/pg_type_map.c +200 -0
  52. data/ext/pg_type_map_all_strings.c +130 -0
  53. data/ext/pg_type_map_by_class.c +271 -0
  54. data/ext/pg_type_map_by_column.c +355 -0
  55. data/ext/pg_type_map_by_mri_type.c +313 -0
  56. data/ext/pg_type_map_by_oid.c +388 -0
  57. data/ext/pg_type_map_in_ruby.c +333 -0
  58. data/ext/pg_util.c +149 -0
  59. data/ext/pg_util.h +65 -0
  60. data/ext/vc/pg.sln +26 -0
  61. data/ext/vc/pg_18/pg.vcproj +216 -0
  62. data/ext/vc/pg_19/pg_19.vcproj +209 -0
  63. data/lib/pg/basic_type_map_based_on_result.rb +67 -0
  64. data/lib/pg/basic_type_map_for_queries.rb +202 -0
  65. data/lib/pg/basic_type_map_for_results.rb +104 -0
  66. data/lib/pg/basic_type_registry.rb +303 -0
  67. data/lib/pg/binary_decoder/date.rb +9 -0
  68. data/lib/pg/binary_decoder/timestamp.rb +26 -0
  69. data/lib/pg/binary_encoder/timestamp.rb +20 -0
  70. data/lib/pg/coder.rb +106 -0
  71. data/lib/pg/connection.rb +990 -0
  72. data/lib/pg/exceptions.rb +25 -0
  73. data/lib/pg/load_balance_service.rb +406 -0
  74. data/lib/pg/result.rb +43 -0
  75. data/lib/pg/text_decoder/date.rb +18 -0
  76. data/lib/pg/text_decoder/inet.rb +9 -0
  77. data/lib/pg/text_decoder/json.rb +14 -0
  78. data/lib/pg/text_decoder/numeric.rb +9 -0
  79. data/lib/pg/text_decoder/timestamp.rb +30 -0
  80. data/lib/pg/text_encoder/date.rb +12 -0
  81. data/lib/pg/text_encoder/inet.rb +28 -0
  82. data/lib/pg/text_encoder/json.rb +14 -0
  83. data/lib/pg/text_encoder/numeric.rb +9 -0
  84. data/lib/pg/text_encoder/timestamp.rb +24 -0
  85. data/lib/pg/tuple.rb +30 -0
  86. data/lib/pg/type_map_by_column.rb +16 -0
  87. data/lib/pg/version.rb +5 -0
  88. data/lib/yugabyte_ysql.rb +130 -0
  89. data/misc/openssl-pg-segfault.rb +31 -0
  90. data/misc/postgres/History.txt +9 -0
  91. data/misc/postgres/Manifest.txt +5 -0
  92. data/misc/postgres/README.txt +21 -0
  93. data/misc/postgres/Rakefile +21 -0
  94. data/misc/postgres/lib/postgres.rb +16 -0
  95. data/misc/ruby-pg/History.txt +9 -0
  96. data/misc/ruby-pg/Manifest.txt +5 -0
  97. data/misc/ruby-pg/README.txt +21 -0
  98. data/misc/ruby-pg/Rakefile +21 -0
  99. data/misc/ruby-pg/lib/ruby/pg.rb +16 -0
  100. data/rakelib/task_extension.rb +46 -0
  101. data/sample/array_insert.rb +20 -0
  102. data/sample/async_api.rb +102 -0
  103. data/sample/async_copyto.rb +39 -0
  104. data/sample/async_mixed.rb +56 -0
  105. data/sample/check_conn.rb +21 -0
  106. data/sample/copydata.rb +71 -0
  107. data/sample/copyfrom.rb +81 -0
  108. data/sample/copyto.rb +19 -0
  109. data/sample/cursor.rb +21 -0
  110. data/sample/disk_usage_report.rb +177 -0
  111. data/sample/issue-119.rb +94 -0
  112. data/sample/losample.rb +69 -0
  113. data/sample/minimal-testcase.rb +17 -0
  114. data/sample/notify_wait.rb +72 -0
  115. data/sample/pg_statistics.rb +285 -0
  116. data/sample/replication_monitor.rb +222 -0
  117. data/sample/test_binary_values.rb +33 -0
  118. data/sample/wal_shipper.rb +434 -0
  119. data/sample/warehouse_partitions.rb +311 -0
  120. data/yugabyte_ysql.gemspec +33 -0
  121. metadata +232 -0
data/ext/pg_coder.c ADDED
@@ -0,0 +1,624 @@
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_compact_callback(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_compact_callback(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;
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
+ RB_GC_GUARD(intermediate);
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.elements_type = coder
429
+ *
430
+ * Specifies the PG::Coder object that is used to encode or decode
431
+ * the single elementes of this composite type.
432
+ *
433
+ * If set to +nil+ all values are encoded and decoded as String objects.
434
+ */
435
+ static VALUE
436
+ pg_coder_elements_type_set(VALUE self, VALUE elem_type)
437
+ {
438
+ t_pg_composite_coder *this = RTYPEDDATA_DATA( self );
439
+
440
+ rb_check_frozen(self);
441
+ if ( NIL_P(elem_type) ){
442
+ this->elem = NULL;
443
+ } else if ( rb_obj_is_kind_of(elem_type, rb_cPG_Coder) ){
444
+ this->elem = RTYPEDDATA_DATA( elem_type );
445
+ } else {
446
+ rb_raise( rb_eTypeError, "wrong elements type %s (expected some kind of PG::Coder)",
447
+ rb_obj_classname( elem_type ) );
448
+ }
449
+
450
+ rb_iv_set( self, "@elements_type", elem_type );
451
+ return elem_type;
452
+ }
453
+
454
+ static const rb_data_type_t pg_coder_cfunc_type = {
455
+ "PG::Coder::CFUNC",
456
+ {
457
+ (RUBY_DATA_FUNC)NULL,
458
+ (RUBY_DATA_FUNC)NULL,
459
+ (size_t (*)(const void *))NULL,
460
+ },
461
+ 0,
462
+ 0,
463
+ RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED | PG_RUBY_TYPED_FROZEN_SHAREABLE,
464
+ };
465
+
466
+ VALUE
467
+ pg_define_coder( const char *name, void *func, VALUE base_klass, VALUE nsp )
468
+ {
469
+ VALUE cfunc_obj = TypedData_Wrap_Struct( rb_cObject, &pg_coder_cfunc_type, func );
470
+ VALUE coder_klass = rb_define_class_under( nsp, name, base_klass );
471
+ if( nsp==rb_mPG_BinaryEncoder || nsp==rb_mPG_BinaryDecoder )
472
+ rb_include_module( coder_klass, rb_mPG_BinaryFormatting );
473
+
474
+ if( nsp==rb_mPG_BinaryEncoder || nsp==rb_mPG_TextEncoder )
475
+ rb_define_method( coder_klass, "encode", pg_coder_encode, -1 );
476
+ if( nsp==rb_mPG_BinaryDecoder || nsp==rb_mPG_TextDecoder )
477
+ rb_define_method( coder_klass, "decode", pg_coder_decode, -1 );
478
+
479
+ rb_define_const( coder_klass, "CFUNC", rb_obj_freeze(cfunc_obj) );
480
+
481
+ RB_GC_GUARD(cfunc_obj);
482
+ return coder_klass;
483
+ }
484
+
485
+
486
+ static int
487
+ pg_text_enc_in_ruby(t_pg_coder *conv, VALUE value, char *out, VALUE *intermediate, int enc_idx)
488
+ {
489
+ int arity = rb_obj_method_arity(conv->coder_obj, s_id_encode);
490
+ if( arity == 1 ){
491
+ VALUE out_str = rb_funcall( conv->coder_obj, s_id_encode, 1, value );
492
+ StringValue( out_str );
493
+ *intermediate = rb_str_export_to_enc(out_str, rb_enc_from_index(enc_idx));
494
+ }else{
495
+ VALUE enc = rb_enc_from_encoding(rb_enc_from_index(enc_idx));
496
+ VALUE out_str = rb_funcall( conv->coder_obj, s_id_encode, 2, value, enc );
497
+ StringValue( out_str );
498
+ *intermediate = out_str;
499
+ }
500
+ return -1;
501
+ }
502
+
503
+ t_pg_coder_enc_func
504
+ pg_coder_enc_func(t_pg_coder *this)
505
+ {
506
+ if( this ){
507
+ if( this->enc_func ){
508
+ return this->enc_func;
509
+ }else{
510
+ return pg_text_enc_in_ruby;
511
+ }
512
+ }else{
513
+ /* no element encoder defined -> use std to_str conversion */
514
+ return pg_coder_enc_to_s;
515
+ }
516
+ }
517
+
518
+ static VALUE
519
+ pg_text_dec_in_ruby(t_pg_coder *this, const char *val, int len, int tuple, int field, int enc_idx)
520
+ {
521
+ VALUE string = pg_text_dec_string(this, val, len, tuple, field, enc_idx);
522
+ return rb_funcall( this->coder_obj, s_id_decode, 3, string, INT2NUM(tuple), INT2NUM(field) );
523
+ }
524
+
525
+ static VALUE
526
+ pg_bin_dec_in_ruby(t_pg_coder *this, const char *val, int len, int tuple, int field, int enc_idx)
527
+ {
528
+ VALUE string = pg_bin_dec_bytea(this, val, len, tuple, field, enc_idx);
529
+ return rb_funcall( this->coder_obj, s_id_decode, 3, string, INT2NUM(tuple), INT2NUM(field) );
530
+ }
531
+
532
+ t_pg_coder_dec_func
533
+ pg_coder_dec_func(t_pg_coder *this, int binary)
534
+ {
535
+ if( this ){
536
+ if( this->dec_func ){
537
+ return this->dec_func;
538
+ }else{
539
+ return binary ? pg_bin_dec_in_ruby : pg_text_dec_in_ruby;
540
+ }
541
+ }else{
542
+ /* no element decoder defined -> use std String conversion */
543
+ return binary ? pg_bin_dec_bytea : pg_text_dec_string;
544
+ }
545
+ }
546
+
547
+
548
+ void
549
+ init_pg_coder(void)
550
+ {
551
+ s_id_encode = rb_intern("encode");
552
+ s_id_decode = rb_intern("decode");
553
+ s_id_CFUNC = rb_intern("CFUNC");
554
+
555
+ /* Document-class: PG::Coder < Object
556
+ *
557
+ * This is the base class for all type cast encoder and decoder classes.
558
+ *
559
+ * It can be used for implicit type casts by a PG::TypeMap or to
560
+ * convert single values to/from their string representation by #encode
561
+ * and #decode.
562
+ *
563
+ * Ruby +nil+ values are not handled by encoders, but are always transmitted
564
+ * as SQL +NULL+ value. Vice versa SQL +NULL+ values are not handled by decoders,
565
+ * but are always returned as a +nil+ value.
566
+ */
567
+ rb_cPG_Coder = rb_define_class_under( rb_mPG, "Coder", rb_cObject );
568
+ rb_define_alloc_func( rb_cPG_Coder, pg_coder_allocate );
569
+ rb_define_method( rb_cPG_Coder, "oid=", pg_coder_oid_set, 1 );
570
+ rb_define_method( rb_cPG_Coder, "oid", pg_coder_oid_get, 0 );
571
+ rb_define_method( rb_cPG_Coder, "format=", pg_coder_format_set, 1 );
572
+ rb_define_method( rb_cPG_Coder, "format", pg_coder_format_get, 0 );
573
+ rb_define_method( rb_cPG_Coder, "flags=", pg_coder_flags_set, 1 );
574
+ rb_define_method( rb_cPG_Coder, "flags", pg_coder_flags_get, 0 );
575
+
576
+ /* define flags to be used with PG::Coder#flags= */
577
+ rb_define_const( rb_cPG_Coder, "TIMESTAMP_DB_UTC", INT2NUM(PG_CODER_TIMESTAMP_DB_UTC));
578
+ rb_define_const( rb_cPG_Coder, "TIMESTAMP_DB_LOCAL", INT2NUM(PG_CODER_TIMESTAMP_DB_LOCAL));
579
+ rb_define_const( rb_cPG_Coder, "TIMESTAMP_APP_UTC", INT2NUM(PG_CODER_TIMESTAMP_APP_UTC));
580
+ rb_define_const( rb_cPG_Coder, "TIMESTAMP_APP_LOCAL", INT2NUM(PG_CODER_TIMESTAMP_APP_LOCAL));
581
+ rb_define_const( rb_cPG_Coder, "FORMAT_ERROR_MASK", INT2NUM(PG_CODER_FORMAT_ERROR_MASK));
582
+ rb_define_const( rb_cPG_Coder, "FORMAT_ERROR_TO_RAISE", INT2NUM(PG_CODER_FORMAT_ERROR_TO_RAISE));
583
+ rb_define_const( rb_cPG_Coder, "FORMAT_ERROR_TO_STRING", INT2NUM(PG_CODER_FORMAT_ERROR_TO_STRING));
584
+ rb_define_const( rb_cPG_Coder, "FORMAT_ERROR_TO_PARTIAL", INT2NUM(PG_CODER_FORMAT_ERROR_TO_PARTIAL));
585
+
586
+ /*
587
+ * Name of the coder or the corresponding data type.
588
+ *
589
+ * This accessor is only used in PG::Coder#inspect .
590
+ */
591
+ rb_define_attr( rb_cPG_Coder, "name", 1, 1 );
592
+
593
+ /* Document-class: PG::SimpleCoder < PG::Coder */
594
+ rb_cPG_SimpleCoder = rb_define_class_under( rb_mPG, "SimpleCoder", rb_cPG_Coder );
595
+
596
+ /* Document-class: PG::SimpleEncoder < PG::SimpleCoder */
597
+ rb_cPG_SimpleEncoder = rb_define_class_under( rb_mPG, "SimpleEncoder", rb_cPG_SimpleCoder );
598
+ rb_define_alloc_func( rb_cPG_SimpleEncoder, pg_simple_encoder_allocate );
599
+ /* Document-class: PG::SimpleDecoder < PG::SimpleCoder */
600
+ rb_cPG_SimpleDecoder = rb_define_class_under( rb_mPG, "SimpleDecoder", rb_cPG_SimpleCoder );
601
+ rb_define_alloc_func( rb_cPG_SimpleDecoder, pg_simple_decoder_allocate );
602
+
603
+ /* Document-class: PG::CompositeCoder < PG::Coder
604
+ *
605
+ * This is the base class for all type cast classes of PostgreSQL types,
606
+ * that are made up of some sub type.
607
+ */
608
+ rb_cPG_CompositeCoder = rb_define_class_under( rb_mPG, "CompositeCoder", rb_cPG_Coder );
609
+ rb_define_method( rb_cPG_CompositeCoder, "elements_type=", pg_coder_elements_type_set, 1 );
610
+ rb_define_attr( rb_cPG_CompositeCoder, "elements_type", 1, 0 );
611
+ rb_define_method( rb_cPG_CompositeCoder, "needs_quotation=", pg_coder_needs_quotation_set, 1 );
612
+ rb_define_method( rb_cPG_CompositeCoder, "needs_quotation?", pg_coder_needs_quotation_get, 0 );
613
+ rb_define_method( rb_cPG_CompositeCoder, "delimiter=", pg_coder_delimiter_set, 1 );
614
+ rb_define_method( rb_cPG_CompositeCoder, "delimiter", pg_coder_delimiter_get, 0 );
615
+
616
+ /* Document-class: PG::CompositeEncoder < PG::CompositeCoder */
617
+ rb_cPG_CompositeEncoder = rb_define_class_under( rb_mPG, "CompositeEncoder", rb_cPG_CompositeCoder );
618
+ rb_define_alloc_func( rb_cPG_CompositeEncoder, pg_composite_encoder_allocate );
619
+ /* Document-class: PG::CompositeDecoder < PG::CompositeCoder */
620
+ rb_cPG_CompositeDecoder = rb_define_class_under( rb_mPG, "CompositeDecoder", rb_cPG_CompositeCoder );
621
+ rb_define_alloc_func( rb_cPG_CompositeDecoder, pg_composite_decoder_allocate );
622
+
623
+ rb_mPG_BinaryFormatting = rb_define_module_under( rb_cPG_Coder, "BinaryFormatting");
624
+ }