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.h CHANGED
@@ -11,6 +11,7 @@
11
11
  #include <sys/types.h>
12
12
  #if !defined(_WIN32)
13
13
  # include <sys/time.h>
14
+ # include <sys/socket.h>
14
15
  #endif
15
16
  #if defined(HAVE_UNISTD_H) && !defined(_WIN32)
16
17
  # include <unistd.h>
@@ -18,89 +19,18 @@
18
19
 
19
20
  /* Ruby headers */
20
21
  #include "ruby.h"
21
- #ifdef HAVE_RUBY_ST_H
22
- # include "ruby/st.h"
23
- #elif HAVE_ST_H
24
- # include "st.h"
25
- #endif
22
+ #include "ruby/st.h"
23
+ #include "ruby/encoding.h"
26
24
 
27
- #if defined(HAVE_RUBY_ENCODING_H) && HAVE_RUBY_ENCODING_H
28
- # include "ruby/encoding.h"
29
- # define M17N_SUPPORTED
30
- # ifdef HAVE_RB_ENCDB_ALIAS
31
- extern int rb_encdb_alias(const char *, const char *);
32
- # define ENC_ALIAS(name, orig) rb_encdb_alias((name), (orig))
33
- # elif HAVE_RB_ENC_ALIAS
34
- extern int rb_enc_alias(const char *, const char *);
35
- # define ENC_ALIAS(name, orig) rb_enc_alias((name), (orig))
36
- # else
37
- extern int rb_enc_alias(const char *alias, const char *orig); /* declaration missing in Ruby 1.9.1 */
38
- # define ENC_ALIAS(name, orig) rb_enc_alias((name), (orig))
39
- # endif
40
-
41
-
42
- # if !defined(ENCODING_SET_INLINED)
43
- /* Rubinius doesn't define ENCODING_SET_INLINED, so we fall back to the more
44
- * portable version.
45
- */
46
- # define PG_ENCODING_SET_NOCHECK(obj,i) \
47
- do { \
48
- rb_enc_set_index((obj), (i)); \
49
- } while(0)
50
- # else
51
- # define PG_ENCODING_SET_NOCHECK(obj,i) \
25
+ #define PG_ENCODING_SET_NOCHECK(obj,i) \
52
26
  do { \
53
27
  if ((i) < ENCODING_INLINE_MAX) \
54
28
  ENCODING_SET_INLINED((obj), (i)); \
55
29
  else \
56
30
  rb_enc_set_index((obj), (i)); \
57
31
  } while(0)
58
- # endif
59
-
60
- #else
61
- # define PG_ENCODING_SET_NOCHECK(obj,i) /* nothing */
62
- #endif
63
-
64
- #if RUBY_VM != 1
65
- # define RUBY_18_COMPAT
66
- #endif
67
-
68
- #ifndef RARRAY_LEN
69
- # define RARRAY_LEN(x) RARRAY((x))->len
70
- #endif /* RARRAY_LEN */
71
-
72
- #ifndef RSTRING_LEN
73
- # define RSTRING_LEN(x) RSTRING((x))->len
74
- #endif /* RSTRING_LEN */
75
-
76
- #ifndef RSTRING_PTR
77
- # define RSTRING_PTR(x) RSTRING((x))->ptr
78
- #endif /* RSTRING_PTR */
79
32
 
80
- #ifndef StringValuePtr
81
- # define StringValuePtr(x) STR2CSTR(x)
82
- #endif /* StringValuePtr */
83
-
84
- #ifdef RUBY_18_COMPAT
85
- # define rb_io_stdio_file GetWriteFile
86
- # include "rubyio.h"
87
- #else
88
- # include "ruby/io.h"
89
- #endif
90
-
91
- #ifdef RUBINIUS
92
- /* Workaround for wrong FIXNUM_MAX definition */
93
- typedef intptr_t native_int;
94
- #endif
95
-
96
- #ifndef RETURN_SIZED_ENUMERATOR
97
- #define RETURN_SIZED_ENUMERATOR(obj, argc, argv, size_fn) RETURN_ENUMERATOR((obj), (argc), (argv))
98
- #endif
99
-
100
- #ifndef HAVE_RB_HASH_DUP
101
- /* Rubinius doesn't define rb_hash_dup() */
102
- #define rb_hash_dup(tuple) rb_funcall((tuple), rb_intern("dup"), 0)
103
- #endif
33
+ #include "ruby/io.h"
104
34
 
105
35
  #ifndef timeradd
106
36
  #define timeradd(a, b, result) \
@@ -145,6 +75,21 @@ typedef long suseconds_t;
145
75
  #define PG_MAX_COLUMNS 4000
146
76
  #endif
147
77
 
78
+ #ifndef RARRAY_AREF
79
+ #define RARRAY_AREF(a, i) (RARRAY_PTR(a)[i])
80
+ #endif
81
+
82
+ #ifdef HAVE_RB_GC_MARK_MOVABLE
83
+ #define pg_compact_callback(x) (x)
84
+ #define pg_gc_location(x) x = rb_gc_location(x)
85
+ #else
86
+ #define rb_gc_mark_movable(x) rb_gc_mark(x)
87
+ #define pg_compact_callback(x) {(x)}
88
+ #define pg_gc_location(x) UNUSED(x)
89
+ #endif
90
+
91
+ #define PG_ENC_IDX_BITS 28
92
+
148
93
  /* The data behind each PG::Connection object */
149
94
  typedef struct {
150
95
  PGconn *pgconn;
@@ -161,13 +106,21 @@ typedef struct {
161
106
  VALUE type_map_for_results;
162
107
  /* IO object internally used for the trace stream */
163
108
  VALUE trace_stream;
164
- /* Cached Encoding object */
165
- VALUE external_encoding;
166
109
  /* Kind of PG::Coder object for casting ruby values to COPY rows */
167
110
  VALUE encoder_for_put_copy_data;
168
111
  /* Kind of PG::Coder object for casting COPY rows to ruby values */
169
112
  VALUE decoder_for_get_copy_data;
113
+ /* Ruby encoding index of the client/internal encoding */
114
+ int enc_idx : PG_ENC_IDX_BITS;
115
+ /* flags controlling Symbol/String field names */
116
+ unsigned int flags : 2;
117
+ /* enable automatic flushing of send data at the end of send_query calls */
118
+ unsigned int flush_data : 1;
170
119
 
120
+ #if defined(_WIN32)
121
+ /* File descriptor to be used for rb_w32_unwrap_io_handle() */
122
+ int ruby_sd;
123
+ #endif
171
124
  } t_pg_connection;
172
125
 
173
126
  typedef struct pg_coder t_pg_coder;
@@ -188,20 +141,32 @@ typedef struct {
188
141
  */
189
142
  t_typemap *p_typemap;
190
143
 
144
+ /* Ruby encoding index of the client/internal encoding */
145
+ int enc_idx : PG_ENC_IDX_BITS;
146
+
191
147
  /* 0 = PGresult is cleared by PG::Result#clear or by the GC
192
148
  * 1 = PGresult is cleared internally by libpq
193
149
  */
194
- int autoclear;
150
+ unsigned int autoclear : 1;
151
+
152
+ /* flags controlling Symbol/String field names */
153
+ unsigned int flags : 2;
195
154
 
196
155
  /* Number of fields in fnames[] .
197
156
  * Set to -1 if fnames[] is not yet initialized.
198
157
  */
199
158
  int nfields;
200
159
 
160
+ /* Size of PGresult as published to ruby memory management. */
161
+ ssize_t result_size;
162
+
201
163
  /* Prefilled tuple Hash with fnames[] as keys. */
202
164
  VALUE tuple_hash;
203
165
 
204
- /* List of field names as frozen String objects.
166
+ /* Hash with fnames[] to field number mapping. */
167
+ VALUE field_map;
168
+
169
+ /* List of field names as frozen String or Symbol objects.
205
170
  * Only valid if nfields != -1
206
171
  */
207
172
  VALUE fnames[0];
@@ -209,7 +174,7 @@ typedef struct {
209
174
 
210
175
 
211
176
  typedef int (* t_pg_coder_enc_func)(t_pg_coder *, VALUE, char *, VALUE *, int);
212
- typedef VALUE (* t_pg_coder_dec_func)(t_pg_coder *, char *, int, int, int, int);
177
+ typedef VALUE (* t_pg_coder_dec_func)(t_pg_coder *, const char *, int, int, int, int);
213
178
  typedef VALUE (* t_pg_fit_to_result)(VALUE, VALUE);
214
179
  typedef VALUE (* t_pg_fit_to_query)(VALUE, VALUE);
215
180
  typedef int (* t_pg_fit_to_copy_get)(VALUE);
@@ -217,12 +182,27 @@ typedef VALUE (* t_pg_typecast_result)(t_typemap *, VALUE, int, int);
217
182
  typedef t_pg_coder *(* t_pg_typecast_query_param)(t_typemap *, VALUE, int);
218
183
  typedef VALUE (* t_pg_typecast_copy_get)( t_typemap *, VALUE, int, int, int );
219
184
 
185
+ #define PG_RESULT_FIELD_NAMES_MASK 0x03
186
+ #define PG_RESULT_FIELD_NAMES_SYMBOL 0x01
187
+ #define PG_RESULT_FIELD_NAMES_STATIC_SYMBOL 0x02
188
+
189
+ #define PG_CODER_TIMESTAMP_DB_UTC 0x0
190
+ #define PG_CODER_TIMESTAMP_DB_LOCAL 0x1
191
+ #define PG_CODER_TIMESTAMP_APP_UTC 0x0
192
+ #define PG_CODER_TIMESTAMP_APP_LOCAL 0x2
193
+ #define PG_CODER_FORMAT_ERROR_MASK 0xc
194
+ #define PG_CODER_FORMAT_ERROR_TO_RAISE 0x4
195
+ #define PG_CODER_FORMAT_ERROR_TO_STRING 0x8
196
+ #define PG_CODER_FORMAT_ERROR_TO_PARTIAL 0xc
197
+
220
198
  struct pg_coder {
221
199
  t_pg_coder_enc_func enc_func;
222
200
  t_pg_coder_dec_func dec_func;
223
201
  VALUE coder_obj;
224
202
  Oid oid;
225
203
  int format;
204
+ /* OR-ed values out of PG_CODER_* */
205
+ int flags;
226
206
  };
227
207
 
228
208
  typedef struct {
@@ -252,6 +232,8 @@ typedef struct {
252
232
  } convs[0];
253
233
  } t_tmbc;
254
234
 
235
+ extern const rb_data_type_t pg_typemap_type;
236
+ extern const rb_data_type_t pg_coder_type;
255
237
 
256
238
  #include "gvl_wrappers.h"
257
239
 
@@ -259,6 +241,7 @@ typedef struct {
259
241
  * Globals
260
242
  **************************************************************************/
261
243
 
244
+ extern int pg_skip_deprecation_warning;
262
245
  extern VALUE rb_mPG;
263
246
  extern VALUE rb_ePGerror;
264
247
  extern VALUE rb_eServerError;
@@ -317,13 +300,15 @@ void init_pg_type_map_by_oid _(( void ));
317
300
  void init_pg_type_map_in_ruby _(( void ));
318
301
  void init_pg_coder _(( void ));
319
302
  void init_pg_copycoder _(( void ));
303
+ void init_pg_recordcoder _(( void ));
320
304
  void init_pg_text_encoder _(( void ));
321
305
  void init_pg_text_decoder _(( void ));
322
306
  void init_pg_binary_encoder _(( void ));
323
307
  void init_pg_binary_decoder _(( void ));
308
+ void init_pg_tuple _(( void ));
324
309
  VALUE lookup_error_class _(( const char * ));
325
- VALUE pg_bin_dec_bytea _(( t_pg_coder*, char *, int, int, int, int ));
326
- VALUE pg_text_dec_string _(( t_pg_coder*, char *, int, int, int, int ));
310
+ VALUE pg_bin_dec_bytea _(( t_pg_coder*, const char *, int, int, int, int ));
311
+ VALUE pg_text_dec_string _(( t_pg_coder*, const char *, int, int, int, int ));
327
312
  int pg_coder_enc_to_s _(( t_pg_coder*, VALUE, char *, VALUE *, int));
328
313
  int pg_text_enc_identifier _(( t_pg_coder*, VALUE, char *, VALUE *, int));
329
314
  t_pg_coder_enc_func pg_coder_enc_func _(( t_pg_coder* ));
@@ -333,6 +318,7 @@ VALUE pg_obj_to_i _(( VALUE ));
333
318
  VALUE pg_tmbc_allocate _(( void ));
334
319
  void pg_coder_init_encoder _(( VALUE ));
335
320
  void pg_coder_init_decoder _(( VALUE ));
321
+ void pg_coder_compact _(( void * ));
336
322
  char *pg_rb_str_ensure_capa _(( VALUE, long, char *, char ** ));
337
323
 
338
324
  #define PG_RB_STR_ENSURE_CAPA( str, expand_len, curr_ptr, end_ptr ) \
@@ -346,17 +332,15 @@ char *pg_rb_str_ensure_capa _(( VALUE, long, char *,
346
332
  (curr_ptr) = (end_ptr) = RSTRING_PTR(str) \
347
333
  )
348
334
 
349
- #define PG_RB_TAINTED_STR_NEW( str, curr_ptr, end_ptr ) ( \
350
- (str) = rb_tainted_str_new( NULL, 0 ), \
351
- (curr_ptr) = (end_ptr) = RSTRING_PTR(str) \
352
- )
353
-
354
335
  VALUE pg_typemap_fit_to_result _(( VALUE, VALUE ));
355
336
  VALUE pg_typemap_fit_to_query _(( VALUE, VALUE ));
356
337
  int pg_typemap_fit_to_copy_get _(( VALUE ));
357
338
  VALUE pg_typemap_result_value _(( t_typemap *, VALUE, int, int ));
358
339
  t_pg_coder *pg_typemap_typecast_query_param _(( t_typemap *, VALUE, int ));
359
340
  VALUE pg_typemap_typecast_copy_get _(( t_typemap *, VALUE, int, int, int ));
341
+ void pg_typemap_mark _(( void * ));
342
+ size_t pg_typemap_memsize _(( const void * ));
343
+ void pg_typemap_compact _(( void * ));
360
344
 
361
345
  PGconn *pg_get_pgconn _(( VALUE ));
362
346
  t_pg_connection *pg_get_connection _(( VALUE ));
@@ -366,6 +350,7 @@ VALUE pg_new_result_autoclear _(( PGresult *, VALUE ));
366
350
  PGresult* pgresult_get _(( VALUE ));
367
351
  VALUE pg_result_check _(( VALUE ));
368
352
  VALUE pg_result_clear _(( VALUE ));
353
+ VALUE pg_tuple_new _(( VALUE, int ));
369
354
 
370
355
  /*
371
356
  * Fetch the data pointer for the result object
@@ -373,23 +358,28 @@ VALUE pg_result_clear _(( VALUE ));
373
358
  static inline t_pg_result *
374
359
  pgresult_get_this( VALUE self )
375
360
  {
376
- t_pg_result *this = DATA_PTR(self);
377
-
378
- if( this == NULL )
379
- rb_raise(rb_ePGerror, "result has been cleared");
380
-
381
- return this;
361
+ return RTYPEDDATA_DATA(self);
382
362
  }
383
363
 
384
364
 
385
- #ifdef M17N_SUPPORTED
386
365
  rb_encoding * pg_get_pg_encoding_as_rb_encoding _(( int ));
387
366
  rb_encoding * pg_get_pg_encname_as_rb_encoding _(( const char * ));
388
367
  const char * pg_get_rb_encoding_as_pg_encoding _(( rb_encoding * ));
389
368
  rb_encoding *pg_conn_enc_get _(( PGconn * ));
390
- #endif /* M17N_SUPPORTED */
391
369
 
392
370
  void notice_receiver_proxy(void *arg, const PGresult *result);
393
371
  void notice_processor_proxy(void *arg, const char *message);
394
372
 
373
+ /* reports if `-W' specified and PG_SKIP_DEPRECATION_WARNING environment variable isn't set
374
+ *
375
+ * message_id identifies the warning, so that it's reported only once.
376
+ */
377
+ #define pg_deprecated(message_id, format_args) \
378
+ do { \
379
+ if( !(pg_skip_deprecation_warning & (1 << message_id)) ){ \
380
+ pg_skip_deprecation_warning |= 1 << message_id; \
381
+ rb_warning format_args; \
382
+ } \
383
+ } while(0);
384
+
395
385
  #endif /* end __pg_h */
@@ -1,11 +1,12 @@
1
1
  /*
2
2
  * pg_column_map.c - PG::ColumnMap class extension
3
- * $Id: pg_binary_decoder.c,v fcf731d3dff7 2015/09/08 12:25:06 jfali $
3
+ * $Id$
4
4
  *
5
5
  */
6
6
 
7
+ #include "ruby/version.h"
7
8
  #include "pg.h"
8
- #include "util.h"
9
+ #include "pg_util.h"
9
10
  #ifdef HAVE_INTTYPES_H
10
11
  #include <inttypes.h>
11
12
  #endif
@@ -16,12 +17,12 @@ VALUE rb_mPG_BinaryDecoder;
16
17
  /*
17
18
  * Document-class: PG::BinaryDecoder::Boolean < PG::SimpleDecoder
18
19
  *
19
- * This is a decoder class for conversion of PostgreSQL binary bool type
20
- * to Ruby true or false objects.
20
+ * This is a decoder class for conversion of PostgreSQL binary +bool+ type
21
+ * to Ruby +true+ or +false+ objects.
21
22
  *
22
23
  */
23
24
  static VALUE
24
- pg_bin_dec_boolean(t_pg_coder *conv, char *val, int len, int tuple, int field, int enc_idx)
25
+ pg_bin_dec_boolean(t_pg_coder *conv, const char *val, int len, int tuple, int field, int enc_idx)
25
26
  {
26
27
  if (len < 1) {
27
28
  rb_raise( rb_eTypeError, "wrong data for binary boolean converter in tuple %d field %d", tuple, field);
@@ -32,12 +33,12 @@ pg_bin_dec_boolean(t_pg_coder *conv, char *val, int len, int tuple, int field, i
32
33
  /*
33
34
  * Document-class: PG::BinaryDecoder::Integer < PG::SimpleDecoder
34
35
  *
35
- * This is a decoder class for conversion of PostgreSQL binary int2, int4 and int8 types
36
+ * This is a decoder class for conversion of PostgreSQL binary +int2+, +int4+ and +int8+ types
36
37
  * to Ruby Integer objects.
37
38
  *
38
39
  */
39
40
  static VALUE
40
- pg_bin_dec_integer(t_pg_coder *conv, char *val, int len, int tuple, int field, int enc_idx)
41
+ pg_bin_dec_integer(t_pg_coder *conv, const char *val, int len, int tuple, int field, int enc_idx)
41
42
  {
42
43
  switch( len ){
43
44
  case 2:
@@ -54,12 +55,12 @@ pg_bin_dec_integer(t_pg_coder *conv, char *val, int len, int tuple, int field, i
54
55
  /*
55
56
  * Document-class: PG::BinaryDecoder::Float < PG::SimpleDecoder
56
57
  *
57
- * This is a decoder class for conversion of PostgreSQL binary float4 and float8 types
58
+ * This is a decoder class for conversion of PostgreSQL binary +float4+ and +float8+ types
58
59
  * to Ruby Float objects.
59
60
  *
60
61
  */
61
62
  static VALUE
62
- pg_bin_dec_float(t_pg_coder *conv, char *val, int len, int tuple, int field, int enc_idx)
63
+ pg_bin_dec_float(t_pg_coder *conv, const char *val, int len, int tuple, int field, int enc_idx)
63
64
  {
64
65
  union {
65
66
  float f;
@@ -86,15 +87,15 @@ pg_bin_dec_float(t_pg_coder *conv, char *val, int len, int tuple, int field, int
86
87
  * Document-class: PG::BinaryDecoder::Bytea < PG::SimpleDecoder
87
88
  *
88
89
  * This decoder class delivers the data received from the server as binary String object.
89
- * It is therefore suitable for conversion of PostgreSQL bytea data as well as any other
90
+ * It is therefore suitable for conversion of PostgreSQL +bytea+ data as well as any other
90
91
  * data in binary format.
91
92
  *
92
93
  */
93
94
  VALUE
94
- pg_bin_dec_bytea(t_pg_coder *conv, char *val, int len, int tuple, int field, int enc_idx)
95
+ pg_bin_dec_bytea(t_pg_coder *conv, const char *val, int len, int tuple, int field, int enc_idx)
95
96
  {
96
97
  VALUE ret;
97
- ret = rb_tainted_str_new( val, len );
98
+ ret = rb_str_new( val, len );
98
99
  PG_ENCODING_SET_NOCHECK( ret, rb_ascii8bit_encindex() );
99
100
  return ret;
100
101
  }
@@ -102,17 +103,17 @@ pg_bin_dec_bytea(t_pg_coder *conv, char *val, int len, int tuple, int field, int
102
103
  /*
103
104
  * Document-class: PG::BinaryDecoder::ToBase64 < PG::CompositeDecoder
104
105
  *
105
- * This is a decoder class for conversion of binary (bytea) to base64 data.
106
+ * This is a decoder class for conversion of binary +bytea+ to base64 data.
106
107
  *
107
108
  */
108
109
  static VALUE
109
- pg_bin_dec_to_base64(t_pg_coder *conv, char *val, int len, int tuple, int field, int enc_idx)
110
+ pg_bin_dec_to_base64(t_pg_coder *conv, const char *val, int len, int tuple, int field, int enc_idx)
110
111
  {
111
112
  t_pg_composite_coder *this = (t_pg_composite_coder *)conv;
112
113
  t_pg_coder_dec_func dec_func = pg_coder_dec_func(this->elem, this->comp.format);
113
114
  int encoded_len = BASE64_ENCODED_SIZE(len);
114
115
  /* create a buffer of the encoded length */
115
- VALUE out_value = rb_tainted_str_new(NULL, encoded_len);
116
+ VALUE out_value = rb_str_new(NULL, encoded_len);
116
117
 
117
118
  base64_encode( RSTRING_PTR(out_value), val, len );
118
119
 
@@ -130,6 +131,70 @@ pg_bin_dec_to_base64(t_pg_coder *conv, char *val, int len, int tuple, int field,
130
131
  return out_value;
131
132
  }
132
133
 
134
+ #define PG_INT64_MIN (-0x7FFFFFFFFFFFFFFFL - 1)
135
+ #define PG_INT64_MAX 0x7FFFFFFFFFFFFFFFL
136
+
137
+ /*
138
+ * Document-class: PG::BinaryDecoder::Timestamp < PG::SimpleDecoder
139
+ *
140
+ * This is a decoder class for conversion of PostgreSQL binary timestamps
141
+ * to Ruby Time objects.
142
+ *
143
+ * The following flags can be used to specify timezone interpretation:
144
+ * * +PG::Coder::TIMESTAMP_DB_UTC+ : Interpret timestamp as UTC time (default)
145
+ * * +PG::Coder::TIMESTAMP_DB_LOCAL+ : Interpret timestamp as local time
146
+ * * +PG::Coder::TIMESTAMP_APP_UTC+ : Return timestamp as UTC time (default)
147
+ * * +PG::Coder::TIMESTAMP_APP_LOCAL+ : Return timestamp as local time
148
+ *
149
+ * Example:
150
+ * deco = PG::BinaryDecoder::Timestamp.new(flags: PG::Coder::TIMESTAMP_DB_UTC | PG::Coder::TIMESTAMP_APP_LOCAL)
151
+ * deco.decode("\0"*8) # => 2000-01-01 01:00:00 +0100
152
+ */
153
+ static VALUE
154
+ pg_bin_dec_timestamp(t_pg_coder *conv, const char *val, int len, int tuple, int field, int enc_idx)
155
+ {
156
+ int64_t timestamp;
157
+ int64_t sec;
158
+ int64_t nsec;
159
+ VALUE t;
160
+
161
+ if( len != sizeof(timestamp) ){
162
+ rb_raise( rb_eTypeError, "wrong data for timestamp converter in tuple %d field %d length %d", tuple, field, len);
163
+ }
164
+
165
+ timestamp = read_nbo64(val);
166
+
167
+ switch(timestamp){
168
+ case PG_INT64_MAX:
169
+ return rb_str_new2("infinity");
170
+ case PG_INT64_MIN:
171
+ return rb_str_new2("-infinity");
172
+ default:
173
+ /* PostgreSQL's timestamp is based on year 2000 and Ruby's time is based on 1970.
174
+ * Adjust the 30 years difference. */
175
+ sec = (timestamp / 1000000) + 10957L * 24L * 3600L;
176
+ nsec = (timestamp % 1000000) * 1000;
177
+
178
+ #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
179
+ /* Fast path for time conversion */
180
+ {
181
+ struct timespec ts = {sec, nsec};
182
+ t = rb_time_timespec_new(&ts, conv->flags & PG_CODER_TIMESTAMP_APP_LOCAL ? INT_MAX : INT_MAX-1);
183
+ }
184
+ #else
185
+ t = rb_funcall(rb_cTime, rb_intern("at"), 2, LL2NUM(sec), LL2NUM(nsec / 1000));
186
+ if( !(conv->flags & PG_CODER_TIMESTAMP_APP_LOCAL) ) {
187
+ t = rb_funcall(t, rb_intern("utc"), 0);
188
+ }
189
+ #endif
190
+ if( conv->flags & PG_CODER_TIMESTAMP_DB_LOCAL ) {
191
+ /* interpret it as local time */
192
+ t = rb_funcall(t, rb_intern("-"), 1, rb_funcall(t, rb_intern("utc_offset"), 0));
193
+ }
194
+ return t;
195
+ }
196
+ }
197
+
133
198
  /*
134
199
  * Document-class: PG::BinaryDecoder::String < PG::SimpleDecoder
135
200
  *
@@ -156,6 +221,8 @@ init_pg_binary_decoder()
156
221
  pg_define_coder( "String", pg_text_dec_string, rb_cPG_SimpleDecoder, rb_mPG_BinaryDecoder );
157
222
  /* dummy = rb_define_class_under( rb_mPG_BinaryDecoder, "Bytea", rb_cPG_SimpleDecoder ); */
158
223
  pg_define_coder( "Bytea", pg_bin_dec_bytea, rb_cPG_SimpleDecoder, rb_mPG_BinaryDecoder );
224
+ /* dummy = rb_define_class_under( rb_mPG_BinaryDecoder, "Timestamp", rb_cPG_SimpleDecoder ); */
225
+ pg_define_coder( "Timestamp", pg_bin_dec_timestamp, rb_cPG_SimpleDecoder, rb_mPG_BinaryDecoder );
159
226
 
160
227
  /* dummy = rb_define_class_under( rb_mPG_BinaryDecoder, "ToBase64", rb_cPG_CompositeDecoder ); */
161
228
  pg_define_coder( "ToBase64", pg_bin_dec_to_base64, rb_cPG_CompositeDecoder, rb_mPG_BinaryDecoder );
@@ -1,11 +1,11 @@
1
1
  /*
2
2
  * pg_column_map.c - PG::ColumnMap class extension
3
- * $Id: pg_binary_encoder.c,v e61a06f1f5ed 2015/12/25 21:14:21 lars $
3
+ * $Id$
4
4
  *
5
5
  */
6
6
 
7
7
  #include "pg.h"
8
- #include "util.h"
8
+ #include "pg_util.h"
9
9
  #ifdef HAVE_INTTYPES_H
10
10
  #include <inttypes.h>
11
11
  #endif
@@ -25,11 +25,12 @@ static int
25
25
  pg_bin_enc_boolean(t_pg_coder *conv, VALUE value, char *out, VALUE *intermediate, int enc_idx)
26
26
  {
27
27
  char mybool;
28
- switch(value){
29
- case Qtrue : mybool = 1; break;
30
- case Qfalse : mybool = 0; break;
31
- default :
32
- rb_raise( rb_eTypeError, "wrong data for binary boolean converter" );
28
+ if (value == Qtrue) {
29
+ mybool = 1;
30
+ } else if (value == Qfalse) {
31
+ mybool = 0;
32
+ } else {
33
+ rb_raise( rb_eTypeError, "wrong data for binary boolean converter" );
33
34
  }
34
35
  if(out) *out = mybool;
35
36
  return 1;
@@ -38,7 +39,7 @@ pg_bin_enc_boolean(t_pg_coder *conv, VALUE value, char *out, VALUE *intermediate
38
39
  /*
39
40
  * Document-class: PG::BinaryEncoder::Int2 < PG::SimpleEncoder
40
41
  *
41
- * This is the encoder class for the PostgreSQL int2 type.
42
+ * This is the encoder class for the PostgreSQL +int2+ (alias +smallint+) type.
42
43
  *
43
44
  * Non-Number values are expected to have method +to_i+ defined.
44
45
  *
@@ -55,9 +56,9 @@ pg_bin_enc_int2(t_pg_coder *conv, VALUE value, char *out, VALUE *intermediate, i
55
56
  }
56
57
 
57
58
  /*
58
- * Document-class: PG::BinaryEncoder::Int2 < PG::SimpleEncoder
59
+ * Document-class: PG::BinaryEncoder::Int4 < PG::SimpleEncoder
59
60
  *
60
- * This is the encoder class for the PostgreSQL int4 type.
61
+ * This is the encoder class for the PostgreSQL +int4+ (alias +integer+) type.
61
62
  *
62
63
  * Non-Number values are expected to have method +to_i+ defined.
63
64
  *
@@ -74,9 +75,9 @@ pg_bin_enc_int4(t_pg_coder *conv, VALUE value, char *out, VALUE *intermediate, i
74
75
  }
75
76
 
76
77
  /*
77
- * Document-class: PG::BinaryEncoder::Int2 < PG::SimpleEncoder
78
+ * Document-class: PG::BinaryEncoder::Int8 < PG::SimpleEncoder
78
79
  *
79
- * This is the encoder class for the PostgreSQL int8 type.
80
+ * This is the encoder class for the PostgreSQL +int8+ (alias +bigint+) type.
80
81
  *
81
82
  * Non-Number values are expected to have method +to_i+ defined.
82
83
  *