pg 1.4.5-x86-mingw32 → 1.5.0-x86-mingw32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (75) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data/.appveyor.yml +15 -9
  4. data/.github/workflows/binary-gems.yml +41 -10
  5. data/.github/workflows/source-gem.yml +18 -12
  6. data/.gitignore +11 -2
  7. data/.travis.yml +2 -2
  8. data/{History.rdoc → History.md} +223 -153
  9. data/README.ja.md +276 -0
  10. data/README.md +286 -0
  11. data/Rakefile +12 -3
  12. data/Rakefile.cross +7 -11
  13. data/certs/larskanis-2023.pem +24 -0
  14. data/ext/pg.c +10 -28
  15. data/ext/pg.h +10 -5
  16. data/ext/pg_binary_decoder.c +79 -0
  17. data/ext/pg_binary_encoder.c +224 -0
  18. data/ext/pg_coder.c +16 -7
  19. data/ext/pg_connection.c +156 -60
  20. data/ext/pg_copy_coder.c +306 -17
  21. data/ext/pg_record_coder.c +5 -4
  22. data/ext/pg_result.c +88 -17
  23. data/ext/pg_text_decoder.c +28 -10
  24. data/ext/pg_text_encoder.c +22 -9
  25. data/ext/pg_tuple.c +34 -31
  26. data/ext/pg_type_map.c +3 -2
  27. data/ext/pg_type_map_all_strings.c +2 -2
  28. data/ext/pg_type_map_by_class.c +5 -3
  29. data/ext/pg_type_map_by_column.c +9 -3
  30. data/ext/pg_type_map_by_oid.c +7 -4
  31. data/ext/pg_type_map_in_ruby.c +5 -2
  32. data/lib/2.5/pg_ext.so +0 -0
  33. data/lib/2.6/pg_ext.so +0 -0
  34. data/lib/2.7/pg_ext.so +0 -0
  35. data/lib/3.0/pg_ext.so +0 -0
  36. data/lib/3.1/pg_ext.so +0 -0
  37. data/lib/3.2/pg_ext.so +0 -0
  38. data/lib/pg/basic_type_map_based_on_result.rb +21 -1
  39. data/lib/pg/basic_type_map_for_queries.rb +13 -8
  40. data/lib/pg/basic_type_map_for_results.rb +26 -3
  41. data/lib/pg/basic_type_registry.rb +30 -32
  42. data/lib/pg/binary_decoder/date.rb +9 -0
  43. data/lib/pg/binary_decoder/timestamp.rb +26 -0
  44. data/lib/pg/binary_encoder/timestamp.rb +20 -0
  45. data/lib/pg/coder.rb +15 -13
  46. data/lib/pg/connection.rb +82 -30
  47. data/lib/pg/exceptions.rb +7 -0
  48. data/lib/pg/text_decoder/date.rb +18 -0
  49. data/lib/pg/text_decoder/inet.rb +9 -0
  50. data/lib/pg/text_decoder/json.rb +14 -0
  51. data/lib/pg/text_decoder/numeric.rb +9 -0
  52. data/lib/pg/text_decoder/timestamp.rb +30 -0
  53. data/lib/pg/text_encoder/date.rb +12 -0
  54. data/lib/pg/text_encoder/inet.rb +28 -0
  55. data/lib/pg/text_encoder/json.rb +14 -0
  56. data/lib/pg/text_encoder/numeric.rb +9 -0
  57. data/lib/pg/text_encoder/timestamp.rb +24 -0
  58. data/lib/pg/version.rb +1 -1
  59. data/lib/pg.rb +44 -15
  60. data/lib/x86-mingw32/libpq.dll +0 -0
  61. data/pg.gemspec +4 -2
  62. data/rakelib/task_extension.rb +1 -1
  63. data/translation/.po4a-version +7 -0
  64. data/translation/po/all.pot +910 -0
  65. data/translation/po/ja.po +1047 -0
  66. data/translation/po4a.cfg +12 -0
  67. data.tar.gz.sig +0 -0
  68. metadata +111 -35
  69. metadata.gz.sig +0 -0
  70. data/README.ja.rdoc +0 -13
  71. data/README.rdoc +0 -233
  72. data/lib/pg/binary_decoder.rb +0 -23
  73. data/lib/pg/constants.rb +0 -12
  74. data/lib/pg/text_decoder.rb +0 -46
  75. 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
  }
@@ -310,6 +317,123 @@ pg_text_enc_copy_row(t_pg_coder *conv, VALUE value, char *out, VALUE *intermedia
310
317
  }
311
318
 
312
319
 
320
+ /*
321
+ * Document-class: PG::BinaryEncoder::CopyRow < PG::CopyEncoder
322
+ *
323
+ * This class encodes one row of arbitrary columns for transmission as COPY data in binary format.
324
+ * See the {COPY command}[http://www.postgresql.org/docs/current/static/sql-copy.html]
325
+ * for description of the format.
326
+ *
327
+ * It is intended to be used in conjunction with PG::Connection#put_copy_data .
328
+ *
329
+ * The columns are expected as Array of values. The single values are encoded as defined
330
+ * in the assigned #type_map. If no type_map was assigned, all values are converted to
331
+ * strings by PG::BinaryEncoder::String.
332
+ *
333
+ * Example with default type map ( TypeMapAllStrings ):
334
+ * conn.exec "create table my_table (a text,b int,c bool)"
335
+ * enco = PG::BinaryEncoder::CopyRow.new
336
+ * conn.copy_data "COPY my_table FROM STDIN WITH (FORMAT binary)", enco do
337
+ * conn.put_copy_data ["astring", "\x00\x00\x00\a", "\x00"]
338
+ * conn.put_copy_data ["string2", "\x00\x00\x00*", "\x01"]
339
+ * end
340
+ * This creates +my_table+ and inserts two rows with binary fields.
341
+ *
342
+ * The binary format is less portable and less readable than the text format.
343
+ * It is therefore recommended to either manually assign a type encoder for each column per PG::TypeMapByColumn,
344
+ * or to make use of PG::BasicTypeMapBasedOnResult to assign them based on the table OIDs.
345
+ *
346
+ * Manually assigning a type encoder works per type map like so:
347
+ *
348
+ * conn.exec "create table my_table (a text,b int,c bool)"
349
+ * tm = PG::TypeMapByColumn.new( [
350
+ * PG::BinaryEncoder::String.new,
351
+ * PG::BinaryEncoder::Int4.new,
352
+ * PG::BinaryEncoder::Boolean.new] )
353
+ * enco = PG::BinaryEncoder::CopyRow.new( type_map: tm )
354
+ * conn.copy_data "COPY my_table FROM STDIN WITH (FORMAT binary)", enco do
355
+ * conn.put_copy_data ["astring", 7, false]
356
+ * conn.put_copy_data ["string2", 42, true]
357
+ * end
358
+ *
359
+ * See also PG::BinaryDecoder::CopyRow for the decoding direction with
360
+ * PG::Connection#get_copy_data .
361
+ */
362
+ static int
363
+ pg_bin_enc_copy_row(t_pg_coder *conv, VALUE value, char *out, VALUE *intermediate, int enc_idx)
364
+ {
365
+ t_pg_copycoder *this = (t_pg_copycoder *)conv;
366
+ int i;
367
+ t_typemap *p_typemap;
368
+ char *current_out;
369
+ char *end_capa_ptr;
370
+
371
+ p_typemap = RTYPEDDATA_DATA( this->typemap );
372
+ p_typemap->funcs.fit_to_query( this->typemap, value );
373
+
374
+ /* Allocate a new string with embedded capacity and realloc exponential when needed. */
375
+ PG_RB_STR_NEW( *intermediate, current_out, end_capa_ptr );
376
+ PG_ENCODING_SET_NOCHECK(*intermediate, enc_idx);
377
+
378
+ /* 2 bytes for number of fields */
379
+ PG_RB_STR_ENSURE_CAPA( *intermediate, 2, current_out, end_capa_ptr );
380
+ write_nbo16(RARRAY_LEN(value), current_out);
381
+ current_out += 2;
382
+
383
+ for( i=0; i<RARRAY_LEN(value); i++){
384
+ int strlen;
385
+ VALUE subint;
386
+ VALUE entry;
387
+ t_pg_coder_enc_func enc_func;
388
+ static t_pg_coder *p_elem_coder;
389
+
390
+ entry = rb_ary_entry(value, i);
391
+
392
+ switch(TYPE(entry)){
393
+ case T_NIL:
394
+ /* 4 bytes for -1 indicationg a NULL value */
395
+ PG_RB_STR_ENSURE_CAPA( *intermediate, 4, current_out, end_capa_ptr );
396
+ write_nbo32(-1, current_out);
397
+ current_out += 4;
398
+ break;
399
+ default:
400
+ p_elem_coder = p_typemap->funcs.typecast_query_param(p_typemap, entry, i);
401
+ enc_func = pg_coder_enc_func(p_elem_coder);
402
+
403
+ /* 1st pass for retiving the required memory space */
404
+ strlen = enc_func(p_elem_coder, entry, NULL, &subint, enc_idx);
405
+
406
+ if( strlen == -1 ){
407
+ /* we can directly use String value in subint */
408
+ strlen = RSTRING_LENINT(subint);
409
+
410
+ PG_RB_STR_ENSURE_CAPA( *intermediate, 4 + strlen, current_out, end_capa_ptr );
411
+ /* 4 bytes length */
412
+ write_nbo32(strlen, current_out);
413
+ current_out += 4;
414
+
415
+ memcpy( current_out, RSTRING_PTR(subint), strlen );
416
+ current_out += strlen;
417
+ } else {
418
+ /* 2nd pass for writing the data to prepared buffer */
419
+ PG_RB_STR_ENSURE_CAPA( *intermediate, 4 + strlen, current_out, end_capa_ptr );
420
+ /* 4 bytes length */
421
+ write_nbo32(strlen, current_out);
422
+ current_out += 4;
423
+
424
+ /* Place the string at current output position. */
425
+ strlen = enc_func(p_elem_coder, entry, current_out, &subint, enc_idx);
426
+ current_out += strlen;
427
+ }
428
+ }
429
+ }
430
+
431
+ rb_str_set_len( *intermediate, current_out - RSTRING_PTR(*intermediate) );
432
+
433
+ return -1;
434
+ }
435
+
436
+
313
437
  /*
314
438
  * Return decimal value for a hexadecimal digit
315
439
  */
@@ -591,9 +715,168 @@ pg_text_dec_copy_row(t_pg_coder *conv, const char *input_line, int len, int _tup
591
715
  }
592
716
 
593
717
 
718
+ static const char BinarySignature[11] = "PGCOPY\n\377\r\n\0";
719
+
720
+ /*
721
+ * Document-class: PG::BinaryDecoder::CopyRow < PG::CopyDecoder
722
+ *
723
+ * This class decodes one row of arbitrary columns received as COPY data in binary format.
724
+ * See the {COPY command}[http://www.postgresql.org/docs/current/static/sql-copy.html]
725
+ * for description of the format.
726
+ *
727
+ * It is intended to be used in conjunction with PG::Connection#get_copy_data .
728
+ *
729
+ * The columns are retrieved as Array of values. The single values are decoded as defined
730
+ * in the assigned #type_map. If no type_map was assigned, all values are converted to
731
+ * strings by PG::BinaryDecoder::String.
732
+ *
733
+ * Example with default type map ( TypeMapAllStrings ):
734
+ * conn.exec("CREATE TABLE my_table AS VALUES('astring', 7, FALSE), ('string2', 42, TRUE) ")
735
+ *
736
+ * deco = PG::BinaryDecoder::CopyRow.new
737
+ * conn.copy_data "COPY my_table TO STDOUT WITH (FORMAT binary)", deco do
738
+ * while row=conn.get_copy_data
739
+ * p row
740
+ * end
741
+ * end
742
+ * This prints all rows of +my_table+ in binary format:
743
+ * ["astring", "\x00\x00\x00\a", "\x00"]
744
+ * ["string2", "\x00\x00\x00*", "\x01"]
745
+ *
746
+ * Example with column based type map:
747
+ * tm = PG::TypeMapByColumn.new( [
748
+ * PG::BinaryDecoder::String.new,
749
+ * PG::BinaryDecoder::Integer.new,
750
+ * PG::BinaryDecoder::Boolean.new] )
751
+ * deco = PG::BinaryDecoder::CopyRow.new( type_map: tm )
752
+ * conn.copy_data "COPY my_table TO STDOUT WITH (FORMAT binary)", deco do
753
+ * while row=conn.get_copy_data
754
+ * p row
755
+ * end
756
+ * end
757
+ * This prints the rows with type casted columns:
758
+ * ["astring", 7, false]
759
+ * ["string2", 42, true]
760
+ *
761
+ * Instead of manually assigning a type decoder for each column, PG::BasicTypeMapForResults
762
+ * can be used to assign them based on the table OIDs.
763
+ *
764
+ * See also PG::BinaryEncoder::CopyRow for the encoding direction with
765
+ * PG::Connection#put_copy_data .
766
+ */
767
+ static VALUE
768
+ pg_bin_dec_copy_row(t_pg_coder *conv, const char *input_line, int len, int _tuple, int _field, int enc_idx)
769
+ {
770
+ t_pg_copycoder *this = (t_pg_copycoder *)conv;
771
+
772
+ /* Return value: array */
773
+ VALUE array;
774
+
775
+ /* Current field */
776
+ VALUE field_str;
777
+
778
+ int nfields;
779
+ int expected_fields;
780
+ int fieldno;
781
+ char *output_ptr;
782
+ const char *cur_ptr;
783
+ const char *line_end_ptr;
784
+ char *end_capa_ptr;
785
+ t_typemap *p_typemap;
786
+
787
+ p_typemap = RTYPEDDATA_DATA( this->typemap );
788
+ expected_fields = p_typemap->funcs.fit_to_copy_get( this->typemap );
789
+
790
+ /* Allocate a new string with embedded capacity and realloc later with
791
+ * exponential growing size when needed. */
792
+ PG_RB_STR_NEW( field_str, output_ptr, end_capa_ptr );
793
+
794
+ /* set pointer variables for loop */
795
+ cur_ptr = input_line;
796
+ line_end_ptr = input_line + len;
797
+
798
+ if (cur_ptr + 11 <= line_end_ptr && memcmp(cur_ptr, BinarySignature, 11) == 0){
799
+ /* binary COPY header signature detected -> just drop it */
800
+ int ext_bytes;
801
+ cur_ptr += 11;
802
+
803
+ /* read flags */
804
+ if (cur_ptr + 4 > line_end_ptr) goto length_error;
805
+ cur_ptr += 4;
806
+
807
+ /* read header extensions */
808
+ if (cur_ptr + 4 > line_end_ptr) goto length_error;
809
+ ext_bytes = read_nbo32(cur_ptr);
810
+ if (ext_bytes < 0) goto length_error;
811
+ cur_ptr += 4;
812
+ if (cur_ptr + ext_bytes > line_end_ptr) goto length_error;
813
+ cur_ptr += ext_bytes;
814
+ }
815
+
816
+ /* read row header */
817
+ if (cur_ptr + 2 > line_end_ptr) goto length_error;
818
+ nfields = read_nbo16(cur_ptr);
819
+ cur_ptr += 2;
820
+
821
+ /* COPY data trailer? */
822
+ if (nfields < 0) {
823
+ if (nfields != -1) goto length_error;
824
+ array = Qnil;
825
+ } else {
826
+ array = rb_ary_new2(expected_fields);
827
+
828
+ for( fieldno = 0; fieldno < nfields; fieldno++){
829
+ long input_len;
830
+ VALUE field_value;
831
+
832
+ /* read field size */
833
+ if (cur_ptr + 4 > line_end_ptr) goto length_error;
834
+ input_len = read_nbo32(cur_ptr);
835
+ cur_ptr += 4;
836
+
837
+ if (input_len < 0) {
838
+ if (input_len != -1) goto length_error;
839
+ /* NULL indicator */
840
+ rb_ary_push(array, Qnil);
841
+ } else {
842
+ if (cur_ptr + input_len > line_end_ptr) goto length_error;
843
+
844
+ /* copy input data to field_str */
845
+ PG_RB_STR_ENSURE_CAPA( field_str, input_len, output_ptr, end_capa_ptr );
846
+ memcpy(output_ptr, cur_ptr, input_len);
847
+ cur_ptr += input_len;
848
+ output_ptr += input_len;
849
+ /* convert field_str through the type map */
850
+ rb_str_set_len( field_str, output_ptr - RSTRING_PTR(field_str) );
851
+ field_value = p_typemap->funcs.typecast_copy_get( p_typemap, field_str, fieldno, 1, enc_idx );
852
+
853
+ rb_ary_push(array, field_value);
854
+
855
+ if( field_value == field_str ){
856
+ /* Our output string will be send to the user, so we can not reuse
857
+ * it for the next field. */
858
+ PG_RB_STR_NEW( field_str, output_ptr, end_capa_ptr );
859
+ }
860
+ }
861
+
862
+ /* Reset the pointer to the start of the output/buffer string. */
863
+ output_ptr = RSTRING_PTR(field_str);
864
+ }
865
+ }
866
+
867
+ if (cur_ptr < line_end_ptr)
868
+ rb_raise( rb_eArgError, "trailing data after row data at position: %ld", (long)(cur_ptr - input_line) + 1 );
869
+
870
+ return array;
871
+
872
+ length_error:
873
+ rb_raise( rb_eArgError, "premature end of COPY data at position: %ld", (long)(cur_ptr - input_line) + 1 );
874
+ }
875
+
594
876
  void
595
877
  init_pg_copycoder(void)
596
878
  {
879
+ VALUE coder;
597
880
  /* Document-class: PG::CopyCoder < PG::Coder
598
881
  *
599
882
  * This is the base class for all type cast classes for COPY data,
@@ -616,13 +899,19 @@ init_pg_copycoder(void)
616
899
  /* Make RDoc aware of the encoder classes... */
617
900
  /* rb_mPG_TextEncoder = rb_define_module_under( rb_mPG, "TextEncoder" ); */
618
901
  /* 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 );
902
+ coder = pg_define_coder( "CopyRow", pg_text_enc_copy_row, rb_cPG_CopyEncoder, rb_mPG_TextEncoder );
903
+ rb_include_module( coder, rb_mPG_BinaryFormatting );
904
+ /* rb_mPG_BinaryEncoder = rb_define_module_under( rb_mPG, "BinaryEncoder" ); */
905
+ /* dummy = rb_define_class_under( rb_mPG_BinaryEncoder, "CopyRow", rb_cPG_CopyEncoder ); */
906
+ pg_define_coder( "CopyRow", pg_bin_enc_copy_row, rb_cPG_CopyEncoder, rb_mPG_BinaryEncoder );
621
907
 
622
908
  /* rb_mPG_TextDecoder = rb_define_module_under( rb_mPG, "TextDecoder" ); */
623
909
  /* 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 );
910
+ coder = pg_define_coder( "CopyRow", pg_text_dec_copy_row, rb_cPG_CopyDecoder, rb_mPG_TextDecoder );
625
911
  /* Although CopyRow is a text decoder, data can contain zero bytes and are not zero terminated.
626
912
  * They are handled like binaries. So format is set to 1 (binary). */
627
- rb_include_module( rb_cPG_CopyDecoder, rb_mPG_BinaryFormatting );
913
+ rb_include_module( coder, rb_mPG_BinaryFormatting );
914
+ /* rb_mPG_BinaryDecoder = rb_define_module_under( rb_mPG, "BinaryDecoder" ); */
915
+ /* dummy = rb_define_class_under( rb_mPG_BinaryDecoder, "CopyRow", rb_cPG_CopyDecoder ); */
916
+ pg_define_coder( "CopyRow", pg_bin_dec_copy_row, rb_cPG_CopyDecoder, rb_mPG_BinaryDecoder );
628
917
  }
@@ -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
  }
data/ext/pg_result.c CHANGED
@@ -183,7 +183,7 @@ static const rb_data_type_t pgresult_type = {
183
183
  pg_compact_callback(pgresult_gc_compact),
184
184
  },
185
185
  0, 0,
186
- RUBY_TYPED_FREE_IMMEDIATELY,
186
+ RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED | PG_RUBY_TYPED_FROZEN_SHAREABLE,
187
187
  };
188
188
 
189
189
  /* Needed by sequel_pg gem, do not delete */
@@ -208,14 +208,12 @@ pg_new_result2(PGresult *result, VALUE rb_pgconn)
208
208
 
209
209
  this = (t_pg_result *)xmalloc(sizeof(*this) + sizeof(*this->fnames) * nfields);
210
210
  this->pgresult = result;
211
- this->connection = rb_pgconn;
212
- this->typemap = pg_typemap_all_strings;
213
- this->p_typemap = RTYPEDDATA_DATA( this->typemap );
214
211
  this->nfields = -1;
215
212
  this->tuple_hash = Qnil;
216
213
  this->field_map = Qnil;
217
214
  this->flags = 0;
218
215
  self = TypedData_Wrap_Struct(rb_cPGresult, &pgresult_type, this);
216
+ RB_OBJ_WRITE(self, &this->connection, rb_pgconn);
219
217
 
220
218
  if( result ){
221
219
  t_pg_connection *p_conn = pg_get_connection(rb_pgconn);
@@ -224,10 +222,13 @@ pg_new_result2(PGresult *result, VALUE rb_pgconn)
224
222
  t_typemap *p_typemap = RTYPEDDATA_DATA(typemap);
225
223
 
226
224
  this->enc_idx = p_conn->enc_idx;
227
- this->typemap = p_typemap->funcs.fit_to_result( typemap, self );
225
+ typemap = p_typemap->funcs.fit_to_result( typemap, self );
226
+ RB_OBJ_WRITE(self, &this->typemap, typemap);
228
227
  this->p_typemap = RTYPEDDATA_DATA( this->typemap );
229
228
  this->flags = p_conn->flags;
230
229
  } else {
230
+ RB_OBJ_WRITE(self, &this->typemap, pg_typemap_all_strings);
231
+ this->p_typemap = RTYPEDDATA_DATA( this->typemap );
231
232
  this->enc_idx = rb_locale_encindex();
232
233
  }
233
234
 
@@ -374,10 +375,30 @@ VALUE
374
375
  pg_result_clear(VALUE self)
375
376
  {
376
377
  t_pg_result *this = pgresult_get_this(self);
378
+ rb_check_frozen(self);
377
379
  pgresult_clear( this );
378
380
  return Qnil;
379
381
  }
380
382
 
383
+ /*
384
+ * call-seq:
385
+ * res.freeze
386
+ *
387
+ * Freeze the PG::Result object and unlink the result from the related PG::Connection.
388
+ *
389
+ * A frozen PG::Result object doesn't allow any streaming and it can't be cleared.
390
+ * It also denies setting a type_map or field_name_type.
391
+ *
392
+ */
393
+ VALUE
394
+ pg_result_freeze(VALUE self)
395
+ {
396
+ t_pg_result *this = pgresult_get_this(self);
397
+
398
+ RB_OBJ_WRITE(self, &this->connection, Qnil);
399
+ return rb_call_super(0, NULL);
400
+ }
401
+
381
402
  /*
382
403
  * call-seq:
383
404
  * res.cleared? -> boolean
@@ -477,7 +498,8 @@ static void pgresult_init_fnames(VALUE self)
477
498
 
478
499
  for( i=0; i<nfields; i++ ){
479
500
  char *cfname = PQfname(this->pgresult, i);
480
- this->fnames[i] = pg_cstr_to_sym(cfname, this->flags, this->enc_idx);
501
+ VALUE fname = pg_cstr_to_sym(cfname, this->flags, this->enc_idx);
502
+ RB_OBJ_WRITE(self, &this->fnames[i], fname);
481
503
  this->nfields = i + 1;
482
504
  }
483
505
  this->nfields = nfields;
@@ -527,6 +549,8 @@ static void pgresult_init_fnames(VALUE self)
527
549
  * * +PGRES_SINGLE_TUPLE+
528
550
  * * +PGRES_PIPELINE_SYNC+
529
551
  * * +PGRES_PIPELINE_ABORTED+
552
+ *
553
+ * Use <tt>res.res_status</tt> to retrieve the string representation.
530
554
  */
531
555
  static VALUE
532
556
  pgresult_result_status(VALUE self)
@@ -536,16 +560,38 @@ pgresult_result_status(VALUE self)
536
560
 
537
561
  /*
538
562
  * call-seq:
539
- * res.res_status( status ) -> String
563
+ * PG::Result.res_status( status ) -> String
540
564
  *
541
565
  * Returns the string representation of +status+.
542
566
  *
543
567
  */
544
568
  static VALUE
545
- pgresult_res_status(VALUE self, VALUE status)
569
+ pgresult_s_res_status(VALUE self, VALUE status)
570
+ {
571
+ return rb_utf8_str_new_cstr(PQresStatus(NUM2INT(status)));
572
+ }
573
+
574
+ /*
575
+ * call-seq:
576
+ * res.res_status -> String
577
+ * res.res_status( status ) -> String
578
+ *
579
+ * Returns the string representation of the status of the result or of the provided +status+.
580
+ *
581
+ */
582
+ static VALUE
583
+ pgresult_res_status(int argc, VALUE *argv, VALUE self)
546
584
  {
547
585
  t_pg_result *this = pgresult_get_this_safe(self);
548
- VALUE ret = rb_str_new2(PQresStatus(NUM2INT(status)));
586
+ VALUE ret;
587
+
588
+ if( argc == 0 ){
589
+ ret = rb_str_new2(PQresStatus(PQresultStatus(this->pgresult)));
590
+ }else if( argc == 1 ){
591
+ ret = rb_str_new2(PQresStatus(NUM2INT(argv[0])));
592
+ }else{
593
+ rb_raise(rb_eArgError, "only 0 or 1 arguments expected");
594
+ }
549
595
  PG_ENCODING_SET_NOCHECK(ret, this->enc_idx);
550
596
  return ret;
551
597
  }
@@ -685,6 +731,21 @@ pgresult_nfields(VALUE self)
685
731
  return INT2NUM(PQnfields(pgresult_get(self)));
686
732
  }
687
733
 
734
+ /*
735
+ * call-seq:
736
+ * res.binary_tuples() -> Integer
737
+ *
738
+ * Returns 1 if the PGresult contains binary data and 0 if it contains text data.
739
+ *
740
+ * This function is deprecated (except for its use in connection with COPY), because it is possible for a single PGresult to contain text data in some columns and binary data in others.
741
+ * Result#fformat is preferred. binary_tuples returns 1 only if all columns of the result are binary (format 1).
742
+ */
743
+ static VALUE
744
+ pgresult_binary_tuples(VALUE self)
745
+ {
746
+ return INT2NUM(PQbinaryTuples(pgresult_get(self)));
747
+ }
748
+
688
749
  /*
689
750
  * call-seq:
690
751
  * res.fname( index ) -> String or Symbol
@@ -1087,7 +1148,7 @@ pgresult_aref(VALUE self, VALUE index)
1087
1148
  }
1088
1149
  /* Store a copy of the filled hash for use at the next row. */
1089
1150
  if( num_tuples > 10 )
1090
- this->tuple_hash = rb_hash_dup(tuple);
1151
+ RB_OBJ_WRITE(self, &this->tuple_hash, rb_hash_dup(tuple));
1091
1152
 
1092
1153
  return tuple;
1093
1154
  }
@@ -1269,7 +1330,7 @@ static void ensure_init_for_tuple(VALUE self)
1269
1330
  rb_hash_aset(field_map, this->fnames[i], INT2FIX(i));
1270
1331
  }
1271
1332
  rb_obj_freeze(field_map);
1272
- this->field_map = field_map;
1333
+ RB_OBJ_WRITE(self, &this->field_map, field_map);
1273
1334
  }
1274
1335
  }
1275
1336
 
@@ -1357,10 +1418,11 @@ pgresult_type_map_set(VALUE self, VALUE typemap)
1357
1418
  t_pg_result *this = pgresult_get_this(self);
1358
1419
  t_typemap *p_typemap;
1359
1420
 
1421
+ rb_check_frozen(self);
1360
1422
  /* Check type of method param */
1361
1423
  TypedData_Get_Struct(typemap, t_typemap, &pg_typemap_type, p_typemap);
1362
1424
 
1363
- this->typemap = p_typemap->funcs.fit_to_result( typemap, self );
1425
+ RB_OBJ_WRITE(self, &this->typemap, p_typemap->funcs.fit_to_result( typemap, self ));
1364
1426
  this->p_typemap = RTYPEDDATA_DATA( this->typemap );
1365
1427
 
1366
1428
  return typemap;
@@ -1441,10 +1503,11 @@ VALUE
1441
1503
  pgresult_stream_any(VALUE self, int (*yielder)(VALUE, int, int, void*), void* data)
1442
1504
  {
1443
1505
  t_pg_result *this;
1444
- int nfields;
1506
+ int nfields, nfields2;
1445
1507
  PGconn *pgconn;
1446
1508
  PGresult *pgresult;
1447
1509
 
1510
+ rb_check_frozen(self);
1448
1511
  RETURN_ENUMERATOR(self, 0, NULL);
1449
1512
 
1450
1513
  this = pgresult_get_this_safe(self);
@@ -1467,6 +1530,12 @@ pgresult_stream_any(VALUE self, int (*yielder)(VALUE, int, int, void*), void* da
1467
1530
  pg_result_check( self );
1468
1531
  }
1469
1532
 
1533
+ nfields2 = PQnfields(pgresult);
1534
+ if( nfields != nfields2 ){
1535
+ pgresult_clear( this );
1536
+ rb_raise( rb_eInvalidChangeOfResultFields, "number of fields changed in single row mode from %d to %d - this is a sign for intersection with another query", nfields, nfields2);
1537
+ }
1538
+
1470
1539
  if( yielder( self, ntuples, nfields, data ) ){
1471
1540
  pgresult_clear( this );
1472
1541
  }
@@ -1480,9 +1549,6 @@ pgresult_stream_any(VALUE self, int (*yielder)(VALUE, int, int, void*), void* da
1480
1549
  if( pgresult == NULL )
1481
1550
  rb_raise( rb_eNoResultError, "no result received - possibly an intersection with another query");
1482
1551
 
1483
- if( nfields != PQnfields(pgresult) )
1484
- rb_raise( rb_eInvalidChangeOfResultFields, "number of fields changed in single row mode from %d to %d - this is a sign for intersection with another query", nfields, PQnfields(pgresult));
1485
-
1486
1552
  this->pgresult = pgresult;
1487
1553
  }
1488
1554
 
@@ -1586,6 +1652,8 @@ static VALUE
1586
1652
  pgresult_field_name_type_set(VALUE self, VALUE sym)
1587
1653
  {
1588
1654
  t_pg_result *this = pgresult_get_this(self);
1655
+
1656
+ rb_check_frozen(self);
1589
1657
  if( this->nfields != -1 ) rb_raise(rb_eArgError, "field names are already materialized");
1590
1658
 
1591
1659
  this->flags &= ~PG_RESULT_FIELD_NAMES_MASK;
@@ -1632,7 +1700,8 @@ init_pg_result(void)
1632
1700
 
1633
1701
  /****** PG::Result INSTANCE METHODS: libpq ******/
1634
1702
  rb_define_method(rb_cPGresult, "result_status", pgresult_result_status, 0);
1635
- rb_define_method(rb_cPGresult, "res_status", pgresult_res_status, 1);
1703
+ rb_define_method(rb_cPGresult, "res_status", pgresult_res_status, -1);
1704
+ rb_define_singleton_method(rb_cPGresult, "res_status", pgresult_s_res_status, 1);
1636
1705
  rb_define_method(rb_cPGresult, "error_message", pgresult_error_message, 0);
1637
1706
  rb_define_alias( rb_cPGresult, "result_error_message", "error_message");
1638
1707
  #ifdef HAVE_PQRESULTVERBOSEERRORMESSAGE
@@ -1642,12 +1711,14 @@ init_pg_result(void)
1642
1711
  rb_define_method(rb_cPGresult, "error_field", pgresult_error_field, 1);
1643
1712
  rb_define_alias( rb_cPGresult, "result_error_field", "error_field" );
1644
1713
  rb_define_method(rb_cPGresult, "clear", pg_result_clear, 0);
1714
+ rb_define_method(rb_cPGresult, "freeze", pg_result_freeze, 0 );
1645
1715
  rb_define_method(rb_cPGresult, "check", pg_result_check, 0);
1646
1716
  rb_define_alias (rb_cPGresult, "check_result", "check");
1647
1717
  rb_define_method(rb_cPGresult, "ntuples", pgresult_ntuples, 0);
1648
1718
  rb_define_alias(rb_cPGresult, "num_tuples", "ntuples");
1649
1719
  rb_define_method(rb_cPGresult, "nfields", pgresult_nfields, 0);
1650
1720
  rb_define_alias(rb_cPGresult, "num_fields", "nfields");
1721
+ rb_define_method(rb_cPGresult, "binary_tuples", pgresult_binary_tuples, 0);
1651
1722
  rb_define_method(rb_cPGresult, "fname", pgresult_fname, 1);
1652
1723
  rb_define_method(rb_cPGresult, "fnumber", pgresult_fnumber, 1);
1653
1724
  rb_define_method(rb_cPGresult, "ftable", pgresult_ftable, 1);