pg 0.21.0 → 1.3.2

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 (117) hide show
  1. checksums.yaml +5 -5
  2. checksums.yaml.gz.sig +0 -0
  3. data/.appveyor.yml +36 -0
  4. data/.gems +6 -0
  5. data/.github/workflows/binary-gems.yml +86 -0
  6. data/.github/workflows/source-gem.yml +130 -0
  7. data/.gitignore +13 -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/Gemfile +14 -0
  15. data/History.rdoc +291 -4
  16. data/Manifest.txt +8 -4
  17. data/README-Windows.rdoc +4 -4
  18. data/README.ja.rdoc +1 -2
  19. data/README.rdoc +62 -16
  20. data/Rakefile +32 -142
  21. data/Rakefile.cross +69 -71
  22. data/certs/ged.pem +24 -0
  23. data/certs/larskanis-2022.pem +26 -0
  24. data/ext/errorcodes.def +84 -0
  25. data/ext/errorcodes.rb +1 -1
  26. data/ext/errorcodes.txt +23 -2
  27. data/ext/extconf.rb +109 -52
  28. data/ext/gvl_wrappers.c +8 -0
  29. data/ext/gvl_wrappers.h +40 -33
  30. data/ext/pg.c +211 -146
  31. data/ext/pg.h +85 -95
  32. data/ext/pg_binary_decoder.c +82 -15
  33. data/ext/pg_binary_encoder.c +13 -12
  34. data/ext/pg_coder.c +145 -30
  35. data/ext/pg_connection.c +1349 -929
  36. data/ext/pg_copy_coder.c +60 -23
  37. data/ext/pg_record_coder.c +519 -0
  38. data/ext/pg_result.c +613 -207
  39. data/ext/pg_text_decoder.c +606 -40
  40. data/ext/pg_text_encoder.c +190 -59
  41. data/ext/pg_tuple.c +575 -0
  42. data/ext/pg_type_map.c +42 -9
  43. data/ext/pg_type_map_all_strings.c +19 -5
  44. data/ext/pg_type_map_by_class.c +54 -24
  45. data/ext/pg_type_map_by_column.c +73 -34
  46. data/ext/pg_type_map_by_mri_type.c +48 -19
  47. data/ext/pg_type_map_by_oid.c +55 -25
  48. data/ext/pg_type_map_in_ruby.c +51 -20
  49. data/ext/{util.c → pg_util.c} +12 -12
  50. data/ext/{util.h → pg_util.h} +2 -2
  51. data/lib/pg/basic_type_map_based_on_result.rb +47 -0
  52. data/lib/pg/basic_type_map_for_queries.rb +193 -0
  53. data/lib/pg/basic_type_map_for_results.rb +81 -0
  54. data/lib/pg/basic_type_registry.rb +296 -0
  55. data/lib/pg/binary_decoder.rb +23 -0
  56. data/lib/pg/coder.rb +24 -3
  57. data/lib/pg/connection.rb +600 -46
  58. data/lib/pg/constants.rb +2 -1
  59. data/lib/pg/exceptions.rb +2 -1
  60. data/lib/pg/result.rb +14 -2
  61. data/lib/pg/text_decoder.rb +21 -26
  62. data/lib/pg/text_encoder.rb +32 -8
  63. data/lib/pg/tuple.rb +30 -0
  64. data/lib/pg/type_map_by_column.rb +3 -2
  65. data/lib/pg/version.rb +4 -0
  66. data/lib/pg.rb +51 -38
  67. data/misc/openssl-pg-segfault.rb +31 -0
  68. data/misc/postgres/History.txt +9 -0
  69. data/misc/postgres/Manifest.txt +5 -0
  70. data/misc/postgres/README.txt +21 -0
  71. data/misc/postgres/Rakefile +21 -0
  72. data/misc/postgres/lib/postgres.rb +16 -0
  73. data/misc/ruby-pg/History.txt +9 -0
  74. data/misc/ruby-pg/Manifest.txt +5 -0
  75. data/misc/ruby-pg/README.txt +21 -0
  76. data/misc/ruby-pg/Rakefile +21 -0
  77. data/misc/ruby-pg/lib/ruby/pg.rb +16 -0
  78. data/pg.gemspec +32 -0
  79. data/sample/array_insert.rb +20 -0
  80. data/sample/async_api.rb +106 -0
  81. data/sample/async_copyto.rb +39 -0
  82. data/sample/async_mixed.rb +56 -0
  83. data/sample/check_conn.rb +21 -0
  84. data/sample/copydata.rb +71 -0
  85. data/sample/copyfrom.rb +81 -0
  86. data/sample/copyto.rb +19 -0
  87. data/sample/cursor.rb +21 -0
  88. data/sample/disk_usage_report.rb +177 -0
  89. data/sample/issue-119.rb +94 -0
  90. data/sample/losample.rb +69 -0
  91. data/sample/minimal-testcase.rb +17 -0
  92. data/sample/notify_wait.rb +72 -0
  93. data/sample/pg_statistics.rb +285 -0
  94. data/sample/replication_monitor.rb +222 -0
  95. data/sample/test_binary_values.rb +33 -0
  96. data/sample/wal_shipper.rb +434 -0
  97. data/sample/warehouse_partitions.rb +311 -0
  98. data.tar.gz.sig +0 -0
  99. metadata +96 -229
  100. metadata.gz.sig +0 -0
  101. data/ChangeLog +0 -6595
  102. data/lib/pg/basic_type_mapping.rb +0 -426
  103. data/lib/pg/deprecated_constants.rb +0 -21
  104. data/spec/data/expected_trace.out +0 -26
  105. data/spec/data/random_binary_data +0 -0
  106. data/spec/helpers.rb +0 -352
  107. data/spec/pg/basic_type_mapping_spec.rb +0 -305
  108. data/spec/pg/connection_spec.rb +0 -1676
  109. data/spec/pg/result_spec.rb +0 -456
  110. data/spec/pg/type_map_by_class_spec.rb +0 -138
  111. data/spec/pg/type_map_by_column_spec.rb +0 -222
  112. data/spec/pg/type_map_by_mri_type_spec.rb +0 -136
  113. data/spec/pg/type_map_by_oid_spec.rb +0 -149
  114. data/spec/pg/type_map_in_ruby_spec.rb +0 -164
  115. data/spec/pg/type_map_spec.rb +0 -22
  116. data/spec/pg/type_spec.rb +0 -777
  117. data/spec/pg_spec.rb +0 -50
data/ext/pg_tuple.c ADDED
@@ -0,0 +1,575 @@
1
+ #include "pg.h"
2
+
3
+ /********************************************************************
4
+ *
5
+ * Document-class: PG::Tuple
6
+ *
7
+ * The class to represent one query result tuple (row).
8
+ * An instance of this class can be created by PG::Result#tuple .
9
+ *
10
+ * All field values of the tuple are retrieved on demand from the underlying PGresult object and converted to a Ruby object.
11
+ * Subsequent access to the same field returns the same object, since they are cached when materialized.
12
+ * Each PG::Tuple holds a reference to the related PG::Result object, but gets detached, when all fields are materialized.
13
+ *
14
+ * Example:
15
+ * require 'pg'
16
+ * conn = PG.connect(:dbname => 'test')
17
+ * res = conn.exec('VALUES(1,2), (3,4)')
18
+ * t0 = res.tuple(0) # => #<PG::Tuple column1: "1", column2: "2">
19
+ * t1 = res.tuple(1) # => #<PG::Tuple column1: "3", column2: "4">
20
+ * t1[0] # => "3"
21
+ * t1["column2"] # => "4"
22
+ */
23
+
24
+ static VALUE rb_cPG_Tuple;
25
+
26
+ typedef struct {
27
+ /* PG::Result object this tuple was retrieved from.
28
+ * Qnil when all fields are materialized.
29
+ */
30
+ VALUE result;
31
+
32
+ /* Store the typemap of the result.
33
+ * It's not enough to reference the PG::TypeMap object through the result,
34
+ * since it could be exchanged after the tuple has been created.
35
+ */
36
+ VALUE typemap;
37
+
38
+ /* Hash with maps field names to index into values[]
39
+ * Shared between all instances retrieved from one PG::Result.
40
+ */
41
+ VALUE field_map;
42
+
43
+ /* Row number within the result set. */
44
+ int row_num;
45
+
46
+ /* Number of fields in the result set. */
47
+ int num_fields;
48
+
49
+ /* Materialized values.
50
+ * And in case of dup column names, a field_names Array subsequently.
51
+ */
52
+ VALUE values[0];
53
+ } t_pg_tuple;
54
+
55
+ static inline VALUE *
56
+ pg_tuple_get_field_names_ptr( t_pg_tuple *this )
57
+ {
58
+ if( this->num_fields != (int)RHASH_SIZE(this->field_map) ){
59
+ return &this->values[this->num_fields];
60
+ } else {
61
+ static VALUE f = Qfalse;
62
+ return &f;
63
+ }
64
+ }
65
+
66
+ static inline VALUE
67
+ pg_tuple_get_field_names( t_pg_tuple *this )
68
+ {
69
+ return *pg_tuple_get_field_names_ptr(this);
70
+ }
71
+
72
+ static void
73
+ pg_tuple_gc_mark( void *_this )
74
+ {
75
+ t_pg_tuple *this = (t_pg_tuple *)_this;
76
+ int i;
77
+
78
+ if( !this ) return;
79
+ rb_gc_mark_movable( this->result );
80
+ rb_gc_mark_movable( this->typemap );
81
+ rb_gc_mark_movable( this->field_map );
82
+
83
+ for( i = 0; i < this->num_fields; i++ ){
84
+ rb_gc_mark_movable( this->values[i] );
85
+ }
86
+ rb_gc_mark_movable( pg_tuple_get_field_names(this) );
87
+ }
88
+
89
+ static void
90
+ pg_tuple_gc_compact( void *_this )
91
+ {
92
+ t_pg_tuple *this = (t_pg_tuple *)_this;
93
+ int i;
94
+
95
+ if( !this ) return;
96
+ pg_gc_location( this->result );
97
+ pg_gc_location( this->typemap );
98
+ pg_gc_location( this->field_map );
99
+
100
+ for( i = 0; i < this->num_fields; i++ ){
101
+ pg_gc_location( this->values[i] );
102
+ }
103
+ pg_gc_location( *pg_tuple_get_field_names_ptr(this) );
104
+ }
105
+
106
+ static void
107
+ pg_tuple_gc_free( void *_this )
108
+ {
109
+ t_pg_tuple *this = (t_pg_tuple *)_this;
110
+ if( !this ) return;
111
+ xfree(this);
112
+ }
113
+
114
+ static size_t
115
+ pg_tuple_memsize( const void *_this )
116
+ {
117
+ const t_pg_tuple *this = (const t_pg_tuple *)_this;
118
+ if( this==NULL ) return 0;
119
+ return sizeof(*this) + sizeof(*this->values) * this->num_fields;
120
+ }
121
+
122
+ static const rb_data_type_t pg_tuple_type = {
123
+ "PG::Tuple",
124
+ {
125
+ pg_tuple_gc_mark,
126
+ pg_tuple_gc_free,
127
+ pg_tuple_memsize,
128
+ pg_compact_callback(pg_tuple_gc_compact),
129
+ },
130
+ 0, 0,
131
+ RUBY_TYPED_FREE_IMMEDIATELY,
132
+ };
133
+
134
+ /*
135
+ * Document-method: allocate
136
+ *
137
+ * call-seq:
138
+ * PG::VeryTuple.allocate -> obj
139
+ */
140
+ static VALUE
141
+ pg_tuple_s_allocate( VALUE klass )
142
+ {
143
+ return TypedData_Wrap_Struct( klass, &pg_tuple_type, NULL );
144
+ }
145
+
146
+ VALUE
147
+ pg_tuple_new(VALUE result, int row_num)
148
+ {
149
+ t_pg_tuple *this;
150
+ VALUE self = pg_tuple_s_allocate( rb_cPG_Tuple );
151
+ t_pg_result *p_result = pgresult_get_this(result);
152
+ int num_fields = p_result->nfields;
153
+ int i;
154
+ VALUE field_map = p_result->field_map;
155
+ int dup_names = num_fields != (int)RHASH_SIZE(field_map);
156
+
157
+ this = (t_pg_tuple *)xmalloc(
158
+ sizeof(*this) +
159
+ sizeof(*this->values) * num_fields +
160
+ sizeof(*this->values) * (dup_names ? 1 : 0));
161
+
162
+ this->result = result;
163
+ this->typemap = p_result->typemap;
164
+ this->field_map = field_map;
165
+ this->row_num = row_num;
166
+ this->num_fields = num_fields;
167
+
168
+ for( i = 0; i < num_fields; i++ ){
169
+ this->values[i] = Qundef;
170
+ }
171
+
172
+ if( dup_names ){
173
+ /* Some of the column names are duplicated -> we need the keys as Array in addition.
174
+ * Store it behind the values to save the space in the common case of no dups.
175
+ */
176
+ this->values[num_fields] = rb_obj_freeze(rb_ary_new4(num_fields, p_result->fnames));
177
+ }
178
+
179
+ RTYPEDDATA_DATA(self) = this;
180
+
181
+ return self;
182
+ }
183
+
184
+ static inline t_pg_tuple *
185
+ pg_tuple_get_this( VALUE self )
186
+ {
187
+ t_pg_tuple *this;
188
+ TypedData_Get_Struct(self, t_pg_tuple, &pg_tuple_type, this);
189
+ if (this == NULL)
190
+ rb_raise(rb_eTypeError, "tuple is empty");
191
+
192
+ return this;
193
+ }
194
+
195
+ static VALUE
196
+ pg_tuple_materialize_field(t_pg_tuple *this, int col)
197
+ {
198
+ VALUE value = this->values[col];
199
+
200
+ if( value == Qundef ){
201
+ t_typemap *p_typemap = RTYPEDDATA_DATA( this->typemap );
202
+
203
+ pgresult_get(this->result); /* make sure we have a valid PGresult object */
204
+ value = p_typemap->funcs.typecast_result_value(p_typemap, this->result, this->row_num, col);
205
+ this->values[col] = value;
206
+ }
207
+
208
+ return value;
209
+ }
210
+
211
+ static void
212
+ pg_tuple_detach(t_pg_tuple *this)
213
+ {
214
+ this->result = Qnil;
215
+ this->typemap = Qnil;
216
+ this->row_num = -1;
217
+ }
218
+
219
+ static void
220
+ pg_tuple_materialize(t_pg_tuple *this)
221
+ {
222
+ int field_num;
223
+ for(field_num = 0; field_num < this->num_fields; field_num++) {
224
+ pg_tuple_materialize_field(this, field_num);
225
+ }
226
+
227
+ pg_tuple_detach(this);
228
+ }
229
+
230
+ /*
231
+ * call-seq:
232
+ * tup.fetch(key) → value
233
+ * tup.fetch(key, default) → value
234
+ * tup.fetch(key) { |key| block } → value
235
+ *
236
+ * Returns a field value by either column index or column name.
237
+ *
238
+ * An integer +key+ is interpreted as column index.
239
+ * Negative values of index count from the end of the array.
240
+ *
241
+ * Depending on Result#field_name_type= a string or symbol +key+ is interpreted as column name.
242
+ *
243
+ * If the key can't be found, there are several options:
244
+ * With no other arguments, it will raise a IndexError exception;
245
+ * if default is given, then that will be returned;
246
+ * if the optional code block is specified, then that will be run and its result returned.
247
+ */
248
+ static VALUE
249
+ pg_tuple_fetch(int argc, VALUE *argv, VALUE self)
250
+ {
251
+ VALUE key;
252
+ long block_given;
253
+ VALUE index;
254
+ int field_num;
255
+ t_pg_tuple *this = pg_tuple_get_this(self);
256
+
257
+ rb_check_arity(argc, 1, 2);
258
+ key = argv[0];
259
+
260
+ block_given = rb_block_given_p();
261
+ if (block_given && argc == 2) {
262
+ rb_warn("block supersedes default value argument");
263
+ }
264
+
265
+ switch(rb_type(key)){
266
+ case T_FIXNUM:
267
+ case T_BIGNUM:
268
+ field_num = NUM2INT(key);
269
+ if ( field_num < 0 )
270
+ field_num = this->num_fields + field_num;
271
+ if ( field_num < 0 || field_num >= this->num_fields ){
272
+ if (block_given) return rb_yield(key);
273
+ if (argc == 1) rb_raise( rb_eIndexError, "Index %d is out of range", field_num );
274
+ return argv[1];
275
+ }
276
+ break;
277
+ default:
278
+ index = rb_hash_aref(this->field_map, key);
279
+
280
+ if (index == Qnil) {
281
+ if (block_given) return rb_yield(key);
282
+ if (argc == 1) rb_raise( rb_eKeyError, "column not found" );
283
+ return argv[1];
284
+ }
285
+
286
+ field_num = NUM2INT(index);
287
+ }
288
+
289
+ return pg_tuple_materialize_field(this, field_num);
290
+ }
291
+
292
+ /*
293
+ * call-seq:
294
+ * tup[ key ] -> value
295
+ *
296
+ * Returns a field value by either column index or column name.
297
+ *
298
+ * An integer +key+ is interpreted as column index.
299
+ * Negative values of index count from the end of the array.
300
+ *
301
+ * Depending on Result#field_name_type= a string or symbol +key+ is interpreted as column name.
302
+ *
303
+ * If the key can't be found, it returns +nil+ .
304
+ */
305
+ static VALUE
306
+ pg_tuple_aref(VALUE self, VALUE key)
307
+ {
308
+ VALUE index;
309
+ int field_num;
310
+ t_pg_tuple *this = pg_tuple_get_this(self);
311
+
312
+ switch(rb_type(key)){
313
+ case T_FIXNUM:
314
+ case T_BIGNUM:
315
+ field_num = NUM2INT(key);
316
+ if ( field_num < 0 )
317
+ field_num = this->num_fields + field_num;
318
+ if ( field_num < 0 || field_num >= this->num_fields )
319
+ return Qnil;
320
+ break;
321
+ default:
322
+ index = rb_hash_aref(this->field_map, key);
323
+ if( index == Qnil ) return Qnil;
324
+ field_num = NUM2INT(index);
325
+ }
326
+
327
+ return pg_tuple_materialize_field(this, field_num);
328
+ }
329
+
330
+ static VALUE
331
+ pg_tuple_num_fields_for_enum(VALUE self, VALUE args, VALUE eobj)
332
+ {
333
+ t_pg_tuple *this = pg_tuple_get_this(self);
334
+ return INT2NUM(this->num_fields);
335
+ }
336
+
337
+ static int
338
+ pg_tuple_yield_key_value(VALUE key, VALUE index, VALUE _this)
339
+ {
340
+ t_pg_tuple *this = (t_pg_tuple *)_this;
341
+ VALUE value = pg_tuple_materialize_field(this, NUM2INT(index));
342
+ rb_yield_values(2, key, value);
343
+ return ST_CONTINUE;
344
+ }
345
+
346
+ /*
347
+ * call-seq:
348
+ * tup.each{ |key, value| ... }
349
+ *
350
+ * Invokes block for each field name and value in the tuple.
351
+ */
352
+ static VALUE
353
+ pg_tuple_each(VALUE self)
354
+ {
355
+ t_pg_tuple *this = pg_tuple_get_this(self);
356
+ VALUE field_names;
357
+
358
+ RETURN_SIZED_ENUMERATOR(self, 0, NULL, pg_tuple_num_fields_for_enum);
359
+
360
+ field_names = pg_tuple_get_field_names(this);
361
+
362
+ if( field_names == Qfalse ){
363
+ rb_hash_foreach(this->field_map, pg_tuple_yield_key_value, (VALUE)this);
364
+ } else {
365
+ int i;
366
+ for( i = 0; i < this->num_fields; i++ ){
367
+ VALUE value = pg_tuple_materialize_field(this, i);
368
+ rb_yield_values(2, RARRAY_AREF(field_names, i), value);
369
+ }
370
+ }
371
+
372
+ pg_tuple_detach(this);
373
+ return self;
374
+ }
375
+
376
+ /*
377
+ * call-seq:
378
+ * tup.each_value{ |value| ... }
379
+ *
380
+ * Invokes block for each field value in the tuple.
381
+ */
382
+ static VALUE
383
+ pg_tuple_each_value(VALUE self)
384
+ {
385
+ t_pg_tuple *this = pg_tuple_get_this(self);
386
+ int field_num;
387
+
388
+ RETURN_SIZED_ENUMERATOR(self, 0, NULL, pg_tuple_num_fields_for_enum);
389
+
390
+ for(field_num = 0; field_num < this->num_fields; field_num++) {
391
+ VALUE value = pg_tuple_materialize_field(this, field_num);
392
+ rb_yield(value);
393
+ }
394
+
395
+ pg_tuple_detach(this);
396
+ return self;
397
+ }
398
+
399
+
400
+ /*
401
+ * call-seq:
402
+ * tup.values -> Array
403
+ *
404
+ * Returns the values of this tuple as Array.
405
+ * +res.tuple(i).values+ is equal to +res.tuple_values(i)+ .
406
+ */
407
+ static VALUE
408
+ pg_tuple_values(VALUE self)
409
+ {
410
+ t_pg_tuple *this = pg_tuple_get_this(self);
411
+
412
+ pg_tuple_materialize(this);
413
+ return rb_ary_new4(this->num_fields, &this->values[0]);
414
+ }
415
+
416
+ static VALUE
417
+ pg_tuple_field_map(VALUE self)
418
+ {
419
+ t_pg_tuple *this = pg_tuple_get_this(self);
420
+ return this->field_map;
421
+ }
422
+
423
+ static VALUE
424
+ pg_tuple_field_names(VALUE self)
425
+ {
426
+ t_pg_tuple *this = pg_tuple_get_this(self);
427
+ return pg_tuple_get_field_names(this);
428
+ }
429
+
430
+ /*
431
+ * call-seq:
432
+ * tup.length → integer
433
+ *
434
+ * Returns number of fields of this tuple.
435
+ */
436
+ static VALUE
437
+ pg_tuple_length(VALUE self)
438
+ {
439
+ t_pg_tuple *this = pg_tuple_get_this(self);
440
+ return INT2NUM(this->num_fields);
441
+ }
442
+
443
+ /*
444
+ * call-seq:
445
+ * tup.index(key) → integer
446
+ *
447
+ * Returns the field number which matches the given column name.
448
+ */
449
+ static VALUE
450
+ pg_tuple_index(VALUE self, VALUE key)
451
+ {
452
+ t_pg_tuple *this = pg_tuple_get_this(self);
453
+ return rb_hash_aref(this->field_map, key);
454
+ }
455
+
456
+
457
+ static VALUE
458
+ pg_tuple_dump(VALUE self)
459
+ {
460
+ VALUE field_names;
461
+ VALUE values;
462
+ VALUE a;
463
+ t_pg_tuple *this = pg_tuple_get_this(self);
464
+
465
+ pg_tuple_materialize(this);
466
+
467
+ field_names = pg_tuple_get_field_names(this);
468
+ if( field_names == Qfalse )
469
+ field_names = rb_funcall(this->field_map, rb_intern("keys"), 0);
470
+
471
+ values = rb_ary_new4(this->num_fields, &this->values[0]);
472
+ a = rb_ary_new3(2, field_names, values);
473
+
474
+ if (FL_TEST(self, FL_EXIVAR)) {
475
+ rb_copy_generic_ivar(a, self);
476
+ FL_SET(a, FL_EXIVAR);
477
+ }
478
+
479
+ return a;
480
+ }
481
+
482
+ static VALUE
483
+ pg_tuple_load(VALUE self, VALUE a)
484
+ {
485
+ int num_fields;
486
+ int i;
487
+ t_pg_tuple *this;
488
+ VALUE values;
489
+ VALUE field_names;
490
+ VALUE field_map;
491
+ int dup_names;
492
+
493
+ rb_check_frozen(self);
494
+
495
+ TypedData_Get_Struct(self, t_pg_tuple, &pg_tuple_type, this);
496
+ if (this)
497
+ rb_raise(rb_eTypeError, "tuple is not empty");
498
+
499
+ Check_Type(a, T_ARRAY);
500
+ if (RARRAY_LEN(a) != 2)
501
+ rb_raise(rb_eTypeError, "expected an array of 2 elements");
502
+
503
+ field_names = RARRAY_AREF(a, 0);
504
+ Check_Type(field_names, T_ARRAY);
505
+ rb_obj_freeze(field_names);
506
+ values = RARRAY_AREF(a, 1);
507
+ Check_Type(values, T_ARRAY);
508
+ num_fields = RARRAY_LENINT(values);
509
+
510
+ if (RARRAY_LENINT(field_names) != num_fields)
511
+ rb_raise(rb_eTypeError, "different number of fields and values");
512
+
513
+ field_map = rb_hash_new();
514
+ for( i = 0; i < num_fields; i++ ){
515
+ rb_hash_aset(field_map, RARRAY_AREF(field_names, i), INT2FIX(i));
516
+ }
517
+ rb_obj_freeze(field_map);
518
+
519
+ dup_names = num_fields != (int)RHASH_SIZE(field_map);
520
+
521
+ this = (t_pg_tuple *)xmalloc(
522
+ sizeof(*this) +
523
+ sizeof(*this->values) * num_fields +
524
+ sizeof(*this->values) * (dup_names ? 1 : 0));
525
+
526
+ this->result = Qnil;
527
+ this->typemap = Qnil;
528
+ this->row_num = -1;
529
+ this->num_fields = num_fields;
530
+ this->field_map = field_map;
531
+
532
+ for( i = 0; i < num_fields; i++ ){
533
+ VALUE v = RARRAY_AREF(values, i);
534
+ if( v == Qundef )
535
+ rb_raise(rb_eTypeError, "field %d is not materialized", i);
536
+ this->values[i] = v;
537
+ }
538
+
539
+ if( dup_names ){
540
+ this->values[num_fields] = field_names;
541
+ }
542
+
543
+ RTYPEDDATA_DATA(self) = this;
544
+
545
+ if (FL_TEST(a, FL_EXIVAR)) {
546
+ rb_copy_generic_ivar(self, a);
547
+ FL_SET(self, FL_EXIVAR);
548
+ }
549
+
550
+ return self;
551
+ }
552
+
553
+ void
554
+ init_pg_tuple()
555
+ {
556
+ rb_cPG_Tuple = rb_define_class_under( rb_mPG, "Tuple", rb_cObject );
557
+ rb_define_alloc_func( rb_cPG_Tuple, pg_tuple_s_allocate );
558
+ rb_include_module(rb_cPG_Tuple, rb_mEnumerable);
559
+
560
+ rb_define_method(rb_cPG_Tuple, "fetch", pg_tuple_fetch, -1);
561
+ rb_define_method(rb_cPG_Tuple, "[]", pg_tuple_aref, 1);
562
+ rb_define_method(rb_cPG_Tuple, "each", pg_tuple_each, 0);
563
+ rb_define_method(rb_cPG_Tuple, "each_value", pg_tuple_each_value, 0);
564
+ rb_define_method(rb_cPG_Tuple, "values", pg_tuple_values, 0);
565
+ rb_define_method(rb_cPG_Tuple, "length", pg_tuple_length, 0);
566
+ rb_define_alias(rb_cPG_Tuple, "size", "length");
567
+ rb_define_method(rb_cPG_Tuple, "index", pg_tuple_index, 1);
568
+
569
+ rb_define_private_method(rb_cPG_Tuple, "field_map", pg_tuple_field_map, 0);
570
+ rb_define_private_method(rb_cPG_Tuple, "field_names", pg_tuple_field_names, 0);
571
+
572
+ /* methods for marshaling */
573
+ rb_define_private_method(rb_cPG_Tuple, "marshal_dump", pg_tuple_dump, 0);
574
+ rb_define_private_method(rb_cPG_Tuple, "marshal_load", pg_tuple_load, 1);
575
+ }
data/ext/pg_type_map.c CHANGED
@@ -1,11 +1,45 @@
1
1
  /*
2
2
  * pg_column_map.c - PG::ColumnMap class extension
3
- * $Id: pg_type_map.c,v 2af122820861 2017/01/14 19:56:36 lars $
3
+ * $Id$
4
4
  *
5
5
  */
6
6
 
7
7
  #include "pg.h"
8
8
 
9
+ void
10
+ pg_typemap_mark( void *_this )
11
+ {
12
+ t_typemap *this = (t_typemap *)_this;
13
+ rb_gc_mark_movable(this->default_typemap);
14
+ }
15
+
16
+ size_t
17
+ pg_typemap_memsize( const void *_this )
18
+ {
19
+ t_typemap *this = (t_typemap *)_this;
20
+ return sizeof(*this);
21
+ }
22
+
23
+ void
24
+ pg_typemap_compact( void *_this )
25
+ {
26
+ t_typemap *this = (t_typemap *)_this;
27
+ pg_gc_location(this->default_typemap);
28
+ }
29
+
30
+ const rb_data_type_t pg_typemap_type = {
31
+ "PG::TypeMap",
32
+ {
33
+ pg_typemap_mark,
34
+ RUBY_TYPED_DEFAULT_FREE,
35
+ pg_typemap_memsize,
36
+ pg_compact_callback(pg_typemap_compact),
37
+ },
38
+ 0,
39
+ 0,
40
+ RUBY_TYPED_FREE_IMMEDIATELY,
41
+ };
42
+
9
43
  VALUE rb_cTypeMap;
10
44
  VALUE rb_mDefaultTypeMappable;
11
45
  static ID s_id_fit_to_query;
@@ -75,7 +109,7 @@ pg_typemap_s_allocate( VALUE klass )
75
109
  VALUE self;
76
110
  t_typemap *this;
77
111
 
78
- self = Data_Make_Struct( klass, t_typemap, NULL, -1, this );
112
+ self = TypedData_Make_Struct( klass, t_typemap, &pg_typemap_type, this );
79
113
  this->funcs = pg_typemap_funcs;
80
114
 
81
115
  return self;
@@ -94,13 +128,12 @@ pg_typemap_s_allocate( VALUE klass )
94
128
  static VALUE
95
129
  pg_typemap_default_type_map_set(VALUE self, VALUE typemap)
96
130
  {
97
- t_typemap *this = DATA_PTR( self );
131
+ t_typemap *this = RTYPEDDATA_DATA( self );
132
+ t_typemap *tm;
133
+ UNUSED(tm);
98
134
 
99
- if ( !rb_obj_is_kind_of(typemap, rb_cTypeMap) ) {
100
- rb_raise( rb_eTypeError, "wrong argument type %s (expected kind of PG::TypeMap)",
101
- rb_obj_classname( typemap ) );
102
- }
103
- Check_Type(typemap, T_DATA);
135
+ /* Check type of method param */
136
+ TypedData_Get_Struct(typemap, t_typemap, &pg_typemap_type, tm);
104
137
  this->default_typemap = typemap;
105
138
 
106
139
  return typemap;
@@ -119,7 +152,7 @@ pg_typemap_default_type_map_set(VALUE self, VALUE typemap)
119
152
  static VALUE
120
153
  pg_typemap_default_type_map_get(VALUE self)
121
154
  {
122
- t_typemap *this = DATA_PTR( self );
155
+ t_typemap *this = RTYPEDDATA_DATA( self );
123
156
 
124
157
  return this->default_typemap;
125
158
  }
@@ -1,6 +1,6 @@
1
1
  /*
2
2
  * pg_type_map_all_strings.c - PG::TypeMapAllStrings class extension
3
- * $Id: pg_type_map_all_strings.c,v c53f993a4254 2014/12/12 21:57:29 lars $
3
+ * $Id$
4
4
  *
5
5
  * This is the default typemap.
6
6
  *
@@ -8,6 +8,19 @@
8
8
 
9
9
  #include "pg.h"
10
10
 
11
+ static const rb_data_type_t pg_tmas_type = {
12
+ "PG::TypeMapAllStrings",
13
+ {
14
+ pg_typemap_mark,
15
+ RUBY_TYPED_DEFAULT_FREE,
16
+ pg_typemap_memsize,
17
+ pg_compact_callback(pg_typemap_compact),
18
+ },
19
+ &pg_typemap_type,
20
+ 0,
21
+ RUBY_TYPED_FREE_IMMEDIATELY,
22
+ };
23
+
11
24
  VALUE rb_cTypeMapAllStrings;
12
25
  VALUE pg_typemap_all_strings;
13
26
 
@@ -33,9 +46,9 @@ pg_tmas_result_value( t_typemap *p_typemap, VALUE result, int tuple, int field )
33
46
  len = PQgetlength( p_result->pgresult, tuple, field );
34
47
 
35
48
  if ( 0 == PQfformat(p_result->pgresult, field) ) {
36
- ret = pg_text_dec_string(NULL, val, len, tuple, field, ENCODING_GET(result));
49
+ ret = pg_text_dec_string(NULL, val, len, tuple, field, p_result->enc_idx);
37
50
  } else {
38
- ret = pg_bin_dec_bytea(NULL, val, len, tuple, field, ENCODING_GET(result));
51
+ ret = pg_bin_dec_bytea(NULL, val, len, tuple, field, p_result->enc_idx);
39
52
  }
40
53
 
41
54
  return ret;
@@ -63,6 +76,7 @@ pg_tmas_fit_to_copy_get( VALUE self )
63
76
  static VALUE
64
77
  pg_tmas_typecast_copy_get( t_typemap *p_typemap, VALUE field_str, int fieldno, int format, int enc_idx )
65
78
  {
79
+ rb_str_modify(field_str);
66
80
  if( format == 0 ){
67
81
  PG_ENCODING_SET_NOCHECK( field_str, enc_idx );
68
82
  } else {
@@ -77,7 +91,7 @@ pg_tmas_s_allocate( VALUE klass )
77
91
  t_typemap *this;
78
92
  VALUE self;
79
93
 
80
- self = Data_Make_Struct( klass, t_typemap, NULL, -1, this );
94
+ self = TypedData_Make_Struct( klass, t_typemap, &pg_tmas_type, this );
81
95
 
82
96
  this->funcs.fit_to_result = pg_tmas_fit_to_result;
83
97
  this->funcs.fit_to_query = pg_tmas_fit_to_query;
@@ -99,7 +113,7 @@ init_pg_type_map_all_strings()
99
113
  * This type map casts all values received from the database server to Strings
100
114
  * and sends all values to the server after conversion to String by +#to_s+ .
101
115
  * That means, it is hard coded to PG::TextEncoder::String for value encoding
102
- * and to PG::TextDecoder::String for text format respectivly PG::BinaryDecoder::Bytea
116
+ * and to PG::TextDecoder::String for text format respectively PG::BinaryDecoder::Bytea
103
117
  * for binary format received from the server.
104
118
  *
105
119
  * It is suitable for type casting query bind parameters, result values and