pg 1.4.4 → 1.5.9

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 (77) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data/Gemfile +6 -0
  4. data/{History.rdoc → History.md} +303 -151
  5. data/README.ja.md +300 -0
  6. data/README.md +286 -0
  7. data/Rakefile +16 -4
  8. data/Rakefile.cross +15 -14
  9. data/certs/kanis@comcard.de.pem +20 -0
  10. data/certs/larskanis-2023.pem +24 -0
  11. data/certs/larskanis-2024.pem +24 -0
  12. data/ext/errorcodes.def +8 -5
  13. data/ext/errorcodes.txt +3 -5
  14. data/ext/extconf.rb +7 -0
  15. data/ext/pg.c +15 -30
  16. data/ext/pg.h +10 -6
  17. data/ext/pg_binary_decoder.c +81 -0
  18. data/ext/pg_binary_encoder.c +224 -0
  19. data/ext/pg_coder.c +16 -7
  20. data/ext/pg_connection.c +220 -82
  21. data/ext/pg_copy_coder.c +315 -22
  22. data/ext/pg_record_coder.c +11 -10
  23. data/ext/pg_result.c +93 -19
  24. data/ext/pg_text_decoder.c +31 -10
  25. data/ext/pg_text_encoder.c +38 -19
  26. data/ext/pg_tuple.c +34 -31
  27. data/ext/pg_type_map.c +3 -2
  28. data/ext/pg_type_map_all_strings.c +2 -2
  29. data/ext/pg_type_map_by_class.c +5 -3
  30. data/ext/pg_type_map_by_column.c +7 -3
  31. data/ext/pg_type_map_by_oid.c +7 -4
  32. data/ext/pg_type_map_in_ruby.c +5 -2
  33. data/lib/pg/basic_type_map_based_on_result.rb +21 -1
  34. data/lib/pg/basic_type_map_for_queries.rb +19 -10
  35. data/lib/pg/basic_type_map_for_results.rb +26 -3
  36. data/lib/pg/basic_type_registry.rb +44 -34
  37. data/lib/pg/binary_decoder/date.rb +9 -0
  38. data/lib/pg/binary_decoder/timestamp.rb +26 -0
  39. data/lib/pg/binary_encoder/timestamp.rb +20 -0
  40. data/lib/pg/coder.rb +15 -13
  41. data/lib/pg/connection.rb +158 -64
  42. data/lib/pg/exceptions.rb +13 -0
  43. data/lib/pg/text_decoder/date.rb +21 -0
  44. data/lib/pg/text_decoder/inet.rb +9 -0
  45. data/lib/pg/text_decoder/json.rb +17 -0
  46. data/lib/pg/text_decoder/numeric.rb +9 -0
  47. data/lib/pg/text_decoder/timestamp.rb +30 -0
  48. data/lib/pg/text_encoder/date.rb +13 -0
  49. data/lib/pg/text_encoder/inet.rb +31 -0
  50. data/lib/pg/text_encoder/json.rb +17 -0
  51. data/lib/pg/text_encoder/numeric.rb +9 -0
  52. data/lib/pg/text_encoder/timestamp.rb +24 -0
  53. data/lib/pg/version.rb +1 -1
  54. data/lib/pg.rb +65 -15
  55. data/pg.gemspec +7 -3
  56. data/rakelib/task_extension.rb +1 -1
  57. data.tar.gz.sig +2 -4
  58. metadata +104 -46
  59. metadata.gz.sig +0 -0
  60. data/.appveyor.yml +0 -36
  61. data/.gems +0 -6
  62. data/.gemtest +0 -0
  63. data/.github/workflows/binary-gems.yml +0 -86
  64. data/.github/workflows/source-gem.yml +0 -131
  65. data/.gitignore +0 -13
  66. data/.hgsigs +0 -34
  67. data/.hgtags +0 -41
  68. data/.irbrc +0 -23
  69. data/.pryrc +0 -23
  70. data/.tm_properties +0 -21
  71. data/.travis.yml +0 -49
  72. data/README.ja.rdoc +0 -13
  73. data/README.rdoc +0 -233
  74. data/lib/pg/binary_decoder.rb +0 -23
  75. data/lib/pg/constants.rb +0 -12
  76. data/lib/pg/text_decoder.rb +0 -46
  77. data/lib/pg/text_encoder.rb +0 -59
data/ext/pg_copy_coder.c CHANGED
@@ -4,6 +4,7 @@
4
4
  */
5
5
 
6
6
  #include "pg.h"
7
+ #include "pg_util.h"
7
8
 
8
9
  #define ISOCTAL(c) (((c) >= '0') && ((c) <= '7'))
9
10
  #define OCTVALUE(c) ((c) - '0')
@@ -54,7 +55,7 @@ static const rb_data_type_t pg_copycoder_type = {
54
55
  },
55
56
  &pg_coder_type,
56
57
  0,
57
- RUBY_TYPED_FREE_IMMEDIATELY,
58
+ RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED | PG_RUBY_TYPED_FROZEN_SHAREABLE,
58
59
  };
59
60
 
60
61
  static VALUE
@@ -63,9 +64,9 @@ pg_copycoder_encoder_allocate( VALUE klass )
63
64
  t_pg_copycoder *this;
64
65
  VALUE self = TypedData_Make_Struct( klass, t_pg_copycoder, &pg_copycoder_type, this );
65
66
  pg_coder_init_encoder( self );
66
- this->typemap = pg_typemap_all_strings;
67
+ RB_OBJ_WRITE(self, &this->typemap, pg_typemap_all_strings);
67
68
  this->delimiter = '\t';
68
- this->null_string = rb_str_new_cstr("\\N");
69
+ RB_OBJ_WRITE(self, &this->null_string, rb_str_new_cstr("\\N"));
69
70
  return self;
70
71
  }
71
72
 
@@ -75,9 +76,9 @@ pg_copycoder_decoder_allocate( VALUE klass )
75
76
  t_pg_copycoder *this;
76
77
  VALUE self = TypedData_Make_Struct( klass, t_pg_copycoder, &pg_copycoder_type, this );
77
78
  pg_coder_init_decoder( self );
78
- this->typemap = pg_typemap_all_strings;
79
+ RB_OBJ_WRITE(self, &this->typemap, pg_typemap_all_strings);
79
80
  this->delimiter = '\t';
80
- this->null_string = rb_str_new_cstr("\\N");
81
+ RB_OBJ_WRITE(self, &this->null_string, rb_str_new_cstr("\\N"));
81
82
  return self;
82
83
  }
83
84
 
@@ -86,13 +87,16 @@ pg_copycoder_decoder_allocate( VALUE klass )
86
87
  * coder.delimiter = String
87
88
  *
88
89
  * Specifies the character that separates columns within each row (line) of the file.
89
- * The default is a tab character in text format, a comma in CSV format.
90
- * This must be a single one-byte character. This option is ignored when using binary format.
90
+ * The default is a tab character in text format.
91
+ * This must be a single one-byte character.
92
+ *
93
+ * This option is ignored when using binary format.
91
94
  */
92
95
  static VALUE
93
96
  pg_copycoder_delimiter_set(VALUE self, VALUE delimiter)
94
97
  {
95
98
  t_pg_copycoder *this = RTYPEDDATA_DATA(self);
99
+ rb_check_frozen(self);
96
100
  StringValue(delimiter);
97
101
  if(RSTRING_LEN(delimiter) != 1)
98
102
  rb_raise( rb_eArgError, "delimiter size must be one byte");
@@ -114,17 +118,19 @@ pg_copycoder_delimiter_get(VALUE self)
114
118
  }
115
119
 
116
120
  /*
117
- * Specifies the string that represents a null value. The default is \\N (backslash-N)
118
- * in text format, and an unquoted empty string in CSV format. You might prefer an
119
- * empty string even in text format for cases where you don't want to distinguish nulls
120
- * from empty strings. This option is ignored when using binary format.
121
+ * Specifies the string that represents a null value.
122
+ * The default is \\N (backslash-N) in text format.
123
+ * You might prefer an empty string even in text format for cases where you don't want to distinguish nulls from empty strings.
124
+ *
125
+ * This option is ignored when using binary format.
121
126
  */
122
127
  static VALUE
123
128
  pg_copycoder_null_string_set(VALUE self, VALUE null_string)
124
129
  {
125
130
  t_pg_copycoder *this = RTYPEDDATA_DATA(self);
131
+ rb_check_frozen(self);
126
132
  StringValue(null_string);
127
- this->null_string = null_string;
133
+ RB_OBJ_WRITE(self, &this->null_string, null_string);
128
134
  return null_string;
129
135
  }
130
136
 
@@ -154,11 +160,12 @@ pg_copycoder_type_map_set(VALUE self, VALUE type_map)
154
160
  {
155
161
  t_pg_copycoder *this = RTYPEDDATA_DATA( self );
156
162
 
163
+ rb_check_frozen(self);
157
164
  if ( !rb_obj_is_kind_of(type_map, rb_cTypeMap) ){
158
165
  rb_raise( rb_eTypeError, "wrong elements type %s (expected some kind of PG::TypeMap)",
159
166
  rb_obj_classname( type_map ) );
160
167
  }
161
- this->typemap = type_map;
168
+ RB_OBJ_WRITE(self, &this->typemap, type_map);
162
169
 
163
170
  return type_map;
164
171
  }
@@ -205,6 +212,7 @@ pg_copycoder_type_map_get(VALUE self)
205
212
  *
206
213
  * See also PG::TextDecoder::CopyRow for the decoding direction with
207
214
  * PG::Connection#get_copy_data .
215
+ * And see PG::BinaryEncoder::CopyRow for an encoder of the COPY binary format.
208
216
  */
209
217
  static int
210
218
  pg_text_enc_copy_row(t_pg_coder *conv, VALUE value, char *out, VALUE *intermediate, int enc_idx)
@@ -228,7 +236,7 @@ pg_text_enc_copy_row(t_pg_coder *conv, VALUE value, char *out, VALUE *intermedia
228
236
  char *ptr1;
229
237
  char *ptr2;
230
238
  int strlen;
231
- int backslashs;
239
+ int backslashes;
232
240
  VALUE subint;
233
241
  VALUE entry;
234
242
 
@@ -279,19 +287,19 @@ pg_text_enc_copy_row(t_pg_coder *conv, VALUE value, char *out, VALUE *intermedia
279
287
  ptr2 = current_out + strlen;
280
288
 
281
289
  /* count required backlashs */
282
- for(backslashs = 0; ptr1 != ptr2; ptr1++) {
290
+ for(backslashes = 0; ptr1 != ptr2; ptr1++) {
283
291
  /* Escape backslash itself, newline, carriage return, and the current delimiter character. */
284
292
  if(*ptr1 == '\\' || *ptr1 == '\n' || *ptr1 == '\r' || *ptr1 == this->delimiter){
285
- backslashs++;
293
+ backslashes++;
286
294
  }
287
295
  }
288
296
 
289
297
  ptr1 = current_out + strlen;
290
- ptr2 = current_out + strlen + backslashs;
298
+ ptr2 = current_out + strlen + backslashes;
291
299
  current_out = ptr2;
292
300
 
293
301
  /* Then store the escaped string on the final position, walking
294
- * right to left, until all backslashs are placed. */
302
+ * right to left, until all backslashes are placed. */
295
303
  while( ptr1 != ptr2 ) {
296
304
  *--ptr2 = *--ptr1;
297
305
  if(*ptr1 == '\\' || *ptr1 == '\n' || *ptr1 == '\r' || *ptr1 == this->delimiter){
@@ -310,6 +318,124 @@ pg_text_enc_copy_row(t_pg_coder *conv, VALUE value, char *out, VALUE *intermedia
310
318
  }
311
319
 
312
320
 
321
+ /*
322
+ * Document-class: PG::BinaryEncoder::CopyRow < PG::CopyEncoder
323
+ *
324
+ * This class encodes one row of arbitrary columns for transmission as COPY data in binary format.
325
+ * See the {COPY command}[http://www.postgresql.org/docs/current/static/sql-copy.html]
326
+ * for description of the format.
327
+ *
328
+ * It is intended to be used in conjunction with PG::Connection#put_copy_data .
329
+ *
330
+ * The columns are expected as Array of values. The single values are encoded as defined
331
+ * in the assigned #type_map. If no type_map was assigned, all values are converted to
332
+ * strings by PG::BinaryEncoder::String.
333
+ *
334
+ * Example with default type map ( TypeMapAllStrings ):
335
+ * conn.exec "create table my_table (a text,b int,c bool)"
336
+ * enco = PG::BinaryEncoder::CopyRow.new
337
+ * conn.copy_data "COPY my_table FROM STDIN WITH (FORMAT binary)", enco do
338
+ * conn.put_copy_data ["astring", "\x00\x00\x00\a", "\x00"]
339
+ * conn.put_copy_data ["string2", "\x00\x00\x00*", "\x01"]
340
+ * end
341
+ * This creates +my_table+ and inserts two rows with binary fields.
342
+ *
343
+ * The binary format is less portable and less readable than the text format.
344
+ * It is therefore recommended to either manually assign a type encoder for each column per PG::TypeMapByColumn,
345
+ * or to make use of PG::BasicTypeMapBasedOnResult to assign them based on the table OIDs.
346
+ *
347
+ * Manually assigning a type encoder works per type map like so:
348
+ *
349
+ * conn.exec "create table my_table (a text,b int,c bool)"
350
+ * tm = PG::TypeMapByColumn.new( [
351
+ * PG::BinaryEncoder::String.new,
352
+ * PG::BinaryEncoder::Int4.new,
353
+ * PG::BinaryEncoder::Boolean.new] )
354
+ * enco = PG::BinaryEncoder::CopyRow.new( type_map: tm )
355
+ * conn.copy_data "COPY my_table FROM STDIN WITH (FORMAT binary)", enco do
356
+ * conn.put_copy_data ["astring", 7, false]
357
+ * conn.put_copy_data ["string2", 42, true]
358
+ * end
359
+ *
360
+ * See also PG::BinaryDecoder::CopyRow for the decoding direction with
361
+ * PG::Connection#get_copy_data .
362
+ * And see PG::TextEncoder::CopyRow for an encoder of the COPY text format.
363
+ */
364
+ static int
365
+ pg_bin_enc_copy_row(t_pg_coder *conv, VALUE value, char *out, VALUE *intermediate, int enc_idx)
366
+ {
367
+ t_pg_copycoder *this = (t_pg_copycoder *)conv;
368
+ int i;
369
+ t_typemap *p_typemap;
370
+ char *current_out;
371
+ char *end_capa_ptr;
372
+
373
+ p_typemap = RTYPEDDATA_DATA( this->typemap );
374
+ p_typemap->funcs.fit_to_query( this->typemap, value );
375
+
376
+ /* Allocate a new string with embedded capacity and realloc exponential when needed. */
377
+ PG_RB_STR_NEW( *intermediate, current_out, end_capa_ptr );
378
+ PG_ENCODING_SET_NOCHECK(*intermediate, enc_idx);
379
+
380
+ /* 2 bytes for number of fields */
381
+ PG_RB_STR_ENSURE_CAPA( *intermediate, 2, current_out, end_capa_ptr );
382
+ write_nbo16(RARRAY_LEN(value), current_out);
383
+ current_out += 2;
384
+
385
+ for( i=0; i<RARRAY_LEN(value); i++){
386
+ int strlen;
387
+ VALUE subint;
388
+ VALUE entry;
389
+ t_pg_coder_enc_func enc_func;
390
+ static t_pg_coder *p_elem_coder;
391
+
392
+ entry = rb_ary_entry(value, i);
393
+
394
+ switch(TYPE(entry)){
395
+ case T_NIL:
396
+ /* 4 bytes for -1 indicating a NULL value */
397
+ PG_RB_STR_ENSURE_CAPA( *intermediate, 4, current_out, end_capa_ptr );
398
+ write_nbo32(-1, current_out);
399
+ current_out += 4;
400
+ break;
401
+ default:
402
+ p_elem_coder = p_typemap->funcs.typecast_query_param(p_typemap, entry, i);
403
+ enc_func = pg_coder_enc_func(p_elem_coder);
404
+
405
+ /* 1st pass for retiving the required memory space */
406
+ strlen = enc_func(p_elem_coder, entry, NULL, &subint, enc_idx);
407
+
408
+ if( strlen == -1 ){
409
+ /* we can directly use String value in subint */
410
+ strlen = RSTRING_LENINT(subint);
411
+
412
+ PG_RB_STR_ENSURE_CAPA( *intermediate, 4 + strlen, current_out, end_capa_ptr );
413
+ /* 4 bytes length */
414
+ write_nbo32(strlen, current_out);
415
+ current_out += 4;
416
+
417
+ memcpy( current_out, RSTRING_PTR(subint), strlen );
418
+ current_out += strlen;
419
+ } else {
420
+ /* 2nd pass for writing the data to prepared buffer */
421
+ PG_RB_STR_ENSURE_CAPA( *intermediate, 4 + strlen, current_out, end_capa_ptr );
422
+ /* 4 bytes length */
423
+ write_nbo32(strlen, current_out);
424
+ current_out += 4;
425
+
426
+ /* Place the string at current output position. */
427
+ strlen = enc_func(p_elem_coder, entry, current_out, &subint, enc_idx);
428
+ current_out += strlen;
429
+ }
430
+ }
431
+ }
432
+
433
+ rb_str_set_len( *intermediate, current_out - RSTRING_PTR(*intermediate) );
434
+
435
+ return -1;
436
+ }
437
+
438
+
313
439
  /*
314
440
  * Return decimal value for a hexadecimal digit
315
441
  */
@@ -372,6 +498,7 @@ GetDecimalFromHex(char hex)
372
498
  *
373
499
  * See also PG::TextEncoder::CopyRow for the encoding direction with
374
500
  * PG::Connection#put_copy_data .
501
+ * And see PG::BinaryDecoder::CopyRow for a decoder of the COPY binary format.
375
502
  */
376
503
  /*
377
504
  * Parse the current line into separate attributes (fields),
@@ -591,9 +718,169 @@ pg_text_dec_copy_row(t_pg_coder *conv, const char *input_line, int len, int _tup
591
718
  }
592
719
 
593
720
 
721
+ static const char BinarySignature[11] = "PGCOPY\n\377\r\n\0";
722
+
723
+ /*
724
+ * Document-class: PG::BinaryDecoder::CopyRow < PG::CopyDecoder
725
+ *
726
+ * This class decodes one row of arbitrary columns received as COPY data in binary format.
727
+ * See the {COPY command}[http://www.postgresql.org/docs/current/static/sql-copy.html]
728
+ * for description of the format.
729
+ *
730
+ * It is intended to be used in conjunction with PG::Connection#get_copy_data .
731
+ *
732
+ * The columns are retrieved as Array of values. The single values are decoded as defined
733
+ * in the assigned #type_map. If no type_map was assigned, all values are converted to
734
+ * strings by PG::BinaryDecoder::String.
735
+ *
736
+ * Example with default type map ( TypeMapAllStrings ):
737
+ * conn.exec("CREATE TABLE my_table AS VALUES('astring', 7, FALSE), ('string2', 42, TRUE) ")
738
+ *
739
+ * deco = PG::BinaryDecoder::CopyRow.new
740
+ * conn.copy_data "COPY my_table TO STDOUT WITH (FORMAT binary)", deco do
741
+ * while row=conn.get_copy_data
742
+ * p row
743
+ * end
744
+ * end
745
+ * This prints all rows of +my_table+ in binary format:
746
+ * ["astring", "\x00\x00\x00\a", "\x00"]
747
+ * ["string2", "\x00\x00\x00*", "\x01"]
748
+ *
749
+ * Example with column based type map:
750
+ * tm = PG::TypeMapByColumn.new( [
751
+ * PG::BinaryDecoder::String.new,
752
+ * PG::BinaryDecoder::Integer.new,
753
+ * PG::BinaryDecoder::Boolean.new] )
754
+ * deco = PG::BinaryDecoder::CopyRow.new( type_map: tm )
755
+ * conn.copy_data "COPY my_table TO STDOUT WITH (FORMAT binary)", deco do
756
+ * while row=conn.get_copy_data
757
+ * p row
758
+ * end
759
+ * end
760
+ * This prints the rows with type casted columns:
761
+ * ["astring", 7, false]
762
+ * ["string2", 42, true]
763
+ *
764
+ * Instead of manually assigning a type decoder for each column, PG::BasicTypeMapForResults
765
+ * can be used to assign them based on the table OIDs.
766
+ *
767
+ * See also PG::BinaryEncoder::CopyRow for the encoding direction with
768
+ * PG::Connection#put_copy_data .
769
+ * And see PG::TextDecoder::CopyRow for a decoder of the COPY text format.
770
+ */
771
+ static VALUE
772
+ pg_bin_dec_copy_row(t_pg_coder *conv, const char *input_line, int len, int _tuple, int _field, int enc_idx)
773
+ {
774
+ t_pg_copycoder *this = (t_pg_copycoder *)conv;
775
+
776
+ /* Return value: array */
777
+ VALUE array;
778
+
779
+ /* Current field */
780
+ VALUE field_str;
781
+
782
+ int nfields;
783
+ int expected_fields;
784
+ int fieldno;
785
+ char *output_ptr;
786
+ const char *cur_ptr;
787
+ const char *line_end_ptr;
788
+ char *end_capa_ptr;
789
+ t_typemap *p_typemap;
790
+
791
+ p_typemap = RTYPEDDATA_DATA( this->typemap );
792
+ expected_fields = p_typemap->funcs.fit_to_copy_get( this->typemap );
793
+
794
+ /* Allocate a new string with embedded capacity and realloc later with
795
+ * exponential growing size when needed. */
796
+ PG_RB_STR_NEW( field_str, output_ptr, end_capa_ptr );
797
+
798
+ /* set pointer variables for loop */
799
+ cur_ptr = input_line;
800
+ line_end_ptr = input_line + len;
801
+
802
+ if (line_end_ptr - cur_ptr >= 11 && memcmp(cur_ptr, BinarySignature, 11) == 0){
803
+ /* binary COPY header signature detected -> just drop it */
804
+ int ext_bytes;
805
+ cur_ptr += 11;
806
+
807
+ /* read flags */
808
+ if (line_end_ptr - cur_ptr < 4 ) goto length_error;
809
+ cur_ptr += 4;
810
+
811
+ /* read header extensions */
812
+ if (line_end_ptr - cur_ptr < 4 ) goto length_error;
813
+ ext_bytes = read_nbo32(cur_ptr);
814
+ if (ext_bytes < 0) goto length_error;
815
+ cur_ptr += 4;
816
+ if (line_end_ptr - cur_ptr < ext_bytes ) goto length_error;
817
+ cur_ptr += ext_bytes;
818
+ }
819
+
820
+ /* read row header */
821
+ if (line_end_ptr - cur_ptr < 2 ) goto length_error;
822
+ nfields = read_nbo16(cur_ptr);
823
+ cur_ptr += 2;
824
+
825
+ /* COPY data trailer? */
826
+ if (nfields < 0) {
827
+ if (nfields != -1) goto length_error;
828
+ array = Qnil;
829
+ } else {
830
+ array = rb_ary_new2(expected_fields);
831
+
832
+ for( fieldno = 0; fieldno < nfields; fieldno++){
833
+ long input_len;
834
+ VALUE field_value;
835
+
836
+ /* read field size */
837
+ if (line_end_ptr - cur_ptr < 4 ) goto length_error;
838
+ input_len = read_nbo32(cur_ptr);
839
+ cur_ptr += 4;
840
+
841
+ if (input_len < 0) {
842
+ if (input_len != -1) goto length_error;
843
+ /* NULL indicator */
844
+ rb_ary_push(array, Qnil);
845
+ } else {
846
+ if (line_end_ptr - cur_ptr < input_len ) goto length_error;
847
+
848
+ /* copy input data to field_str */
849
+ PG_RB_STR_ENSURE_CAPA( field_str, input_len, output_ptr, end_capa_ptr );
850
+ memcpy(output_ptr, cur_ptr, input_len);
851
+ cur_ptr += input_len;
852
+ output_ptr += input_len;
853
+ /* convert field_str through the type map */
854
+ rb_str_set_len( field_str, output_ptr - RSTRING_PTR(field_str) );
855
+ field_value = p_typemap->funcs.typecast_copy_get( p_typemap, field_str, fieldno, 1, enc_idx );
856
+
857
+ rb_ary_push(array, field_value);
858
+
859
+ if( field_value == field_str ){
860
+ /* Our output string will be send to the user, so we can not reuse
861
+ * it for the next field. */
862
+ PG_RB_STR_NEW( field_str, output_ptr, end_capa_ptr );
863
+ }
864
+ }
865
+
866
+ /* Reset the pointer to the start of the output/buffer string. */
867
+ output_ptr = RSTRING_PTR(field_str);
868
+ }
869
+ }
870
+
871
+ if (cur_ptr < line_end_ptr)
872
+ rb_raise( rb_eArgError, "trailing data after row data at position: %ld", (long)(cur_ptr - input_line) + 1 );
873
+
874
+ return array;
875
+
876
+ length_error:
877
+ rb_raise( rb_eArgError, "premature end of COPY data at position: %ld", (long)(cur_ptr - input_line) + 1 );
878
+ }
879
+
594
880
  void
595
881
  init_pg_copycoder(void)
596
882
  {
883
+ VALUE coder;
597
884
  /* Document-class: PG::CopyCoder < PG::Coder
598
885
  *
599
886
  * This is the base class for all type cast classes for COPY data,
@@ -616,13 +903,19 @@ init_pg_copycoder(void)
616
903
  /* Make RDoc aware of the encoder classes... */
617
904
  /* rb_mPG_TextEncoder = rb_define_module_under( rb_mPG, "TextEncoder" ); */
618
905
  /* dummy = rb_define_class_under( rb_mPG_TextEncoder, "CopyRow", rb_cPG_CopyEncoder ); */
619
- pg_define_coder( "CopyRow", pg_text_enc_copy_row, rb_cPG_CopyEncoder, rb_mPG_TextEncoder );
620
- rb_include_module( rb_cPG_CopyEncoder, rb_mPG_BinaryFormatting );
906
+ coder = pg_define_coder( "CopyRow", pg_text_enc_copy_row, rb_cPG_CopyEncoder, rb_mPG_TextEncoder );
907
+ rb_include_module( coder, rb_mPG_BinaryFormatting );
908
+ /* rb_mPG_BinaryEncoder = rb_define_module_under( rb_mPG, "BinaryEncoder" ); */
909
+ /* dummy = rb_define_class_under( rb_mPG_BinaryEncoder, "CopyRow", rb_cPG_CopyEncoder ); */
910
+ pg_define_coder( "CopyRow", pg_bin_enc_copy_row, rb_cPG_CopyEncoder, rb_mPG_BinaryEncoder );
621
911
 
622
912
  /* rb_mPG_TextDecoder = rb_define_module_under( rb_mPG, "TextDecoder" ); */
623
913
  /* dummy = rb_define_class_under( rb_mPG_TextDecoder, "CopyRow", rb_cPG_CopyDecoder ); */
624
- pg_define_coder( "CopyRow", pg_text_dec_copy_row, rb_cPG_CopyDecoder, rb_mPG_TextDecoder );
914
+ coder = pg_define_coder( "CopyRow", pg_text_dec_copy_row, rb_cPG_CopyDecoder, rb_mPG_TextDecoder );
625
915
  /* Although CopyRow is a text decoder, data can contain zero bytes and are not zero terminated.
626
916
  * They are handled like binaries. So format is set to 1 (binary). */
627
- rb_include_module( rb_cPG_CopyDecoder, rb_mPG_BinaryFormatting );
917
+ rb_include_module( coder, rb_mPG_BinaryFormatting );
918
+ /* rb_mPG_BinaryDecoder = rb_define_module_under( rb_mPG, "BinaryDecoder" ); */
919
+ /* dummy = rb_define_class_under( rb_mPG_BinaryDecoder, "CopyRow", rb_cPG_CopyDecoder ); */
920
+ pg_define_coder( "CopyRow", pg_bin_dec_copy_row, rb_cPG_CopyDecoder, rb_mPG_BinaryDecoder );
628
921
  }
@@ -47,7 +47,7 @@ static const rb_data_type_t pg_recordcoder_type = {
47
47
  },
48
48
  &pg_coder_type,
49
49
  0,
50
- RUBY_TYPED_FREE_IMMEDIATELY,
50
+ RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED | PG_RUBY_TYPED_FROZEN_SHAREABLE,
51
51
  };
52
52
 
53
53
  static VALUE
@@ -56,7 +56,7 @@ pg_recordcoder_encoder_allocate( VALUE klass )
56
56
  t_pg_recordcoder *this;
57
57
  VALUE self = TypedData_Make_Struct( klass, t_pg_recordcoder, &pg_recordcoder_type, this );
58
58
  pg_coder_init_encoder( self );
59
- this->typemap = pg_typemap_all_strings;
59
+ RB_OBJ_WRITE(self, &this->typemap, pg_typemap_all_strings);
60
60
  return self;
61
61
  }
62
62
 
@@ -66,7 +66,7 @@ pg_recordcoder_decoder_allocate( VALUE klass )
66
66
  t_pg_recordcoder *this;
67
67
  VALUE self = TypedData_Make_Struct( klass, t_pg_recordcoder, &pg_recordcoder_type, this );
68
68
  pg_coder_init_decoder( self );
69
- this->typemap = pg_typemap_all_strings;
69
+ RB_OBJ_WRITE(self, &this->typemap, pg_typemap_all_strings);
70
70
  return self;
71
71
  }
72
72
 
@@ -86,11 +86,12 @@ pg_recordcoder_type_map_set(VALUE self, VALUE type_map)
86
86
  {
87
87
  t_pg_recordcoder *this = RTYPEDDATA_DATA( self );
88
88
 
89
+ rb_check_frozen(self);
89
90
  if ( !rb_obj_is_kind_of(type_map, rb_cTypeMap) ){
90
91
  rb_raise( rb_eTypeError, "wrong elements type %s (expected some kind of PG::TypeMap)",
91
92
  rb_obj_classname( type_map ) );
92
93
  }
93
- this->typemap = type_map;
94
+ RB_OBJ_WRITE(self, &this->typemap, type_map);
94
95
 
95
96
  return type_map;
96
97
  }
@@ -197,7 +198,7 @@ pg_text_enc_record(t_pg_coder *conv, VALUE value, char *out, VALUE *intermediate
197
198
  char *ptr1;
198
199
  char *ptr2;
199
200
  long strlen;
200
- int backslashs;
201
+ int backslashes;
201
202
  VALUE subint;
202
203
  VALUE entry;
203
204
 
@@ -248,19 +249,19 @@ pg_text_enc_record(t_pg_coder *conv, VALUE value, char *out, VALUE *intermediate
248
249
  ptr2 = current_out + strlen;
249
250
 
250
251
  /* count required backlashs */
251
- for(backslashs = 0; ptr1 != ptr2; ptr1++) {
252
+ for(backslashes = 0; ptr1 != ptr2; ptr1++) {
252
253
  /* Escape backslash itself, newline, carriage return, and the current delimiter character. */
253
254
  if(*ptr1 == '"' || *ptr1 == '\\'){
254
- backslashs++;
255
+ backslashes++;
255
256
  }
256
257
  }
257
258
 
258
259
  ptr1 = current_out + strlen;
259
- ptr2 = current_out + strlen + backslashs;
260
+ ptr2 = current_out + strlen + backslashes;
260
261
  current_out = ptr2;
261
262
 
262
263
  /* Then store the escaped string on the final position, walking
263
- * right to left, until all backslashs are placed. */
264
+ * right to left, until all backslashes are placed. */
264
265
  while( ptr1 != ptr2 ) {
265
266
  *--ptr2 = *--ptr1;
266
267
  if(*ptr1 == '"' || *ptr1 == '\\'){
@@ -339,7 +340,7 @@ record_isspace(char ch)
339
340
  * conn.exec("SELECT * FROM my_table").map_types!(PG::TypeMapByColumn.new([deco]*2)).to_a
340
341
  * # => [{"v1"=>[2.0, 3.0], "v2"=>[4.0, 5.0]}, {"v1"=>[6.0, 7.0], "v2"=>[8.0, 9.0]}]
341
342
  *
342
- * It's more very convenient to use the PG::BasicTypeRegistry, which is based on database OIDs.
343
+ * It's more convenient to use the PG::BasicTypeRegistry, which is based on database OIDs.
343
344
  * # Fetch a NULL record of our type to retrieve the OIDs of the two fields "r" and "i"
344
345
  * oids = conn.exec( "SELECT (NULL::complex).*" )
345
346
  * # Build a type map (PG::TypeMapByColumn) for decoding the "complex" type