pg 1.0.0 → 1.5.9
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.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data/Gemfile +20 -0
- data/History.md +932 -0
- data/Manifest.txt +8 -3
- data/README-Windows.rdoc +4 -4
- data/README.ja.md +300 -0
- data/README.md +286 -0
- data/Rakefile +41 -138
- data/Rakefile.cross +71 -66
- data/certs/ged.pem +24 -0
- data/certs/kanis@comcard.de.pem +20 -0
- data/certs/larskanis-2022.pem +26 -0
- data/certs/larskanis-2023.pem +24 -0
- data/certs/larskanis-2024.pem +24 -0
- data/ext/errorcodes.def +84 -5
- data/ext/errorcodes.rb +1 -1
- data/ext/errorcodes.txt +23 -6
- data/ext/extconf.rb +109 -25
- data/ext/gvl_wrappers.c +4 -0
- data/ext/gvl_wrappers.h +23 -0
- data/ext/pg.c +213 -155
- data/ext/pg.h +89 -23
- data/ext/pg_binary_decoder.c +164 -16
- data/ext/pg_binary_encoder.c +238 -13
- data/ext/pg_coder.c +159 -35
- data/ext/pg_connection.c +1584 -967
- data/ext/pg_copy_coder.c +373 -43
- data/ext/pg_errors.c +1 -1
- data/ext/pg_record_coder.c +522 -0
- data/ext/pg_result.c +710 -217
- data/ext/pg_text_decoder.c +630 -43
- data/ext/pg_text_encoder.c +222 -72
- data/ext/pg_tuple.c +572 -0
- data/ext/pg_type_map.c +45 -11
- data/ext/pg_type_map_all_strings.c +21 -7
- data/ext/pg_type_map_by_class.c +59 -27
- data/ext/pg_type_map_by_column.c +80 -37
- data/ext/pg_type_map_by_mri_type.c +49 -20
- data/ext/pg_type_map_by_oid.c +62 -29
- data/ext/pg_type_map_in_ruby.c +56 -22
- data/ext/{util.c → pg_util.c} +12 -12
- data/ext/{util.h → pg_util.h} +2 -2
- data/lib/pg/basic_type_map_based_on_result.rb +67 -0
- data/lib/pg/basic_type_map_for_queries.rb +202 -0
- data/lib/pg/basic_type_map_for_results.rb +104 -0
- data/lib/pg/basic_type_registry.rb +311 -0
- data/lib/pg/binary_decoder/date.rb +9 -0
- data/lib/pg/binary_decoder/timestamp.rb +26 -0
- data/lib/pg/binary_encoder/timestamp.rb +20 -0
- data/lib/pg/coder.rb +36 -13
- data/lib/pg/connection.rb +769 -70
- data/lib/pg/exceptions.rb +22 -2
- data/lib/pg/result.rb +14 -2
- data/lib/pg/text_decoder/date.rb +21 -0
- data/lib/pg/text_decoder/inet.rb +9 -0
- data/lib/pg/text_decoder/json.rb +17 -0
- data/lib/pg/text_decoder/numeric.rb +9 -0
- data/lib/pg/text_decoder/timestamp.rb +30 -0
- data/lib/pg/text_encoder/date.rb +13 -0
- data/lib/pg/text_encoder/inet.rb +31 -0
- data/lib/pg/text_encoder/json.rb +17 -0
- data/lib/pg/text_encoder/numeric.rb +9 -0
- data/lib/pg/text_encoder/timestamp.rb +24 -0
- data/lib/pg/tuple.rb +30 -0
- data/lib/pg/type_map_by_column.rb +3 -2
- data/lib/pg/version.rb +4 -0
- data/lib/pg.rb +106 -39
- data/misc/openssl-pg-segfault.rb +31 -0
- data/misc/postgres/History.txt +9 -0
- data/misc/postgres/Manifest.txt +5 -0
- data/misc/postgres/README.txt +21 -0
- data/misc/postgres/Rakefile +21 -0
- data/misc/postgres/lib/postgres.rb +16 -0
- data/misc/ruby-pg/History.txt +9 -0
- data/misc/ruby-pg/Manifest.txt +5 -0
- data/misc/ruby-pg/README.txt +21 -0
- data/misc/ruby-pg/Rakefile +21 -0
- data/misc/ruby-pg/lib/ruby/pg.rb +16 -0
- data/pg.gemspec +36 -0
- data/rakelib/task_extension.rb +46 -0
- data/sample/array_insert.rb +20 -0
- data/sample/async_api.rb +102 -0
- data/sample/async_copyto.rb +39 -0
- data/sample/async_mixed.rb +56 -0
- data/sample/check_conn.rb +21 -0
- data/sample/copydata.rb +71 -0
- data/sample/copyfrom.rb +81 -0
- data/sample/copyto.rb +19 -0
- data/sample/cursor.rb +21 -0
- data/sample/disk_usage_report.rb +177 -0
- data/sample/issue-119.rb +94 -0
- data/sample/losample.rb +69 -0
- data/sample/minimal-testcase.rb +17 -0
- data/sample/notify_wait.rb +72 -0
- data/sample/pg_statistics.rb +285 -0
- data/sample/replication_monitor.rb +222 -0
- data/sample/test_binary_values.rb +33 -0
- data/sample/wal_shipper.rb +434 -0
- data/sample/warehouse_partitions.rb +311 -0
- data.tar.gz.sig +0 -0
- metadata +138 -223
- metadata.gz.sig +0 -0
- data/.gemtest +0 -0
- data/ChangeLog +0 -6595
- data/History.rdoc +0 -422
- data/README.ja.rdoc +0 -14
- data/README.rdoc +0 -167
- data/lib/pg/basic_type_mapping.rb +0 -426
- data/lib/pg/constants.rb +0 -11
- data/lib/pg/text_decoder.rb +0 -51
- data/lib/pg/text_encoder.rb +0 -35
- data/spec/data/expected_trace.out +0 -26
- data/spec/data/random_binary_data +0 -0
- data/spec/helpers.rb +0 -348
- data/spec/pg/basic_type_mapping_spec.rb +0 -305
- data/spec/pg/connection_spec.rb +0 -1719
- data/spec/pg/result_spec.rb +0 -456
- data/spec/pg/type_map_by_class_spec.rb +0 -138
- data/spec/pg/type_map_by_column_spec.rb +0 -222
- data/spec/pg/type_map_by_mri_type_spec.rb +0 -136
- data/spec/pg/type_map_by_oid_spec.rb +0 -149
- data/spec/pg/type_map_in_ruby_spec.rb +0 -164
- data/spec/pg/type_map_spec.rb +0 -22
- data/spec/pg/type_spec.rb +0 -777
- 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>
|
@@ -21,9 +22,6 @@
|
|
21
22
|
#include "ruby/st.h"
|
22
23
|
#include "ruby/encoding.h"
|
23
24
|
|
24
|
-
/* exported by ruby-1.9.3+ but not declared */
|
25
|
-
extern int rb_encdb_alias(const char *, const char *);
|
26
|
-
|
27
25
|
#define PG_ENCODING_SET_NOCHECK(obj,i) \
|
28
26
|
do { \
|
29
27
|
if ((i) < ENCODING_INLINE_MAX) \
|
@@ -59,6 +57,7 @@ extern int rb_encdb_alias(const char *, const char *);
|
|
59
57
|
#endif
|
60
58
|
|
61
59
|
/* PostgreSQL headers */
|
60
|
+
#include "pg_config.h"
|
62
61
|
#include "libpq-fe.h"
|
63
62
|
#include "libpq/libpq-fs.h" /* large-object interface */
|
64
63
|
#include "pg_config_manual.h"
|
@@ -77,12 +76,32 @@ typedef long suseconds_t;
|
|
77
76
|
#define PG_MAX_COLUMNS 4000
|
78
77
|
#endif
|
79
78
|
|
79
|
+
#ifdef HAVE_RB_GC_MARK_MOVABLE
|
80
|
+
#define pg_compact_callback(x) (x)
|
81
|
+
#define pg_gc_location(x) x = rb_gc_location(x)
|
82
|
+
#else
|
83
|
+
#define rb_gc_mark_movable(x) rb_gc_mark(x)
|
84
|
+
#define pg_compact_callback(x) {(x)}
|
85
|
+
#define pg_gc_location(x) UNUSED(x)
|
86
|
+
#endif
|
87
|
+
|
88
|
+
/* For compatibility with ruby < 3.0 */
|
89
|
+
#ifndef RUBY_TYPED_FROZEN_SHAREABLE
|
90
|
+
#define PG_RUBY_TYPED_FROZEN_SHAREABLE 0
|
91
|
+
#else
|
92
|
+
#define PG_RUBY_TYPED_FROZEN_SHAREABLE RUBY_TYPED_FROZEN_SHAREABLE
|
93
|
+
#endif
|
94
|
+
#define PG_ENC_IDX_BITS 28
|
95
|
+
|
80
96
|
/* The data behind each PG::Connection object */
|
81
97
|
typedef struct {
|
82
98
|
PGconn *pgconn;
|
83
99
|
|
84
100
|
/* Cached IO object for the socket descriptor */
|
85
101
|
VALUE socket_io;
|
102
|
+
/* function pointers of the original libpq notice receivers */
|
103
|
+
PQnoticeReceiver default_notice_receiver;
|
104
|
+
PQnoticeProcessor default_notice_processor;
|
86
105
|
/* Proc object that receives notices as PG::Result objects */
|
87
106
|
VALUE notice_receiver;
|
88
107
|
/* Proc object that receives notices as String objects */
|
@@ -93,13 +112,21 @@ typedef struct {
|
|
93
112
|
VALUE type_map_for_results;
|
94
113
|
/* IO object internally used for the trace stream */
|
95
114
|
VALUE trace_stream;
|
96
|
-
/* Cached Encoding object */
|
97
|
-
VALUE external_encoding;
|
98
115
|
/* Kind of PG::Coder object for casting ruby values to COPY rows */
|
99
116
|
VALUE encoder_for_put_copy_data;
|
100
117
|
/* Kind of PG::Coder object for casting COPY rows to ruby values */
|
101
118
|
VALUE decoder_for_get_copy_data;
|
119
|
+
/* Ruby encoding index of the client/internal encoding */
|
120
|
+
int enc_idx : PG_ENC_IDX_BITS;
|
121
|
+
/* flags controlling Symbol/String field names */
|
122
|
+
unsigned int flags : 2;
|
123
|
+
/* enable automatic flushing of send data at the end of send_query calls */
|
124
|
+
unsigned int flush_data : 1;
|
102
125
|
|
126
|
+
#if defined(_WIN32)
|
127
|
+
/* File descriptor to be used for rb_w32_unwrap_io_handle() */
|
128
|
+
int ruby_sd;
|
129
|
+
#endif
|
103
130
|
} t_pg_connection;
|
104
131
|
|
105
132
|
typedef struct pg_coder t_pg_coder;
|
@@ -120,20 +147,32 @@ typedef struct {
|
|
120
147
|
*/
|
121
148
|
t_typemap *p_typemap;
|
122
149
|
|
150
|
+
/* Ruby encoding index of the client/internal encoding */
|
151
|
+
int enc_idx : PG_ENC_IDX_BITS;
|
152
|
+
|
123
153
|
/* 0 = PGresult is cleared by PG::Result#clear or by the GC
|
124
154
|
* 1 = PGresult is cleared internally by libpq
|
125
155
|
*/
|
126
|
-
int autoclear;
|
156
|
+
unsigned int autoclear : 1;
|
157
|
+
|
158
|
+
/* flags controlling Symbol/String field names */
|
159
|
+
unsigned int flags : 2;
|
127
160
|
|
128
161
|
/* Number of fields in fnames[] .
|
129
162
|
* Set to -1 if fnames[] is not yet initialized.
|
130
163
|
*/
|
131
164
|
int nfields;
|
132
165
|
|
166
|
+
/* Size of PGresult as published to ruby memory management. */
|
167
|
+
ssize_t result_size;
|
168
|
+
|
133
169
|
/* Prefilled tuple Hash with fnames[] as keys. */
|
134
170
|
VALUE tuple_hash;
|
135
171
|
|
136
|
-
/*
|
172
|
+
/* Hash with fnames[] to field number mapping. */
|
173
|
+
VALUE field_map;
|
174
|
+
|
175
|
+
/* List of field names as frozen String or Symbol objects.
|
137
176
|
* Only valid if nfields != -1
|
138
177
|
*/
|
139
178
|
VALUE fnames[0];
|
@@ -141,7 +180,7 @@ typedef struct {
|
|
141
180
|
|
142
181
|
|
143
182
|
typedef int (* t_pg_coder_enc_func)(t_pg_coder *, VALUE, char *, VALUE *, int);
|
144
|
-
typedef VALUE (* t_pg_coder_dec_func)(t_pg_coder *, char *, int, int, int, int);
|
183
|
+
typedef VALUE (* t_pg_coder_dec_func)(t_pg_coder *, const char *, int, int, int, int);
|
145
184
|
typedef VALUE (* t_pg_fit_to_result)(VALUE, VALUE);
|
146
185
|
typedef VALUE (* t_pg_fit_to_query)(VALUE, VALUE);
|
147
186
|
typedef int (* t_pg_fit_to_copy_get)(VALUE);
|
@@ -149,12 +188,27 @@ typedef VALUE (* t_pg_typecast_result)(t_typemap *, VALUE, int, int);
|
|
149
188
|
typedef t_pg_coder *(* t_pg_typecast_query_param)(t_typemap *, VALUE, int);
|
150
189
|
typedef VALUE (* t_pg_typecast_copy_get)( t_typemap *, VALUE, int, int, int );
|
151
190
|
|
191
|
+
#define PG_RESULT_FIELD_NAMES_MASK 0x03
|
192
|
+
#define PG_RESULT_FIELD_NAMES_SYMBOL 0x01
|
193
|
+
#define PG_RESULT_FIELD_NAMES_STATIC_SYMBOL 0x02
|
194
|
+
|
195
|
+
#define PG_CODER_TIMESTAMP_DB_UTC 0x0
|
196
|
+
#define PG_CODER_TIMESTAMP_DB_LOCAL 0x1
|
197
|
+
#define PG_CODER_TIMESTAMP_APP_UTC 0x0
|
198
|
+
#define PG_CODER_TIMESTAMP_APP_LOCAL 0x2
|
199
|
+
#define PG_CODER_FORMAT_ERROR_MASK 0xc
|
200
|
+
#define PG_CODER_FORMAT_ERROR_TO_RAISE 0x4
|
201
|
+
#define PG_CODER_FORMAT_ERROR_TO_STRING 0x8
|
202
|
+
#define PG_CODER_FORMAT_ERROR_TO_PARTIAL 0xc
|
203
|
+
|
152
204
|
struct pg_coder {
|
153
205
|
t_pg_coder_enc_func enc_func;
|
154
206
|
t_pg_coder_dec_func dec_func;
|
155
207
|
VALUE coder_obj;
|
156
208
|
Oid oid;
|
157
209
|
int format;
|
210
|
+
/* OR-ed values out of PG_CODER_* */
|
211
|
+
int flags;
|
158
212
|
};
|
159
213
|
|
160
214
|
typedef struct {
|
@@ -184,6 +238,8 @@ typedef struct {
|
|
184
238
|
} convs[0];
|
185
239
|
} t_tmbc;
|
186
240
|
|
241
|
+
extern const rb_data_type_t pg_typemap_type;
|
242
|
+
extern const rb_data_type_t pg_coder_type;
|
187
243
|
|
188
244
|
#include "gvl_wrappers.h"
|
189
245
|
|
@@ -191,6 +247,7 @@ typedef struct {
|
|
191
247
|
* Globals
|
192
248
|
**************************************************************************/
|
193
249
|
|
250
|
+
extern int pg_skip_deprecation_warning;
|
194
251
|
extern VALUE rb_mPG;
|
195
252
|
extern VALUE rb_ePGerror;
|
196
253
|
extern VALUE rb_eServerError;
|
@@ -249,22 +306,25 @@ void init_pg_type_map_by_oid _(( void ));
|
|
249
306
|
void init_pg_type_map_in_ruby _(( void ));
|
250
307
|
void init_pg_coder _(( void ));
|
251
308
|
void init_pg_copycoder _(( void ));
|
309
|
+
void init_pg_recordcoder _(( void ));
|
252
310
|
void init_pg_text_encoder _(( void ));
|
253
311
|
void init_pg_text_decoder _(( void ));
|
254
312
|
void init_pg_binary_encoder _(( void ));
|
255
313
|
void init_pg_binary_decoder _(( void ));
|
314
|
+
void init_pg_tuple _(( void ));
|
256
315
|
VALUE lookup_error_class _(( const char * ));
|
257
|
-
VALUE pg_bin_dec_bytea _(( t_pg_coder*, char *, int, int, int, int ));
|
258
|
-
VALUE pg_text_dec_string _(( t_pg_coder*, char *, int, int, int, int ));
|
316
|
+
VALUE pg_bin_dec_bytea _(( t_pg_coder*, const char *, int, int, int, int ));
|
317
|
+
VALUE pg_text_dec_string _(( t_pg_coder*, const char *, int, int, int, int ));
|
259
318
|
int pg_coder_enc_to_s _(( t_pg_coder*, VALUE, char *, VALUE *, int));
|
260
319
|
int pg_text_enc_identifier _(( t_pg_coder*, VALUE, char *, VALUE *, int));
|
261
320
|
t_pg_coder_enc_func pg_coder_enc_func _(( t_pg_coder* ));
|
262
321
|
t_pg_coder_dec_func pg_coder_dec_func _(( t_pg_coder*, int ));
|
263
|
-
|
322
|
+
VALUE pg_define_coder _(( const char *, void *, VALUE, VALUE ));
|
264
323
|
VALUE pg_obj_to_i _(( VALUE ));
|
265
324
|
VALUE pg_tmbc_allocate _(( void ));
|
266
325
|
void pg_coder_init_encoder _(( VALUE ));
|
267
326
|
void pg_coder_init_decoder _(( VALUE ));
|
327
|
+
void pg_coder_compact _(( void * ));
|
268
328
|
char *pg_rb_str_ensure_capa _(( VALUE, long, char *, char ** ));
|
269
329
|
|
270
330
|
#define PG_RB_STR_ENSURE_CAPA( str, expand_len, curr_ptr, end_ptr ) \
|
@@ -278,26 +338,26 @@ char *pg_rb_str_ensure_capa _(( VALUE, long, char *,
|
|
278
338
|
(curr_ptr) = (end_ptr) = RSTRING_PTR(str) \
|
279
339
|
)
|
280
340
|
|
281
|
-
#define PG_RB_TAINTED_STR_NEW( str, curr_ptr, end_ptr ) ( \
|
282
|
-
(str) = rb_tainted_str_new( NULL, 0 ), \
|
283
|
-
(curr_ptr) = (end_ptr) = RSTRING_PTR(str) \
|
284
|
-
)
|
285
|
-
|
286
341
|
VALUE pg_typemap_fit_to_result _(( VALUE, VALUE ));
|
287
342
|
VALUE pg_typemap_fit_to_query _(( VALUE, VALUE ));
|
288
343
|
int pg_typemap_fit_to_copy_get _(( VALUE ));
|
289
344
|
VALUE pg_typemap_result_value _(( t_typemap *, VALUE, int, int ));
|
290
345
|
t_pg_coder *pg_typemap_typecast_query_param _(( t_typemap *, VALUE, int ));
|
291
346
|
VALUE pg_typemap_typecast_copy_get _(( t_typemap *, VALUE, int, int, int ));
|
347
|
+
void pg_typemap_mark _(( void * ));
|
348
|
+
size_t pg_typemap_memsize _(( const void * ));
|
349
|
+
void pg_typemap_compact _(( void * ));
|
292
350
|
|
293
351
|
PGconn *pg_get_pgconn _(( VALUE ));
|
294
352
|
t_pg_connection *pg_get_connection _(( VALUE ));
|
353
|
+
VALUE pgconn_block _(( int, VALUE *, VALUE ));
|
295
354
|
|
296
355
|
VALUE pg_new_result _(( PGresult *, VALUE ));
|
297
356
|
VALUE pg_new_result_autoclear _(( PGresult *, VALUE ));
|
298
357
|
PGresult* pgresult_get _(( VALUE ));
|
299
358
|
VALUE pg_result_check _(( VALUE ));
|
300
359
|
VALUE pg_result_clear _(( VALUE ));
|
360
|
+
VALUE pg_tuple_new _(( VALUE, int ));
|
301
361
|
|
302
362
|
/*
|
303
363
|
* Fetch the data pointer for the result object
|
@@ -305,16 +365,10 @@ VALUE pg_result_clear _(( VALUE ));
|
|
305
365
|
static inline t_pg_result *
|
306
366
|
pgresult_get_this( VALUE self )
|
307
367
|
{
|
308
|
-
|
309
|
-
|
310
|
-
if( this == NULL )
|
311
|
-
rb_raise(rb_ePGerror, "result has been cleared");
|
312
|
-
|
313
|
-
return this;
|
368
|
+
return RTYPEDDATA_DATA(self);
|
314
369
|
}
|
315
370
|
|
316
371
|
|
317
|
-
rb_encoding * pg_get_pg_encoding_as_rb_encoding _(( int ));
|
318
372
|
rb_encoding * pg_get_pg_encname_as_rb_encoding _(( const char * ));
|
319
373
|
const char * pg_get_rb_encoding_as_pg_encoding _(( rb_encoding * ));
|
320
374
|
rb_encoding *pg_conn_enc_get _(( PGconn * ));
|
@@ -322,4 +376,16 @@ rb_encoding *pg_conn_enc_get _(( PGconn * ));
|
|
322
376
|
void notice_receiver_proxy(void *arg, const PGresult *result);
|
323
377
|
void notice_processor_proxy(void *arg, const char *message);
|
324
378
|
|
379
|
+
/* reports if `-W' specified and PG_SKIP_DEPRECATION_WARNING environment variable isn't set
|
380
|
+
*
|
381
|
+
* message_id identifies the warning, so that it's reported only once.
|
382
|
+
*/
|
383
|
+
#define pg_deprecated(message_id, format_args) \
|
384
|
+
do { \
|
385
|
+
if( !(pg_skip_deprecation_warning & (1 << message_id)) ){ \
|
386
|
+
pg_skip_deprecation_warning |= 1 << message_id; \
|
387
|
+
rb_warning format_args; \
|
388
|
+
} \
|
389
|
+
} while(0);
|
390
|
+
|
325
391
|
#endif /* end __pg_h */
|
data/ext/pg_binary_decoder.c
CHANGED
@@ -1,27 +1,30 @@
|
|
1
1
|
/*
|
2
2
|
* pg_column_map.c - PG::ColumnMap class extension
|
3
|
-
* $Id
|
3
|
+
* $Id$
|
4
4
|
*
|
5
5
|
*/
|
6
6
|
|
7
|
+
#include "ruby/version.h"
|
7
8
|
#include "pg.h"
|
8
|
-
#include "
|
9
|
+
#include "pg_util.h"
|
9
10
|
#ifdef HAVE_INTTYPES_H
|
10
11
|
#include <inttypes.h>
|
11
12
|
#endif
|
12
13
|
|
13
14
|
VALUE rb_mPG_BinaryDecoder;
|
15
|
+
static VALUE s_Date;
|
16
|
+
static ID s_id_new;
|
14
17
|
|
15
18
|
|
16
19
|
/*
|
17
20
|
* Document-class: PG::BinaryDecoder::Boolean < PG::SimpleDecoder
|
18
21
|
*
|
19
|
-
* This is a decoder class for conversion of PostgreSQL binary bool type
|
20
|
-
* to Ruby true or false objects.
|
22
|
+
* This is a decoder class for conversion of PostgreSQL binary +bool+ type
|
23
|
+
* to Ruby +true+ or +false+ objects.
|
21
24
|
*
|
22
25
|
*/
|
23
26
|
static VALUE
|
24
|
-
pg_bin_dec_boolean(t_pg_coder *conv, char *val, int len, int tuple, int field, int enc_idx)
|
27
|
+
pg_bin_dec_boolean(t_pg_coder *conv, const char *val, int len, int tuple, int field, int enc_idx)
|
25
28
|
{
|
26
29
|
if (len < 1) {
|
27
30
|
rb_raise( rb_eTypeError, "wrong data for binary boolean converter in tuple %d field %d", tuple, field);
|
@@ -32,12 +35,12 @@ pg_bin_dec_boolean(t_pg_coder *conv, char *val, int len, int tuple, int field, i
|
|
32
35
|
/*
|
33
36
|
* Document-class: PG::BinaryDecoder::Integer < PG::SimpleDecoder
|
34
37
|
*
|
35
|
-
* This is a decoder class for conversion of PostgreSQL binary int2
|
38
|
+
* This is a decoder class for conversion of PostgreSQL binary +int2+, +int4+ and +int8+ types
|
36
39
|
* to Ruby Integer objects.
|
37
40
|
*
|
38
41
|
*/
|
39
42
|
static VALUE
|
40
|
-
pg_bin_dec_integer(t_pg_coder *conv, char *val, int len, int tuple, int field, int enc_idx)
|
43
|
+
pg_bin_dec_integer(t_pg_coder *conv, const char *val, int len, int tuple, int field, int enc_idx)
|
41
44
|
{
|
42
45
|
switch( len ){
|
43
46
|
case 2:
|
@@ -54,12 +57,12 @@ pg_bin_dec_integer(t_pg_coder *conv, char *val, int len, int tuple, int field, i
|
|
54
57
|
/*
|
55
58
|
* Document-class: PG::BinaryDecoder::Float < PG::SimpleDecoder
|
56
59
|
*
|
57
|
-
* This is a decoder class for conversion of PostgreSQL binary float4 and float8 types
|
60
|
+
* This is a decoder class for conversion of PostgreSQL binary +float4+ and +float8+ types
|
58
61
|
* to Ruby Float objects.
|
59
62
|
*
|
60
63
|
*/
|
61
64
|
static VALUE
|
62
|
-
pg_bin_dec_float(t_pg_coder *conv, char *val, int len, int tuple, int field, int enc_idx)
|
65
|
+
pg_bin_dec_float(t_pg_coder *conv, const char *val, int len, int tuple, int field, int enc_idx)
|
63
66
|
{
|
64
67
|
union {
|
65
68
|
float f;
|
@@ -86,15 +89,15 @@ pg_bin_dec_float(t_pg_coder *conv, char *val, int len, int tuple, int field, int
|
|
86
89
|
* Document-class: PG::BinaryDecoder::Bytea < PG::SimpleDecoder
|
87
90
|
*
|
88
91
|
* 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
|
92
|
+
* It is therefore suitable for conversion of PostgreSQL +bytea+ data as well as any other
|
90
93
|
* data in binary format.
|
91
94
|
*
|
92
95
|
*/
|
93
96
|
VALUE
|
94
|
-
pg_bin_dec_bytea(t_pg_coder *conv, char *val, int len, int tuple, int field, int enc_idx)
|
97
|
+
pg_bin_dec_bytea(t_pg_coder *conv, const char *val, int len, int tuple, int field, int enc_idx)
|
95
98
|
{
|
96
99
|
VALUE ret;
|
97
|
-
ret =
|
100
|
+
ret = rb_str_new( val, len );
|
98
101
|
PG_ENCODING_SET_NOCHECK( ret, rb_ascii8bit_encindex() );
|
99
102
|
return ret;
|
100
103
|
}
|
@@ -102,17 +105,17 @@ pg_bin_dec_bytea(t_pg_coder *conv, char *val, int len, int tuple, int field, int
|
|
102
105
|
/*
|
103
106
|
* Document-class: PG::BinaryDecoder::ToBase64 < PG::CompositeDecoder
|
104
107
|
*
|
105
|
-
* This is a decoder class for conversion of binary
|
108
|
+
* This is a decoder class for conversion of binary +bytea+ to base64 data.
|
106
109
|
*
|
107
110
|
*/
|
108
111
|
static VALUE
|
109
|
-
pg_bin_dec_to_base64(t_pg_coder *conv, char *val, int len, int tuple, int field, int enc_idx)
|
112
|
+
pg_bin_dec_to_base64(t_pg_coder *conv, const char *val, int len, int tuple, int field, int enc_idx)
|
110
113
|
{
|
111
114
|
t_pg_composite_coder *this = (t_pg_composite_coder *)conv;
|
112
115
|
t_pg_coder_dec_func dec_func = pg_coder_dec_func(this->elem, this->comp.format);
|
113
116
|
int encoded_len = BASE64_ENCODED_SIZE(len);
|
114
117
|
/* create a buffer of the encoded length */
|
115
|
-
VALUE out_value =
|
118
|
+
VALUE out_value = rb_str_new(NULL, encoded_len);
|
116
119
|
|
117
120
|
base64_encode( RSTRING_PTR(out_value), val, len );
|
118
121
|
|
@@ -130,6 +133,148 @@ pg_bin_dec_to_base64(t_pg_coder *conv, char *val, int len, int tuple, int field,
|
|
130
133
|
return out_value;
|
131
134
|
}
|
132
135
|
|
136
|
+
#define PG_INT64_MIN (-0x7FFFFFFFFFFFFFFFL - 1)
|
137
|
+
#define PG_INT64_MAX 0x7FFFFFFFFFFFFFFFL
|
138
|
+
|
139
|
+
/*
|
140
|
+
* Document-class: PG::BinaryDecoder::Timestamp < PG::SimpleDecoder
|
141
|
+
*
|
142
|
+
* This is a decoder class for conversion of PostgreSQL binary timestamps
|
143
|
+
* to Ruby Time objects.
|
144
|
+
*
|
145
|
+
* The following flags can be used to specify timezone interpretation:
|
146
|
+
* * +PG::Coder::TIMESTAMP_DB_UTC+ : Interpret timestamp as UTC time (default)
|
147
|
+
* * +PG::Coder::TIMESTAMP_DB_LOCAL+ : Interpret timestamp as local time
|
148
|
+
* * +PG::Coder::TIMESTAMP_APP_UTC+ : Return timestamp as UTC time (default)
|
149
|
+
* * +PG::Coder::TIMESTAMP_APP_LOCAL+ : Return timestamp as local time
|
150
|
+
*
|
151
|
+
* Example:
|
152
|
+
* deco = PG::BinaryDecoder::Timestamp.new(flags: PG::Coder::TIMESTAMP_DB_UTC | PG::Coder::TIMESTAMP_APP_LOCAL)
|
153
|
+
* deco.decode("\0"*8) # => 2000-01-01 01:00:00 +0100
|
154
|
+
*/
|
155
|
+
static VALUE
|
156
|
+
pg_bin_dec_timestamp(t_pg_coder *conv, const char *val, int len, int tuple, int field, int enc_idx)
|
157
|
+
{
|
158
|
+
int64_t timestamp;
|
159
|
+
int64_t sec;
|
160
|
+
int64_t nsec;
|
161
|
+
VALUE t;
|
162
|
+
|
163
|
+
if( len != sizeof(timestamp) ){
|
164
|
+
rb_raise( rb_eTypeError, "wrong data for timestamp converter in tuple %d field %d length %d", tuple, field, len);
|
165
|
+
}
|
166
|
+
|
167
|
+
timestamp = read_nbo64(val);
|
168
|
+
|
169
|
+
switch(timestamp){
|
170
|
+
case PG_INT64_MAX:
|
171
|
+
return rb_str_new2("infinity");
|
172
|
+
case PG_INT64_MIN:
|
173
|
+
return rb_str_new2("-infinity");
|
174
|
+
default:
|
175
|
+
/* PostgreSQL's timestamp is based on year 2000 and Ruby's time is based on 1970.
|
176
|
+
* Adjust the 30 years difference. */
|
177
|
+
sec = (timestamp / 1000000) + 10957L * 24L * 3600L;
|
178
|
+
nsec = (timestamp % 1000000) * 1000;
|
179
|
+
|
180
|
+
#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
|
181
|
+
/* Fast path for time conversion */
|
182
|
+
{
|
183
|
+
struct timespec ts = {sec, nsec};
|
184
|
+
t = rb_time_timespec_new(&ts, conv->flags & PG_CODER_TIMESTAMP_APP_LOCAL ? INT_MAX : INT_MAX-1);
|
185
|
+
}
|
186
|
+
#else
|
187
|
+
t = rb_funcall(rb_cTime, rb_intern("at"), 2, LL2NUM(sec), LL2NUM(nsec / 1000));
|
188
|
+
if( !(conv->flags & PG_CODER_TIMESTAMP_APP_LOCAL) ) {
|
189
|
+
t = rb_funcall(t, rb_intern("utc"), 0);
|
190
|
+
}
|
191
|
+
#endif
|
192
|
+
if( conv->flags & PG_CODER_TIMESTAMP_DB_LOCAL ) {
|
193
|
+
/* interpret it as local time */
|
194
|
+
t = rb_funcall(t, rb_intern("-"), 1, rb_funcall(t, rb_intern("utc_offset"), 0));
|
195
|
+
}
|
196
|
+
return t;
|
197
|
+
}
|
198
|
+
}
|
199
|
+
|
200
|
+
#define PG_INT32_MIN (-0x7FFFFFFF-1)
|
201
|
+
#define PG_INT32_MAX (0x7FFFFFFF)
|
202
|
+
#define POSTGRES_EPOCH_JDATE 2451545 /* == date2j(2000, 1, 1) */
|
203
|
+
#define MONTHS_PER_YEAR 12
|
204
|
+
|
205
|
+
/* taken from PostgreSQL sources at src/backend/utils/adt/datetime.c */
|
206
|
+
void
|
207
|
+
j2date(int jd, int *year, int *month, int *day)
|
208
|
+
{
|
209
|
+
unsigned int julian;
|
210
|
+
unsigned int quad;
|
211
|
+
unsigned int extra;
|
212
|
+
int y;
|
213
|
+
|
214
|
+
julian = jd;
|
215
|
+
julian += 32044;
|
216
|
+
quad = julian / 146097;
|
217
|
+
extra = (julian - quad * 146097) * 4 + 3;
|
218
|
+
julian += 60 + quad * 3 + extra / 146097;
|
219
|
+
quad = julian / 1461;
|
220
|
+
julian -= quad * 1461;
|
221
|
+
y = julian * 4 / 1461;
|
222
|
+
julian = ((y != 0) ? ((julian + 305) % 365) : ((julian + 306) % 366))
|
223
|
+
+ 123;
|
224
|
+
y += quad * 4;
|
225
|
+
*year = y - 4800;
|
226
|
+
quad = julian * 2141 / 65536;
|
227
|
+
*day = julian - 7834 * quad / 256;
|
228
|
+
*month = (quad + 10) % MONTHS_PER_YEAR + 1;
|
229
|
+
} /* j2date() */
|
230
|
+
|
231
|
+
/*
|
232
|
+
* Document-class: PG::BinaryDecoder::Date < PG::SimpleDecoder
|
233
|
+
*
|
234
|
+
* This is a decoder class for conversion of PostgreSQL binary date
|
235
|
+
* to Ruby Date objects.
|
236
|
+
*
|
237
|
+
* As soon as this class is used, it requires the ruby standard library 'date'.
|
238
|
+
*/
|
239
|
+
static VALUE
|
240
|
+
pg_bin_dec_date(t_pg_coder *conv, const char *val, int len, int tuple, int field, int enc_idx)
|
241
|
+
{
|
242
|
+
int year, month, day;
|
243
|
+
int date;
|
244
|
+
|
245
|
+
if (len != 4) {
|
246
|
+
rb_raise(rb_eTypeError, "unexpected date format != 4 bytes");
|
247
|
+
}
|
248
|
+
|
249
|
+
date = read_nbo32(val);
|
250
|
+
switch(date){
|
251
|
+
case PG_INT32_MAX:
|
252
|
+
return rb_str_new2("infinity");
|
253
|
+
case PG_INT32_MIN:
|
254
|
+
return rb_str_new2("-infinity");
|
255
|
+
default:
|
256
|
+
j2date(date + POSTGRES_EPOCH_JDATE, &year, &month, &day);
|
257
|
+
|
258
|
+
return rb_funcall(s_Date, s_id_new, 3, INT2NUM(year), INT2NUM(month), INT2NUM(day));
|
259
|
+
}
|
260
|
+
}
|
261
|
+
|
262
|
+
/* called per autoload when BinaryDecoder::Date is used */
|
263
|
+
static VALUE
|
264
|
+
init_pg_bin_decoder_date(VALUE rb_mPG_BinaryDecoder)
|
265
|
+
{
|
266
|
+
rb_require("date");
|
267
|
+
s_Date = rb_const_get(rb_cObject, rb_intern("Date"));
|
268
|
+
rb_gc_register_mark_object(s_Date);
|
269
|
+
s_id_new = rb_intern("new");
|
270
|
+
|
271
|
+
/* dummy = rb_define_class_under( rb_mPG_BinaryDecoder, "Date", rb_cPG_SimpleDecoder ); */
|
272
|
+
pg_define_coder( "Date", pg_bin_dec_date, rb_cPG_SimpleDecoder, rb_mPG_BinaryDecoder );
|
273
|
+
|
274
|
+
return Qnil;
|
275
|
+
}
|
276
|
+
|
277
|
+
|
133
278
|
/*
|
134
279
|
* Document-class: PG::BinaryDecoder::String < PG::SimpleDecoder
|
135
280
|
*
|
@@ -140,10 +285,11 @@ pg_bin_dec_to_base64(t_pg_coder *conv, char *val, int len, int tuple, int field,
|
|
140
285
|
*/
|
141
286
|
|
142
287
|
void
|
143
|
-
init_pg_binary_decoder()
|
288
|
+
init_pg_binary_decoder(void)
|
144
289
|
{
|
145
290
|
/* This module encapsulates all decoder classes with binary input format */
|
146
291
|
rb_mPG_BinaryDecoder = rb_define_module_under( rb_mPG, "BinaryDecoder" );
|
292
|
+
rb_define_private_method(rb_singleton_class(rb_mPG_BinaryDecoder), "init_date", init_pg_bin_decoder_date, 0);
|
147
293
|
|
148
294
|
/* Make RDoc aware of the decoder classes... */
|
149
295
|
/* dummy = rb_define_class_under( rb_mPG_BinaryDecoder, "Boolean", rb_cPG_SimpleDecoder ); */
|
@@ -156,6 +302,8 @@ init_pg_binary_decoder()
|
|
156
302
|
pg_define_coder( "String", pg_text_dec_string, rb_cPG_SimpleDecoder, rb_mPG_BinaryDecoder );
|
157
303
|
/* dummy = rb_define_class_under( rb_mPG_BinaryDecoder, "Bytea", rb_cPG_SimpleDecoder ); */
|
158
304
|
pg_define_coder( "Bytea", pg_bin_dec_bytea, rb_cPG_SimpleDecoder, rb_mPG_BinaryDecoder );
|
305
|
+
/* dummy = rb_define_class_under( rb_mPG_BinaryDecoder, "Timestamp", rb_cPG_SimpleDecoder ); */
|
306
|
+
pg_define_coder( "Timestamp", pg_bin_dec_timestamp, rb_cPG_SimpleDecoder, rb_mPG_BinaryDecoder );
|
159
307
|
|
160
308
|
/* dummy = rb_define_class_under( rb_mPG_BinaryDecoder, "ToBase64", rb_cPG_CompositeDecoder ); */
|
161
309
|
pg_define_coder( "ToBase64", pg_bin_dec_to_base64, rb_cPG_CompositeDecoder, rb_mPG_BinaryDecoder );
|