pg 1.7.0 → 1.7.1

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 (40) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data/CHANGELOG.md +16 -0
  4. data/Gemfile +17 -1
  5. data/README.md +1 -0
  6. data/Rakefile +8 -3
  7. data/duel/coder_duel.rb +105 -0
  8. data/duel/connect_duel.rb +26 -0
  9. data/duel/copy_duel.rb +59 -0
  10. data/duel/duel_helper.rb +47 -0
  11. data/duel/escape_duel.rb +31 -0
  12. data/duel/query_duel.rb +42 -0
  13. data/duel/result_duel.rb +64 -0
  14. data/duel/type_map_duel.rb +53 -0
  15. data/ext/errorcodes.def +4 -0
  16. data/ext/errorcodes.txt +2 -1
  17. data/ext/extconf.rb +10 -4
  18. data/ext/pg.c +53 -0
  19. data/ext/pg.h +6 -0
  20. data/ext/pg_binary_decoder.c +3 -4
  21. data/ext/pg_copy_coder.c +7 -3
  22. data/ext/pg_record_coder.c +5 -1
  23. data/ext/pg_text_decoder.c +113 -69
  24. data/ext/pg_text_encoder.c +6 -3
  25. data/ext/pg_tuple.c +17 -1
  26. data/ext/pg_type_map_by_class.c +1 -1
  27. data/ext/pg_type_map_by_column.c +1 -1
  28. data/ext/pg_util.c +8 -8
  29. data/lib/pg/connection.rb +3 -2
  30. data/lib/pg/text_decoder/date.rb +2 -14
  31. data/lib/pg/version.rb +1 -1
  32. data/ports/patches/postgresql/19.0/0001-Don-t-stop-build-due-to-atexit.patch +27 -0
  33. data/ports/patches/postgresql/19.0/0001-Fix-compatibility-to-old-MacOSX11.1.sdk-of-rake-comp.patch +31 -0
  34. data/ports/patches/postgresql/19.0/0001-PATCH-Don-t-use-workaround-of-__builtin_setjmp-on-MI.patch +27 -0
  35. data.tar.gz.sig +0 -0
  36. metadata +14 -4
  37. metadata.gz.sig +0 -0
  38. data/ports/patches/postgresql/18.6/0001-Use-workaround-of-__builtin_setjmp-only-on-MINGW-on-.patch +0 -42
  39. /data/ports/patches/krb5/{1.22.2 → 1.22.3}/0001-Allow-static-linking-krb5-library.patch +0 -0
  40. /data/ports/patches/krb5/{1.22.2 → 1.22.3}/0002-unknown-command-line-option-on-clang.patch +0 -0
data/ext/pg.c CHANGED
@@ -46,6 +46,7 @@
46
46
  */
47
47
 
48
48
  #include "pg.h"
49
+ #include "pg_util.h"
49
50
 
50
51
  int pg_skip_deprecation_warning;
51
52
  VALUE rb_mPG;
@@ -182,6 +183,58 @@ pg_get_rb_encoding_as_pg_encoding( rb_encoding *enc )
182
183
  return encname;
183
184
  }
184
185
 
186
+ /*
187
+ * Raise an error if encoding isn't safe to walk byte-wise.
188
+ *
189
+ * Safe encodings are single byte encodings or UTF-8, whose multibyte characters never contain ASCII bytes.
190
+ *
191
+ * Client encodings like SJIS, BIG5, GBK and GB18030 allow ASCII bytes (for instance 0x5C backslash) as the trail byte of a multibyte character.
192
+ * Such bytes must not be escaped or treated as special when generating or parsing SQL/COPY text, otherwise the escaping is shifted by one byte.
193
+ * For these reasons PostgreSQL doesn't allow them as server/database encoding, but it allows them as client/connection encodings.
194
+ */
195
+ void pg_validate_safe_ascii_scanning_encoding(int encidx) {
196
+ rb_encoding *enc;
197
+ const char *name;
198
+
199
+ /* Fast path for UTF-8, US-ASCII, and ASCII-8BIT (BINARY / SQL_ASCII) */
200
+ if (encidx == rb_utf8_encindex() ||
201
+ encidx == rb_usascii_encindex() ||
202
+ encidx == rb_ascii8bit_encindex()) {
203
+ return;
204
+ }
205
+
206
+ enc = rb_enc_from_index(encidx);
207
+ if (!enc) {
208
+ rb_raise(rb_eArgError, "Invalid encoding index: %d", encidx);
209
+ }
210
+
211
+ /* Single-byte ASCII-compatible encodings (e.g., LATIN1..10, WIN1250..1258, KOI8R/U) are safe */
212
+ if (rb_enc_asciicompat(enc) && rb_enc_mbmaxlen(enc) == 1) {
213
+ return;
214
+ }
215
+
216
+ /*
217
+ * Allow only PostgreSQL-supported EUC multibyte encodings whose trailing bytes
218
+ * are guaranteed to be >= 0x80:
219
+ */
220
+ name = rb_enc_name(enc);
221
+ if (rbpg_strncasecmp(name, "EUC-JP", 7) == 0 ||
222
+ rbpg_strncasecmp(name, "EUC-JIS-2004", 13) == 0 ||
223
+ rbpg_strncasecmp(name, "EUC-KR", 7) == 0 ||
224
+ rbpg_strncasecmp(name, "EUC-TW", 7) == 0 ||
225
+ rbpg_strncasecmp(name, "GB2312", 7) == 0 ||
226
+ rbpg_strncasecmp(name, "EUC-CN", 7) == 0) {
227
+ return;
228
+ }
229
+
230
+ /* Reject all other multibyte encodings (SJIS, SHIFT_JIS_2004, GBK, GB18030, BIG5, UHC, JOHAB, etc.) */
231
+ rb_raise(
232
+ rb_eEncodingError,
233
+ "Unsupported encoding '%s': multibyte encodings that may contain ASCII byte values in trailing bytes are not allowed for CopyRow",
234
+ name
235
+ );
236
+ }
237
+
185
238
 
186
239
  /*
187
240
  * Ensures that the given string has enough capacity to take expand_len
data/ext/pg.h CHANGED
@@ -80,6 +80,11 @@ typedef long suseconds_t;
80
80
  #define rb_hash_new_capa(x) rb_hash_new()
81
81
  #endif
82
82
 
83
+ #ifndef HAVE_RB_LEN_T
84
+ typedef long rb_len_t;
85
+ typedef unsigned long rb_ulen_t;
86
+ #endif
87
+
83
88
  #define pg_gc_location(x) x = rb_gc_location(x)
84
89
 
85
90
  extern int ruby_native_thread_p(void);
@@ -371,6 +376,7 @@ pgresult_get_this( VALUE self )
371
376
  rb_encoding * pg_get_pg_encname_as_rb_encoding _(( const char * ));
372
377
  const char * pg_get_rb_encoding_as_pg_encoding _(( rb_encoding * ));
373
378
  rb_encoding *pg_conn_enc_get _(( PGconn * ));
379
+ void pg_validate_safe_ascii_scanning_encoding _(( int ));
374
380
 
375
381
  void notice_receiver_proxy(void *arg, const PGresult *result);
376
382
  void notice_processor_proxy(void *arg, const char *message);
@@ -4,7 +4,6 @@
4
4
  *
5
5
  */
6
6
 
7
- #include "ruby/version.h"
8
7
  #include "pg.h"
9
8
  #include "pg_util.h"
10
9
  #ifdef HAVE_INTTYPES_H
@@ -275,12 +274,12 @@ pg_bin_dec_array(t_pg_coder *conv, const char *input_line, int len, int tuple, i
275
274
  }
276
275
 
277
276
  if (cur_ptr < line_end_ptr)
278
- rb_raise( rb_eArgError, "trailing data after binary array data at position: %ld", (long)(cur_ptr - input_line) + 1 );
277
+ rb_raise( rb_eArgError, "trailing data after binary array data at position: %lld", (long long)(cur_ptr - input_line) + 1 );
279
278
 
280
279
  return arrays[0];
281
280
 
282
281
  length_error:
283
- rb_raise( rb_eArgError, "premature end of binary array data at position: %ld", (long)(cur_ptr - input_line) + 1 );
282
+ rb_raise( rb_eArgError, "premature end of binary array data at position: %lld", (long long)(cur_ptr - input_line) + 1 );
284
283
  }
285
284
 
286
285
  #define PG_INT64_MIN (-0x7FFFFFFFFFFFFFFFL - 1)
@@ -327,7 +326,7 @@ pg_bin_dec_timestamp(t_pg_coder *conv, const char *val, int len, int tuple, int
327
326
  sec = (timestamp / 1000000) + 10957L * 24L * 3600L;
328
327
  nsec = (timestamp % 1000000) * 1000;
329
328
 
330
- #if (RUBY_API_VERSION_MAJOR > 2 || (RUBY_API_VERSION_MAJOR == 2 && RUBY_API_VERSION_MINOR >= 3)) && defined(NEGATIVE_TIME_T) && defined(SIZEOF_TIME_T) && SIZEOF_TIME_T >= 8
329
+ #if defined(NEGATIVE_TIME_T) && SIZEOF_TIME_T >= 8
331
330
  /* Fast path for time conversion */
332
331
  {
333
332
  struct timespec ts = {sec, nsec};
data/ext/pg_copy_coder.c CHANGED
@@ -226,6 +226,8 @@ pg_text_enc_copy_row(t_pg_coder *conv, VALUE value, char *out, VALUE *intermedia
226
226
  char *current_out;
227
227
  char *end_capa_ptr;
228
228
 
229
+ pg_validate_safe_ascii_scanning_encoding(enc_idx);
230
+
229
231
  p_typemap = RTYPEDDATA_DATA( rb_typemap );
230
232
  p_typemap->funcs.fit_to_query( rb_typemap, value );
231
233
 
@@ -541,6 +543,8 @@ pg_text_dec_copy_row(t_pg_coder *conv, const char *input_line, int len, int _tup
541
543
  VALUE rb_typemap = this->typemap;
542
544
  t_typemap *p_typemap;
543
545
 
546
+ pg_validate_safe_ascii_scanning_encoding(enc_idx);
547
+
544
548
  p_typemap = RTYPEDDATA_DATA( rb_typemap );
545
549
  expected_fields = p_typemap->funcs.fit_to_copy_get( rb_typemap );
546
550
 
@@ -692,7 +696,7 @@ pg_text_dec_copy_row(t_pg_coder *conv, const char *input_line, int len, int _tup
692
696
  }
693
697
 
694
698
  if (!found_delim && cur_ptr < line_end_ptr)
695
- rb_raise( rb_eArgError, "trailing data after linefeed at position: %ld", (long)(cur_ptr - input_line) + 1 );
699
+ rb_raise( rb_eArgError, "trailing data after linefeed at position: %lld", (long long)(cur_ptr - input_line) + 1 );
696
700
 
697
701
 
698
702
  /* Check whether raw input matched null marker */
@@ -881,13 +885,13 @@ pg_bin_dec_copy_row(t_pg_coder *conv, const char *input_line, int len, int _tupl
881
885
  }
882
886
 
883
887
  if (cur_ptr < line_end_ptr)
884
- rb_raise( rb_eArgError, "trailing data after row data at position: %ld", (long)(cur_ptr - input_line) + 1 );
888
+ rb_raise( rb_eArgError, "trailing data after row data at position: %lld", (long long)(cur_ptr - input_line) + 1 );
885
889
 
886
890
  RB_GC_GUARD(rb_typemap);
887
891
  return array;
888
892
 
889
893
  length_error:
890
- rb_raise( rb_eArgError, "premature end of COPY data at position: %ld", (long)(cur_ptr - input_line) + 1 );
894
+ rb_raise( rb_eArgError, "premature end of COPY data at position: %lld", (long long)(cur_ptr - input_line) + 1 );
891
895
  }
892
896
 
893
897
  void
@@ -186,6 +186,8 @@ pg_text_enc_record(t_pg_coder *conv, VALUE value, char *out, VALUE *intermediate
186
186
  char *current_out;
187
187
  char *end_capa_ptr;
188
188
 
189
+ pg_validate_safe_ascii_scanning_encoding(enc_idx);
190
+
189
191
  p_typemap = RTYPEDDATA_DATA( rb_typemap );
190
192
  p_typemap->funcs.fit_to_query( rb_typemap, value );
191
193
 
@@ -198,7 +200,7 @@ pg_text_enc_record(t_pg_coder *conv, VALUE value, char *out, VALUE *intermediate
198
200
  for( i=0; i<RARRAY_LEN(value); i++){
199
201
  char *ptr1;
200
202
  char *ptr2;
201
- long strlen;
203
+ rb_len_t strlen;
202
204
  int backslashes;
203
205
  VALUE subint;
204
206
  VALUE entry;
@@ -392,6 +394,8 @@ pg_text_dec_record(t_pg_coder *conv, char *input_line, int len, int _tuple, int
392
394
  VALUE rb_typemap = this->typemap;
393
395
  t_typemap *p_typemap;
394
396
 
397
+ pg_validate_safe_ascii_scanning_encoding(enc_idx);
398
+
395
399
  p_typemap = RTYPEDDATA_DATA( rb_typemap );
396
400
  expected_fields = p_typemap->funcs.fit_to_copy_get( rb_typemap );
397
401
 
@@ -28,13 +28,11 @@
28
28
  *
29
29
  */
30
30
 
31
- #include "ruby/version.h"
32
31
  #include "pg.h"
33
32
  #include "pg_util.h"
34
33
  #ifdef HAVE_INTTYPES_H
35
34
  #include <inttypes.h>
36
35
  #endif
37
- #include <ctype.h>
38
36
  #include <time.h>
39
37
  #if !defined(_WIN32)
40
38
  #include <arpa/inet.h>
@@ -49,6 +47,8 @@ static ID s_id_utc;
49
47
  static ID s_id_getlocal;
50
48
  static ID s_id_BigDecimal;
51
49
 
50
+ static VALUE s_Date;
51
+ static VALUE s_Date_GREGORIAN; /* Date::GREGORIAN */
52
52
  static VALUE s_IPAddr;
53
53
  static VALUE s_vmasks4;
54
54
  static VALUE s_vmasks6;
@@ -103,7 +103,7 @@ pg_text_dec_string(t_pg_coder *conv, const char *val, int len, int tuple, int fi
103
103
  static VALUE
104
104
  pg_text_dec_integer(t_pg_coder *conv, const char *val, int len, int tuple, int field, int enc_idx)
105
105
  {
106
- long i;
106
+ rb_len_t i;
107
107
  int max_len;
108
108
 
109
109
  if( sizeof(i) >= 8 && FIXNUM_MAX >= 1000000000000000000LL ){
@@ -417,6 +417,8 @@ pg_text_dec_array(t_pg_coder *conv, const char *c_pg_array_string, int array_str
417
417
  VALUE ret;
418
418
  t_pg_composite_coder *this = (t_pg_composite_coder *)conv;
419
419
 
420
+ pg_validate_safe_ascii_scanning_encoding(enc_idx);
421
+
420
422
  /*
421
423
  * If the input string starts with dimension info, read and use that.
422
424
  * Otherwise, we require the input to be in curly-brace style, and we
@@ -612,7 +614,7 @@ static int parse_year(const char **str) {
612
614
  int i;
613
615
  const char * p = *str;
614
616
 
615
- for(i = 0; isdigit(*p) && i < 7; i++, p++) {
617
+ for(i = 0; rb_isdigit(*p) && i < 7; i++, p++) {
616
618
  year = 10 * year + char_to_digit(*p);
617
619
  }
618
620
 
@@ -620,6 +622,25 @@ static int parse_year(const char **str) {
620
622
  return year;
621
623
  }
622
624
 
625
+ /*
626
+ * Seconds since 1970-01-01 00:00:00 UTC in the proleptic Gregorian calendar, like timegm() but much faster on macOS.
627
+ * Days are counted per http://howardhinnant.github.io/date_algorithms.html#days_from_civil
628
+ */
629
+ static int64_t seconds_since_epoch(int year, int mon, int day, int hour, int min, int sec)
630
+ {
631
+ int64_t era, days;
632
+ int year_of_era, day_of_year, day_of_era;
633
+
634
+ year -= mon <= 2;
635
+ era = (year >= 0 ? year : year - 399) / 400;
636
+ year_of_era = (int)(year - era * 400);
637
+ day_of_year = (153 * (mon > 2 ? mon - 3 : mon + 9) + 2) / 5 + day - 1;
638
+ day_of_era = year_of_era * 365 + year_of_era / 4 - year_of_era / 100 + day_of_year;
639
+ days = era * 146097 + day_of_era - 719468;
640
+
641
+ return days * 86400 + hour * 3600 + min * 60 + sec;
642
+ }
643
+
623
644
  #define TZ_NEG 1
624
645
  #define TZ_POS 2
625
646
 
@@ -654,11 +675,11 @@ static VALUE pg_text_dec_timestamp(t_pg_coder *conv, const char *val, int len, i
654
675
  year = parse_year(&str);
655
676
 
656
677
  if ( year > 0
657
- && str[0] == '-' && isdigit(str[1]) && isdigit(str[2])
658
- && str[3] == '-' && isdigit(str[4]) && isdigit(str[5])
659
- && str[6] == ' ' && isdigit(str[7]) && isdigit(str[8])
660
- && str[9] == ':' && isdigit(str[10]) && isdigit(str[11])
661
- && str[12] == ':' && isdigit(str[13]) && isdigit(str[14])
678
+ && str[0] == '-' && rb_isdigit(str[1]) && rb_isdigit(str[2])
679
+ && str[3] == '-' && rb_isdigit(str[4]) && rb_isdigit(str[5])
680
+ && str[6] == ' ' && rb_isdigit(str[7]) && rb_isdigit(str[8])
681
+ && str[9] == ':' && rb_isdigit(str[10]) && rb_isdigit(str[11])
682
+ && str[12] == ':' && rb_isdigit(str[13]) && rb_isdigit(str[14])
662
683
  ) {
663
684
 
664
685
  mon = str2_to_int(str+1); str += 3;
@@ -667,7 +688,7 @@ static VALUE pg_text_dec_timestamp(t_pg_coder *conv, const char *val, int len, i
667
688
  min = str2_to_int(str+1); str += 3;
668
689
  sec = str2_to_int(str+1); str += 3;
669
690
 
670
- if (str[0] == '.' && isdigit(str[1])) {
691
+ if (str[0] == '.' && rb_isdigit(str[1])) {
671
692
  /* nano second part, up to 9 digits */
672
693
  static const int coef[9] = {
673
694
  100000000, 10000000, 1000000,
@@ -676,23 +697,23 @@ static VALUE pg_text_dec_timestamp(t_pg_coder *conv, const char *val, int len, i
676
697
  int i;
677
698
 
678
699
  str++;
679
- for (i = 0; i < 9 && isdigit(*str); i++)
700
+ for (i = 0; i < 9 && rb_isdigit(*str); i++)
680
701
  {
681
702
  nsec += coef[i] * char_to_digit(*str++);
682
703
  }
683
704
  /* consume digits smaller than nsec */
684
- while(isdigit(*str)) str++;
705
+ while(rb_isdigit(*str)) str++;
685
706
  }
686
707
 
687
- if ((str[0] == '+' || str[0] == '-') && isdigit(str[1]) && isdigit(str[2])) {
708
+ if ((str[0] == '+' || str[0] == '-') && rb_isdigit(str[1]) && rb_isdigit(str[2])) {
688
709
  tz_given = str[0] == '-' ? TZ_NEG : TZ_POS;
689
710
  tz_hour = str2_to_int(str+1); str += 3;
690
711
 
691
- if (str[0] == ':' && isdigit(str[1]) && isdigit(str[2]))
712
+ if (str[0] == ':' && rb_isdigit(str[1]) && rb_isdigit(str[2]))
692
713
  {
693
714
  tz_min = str2_to_int(str+1); str += 3;
694
715
  }
695
- if (str[0] == ':' && isdigit(str[1]) && isdigit(str[2]))
716
+ if (str[0] == ':' && rb_isdigit(str[1]) && rb_isdigit(str[2]))
696
717
  {
697
718
  tz_sec = str2_to_int(str+1); str += 3;
698
719
  }
@@ -707,71 +728,48 @@ static VALUE pg_text_dec_timestamp(t_pg_coder *conv, const char *val, int len, i
707
728
  VALUE sec_value;
708
729
  VALUE gmt_offset_value;
709
730
  VALUE res;
710
-
711
- #if (RUBY_API_VERSION_MAJOR > 2 || (RUBY_API_VERSION_MAJOR == 2 && RUBY_API_VERSION_MINOR >= 3)) && defined(HAVE_TIMEGM)
712
- /* Fast path for time conversion */
713
- struct tm tm;
714
731
  struct timespec ts;
715
- tm.tm_year = year - 1900;
716
- tm.tm_mon = mon - 1;
717
- tm.tm_mday = day;
718
- tm.tm_hour = hour;
719
- tm.tm_min = min;
720
- tm.tm_sec = sec;
721
- tm.tm_isdst = -1;
732
+ int gmt_offset = tz_hour * 3600 + tz_min * 60 + tz_sec;
722
733
 
723
- if (tz_given) {
724
- /* with timezone */
725
- time_t time = timegm(&tm);
726
- if (time != -1){
727
- int gmt_offset;
734
+ if (tz_given == TZ_NEG)
735
+ {
736
+ gmt_offset = - gmt_offset;
737
+ }
728
738
 
729
- gmt_offset = tz_hour * 3600 + tz_min * 60 + tz_sec;
730
- if (tz_given == TZ_NEG)
731
- {
732
- gmt_offset = - gmt_offset;
733
- }
734
- ts.tv_sec = time - gmt_offset;
735
- ts.tv_nsec = nsec;
736
- return rb_time_timespec_new(&ts, gmt_offset);
739
+ /* Fast path for time conversion */
740
+ ts.tv_nsec = nsec;
741
+ if (!tz_given && (conv->flags & PG_CODER_TIMESTAMP_DB_LOCAL)) {
742
+ /* without timezone, interpreted as local time */
743
+ struct tm tm;
744
+ tm.tm_year = year - 1900;
745
+ tm.tm_mon = mon - 1;
746
+ tm.tm_mday = day;
747
+ tm.tm_hour = hour;
748
+ tm.tm_min = min;
749
+ tm.tm_sec = sec;
750
+ tm.tm_isdst = -1;
751
+
752
+ ts.tv_sec = mktime(&tm);
753
+ if (ts.tv_sec != -1) {
754
+ return rb_time_timespec_new(&ts, conv->flags & PG_CODER_TIMESTAMP_APP_LOCAL ? INT_MAX : INT_MAX-1);
737
755
  }
738
756
  } else {
739
- /* without timezone */
740
- time_t time;
741
-
742
- if( conv->flags & PG_CODER_TIMESTAMP_DB_LOCAL ) {
743
- time = mktime(&tm);
744
- } else {
745
- time = timegm(&tm);
746
- }
747
- if (time != -1){
748
- ts.tv_sec = time;
749
- ts.tv_nsec = nsec;
750
- return rb_time_timespec_new(&ts, conv->flags & PG_CODER_TIMESTAMP_APP_LOCAL ? INT_MAX : INT_MAX-1);
757
+ /* with timezone, or without timezone interpreted as UTC */
758
+ int64_t time = seconds_since_epoch(year, mon, day, hour, min, sec) - gmt_offset;
759
+ ts.tv_sec = (time_t)time;
760
+ if (ts.tv_sec == time) {
761
+ return rb_time_timespec_new(&ts, tz_given ? gmt_offset : conv->flags & PG_CODER_TIMESTAMP_APP_LOCAL ? INT_MAX : INT_MAX-1);
751
762
  }
752
763
  }
753
- /* Some libc implementations fail to convert certain values,
764
+ /* mktime() fails on some values with some libc implementations,
765
+ * and a 32 bit time_t can't hold every PostgreSQL timestamp,
754
766
  * so that we fall through to the slow path.
755
767
  */
756
- #endif
757
- if (nsec) {
758
- int sec_numerator = sec * 1000000 + nsec / 1000;
759
- int sec_denominator = 1000000;
760
- sec_value = rb_funcall(Qnil, s_id_Rational, 2,
761
- INT2NUM(sec_numerator), INT2NUM(sec_denominator));
762
- } else {
763
- sec_value = INT2NUM(sec);
764
- }
768
+ sec_value = rb_funcall(Qnil, s_id_Rational, 2,
769
+ LL2NUM((long long)sec * 1000000000 + nsec), INT2NUM(1000000000));
765
770
 
766
771
  if (tz_given) {
767
772
  /* with timezone */
768
- int gmt_offset;
769
-
770
- gmt_offset = tz_hour * 3600 + tz_min * 60 + tz_sec;
771
- if (tz_given == TZ_NEG)
772
- {
773
- gmt_offset = - gmt_offset;
774
- }
775
773
  gmt_offset_value = INT2NUM(gmt_offset);
776
774
  } else {
777
775
  /* without timezone */
@@ -827,7 +825,7 @@ pg_text_dec_inet(t_pg_coder *conv, const char *val, int len, int tuple, int fiel
827
825
  VALUE vmasks;
828
826
  char dst[16];
829
827
  char buf[64];
830
- int af = strchr(val, '.') ? AF_INET : AF_INET6;
828
+ int af = strchr(val, ':') ? AF_INET6 : AF_INET;
831
829
  int mask = -1;
832
830
 
833
831
  if (len >= 64) {
@@ -937,6 +935,51 @@ pg_text_dec_inet(t_pg_coder *conv, const char *val, int len, int tuple, int fiel
937
935
  return ip;
938
936
  }
939
937
 
938
+ /*
939
+ * Document-class: PG::TextDecoder::Date < PG::SimpleDecoder
940
+ *
941
+ * This is a decoder class for conversion of PostgreSQL date type to Ruby Date values.
942
+ *
943
+ * Dates are expected in ISO format (+YYYY-MM-DD+), which is PostgreSQL's default DateStyle.
944
+ * Other formats, BC dates, years beyond 9999 and the special values +infinity+ and +-infinity+
945
+ * are returned as String.
946
+ *
947
+ * As soon as this class is used, it requires the ruby standard library 'date'.
948
+ */
949
+ static VALUE
950
+ pg_text_dec_date(t_pg_coder *conv, const char *val, int len, int tuple, int field, int enc_idx)
951
+ {
952
+ if (len == 10
953
+ && rb_isdigit(val[0]) && rb_isdigit(val[1]) && rb_isdigit(val[2]) && rb_isdigit(val[3])
954
+ && val[4] == '-' && rb_isdigit(val[5]) && rb_isdigit(val[6])
955
+ && val[7] == '-' && rb_isdigit(val[8]) && rb_isdigit(val[9])
956
+ ) {
957
+ int year = str2_to_int(val) * 100 + str2_to_int(val + 2);
958
+ int mon = str2_to_int(val + 5);
959
+ int day = str2_to_int(val + 8);
960
+
961
+ return rb_funcall(s_Date, s_id_new, 4, INT2NUM(year), INT2NUM(mon), INT2NUM(day), s_Date_GREGORIAN);
962
+ }
963
+
964
+ return pg_text_dec_string(conv, val, len, tuple, field, enc_idx);
965
+ }
966
+
967
+ /* called per autoload when TextDecoder::Date is used */
968
+ static VALUE
969
+ init_pg_text_decoder_date(VALUE rb_mPG_TextDecoder)
970
+ {
971
+ rb_require("date");
972
+ rb_gc_register_address(&s_Date);
973
+ rb_gc_register_address(&s_Date_GREGORIAN);
974
+ s_Date = rb_const_get(rb_cObject, rb_intern("Date"));
975
+ s_Date_GREGORIAN = rb_const_get(s_Date, rb_intern("GREGORIAN"));
976
+
977
+ /* dummy = rb_define_class_under( rb_mPG_TextDecoder, "Date", rb_cPG_SimpleDecoder ); */
978
+ pg_define_coder( "Date", pg_text_dec_date, rb_cPG_SimpleDecoder, rb_mPG_TextDecoder );
979
+
980
+ return Qnil;
981
+ }
982
+
940
983
  /* called per autoload when TextDecoder::Inet is used */
941
984
  static VALUE
942
985
  init_pg_text_decoder_inet(VALUE rb_mPG_TextDecoder)
@@ -982,6 +1025,7 @@ init_pg_text_decoder(void)
982
1025
 
983
1026
  /* This module encapsulates all decoder classes with text input format */
984
1027
  rb_mPG_TextDecoder = rb_define_module_under( rb_mPG, "TextDecoder" );
1028
+ rb_define_private_method(rb_singleton_class(rb_mPG_TextDecoder), "init_date", init_pg_text_decoder_date, 0);
985
1029
  rb_define_private_method(rb_singleton_class(rb_mPG_TextDecoder), "init_inet", init_pg_text_decoder_inet, 0);
986
1030
  rb_define_private_method(rb_singleton_class(rb_mPG_TextDecoder), "init_numeric", init_pg_text_decoder_numeric, 0);
987
1031
 
@@ -430,7 +430,7 @@ pg_text_enc_bytea(t_pg_coder *conv, VALUE value, char *out, VALUE *intermediate,
430
430
  }
431
431
  return (int)(optr - out);
432
432
  }else{
433
- long size;
433
+ rb_len_t size;
434
434
  *intermediate = rb_obj_as_string(value);
435
435
  size = RSTRING_LEN(*intermediate);
436
436
 
@@ -610,6 +610,8 @@ pg_text_enc_array(t_pg_coder *conv, VALUE value, char *out, VALUE *intermediate,
610
610
  char *end_ptr;
611
611
  t_pg_composite_coder *this = (t_pg_composite_coder *)conv;
612
612
 
613
+ pg_validate_safe_ascii_scanning_encoding(enc_idx);
614
+
613
615
  if( TYPE(value) == T_ARRAY){
614
616
  VALUE out_str = rb_str_new(NULL, 0);
615
617
  PG_ENCODING_SET_NOCHECK(out_str, enc_idx);
@@ -653,8 +655,8 @@ quote_identifier( VALUE value, VALUE out_string, char *current_out ){
653
655
  static char *
654
656
  pg_text_enc_array_identifier(VALUE value, VALUE string, char *out, int enc_idx)
655
657
  {
656
- long i;
657
- long nr_elems;
658
+ rb_len_t i;
659
+ rb_len_t nr_elems;
658
660
 
659
661
  Check_Type(value, T_ARRAY);
660
662
  nr_elems = RARRAY_LEN(value);
@@ -763,6 +765,7 @@ pg_text_enc_quoted_literal(t_pg_coder *conv, VALUE value, char *out, VALUE *inte
763
765
  {
764
766
  t_pg_composite_coder *this = (t_pg_composite_coder *)conv;
765
767
  VALUE out_str = rb_str_new(NULL, 0);
768
+ pg_validate_safe_ascii_scanning_encoding(enc_idx);
766
769
  PG_ENCODING_SET_NOCHECK(out_str, enc_idx);
767
770
 
768
771
  out = RSTRING_PTR(out_str);
data/ext/pg_tuple.c CHANGED
@@ -253,7 +253,7 @@ static VALUE
253
253
  pg_tuple_fetch(int argc, VALUE *argv, VALUE self)
254
254
  {
255
255
  VALUE key;
256
- long block_given;
256
+ int block_given;
257
257
  VALUE index;
258
258
  int field_num;
259
259
  t_pg_tuple *this = pg_tuple_get_this(self);
@@ -547,14 +547,30 @@ pg_tuple_load(VALUE self, VALUE a)
547
547
  return self;
548
548
  }
549
549
 
550
+ /*
551
+ * call-seq:
552
+ * res.freeze
553
+ *
554
+ * Freeze the PG::Tuple object and materialize all fields, so that it can be safely shared across ractors.
555
+ *
556
+ */
557
+ static VALUE
558
+ pg_tuple_freeze(VALUE self)
559
+ {
560
+ pg_tuple_materialize(self);
561
+ return rb_call_super(0, NULL);
562
+ }
563
+
550
564
  void
551
565
  init_pg_tuple(void)
552
566
  {
553
567
  rb_cPG_Tuple = rb_define_class_under( rb_mPG, "Tuple", rb_cObject );
554
568
  rb_define_alloc_func( rb_cPG_Tuple, pg_tuple_s_allocate );
569
+ rb_undef_method(rb_singleton_class(rb_cPG_Tuple), "new");
555
570
  rb_include_module(rb_cPG_Tuple, rb_mEnumerable);
556
571
 
557
572
  rb_define_method(rb_cPG_Tuple, "fetch", pg_tuple_fetch, -1);
573
+ rb_define_method(rb_cPG_Tuple, "freeze", pg_tuple_freeze, 0 );
558
574
  rb_define_method(rb_cPG_Tuple, "[]", pg_tuple_aref, 1);
559
575
  rb_define_method(rb_cPG_Tuple, "each", pg_tuple_each, 0);
560
576
  rb_define_method(rb_cPG_Tuple, "each_value", pg_tuple_each_value, 0);
@@ -27,7 +27,7 @@ typedef struct {
27
27
  * We use 8 Bits of the klass object id as index to a 256 entry cache.
28
28
  * This avoids full lookups in most cases.
29
29
  */
30
- #define CACHE_LOOKUP(this, klass) ( &this->cache_row[(((unsigned long)klass) >> 8) & 0xff] )
30
+ #define CACHE_LOOKUP(this, klass) ( &this->cache_row[(((unsigned int)klass) >> 8) & 0xff] )
31
31
 
32
32
 
33
33
  static t_pg_coder *
@@ -263,7 +263,7 @@ pg_tmbc_allocate(void)
263
263
  static VALUE
264
264
  pg_tmbc_init(VALUE self, VALUE conv_ary)
265
265
  {
266
- long i;
266
+ int i;
267
267
  t_tmbc *this;
268
268
  int conv_ary_len;
269
269
 
data/ext/pg_util.c CHANGED
@@ -22,10 +22,10 @@ rbpg_base64_encode( char *out, const char *in, int len)
22
22
  int part_len = len % 3;
23
23
 
24
24
  if( part_len > 0 ){
25
- long byte2 = 0;
26
- long byte1 = part_len > 1 ? *--in_ptr : 0;
27
- long byte0 = *--in_ptr;
28
- long triple = (byte0 << 16) + (byte1 << 8) + byte2;
25
+ int byte2 = 0;
26
+ int byte1 = part_len > 1 ? *--in_ptr : 0;
27
+ int byte0 = *--in_ptr;
28
+ int triple = (byte0 << 16) + (byte1 << 8) + byte2;
29
29
 
30
30
  *--out_ptr = '=';
31
31
  *--out_ptr = part_len > 1 ? base64_encode_table[(triple >> 1 * 6) & 0x3F] : '=';
@@ -34,10 +34,10 @@ rbpg_base64_encode( char *out, const char *in, int len)
34
34
  }
35
35
 
36
36
  while( out_ptr > out ){
37
- long byte2 = *--in_ptr;
38
- long byte1 = *--in_ptr;
39
- long byte0 = *--in_ptr;
40
- long triple = (byte0 << 16) + (byte1 << 8) + byte2;
37
+ int byte2 = *--in_ptr;
38
+ int byte1 = *--in_ptr;
39
+ int byte0 = *--in_ptr;
40
+ int triple = (byte0 << 16) + (byte1 << 8) + byte2;
41
41
 
42
42
  *--out_ptr = base64_encode_table[(triple >> 0 * 6) & 0x3F];
43
43
  *--out_ptr = base64_encode_table[(triple >> 1 * 6) & 0x3F];
data/lib/pg/connection.rb CHANGED
@@ -810,7 +810,8 @@ class PG::Connection
810
810
  # connection to server on socket "/var/run/postgresql/.s.PGSQL.5433" failed: No such file or directory
811
811
  unless event
812
812
  connection_errors << (error_message + "timeout expired")
813
- if hostcnt > 0
813
+ time_left = !stop_time || Process.clock_gettime(Process::CLOCK_MONOTONIC) < stop_time
814
+ if hostcnt > 0 && time_left
814
815
  reset_start2(self.class.parse_connect_args(iopts))
815
816
  # Restart polling with waiting for writable.
816
817
  # Otherwise "not connected" error is raised on Windows.
@@ -848,7 +849,7 @@ class PG::Connection
848
849
 
849
850
  idx = (ihosts || ihostaddrs || iports).index.with_index do |_, i|
850
851
  (ihosts ? ihosts[i] == host : true) &&
851
- (ihostaddrs && respond_to?(:hostaddr, true) ? ihostaddrs[i] == hostaddr : true) &&
852
+ (ihostaddrs && respond_to?(:hostaddr, true) ? ihostaddrs[i].to_s.empty? || ihostaddrs[i] == hostaddr : true) &&
852
853
  (iports ? iports[i].to_i == port : true)
853
854
  end
854
855
 
@@ -1,21 +1,9 @@
1
1
  # -*- ruby -*-
2
2
  # frozen_string_literal: true
3
3
 
4
- require 'date'
5
-
6
4
  module PG
7
5
  module TextDecoder
8
- # This is a decoder class for conversion of PostgreSQL date type to Ruby Date values.
9
- #
10
- # As soon as this class is used, it requires the ruby standard library 'date'.
11
- class Date < SimpleDecoder
12
- def decode(string, tuple=nil, field=nil)
13
- if string =~ /\A(\d{4})-(\d\d)-(\d\d)\z/
14
- ::Date.new $1.to_i, $2.to_i, $3.to_i, ::Date::GREGORIAN
15
- else
16
- string
17
- end
18
- end
19
- end
6
+ # Init C part of the decoder
7
+ init_date
20
8
  end
21
9
  end # module PG
data/lib/pg/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
  module PG
3
3
  # Library version
4
- VERSION = '1.7.0'
4
+ VERSION = '1.7.1'
5
5
  end