pg 1.3.0.rc2-x64-mingw-ucrt

Sign up to get free protection for your applications and to get access to all the features.
Files changed (111) hide show
  1. checksums.yaml +7 -0
  2. checksums.yaml.gz.sig +3 -0
  3. data/.appveyor.yml +36 -0
  4. data/.gems +6 -0
  5. data/.gemtest +0 -0
  6. data/.github/workflows/binary-gems.yml +85 -0
  7. data/.github/workflows/source-gem.yml +130 -0
  8. data/.gitignore +13 -0
  9. data/.hgsigs +34 -0
  10. data/.hgtags +41 -0
  11. data/.irbrc +23 -0
  12. data/.pryrc +23 -0
  13. data/.tm_properties +21 -0
  14. data/.travis.yml +49 -0
  15. data/BSDL +22 -0
  16. data/Contributors.rdoc +46 -0
  17. data/Gemfile +14 -0
  18. data/History.rdoc +648 -0
  19. data/LICENSE +56 -0
  20. data/Manifest.txt +72 -0
  21. data/POSTGRES +23 -0
  22. data/README-OS_X.rdoc +68 -0
  23. data/README-Windows.rdoc +56 -0
  24. data/README.ja.rdoc +13 -0
  25. data/README.rdoc +214 -0
  26. data/Rakefile +106 -0
  27. data/Rakefile.cross +300 -0
  28. data/certs/ged.pem +24 -0
  29. data/ext/errorcodes.def +1040 -0
  30. data/ext/errorcodes.rb +45 -0
  31. data/ext/errorcodes.txt +496 -0
  32. data/ext/extconf.rb +165 -0
  33. data/ext/gvl_wrappers.c +21 -0
  34. data/ext/gvl_wrappers.h +264 -0
  35. data/ext/pg.c +732 -0
  36. data/ext/pg.h +385 -0
  37. data/ext/pg_binary_decoder.c +229 -0
  38. data/ext/pg_binary_encoder.c +163 -0
  39. data/ext/pg_coder.c +615 -0
  40. data/ext/pg_connection.c +4415 -0
  41. data/ext/pg_copy_coder.c +628 -0
  42. data/ext/pg_errors.c +95 -0
  43. data/ext/pg_record_coder.c +519 -0
  44. data/ext/pg_result.c +1683 -0
  45. data/ext/pg_text_decoder.c +987 -0
  46. data/ext/pg_text_encoder.c +814 -0
  47. data/ext/pg_tuple.c +575 -0
  48. data/ext/pg_type_map.c +199 -0
  49. data/ext/pg_type_map_all_strings.c +129 -0
  50. data/ext/pg_type_map_by_class.c +269 -0
  51. data/ext/pg_type_map_by_column.c +349 -0
  52. data/ext/pg_type_map_by_mri_type.c +313 -0
  53. data/ext/pg_type_map_by_oid.c +385 -0
  54. data/ext/pg_type_map_in_ruby.c +330 -0
  55. data/ext/pg_util.c +149 -0
  56. data/ext/pg_util.h +65 -0
  57. data/ext/vc/pg.sln +26 -0
  58. data/ext/vc/pg_18/pg.vcproj +216 -0
  59. data/ext/vc/pg_19/pg_19.vcproj +209 -0
  60. data/lib/3.1/pg_ext.so +0 -0
  61. data/lib/pg/basic_type_map_based_on_result.rb +47 -0
  62. data/lib/pg/basic_type_map_for_queries.rb +193 -0
  63. data/lib/pg/basic_type_map_for_results.rb +81 -0
  64. data/lib/pg/basic_type_registry.rb +296 -0
  65. data/lib/pg/binary_decoder.rb +23 -0
  66. data/lib/pg/coder.rb +104 -0
  67. data/lib/pg/connection.rb +813 -0
  68. data/lib/pg/constants.rb +12 -0
  69. data/lib/pg/exceptions.rb +12 -0
  70. data/lib/pg/result.rb +43 -0
  71. data/lib/pg/text_decoder.rb +46 -0
  72. data/lib/pg/text_encoder.rb +59 -0
  73. data/lib/pg/tuple.rb +30 -0
  74. data/lib/pg/type_map_by_column.rb +16 -0
  75. data/lib/pg/version.rb +4 -0
  76. data/lib/pg.rb +87 -0
  77. data/lib/x64-mingw-ucrt/libpq.dll +0 -0
  78. data/misc/openssl-pg-segfault.rb +31 -0
  79. data/misc/postgres/History.txt +9 -0
  80. data/misc/postgres/Manifest.txt +5 -0
  81. data/misc/postgres/README.txt +21 -0
  82. data/misc/postgres/Rakefile +21 -0
  83. data/misc/postgres/lib/postgres.rb +16 -0
  84. data/misc/ruby-pg/History.txt +9 -0
  85. data/misc/ruby-pg/Manifest.txt +5 -0
  86. data/misc/ruby-pg/README.txt +21 -0
  87. data/misc/ruby-pg/Rakefile +21 -0
  88. data/misc/ruby-pg/lib/ruby/pg.rb +16 -0
  89. data/pg.gemspec +32 -0
  90. data/sample/array_insert.rb +20 -0
  91. data/sample/async_api.rb +106 -0
  92. data/sample/async_copyto.rb +39 -0
  93. data/sample/async_mixed.rb +56 -0
  94. data/sample/check_conn.rb +21 -0
  95. data/sample/copydata.rb +71 -0
  96. data/sample/copyfrom.rb +81 -0
  97. data/sample/copyto.rb +19 -0
  98. data/sample/cursor.rb +21 -0
  99. data/sample/disk_usage_report.rb +177 -0
  100. data/sample/issue-119.rb +94 -0
  101. data/sample/losample.rb +69 -0
  102. data/sample/minimal-testcase.rb +17 -0
  103. data/sample/notify_wait.rb +72 -0
  104. data/sample/pg_statistics.rb +285 -0
  105. data/sample/replication_monitor.rb +222 -0
  106. data/sample/test_binary_values.rb +33 -0
  107. data/sample/wal_shipper.rb +434 -0
  108. data/sample/warehouse_partitions.rb +311 -0
  109. data.tar.gz.sig +0 -0
  110. metadata +188 -0
  111. metadata.gz.sig +0 -0
data/ext/pg.h ADDED
@@ -0,0 +1,385 @@
1
+ #ifndef __pg_h
2
+ #define __pg_h
3
+
4
+ #ifdef RUBY_EXTCONF_H
5
+ # include RUBY_EXTCONF_H
6
+ #endif
7
+
8
+ /* System headers */
9
+ #include <stdio.h>
10
+ #include <stdlib.h>
11
+ #include <sys/types.h>
12
+ #if !defined(_WIN32)
13
+ # include <sys/time.h>
14
+ # include <sys/socket.h>
15
+ #endif
16
+ #if defined(HAVE_UNISTD_H) && !defined(_WIN32)
17
+ # include <unistd.h>
18
+ #endif /* HAVE_UNISTD_H */
19
+
20
+ /* Ruby headers */
21
+ #include "ruby.h"
22
+ #include "ruby/st.h"
23
+ #include "ruby/encoding.h"
24
+
25
+ #define PG_ENCODING_SET_NOCHECK(obj,i) \
26
+ do { \
27
+ if ((i) < ENCODING_INLINE_MAX) \
28
+ ENCODING_SET_INLINED((obj), (i)); \
29
+ else \
30
+ rb_enc_set_index((obj), (i)); \
31
+ } while(0)
32
+
33
+ #include "ruby/io.h"
34
+
35
+ #ifndef timeradd
36
+ #define timeradd(a, b, result) \
37
+ do { \
38
+ (result)->tv_sec = (a)->tv_sec + (b)->tv_sec; \
39
+ (result)->tv_usec = (a)->tv_usec + (b)->tv_usec; \
40
+ if ((result)->tv_usec >= 1000000L) { \
41
+ ++(result)->tv_sec; \
42
+ (result)->tv_usec -= 1000000L; \
43
+ } \
44
+ } while (0)
45
+ #endif
46
+
47
+ #ifndef timersub
48
+ #define timersub(a, b, result) \
49
+ do { \
50
+ (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
51
+ (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
52
+ if ((result)->tv_usec < 0) { \
53
+ --(result)->tv_sec; \
54
+ (result)->tv_usec += 1000000L; \
55
+ } \
56
+ } while (0)
57
+ #endif
58
+
59
+ /* PostgreSQL headers */
60
+ #include "libpq-fe.h"
61
+ #include "libpq/libpq-fs.h" /* large-object interface */
62
+ #include "pg_config_manual.h"
63
+
64
+ #if defined(_WIN32)
65
+ # include <fcntl.h>
66
+ typedef long suseconds_t;
67
+ #endif
68
+
69
+ #if defined(HAVE_VARIABLE_LENGTH_ARRAYS)
70
+ #define PG_VARIABLE_LENGTH_ARRAY(type, name, len, maxlen) type name[(len)];
71
+ #else
72
+ #define PG_VARIABLE_LENGTH_ARRAY(type, name, len, maxlen) \
73
+ type name[(maxlen)] = {(len)>(maxlen) ? (rb_raise(rb_eArgError, "Number of " #name " (%d) exceeds allowed maximum of " #maxlen, (len) ), (type)1) : (type)0};
74
+
75
+ #define PG_MAX_COLUMNS 4000
76
+ #endif
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
+
93
+ /* The data behind each PG::Connection object */
94
+ typedef struct {
95
+ PGconn *pgconn;
96
+
97
+ /* Cached IO object for the socket descriptor */
98
+ VALUE socket_io;
99
+ /* Proc object that receives notices as PG::Result objects */
100
+ VALUE notice_receiver;
101
+ /* Proc object that receives notices as String objects */
102
+ VALUE notice_processor;
103
+ /* Kind of PG::TypeMap object for casting query params */
104
+ VALUE type_map_for_queries;
105
+ /* Kind of PG::TypeMap object for casting result values */
106
+ VALUE type_map_for_results;
107
+ /* IO object internally used for the trace stream */
108
+ VALUE trace_stream;
109
+ /* Kind of PG::Coder object for casting ruby values to COPY rows */
110
+ VALUE encoder_for_put_copy_data;
111
+ /* Kind of PG::Coder object for casting COPY rows to ruby values */
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;
119
+
120
+ #if defined(_WIN32)
121
+ /* File descriptor to be used for rb_w32_unwrap_io_handle() */
122
+ int ruby_sd;
123
+ #endif
124
+ } t_pg_connection;
125
+
126
+ typedef struct pg_coder t_pg_coder;
127
+ typedef struct pg_typemap t_typemap;
128
+
129
+ /* The data behind each PG::Result object */
130
+ typedef struct {
131
+ PGresult *pgresult;
132
+
133
+ /* The connection object used to build this result */
134
+ VALUE connection;
135
+
136
+ /* The TypeMap used to type cast result values */
137
+ VALUE typemap;
138
+
139
+ /* Pointer to the typemap object data. This is assumed to be
140
+ * always valid.
141
+ */
142
+ t_typemap *p_typemap;
143
+
144
+ /* Ruby encoding index of the client/internal encoding */
145
+ int enc_idx : PG_ENC_IDX_BITS;
146
+
147
+ /* 0 = PGresult is cleared by PG::Result#clear or by the GC
148
+ * 1 = PGresult is cleared internally by libpq
149
+ */
150
+ unsigned int autoclear : 1;
151
+
152
+ /* flags controlling Symbol/String field names */
153
+ unsigned int flags : 2;
154
+
155
+ /* Number of fields in fnames[] .
156
+ * Set to -1 if fnames[] is not yet initialized.
157
+ */
158
+ int nfields;
159
+
160
+ /* Size of PGresult as published to ruby memory management. */
161
+ ssize_t result_size;
162
+
163
+ /* Prefilled tuple Hash with fnames[] as keys. */
164
+ VALUE tuple_hash;
165
+
166
+ /* Hash with fnames[] to field number mapping. */
167
+ VALUE field_map;
168
+
169
+ /* List of field names as frozen String or Symbol objects.
170
+ * Only valid if nfields != -1
171
+ */
172
+ VALUE fnames[0];
173
+ } t_pg_result;
174
+
175
+
176
+ typedef int (* t_pg_coder_enc_func)(t_pg_coder *, VALUE, char *, VALUE *, int);
177
+ typedef VALUE (* t_pg_coder_dec_func)(t_pg_coder *, const char *, int, int, int, int);
178
+ typedef VALUE (* t_pg_fit_to_result)(VALUE, VALUE);
179
+ typedef VALUE (* t_pg_fit_to_query)(VALUE, VALUE);
180
+ typedef int (* t_pg_fit_to_copy_get)(VALUE);
181
+ typedef VALUE (* t_pg_typecast_result)(t_typemap *, VALUE, int, int);
182
+ typedef t_pg_coder *(* t_pg_typecast_query_param)(t_typemap *, VALUE, int);
183
+ typedef VALUE (* t_pg_typecast_copy_get)( t_typemap *, VALUE, int, int, int );
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
+
198
+ struct pg_coder {
199
+ t_pg_coder_enc_func enc_func;
200
+ t_pg_coder_dec_func dec_func;
201
+ VALUE coder_obj;
202
+ Oid oid;
203
+ int format;
204
+ /* OR-ed values out of PG_CODER_* */
205
+ int flags;
206
+ };
207
+
208
+ typedef struct {
209
+ t_pg_coder comp;
210
+ t_pg_coder *elem;
211
+ int needs_quotation;
212
+ char delimiter;
213
+ } t_pg_composite_coder;
214
+
215
+ struct pg_typemap {
216
+ struct pg_typemap_funcs {
217
+ t_pg_fit_to_result fit_to_result;
218
+ t_pg_fit_to_query fit_to_query;
219
+ t_pg_fit_to_copy_get fit_to_copy_get;
220
+ t_pg_typecast_result typecast_result_value;
221
+ t_pg_typecast_query_param typecast_query_param;
222
+ t_pg_typecast_copy_get typecast_copy_get;
223
+ } funcs;
224
+ VALUE default_typemap;
225
+ };
226
+
227
+ typedef struct {
228
+ t_typemap typemap;
229
+ int nfields;
230
+ struct pg_tmbc_converter {
231
+ t_pg_coder *cconv;
232
+ } convs[0];
233
+ } t_tmbc;
234
+
235
+ extern const rb_data_type_t pg_typemap_type;
236
+ extern const rb_data_type_t pg_coder_type;
237
+
238
+ #include "gvl_wrappers.h"
239
+
240
+ /***************************************************************************
241
+ * Globals
242
+ **************************************************************************/
243
+
244
+ extern int pg_skip_deprecation_warning;
245
+ extern VALUE rb_mPG;
246
+ extern VALUE rb_ePGerror;
247
+ extern VALUE rb_eServerError;
248
+ extern VALUE rb_eUnableToSend;
249
+ extern VALUE rb_eConnectionBad;
250
+ extern VALUE rb_eInvalidResultStatus;
251
+ extern VALUE rb_eNoResultError;
252
+ extern VALUE rb_eInvalidChangeOfResultFields;
253
+ extern VALUE rb_mPGconstants;
254
+ extern VALUE rb_cPGconn;
255
+ extern VALUE rb_cPGresult;
256
+ extern VALUE rb_hErrors;
257
+ extern VALUE rb_cTypeMap;
258
+ extern VALUE rb_cTypeMapAllStrings;
259
+ extern VALUE rb_mDefaultTypeMappable;
260
+ extern VALUE rb_cPG_Coder;
261
+ extern VALUE rb_cPG_SimpleEncoder;
262
+ extern VALUE rb_cPG_SimpleDecoder;
263
+ extern VALUE rb_cPG_CompositeEncoder;
264
+ extern VALUE rb_cPG_CompositeDecoder;
265
+ extern VALUE rb_cPG_CopyCoder;
266
+ extern VALUE rb_cPG_CopyEncoder;
267
+ extern VALUE rb_cPG_CopyDecoder;
268
+ extern VALUE rb_mPG_TextEncoder;
269
+ extern VALUE rb_mPG_TextDecoder;
270
+ extern VALUE rb_mPG_BinaryEncoder;
271
+ extern VALUE rb_mPG_BinaryDecoder;
272
+ extern VALUE rb_mPG_BinaryFormatting;
273
+ extern const struct pg_typemap_funcs pg_tmbc_funcs;
274
+ extern const struct pg_typemap_funcs pg_typemap_funcs;
275
+
276
+ extern VALUE pg_typemap_all_strings;
277
+
278
+ /***************************************************************************
279
+ * MACROS
280
+ **************************************************************************/
281
+
282
+ #define UNUSED(x) ((void)(x))
283
+ #define SINGLETON_ALIAS(klass,new,old) rb_define_alias(rb_singleton_class((klass)),(new),(old))
284
+
285
+
286
+ /***************************************************************************
287
+ * PROTOTYPES
288
+ **************************************************************************/
289
+ void Init_pg_ext _(( void ));
290
+
291
+ void init_pg_connection _(( void ));
292
+ void init_pg_result _(( void ));
293
+ void init_pg_errors _(( void ));
294
+ void init_pg_type_map _(( void ));
295
+ void init_pg_type_map_all_strings _(( void ));
296
+ void init_pg_type_map_by_class _(( void ));
297
+ void init_pg_type_map_by_column _(( void ));
298
+ void init_pg_type_map_by_mri_type _(( void ));
299
+ void init_pg_type_map_by_oid _(( void ));
300
+ void init_pg_type_map_in_ruby _(( void ));
301
+ void init_pg_coder _(( void ));
302
+ void init_pg_copycoder _(( void ));
303
+ void init_pg_recordcoder _(( void ));
304
+ void init_pg_text_encoder _(( void ));
305
+ void init_pg_text_decoder _(( void ));
306
+ void init_pg_binary_encoder _(( void ));
307
+ void init_pg_binary_decoder _(( void ));
308
+ void init_pg_tuple _(( void ));
309
+ VALUE lookup_error_class _(( const char * ));
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 ));
312
+ int pg_coder_enc_to_s _(( t_pg_coder*, VALUE, char *, VALUE *, int));
313
+ int pg_text_enc_identifier _(( t_pg_coder*, VALUE, char *, VALUE *, int));
314
+ t_pg_coder_enc_func pg_coder_enc_func _(( t_pg_coder* ));
315
+ t_pg_coder_dec_func pg_coder_dec_func _(( t_pg_coder*, int ));
316
+ void pg_define_coder _(( const char *, void *, VALUE, VALUE ));
317
+ VALUE pg_obj_to_i _(( VALUE ));
318
+ VALUE pg_tmbc_allocate _(( void ));
319
+ void pg_coder_init_encoder _(( VALUE ));
320
+ void pg_coder_init_decoder _(( VALUE ));
321
+ void pg_coder_compact _(( void * ));
322
+ char *pg_rb_str_ensure_capa _(( VALUE, long, char *, char ** ));
323
+
324
+ #define PG_RB_STR_ENSURE_CAPA( str, expand_len, curr_ptr, end_ptr ) \
325
+ do { \
326
+ if( (curr_ptr) + (expand_len) >= (end_ptr) ) \
327
+ (curr_ptr) = pg_rb_str_ensure_capa( (str), (expand_len), (curr_ptr), &(end_ptr) ); \
328
+ } while(0);
329
+
330
+ #define PG_RB_STR_NEW( str, curr_ptr, end_ptr ) ( \
331
+ (str) = rb_str_new( NULL, 0 ), \
332
+ (curr_ptr) = (end_ptr) = RSTRING_PTR(str) \
333
+ )
334
+
335
+ VALUE pg_typemap_fit_to_result _(( VALUE, VALUE ));
336
+ VALUE pg_typemap_fit_to_query _(( VALUE, VALUE ));
337
+ int pg_typemap_fit_to_copy_get _(( VALUE ));
338
+ VALUE pg_typemap_result_value _(( t_typemap *, VALUE, int, int ));
339
+ t_pg_coder *pg_typemap_typecast_query_param _(( t_typemap *, VALUE, int ));
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 * ));
344
+
345
+ PGconn *pg_get_pgconn _(( VALUE ));
346
+ t_pg_connection *pg_get_connection _(( VALUE ));
347
+
348
+ VALUE pg_new_result _(( PGresult *, VALUE ));
349
+ VALUE pg_new_result_autoclear _(( PGresult *, VALUE ));
350
+ PGresult* pgresult_get _(( VALUE ));
351
+ VALUE pg_result_check _(( VALUE ));
352
+ VALUE pg_result_clear _(( VALUE ));
353
+ VALUE pg_tuple_new _(( VALUE, int ));
354
+
355
+ /*
356
+ * Fetch the data pointer for the result object
357
+ */
358
+ static inline t_pg_result *
359
+ pgresult_get_this( VALUE self )
360
+ {
361
+ return RTYPEDDATA_DATA(self);
362
+ }
363
+
364
+
365
+ rb_encoding * pg_get_pg_encoding_as_rb_encoding _(( int ));
366
+ rb_encoding * pg_get_pg_encname_as_rb_encoding _(( const char * ));
367
+ const char * pg_get_rb_encoding_as_pg_encoding _(( rb_encoding * ));
368
+ rb_encoding *pg_conn_enc_get _(( PGconn * ));
369
+
370
+ void notice_receiver_proxy(void *arg, const PGresult *result);
371
+ void notice_processor_proxy(void *arg, const char *message);
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
+
385
+ #endif /* end __pg_h */
@@ -0,0 +1,229 @@
1
+ /*
2
+ * pg_column_map.c - PG::ColumnMap class extension
3
+ * $Id$
4
+ *
5
+ */
6
+
7
+ #include "ruby/version.h"
8
+ #include "pg.h"
9
+ #include "pg_util.h"
10
+ #ifdef HAVE_INTTYPES_H
11
+ #include <inttypes.h>
12
+ #endif
13
+
14
+ VALUE rb_mPG_BinaryDecoder;
15
+
16
+
17
+ /*
18
+ * Document-class: PG::BinaryDecoder::Boolean < PG::SimpleDecoder
19
+ *
20
+ * This is a decoder class for conversion of PostgreSQL binary +bool+ type
21
+ * to Ruby +true+ or +false+ objects.
22
+ *
23
+ */
24
+ static VALUE
25
+ pg_bin_dec_boolean(t_pg_coder *conv, const char *val, int len, int tuple, int field, int enc_idx)
26
+ {
27
+ if (len < 1) {
28
+ rb_raise( rb_eTypeError, "wrong data for binary boolean converter in tuple %d field %d", tuple, field);
29
+ }
30
+ return *val == 0 ? Qfalse : Qtrue;
31
+ }
32
+
33
+ /*
34
+ * Document-class: PG::BinaryDecoder::Integer < PG::SimpleDecoder
35
+ *
36
+ * This is a decoder class for conversion of PostgreSQL binary +int2+, +int4+ and +int8+ types
37
+ * to Ruby Integer objects.
38
+ *
39
+ */
40
+ static VALUE
41
+ pg_bin_dec_integer(t_pg_coder *conv, const char *val, int len, int tuple, int field, int enc_idx)
42
+ {
43
+ switch( len ){
44
+ case 2:
45
+ return INT2NUM(read_nbo16(val));
46
+ case 4:
47
+ return LONG2NUM(read_nbo32(val));
48
+ case 8:
49
+ return LL2NUM(read_nbo64(val));
50
+ default:
51
+ rb_raise( rb_eTypeError, "wrong data for binary integer converter in tuple %d field %d length %d", tuple, field, len);
52
+ }
53
+ }
54
+
55
+ /*
56
+ * Document-class: PG::BinaryDecoder::Float < PG::SimpleDecoder
57
+ *
58
+ * This is a decoder class for conversion of PostgreSQL binary +float4+ and +float8+ types
59
+ * to Ruby Float objects.
60
+ *
61
+ */
62
+ static VALUE
63
+ pg_bin_dec_float(t_pg_coder *conv, const char *val, int len, int tuple, int field, int enc_idx)
64
+ {
65
+ union {
66
+ float f;
67
+ int32_t i;
68
+ } swap4;
69
+ union {
70
+ double f;
71
+ int64_t i;
72
+ } swap8;
73
+
74
+ switch( len ){
75
+ case 4:
76
+ swap4.i = read_nbo32(val);
77
+ return rb_float_new(swap4.f);
78
+ case 8:
79
+ swap8.i = read_nbo64(val);
80
+ return rb_float_new(swap8.f);
81
+ default:
82
+ rb_raise( rb_eTypeError, "wrong data for BinaryFloat converter in tuple %d field %d length %d", tuple, field, len);
83
+ }
84
+ }
85
+
86
+ /*
87
+ * Document-class: PG::BinaryDecoder::Bytea < PG::SimpleDecoder
88
+ *
89
+ * This decoder class delivers the data received from the server as binary String object.
90
+ * It is therefore suitable for conversion of PostgreSQL +bytea+ data as well as any other
91
+ * data in binary format.
92
+ *
93
+ */
94
+ VALUE
95
+ pg_bin_dec_bytea(t_pg_coder *conv, const char *val, int len, int tuple, int field, int enc_idx)
96
+ {
97
+ VALUE ret;
98
+ ret = rb_str_new( val, len );
99
+ PG_ENCODING_SET_NOCHECK( ret, rb_ascii8bit_encindex() );
100
+ return ret;
101
+ }
102
+
103
+ /*
104
+ * Document-class: PG::BinaryDecoder::ToBase64 < PG::CompositeDecoder
105
+ *
106
+ * This is a decoder class for conversion of binary +bytea+ to base64 data.
107
+ *
108
+ */
109
+ static VALUE
110
+ pg_bin_dec_to_base64(t_pg_coder *conv, const char *val, int len, int tuple, int field, int enc_idx)
111
+ {
112
+ t_pg_composite_coder *this = (t_pg_composite_coder *)conv;
113
+ t_pg_coder_dec_func dec_func = pg_coder_dec_func(this->elem, this->comp.format);
114
+ int encoded_len = BASE64_ENCODED_SIZE(len);
115
+ /* create a buffer of the encoded length */
116
+ VALUE out_value = rb_str_new(NULL, encoded_len);
117
+
118
+ base64_encode( RSTRING_PTR(out_value), val, len );
119
+
120
+ /* Is it a pure String conversion? Then we can directly send out_value to the user. */
121
+ if( this->comp.format == 0 && dec_func == pg_text_dec_string ){
122
+ PG_ENCODING_SET_NOCHECK( out_value, enc_idx );
123
+ return out_value;
124
+ }
125
+ if( this->comp.format == 1 && dec_func == pg_bin_dec_bytea ){
126
+ PG_ENCODING_SET_NOCHECK( out_value, rb_ascii8bit_encindex() );
127
+ return out_value;
128
+ }
129
+ out_value = dec_func(this->elem, RSTRING_PTR(out_value), encoded_len, tuple, field, enc_idx);
130
+
131
+ return out_value;
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
+
198
+ /*
199
+ * Document-class: PG::BinaryDecoder::String < PG::SimpleDecoder
200
+ *
201
+ * This is a decoder class for conversion of PostgreSQL text output to
202
+ * to Ruby String object. The output value will have the character encoding
203
+ * set with PG::Connection#internal_encoding= .
204
+ *
205
+ */
206
+
207
+ void
208
+ init_pg_binary_decoder()
209
+ {
210
+ /* This module encapsulates all decoder classes with binary input format */
211
+ rb_mPG_BinaryDecoder = rb_define_module_under( rb_mPG, "BinaryDecoder" );
212
+
213
+ /* Make RDoc aware of the decoder classes... */
214
+ /* dummy = rb_define_class_under( rb_mPG_BinaryDecoder, "Boolean", rb_cPG_SimpleDecoder ); */
215
+ pg_define_coder( "Boolean", pg_bin_dec_boolean, rb_cPG_SimpleDecoder, rb_mPG_BinaryDecoder );
216
+ /* dummy = rb_define_class_under( rb_mPG_BinaryDecoder, "Integer", rb_cPG_SimpleDecoder ); */
217
+ pg_define_coder( "Integer", pg_bin_dec_integer, rb_cPG_SimpleDecoder, rb_mPG_BinaryDecoder );
218
+ /* dummy = rb_define_class_under( rb_mPG_BinaryDecoder, "Float", rb_cPG_SimpleDecoder ); */
219
+ pg_define_coder( "Float", pg_bin_dec_float, rb_cPG_SimpleDecoder, rb_mPG_BinaryDecoder );
220
+ /* dummy = rb_define_class_under( rb_mPG_BinaryDecoder, "String", rb_cPG_SimpleDecoder ); */
221
+ pg_define_coder( "String", pg_text_dec_string, rb_cPG_SimpleDecoder, rb_mPG_BinaryDecoder );
222
+ /* dummy = rb_define_class_under( rb_mPG_BinaryDecoder, "Bytea", rb_cPG_SimpleDecoder ); */
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 );
226
+
227
+ /* dummy = rb_define_class_under( rb_mPG_BinaryDecoder, "ToBase64", rb_cPG_CompositeDecoder ); */
228
+ pg_define_coder( "ToBase64", pg_bin_dec_to_base64, rb_cPG_CompositeDecoder, rb_mPG_BinaryDecoder );
229
+ }