pg 1.1.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (77) hide show
  1. checksums.yaml +7 -0
  2. checksums.yaml.gz.sig +3 -0
  3. data.tar.gz.sig +0 -0
  4. data/.gemtest +0 -0
  5. data/BSDL +22 -0
  6. data/ChangeLog +6595 -0
  7. data/Contributors.rdoc +46 -0
  8. data/History.rdoc +492 -0
  9. data/LICENSE +56 -0
  10. data/Manifest.txt +72 -0
  11. data/POSTGRES +23 -0
  12. data/README-OS_X.rdoc +68 -0
  13. data/README-Windows.rdoc +56 -0
  14. data/README.ja.rdoc +14 -0
  15. data/README.rdoc +178 -0
  16. data/Rakefile +215 -0
  17. data/Rakefile.cross +298 -0
  18. data/ext/errorcodes.def +968 -0
  19. data/ext/errorcodes.rb +45 -0
  20. data/ext/errorcodes.txt +478 -0
  21. data/ext/extconf.rb +94 -0
  22. data/ext/gvl_wrappers.c +17 -0
  23. data/ext/gvl_wrappers.h +241 -0
  24. data/ext/pg.c +640 -0
  25. data/ext/pg.h +365 -0
  26. data/ext/pg_binary_decoder.c +229 -0
  27. data/ext/pg_binary_encoder.c +162 -0
  28. data/ext/pg_coder.c +549 -0
  29. data/ext/pg_connection.c +4252 -0
  30. data/ext/pg_copy_coder.c +596 -0
  31. data/ext/pg_errors.c +95 -0
  32. data/ext/pg_result.c +1501 -0
  33. data/ext/pg_text_decoder.c +981 -0
  34. data/ext/pg_text_encoder.c +682 -0
  35. data/ext/pg_tuple.c +541 -0
  36. data/ext/pg_type_map.c +166 -0
  37. data/ext/pg_type_map_all_strings.c +116 -0
  38. data/ext/pg_type_map_by_class.c +239 -0
  39. data/ext/pg_type_map_by_column.c +312 -0
  40. data/ext/pg_type_map_by_mri_type.c +284 -0
  41. data/ext/pg_type_map_by_oid.c +355 -0
  42. data/ext/pg_type_map_in_ruby.c +299 -0
  43. data/ext/util.c +149 -0
  44. data/ext/util.h +65 -0
  45. data/ext/vc/pg.sln +26 -0
  46. data/ext/vc/pg_18/pg.vcproj +216 -0
  47. data/ext/vc/pg_19/pg_19.vcproj +209 -0
  48. data/lib/pg.rb +74 -0
  49. data/lib/pg/basic_type_mapping.rb +459 -0
  50. data/lib/pg/binary_decoder.rb +22 -0
  51. data/lib/pg/coder.rb +83 -0
  52. data/lib/pg/connection.rb +291 -0
  53. data/lib/pg/constants.rb +11 -0
  54. data/lib/pg/exceptions.rb +11 -0
  55. data/lib/pg/result.rb +31 -0
  56. data/lib/pg/text_decoder.rb +47 -0
  57. data/lib/pg/text_encoder.rb +69 -0
  58. data/lib/pg/tuple.rb +30 -0
  59. data/lib/pg/type_map_by_column.rb +15 -0
  60. data/spec/data/expected_trace.out +26 -0
  61. data/spec/data/random_binary_data +0 -0
  62. data/spec/helpers.rb +380 -0
  63. data/spec/pg/basic_type_mapping_spec.rb +508 -0
  64. data/spec/pg/connection_spec.rb +1872 -0
  65. data/spec/pg/connection_sync_spec.rb +41 -0
  66. data/spec/pg/result_spec.rb +491 -0
  67. data/spec/pg/tuple_spec.rb +280 -0
  68. data/spec/pg/type_map_by_class_spec.rb +138 -0
  69. data/spec/pg/type_map_by_column_spec.rb +222 -0
  70. data/spec/pg/type_map_by_mri_type_spec.rb +136 -0
  71. data/spec/pg/type_map_by_oid_spec.rb +149 -0
  72. data/spec/pg/type_map_in_ruby_spec.rb +164 -0
  73. data/spec/pg/type_map_spec.rb +22 -0
  74. data/spec/pg/type_spec.rb +949 -0
  75. data/spec/pg_spec.rb +50 -0
  76. metadata +322 -0
  77. metadata.gz.sig +0 -0
@@ -0,0 +1,365 @@
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
+ #endif
15
+ #if defined(HAVE_UNISTD_H) && !defined(_WIN32)
16
+ # include <unistd.h>
17
+ #endif /* HAVE_UNISTD_H */
18
+
19
+ /* Ruby headers */
20
+ #include "ruby.h"
21
+ #include "ruby/st.h"
22
+ #include "ruby/encoding.h"
23
+
24
+ #define PG_ENCODING_SET_NOCHECK(obj,i) \
25
+ do { \
26
+ if ((i) < ENCODING_INLINE_MAX) \
27
+ ENCODING_SET_INLINED((obj), (i)); \
28
+ else \
29
+ rb_enc_set_index((obj), (i)); \
30
+ } while(0)
31
+
32
+ #include "ruby/io.h"
33
+
34
+ #ifndef timeradd
35
+ #define timeradd(a, b, result) \
36
+ do { \
37
+ (result)->tv_sec = (a)->tv_sec + (b)->tv_sec; \
38
+ (result)->tv_usec = (a)->tv_usec + (b)->tv_usec; \
39
+ if ((result)->tv_usec >= 1000000L) { \
40
+ ++(result)->tv_sec; \
41
+ (result)->tv_usec -= 1000000L; \
42
+ } \
43
+ } while (0)
44
+ #endif
45
+
46
+ #ifndef timersub
47
+ #define timersub(a, b, result) \
48
+ do { \
49
+ (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
50
+ (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
51
+ if ((result)->tv_usec < 0) { \
52
+ --(result)->tv_sec; \
53
+ (result)->tv_usec += 1000000L; \
54
+ } \
55
+ } while (0)
56
+ #endif
57
+
58
+ /* PostgreSQL headers */
59
+ #include "libpq-fe.h"
60
+ #include "libpq/libpq-fs.h" /* large-object interface */
61
+ #include "pg_config_manual.h"
62
+
63
+ #if defined(_WIN32)
64
+ # include <fcntl.h>
65
+ typedef long suseconds_t;
66
+ #endif
67
+
68
+ #if defined(HAVE_VARIABLE_LENGTH_ARRAYS)
69
+ #define PG_VARIABLE_LENGTH_ARRAY(type, name, len, maxlen) type name[(len)];
70
+ #else
71
+ #define PG_VARIABLE_LENGTH_ARRAY(type, name, len, maxlen) \
72
+ type name[(maxlen)] = {(len)>(maxlen) ? (rb_raise(rb_eArgError, "Number of " #name " (%d) exceeds allowed maximum of " #maxlen, (len) ), (type)1) : (type)0};
73
+
74
+ #define PG_MAX_COLUMNS 4000
75
+ #endif
76
+
77
+ #ifndef RARRAY_AREF
78
+ #define RARRAY_AREF(a, i) (RARRAY_PTR(a)[i])
79
+ #endif
80
+
81
+ /* The data behind each PG::Connection object */
82
+ typedef struct {
83
+ PGconn *pgconn;
84
+
85
+ /* Cached IO object for the socket descriptor */
86
+ VALUE socket_io;
87
+ /* Proc object that receives notices as PG::Result objects */
88
+ VALUE notice_receiver;
89
+ /* Proc object that receives notices as String objects */
90
+ VALUE notice_processor;
91
+ /* Kind of PG::TypeMap object for casting query params */
92
+ VALUE type_map_for_queries;
93
+ /* Kind of PG::TypeMap object for casting result values */
94
+ VALUE type_map_for_results;
95
+ /* IO object internally used for the trace stream */
96
+ VALUE trace_stream;
97
+ /* Cached Encoding object */
98
+ VALUE external_encoding;
99
+ /* Kind of PG::Coder object for casting ruby values to COPY rows */
100
+ VALUE encoder_for_put_copy_data;
101
+ /* Kind of PG::Coder object for casting COPY rows to ruby values */
102
+ VALUE decoder_for_get_copy_data;
103
+
104
+ /* enable/disable guessing size of PGresult's allocated memory */
105
+ int guess_result_memsize;
106
+
107
+ #if defined(_WIN32)
108
+ /* File descriptor to be used for rb_w32_unwrap_io_handle() */
109
+ int ruby_sd;
110
+ #endif
111
+ } t_pg_connection;
112
+
113
+ typedef struct pg_coder t_pg_coder;
114
+ typedef struct pg_typemap t_typemap;
115
+
116
+ /* The data behind each PG::Result object */
117
+ typedef struct {
118
+ PGresult *pgresult;
119
+
120
+ /* The connection object used to build this result */
121
+ VALUE connection;
122
+
123
+ /* The TypeMap used to type cast result values */
124
+ VALUE typemap;
125
+
126
+ /* Pointer to the typemap object data. This is assumed to be
127
+ * always valid.
128
+ */
129
+ t_typemap *p_typemap;
130
+
131
+ /* 0 = PGresult is cleared by PG::Result#clear or by the GC
132
+ * 1 = PGresult is cleared internally by libpq
133
+ */
134
+ int autoclear;
135
+
136
+ /* Number of fields in fnames[] .
137
+ * Set to -1 if fnames[] is not yet initialized.
138
+ */
139
+ int nfields;
140
+
141
+ /* Size of PGresult as published to ruby memory management. */
142
+ ssize_t result_size;
143
+
144
+ /* Prefilled tuple Hash with fnames[] as keys. */
145
+ VALUE tuple_hash;
146
+
147
+ /* Hash with fnames[] to field number mapping. */
148
+ VALUE field_map;
149
+
150
+ /* List of field names as frozen String objects.
151
+ * Only valid if nfields != -1
152
+ */
153
+ VALUE fnames[0];
154
+ } t_pg_result;
155
+
156
+
157
+ typedef int (* t_pg_coder_enc_func)(t_pg_coder *, VALUE, char *, VALUE *, int);
158
+ typedef VALUE (* t_pg_coder_dec_func)(t_pg_coder *, const char *, int, int, int, int);
159
+ typedef VALUE (* t_pg_fit_to_result)(VALUE, VALUE);
160
+ typedef VALUE (* t_pg_fit_to_query)(VALUE, VALUE);
161
+ typedef int (* t_pg_fit_to_copy_get)(VALUE);
162
+ typedef VALUE (* t_pg_typecast_result)(t_typemap *, VALUE, int, int);
163
+ typedef t_pg_coder *(* t_pg_typecast_query_param)(t_typemap *, VALUE, int);
164
+ typedef VALUE (* t_pg_typecast_copy_get)( t_typemap *, VALUE, int, int, int );
165
+
166
+ #define PG_CODER_TIMESTAMP_DB_UTC 0x0
167
+ #define PG_CODER_TIMESTAMP_DB_LOCAL 0x1
168
+ #define PG_CODER_TIMESTAMP_APP_UTC 0x0
169
+ #define PG_CODER_TIMESTAMP_APP_LOCAL 0x2
170
+ #define PG_CODER_FORMAT_ERROR_MASK 0xc
171
+ #define PG_CODER_FORMAT_ERROR_TO_RAISE 0x4
172
+ #define PG_CODER_FORMAT_ERROR_TO_STRING 0x8
173
+ #define PG_CODER_FORMAT_ERROR_TO_PARTIAL 0xc
174
+
175
+ struct pg_coder {
176
+ t_pg_coder_enc_func enc_func;
177
+ t_pg_coder_dec_func dec_func;
178
+ VALUE coder_obj;
179
+ Oid oid;
180
+ int format;
181
+ /* OR-ed values out of PG_CODER_* */
182
+ int flags;
183
+ };
184
+
185
+ typedef struct {
186
+ t_pg_coder comp;
187
+ t_pg_coder *elem;
188
+ int needs_quotation;
189
+ char delimiter;
190
+ } t_pg_composite_coder;
191
+
192
+ struct pg_typemap {
193
+ struct pg_typemap_funcs {
194
+ t_pg_fit_to_result fit_to_result;
195
+ t_pg_fit_to_query fit_to_query;
196
+ t_pg_fit_to_copy_get fit_to_copy_get;
197
+ t_pg_typecast_result typecast_result_value;
198
+ t_pg_typecast_query_param typecast_query_param;
199
+ t_pg_typecast_copy_get typecast_copy_get;
200
+ } funcs;
201
+ VALUE default_typemap;
202
+ };
203
+
204
+ typedef struct {
205
+ t_typemap typemap;
206
+ int nfields;
207
+ struct pg_tmbc_converter {
208
+ t_pg_coder *cconv;
209
+ } convs[0];
210
+ } t_tmbc;
211
+
212
+
213
+ #include "gvl_wrappers.h"
214
+
215
+ /***************************************************************************
216
+ * Globals
217
+ **************************************************************************/
218
+
219
+ extern int pg_skip_deprecation_warning;
220
+ extern VALUE rb_mPG;
221
+ extern VALUE rb_ePGerror;
222
+ extern VALUE rb_eServerError;
223
+ extern VALUE rb_eUnableToSend;
224
+ extern VALUE rb_eConnectionBad;
225
+ extern VALUE rb_eInvalidResultStatus;
226
+ extern VALUE rb_eNoResultError;
227
+ extern VALUE rb_eInvalidChangeOfResultFields;
228
+ extern VALUE rb_mPGconstants;
229
+ extern VALUE rb_cPGconn;
230
+ extern VALUE rb_cPGresult;
231
+ extern VALUE rb_hErrors;
232
+ extern VALUE rb_cTypeMap;
233
+ extern VALUE rb_cTypeMapAllStrings;
234
+ extern VALUE rb_mDefaultTypeMappable;
235
+ extern VALUE rb_cPG_Coder;
236
+ extern VALUE rb_cPG_SimpleEncoder;
237
+ extern VALUE rb_cPG_SimpleDecoder;
238
+ extern VALUE rb_cPG_CompositeEncoder;
239
+ extern VALUE rb_cPG_CompositeDecoder;
240
+ extern VALUE rb_cPG_CopyCoder;
241
+ extern VALUE rb_cPG_CopyEncoder;
242
+ extern VALUE rb_cPG_CopyDecoder;
243
+ extern VALUE rb_mPG_TextEncoder;
244
+ extern VALUE rb_mPG_TextDecoder;
245
+ extern VALUE rb_mPG_BinaryEncoder;
246
+ extern VALUE rb_mPG_BinaryDecoder;
247
+ extern VALUE rb_mPG_BinaryFormatting;
248
+ extern const struct pg_typemap_funcs pg_tmbc_funcs;
249
+ extern const struct pg_typemap_funcs pg_typemap_funcs;
250
+
251
+ extern VALUE pg_typemap_all_strings;
252
+
253
+ /***************************************************************************
254
+ * MACROS
255
+ **************************************************************************/
256
+
257
+ #define UNUSED(x) ((void)(x))
258
+ #define SINGLETON_ALIAS(klass,new,old) rb_define_alias(rb_singleton_class((klass)),(new),(old))
259
+
260
+
261
+ /***************************************************************************
262
+ * PROTOTYPES
263
+ **************************************************************************/
264
+ void Init_pg_ext _(( void ));
265
+
266
+ void init_pg_connection _(( void ));
267
+ void init_pg_result _(( void ));
268
+ void init_pg_errors _(( void ));
269
+ void init_pg_type_map _(( void ));
270
+ void init_pg_type_map_all_strings _(( void ));
271
+ void init_pg_type_map_by_class _(( void ));
272
+ void init_pg_type_map_by_column _(( void ));
273
+ void init_pg_type_map_by_mri_type _(( void ));
274
+ void init_pg_type_map_by_oid _(( void ));
275
+ void init_pg_type_map_in_ruby _(( void ));
276
+ void init_pg_coder _(( void ));
277
+ void init_pg_copycoder _(( void ));
278
+ void init_pg_text_encoder _(( void ));
279
+ void init_pg_text_decoder _(( void ));
280
+ void init_pg_binary_encoder _(( void ));
281
+ void init_pg_binary_decoder _(( void ));
282
+ void init_pg_tuple _(( void ));
283
+ VALUE lookup_error_class _(( const char * ));
284
+ VALUE pg_bin_dec_bytea _(( t_pg_coder*, const char *, int, int, int, int ));
285
+ VALUE pg_text_dec_string _(( t_pg_coder*, const char *, int, int, int, int ));
286
+ int pg_coder_enc_to_s _(( t_pg_coder*, VALUE, char *, VALUE *, int));
287
+ int pg_text_enc_identifier _(( t_pg_coder*, VALUE, char *, VALUE *, int));
288
+ t_pg_coder_enc_func pg_coder_enc_func _(( t_pg_coder* ));
289
+ t_pg_coder_dec_func pg_coder_dec_func _(( t_pg_coder*, int ));
290
+ void pg_define_coder _(( const char *, void *, VALUE, VALUE ));
291
+ VALUE pg_obj_to_i _(( VALUE ));
292
+ VALUE pg_tmbc_allocate _(( void ));
293
+ void pg_coder_init_encoder _(( VALUE ));
294
+ void pg_coder_init_decoder _(( VALUE ));
295
+ char *pg_rb_str_ensure_capa _(( VALUE, long, char *, char ** ));
296
+
297
+ #define PG_RB_STR_ENSURE_CAPA( str, expand_len, curr_ptr, end_ptr ) \
298
+ do { \
299
+ if( (curr_ptr) + (expand_len) >= (end_ptr) ) \
300
+ (curr_ptr) = pg_rb_str_ensure_capa( (str), (expand_len), (curr_ptr), &(end_ptr) ); \
301
+ } while(0);
302
+
303
+ #define PG_RB_STR_NEW( str, curr_ptr, end_ptr ) ( \
304
+ (str) = rb_str_new( NULL, 0 ), \
305
+ (curr_ptr) = (end_ptr) = RSTRING_PTR(str) \
306
+ )
307
+
308
+ #define PG_RB_TAINTED_STR_NEW( str, curr_ptr, end_ptr ) ( \
309
+ (str) = rb_tainted_str_new( NULL, 0 ), \
310
+ (curr_ptr) = (end_ptr) = RSTRING_PTR(str) \
311
+ )
312
+
313
+ VALUE pg_typemap_fit_to_result _(( VALUE, VALUE ));
314
+ VALUE pg_typemap_fit_to_query _(( VALUE, VALUE ));
315
+ int pg_typemap_fit_to_copy_get _(( VALUE ));
316
+ VALUE pg_typemap_result_value _(( t_typemap *, VALUE, int, int ));
317
+ t_pg_coder *pg_typemap_typecast_query_param _(( t_typemap *, VALUE, int ));
318
+ VALUE pg_typemap_typecast_copy_get _(( t_typemap *, VALUE, int, int, int ));
319
+
320
+ PGconn *pg_get_pgconn _(( VALUE ));
321
+ t_pg_connection *pg_get_connection _(( VALUE ));
322
+
323
+ VALUE pg_new_result _(( PGresult *, VALUE ));
324
+ VALUE pg_new_result_autoclear _(( PGresult *, VALUE ));
325
+ PGresult* pgresult_get _(( VALUE ));
326
+ VALUE pg_result_check _(( VALUE ));
327
+ VALUE pg_result_clear _(( VALUE ));
328
+ VALUE pg_tuple_new _(( VALUE, int ));
329
+
330
+ /*
331
+ * Fetch the data pointer for the result object
332
+ */
333
+ static inline t_pg_result *
334
+ pgresult_get_this( VALUE self )
335
+ {
336
+ t_pg_result *this = RTYPEDDATA_DATA(self);
337
+
338
+ if( this == NULL )
339
+ rb_raise(rb_ePGerror, "result has been cleared");
340
+
341
+ return this;
342
+ }
343
+
344
+
345
+ rb_encoding * pg_get_pg_encoding_as_rb_encoding _(( int ));
346
+ rb_encoding * pg_get_pg_encname_as_rb_encoding _(( const char * ));
347
+ const char * pg_get_rb_encoding_as_pg_encoding _(( rb_encoding * ));
348
+ rb_encoding *pg_conn_enc_get _(( PGconn * ));
349
+
350
+ void notice_receiver_proxy(void *arg, const PGresult *result);
351
+ void notice_processor_proxy(void *arg, const char *message);
352
+
353
+ /* reports if `-W' specified and PG_SKIP_DEPRECATION_WARNING environment variable isn't set
354
+ *
355
+ * message_id identifies the warning, so that it's reported only once.
356
+ */
357
+ #define pg_deprecated(message_id, format_args) \
358
+ do { \
359
+ if( !(pg_skip_deprecation_warning & (1 << message_id)) ){ \
360
+ pg_skip_deprecation_warning |= 1 << message_id; \
361
+ rb_warning format_args; \
362
+ } \
363
+ } while(0);
364
+
365
+ #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 "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_tainted_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_tainted_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
+ }