pg 1.6.0.rc1-x86_64-linux
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- checksums.yaml.gz.sig +4 -0
- data/BSDL +22 -0
- data/Contributors.rdoc +46 -0
- data/Gemfile +23 -0
- data/History.md +958 -0
- data/LICENSE +56 -0
- data/Manifest.txt +72 -0
- data/POSTGRES +23 -0
- data/README-OS_X.rdoc +68 -0
- data/README-Windows.rdoc +56 -0
- data/README.ja.md +300 -0
- data/README.md +286 -0
- data/Rakefile +161 -0
- 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 +1043 -0
- data/ext/errorcodes.rb +45 -0
- data/ext/errorcodes.txt +494 -0
- data/ext/extconf.rb +282 -0
- data/ext/gvl_wrappers.c +32 -0
- data/ext/gvl_wrappers.h +297 -0
- data/ext/pg.c +703 -0
- data/ext/pg.h +390 -0
- data/ext/pg_binary_decoder.c +460 -0
- data/ext/pg_binary_encoder.c +583 -0
- data/ext/pg_cancel_connection.c +360 -0
- data/ext/pg_coder.c +622 -0
- data/ext/pg_connection.c +4869 -0
- data/ext/pg_copy_coder.c +921 -0
- data/ext/pg_errors.c +95 -0
- data/ext/pg_record_coder.c +522 -0
- data/ext/pg_result.c +1764 -0
- data/ext/pg_text_decoder.c +1008 -0
- data/ext/pg_text_encoder.c +833 -0
- data/ext/pg_tuple.c +572 -0
- data/ext/pg_type_map.c +200 -0
- data/ext/pg_type_map_all_strings.c +130 -0
- data/ext/pg_type_map_by_class.c +271 -0
- data/ext/pg_type_map_by_column.c +355 -0
- data/ext/pg_type_map_by_mri_type.c +313 -0
- data/ext/pg_type_map_by_oid.c +388 -0
- data/ext/pg_type_map_in_ruby.c +333 -0
- data/ext/pg_util.c +149 -0
- data/ext/pg_util.h +65 -0
- data/ext/vc/pg.sln +26 -0
- data/ext/vc/pg_18/pg.vcproj +216 -0
- data/ext/vc/pg_19/pg_19.vcproj +209 -0
- data/lib/2.7/pg_ext.so +0 -0
- data/lib/3.0/pg_ext.so +0 -0
- data/lib/3.1/pg_ext.so +0 -0
- data/lib/3.2/pg_ext.so +0 -0
- data/lib/3.3/pg_ext.so +0 -0
- 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/cancel_connection.rb +30 -0
- data/lib/pg/coder.rb +106 -0
- data/lib/pg/connection.rb +1027 -0
- data/lib/pg/exceptions.rb +31 -0
- data/lib/pg/result.rb +43 -0
- 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 +16 -0
- data/lib/pg/version.rb +4 -0
- data/lib/pg.rb +144 -0
- 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/ports/x86_64-linux/lib/libpq-ruby-pg.so.1 +0 -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 +252 -0
- metadata.gz.sig +0 -0
@@ -0,0 +1,460 @@
|
|
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
|
+
static VALUE s_Date;
|
16
|
+
static ID s_id_new;
|
17
|
+
|
18
|
+
|
19
|
+
/*
|
20
|
+
* Document-class: PG::BinaryDecoder::Boolean < PG::SimpleDecoder
|
21
|
+
*
|
22
|
+
* This is a decoder class for conversion of PostgreSQL binary +bool+ type
|
23
|
+
* to Ruby +true+ or +false+ objects.
|
24
|
+
*
|
25
|
+
*/
|
26
|
+
static VALUE
|
27
|
+
pg_bin_dec_boolean(t_pg_coder *conv, const char *val, int len, int tuple, int field, int enc_idx)
|
28
|
+
{
|
29
|
+
if (len < 1) {
|
30
|
+
rb_raise( rb_eTypeError, "wrong data for binary boolean converter in tuple %d field %d", tuple, field);
|
31
|
+
}
|
32
|
+
return *val == 0 ? Qfalse : Qtrue;
|
33
|
+
}
|
34
|
+
|
35
|
+
/*
|
36
|
+
* Document-class: PG::BinaryDecoder::Integer < PG::SimpleDecoder
|
37
|
+
*
|
38
|
+
* This is a decoder class for conversion of PostgreSQL binary +int2+, +int4+ and +int8+ types
|
39
|
+
* to Ruby Integer objects.
|
40
|
+
*
|
41
|
+
*/
|
42
|
+
static VALUE
|
43
|
+
pg_bin_dec_integer(t_pg_coder *conv, const char *val, int len, int tuple, int field, int enc_idx)
|
44
|
+
{
|
45
|
+
switch( len ){
|
46
|
+
case 2:
|
47
|
+
return INT2NUM(read_nbo16(val));
|
48
|
+
case 4:
|
49
|
+
return LONG2NUM(read_nbo32(val));
|
50
|
+
case 8:
|
51
|
+
return LL2NUM(read_nbo64(val));
|
52
|
+
default:
|
53
|
+
rb_raise( rb_eTypeError, "wrong data for binary integer converter in tuple %d field %d length %d", tuple, field, len);
|
54
|
+
}
|
55
|
+
}
|
56
|
+
|
57
|
+
/*
|
58
|
+
* Document-class: PG::BinaryDecoder::Float < PG::SimpleDecoder
|
59
|
+
*
|
60
|
+
* This is a decoder class for conversion of PostgreSQL binary +float4+ and +float8+ types
|
61
|
+
* to Ruby Float objects.
|
62
|
+
*
|
63
|
+
*/
|
64
|
+
static VALUE
|
65
|
+
pg_bin_dec_float(t_pg_coder *conv, const char *val, int len, int tuple, int field, int enc_idx)
|
66
|
+
{
|
67
|
+
union {
|
68
|
+
float f;
|
69
|
+
int32_t i;
|
70
|
+
} swap4;
|
71
|
+
union {
|
72
|
+
double f;
|
73
|
+
int64_t i;
|
74
|
+
} swap8;
|
75
|
+
|
76
|
+
switch( len ){
|
77
|
+
case 4:
|
78
|
+
swap4.i = read_nbo32(val);
|
79
|
+
return rb_float_new(swap4.f);
|
80
|
+
case 8:
|
81
|
+
swap8.i = read_nbo64(val);
|
82
|
+
return rb_float_new(swap8.f);
|
83
|
+
default:
|
84
|
+
rb_raise( rb_eTypeError, "wrong data for BinaryFloat converter in tuple %d field %d length %d", tuple, field, len);
|
85
|
+
}
|
86
|
+
}
|
87
|
+
|
88
|
+
/*
|
89
|
+
* Document-class: PG::BinaryDecoder::Bytea < PG::SimpleDecoder
|
90
|
+
*
|
91
|
+
* This decoder class delivers the data received from the server as binary String object.
|
92
|
+
* It is therefore suitable for conversion of PostgreSQL +bytea+ data as well as any other
|
93
|
+
* data in binary format.
|
94
|
+
*
|
95
|
+
*/
|
96
|
+
VALUE
|
97
|
+
pg_bin_dec_bytea(t_pg_coder *conv, const char *val, int len, int tuple, int field, int enc_idx)
|
98
|
+
{
|
99
|
+
VALUE ret;
|
100
|
+
ret = rb_str_new( val, len );
|
101
|
+
PG_ENCODING_SET_NOCHECK( ret, rb_ascii8bit_encindex() );
|
102
|
+
return ret;
|
103
|
+
}
|
104
|
+
|
105
|
+
/*
|
106
|
+
* Document-class: PG::BinaryDecoder::ToBase64 < PG::CompositeDecoder
|
107
|
+
*
|
108
|
+
* This is a decoder class for conversion of binary +bytea+ to base64 data.
|
109
|
+
*
|
110
|
+
*/
|
111
|
+
static VALUE
|
112
|
+
pg_bin_dec_to_base64(t_pg_coder *conv, const char *val, int len, int tuple, int field, int enc_idx)
|
113
|
+
{
|
114
|
+
t_pg_composite_coder *this = (t_pg_composite_coder *)conv;
|
115
|
+
t_pg_coder_dec_func dec_func = pg_coder_dec_func(this->elem, this->comp.format);
|
116
|
+
int encoded_len = BASE64_ENCODED_SIZE(len);
|
117
|
+
/* create a buffer of the encoded length */
|
118
|
+
VALUE out_value = rb_str_new(NULL, encoded_len);
|
119
|
+
|
120
|
+
base64_encode( RSTRING_PTR(out_value), val, len );
|
121
|
+
|
122
|
+
/* Is it a pure String conversion? Then we can directly send out_value to the user. */
|
123
|
+
if( this->comp.format == 0 && dec_func == pg_text_dec_string ){
|
124
|
+
PG_ENCODING_SET_NOCHECK( out_value, enc_idx );
|
125
|
+
return out_value;
|
126
|
+
}
|
127
|
+
if( this->comp.format == 1 && dec_func == pg_bin_dec_bytea ){
|
128
|
+
PG_ENCODING_SET_NOCHECK( out_value, rb_ascii8bit_encindex() );
|
129
|
+
return out_value;
|
130
|
+
}
|
131
|
+
out_value = dec_func(this->elem, RSTRING_PTR(out_value), encoded_len, tuple, field, enc_idx);
|
132
|
+
|
133
|
+
return out_value;
|
134
|
+
}
|
135
|
+
|
136
|
+
/*
|
137
|
+
* Maximum number of array subscripts (arbitrary limit)
|
138
|
+
*/
|
139
|
+
#define MAXDIM 6
|
140
|
+
|
141
|
+
/*
|
142
|
+
* Document-class: PG::BinaryDecoder::Array < PG::CompositeDecoder
|
143
|
+
*
|
144
|
+
* This is a decoder class for conversion of binary array types.
|
145
|
+
*
|
146
|
+
* It returns an Array with possibly an arbitrary number of sub-Arrays.
|
147
|
+
* All values are decoded according to the #elements_type accessor.
|
148
|
+
* Sub-arrays are decoded recursively.
|
149
|
+
*
|
150
|
+
* This decoder simply ignores any dimension decorations preceding the array values.
|
151
|
+
* It returns all array values as regular ruby Array with a zero based index, regardless of the index given in the dimension decoration.
|
152
|
+
*
|
153
|
+
* An array decoder which respects dimension decorations is waiting to be implemented.
|
154
|
+
*
|
155
|
+
*/
|
156
|
+
static VALUE
|
157
|
+
pg_bin_dec_array(t_pg_coder *conv, const char *input_line, int len, int tuple, int field, int enc_idx)
|
158
|
+
{
|
159
|
+
t_pg_composite_coder *this = (t_pg_composite_coder *)conv;
|
160
|
+
t_pg_coder_dec_func dec_func = pg_coder_dec_func(this->elem, this->comp.format);
|
161
|
+
|
162
|
+
/* Current field */
|
163
|
+
VALUE field_str;
|
164
|
+
|
165
|
+
int32_t nitems32;
|
166
|
+
int i;
|
167
|
+
int ndim;
|
168
|
+
int nitems;
|
169
|
+
int flags;
|
170
|
+
int dim;
|
171
|
+
int dim_sizes[MAXDIM];
|
172
|
+
VALUE arrays[MAXDIM];
|
173
|
+
char *output_ptr;
|
174
|
+
const char *cur_ptr;
|
175
|
+
const char *line_end_ptr;
|
176
|
+
char *end_capa_ptr;
|
177
|
+
|
178
|
+
/* Allocate a new string with embedded capacity and realloc later with
|
179
|
+
* exponential growing size when needed. */
|
180
|
+
PG_RB_STR_NEW( field_str, output_ptr, end_capa_ptr );
|
181
|
+
|
182
|
+
/* set pointer variables for loop */
|
183
|
+
cur_ptr = input_line;
|
184
|
+
line_end_ptr = input_line + len;
|
185
|
+
|
186
|
+
/* read number of dimensions */
|
187
|
+
if (line_end_ptr - cur_ptr < 4 ) goto length_error;
|
188
|
+
ndim = read_nbo32(cur_ptr);
|
189
|
+
if (ndim < 0 || ndim > MAXDIM) {
|
190
|
+
rb_raise( rb_eArgError, "unsupported number of array dimensions: %d", ndim );
|
191
|
+
}
|
192
|
+
cur_ptr += 4;
|
193
|
+
|
194
|
+
/* read flags */
|
195
|
+
if (line_end_ptr - cur_ptr < 4 ) goto length_error;
|
196
|
+
flags = read_nbo32(cur_ptr);
|
197
|
+
if (flags != 0 && flags != 1) {
|
198
|
+
rb_raise( rb_eArgError, "unsupported binary array flags: %d", flags );
|
199
|
+
}
|
200
|
+
cur_ptr += 4;
|
201
|
+
|
202
|
+
/* ignore element OID */
|
203
|
+
if (line_end_ptr - cur_ptr < 4 ) goto length_error;
|
204
|
+
cur_ptr += 4;
|
205
|
+
|
206
|
+
nitems32 = ndim == 0 ? 0 : 1;
|
207
|
+
for (i = 0; i < ndim; i++) {
|
208
|
+
int64_t prod;
|
209
|
+
|
210
|
+
/* read size of dimensions and ignore lower bound */
|
211
|
+
if (line_end_ptr - cur_ptr < 8 ) goto length_error;
|
212
|
+
dim_sizes[i] = read_nbo32(cur_ptr);
|
213
|
+
prod = (int64_t) nitems32 * (int64_t) dim_sizes[i];
|
214
|
+
nitems32 = (int32_t) prod;
|
215
|
+
if (dim_sizes[i] < 0 || (int64_t) nitems32 != prod) {
|
216
|
+
rb_raise( rb_eArgError, "unsupported array size: %" PRId64, prod );
|
217
|
+
}
|
218
|
+
cur_ptr += 8;
|
219
|
+
}
|
220
|
+
nitems = (int)nitems32;
|
221
|
+
|
222
|
+
dim = 0;
|
223
|
+
arrays[dim] = rb_ary_new2(ndim == 0 ? 0 : dim_sizes[dim]);
|
224
|
+
for (i = 0; i < nitems; i++) {
|
225
|
+
int input_len;
|
226
|
+
|
227
|
+
/* traverse dimensions down */
|
228
|
+
while (dim < ndim - 1) {
|
229
|
+
dim++;
|
230
|
+
arrays[dim] = rb_ary_new2(dim_sizes[dim]);
|
231
|
+
rb_ary_push(arrays[dim - 1], arrays[dim]);
|
232
|
+
}
|
233
|
+
|
234
|
+
/* read element length */
|
235
|
+
if (line_end_ptr - cur_ptr < 4 ) goto length_error;
|
236
|
+
input_len = read_nbo32(cur_ptr);
|
237
|
+
cur_ptr += 4;
|
238
|
+
|
239
|
+
/* convert and put element into array */
|
240
|
+
if (input_len < 0) {
|
241
|
+
if (input_len != -1) goto length_error;
|
242
|
+
/* NULL indicator */
|
243
|
+
rb_ary_push(arrays[dim], Qnil);
|
244
|
+
} else {
|
245
|
+
VALUE field_value;
|
246
|
+
if (line_end_ptr - cur_ptr < input_len ) goto length_error;
|
247
|
+
|
248
|
+
/* copy input data to field_str */
|
249
|
+
PG_RB_STR_ENSURE_CAPA( field_str, input_len, output_ptr, end_capa_ptr );
|
250
|
+
memcpy(output_ptr, cur_ptr, input_len);
|
251
|
+
cur_ptr += input_len;
|
252
|
+
output_ptr += input_len;
|
253
|
+
/* convert field_str through the type map */
|
254
|
+
rb_str_set_len( field_str, output_ptr - RSTRING_PTR(field_str) );
|
255
|
+
field_value = dec_func(this->elem, RSTRING_PTR(field_str), input_len, tuple, field, enc_idx);
|
256
|
+
|
257
|
+
rb_ary_push(arrays[dim], field_value);
|
258
|
+
|
259
|
+
if( field_value == field_str ){
|
260
|
+
/* Our output string will be send to the user, so we can not reuse
|
261
|
+
* it for the next field. */
|
262
|
+
PG_RB_STR_NEW( field_str, output_ptr, end_capa_ptr );
|
263
|
+
}
|
264
|
+
}
|
265
|
+
|
266
|
+
/* Reset the pointer to the start of the output/buffer string. */
|
267
|
+
output_ptr = RSTRING_PTR(field_str);
|
268
|
+
|
269
|
+
/* traverse dimensions up */
|
270
|
+
while (RARRAY_LEN(arrays[dim]) >= dim_sizes[dim] && dim > 0) {
|
271
|
+
dim--;
|
272
|
+
}
|
273
|
+
}
|
274
|
+
|
275
|
+
if (cur_ptr < line_end_ptr)
|
276
|
+
rb_raise( rb_eArgError, "trailing data after binary array data at position: %ld", (long)(cur_ptr - input_line) + 1 );
|
277
|
+
|
278
|
+
return arrays[0];
|
279
|
+
|
280
|
+
length_error:
|
281
|
+
rb_raise( rb_eArgError, "premature end of binary array data at position: %ld", (long)(cur_ptr - input_line) + 1 );
|
282
|
+
}
|
283
|
+
|
284
|
+
#define PG_INT64_MIN (-0x7FFFFFFFFFFFFFFFL - 1)
|
285
|
+
#define PG_INT64_MAX 0x7FFFFFFFFFFFFFFFL
|
286
|
+
|
287
|
+
/*
|
288
|
+
* Document-class: PG::BinaryDecoder::Timestamp < PG::SimpleDecoder
|
289
|
+
*
|
290
|
+
* This is a decoder class for conversion of PostgreSQL binary timestamps
|
291
|
+
* to Ruby Time objects.
|
292
|
+
*
|
293
|
+
* The following flags can be used to specify timezone interpretation:
|
294
|
+
* * +PG::Coder::TIMESTAMP_DB_UTC+ : Interpret timestamp as UTC time (default)
|
295
|
+
* * +PG::Coder::TIMESTAMP_DB_LOCAL+ : Interpret timestamp as local time
|
296
|
+
* * +PG::Coder::TIMESTAMP_APP_UTC+ : Return timestamp as UTC time (default)
|
297
|
+
* * +PG::Coder::TIMESTAMP_APP_LOCAL+ : Return timestamp as local time
|
298
|
+
*
|
299
|
+
* Example:
|
300
|
+
* deco = PG::BinaryDecoder::Timestamp.new(flags: PG::Coder::TIMESTAMP_DB_UTC | PG::Coder::TIMESTAMP_APP_LOCAL)
|
301
|
+
* deco.decode("\0"*8) # => 2000-01-01 01:00:00 +0100
|
302
|
+
*/
|
303
|
+
static VALUE
|
304
|
+
pg_bin_dec_timestamp(t_pg_coder *conv, const char *val, int len, int tuple, int field, int enc_idx)
|
305
|
+
{
|
306
|
+
int64_t timestamp;
|
307
|
+
int64_t sec;
|
308
|
+
int64_t nsec;
|
309
|
+
VALUE t;
|
310
|
+
|
311
|
+
if( len != sizeof(timestamp) ){
|
312
|
+
rb_raise( rb_eTypeError, "wrong data for timestamp converter in tuple %d field %d length %d", tuple, field, len);
|
313
|
+
}
|
314
|
+
|
315
|
+
timestamp = read_nbo64(val);
|
316
|
+
|
317
|
+
switch(timestamp){
|
318
|
+
case PG_INT64_MAX:
|
319
|
+
return rb_str_new2("infinity");
|
320
|
+
case PG_INT64_MIN:
|
321
|
+
return rb_str_new2("-infinity");
|
322
|
+
default:
|
323
|
+
/* PostgreSQL's timestamp is based on year 2000 and Ruby's time is based on 1970.
|
324
|
+
* Adjust the 30 years difference. */
|
325
|
+
sec = (timestamp / 1000000) + 10957L * 24L * 3600L;
|
326
|
+
nsec = (timestamp % 1000000) * 1000;
|
327
|
+
|
328
|
+
#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
|
329
|
+
/* Fast path for time conversion */
|
330
|
+
{
|
331
|
+
struct timespec ts = {sec, nsec};
|
332
|
+
t = rb_time_timespec_new(&ts, conv->flags & PG_CODER_TIMESTAMP_APP_LOCAL ? INT_MAX : INT_MAX-1);
|
333
|
+
}
|
334
|
+
#else
|
335
|
+
t = rb_funcall(rb_cTime, rb_intern("at"), 2, LL2NUM(sec), LL2NUM(nsec / 1000));
|
336
|
+
if( !(conv->flags & PG_CODER_TIMESTAMP_APP_LOCAL) ) {
|
337
|
+
t = rb_funcall(t, rb_intern("utc"), 0);
|
338
|
+
}
|
339
|
+
#endif
|
340
|
+
if( conv->flags & PG_CODER_TIMESTAMP_DB_LOCAL ) {
|
341
|
+
/* interpret it as local time */
|
342
|
+
t = rb_funcall(t, rb_intern("-"), 1, rb_funcall(t, rb_intern("utc_offset"), 0));
|
343
|
+
}
|
344
|
+
return t;
|
345
|
+
}
|
346
|
+
}
|
347
|
+
|
348
|
+
#define PG_INT32_MIN (-0x7FFFFFFF-1)
|
349
|
+
#define PG_INT32_MAX (0x7FFFFFFF)
|
350
|
+
#define POSTGRES_EPOCH_JDATE 2451545 /* == date2j(2000, 1, 1) */
|
351
|
+
#define MONTHS_PER_YEAR 12
|
352
|
+
|
353
|
+
/* taken from PostgreSQL sources at src/backend/utils/adt/datetime.c */
|
354
|
+
void
|
355
|
+
j2date(int jd, int *year, int *month, int *day)
|
356
|
+
{
|
357
|
+
unsigned int julian;
|
358
|
+
unsigned int quad;
|
359
|
+
unsigned int extra;
|
360
|
+
int y;
|
361
|
+
|
362
|
+
julian = jd;
|
363
|
+
julian += 32044;
|
364
|
+
quad = julian / 146097;
|
365
|
+
extra = (julian - quad * 146097) * 4 + 3;
|
366
|
+
julian += 60 + quad * 3 + extra / 146097;
|
367
|
+
quad = julian / 1461;
|
368
|
+
julian -= quad * 1461;
|
369
|
+
y = julian * 4 / 1461;
|
370
|
+
julian = ((y != 0) ? ((julian + 305) % 365) : ((julian + 306) % 366))
|
371
|
+
+ 123;
|
372
|
+
y += quad * 4;
|
373
|
+
*year = y - 4800;
|
374
|
+
quad = julian * 2141 / 65536;
|
375
|
+
*day = julian - 7834 * quad / 256;
|
376
|
+
*month = (quad + 10) % MONTHS_PER_YEAR + 1;
|
377
|
+
} /* j2date() */
|
378
|
+
|
379
|
+
/*
|
380
|
+
* Document-class: PG::BinaryDecoder::Date < PG::SimpleDecoder
|
381
|
+
*
|
382
|
+
* This is a decoder class for conversion of PostgreSQL binary date
|
383
|
+
* to Ruby Date objects.
|
384
|
+
*
|
385
|
+
* As soon as this class is used, it requires the ruby standard library 'date'.
|
386
|
+
*/
|
387
|
+
static VALUE
|
388
|
+
pg_bin_dec_date(t_pg_coder *conv, const char *val, int len, int tuple, int field, int enc_idx)
|
389
|
+
{
|
390
|
+
int year, month, day;
|
391
|
+
int date;
|
392
|
+
|
393
|
+
if (len != 4) {
|
394
|
+
rb_raise(rb_eTypeError, "unexpected date format != 4 bytes");
|
395
|
+
}
|
396
|
+
|
397
|
+
date = read_nbo32(val);
|
398
|
+
switch(date){
|
399
|
+
case PG_INT32_MAX:
|
400
|
+
return rb_str_new2("infinity");
|
401
|
+
case PG_INT32_MIN:
|
402
|
+
return rb_str_new2("-infinity");
|
403
|
+
default:
|
404
|
+
j2date(date + POSTGRES_EPOCH_JDATE, &year, &month, &day);
|
405
|
+
|
406
|
+
return rb_funcall(s_Date, s_id_new, 3, INT2NUM(year), INT2NUM(month), INT2NUM(day));
|
407
|
+
}
|
408
|
+
}
|
409
|
+
|
410
|
+
/* called per autoload when BinaryDecoder::Date is used */
|
411
|
+
static VALUE
|
412
|
+
init_pg_bin_decoder_date(VALUE rb_mPG_BinaryDecoder)
|
413
|
+
{
|
414
|
+
rb_require("date");
|
415
|
+
s_Date = rb_const_get(rb_cObject, rb_intern("Date"));
|
416
|
+
rb_gc_register_mark_object(s_Date);
|
417
|
+
s_id_new = rb_intern("new");
|
418
|
+
|
419
|
+
/* dummy = rb_define_class_under( rb_mPG_BinaryDecoder, "Date", rb_cPG_SimpleDecoder ); */
|
420
|
+
pg_define_coder( "Date", pg_bin_dec_date, rb_cPG_SimpleDecoder, rb_mPG_BinaryDecoder );
|
421
|
+
|
422
|
+
return Qnil;
|
423
|
+
}
|
424
|
+
|
425
|
+
|
426
|
+
/*
|
427
|
+
* Document-class: PG::BinaryDecoder::String < PG::SimpleDecoder
|
428
|
+
*
|
429
|
+
* This is a decoder class for conversion of PostgreSQL text output to
|
430
|
+
* to Ruby String object. The output value will have the character encoding
|
431
|
+
* set with PG::Connection#internal_encoding= .
|
432
|
+
*
|
433
|
+
*/
|
434
|
+
|
435
|
+
void
|
436
|
+
init_pg_binary_decoder(void)
|
437
|
+
{
|
438
|
+
/* This module encapsulates all decoder classes with binary input format */
|
439
|
+
rb_mPG_BinaryDecoder = rb_define_module_under( rb_mPG, "BinaryDecoder" );
|
440
|
+
rb_define_private_method(rb_singleton_class(rb_mPG_BinaryDecoder), "init_date", init_pg_bin_decoder_date, 0);
|
441
|
+
|
442
|
+
/* Make RDoc aware of the decoder classes... */
|
443
|
+
/* dummy = rb_define_class_under( rb_mPG_BinaryDecoder, "Boolean", rb_cPG_SimpleDecoder ); */
|
444
|
+
pg_define_coder( "Boolean", pg_bin_dec_boolean, rb_cPG_SimpleDecoder, rb_mPG_BinaryDecoder );
|
445
|
+
/* dummy = rb_define_class_under( rb_mPG_BinaryDecoder, "Integer", rb_cPG_SimpleDecoder ); */
|
446
|
+
pg_define_coder( "Integer", pg_bin_dec_integer, rb_cPG_SimpleDecoder, rb_mPG_BinaryDecoder );
|
447
|
+
/* dummy = rb_define_class_under( rb_mPG_BinaryDecoder, "Float", rb_cPG_SimpleDecoder ); */
|
448
|
+
pg_define_coder( "Float", pg_bin_dec_float, rb_cPG_SimpleDecoder, rb_mPG_BinaryDecoder );
|
449
|
+
/* dummy = rb_define_class_under( rb_mPG_BinaryDecoder, "String", rb_cPG_SimpleDecoder ); */
|
450
|
+
pg_define_coder( "String", pg_text_dec_string, rb_cPG_SimpleDecoder, rb_mPG_BinaryDecoder );
|
451
|
+
/* dummy = rb_define_class_under( rb_mPG_BinaryDecoder, "Bytea", rb_cPG_SimpleDecoder ); */
|
452
|
+
pg_define_coder( "Bytea", pg_bin_dec_bytea, rb_cPG_SimpleDecoder, rb_mPG_BinaryDecoder );
|
453
|
+
/* dummy = rb_define_class_under( rb_mPG_BinaryDecoder, "Timestamp", rb_cPG_SimpleDecoder ); */
|
454
|
+
pg_define_coder( "Timestamp", pg_bin_dec_timestamp, rb_cPG_SimpleDecoder, rb_mPG_BinaryDecoder );
|
455
|
+
|
456
|
+
/* dummy = rb_define_class_under( rb_mPG_BinaryDecoder, "Array", rb_cPG_CompositeDecoder ); */
|
457
|
+
pg_define_coder( "Array", pg_bin_dec_array, rb_cPG_CompositeDecoder, rb_mPG_BinaryDecoder );
|
458
|
+
/* dummy = rb_define_class_under( rb_mPG_BinaryDecoder, "ToBase64", rb_cPG_CompositeDecoder ); */
|
459
|
+
pg_define_coder( "ToBase64", pg_bin_dec_to_base64, rb_cPG_CompositeDecoder, rb_mPG_BinaryDecoder );
|
460
|
+
}
|