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,682 @@
1
+ /*
2
+ * pg_text_encoder.c - PG::TextEncoder module
3
+ * $Id: pg_text_encoder.c,v e57f6b452eb3 2018/08/18 10:58:52 lars $
4
+ *
5
+ */
6
+
7
+ /*
8
+ *
9
+ * Type casts for encoding Ruby objects to PostgreSQL string representations.
10
+ *
11
+ * Encoder classes are defined with pg_define_coder(). This creates a new coder class and
12
+ * assigns an encoder function. The encoder function can decide between two different options
13
+ * to return the encoded data. It can either return it as a Ruby String object or write the
14
+ * encoded data to a memory space provided by the caller. In the second case, the encoder
15
+ * function is called twice, once for deciding the encoding option and returning the expected
16
+ * data length, and a second time when the requested memory space was made available by the
17
+ * calling function, to do the actual conversion and writing. Parameter intermediate can be
18
+ * used to store data between these two calls.
19
+ *
20
+ * Signature of all type cast encoders is:
21
+ * int encoder_function(t_pg_coder *this, VALUE value, char *out, VALUE *intermediate)
22
+ *
23
+ * Params:
24
+ * this - The data part of the coder object that belongs to the encoder function.
25
+ * value - The Ruby object to cast.
26
+ * out - NULL for the first call,
27
+ * pointer to a buffer with the requested size for the second call.
28
+ * intermediate - Pointer to a VALUE that might be set by the encoding function to some
29
+ * value in the first call that can be retrieved later in the second call.
30
+ * This VALUE is not yet initialized by the caller.
31
+ * enc_idx - Index of the output Encoding that strings should be converted to.
32
+ *
33
+ * Returns:
34
+ * >= 0 - If out==NULL the encoder function must return the expected output buffer size.
35
+ * This can be larger than the size of the second call, but may not be smaller.
36
+ * If out!=NULL the encoder function must return the actually used output buffer size
37
+ * without a termination character.
38
+ * -1 - The encoder function can alternatively return -1 to indicate that no second call
39
+ * is required, but the String value in *intermediate should be used instead.
40
+ */
41
+
42
+
43
+ #include "pg.h"
44
+ #include "util.h"
45
+ #ifdef HAVE_INTTYPES_H
46
+ #include <inttypes.h>
47
+ #endif
48
+ #include <math.h>
49
+
50
+ VALUE rb_mPG_TextEncoder;
51
+ static ID s_id_encode;
52
+ static ID s_id_to_i;
53
+
54
+ static int pg_text_enc_integer(t_pg_coder *this, VALUE value, char *out, VALUE *intermediate, int enc_idx);
55
+
56
+ VALUE
57
+ pg_obj_to_i( VALUE value )
58
+ {
59
+ switch (TYPE(value)) {
60
+ case T_FIXNUM:
61
+ case T_FLOAT:
62
+ case T_BIGNUM:
63
+ return value;
64
+ default:
65
+ return rb_funcall(value, s_id_to_i, 0);
66
+ }
67
+ }
68
+
69
+ /*
70
+ * Document-class: PG::TextEncoder::Boolean < PG::SimpleEncoder
71
+ *
72
+ * This is the encoder class for the PostgreSQL bool type.
73
+ *
74
+ * Ruby value false is encoded as SQL +FALSE+ value.
75
+ * Ruby value true is encoded as SQL +TRUE+ value.
76
+ * Any other value is sent as it's string representation.
77
+ *
78
+ */
79
+ static int
80
+ pg_text_enc_boolean(t_pg_coder *this, VALUE value, char *out, VALUE *intermediate, int enc_idx)
81
+ {
82
+ switch( TYPE(value) ){
83
+ case T_FALSE:
84
+ if(out) *out = 'f';
85
+ return 1;
86
+ case T_TRUE:
87
+ if(out) *out = 't';
88
+ return 1;
89
+ case T_FIXNUM:
90
+ case T_BIGNUM:
91
+ if( NUM2LONG(value) == 0 ){
92
+ if(out) *out = '0';
93
+ return 1;
94
+ } else if( NUM2LONG(value) == 1 ){
95
+ if(out) *out = '1';
96
+ return 1;
97
+ } else {
98
+ return pg_text_enc_integer(this, value, out, intermediate, enc_idx);
99
+ }
100
+ default:
101
+ return pg_coder_enc_to_s(this, value, out, intermediate, enc_idx);
102
+ }
103
+ /* never reached */
104
+ return 0;
105
+ }
106
+
107
+
108
+ /*
109
+ * Document-class: PG::TextEncoder::String < PG::SimpleEncoder
110
+ *
111
+ * This is the encoder class for the PostgreSQL text types.
112
+ *
113
+ * Non-String values are expected to have method +to_s+ defined.
114
+ *
115
+ */
116
+ int
117
+ pg_coder_enc_to_s(t_pg_coder *this, VALUE value, char *out, VALUE *intermediate, int enc_idx)
118
+ {
119
+ VALUE str = rb_obj_as_string(value);
120
+ if( ENCODING_GET(str) == enc_idx ){
121
+ *intermediate = str;
122
+ }else{
123
+ *intermediate = rb_str_export_to_enc(str, rb_enc_from_index(enc_idx));
124
+ }
125
+ return -1;
126
+ }
127
+
128
+
129
+ /*
130
+ * Document-class: PG::TextEncoder::Integer < PG::SimpleEncoder
131
+ *
132
+ * This is the encoder class for the PostgreSQL int types.
133
+ *
134
+ * Non-Integer values are expected to have method +to_i+ defined.
135
+ *
136
+ */
137
+ static int
138
+ pg_text_enc_integer(t_pg_coder *this, VALUE value, char *out, VALUE *intermediate, int enc_idx)
139
+ {
140
+ if(out){
141
+ if(TYPE(*intermediate) == T_STRING){
142
+ return pg_coder_enc_to_s(this, value, out, intermediate, enc_idx);
143
+ }else{
144
+ char *start = out;
145
+ int len;
146
+ int neg = 0;
147
+ long long ll = NUM2LL(*intermediate);
148
+
149
+ if (ll < 0) {
150
+ /* We don't expect problems with the most negative integer not being representable
151
+ * as a positive integer, because Fixnum is only up to 63 bits.
152
+ */
153
+ ll = -ll;
154
+ neg = 1;
155
+ }
156
+
157
+ /* Compute the result string backwards. */
158
+ do {
159
+ long long remainder;
160
+ long long oldval = ll;
161
+
162
+ ll /= 10;
163
+ remainder = oldval - ll * 10;
164
+ *out++ = '0' + remainder;
165
+ } while (ll != 0);
166
+
167
+ if (neg)
168
+ *out++ = '-';
169
+
170
+ len = out - start;
171
+
172
+ /* Reverse string. */
173
+ out--;
174
+ while (start < out)
175
+ {
176
+ char swap = *start;
177
+
178
+ *start++ = *out;
179
+ *out-- = swap;
180
+ }
181
+
182
+ return len;
183
+ }
184
+ }else{
185
+ *intermediate = pg_obj_to_i(value);
186
+ if(TYPE(*intermediate) == T_FIXNUM){
187
+ int len;
188
+ long long sll = NUM2LL(*intermediate);
189
+ long long ll = sll < 0 ? -sll : sll;
190
+ if( ll < 100000000 ){
191
+ if( ll < 10000 ){
192
+ if( ll < 100 ){
193
+ len = ll < 10 ? 1 : 2;
194
+ }else{
195
+ len = ll < 1000 ? 3 : 4;
196
+ }
197
+ }else{
198
+ if( ll < 1000000 ){
199
+ len = ll < 100000 ? 5 : 6;
200
+ }else{
201
+ len = ll < 10000000 ? 7 : 8;
202
+ }
203
+ }
204
+ }else{
205
+ if( ll < 1000000000000LL ){
206
+ if( ll < 10000000000LL ){
207
+ len = ll < 1000000000LL ? 9 : 10;
208
+ }else{
209
+ len = ll < 100000000000LL ? 11 : 12;
210
+ }
211
+ }else{
212
+ if( ll < 100000000000000LL ){
213
+ len = ll < 10000000000000LL ? 13 : 14;
214
+ }else{
215
+ return pg_coder_enc_to_s(this, *intermediate, NULL, intermediate, enc_idx);
216
+ }
217
+ }
218
+ }
219
+ return sll < 0 ? len+1 : len;
220
+ }else{
221
+ return pg_coder_enc_to_s(this, *intermediate, NULL, intermediate, enc_idx);
222
+ }
223
+ }
224
+ }
225
+
226
+
227
+ /*
228
+ * Document-class: PG::TextEncoder::Float < PG::SimpleEncoder
229
+ *
230
+ * This is the encoder class for the PostgreSQL float types.
231
+ *
232
+ */
233
+ static int
234
+ pg_text_enc_float(t_pg_coder *conv, VALUE value, char *out, VALUE *intermediate, int enc_idx)
235
+ {
236
+ if(out){
237
+ double dvalue = NUM2DBL(value);
238
+ /* Cast to the same strings as value.to_s . */
239
+ if( isinf(dvalue) ){
240
+ if( dvalue < 0 ){
241
+ memcpy( out, "-Infinity", 9);
242
+ return 9;
243
+ } else {
244
+ memcpy( out, "Infinity", 8);
245
+ return 8;
246
+ }
247
+ } else if (isnan(dvalue)) {
248
+ memcpy( out, "NaN", 3);
249
+ return 3;
250
+ }
251
+ return sprintf( out, "%.16E", dvalue);
252
+ }else{
253
+ return 23;
254
+ }
255
+ }
256
+
257
+ static const char hextab[] = {
258
+ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
259
+ };
260
+
261
+ /*
262
+ * Document-class: PG::TextEncoder::Bytea < PG::SimpleEncoder
263
+ *
264
+ * This is an encoder class for the PostgreSQL bytea type for server version 9.0
265
+ * or newer.
266
+ *
267
+ * The binary String is converted to hexadecimal representation for transmission
268
+ * in text format. For query bind parameters it is recommended to use
269
+ * PG::BinaryEncoder::Bytea instead, in order to decrease network traffic and
270
+ * CPU usage.
271
+ *
272
+ */
273
+ static int
274
+ pg_text_enc_bytea(t_pg_coder *conv, VALUE value, char *out, VALUE *intermediate, int enc_idx)
275
+ {
276
+ if(out){
277
+ size_t strlen = RSTRING_LEN(*intermediate);
278
+ char *iptr = RSTRING_PTR(*intermediate);
279
+ char *eptr = iptr + strlen;
280
+ char *optr = out;
281
+ *optr++ = '\\';
282
+ *optr++ = 'x';
283
+
284
+ for( ; iptr < eptr; iptr++ ){
285
+ unsigned char c = *iptr;
286
+ *optr++ = hextab[c >> 4];
287
+ *optr++ = hextab[c & 0xf];
288
+ }
289
+ return optr - out;
290
+ }else{
291
+ *intermediate = rb_obj_as_string(value);
292
+ /* The output starts with "\x" and each character is converted to hex. */
293
+ return 2 + RSTRING_LEN(*intermediate) * 2;
294
+ }
295
+ }
296
+
297
+ typedef int (*t_quote_func)( void *_this, char *p_in, int strlen, char *p_out );
298
+
299
+ static int
300
+ quote_array_buffer( void *_this, char *p_in, int strlen, char *p_out ){
301
+ t_pg_composite_coder *this = _this;
302
+ char *ptr1;
303
+ char *ptr2;
304
+ int backslashs = 0;
305
+ int needquote;
306
+
307
+ /* count data plus backslashes; detect chars needing quotes */
308
+ if (strlen == 0)
309
+ needquote = 1; /* force quotes for empty string */
310
+ else if (strlen == 4 && rbpg_strncasecmp(p_in, "NULL", strlen) == 0)
311
+ needquote = 1; /* force quotes for literal NULL */
312
+ else
313
+ needquote = 0;
314
+
315
+ /* count required backlashs */
316
+ for(ptr1 = p_in; ptr1 != p_in + strlen; ptr1++) {
317
+ char ch = *ptr1;
318
+
319
+ if (ch == '"' || ch == '\\'){
320
+ needquote = 1;
321
+ backslashs++;
322
+ } else if (ch == '{' || ch == '}' || ch == this->delimiter ||
323
+ ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r' || ch == '\v' || ch == '\f'){
324
+ needquote = 1;
325
+ }
326
+ }
327
+
328
+ if( needquote ){
329
+ ptr1 = p_in + strlen;
330
+ ptr2 = p_out + strlen + backslashs + 2;
331
+ /* Write end quote */
332
+ *--ptr2 = '"';
333
+
334
+ /* Then store the escaped string on the final position, walking
335
+ * right to left, until all backslashs are placed. */
336
+ while( ptr1 != p_in ) {
337
+ *--ptr2 = *--ptr1;
338
+ if(*ptr2 == '"' || *ptr2 == '\\'){
339
+ *--ptr2 = '\\';
340
+ }
341
+ }
342
+ /* Write start quote */
343
+ *p_out = '"';
344
+ return strlen + backslashs + 2;
345
+ } else {
346
+ if( p_in != p_out )
347
+ memcpy( p_out, p_in, strlen );
348
+ return strlen;
349
+ }
350
+ }
351
+
352
+ static char *
353
+ quote_string(t_pg_coder *this, VALUE value, VALUE string, char *current_out, int with_quote, t_quote_func quote_buffer, void *func_data, int enc_idx)
354
+ {
355
+ int strlen;
356
+ VALUE subint;
357
+ t_pg_coder_enc_func enc_func = pg_coder_enc_func(this);
358
+
359
+ strlen = enc_func(this, value, NULL, &subint, enc_idx);
360
+
361
+ if( strlen == -1 ){
362
+ /* we can directly use String value in subint */
363
+ strlen = RSTRING_LENINT(subint);
364
+
365
+ if(with_quote){
366
+ /* size of string assuming the worst case, that every character must be escaped. */
367
+ current_out = pg_rb_str_ensure_capa( string, strlen * 2 + 2, current_out, NULL );
368
+
369
+ current_out += quote_buffer( func_data, RSTRING_PTR(subint), strlen, current_out );
370
+ } else {
371
+ current_out = pg_rb_str_ensure_capa( string, strlen, current_out, NULL );
372
+ memcpy( current_out, RSTRING_PTR(subint), strlen );
373
+ current_out += strlen;
374
+ }
375
+
376
+ } else {
377
+
378
+ if(with_quote){
379
+ /* size of string assuming the worst case, that every character must be escaped
380
+ * plus two bytes for quotation.
381
+ */
382
+ current_out = pg_rb_str_ensure_capa( string, 2 * strlen + 2, current_out, NULL );
383
+
384
+ /* Place the unescaped string at current output position. */
385
+ strlen = enc_func(this, value, current_out, &subint, enc_idx);
386
+
387
+ current_out += quote_buffer( func_data, current_out, strlen, current_out );
388
+ }else{
389
+ /* size of the unquoted string */
390
+ current_out = pg_rb_str_ensure_capa( string, strlen, current_out, NULL );
391
+ current_out += enc_func(this, value, current_out, &subint, enc_idx);
392
+ }
393
+ }
394
+ return current_out;
395
+ }
396
+
397
+ static char *
398
+ write_array(t_pg_composite_coder *this, VALUE value, char *current_out, VALUE string, int quote, int enc_idx)
399
+ {
400
+ int i;
401
+
402
+ /* size of "{}" */
403
+ current_out = pg_rb_str_ensure_capa( string, 2, current_out, NULL );
404
+ *current_out++ = '{';
405
+
406
+ for( i=0; i<RARRAY_LEN(value); i++){
407
+ VALUE entry = rb_ary_entry(value, i);
408
+
409
+ if( i > 0 ){
410
+ current_out = pg_rb_str_ensure_capa( string, 1, current_out, NULL );
411
+ *current_out++ = this->delimiter;
412
+ }
413
+
414
+ switch(TYPE(entry)){
415
+ case T_ARRAY:
416
+ current_out = write_array(this, entry, current_out, string, quote, enc_idx);
417
+ break;
418
+ case T_NIL:
419
+ current_out = pg_rb_str_ensure_capa( string, 4, current_out, NULL );
420
+ *current_out++ = 'N';
421
+ *current_out++ = 'U';
422
+ *current_out++ = 'L';
423
+ *current_out++ = 'L';
424
+ break;
425
+ default:
426
+ current_out = quote_string( this->elem, entry, string, current_out, quote, quote_array_buffer, this, enc_idx );
427
+ }
428
+ }
429
+ current_out = pg_rb_str_ensure_capa( string, 1, current_out, NULL );
430
+ *current_out++ = '}';
431
+ return current_out;
432
+ }
433
+
434
+
435
+ /*
436
+ * Document-class: PG::TextEncoder::Array < PG::CompositeEncoder
437
+ *
438
+ * This is the encoder class for PostgreSQL array types.
439
+ *
440
+ * All values are encoded according to the #elements_type
441
+ * accessor. Sub-arrays are encoded recursively.
442
+ *
443
+ * This encoder expects an Array of values or sub-arrays as input.
444
+ * Other values are passed through as text without interpretation.
445
+ *
446
+ */
447
+ static int
448
+ pg_text_enc_array(t_pg_coder *conv, VALUE value, char *out, VALUE *intermediate, int enc_idx)
449
+ {
450
+ char *end_ptr;
451
+ t_pg_composite_coder *this = (t_pg_composite_coder *)conv;
452
+
453
+ if( TYPE(value) == T_ARRAY){
454
+ VALUE out_str = rb_str_new(NULL, 0);
455
+ PG_ENCODING_SET_NOCHECK(out_str, enc_idx);
456
+
457
+ end_ptr = write_array(this, value, RSTRING_PTR(out_str), out_str, this->needs_quotation, enc_idx);
458
+
459
+ rb_str_set_len( out_str, end_ptr - RSTRING_PTR(out_str) );
460
+ *intermediate = out_str;
461
+
462
+ return -1;
463
+ } else {
464
+ return pg_coder_enc_to_s( conv, value, out, intermediate, enc_idx );
465
+ }
466
+ }
467
+
468
+ static char *
469
+ quote_identifier( VALUE value, VALUE out_string, char *current_out ){
470
+ char *p_in = RSTRING_PTR(value);
471
+ size_t strlen = RSTRING_LEN(value);
472
+ char *p_inend = p_in + strlen;
473
+ char *end_capa = current_out;
474
+
475
+ PG_RB_STR_ENSURE_CAPA( out_string, strlen + 2, current_out, end_capa );
476
+ *current_out++ = '"';
477
+ for(; p_in != p_inend; p_in++) {
478
+ char c = *p_in;
479
+ if (c == '"'){
480
+ PG_RB_STR_ENSURE_CAPA( out_string, p_inend - p_in + 2, current_out, end_capa );
481
+ *current_out++ = '"';
482
+ } else if (c == 0){
483
+ rb_raise(rb_eArgError, "string contains null byte");
484
+ }
485
+ *current_out++ = c;
486
+ }
487
+ PG_RB_STR_ENSURE_CAPA( out_string, 1, current_out, end_capa );
488
+ *current_out++ = '"';
489
+
490
+ return current_out;
491
+ }
492
+
493
+ static char *
494
+ pg_text_enc_array_identifier(VALUE value, VALUE string, char *out, int enc_idx)
495
+ {
496
+ int i;
497
+ int nr_elems;
498
+
499
+ Check_Type(value, T_ARRAY);
500
+ nr_elems = RARRAY_LEN(value);
501
+
502
+ for( i=0; i<nr_elems; i++){
503
+ VALUE entry = rb_ary_entry(value, i);
504
+
505
+ StringValue(entry);
506
+ if( ENCODING_GET(entry) != enc_idx ){
507
+ entry = rb_str_export_to_enc(entry, rb_enc_from_index(enc_idx));
508
+ }
509
+ out = quote_identifier(entry, string, out);
510
+ if( i < nr_elems-1 ){
511
+ out = pg_rb_str_ensure_capa( string, 1, out, NULL );
512
+ *out++ = '.';
513
+ }
514
+ }
515
+ return out;
516
+ }
517
+
518
+ /*
519
+ * Document-class: PG::TextEncoder::Identifier < PG::SimpleEncoder
520
+ *
521
+ * This is the encoder class for PostgreSQL identifiers.
522
+ *
523
+ * An Array value can be used for "schema.table.column" type identifiers:
524
+ * PG::TextEncoder::Identifier.new.encode(['schema', 'table', 'column'])
525
+ * => '"schema"."table"."column"'
526
+ *
527
+ * This encoder can also be used per PG::Connection#quote_ident .
528
+ */
529
+ int
530
+ pg_text_enc_identifier(t_pg_coder *this, VALUE value, char *out, VALUE *intermediate, int enc_idx)
531
+ {
532
+ VALUE out_str;
533
+ UNUSED( this );
534
+ if( TYPE(value) == T_ARRAY){
535
+ out_str = rb_str_new(NULL, 0);
536
+ out = RSTRING_PTR(out_str);
537
+ out = pg_text_enc_array_identifier(value, out_str, out, enc_idx);
538
+ } else {
539
+ StringValue(value);
540
+ if( ENCODING_GET(value) != enc_idx ){
541
+ value = rb_str_export_to_enc(value, rb_enc_from_index(enc_idx));
542
+ }
543
+ out_str = rb_str_new(NULL, RSTRING_LEN(value) + 2);
544
+ out = RSTRING_PTR(out_str);
545
+ out = quote_identifier(value, out_str, out);
546
+ }
547
+ rb_str_set_len( out_str, out - RSTRING_PTR(out_str) );
548
+ PG_ENCODING_SET_NOCHECK(out_str, enc_idx);
549
+ *intermediate = out_str;
550
+ return -1;
551
+ }
552
+
553
+
554
+ static int
555
+ quote_literal_buffer( void *_this, char *p_in, int strlen, char *p_out ){
556
+ char *ptr1;
557
+ char *ptr2;
558
+ int backslashs = 0;
559
+
560
+ /* count required backlashs */
561
+ for(ptr1 = p_in; ptr1 != p_in + strlen; ptr1++) {
562
+ if (*ptr1 == '\''){
563
+ backslashs++;
564
+ }
565
+ }
566
+
567
+ ptr1 = p_in + strlen;
568
+ ptr2 = p_out + strlen + backslashs + 2;
569
+ /* Write end quote */
570
+ *--ptr2 = '\'';
571
+
572
+ /* Then store the escaped string on the final position, walking
573
+ * right to left, until all backslashs are placed. */
574
+ while( ptr1 != p_in ) {
575
+ *--ptr2 = *--ptr1;
576
+ if(*ptr2 == '\''){
577
+ *--ptr2 = '\'';
578
+ }
579
+ }
580
+ /* Write start quote */
581
+ *p_out = '\'';
582
+ return strlen + backslashs + 2;
583
+ }
584
+
585
+
586
+ /*
587
+ * Document-class: PG::TextEncoder::QuotedLiteral < PG::CompositeEncoder
588
+ *
589
+ * This is the encoder class for PostgreSQL literals.
590
+ *
591
+ * A literal is quoted and escaped by the +'+ character.
592
+ *
593
+ */
594
+ static int
595
+ pg_text_enc_quoted_literal(t_pg_coder *conv, VALUE value, char *out, VALUE *intermediate, int enc_idx)
596
+ {
597
+ t_pg_composite_coder *this = (t_pg_composite_coder *)conv;
598
+ VALUE out_str = rb_str_new(NULL, 0);
599
+ PG_ENCODING_SET_NOCHECK(out_str, enc_idx);
600
+
601
+ out = RSTRING_PTR(out_str);
602
+ out = quote_string(this->elem, value, out_str, out, this->needs_quotation, quote_literal_buffer, this, enc_idx);
603
+ rb_str_set_len( out_str, out - RSTRING_PTR(out_str) );
604
+ *intermediate = out_str;
605
+ return -1;
606
+ }
607
+
608
+ /*
609
+ * Document-class: PG::TextEncoder::ToBase64 < PG::CompositeEncoder
610
+ *
611
+ * This is an encoder class for conversion of binary to base64 data.
612
+ *
613
+ */
614
+ static int
615
+ pg_text_enc_to_base64(t_pg_coder *conv, VALUE value, char *out, VALUE *intermediate, int enc_idx)
616
+ {
617
+ int strlen;
618
+ VALUE subint;
619
+ t_pg_composite_coder *this = (t_pg_composite_coder *)conv;
620
+ t_pg_coder_enc_func enc_func = pg_coder_enc_func(this->elem);
621
+
622
+ if(out){
623
+ /* Second encoder pass, if required */
624
+ strlen = enc_func(this->elem, value, out, intermediate, enc_idx);
625
+ base64_encode( out, out, strlen );
626
+
627
+ return BASE64_ENCODED_SIZE(strlen);
628
+ } else {
629
+ /* First encoder pass */
630
+ strlen = enc_func(this->elem, value, NULL, &subint, enc_idx);
631
+
632
+ if( strlen == -1 ){
633
+ /* Encoded string is returned in subint */
634
+ VALUE out_str;
635
+
636
+ strlen = RSTRING_LENINT(subint);
637
+ out_str = rb_str_new(NULL, BASE64_ENCODED_SIZE(strlen));
638
+ PG_ENCODING_SET_NOCHECK(out_str, enc_idx);
639
+
640
+ base64_encode( RSTRING_PTR(out_str), RSTRING_PTR(subint), strlen);
641
+ *intermediate = out_str;
642
+
643
+ return -1;
644
+ } else {
645
+ *intermediate = subint;
646
+
647
+ return BASE64_ENCODED_SIZE(strlen);
648
+ }
649
+ }
650
+ }
651
+
652
+
653
+ void
654
+ init_pg_text_encoder()
655
+ {
656
+ s_id_encode = rb_intern("encode");
657
+ s_id_to_i = rb_intern("to_i");
658
+
659
+ /* This module encapsulates all encoder classes with text output format */
660
+ rb_mPG_TextEncoder = rb_define_module_under( rb_mPG, "TextEncoder" );
661
+
662
+ /* Make RDoc aware of the encoder classes... */
663
+ /* dummy = rb_define_class_under( rb_mPG_TextEncoder, "Boolean", rb_cPG_SimpleEncoder ); */
664
+ pg_define_coder( "Boolean", pg_text_enc_boolean, rb_cPG_SimpleEncoder, rb_mPG_TextEncoder );
665
+ /* dummy = rb_define_class_under( rb_mPG_TextEncoder, "Integer", rb_cPG_SimpleEncoder ); */
666
+ pg_define_coder( "Integer", pg_text_enc_integer, rb_cPG_SimpleEncoder, rb_mPG_TextEncoder );
667
+ /* dummy = rb_define_class_under( rb_mPG_TextEncoder, "Float", rb_cPG_SimpleEncoder ); */
668
+ pg_define_coder( "Float", pg_text_enc_float, rb_cPG_SimpleEncoder, rb_mPG_TextEncoder );
669
+ /* dummy = rb_define_class_under( rb_mPG_TextEncoder, "String", rb_cPG_SimpleEncoder ); */
670
+ pg_define_coder( "String", pg_coder_enc_to_s, rb_cPG_SimpleEncoder, rb_mPG_TextEncoder );
671
+ /* dummy = rb_define_class_under( rb_mPG_TextEncoder, "Bytea", rb_cPG_SimpleEncoder ); */
672
+ pg_define_coder( "Bytea", pg_text_enc_bytea, rb_cPG_SimpleEncoder, rb_mPG_TextEncoder );
673
+ /* dummy = rb_define_class_under( rb_mPG_TextEncoder, "Identifier", rb_cPG_SimpleEncoder ); */
674
+ pg_define_coder( "Identifier", pg_text_enc_identifier, rb_cPG_SimpleEncoder, rb_mPG_TextEncoder );
675
+
676
+ /* dummy = rb_define_class_under( rb_mPG_TextEncoder, "Array", rb_cPG_CompositeEncoder ); */
677
+ pg_define_coder( "Array", pg_text_enc_array, rb_cPG_CompositeEncoder, rb_mPG_TextEncoder );
678
+ /* dummy = rb_define_class_under( rb_mPG_TextEncoder, "QuotedLiteral", rb_cPG_CompositeEncoder ); */
679
+ pg_define_coder( "QuotedLiteral", pg_text_enc_quoted_literal, rb_cPG_CompositeEncoder, rb_mPG_TextEncoder );
680
+ /* dummy = rb_define_class_under( rb_mPG_TextEncoder, "ToBase64", rb_cPG_CompositeEncoder ); */
681
+ pg_define_coder( "ToBase64", pg_text_enc_to_base64, rb_cPG_CompositeEncoder, rb_mPG_TextEncoder );
682
+ }