pg 0.17.1 → 0.18.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data/ChangeLog +2407 -2
- data/History.rdoc +68 -0
- data/Manifest.txt +29 -1
- data/README-Windows.rdoc +15 -26
- data/README.rdoc +52 -2
- data/Rakefile +56 -18
- data/Rakefile.cross +77 -49
- data/ext/extconf.rb +33 -26
- data/ext/pg.c +142 -21
- data/ext/pg.h +242 -6
- data/ext/pg_binary_decoder.c +162 -0
- data/ext/pg_binary_encoder.c +162 -0
- data/ext/pg_coder.c +479 -0
- data/ext/pg_connection.c +858 -553
- data/ext/pg_copy_coder.c +561 -0
- data/ext/pg_errors.c +6 -0
- data/ext/pg_result.c +479 -128
- data/ext/pg_text_decoder.c +421 -0
- data/ext/pg_text_encoder.c +663 -0
- data/ext/pg_type_map.c +159 -0
- data/ext/pg_type_map_all_strings.c +116 -0
- data/ext/pg_type_map_by_class.c +239 -0
- data/ext/pg_type_map_by_column.c +312 -0
- data/ext/pg_type_map_by_mri_type.c +284 -0
- data/ext/pg_type_map_by_oid.c +355 -0
- data/ext/pg_type_map_in_ruby.c +299 -0
- data/ext/util.c +149 -0
- data/ext/util.h +65 -0
- data/lib/pg/basic_type_mapping.rb +399 -0
- data/lib/pg/coder.rb +83 -0
- data/lib/pg/connection.rb +81 -29
- data/lib/pg/result.rb +13 -3
- data/lib/pg/text_decoder.rb +44 -0
- data/lib/pg/text_encoder.rb +27 -0
- data/lib/pg/type_map_by_column.rb +15 -0
- data/lib/pg.rb +12 -2
- data/spec/{lib/helpers.rb → helpers.rb} +101 -39
- data/spec/pg/basic_type_mapping_spec.rb +251 -0
- data/spec/pg/connection_spec.rb +516 -218
- data/spec/pg/result_spec.rb +216 -112
- data/spec/pg/type_map_by_class_spec.rb +138 -0
- data/spec/pg/type_map_by_column_spec.rb +222 -0
- data/spec/pg/type_map_by_mri_type_spec.rb +136 -0
- data/spec/pg/type_map_by_oid_spec.rb +149 -0
- data/spec/pg/type_map_in_ruby_spec.rb +164 -0
- data/spec/pg/type_map_spec.rb +22 -0
- data/spec/pg/type_spec.rb +697 -0
- data/spec/pg_spec.rb +24 -18
- data.tar.gz.sig +0 -0
- metadata +111 -45
- metadata.gz.sig +0 -0
@@ -0,0 +1,421 @@
|
|
1
|
+
/*
|
2
|
+
* pg_text_decoder.c - PG::TextDecoder module
|
3
|
+
* $Id: pg_text_decoder.c,v fcf731d3dff7 2015/09/08 12:25:06 jfali $
|
4
|
+
*
|
5
|
+
*/
|
6
|
+
|
7
|
+
/*
|
8
|
+
*
|
9
|
+
* Type casts for decoding PostgreSQL string representations to Ruby objects.
|
10
|
+
*
|
11
|
+
* Decoder classes are defined with pg_define_coder(). This creates a new coder class and
|
12
|
+
* assigns a decoder function.
|
13
|
+
*
|
14
|
+
* Signature of all type cast decoders is:
|
15
|
+
* VALUE decoder_function(t_pg_coder *this, char *val, int len, int tuple, int field, int enc_idx)
|
16
|
+
*
|
17
|
+
* Params:
|
18
|
+
* this - The data part of the coder object that belongs to the decoder function.
|
19
|
+
* val, len - The text or binary data to decode. The caller ensures, that the data is
|
20
|
+
* zero terminated ( that is val[len] = 0 ). The memory should be used read
|
21
|
+
* only by the callee.
|
22
|
+
* tuple - Row of the value within the result set.
|
23
|
+
* field - Column of the value within the result set.
|
24
|
+
* enc_idx - Index of the Encoding that any output String should get assigned.
|
25
|
+
*
|
26
|
+
* Returns:
|
27
|
+
* The type casted Ruby object.
|
28
|
+
*
|
29
|
+
*/
|
30
|
+
|
31
|
+
#include "pg.h"
|
32
|
+
#include "util.h"
|
33
|
+
#ifdef HAVE_INTTYPES_H
|
34
|
+
#include <inttypes.h>
|
35
|
+
#endif
|
36
|
+
|
37
|
+
VALUE rb_mPG_TextDecoder;
|
38
|
+
static ID s_id_decode;
|
39
|
+
|
40
|
+
|
41
|
+
/*
|
42
|
+
* Document-class: PG::TextDecoder::Boolean < PG::SimpleDecoder
|
43
|
+
*
|
44
|
+
* This is a decoder class for conversion of PostgreSQL boolean type
|
45
|
+
* to Ruby true or false values.
|
46
|
+
*
|
47
|
+
*/
|
48
|
+
static VALUE
|
49
|
+
pg_text_dec_boolean(t_pg_coder *conv, char *val, int len, int tuple, int field, int enc_idx)
|
50
|
+
{
|
51
|
+
if (len < 1) {
|
52
|
+
rb_raise( rb_eTypeError, "wrong data for text boolean converter in tuple %d field %d", tuple, field);
|
53
|
+
}
|
54
|
+
return *val == 't' ? Qtrue : Qfalse;
|
55
|
+
}
|
56
|
+
|
57
|
+
/*
|
58
|
+
* Document-class: PG::TextDecoder::String < PG::SimpleDecoder
|
59
|
+
*
|
60
|
+
* This is a decoder class for conversion of PostgreSQL text output to
|
61
|
+
* to Ruby String object. The output value will have the character encoding
|
62
|
+
* set with PG::Connection#internal_encoding= .
|
63
|
+
*
|
64
|
+
*/
|
65
|
+
VALUE
|
66
|
+
pg_text_dec_string(t_pg_coder *conv, char *val, int len, int tuple, int field, int enc_idx)
|
67
|
+
{
|
68
|
+
VALUE ret = rb_tainted_str_new( val, len );
|
69
|
+
PG_ENCODING_SET_NOCHECK( ret, enc_idx );
|
70
|
+
return ret;
|
71
|
+
}
|
72
|
+
|
73
|
+
/*
|
74
|
+
* Document-class: PG::TextDecoder::Integer < PG::SimpleDecoder
|
75
|
+
*
|
76
|
+
* This is a decoder class for conversion of PostgreSQL integer types
|
77
|
+
* to Ruby Integer objects.
|
78
|
+
*
|
79
|
+
*/
|
80
|
+
static VALUE
|
81
|
+
pg_text_dec_integer(t_pg_coder *conv, char *val, int len, int tuple, int field, int enc_idx)
|
82
|
+
{
|
83
|
+
long i;
|
84
|
+
int max_len;
|
85
|
+
|
86
|
+
if( sizeof(i) >= 8 && FIXNUM_MAX >= 1000000000000000000LL ){
|
87
|
+
/* 64 bit system can safely handle all numbers up to 18 digits as Fixnum */
|
88
|
+
max_len = 18;
|
89
|
+
} else if( sizeof(i) >= 4 && FIXNUM_MAX >= 1000000000LL ){
|
90
|
+
/* 32 bit system can safely handle all numbers up to 9 digits as Fixnum */
|
91
|
+
max_len = 9;
|
92
|
+
} else {
|
93
|
+
/* unknown -> don't use fast path for int conversion */
|
94
|
+
max_len = 0;
|
95
|
+
}
|
96
|
+
|
97
|
+
if( len <= max_len ){
|
98
|
+
/* rb_cstr2inum() seems to be slow, so we do the int conversion by hand.
|
99
|
+
* This proved to be 40% faster by the following benchmark:
|
100
|
+
*
|
101
|
+
* conn.type_mapping_for_results = PG::BasicTypeMapForResults.new conn
|
102
|
+
* Benchmark.measure do
|
103
|
+
* conn.exec("select generate_series(1,1000000)").values }
|
104
|
+
* end
|
105
|
+
*/
|
106
|
+
char *val_pos = val;
|
107
|
+
char digit = *val_pos;
|
108
|
+
int neg;
|
109
|
+
int error = 0;
|
110
|
+
|
111
|
+
if( digit=='-' ){
|
112
|
+
neg = 1;
|
113
|
+
i = 0;
|
114
|
+
}else if( digit>='0' && digit<='9' ){
|
115
|
+
neg = 0;
|
116
|
+
i = digit - '0';
|
117
|
+
} else {
|
118
|
+
error = 1;
|
119
|
+
}
|
120
|
+
|
121
|
+
while (!error && (digit=*++val_pos)) {
|
122
|
+
if( digit>='0' && digit<='9' ){
|
123
|
+
i = i * 10 + (digit - '0');
|
124
|
+
} else {
|
125
|
+
error = 1;
|
126
|
+
}
|
127
|
+
}
|
128
|
+
|
129
|
+
if( !error ){
|
130
|
+
return LONG2FIX(neg ? -i : i);
|
131
|
+
}
|
132
|
+
}
|
133
|
+
/* Fallback to ruby method if number too big or unrecognized. */
|
134
|
+
return rb_cstr2inum(val, 10);
|
135
|
+
}
|
136
|
+
|
137
|
+
/*
|
138
|
+
* Document-class: PG::TextDecoder::Float < PG::SimpleDecoder
|
139
|
+
*
|
140
|
+
* This is a decoder class for conversion of PostgreSQL float4 and float8 types
|
141
|
+
* to Ruby Float objects.
|
142
|
+
*
|
143
|
+
*/
|
144
|
+
static VALUE
|
145
|
+
pg_text_dec_float(t_pg_coder *conv, char *val, int len, int tuple, int field, int enc_idx)
|
146
|
+
{
|
147
|
+
return rb_float_new(strtod(val, NULL));
|
148
|
+
}
|
149
|
+
|
150
|
+
/*
|
151
|
+
* Document-class: PG::TextDecoder::Bytea < PG::SimpleDecoder
|
152
|
+
*
|
153
|
+
* This is a decoder class for conversion of PostgreSQL bytea type
|
154
|
+
* to binary String objects.
|
155
|
+
*
|
156
|
+
*/
|
157
|
+
static VALUE
|
158
|
+
pg_text_dec_bytea(t_pg_coder *conv, char *val, int len, int tuple, int field, int enc_idx)
|
159
|
+
{
|
160
|
+
unsigned char *to;
|
161
|
+
size_t to_len;
|
162
|
+
VALUE ret;
|
163
|
+
|
164
|
+
to = PQunescapeBytea( (unsigned char *)val, &to_len);
|
165
|
+
|
166
|
+
ret = rb_tainted_str_new((char*)to, to_len);
|
167
|
+
PQfreemem(to);
|
168
|
+
|
169
|
+
return ret;
|
170
|
+
}
|
171
|
+
|
172
|
+
/*
|
173
|
+
* Array parser functions are thankfully borrowed from here:
|
174
|
+
* https://github.com/dockyard/pg_array_parser
|
175
|
+
*/
|
176
|
+
static VALUE
|
177
|
+
read_array(t_pg_composite_coder *this, int *index, char *c_pg_array_string, int array_string_length, char *word, int enc_idx, int tuple, int field, t_pg_coder_dec_func dec_func)
|
178
|
+
{
|
179
|
+
/* Return value: array */
|
180
|
+
VALUE array;
|
181
|
+
int word_index = 0;
|
182
|
+
|
183
|
+
/* The current character in the input string. */
|
184
|
+
char c;
|
185
|
+
|
186
|
+
/* 0: Currently outside a quoted string, current word never quoted
|
187
|
+
* 1: Currently inside a quoted string
|
188
|
+
* -1: Currently outside a quoted string, current word previously quoted */
|
189
|
+
int openQuote = 0;
|
190
|
+
|
191
|
+
/* Inside quoted input means the next character should be treated literally,
|
192
|
+
* instead of being treated as a metacharacter.
|
193
|
+
* Outside of quoted input, means that the word shouldn't be pushed to the array,
|
194
|
+
* used when the last entry was a subarray (which adds to the array itself). */
|
195
|
+
int escapeNext = 0;
|
196
|
+
|
197
|
+
array = rb_ary_new();
|
198
|
+
|
199
|
+
/* Special case the empty array, so it doesn't need to be handled manually inside
|
200
|
+
* the loop. */
|
201
|
+
if(((*index) < array_string_length) && c_pg_array_string[(*index)] == '}')
|
202
|
+
{
|
203
|
+
return array;
|
204
|
+
}
|
205
|
+
|
206
|
+
for(;(*index) < array_string_length; ++(*index))
|
207
|
+
{
|
208
|
+
c = c_pg_array_string[*index];
|
209
|
+
if(openQuote < 1)
|
210
|
+
{
|
211
|
+
if(c == this->delimiter || c == '}')
|
212
|
+
{
|
213
|
+
if(!escapeNext)
|
214
|
+
{
|
215
|
+
if(openQuote == 0 && word_index == 4 && !strncmp(word, "NULL", word_index))
|
216
|
+
{
|
217
|
+
rb_ary_push(array, Qnil);
|
218
|
+
}
|
219
|
+
else
|
220
|
+
{
|
221
|
+
VALUE val;
|
222
|
+
word[word_index] = 0;
|
223
|
+
val = dec_func(this->elem, word, word_index, tuple, field, enc_idx);
|
224
|
+
rb_ary_push(array, val);
|
225
|
+
}
|
226
|
+
}
|
227
|
+
if(c == '}')
|
228
|
+
{
|
229
|
+
return array;
|
230
|
+
}
|
231
|
+
escapeNext = 0;
|
232
|
+
openQuote = 0;
|
233
|
+
word_index = 0;
|
234
|
+
}
|
235
|
+
else if(c == '"')
|
236
|
+
{
|
237
|
+
openQuote = 1;
|
238
|
+
}
|
239
|
+
else if(c == '{')
|
240
|
+
{
|
241
|
+
(*index)++;
|
242
|
+
rb_ary_push(array, read_array(this, index, c_pg_array_string, array_string_length, word, enc_idx, tuple, field, dec_func));
|
243
|
+
escapeNext = 1;
|
244
|
+
}
|
245
|
+
else
|
246
|
+
{
|
247
|
+
word[word_index] = c;
|
248
|
+
word_index++;
|
249
|
+
}
|
250
|
+
}
|
251
|
+
else if (escapeNext) {
|
252
|
+
word[word_index] = c;
|
253
|
+
word_index++;
|
254
|
+
escapeNext = 0;
|
255
|
+
}
|
256
|
+
else if (c == '\\')
|
257
|
+
{
|
258
|
+
escapeNext = 1;
|
259
|
+
}
|
260
|
+
else if (c == '"')
|
261
|
+
{
|
262
|
+
openQuote = -1;
|
263
|
+
}
|
264
|
+
else
|
265
|
+
{
|
266
|
+
word[word_index] = c;
|
267
|
+
word_index++;
|
268
|
+
}
|
269
|
+
}
|
270
|
+
|
271
|
+
return array;
|
272
|
+
}
|
273
|
+
|
274
|
+
/*
|
275
|
+
* Document-class: PG::TextDecoder::Array < PG::CompositeDecoder
|
276
|
+
*
|
277
|
+
* This is the decoder class for PostgreSQL array types.
|
278
|
+
*
|
279
|
+
* All values are decoded according to the #elements_type
|
280
|
+
* accessor. Sub-arrays are decoded recursively.
|
281
|
+
*
|
282
|
+
*/
|
283
|
+
static VALUE
|
284
|
+
pg_text_dec_array(t_pg_coder *conv, char *val, int len, int tuple, int field, int enc_idx)
|
285
|
+
{
|
286
|
+
t_pg_composite_coder *this = (t_pg_composite_coder *)conv;
|
287
|
+
t_pg_coder_dec_func dec_func = pg_coder_dec_func(this->elem, 0);
|
288
|
+
/* create a buffer of the same length, as that will be the worst case */
|
289
|
+
char *word = xmalloc(len + 1);
|
290
|
+
int index = 1;
|
291
|
+
|
292
|
+
VALUE return_value = read_array(this, &index, val, len, word, enc_idx, tuple, field, dec_func);
|
293
|
+
free(word);
|
294
|
+
return return_value;
|
295
|
+
}
|
296
|
+
|
297
|
+
/*
|
298
|
+
* Document-class: PG::TextDecoder::Identifier < PG::SimpleDecoder
|
299
|
+
*
|
300
|
+
* This is the decoder class for PostgreSQL identifiers.
|
301
|
+
*
|
302
|
+
* Returns an Array of identifiers:
|
303
|
+
* PG::TextDecoder::Identifier.new.decode('schema."table"."column"')
|
304
|
+
* => ["schema", "table", "column"]
|
305
|
+
*
|
306
|
+
*/
|
307
|
+
static VALUE
|
308
|
+
pg_text_dec_identifier(t_pg_coder *conv, char *val, int len, int tuple, int field, int enc_idx)
|
309
|
+
{
|
310
|
+
/* Return value: array */
|
311
|
+
VALUE array;
|
312
|
+
VALUE elem;
|
313
|
+
int word_index = 0;
|
314
|
+
int index;
|
315
|
+
/* Use a buffer of the same length, as that will be the worst case */
|
316
|
+
PG_VARIABLE_LENGTH_ARRAY(char, word, len + 1, NAMEDATALEN)
|
317
|
+
|
318
|
+
/* The current character in the input string. */
|
319
|
+
char c;
|
320
|
+
|
321
|
+
/* 0: Currently outside a quoted string
|
322
|
+
* 1: Currently inside a quoted string, last char was a quote
|
323
|
+
* 2: Currently inside a quoted string, last char was no quote */
|
324
|
+
int openQuote = 0;
|
325
|
+
|
326
|
+
array = rb_ary_new();
|
327
|
+
|
328
|
+
for(index = 0; index < len; ++index) {
|
329
|
+
c = val[index];
|
330
|
+
if(c == '.' && openQuote < 2 ) {
|
331
|
+
word[word_index] = 0;
|
332
|
+
|
333
|
+
elem = pg_text_dec_string(conv, word, word_index, tuple, field, enc_idx);
|
334
|
+
rb_ary_push(array, elem);
|
335
|
+
|
336
|
+
openQuote = 0;
|
337
|
+
word_index = 0;
|
338
|
+
} else if(c == '"') {
|
339
|
+
if (openQuote == 1) {
|
340
|
+
word[word_index] = c;
|
341
|
+
word_index++;
|
342
|
+
openQuote = 2;
|
343
|
+
} else if (openQuote == 2){
|
344
|
+
openQuote = 1;
|
345
|
+
} else {
|
346
|
+
openQuote = 2;
|
347
|
+
}
|
348
|
+
} else {
|
349
|
+
word[word_index] = c;
|
350
|
+
word_index++;
|
351
|
+
}
|
352
|
+
}
|
353
|
+
|
354
|
+
word[word_index] = 0;
|
355
|
+
elem = pg_text_dec_string(conv, word, word_index, tuple, field, enc_idx);
|
356
|
+
rb_ary_push(array, elem);
|
357
|
+
|
358
|
+
return array;
|
359
|
+
}
|
360
|
+
|
361
|
+
/*
|
362
|
+
* Document-class: PG::TextDecoder::FromBase64 < PG::CompositeDecoder
|
363
|
+
*
|
364
|
+
* This is a decoder class for conversion of base64 encoded data
|
365
|
+
* to it's binary representation. It outputs a binary Ruby String
|
366
|
+
* or some other Ruby object, if a #elements_type decoder was defined.
|
367
|
+
*
|
368
|
+
*/
|
369
|
+
static VALUE
|
370
|
+
pg_text_dec_from_base64(t_pg_coder *conv, char *val, int len, int tuple, int field, int enc_idx)
|
371
|
+
{
|
372
|
+
t_pg_composite_coder *this = (t_pg_composite_coder *)conv;
|
373
|
+
t_pg_coder_dec_func dec_func = pg_coder_dec_func(this->elem, this->comp.format);
|
374
|
+
int decoded_len;
|
375
|
+
/* create a buffer of the expected decoded length */
|
376
|
+
VALUE out_value = rb_tainted_str_new(NULL, BASE64_DECODED_SIZE(len));
|
377
|
+
|
378
|
+
decoded_len = base64_decode( RSTRING_PTR(out_value), val, len );
|
379
|
+
rb_str_set_len(out_value, decoded_len);
|
380
|
+
|
381
|
+
/* Is it a pure String conversion? Then we can directly send out_value to the user. */
|
382
|
+
if( this->comp.format == 0 && dec_func == pg_text_dec_string ){
|
383
|
+
PG_ENCODING_SET_NOCHECK( out_value, enc_idx );
|
384
|
+
return out_value;
|
385
|
+
}
|
386
|
+
if( this->comp.format == 1 && dec_func == pg_bin_dec_bytea ){
|
387
|
+
PG_ENCODING_SET_NOCHECK( out_value, rb_ascii8bit_encindex() );
|
388
|
+
return out_value;
|
389
|
+
}
|
390
|
+
out_value = dec_func(this->elem, RSTRING_PTR(out_value), decoded_len, tuple, field, enc_idx);
|
391
|
+
|
392
|
+
return out_value;
|
393
|
+
}
|
394
|
+
|
395
|
+
void
|
396
|
+
init_pg_text_decoder()
|
397
|
+
{
|
398
|
+
s_id_decode = rb_intern("decode");
|
399
|
+
|
400
|
+
/* This module encapsulates all decoder classes with text input format */
|
401
|
+
rb_mPG_TextDecoder = rb_define_module_under( rb_mPG, "TextDecoder" );
|
402
|
+
|
403
|
+
/* Make RDoc aware of the decoder classes... */
|
404
|
+
/* dummy = rb_define_class_under( rb_mPG_TextDecoder, "Boolean", rb_cPG_SimpleDecoder ); */
|
405
|
+
pg_define_coder( "Boolean", pg_text_dec_boolean, rb_cPG_SimpleDecoder, rb_mPG_TextDecoder );
|
406
|
+
/* dummy = rb_define_class_under( rb_mPG_TextDecoder, "Integer", rb_cPG_SimpleDecoder ); */
|
407
|
+
pg_define_coder( "Integer", pg_text_dec_integer, rb_cPG_SimpleDecoder, rb_mPG_TextDecoder );
|
408
|
+
/* dummy = rb_define_class_under( rb_mPG_TextDecoder, "Float", rb_cPG_SimpleDecoder ); */
|
409
|
+
pg_define_coder( "Float", pg_text_dec_float, rb_cPG_SimpleDecoder, rb_mPG_TextDecoder );
|
410
|
+
/* dummy = rb_define_class_under( rb_mPG_TextDecoder, "String", rb_cPG_SimpleDecoder ); */
|
411
|
+
pg_define_coder( "String", pg_text_dec_string, rb_cPG_SimpleDecoder, rb_mPG_TextDecoder );
|
412
|
+
/* dummy = rb_define_class_under( rb_mPG_TextDecoder, "Bytea", rb_cPG_SimpleDecoder ); */
|
413
|
+
pg_define_coder( "Bytea", pg_text_dec_bytea, rb_cPG_SimpleDecoder, rb_mPG_TextDecoder );
|
414
|
+
/* dummy = rb_define_class_under( rb_mPG_TextDecoder, "Identifier", rb_cPG_SimpleDecoder ); */
|
415
|
+
pg_define_coder( "Identifier", pg_text_dec_identifier, rb_cPG_SimpleDecoder, rb_mPG_TextDecoder );
|
416
|
+
|
417
|
+
/* dummy = rb_define_class_under( rb_mPG_TextDecoder, "Array", rb_cPG_CompositeDecoder ); */
|
418
|
+
pg_define_coder( "Array", pg_text_dec_array, rb_cPG_CompositeDecoder, rb_mPG_TextDecoder );
|
419
|
+
/* dummy = rb_define_class_under( rb_mPG_TextDecoder, "FromBase64", rb_cPG_CompositeDecoder ); */
|
420
|
+
pg_define_coder( "FromBase64", pg_text_dec_from_base64, rb_cPG_CompositeDecoder, rb_mPG_TextDecoder );
|
421
|
+
}
|