pg 0.18.2 → 1.4.5

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 (119) 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 +131 -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/BSDL +2 -2
  15. data/Gemfile +14 -0
  16. data/History.rdoc +480 -4
  17. data/Manifest.txt +8 -21
  18. data/README-Windows.rdoc +17 -28
  19. data/README.ja.rdoc +1 -2
  20. data/README.rdoc +92 -20
  21. data/Rakefile +33 -133
  22. data/Rakefile.cross +89 -67
  23. data/certs/ged.pem +24 -0
  24. data/certs/larskanis-2022.pem +26 -0
  25. data/ext/errorcodes.def +113 -0
  26. data/ext/errorcodes.rb +1 -1
  27. data/ext/errorcodes.txt +36 -2
  28. data/ext/extconf.rb +120 -54
  29. data/ext/gvl_wrappers.c +8 -0
  30. data/ext/gvl_wrappers.h +44 -33
  31. data/ext/pg.c +216 -172
  32. data/ext/pg.h +93 -98
  33. data/ext/pg_binary_decoder.c +85 -16
  34. data/ext/pg_binary_encoder.c +25 -22
  35. data/ext/pg_coder.c +176 -40
  36. data/ext/pg_connection.c +1735 -1138
  37. data/ext/pg_copy_coder.c +95 -28
  38. data/ext/pg_errors.c +1 -1
  39. data/ext/pg_record_coder.c +521 -0
  40. data/ext/pg_result.c +642 -221
  41. data/ext/pg_text_decoder.c +609 -41
  42. data/ext/pg_text_encoder.c +254 -100
  43. data/ext/pg_tuple.c +569 -0
  44. data/ext/pg_type_map.c +62 -22
  45. data/ext/pg_type_map_all_strings.c +20 -6
  46. data/ext/pg_type_map_by_class.c +55 -25
  47. data/ext/pg_type_map_by_column.c +81 -42
  48. data/ext/pg_type_map_by_mri_type.c +49 -20
  49. data/ext/pg_type_map_by_oid.c +56 -26
  50. data/ext/pg_type_map_in_ruby.c +52 -21
  51. data/ext/{util.c → pg_util.c} +12 -12
  52. data/ext/{util.h → pg_util.h} +2 -2
  53. data/lib/pg/basic_type_map_based_on_result.rb +47 -0
  54. data/lib/pg/basic_type_map_for_queries.rb +193 -0
  55. data/lib/pg/basic_type_map_for_results.rb +81 -0
  56. data/lib/pg/basic_type_registry.rb +301 -0
  57. data/lib/pg/binary_decoder.rb +23 -0
  58. data/lib/pg/coder.rb +24 -3
  59. data/lib/pg/connection.rb +711 -64
  60. data/lib/pg/constants.rb +2 -1
  61. data/lib/pg/exceptions.rb +9 -2
  62. data/lib/pg/result.rb +24 -7
  63. data/lib/pg/text_decoder.rb +27 -23
  64. data/lib/pg/text_encoder.rb +40 -8
  65. data/lib/pg/tuple.rb +30 -0
  66. data/lib/pg/type_map_by_column.rb +3 -2
  67. data/lib/pg/version.rb +4 -0
  68. data/lib/pg.rb +61 -36
  69. data/misc/openssl-pg-segfault.rb +31 -0
  70. data/misc/postgres/History.txt +9 -0
  71. data/misc/postgres/Manifest.txt +5 -0
  72. data/misc/postgres/README.txt +21 -0
  73. data/misc/postgres/Rakefile +21 -0
  74. data/misc/postgres/lib/postgres.rb +16 -0
  75. data/misc/ruby-pg/History.txt +9 -0
  76. data/misc/ruby-pg/Manifest.txt +5 -0
  77. data/misc/ruby-pg/README.txt +21 -0
  78. data/misc/ruby-pg/Rakefile +21 -0
  79. data/misc/ruby-pg/lib/ruby/pg.rb +16 -0
  80. data/pg.gemspec +32 -0
  81. data/rakelib/task_extension.rb +46 -0
  82. data/sample/array_insert.rb +1 -1
  83. data/sample/async_api.rb +4 -8
  84. data/sample/async_copyto.rb +1 -1
  85. data/sample/async_mixed.rb +1 -1
  86. data/sample/check_conn.rb +1 -1
  87. data/sample/copydata.rb +71 -0
  88. data/sample/copyfrom.rb +1 -1
  89. data/sample/copyto.rb +1 -1
  90. data/sample/cursor.rb +1 -1
  91. data/sample/disk_usage_report.rb +6 -15
  92. data/sample/issue-119.rb +2 -2
  93. data/sample/losample.rb +1 -1
  94. data/sample/minimal-testcase.rb +2 -2
  95. data/sample/notify_wait.rb +1 -1
  96. data/sample/pg_statistics.rb +6 -15
  97. data/sample/replication_monitor.rb +9 -18
  98. data/sample/test_binary_values.rb +1 -1
  99. data/sample/wal_shipper.rb +2 -2
  100. data/sample/warehouse_partitions.rb +8 -17
  101. data.tar.gz.sig +0 -0
  102. metadata +74 -216
  103. metadata.gz.sig +0 -0
  104. data/ChangeLog +0 -5545
  105. data/lib/pg/basic_type_mapping.rb +0 -399
  106. data/spec/data/expected_trace.out +0 -26
  107. data/spec/data/random_binary_data +0 -0
  108. data/spec/helpers.rb +0 -355
  109. data/spec/pg/basic_type_mapping_spec.rb +0 -251
  110. data/spec/pg/connection_spec.rb +0 -1535
  111. data/spec/pg/result_spec.rb +0 -449
  112. data/spec/pg/type_map_by_class_spec.rb +0 -138
  113. data/spec/pg/type_map_by_column_spec.rb +0 -222
  114. data/spec/pg/type_map_by_mri_type_spec.rb +0 -136
  115. data/spec/pg/type_map_by_oid_spec.rb +0 -149
  116. data/spec/pg/type_map_in_ruby_spec.rb +0 -164
  117. data/spec/pg/type_map_spec.rb +0 -22
  118. data/spec/pg/type_spec.rb +0 -688
  119. data/spec/pg_spec.rb +0 -50
data/ext/pg.h CHANGED
@@ -9,95 +9,28 @@
9
9
  #include <stdio.h>
10
10
  #include <stdlib.h>
11
11
  #include <sys/types.h>
12
+ #if !defined(_WIN32)
13
+ # include <sys/time.h>
14
+ # include <sys/socket.h>
15
+ #endif
12
16
  #if defined(HAVE_UNISTD_H) && !defined(_WIN32)
13
17
  # include <unistd.h>
14
18
  #endif /* HAVE_UNISTD_H */
15
19
 
16
20
  /* Ruby headers */
17
21
  #include "ruby.h"
18
- #ifdef HAVE_RUBY_ST_H
19
- # include "ruby/st.h"
20
- #elif HAVE_ST_H
21
- # include "st.h"
22
- #endif
22
+ #include "ruby/st.h"
23
+ #include "ruby/encoding.h"
23
24
 
24
- #if defined(HAVE_RUBY_ENCODING_H) && HAVE_RUBY_ENCODING_H
25
- # include "ruby/encoding.h"
26
- # define M17N_SUPPORTED
27
- # ifdef HAVE_RB_ENCDB_ALIAS
28
- extern int rb_encdb_alias(const char *, const char *);
29
- # define ENC_ALIAS(name, orig) rb_encdb_alias((name), (orig))
30
- # elif HAVE_RB_ENC_ALIAS
31
- extern int rb_enc_alias(const char *, const char *);
32
- # define ENC_ALIAS(name, orig) rb_enc_alias((name), (orig))
33
- # else
34
- extern int rb_enc_alias(const char *alias, const char *orig); /* declaration missing in Ruby 1.9.1 */
35
- # define ENC_ALIAS(name, orig) rb_enc_alias((name), (orig))
36
- # endif
37
-
38
-
39
- # if !defined(ENCODING_SET_INLINED)
40
- /* Rubinius doesn't define ENCODING_SET_INLINED, so we fall back to the more
41
- * portable version.
42
- */
43
- # define PG_ENCODING_SET_NOCHECK(obj,i) \
44
- do { \
45
- rb_enc_set_index((obj), (i)); \
46
- } while(0)
47
- # else
48
- # define PG_ENCODING_SET_NOCHECK(obj,i) \
25
+ #define PG_ENCODING_SET_NOCHECK(obj,i) \
49
26
  do { \
50
27
  if ((i) < ENCODING_INLINE_MAX) \
51
28
  ENCODING_SET_INLINED((obj), (i)); \
52
29
  else \
53
30
  rb_enc_set_index((obj), (i)); \
54
31
  } while(0)
55
- # endif
56
-
57
- #else
58
- # define PG_ENCODING_SET_NOCHECK(obj,i) /* nothing */
59
- #endif
60
-
61
- #if RUBY_VM != 1
62
- # define RUBY_18_COMPAT
63
- #endif
64
-
65
- #ifndef RARRAY_LEN
66
- # define RARRAY_LEN(x) RARRAY((x))->len
67
- #endif /* RARRAY_LEN */
68
-
69
- #ifndef RSTRING_LEN
70
- # define RSTRING_LEN(x) RSTRING((x))->len
71
- #endif /* RSTRING_LEN */
72
32
 
73
- #ifndef RSTRING_PTR
74
- # define RSTRING_PTR(x) RSTRING((x))->ptr
75
- #endif /* RSTRING_PTR */
76
-
77
- #ifndef StringValuePtr
78
- # define StringValuePtr(x) STR2CSTR(x)
79
- #endif /* StringValuePtr */
80
-
81
- #ifdef RUBY_18_COMPAT
82
- # define rb_io_stdio_file GetWriteFile
83
- # include "rubyio.h"
84
- #else
85
- # include "ruby/io.h"
86
- #endif
87
-
88
- #ifdef RUBINIUS
89
- /* Workaround for wrong FIXNUM_MAX definition */
90
- typedef intptr_t native_int;
91
- #endif
92
-
93
- #ifndef RETURN_SIZED_ENUMERATOR
94
- #define RETURN_SIZED_ENUMERATOR(obj, argc, argv, size_fn) RETURN_ENUMERATOR((obj), (argc), (argv))
95
- #endif
96
-
97
- #ifndef HAVE_RB_HASH_DUP
98
- /* Rubinius doesn't define rb_hash_dup() */
99
- #define rb_hash_dup(tuple) rb_funcall((tuple), rb_intern("dup"), 0)
100
- #endif
33
+ #include "ruby/io.h"
101
34
 
102
35
  #ifndef timeradd
103
36
  #define timeradd(a, b, result) \
@@ -124,6 +57,7 @@
124
57
  #endif
125
58
 
126
59
  /* PostgreSQL headers */
60
+ #include "pg_config.h"
127
61
  #include "libpq-fe.h"
128
62
  #include "libpq/libpq-fs.h" /* large-object interface */
129
63
  #include "pg_config_manual.h"
@@ -142,6 +76,21 @@ typedef long suseconds_t;
142
76
  #define PG_MAX_COLUMNS 4000
143
77
  #endif
144
78
 
79
+ #ifndef RARRAY_AREF
80
+ #define RARRAY_AREF(a, i) (RARRAY_PTR(a)[i])
81
+ #endif
82
+
83
+ #ifdef HAVE_RB_GC_MARK_MOVABLE
84
+ #define pg_compact_callback(x) (x)
85
+ #define pg_gc_location(x) x = rb_gc_location(x)
86
+ #else
87
+ #define rb_gc_mark_movable(x) rb_gc_mark(x)
88
+ #define pg_compact_callback(x) {(x)}
89
+ #define pg_gc_location(x) UNUSED(x)
90
+ #endif
91
+
92
+ #define PG_ENC_IDX_BITS 28
93
+
145
94
  /* The data behind each PG::Connection object */
146
95
  typedef struct {
147
96
  PGconn *pgconn;
@@ -158,13 +107,21 @@ typedef struct {
158
107
  VALUE type_map_for_results;
159
108
  /* IO object internally used for the trace stream */
160
109
  VALUE trace_stream;
161
- /* Cached Encoding object */
162
- VALUE external_encoding;
163
110
  /* Kind of PG::Coder object for casting ruby values to COPY rows */
164
111
  VALUE encoder_for_put_copy_data;
165
112
  /* Kind of PG::Coder object for casting COPY rows to ruby values */
166
113
  VALUE decoder_for_get_copy_data;
114
+ /* Ruby encoding index of the client/internal encoding */
115
+ int enc_idx : PG_ENC_IDX_BITS;
116
+ /* flags controlling Symbol/String field names */
117
+ unsigned int flags : 2;
118
+ /* enable automatic flushing of send data at the end of send_query calls */
119
+ unsigned int flush_data : 1;
167
120
 
121
+ #if defined(_WIN32)
122
+ /* File descriptor to be used for rb_w32_unwrap_io_handle() */
123
+ int ruby_sd;
124
+ #endif
168
125
  } t_pg_connection;
169
126
 
170
127
  typedef struct pg_coder t_pg_coder;
@@ -185,28 +142,40 @@ typedef struct {
185
142
  */
186
143
  t_typemap *p_typemap;
187
144
 
145
+ /* Ruby encoding index of the client/internal encoding */
146
+ int enc_idx : PG_ENC_IDX_BITS;
147
+
188
148
  /* 0 = PGresult is cleared by PG::Result#clear or by the GC
189
149
  * 1 = PGresult is cleared internally by libpq
190
150
  */
191
- int autoclear;
151
+ unsigned int autoclear : 1;
152
+
153
+ /* flags controlling Symbol/String field names */
154
+ unsigned int flags : 2;
192
155
 
193
156
  /* Number of fields in fnames[] .
194
157
  * Set to -1 if fnames[] is not yet initialized.
195
158
  */
196
159
  int nfields;
197
160
 
161
+ /* Size of PGresult as published to ruby memory management. */
162
+ ssize_t result_size;
163
+
198
164
  /* Prefilled tuple Hash with fnames[] as keys. */
199
165
  VALUE tuple_hash;
200
166
 
201
- /* List of field names as frozen String objects.
167
+ /* Hash with fnames[] to field number mapping. */
168
+ VALUE field_map;
169
+
170
+ /* List of field names as frozen String or Symbol objects.
202
171
  * Only valid if nfields != -1
203
172
  */
204
173
  VALUE fnames[0];
205
174
  } t_pg_result;
206
175
 
207
176
 
208
- typedef int (* t_pg_coder_enc_func)(t_pg_coder *, VALUE, char *, VALUE *);
209
- typedef VALUE (* t_pg_coder_dec_func)(t_pg_coder *, char *, int, int, int, int);
177
+ typedef int (* t_pg_coder_enc_func)(t_pg_coder *, VALUE, char *, VALUE *, int);
178
+ typedef VALUE (* t_pg_coder_dec_func)(t_pg_coder *, const char *, int, int, int, int);
210
179
  typedef VALUE (* t_pg_fit_to_result)(VALUE, VALUE);
211
180
  typedef VALUE (* t_pg_fit_to_query)(VALUE, VALUE);
212
181
  typedef int (* t_pg_fit_to_copy_get)(VALUE);
@@ -214,12 +183,27 @@ typedef VALUE (* t_pg_typecast_result)(t_typemap *, VALUE, int, int);
214
183
  typedef t_pg_coder *(* t_pg_typecast_query_param)(t_typemap *, VALUE, int);
215
184
  typedef VALUE (* t_pg_typecast_copy_get)( t_typemap *, VALUE, int, int, int );
216
185
 
186
+ #define PG_RESULT_FIELD_NAMES_MASK 0x03
187
+ #define PG_RESULT_FIELD_NAMES_SYMBOL 0x01
188
+ #define PG_RESULT_FIELD_NAMES_STATIC_SYMBOL 0x02
189
+
190
+ #define PG_CODER_TIMESTAMP_DB_UTC 0x0
191
+ #define PG_CODER_TIMESTAMP_DB_LOCAL 0x1
192
+ #define PG_CODER_TIMESTAMP_APP_UTC 0x0
193
+ #define PG_CODER_TIMESTAMP_APP_LOCAL 0x2
194
+ #define PG_CODER_FORMAT_ERROR_MASK 0xc
195
+ #define PG_CODER_FORMAT_ERROR_TO_RAISE 0x4
196
+ #define PG_CODER_FORMAT_ERROR_TO_STRING 0x8
197
+ #define PG_CODER_FORMAT_ERROR_TO_PARTIAL 0xc
198
+
217
199
  struct pg_coder {
218
200
  t_pg_coder_enc_func enc_func;
219
201
  t_pg_coder_dec_func dec_func;
220
202
  VALUE coder_obj;
221
203
  Oid oid;
222
204
  int format;
205
+ /* OR-ed values out of PG_CODER_* */
206
+ int flags;
223
207
  };
224
208
 
225
209
  typedef struct {
@@ -249,6 +233,8 @@ typedef struct {
249
233
  } convs[0];
250
234
  } t_tmbc;
251
235
 
236
+ extern const rb_data_type_t pg_typemap_type;
237
+ extern const rb_data_type_t pg_coder_type;
252
238
 
253
239
  #include "gvl_wrappers.h"
254
240
 
@@ -256,6 +242,7 @@ typedef struct {
256
242
  * Globals
257
243
  **************************************************************************/
258
244
 
245
+ extern int pg_skip_deprecation_warning;
259
246
  extern VALUE rb_mPG;
260
247
  extern VALUE rb_ePGerror;
261
248
  extern VALUE rb_eServerError;
@@ -314,15 +301,17 @@ void init_pg_type_map_by_oid _(( void ));
314
301
  void init_pg_type_map_in_ruby _(( void ));
315
302
  void init_pg_coder _(( void ));
316
303
  void init_pg_copycoder _(( void ));
304
+ void init_pg_recordcoder _(( void ));
317
305
  void init_pg_text_encoder _(( void ));
318
306
  void init_pg_text_decoder _(( void ));
319
307
  void init_pg_binary_encoder _(( void ));
320
308
  void init_pg_binary_decoder _(( void ));
309
+ void init_pg_tuple _(( void ));
321
310
  VALUE lookup_error_class _(( const char * ));
322
- VALUE pg_bin_dec_bytea _(( t_pg_coder*, char *, int, int, int, int ));
323
- VALUE pg_text_dec_string _(( t_pg_coder*, char *, int, int, int, int ));
324
- int pg_coder_enc_to_s _(( t_pg_coder*, VALUE, char *, VALUE *));
325
- int pg_text_enc_identifier _(( t_pg_coder*, VALUE, char *, VALUE *));
311
+ VALUE pg_bin_dec_bytea _(( t_pg_coder*, const char *, int, int, int, int ));
312
+ VALUE pg_text_dec_string _(( t_pg_coder*, const char *, int, int, int, int ));
313
+ int pg_coder_enc_to_s _(( t_pg_coder*, VALUE, char *, VALUE *, int));
314
+ int pg_text_enc_identifier _(( t_pg_coder*, VALUE, char *, VALUE *, int));
326
315
  t_pg_coder_enc_func pg_coder_enc_func _(( t_pg_coder* ));
327
316
  t_pg_coder_dec_func pg_coder_dec_func _(( t_pg_coder*, int ));
328
317
  void pg_define_coder _(( const char *, void *, VALUE, VALUE ));
@@ -330,6 +319,7 @@ VALUE pg_obj_to_i _(( VALUE ));
330
319
  VALUE pg_tmbc_allocate _(( void ));
331
320
  void pg_coder_init_encoder _(( VALUE ));
332
321
  void pg_coder_init_decoder _(( VALUE ));
322
+ void pg_coder_compact _(( void * ));
333
323
  char *pg_rb_str_ensure_capa _(( VALUE, long, char *, char ** ));
334
324
 
335
325
  #define PG_RB_STR_ENSURE_CAPA( str, expand_len, curr_ptr, end_ptr ) \
@@ -343,26 +333,26 @@ char *pg_rb_str_ensure_capa _(( VALUE, long, char *,
343
333
  (curr_ptr) = (end_ptr) = RSTRING_PTR(str) \
344
334
  )
345
335
 
346
- #define PG_RB_TAINTED_STR_NEW( str, curr_ptr, end_ptr ) ( \
347
- (str) = rb_tainted_str_new( NULL, 0 ), \
348
- (curr_ptr) = (end_ptr) = RSTRING_PTR(str) \
349
- )
350
-
351
336
  VALUE pg_typemap_fit_to_result _(( VALUE, VALUE ));
352
337
  VALUE pg_typemap_fit_to_query _(( VALUE, VALUE ));
353
338
  int pg_typemap_fit_to_copy_get _(( VALUE ));
354
339
  VALUE pg_typemap_result_value _(( t_typemap *, VALUE, int, int ));
355
340
  t_pg_coder *pg_typemap_typecast_query_param _(( t_typemap *, VALUE, int ));
356
341
  VALUE pg_typemap_typecast_copy_get _(( t_typemap *, VALUE, int, int, int ));
342
+ void pg_typemap_mark _(( void * ));
343
+ size_t pg_typemap_memsize _(( const void * ));
344
+ void pg_typemap_compact _(( void * ));
357
345
 
358
346
  PGconn *pg_get_pgconn _(( VALUE ));
359
347
  t_pg_connection *pg_get_connection _(( VALUE ));
348
+ VALUE pgconn_block _(( int, VALUE *, VALUE ));
360
349
 
361
350
  VALUE pg_new_result _(( PGresult *, VALUE ));
362
351
  VALUE pg_new_result_autoclear _(( PGresult *, VALUE ));
363
352
  PGresult* pgresult_get _(( VALUE ));
364
353
  VALUE pg_result_check _(( VALUE ));
365
354
  VALUE pg_result_clear _(( VALUE ));
355
+ VALUE pg_tuple_new _(( VALUE, int ));
366
356
 
367
357
  /*
368
358
  * Fetch the data pointer for the result object
@@ -370,23 +360,28 @@ VALUE pg_result_clear _(( VALUE ));
370
360
  static inline t_pg_result *
371
361
  pgresult_get_this( VALUE self )
372
362
  {
373
- t_pg_result *this = DATA_PTR(self);
374
-
375
- if( this == NULL )
376
- rb_raise(rb_ePGerror, "result has been cleared");
377
-
378
- return this;
363
+ return RTYPEDDATA_DATA(self);
379
364
  }
380
365
 
381
366
 
382
- #ifdef M17N_SUPPORTED
383
367
  rb_encoding * pg_get_pg_encoding_as_rb_encoding _(( int ));
384
368
  rb_encoding * pg_get_pg_encname_as_rb_encoding _(( const char * ));
385
369
  const char * pg_get_rb_encoding_as_pg_encoding _(( rb_encoding * ));
386
370
  rb_encoding *pg_conn_enc_get _(( PGconn * ));
387
- #endif /* M17N_SUPPORTED */
388
371
 
389
372
  void notice_receiver_proxy(void *arg, const PGresult *result);
390
373
  void notice_processor_proxy(void *arg, const char *message);
391
374
 
375
+ /* reports if `-W' specified and PG_SKIP_DEPRECATION_WARNING environment variable isn't set
376
+ *
377
+ * message_id identifies the warning, so that it's reported only once.
378
+ */
379
+ #define pg_deprecated(message_id, format_args) \
380
+ do { \
381
+ if( !(pg_skip_deprecation_warning & (1 << message_id)) ){ \
382
+ pg_skip_deprecation_warning |= 1 << message_id; \
383
+ rb_warning format_args; \
384
+ } \
385
+ } while(0);
386
+
392
387
  #endif /* end __pg_h */
@@ -1,12 +1,15 @@
1
1
  /*
2
2
  * pg_column_map.c - PG::ColumnMap class extension
3
- * $Id: pg_binary_decoder.c,v 185638b52684 2014/11/08 20:43:53 lars $
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"
10
+ #ifdef HAVE_INTTYPES_H
9
11
  #include <inttypes.h>
12
+ #endif
10
13
 
11
14
  VALUE rb_mPG_BinaryDecoder;
12
15
 
@@ -14,12 +17,12 @@ VALUE rb_mPG_BinaryDecoder;
14
17
  /*
15
18
  * Document-class: PG::BinaryDecoder::Boolean < PG::SimpleDecoder
16
19
  *
17
- * This is a decoder class for conversion of PostgreSQL binary bool type
18
- * 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.
19
22
  *
20
23
  */
21
24
  static VALUE
22
- 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)
23
26
  {
24
27
  if (len < 1) {
25
28
  rb_raise( rb_eTypeError, "wrong data for binary boolean converter in tuple %d field %d", tuple, field);
@@ -30,12 +33,12 @@ pg_bin_dec_boolean(t_pg_coder *conv, char *val, int len, int tuple, int field, i
30
33
  /*
31
34
  * Document-class: PG::BinaryDecoder::Integer < PG::SimpleDecoder
32
35
  *
33
- * 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
34
37
  * to Ruby Integer objects.
35
38
  *
36
39
  */
37
40
  static VALUE
38
- 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)
39
42
  {
40
43
  switch( len ){
41
44
  case 2:
@@ -52,12 +55,12 @@ pg_bin_dec_integer(t_pg_coder *conv, char *val, int len, int tuple, int field, i
52
55
  /*
53
56
  * Document-class: PG::BinaryDecoder::Float < PG::SimpleDecoder
54
57
  *
55
- * 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
56
59
  * to Ruby Float objects.
57
60
  *
58
61
  */
59
62
  static VALUE
60
- 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)
61
64
  {
62
65
  union {
63
66
  float f;
@@ -84,15 +87,15 @@ pg_bin_dec_float(t_pg_coder *conv, char *val, int len, int tuple, int field, int
84
87
  * Document-class: PG::BinaryDecoder::Bytea < PG::SimpleDecoder
85
88
  *
86
89
  * This decoder class delivers the data received from the server as binary String object.
87
- * 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
88
91
  * data in binary format.
89
92
  *
90
93
  */
91
94
  VALUE
92
- 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)
93
96
  {
94
97
  VALUE ret;
95
- ret = rb_tainted_str_new( val, len );
98
+ ret = rb_str_new( val, len );
96
99
  PG_ENCODING_SET_NOCHECK( ret, rb_ascii8bit_encindex() );
97
100
  return ret;
98
101
  }
@@ -100,17 +103,17 @@ pg_bin_dec_bytea(t_pg_coder *conv, char *val, int len, int tuple, int field, int
100
103
  /*
101
104
  * Document-class: PG::BinaryDecoder::ToBase64 < PG::CompositeDecoder
102
105
  *
103
- * 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.
104
107
  *
105
108
  */
106
109
  static VALUE
107
- 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)
108
111
  {
109
112
  t_pg_composite_coder *this = (t_pg_composite_coder *)conv;
110
113
  t_pg_coder_dec_func dec_func = pg_coder_dec_func(this->elem, this->comp.format);
111
114
  int encoded_len = BASE64_ENCODED_SIZE(len);
112
115
  /* create a buffer of the encoded length */
113
- VALUE out_value = rb_tainted_str_new(NULL, encoded_len);
116
+ VALUE out_value = rb_str_new(NULL, encoded_len);
114
117
 
115
118
  base64_encode( RSTRING_PTR(out_value), val, len );
116
119
 
@@ -128,6 +131,70 @@ pg_bin_dec_to_base64(t_pg_coder *conv, char *val, int len, int tuple, int field,
128
131
  return out_value;
129
132
  }
130
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
+
131
198
  /*
132
199
  * Document-class: PG::BinaryDecoder::String < PG::SimpleDecoder
133
200
  *
@@ -138,7 +205,7 @@ pg_bin_dec_to_base64(t_pg_coder *conv, char *val, int len, int tuple, int field,
138
205
  */
139
206
 
140
207
  void
141
- init_pg_binary_decoder()
208
+ init_pg_binary_decoder(void)
142
209
  {
143
210
  /* This module encapsulates all decoder classes with binary input format */
144
211
  rb_mPG_BinaryDecoder = rb_define_module_under( rb_mPG, "BinaryDecoder" );
@@ -154,6 +221,8 @@ init_pg_binary_decoder()
154
221
  pg_define_coder( "String", pg_text_dec_string, rb_cPG_SimpleDecoder, rb_mPG_BinaryDecoder );
155
222
  /* dummy = rb_define_class_under( rb_mPG_BinaryDecoder, "Bytea", rb_cPG_SimpleDecoder ); */
156
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 );
157
226
 
158
227
  /* dummy = rb_define_class_under( rb_mPG_BinaryDecoder, "ToBase64", rb_cPG_CompositeDecoder ); */
159
228
  pg_define_coder( "ToBase64", pg_bin_dec_to_base64, rb_cPG_CompositeDecoder, rb_mPG_BinaryDecoder );
@@ -1,12 +1,14 @@
1
1
  /*
2
2
  * pg_column_map.c - PG::ColumnMap class extension
3
- * $Id: pg_binary_encoder.c,v ac23631c96d9 2014/10/14 11:50: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
+ #ifdef HAVE_INTTYPES_H
9
10
  #include <inttypes.h>
11
+ #endif
10
12
 
11
13
  VALUE rb_mPG_BinaryEncoder;
12
14
 
@@ -20,29 +22,30 @@ VALUE rb_mPG_BinaryEncoder;
20
22
  *
21
23
  */
22
24
  static int
23
- pg_bin_enc_boolean(t_pg_coder *conv, VALUE value, char *out, VALUE *intermediate)
25
+ pg_bin_enc_boolean(t_pg_coder *conv, VALUE value, char *out, VALUE *intermediate, int enc_idx)
24
26
  {
25
- char bool;
26
- switch(value){
27
- case Qtrue : bool = 1; break;
28
- case Qfalse : bool = 0; break;
29
- default :
30
- rb_raise( rb_eTypeError, "wrong data for binary boolean converter" );
27
+ char mybool;
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" );
31
34
  }
32
- if(out) *out = bool;
35
+ if(out) *out = mybool;
33
36
  return 1;
34
37
  }
35
38
 
36
39
  /*
37
40
  * Document-class: PG::BinaryEncoder::Int2 < PG::SimpleEncoder
38
41
  *
39
- * This is the encoder class for the PostgreSQL int2 type.
42
+ * This is the encoder class for the PostgreSQL +int2+ (alias +smallint+) type.
40
43
  *
41
44
  * Non-Number values are expected to have method +to_i+ defined.
42
45
  *
43
46
  */
44
47
  static int
45
- pg_bin_enc_int2(t_pg_coder *conv, VALUE value, char *out, VALUE *intermediate)
48
+ pg_bin_enc_int2(t_pg_coder *conv, VALUE value, char *out, VALUE *intermediate, int enc_idx)
46
49
  {
47
50
  if(out){
48
51
  write_nbo16(NUM2INT(*intermediate), out);
@@ -53,15 +56,15 @@ pg_bin_enc_int2(t_pg_coder *conv, VALUE value, char *out, VALUE *intermediate)
53
56
  }
54
57
 
55
58
  /*
56
- * Document-class: PG::BinaryEncoder::Int2 < PG::SimpleEncoder
59
+ * Document-class: PG::BinaryEncoder::Int4 < PG::SimpleEncoder
57
60
  *
58
- * This is the encoder class for the PostgreSQL int4 type.
61
+ * This is the encoder class for the PostgreSQL +int4+ (alias +integer+) type.
59
62
  *
60
63
  * Non-Number values are expected to have method +to_i+ defined.
61
64
  *
62
65
  */
63
66
  static int
64
- pg_bin_enc_int4(t_pg_coder *conv, VALUE value, char *out, VALUE *intermediate)
67
+ pg_bin_enc_int4(t_pg_coder *conv, VALUE value, char *out, VALUE *intermediate, int enc_idx)
65
68
  {
66
69
  if(out){
67
70
  write_nbo32(NUM2LONG(*intermediate), out);
@@ -72,15 +75,15 @@ pg_bin_enc_int4(t_pg_coder *conv, VALUE value, char *out, VALUE *intermediate)
72
75
  }
73
76
 
74
77
  /*
75
- * Document-class: PG::BinaryEncoder::Int2 < PG::SimpleEncoder
78
+ * Document-class: PG::BinaryEncoder::Int8 < PG::SimpleEncoder
76
79
  *
77
- * This is the encoder class for the PostgreSQL int8 type.
80
+ * This is the encoder class for the PostgreSQL +int8+ (alias +bigint+) type.
78
81
  *
79
82
  * Non-Number values are expected to have method +to_i+ defined.
80
83
  *
81
84
  */
82
85
  static int
83
- pg_bin_enc_int8(t_pg_coder *conv, VALUE value, char *out, VALUE *intermediate)
86
+ pg_bin_enc_int8(t_pg_coder *conv, VALUE value, char *out, VALUE *intermediate, int enc_idx)
84
87
  {
85
88
  if(out){
86
89
  write_nbo64(NUM2LL(*intermediate), out);
@@ -98,7 +101,7 @@ pg_bin_enc_int8(t_pg_coder *conv, VALUE value, char *out, VALUE *intermediate)
98
101
  *
99
102
  */
100
103
  static int
101
- pg_bin_enc_from_base64(t_pg_coder *conv, VALUE value, char *out, VALUE *intermediate)
104
+ pg_bin_enc_from_base64(t_pg_coder *conv, VALUE value, char *out, VALUE *intermediate, int enc_idx)
102
105
  {
103
106
  int strlen;
104
107
  VALUE subint;
@@ -107,13 +110,13 @@ pg_bin_enc_from_base64(t_pg_coder *conv, VALUE value, char *out, VALUE *intermed
107
110
 
108
111
  if(out){
109
112
  /* Second encoder pass, if required */
110
- strlen = enc_func(this->elem, value, out, intermediate);
113
+ strlen = enc_func(this->elem, value, out, intermediate, enc_idx);
111
114
  strlen = base64_decode( out, out, strlen );
112
115
 
113
116
  return strlen;
114
117
  } else {
115
118
  /* First encoder pass */
116
- strlen = enc_func(this->elem, value, NULL, &subint);
119
+ strlen = enc_func(this->elem, value, NULL, &subint, enc_idx);
117
120
 
118
121
  if( strlen == -1 ){
119
122
  /* Encoded string is returned in subint */
@@ -136,7 +139,7 @@ pg_bin_enc_from_base64(t_pg_coder *conv, VALUE value, char *out, VALUE *intermed
136
139
  }
137
140
 
138
141
  void
139
- init_pg_binary_encoder()
142
+ init_pg_binary_encoder(void)
140
143
  {
141
144
  /* This module encapsulates all encoder classes with binary output format */
142
145
  rb_mPG_BinaryEncoder = rb_define_module_under( rb_mPG, "BinaryEncoder" );