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.
Files changed (118) hide show
  1. checksums.yaml +7 -0
  2. checksums.yaml.gz.sig +4 -0
  3. data/BSDL +22 -0
  4. data/Contributors.rdoc +46 -0
  5. data/Gemfile +23 -0
  6. data/History.md +958 -0
  7. data/LICENSE +56 -0
  8. data/Manifest.txt +72 -0
  9. data/POSTGRES +23 -0
  10. data/README-OS_X.rdoc +68 -0
  11. data/README-Windows.rdoc +56 -0
  12. data/README.ja.md +300 -0
  13. data/README.md +286 -0
  14. data/Rakefile +161 -0
  15. data/certs/ged.pem +24 -0
  16. data/certs/kanis@comcard.de.pem +20 -0
  17. data/certs/larskanis-2022.pem +26 -0
  18. data/certs/larskanis-2023.pem +24 -0
  19. data/certs/larskanis-2024.pem +24 -0
  20. data/ext/errorcodes.def +1043 -0
  21. data/ext/errorcodes.rb +45 -0
  22. data/ext/errorcodes.txt +494 -0
  23. data/ext/extconf.rb +282 -0
  24. data/ext/gvl_wrappers.c +32 -0
  25. data/ext/gvl_wrappers.h +297 -0
  26. data/ext/pg.c +703 -0
  27. data/ext/pg.h +390 -0
  28. data/ext/pg_binary_decoder.c +460 -0
  29. data/ext/pg_binary_encoder.c +583 -0
  30. data/ext/pg_cancel_connection.c +360 -0
  31. data/ext/pg_coder.c +622 -0
  32. data/ext/pg_connection.c +4869 -0
  33. data/ext/pg_copy_coder.c +921 -0
  34. data/ext/pg_errors.c +95 -0
  35. data/ext/pg_record_coder.c +522 -0
  36. data/ext/pg_result.c +1764 -0
  37. data/ext/pg_text_decoder.c +1008 -0
  38. data/ext/pg_text_encoder.c +833 -0
  39. data/ext/pg_tuple.c +572 -0
  40. data/ext/pg_type_map.c +200 -0
  41. data/ext/pg_type_map_all_strings.c +130 -0
  42. data/ext/pg_type_map_by_class.c +271 -0
  43. data/ext/pg_type_map_by_column.c +355 -0
  44. data/ext/pg_type_map_by_mri_type.c +313 -0
  45. data/ext/pg_type_map_by_oid.c +388 -0
  46. data/ext/pg_type_map_in_ruby.c +333 -0
  47. data/ext/pg_util.c +149 -0
  48. data/ext/pg_util.h +65 -0
  49. data/ext/vc/pg.sln +26 -0
  50. data/ext/vc/pg_18/pg.vcproj +216 -0
  51. data/ext/vc/pg_19/pg_19.vcproj +209 -0
  52. data/lib/2.7/pg_ext.so +0 -0
  53. data/lib/3.0/pg_ext.so +0 -0
  54. data/lib/3.1/pg_ext.so +0 -0
  55. data/lib/3.2/pg_ext.so +0 -0
  56. data/lib/3.3/pg_ext.so +0 -0
  57. data/lib/pg/basic_type_map_based_on_result.rb +67 -0
  58. data/lib/pg/basic_type_map_for_queries.rb +202 -0
  59. data/lib/pg/basic_type_map_for_results.rb +104 -0
  60. data/lib/pg/basic_type_registry.rb +311 -0
  61. data/lib/pg/binary_decoder/date.rb +9 -0
  62. data/lib/pg/binary_decoder/timestamp.rb +26 -0
  63. data/lib/pg/binary_encoder/timestamp.rb +20 -0
  64. data/lib/pg/cancel_connection.rb +30 -0
  65. data/lib/pg/coder.rb +106 -0
  66. data/lib/pg/connection.rb +1027 -0
  67. data/lib/pg/exceptions.rb +31 -0
  68. data/lib/pg/result.rb +43 -0
  69. data/lib/pg/text_decoder/date.rb +21 -0
  70. data/lib/pg/text_decoder/inet.rb +9 -0
  71. data/lib/pg/text_decoder/json.rb +17 -0
  72. data/lib/pg/text_decoder/numeric.rb +9 -0
  73. data/lib/pg/text_decoder/timestamp.rb +30 -0
  74. data/lib/pg/text_encoder/date.rb +13 -0
  75. data/lib/pg/text_encoder/inet.rb +31 -0
  76. data/lib/pg/text_encoder/json.rb +17 -0
  77. data/lib/pg/text_encoder/numeric.rb +9 -0
  78. data/lib/pg/text_encoder/timestamp.rb +24 -0
  79. data/lib/pg/tuple.rb +30 -0
  80. data/lib/pg/type_map_by_column.rb +16 -0
  81. data/lib/pg/version.rb +4 -0
  82. data/lib/pg.rb +144 -0
  83. data/misc/openssl-pg-segfault.rb +31 -0
  84. data/misc/postgres/History.txt +9 -0
  85. data/misc/postgres/Manifest.txt +5 -0
  86. data/misc/postgres/README.txt +21 -0
  87. data/misc/postgres/Rakefile +21 -0
  88. data/misc/postgres/lib/postgres.rb +16 -0
  89. data/misc/ruby-pg/History.txt +9 -0
  90. data/misc/ruby-pg/Manifest.txt +5 -0
  91. data/misc/ruby-pg/README.txt +21 -0
  92. data/misc/ruby-pg/Rakefile +21 -0
  93. data/misc/ruby-pg/lib/ruby/pg.rb +16 -0
  94. data/pg.gemspec +36 -0
  95. data/ports/x86_64-linux/lib/libpq-ruby-pg.so.1 +0 -0
  96. data/rakelib/task_extension.rb +46 -0
  97. data/sample/array_insert.rb +20 -0
  98. data/sample/async_api.rb +102 -0
  99. data/sample/async_copyto.rb +39 -0
  100. data/sample/async_mixed.rb +56 -0
  101. data/sample/check_conn.rb +21 -0
  102. data/sample/copydata.rb +71 -0
  103. data/sample/copyfrom.rb +81 -0
  104. data/sample/copyto.rb +19 -0
  105. data/sample/cursor.rb +21 -0
  106. data/sample/disk_usage_report.rb +177 -0
  107. data/sample/issue-119.rb +94 -0
  108. data/sample/losample.rb +69 -0
  109. data/sample/minimal-testcase.rb +17 -0
  110. data/sample/notify_wait.rb +72 -0
  111. data/sample/pg_statistics.rb +285 -0
  112. data/sample/replication_monitor.rb +222 -0
  113. data/sample/test_binary_values.rb +33 -0
  114. data/sample/wal_shipper.rb +434 -0
  115. data/sample/warehouse_partitions.rb +311 -0
  116. data.tar.gz.sig +0 -0
  117. metadata +252 -0
  118. metadata.gz.sig +0 -0
@@ -0,0 +1,833 @@
1
+ /*
2
+ * pg_text_encoder.c - PG::TextEncoder module
3
+ * $Id$
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 "pg_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
+ static ID s_id_to_s;
54
+ static ID s_cBigDecimal;
55
+ static VALUE s_str_F;
56
+
57
+ static int pg_text_enc_integer(t_pg_coder *this, VALUE value, char *out, VALUE *intermediate, int enc_idx);
58
+
59
+ VALUE
60
+ pg_obj_to_i( VALUE value )
61
+ {
62
+ switch (TYPE(value)) {
63
+ case T_FIXNUM:
64
+ case T_FLOAT:
65
+ case T_BIGNUM:
66
+ return value;
67
+ default:
68
+ return rb_funcall(value, s_id_to_i, 0);
69
+ }
70
+ }
71
+
72
+ /*
73
+ * Document-class: PG::TextEncoder::Boolean < PG::SimpleEncoder
74
+ *
75
+ * This is the encoder class for the PostgreSQL bool type.
76
+ *
77
+ * Ruby value false is encoded as SQL +FALSE+ value.
78
+ * Ruby value true is encoded as SQL +TRUE+ value.
79
+ * Any other value is sent as it's string representation.
80
+ *
81
+ */
82
+ static int
83
+ pg_text_enc_boolean(t_pg_coder *this, VALUE value, char *out, VALUE *intermediate, int enc_idx)
84
+ {
85
+ switch( TYPE(value) ){
86
+ case T_FALSE:
87
+ if(out) *out = 'f';
88
+ return 1;
89
+ case T_TRUE:
90
+ if(out) *out = 't';
91
+ return 1;
92
+ case T_FIXNUM:
93
+ case T_BIGNUM:
94
+ if( NUM2LONG(value) == 0 ){
95
+ if(out) *out = '0';
96
+ return 1;
97
+ } else if( NUM2LONG(value) == 1 ){
98
+ if(out) *out = '1';
99
+ return 1;
100
+ } else {
101
+ return pg_text_enc_integer(this, value, out, intermediate, enc_idx);
102
+ }
103
+ default:
104
+ return pg_coder_enc_to_s(this, value, out, intermediate, enc_idx);
105
+ }
106
+ /* never reached */
107
+ return 0;
108
+ }
109
+
110
+
111
+ /*
112
+ * Document-class: PG::TextEncoder::String < PG::SimpleEncoder
113
+ *
114
+ * This is the encoder class for the PostgreSQL text types.
115
+ *
116
+ * Non-String values are expected to have method +to_s+ defined.
117
+ *
118
+ */
119
+ int
120
+ pg_coder_enc_to_s(t_pg_coder *this, VALUE value, char *out, VALUE *intermediate, int enc_idx)
121
+ {
122
+ /* Attention:
123
+ * In contrast to all other encoders, the "this" pointer of this encoder can be NULL.
124
+ * This is because it is used as a fall-back if no encoder is defined.
125
+ */
126
+ VALUE str = rb_obj_as_string(value);
127
+ if( ENCODING_GET(str) == enc_idx ){
128
+ *intermediate = str;
129
+ }else{
130
+ *intermediate = rb_str_export_to_enc(str, rb_enc_from_index(enc_idx));
131
+ }
132
+ return -1;
133
+ }
134
+
135
+ static int
136
+ count_leading_zero_bits(unsigned long long x)
137
+ {
138
+ #if defined(__GNUC__) || defined(__clang__)
139
+ return __builtin_clzll(x);
140
+ #elif defined(_MSC_VER)
141
+ DWORD r = 0;
142
+ _BitScanForward64(&r, x);
143
+ return (int)r;
144
+ #else
145
+ unsigned int a;
146
+ for(a=0; a < sizeof(unsigned long long) * 8; a++){
147
+ if( x & (1 << (sizeof(unsigned long long) * 8 - 1))) return a;
148
+ x <<= 1;
149
+ }
150
+ return a;
151
+ #endif
152
+ }
153
+
154
+ /*
155
+ * Document-class: PG::TextEncoder::Integer < PG::SimpleEncoder
156
+ *
157
+ * This is the encoder class for the PostgreSQL integer types.
158
+ *
159
+ * Non-Integer values are expected to have method +to_i+ defined.
160
+ *
161
+ */
162
+ static int
163
+ pg_text_enc_integer(t_pg_coder *this, VALUE value, char *out, VALUE *intermediate, int enc_idx)
164
+ {
165
+ if(out){
166
+ if(TYPE(*intermediate) == T_STRING){
167
+ return pg_coder_enc_to_s(this, value, out, intermediate, enc_idx);
168
+ }else{
169
+ char *start = out;
170
+ int len;
171
+ int neg = 0;
172
+ long long sll = NUM2LL(*intermediate);
173
+ unsigned long long ll;
174
+
175
+ if (sll < 0) {
176
+ /* Avoid problems with the most negative integer not being representable
177
+ * as a positive integer, by using unsigned long long for encoding.
178
+ */
179
+ ll = -sll;
180
+ neg = 1;
181
+ } else {
182
+ ll = sll;
183
+ }
184
+
185
+ /* Compute the result string backwards. */
186
+ do {
187
+ unsigned long long remainder;
188
+ unsigned long long oldval = ll;
189
+
190
+ ll /= 10;
191
+ remainder = oldval - ll * 10;
192
+ *out++ = '0' + remainder;
193
+ } while (ll != 0);
194
+
195
+ if (neg)
196
+ *out++ = '-';
197
+
198
+ len = (int)(out - start);
199
+
200
+ /* Reverse string. */
201
+ out--;
202
+ while (start < out)
203
+ {
204
+ char swap = *start;
205
+
206
+ *start++ = *out;
207
+ *out-- = swap;
208
+ }
209
+
210
+ return len;
211
+ }
212
+ }else{
213
+ *intermediate = pg_obj_to_i(value);
214
+ if(TYPE(*intermediate) == T_FIXNUM){
215
+ long long sll = NUM2LL(*intermediate);
216
+ unsigned long long ll = sll < 0 ? -sll : sll;
217
+ int len = (sizeof(unsigned long long) * 8 - count_leading_zero_bits(ll)) / 3;
218
+ return sll < 0 ? len+2 : len+1;
219
+ }else{
220
+ return pg_coder_enc_to_s(this, *intermediate, NULL, intermediate, enc_idx);
221
+ }
222
+ }
223
+ }
224
+
225
+ #define MAX_DOUBLE_DIGITS 16
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 *intermediate1, int enc_idx)
235
+ {
236
+ if(out){
237
+ double dvalue = NUM2DBL(value);
238
+ int len = 0;
239
+ int neg = 0;
240
+ int exp2i, exp10i, i;
241
+ unsigned long long ll, remainder, oldval;
242
+
243
+ /* Cast to the same strings as value.to_s . */
244
+ if( isinf(dvalue) ){
245
+ if( dvalue < 0 ){
246
+ memcpy( out, "-Infinity", 9);
247
+ return 9;
248
+ } else {
249
+ memcpy( out, "Infinity", 8);
250
+ return 8;
251
+ }
252
+ } else if (isnan(dvalue)) {
253
+ memcpy( out, "NaN", 3);
254
+ return 3;
255
+ }
256
+
257
+ /*
258
+ * The following computation is roughly a conversion kind of
259
+ * sprintf( out, "%.16E", dvalue);
260
+ */
261
+
262
+ /* write the algebraic sign */
263
+ if( dvalue < 0 ) {
264
+ dvalue = -dvalue;
265
+ *out++ = '-';
266
+ neg++;
267
+ }
268
+
269
+ /* retrieve the power of 2 exponent */
270
+ frexp(dvalue, &exp2i);
271
+ /* compute the power of 10 exponent */
272
+ exp10i = (int)floor(exp2i * 0.30102999566398114); /* Math.log(2)/Math.log(10) */
273
+ /* move the decimal point, so that we get an integer of MAX_DOUBLE_DIGITS decimal digits */
274
+ ll = (unsigned long long)(dvalue * pow(10, MAX_DOUBLE_DIGITS - 1 - exp10i) + 0.5);
275
+
276
+ /* avoid leading zeros due to inaccuracy of deriving exp10i from exp2i */
277
+ /* otherwise we would print "09.0" instead of "9.0" */
278
+ if( ll < 1000000000000000 ){ /* pow(10, MAX_DOUBLE_DIGITS-1) */
279
+ exp10i--;
280
+ ll *= 10;
281
+ }
282
+
283
+ if( exp10i <= -5 || exp10i >= 15 ) {
284
+ /* Write the float in exponent format (1.23e45) */
285
+ VALUE intermediate;
286
+
287
+ /* write fraction digits from right to left */
288
+ for( i = MAX_DOUBLE_DIGITS; i > 1; i--){
289
+ oldval = ll;
290
+ ll /= 10;
291
+ remainder = oldval - ll * 10;
292
+ /* omit trailing zeros */
293
+ if(remainder != 0 || len ) {
294
+ out[i] = '0' + remainder;
295
+ len++;
296
+ }
297
+ }
298
+
299
+ /* write decimal point */
300
+ if( len ){
301
+ out[1] = '.';
302
+ len++;
303
+ }
304
+
305
+ /* write remaining single digit left to the decimal point */
306
+ oldval = ll;
307
+ ll /= 10;
308
+ remainder = oldval - ll * 10;
309
+ out[0] = '0' + remainder;
310
+ len++;
311
+
312
+ /* write exponent */
313
+ out[len++] = 'e';
314
+ intermediate = INT2NUM(exp10i);
315
+
316
+ return neg + len + pg_text_enc_integer(conv, Qnil, out + len, &intermediate, enc_idx);
317
+ } else {
318
+ /* write the float in non exponent format (0.001234 or 123450.0) */
319
+
320
+ /* write digits from right to left */
321
+ int lz = exp10i < 0 ? 0 : exp10i;
322
+ for( i = MAX_DOUBLE_DIGITS - (exp10i < 0 ? exp10i : 0); i >= 0; i-- ){
323
+ oldval = ll;
324
+ ll /= 10;
325
+ remainder = oldval - ll * 10;
326
+ /* write decimal point */
327
+ if( i - 1 == lz ){
328
+ out[i--] = '.';
329
+ len++;
330
+ }
331
+ /* if possible then omit trailing zeros */
332
+ if(remainder != 0 || len || i - 2 == lz) {
333
+ out[i] = '0' + remainder;
334
+ len++;
335
+ }
336
+ }
337
+ return neg + len;
338
+ }
339
+ }else{
340
+ return 1 /*sign*/ + MAX_DOUBLE_DIGITS + 1 /*dot*/ + 1 /*e*/ + 1 /*exp sign*/ + 3 /*exp digits*/;
341
+ }
342
+ }
343
+
344
+
345
+ /*
346
+ * Document-class: PG::TextEncoder::Numeric < PG::SimpleEncoder
347
+ *
348
+ * This is the encoder class for the PostgreSQL numeric types.
349
+ *
350
+ * It converts Integer, Float and BigDecimal objects.
351
+ * All other objects are expected to respond to +to_s+.
352
+ *
353
+ * As soon as this class is used, it requires the 'bigdecimal' gem.
354
+ */
355
+ static int
356
+ pg_text_enc_numeric(t_pg_coder *this, VALUE value, char *out, VALUE *intermediate, int enc_idx)
357
+ {
358
+ switch(TYPE(value)){
359
+ case T_FIXNUM:
360
+ case T_BIGNUM:
361
+ return pg_text_enc_integer(this, value, out, intermediate, enc_idx);
362
+ case T_FLOAT:
363
+ return pg_text_enc_float(this, value, out, intermediate, enc_idx);
364
+ default:
365
+ if(out){ /* second pass */
366
+ rb_bug("unexpected value type: %d", TYPE(value));
367
+ } else { /* first pass */
368
+ if( rb_obj_is_kind_of(value, s_cBigDecimal) ){
369
+ /* value.to_s('F') */
370
+ *intermediate = rb_funcall(value, s_id_to_s, 1, s_str_F);
371
+ return -1; /* no second pass */
372
+ } else {
373
+ return pg_coder_enc_to_s(this, value, NULL, intermediate, enc_idx);
374
+ /* no second pass */
375
+ }
376
+ }
377
+ }
378
+ }
379
+
380
+ /* called per autoload when TextEncoder::Numeric is used */
381
+ static VALUE
382
+ init_pg_text_encoder_numeric(VALUE rb_mPG_TextDecoder)
383
+ {
384
+ s_str_F = rb_str_freeze(rb_str_new_cstr("F"));
385
+ rb_global_variable(&s_str_F);
386
+ rb_funcall(rb_mPG, rb_intern("require_bigdecimal_without_warning"), 0);
387
+ s_cBigDecimal = rb_const_get(rb_cObject, rb_intern("BigDecimal"));
388
+
389
+ /* dummy = rb_define_class_under( rb_mPG_TextEncoder, "Numeric", rb_cPG_SimpleEncoder ); */
390
+ pg_define_coder( "Numeric", pg_text_enc_numeric, rb_cPG_SimpleEncoder, rb_mPG_TextEncoder );
391
+
392
+ return Qnil;
393
+ }
394
+
395
+
396
+ static const char hextab[] = {
397
+ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
398
+ };
399
+
400
+ /*
401
+ * Document-class: PG::TextEncoder::Bytea < PG::SimpleEncoder
402
+ *
403
+ * This is an encoder class for the PostgreSQL +bytea+ type.
404
+ *
405
+ * The binary String is converted to hexadecimal representation for transmission
406
+ * in text format. For query bind parameters it is recommended to use
407
+ * PG::BinaryEncoder::Bytea or the hash form <tt>{value: binary_string, format: 1}</tt> instead,
408
+ * in order to decrease network traffic and CPU usage.
409
+ * See PG::Connection#exec_params for using the hash form.
410
+ *
411
+ * This encoder is particular useful when PG::TextEncoder::CopyRow is used with the COPY command.
412
+ * In this case there's no way to change the format of a single column to binary, so that the data have to be converted to bytea hex representation.
413
+ *
414
+ */
415
+ static int
416
+ pg_text_enc_bytea(t_pg_coder *conv, VALUE value, char *out, VALUE *intermediate, int enc_idx)
417
+ {
418
+ if(out){
419
+ size_t strlen = RSTRING_LEN(*intermediate);
420
+ char *iptr = RSTRING_PTR(*intermediate);
421
+ char *eptr = iptr + strlen;
422
+ char *optr = out;
423
+ *optr++ = '\\';
424
+ *optr++ = 'x';
425
+
426
+ for( ; iptr < eptr; iptr++ ){
427
+ unsigned char c = *iptr;
428
+ *optr++ = hextab[c >> 4];
429
+ *optr++ = hextab[c & 0xf];
430
+ }
431
+ return (int)(optr - out);
432
+ }else{
433
+ *intermediate = rb_obj_as_string(value);
434
+ /* The output starts with "\x" and each character is converted to hex. */
435
+ return 2 + RSTRING_LENINT(*intermediate) * 2;
436
+ }
437
+ }
438
+
439
+ typedef int (*t_quote_func)( void *_this, char *p_in, int strlen, char *p_out );
440
+
441
+ static int
442
+ quote_array_buffer( void *_this, char *p_in, int strlen, char *p_out ){
443
+ t_pg_composite_coder *this = _this;
444
+ char *ptr1;
445
+ char *ptr2;
446
+ int backslashes = 0;
447
+ int needquote;
448
+
449
+ /* count data plus backslashes; detect chars needing quotes */
450
+ if (strlen == 0)
451
+ needquote = 1; /* force quotes for empty string */
452
+ else if (strlen == 4 && rbpg_strncasecmp(p_in, "NULL", strlen) == 0)
453
+ needquote = 1; /* force quotes for literal NULL */
454
+ else
455
+ needquote = 0;
456
+
457
+ /* count required backlashs */
458
+ for(ptr1 = p_in; ptr1 != p_in + strlen; ptr1++) {
459
+ char ch = *ptr1;
460
+
461
+ if (ch == '"' || ch == '\\'){
462
+ needquote = 1;
463
+ backslashes++;
464
+ } else if (ch == '{' || ch == '}' || ch == this->delimiter ||
465
+ ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r' || ch == '\v' || ch == '\f'){
466
+ needquote = 1;
467
+ }
468
+ }
469
+
470
+ if( needquote ){
471
+ ptr1 = p_in + strlen;
472
+ ptr2 = p_out + strlen + backslashes + 2;
473
+ /* Write end quote */
474
+ *--ptr2 = '"';
475
+
476
+ /* Then store the escaped string on the final position, walking
477
+ * right to left, until all backslashes are placed. */
478
+ while( ptr1 != p_in ) {
479
+ *--ptr2 = *--ptr1;
480
+ if(*ptr2 == '"' || *ptr2 == '\\'){
481
+ *--ptr2 = '\\';
482
+ }
483
+ }
484
+ /* Write start quote */
485
+ *p_out = '"';
486
+ return strlen + backslashes + 2;
487
+ } else {
488
+ if( p_in != p_out )
489
+ memcpy( p_out, p_in, strlen );
490
+ return strlen;
491
+ }
492
+ }
493
+
494
+ static char *
495
+ 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)
496
+ {
497
+ int strlen;
498
+ VALUE subint;
499
+ t_pg_coder_enc_func enc_func = pg_coder_enc_func(this);
500
+
501
+ strlen = enc_func(this, value, NULL, &subint, enc_idx);
502
+
503
+ if( strlen == -1 ){
504
+ /* we can directly use String value in subint */
505
+ strlen = RSTRING_LENINT(subint);
506
+
507
+ if(with_quote){
508
+ /* size of string assuming the worst case, that every character must be escaped. */
509
+ current_out = pg_rb_str_ensure_capa( string, strlen * 2 + 2, current_out, NULL );
510
+
511
+ current_out += quote_buffer( func_data, RSTRING_PTR(subint), strlen, current_out );
512
+ } else {
513
+ current_out = pg_rb_str_ensure_capa( string, strlen, current_out, NULL );
514
+ memcpy( current_out, RSTRING_PTR(subint), strlen );
515
+ current_out += strlen;
516
+ }
517
+
518
+ } else {
519
+
520
+ if(with_quote){
521
+ /* size of string assuming the worst case, that every character must be escaped
522
+ * plus two bytes for quotation.
523
+ */
524
+ current_out = pg_rb_str_ensure_capa( string, 2 * strlen + 2, current_out, NULL );
525
+
526
+ /* Place the unescaped string at current output position. */
527
+ strlen = enc_func(this, value, current_out, &subint, enc_idx);
528
+
529
+ current_out += quote_buffer( func_data, current_out, strlen, current_out );
530
+ }else{
531
+ /* size of the unquoted string */
532
+ current_out = pg_rb_str_ensure_capa( string, strlen, current_out, NULL );
533
+ current_out += enc_func(this, value, current_out, &subint, enc_idx);
534
+ }
535
+ }
536
+ return current_out;
537
+ }
538
+
539
+ static char *
540
+ write_array(t_pg_composite_coder *this, VALUE value, char *current_out, VALUE string, int quote, int enc_idx)
541
+ {
542
+ int i;
543
+
544
+ /* size of "{}" */
545
+ current_out = pg_rb_str_ensure_capa( string, 2, current_out, NULL );
546
+ *current_out++ = '{';
547
+
548
+ for( i=0; i<RARRAY_LEN(value); i++){
549
+ VALUE entry = rb_ary_entry(value, i);
550
+
551
+ if( i > 0 ){
552
+ current_out = pg_rb_str_ensure_capa( string, 1, current_out, NULL );
553
+ *current_out++ = this->delimiter;
554
+ }
555
+
556
+ switch(TYPE(entry)){
557
+ case T_ARRAY:
558
+ current_out = write_array(this, entry, current_out, string, quote, enc_idx);
559
+ break;
560
+ case T_NIL:
561
+ current_out = pg_rb_str_ensure_capa( string, 4, current_out, NULL );
562
+ *current_out++ = 'N';
563
+ *current_out++ = 'U';
564
+ *current_out++ = 'L';
565
+ *current_out++ = 'L';
566
+ break;
567
+ default:
568
+ current_out = quote_string( this->elem, entry, string, current_out, quote, quote_array_buffer, this, enc_idx );
569
+ }
570
+ }
571
+ current_out = pg_rb_str_ensure_capa( string, 1, current_out, NULL );
572
+ *current_out++ = '}';
573
+ return current_out;
574
+ }
575
+
576
+
577
+ /*
578
+ * Document-class: PG::TextEncoder::Array < PG::CompositeEncoder
579
+ *
580
+ * This is the encoder class for PostgreSQL array types.
581
+ *
582
+ * All values are encoded according to the #elements_type
583
+ * accessor. Sub-arrays are encoded recursively.
584
+ *
585
+ * This encoder expects an Array of values or sub-arrays as input.
586
+ * Other values are passed through as text without interpretation.
587
+ *
588
+ */
589
+ static int
590
+ pg_text_enc_array(t_pg_coder *conv, VALUE value, char *out, VALUE *intermediate, int enc_idx)
591
+ {
592
+ char *end_ptr;
593
+ t_pg_composite_coder *this = (t_pg_composite_coder *)conv;
594
+
595
+ if( TYPE(value) == T_ARRAY){
596
+ VALUE out_str = rb_str_new(NULL, 0);
597
+ PG_ENCODING_SET_NOCHECK(out_str, enc_idx);
598
+
599
+ end_ptr = write_array(this, value, RSTRING_PTR(out_str), out_str, this->needs_quotation, enc_idx);
600
+
601
+ rb_str_set_len( out_str, end_ptr - RSTRING_PTR(out_str) );
602
+ *intermediate = out_str;
603
+
604
+ return -1;
605
+ } else {
606
+ return pg_coder_enc_to_s( conv, value, out, intermediate, enc_idx );
607
+ }
608
+ }
609
+
610
+ static char *
611
+ quote_identifier( VALUE value, VALUE out_string, char *current_out ){
612
+ char *p_in = RSTRING_PTR(value);
613
+ size_t strlen = RSTRING_LEN(value);
614
+ char *p_inend = p_in + strlen;
615
+ char *end_capa = current_out;
616
+
617
+ PG_RB_STR_ENSURE_CAPA( out_string, strlen + 2, current_out, end_capa );
618
+ *current_out++ = '"';
619
+ for(; p_in != p_inend; p_in++) {
620
+ char c = *p_in;
621
+ if (c == '"'){
622
+ PG_RB_STR_ENSURE_CAPA( out_string, p_inend - p_in + 2, current_out, end_capa );
623
+ *current_out++ = '"';
624
+ } else if (c == 0){
625
+ rb_raise(rb_eArgError, "string contains null byte");
626
+ }
627
+ *current_out++ = c;
628
+ }
629
+ PG_RB_STR_ENSURE_CAPA( out_string, 1, current_out, end_capa );
630
+ *current_out++ = '"';
631
+
632
+ return current_out;
633
+ }
634
+
635
+ static char *
636
+ pg_text_enc_array_identifier(VALUE value, VALUE string, char *out, int enc_idx)
637
+ {
638
+ long i;
639
+ long nr_elems;
640
+
641
+ Check_Type(value, T_ARRAY);
642
+ nr_elems = RARRAY_LEN(value);
643
+
644
+ for( i=0; i<nr_elems; i++){
645
+ VALUE entry = rb_ary_entry(value, i);
646
+
647
+ StringValue(entry);
648
+ if( ENCODING_GET(entry) != enc_idx ){
649
+ entry = rb_str_export_to_enc(entry, rb_enc_from_index(enc_idx));
650
+ }
651
+ out = quote_identifier(entry, string, out);
652
+ if( i < nr_elems-1 ){
653
+ out = pg_rb_str_ensure_capa( string, 1, out, NULL );
654
+ *out++ = '.';
655
+ }
656
+ }
657
+ return out;
658
+ }
659
+
660
+ /*
661
+ * Document-class: PG::TextEncoder::Identifier < PG::SimpleEncoder
662
+ *
663
+ * This is the encoder class for PostgreSQL identifiers.
664
+ *
665
+ * An Array value can be used for identifiers of the kind "schema.table.column".
666
+ * This ensures that each element is properly quoted:
667
+ * PG::TextEncoder::Identifier.new.encode(['schema', 'table', 'column'])
668
+ * => '"schema"."table"."column"'
669
+ *
670
+ * This encoder can also be used per PG::Connection#quote_ident .
671
+ */
672
+ int
673
+ pg_text_enc_identifier(t_pg_coder *this, VALUE value, char *out, VALUE *intermediate, int enc_idx)
674
+ {
675
+ VALUE out_str;
676
+ UNUSED( this );
677
+ if( TYPE(value) == T_ARRAY){
678
+ out_str = rb_str_new(NULL, 0);
679
+ out = RSTRING_PTR(out_str);
680
+ out = pg_text_enc_array_identifier(value, out_str, out, enc_idx);
681
+ } else {
682
+ StringValue(value);
683
+ if( ENCODING_GET(value) != enc_idx ){
684
+ value = rb_str_export_to_enc(value, rb_enc_from_index(enc_idx));
685
+ }
686
+ out_str = rb_str_new(NULL, RSTRING_LEN(value) + 2);
687
+ out = RSTRING_PTR(out_str);
688
+ out = quote_identifier(value, out_str, out);
689
+ }
690
+ rb_str_set_len( out_str, out - RSTRING_PTR(out_str) );
691
+ PG_ENCODING_SET_NOCHECK(out_str, enc_idx);
692
+ *intermediate = out_str;
693
+ return -1;
694
+ }
695
+
696
+
697
+ static int
698
+ quote_literal_buffer( void *_this, char *p_in, int strlen, char *p_out ){
699
+ char *ptr1;
700
+ char *ptr2;
701
+ int backslashes = 0;
702
+
703
+ /* count required backlashs */
704
+ for(ptr1 = p_in; ptr1 != p_in + strlen; ptr1++) {
705
+ if (*ptr1 == '\''){
706
+ backslashes++;
707
+ }
708
+ }
709
+
710
+ ptr1 = p_in + strlen;
711
+ ptr2 = p_out + strlen + backslashes + 2;
712
+ /* Write end quote */
713
+ *--ptr2 = '\'';
714
+
715
+ /* Then store the escaped string on the final position, walking
716
+ * right to left, until all backslashes are placed. */
717
+ while( ptr1 != p_in ) {
718
+ *--ptr2 = *--ptr1;
719
+ if(*ptr2 == '\''){
720
+ *--ptr2 = '\'';
721
+ }
722
+ }
723
+ /* Write start quote */
724
+ *p_out = '\'';
725
+ return strlen + backslashes + 2;
726
+ }
727
+
728
+
729
+ /*
730
+ * Document-class: PG::TextEncoder::QuotedLiteral < PG::CompositeEncoder
731
+ *
732
+ * This is the encoder class for PostgreSQL literals.
733
+ *
734
+ * A literal is quoted and escaped by the <tt>'</tt> character, so that it can be inserted into SQL queries.
735
+ * It works equal to PG::Connection#escape_literal, but integrates into the type cast system of ruby-pg.
736
+ *
737
+ * Both expressions have the same result:
738
+ * conn.escape_literal(PG::TextEncoder::Array.new.encode(["v1","v2"])) # => "'{v1,v2}'"
739
+ * PG::TextEncoder::QuotedLiteral.new(elements_type: PG::TextEncoder::Array.new).encode(["v1","v2"]) # => "'{v1,v2}'"
740
+ * While escape_literal requires a intermediate ruby string allocation, QuotedLiteral encodes the values directly to the result string.
741
+ *
742
+ */
743
+ static int
744
+ pg_text_enc_quoted_literal(t_pg_coder *conv, VALUE value, char *out, VALUE *intermediate, int enc_idx)
745
+ {
746
+ t_pg_composite_coder *this = (t_pg_composite_coder *)conv;
747
+ VALUE out_str = rb_str_new(NULL, 0);
748
+ PG_ENCODING_SET_NOCHECK(out_str, enc_idx);
749
+
750
+ out = RSTRING_PTR(out_str);
751
+ out = quote_string(this->elem, value, out_str, out, this->needs_quotation, quote_literal_buffer, this, enc_idx);
752
+ rb_str_set_len( out_str, out - RSTRING_PTR(out_str) );
753
+ *intermediate = out_str;
754
+ return -1;
755
+ }
756
+
757
+ /*
758
+ * Document-class: PG::TextEncoder::ToBase64 < PG::CompositeEncoder
759
+ *
760
+ * This is an encoder class for conversion of binary to base64 data.
761
+ *
762
+ */
763
+ static int
764
+ pg_text_enc_to_base64(t_pg_coder *conv, VALUE value, char *out, VALUE *intermediate, int enc_idx)
765
+ {
766
+ int strlen;
767
+ VALUE subint;
768
+ t_pg_composite_coder *this = (t_pg_composite_coder *)conv;
769
+ t_pg_coder_enc_func enc_func = pg_coder_enc_func(this->elem);
770
+
771
+ if(out){
772
+ /* Second encoder pass, if required */
773
+ strlen = enc_func(this->elem, value, out, intermediate, enc_idx);
774
+ base64_encode( out, out, strlen );
775
+
776
+ return BASE64_ENCODED_SIZE(strlen);
777
+ } else {
778
+ /* First encoder pass */
779
+ strlen = enc_func(this->elem, value, NULL, &subint, enc_idx);
780
+
781
+ if( strlen == -1 ){
782
+ /* Encoded string is returned in subint */
783
+ VALUE out_str;
784
+
785
+ strlen = RSTRING_LENINT(subint);
786
+ out_str = rb_str_new(NULL, BASE64_ENCODED_SIZE(strlen));
787
+ PG_ENCODING_SET_NOCHECK(out_str, enc_idx);
788
+
789
+ base64_encode( RSTRING_PTR(out_str), RSTRING_PTR(subint), strlen);
790
+ *intermediate = out_str;
791
+
792
+ return -1;
793
+ } else {
794
+ *intermediate = subint;
795
+
796
+ return BASE64_ENCODED_SIZE(strlen);
797
+ }
798
+ }
799
+ }
800
+
801
+
802
+ void
803
+ init_pg_text_encoder(void)
804
+ {
805
+ s_id_encode = rb_intern("encode");
806
+ s_id_to_i = rb_intern("to_i");
807
+ s_id_to_s = rb_intern("to_s");
808
+
809
+ /* This module encapsulates all encoder classes with text output format */
810
+ rb_mPG_TextEncoder = rb_define_module_under( rb_mPG, "TextEncoder" );
811
+ rb_define_private_method(rb_singleton_class(rb_mPG_TextEncoder), "init_numeric", init_pg_text_encoder_numeric, 0);
812
+
813
+ /* Make RDoc aware of the encoder classes... */
814
+ /* dummy = rb_define_class_under( rb_mPG_TextEncoder, "Boolean", rb_cPG_SimpleEncoder ); */
815
+ pg_define_coder( "Boolean", pg_text_enc_boolean, rb_cPG_SimpleEncoder, rb_mPG_TextEncoder );
816
+ /* dummy = rb_define_class_under( rb_mPG_TextEncoder, "Integer", rb_cPG_SimpleEncoder ); */
817
+ pg_define_coder( "Integer", pg_text_enc_integer, rb_cPG_SimpleEncoder, rb_mPG_TextEncoder );
818
+ /* dummy = rb_define_class_under( rb_mPG_TextEncoder, "Float", rb_cPG_SimpleEncoder ); */
819
+ pg_define_coder( "Float", pg_text_enc_float, rb_cPG_SimpleEncoder, rb_mPG_TextEncoder );
820
+ /* dummy = rb_define_class_under( rb_mPG_TextEncoder, "String", rb_cPG_SimpleEncoder ); */
821
+ pg_define_coder( "String", pg_coder_enc_to_s, rb_cPG_SimpleEncoder, rb_mPG_TextEncoder );
822
+ /* dummy = rb_define_class_under( rb_mPG_TextEncoder, "Bytea", rb_cPG_SimpleEncoder ); */
823
+ pg_define_coder( "Bytea", pg_text_enc_bytea, rb_cPG_SimpleEncoder, rb_mPG_TextEncoder );
824
+ /* dummy = rb_define_class_under( rb_mPG_TextEncoder, "Identifier", rb_cPG_SimpleEncoder ); */
825
+ pg_define_coder( "Identifier", pg_text_enc_identifier, rb_cPG_SimpleEncoder, rb_mPG_TextEncoder );
826
+
827
+ /* dummy = rb_define_class_under( rb_mPG_TextEncoder, "Array", rb_cPG_CompositeEncoder ); */
828
+ pg_define_coder( "Array", pg_text_enc_array, rb_cPG_CompositeEncoder, rb_mPG_TextEncoder );
829
+ /* dummy = rb_define_class_under( rb_mPG_TextEncoder, "QuotedLiteral", rb_cPG_CompositeEncoder ); */
830
+ pg_define_coder( "QuotedLiteral", pg_text_enc_quoted_literal, rb_cPG_CompositeEncoder, rb_mPG_TextEncoder );
831
+ /* dummy = rb_define_class_under( rb_mPG_TextEncoder, "ToBase64", rb_cPG_CompositeEncoder ); */
832
+ pg_define_coder( "ToBase64", pg_text_enc_to_base64, rb_cPG_CompositeEncoder, rb_mPG_TextEncoder );
833
+ }