yugabytedb-ysql 0.3

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