yugabyte_ysql 0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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 +373 -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 +990 -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/yugabyte_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/yugabyte_ysql.gemspec +33 -0
  121. metadata +232 -0
@@ -0,0 +1,917 @@
1
+ /*
2
+ * pg_copycoder.c - PG::Coder class extension
3
+ *
4
+ */
5
+
6
+ #include "pg.h"
7
+ #include "pg_util.h"
8
+
9
+ #define ISOCTAL(c) (((c) >= '0') && ((c) <= '7'))
10
+ #define OCTVALUE(c) ((c) - '0')
11
+
12
+ VALUE rb_cPG_CopyCoder;
13
+ VALUE rb_cPG_CopyEncoder;
14
+ VALUE rb_cPG_CopyDecoder;
15
+
16
+ typedef struct {
17
+ t_pg_coder comp;
18
+ VALUE typemap;
19
+ VALUE null_string;
20
+ char delimiter;
21
+ } t_pg_copycoder;
22
+
23
+
24
+ static void
25
+ pg_copycoder_mark( void *_this )
26
+ {
27
+ t_pg_copycoder *this = (t_pg_copycoder *)_this;
28
+ rb_gc_mark_movable(this->typemap);
29
+ rb_gc_mark_movable(this->null_string);
30
+ }
31
+
32
+ static size_t
33
+ pg_copycoder_memsize( const void *_this )
34
+ {
35
+ const t_pg_copycoder *this = (const t_pg_copycoder *)_this;
36
+ return sizeof(*this);
37
+ }
38
+
39
+ static void
40
+ pg_copycoder_compact( void *_this )
41
+ {
42
+ t_pg_copycoder *this = (t_pg_copycoder *)_this;
43
+ pg_coder_compact(&this->comp);
44
+ pg_gc_location(this->typemap);
45
+ pg_gc_location(this->null_string);
46
+ }
47
+
48
+ static const rb_data_type_t pg_copycoder_type = {
49
+ "PG::CopyCoder",
50
+ {
51
+ pg_copycoder_mark,
52
+ RUBY_TYPED_DEFAULT_FREE,
53
+ pg_copycoder_memsize,
54
+ pg_compact_callback(pg_copycoder_compact),
55
+ },
56
+ &pg_coder_type,
57
+ 0,
58
+ RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_WB_PROTECTED | PG_RUBY_TYPED_FROZEN_SHAREABLE,
59
+ };
60
+
61
+ static VALUE
62
+ pg_copycoder_encoder_allocate( VALUE klass )
63
+ {
64
+ t_pg_copycoder *this;
65
+ VALUE self = TypedData_Make_Struct( klass, t_pg_copycoder, &pg_copycoder_type, this );
66
+ pg_coder_init_encoder( self );
67
+ RB_OBJ_WRITE(self, &this->typemap, pg_typemap_all_strings);
68
+ this->delimiter = '\t';
69
+ RB_OBJ_WRITE(self, &this->null_string, rb_str_new_cstr("\\N"));
70
+ return self;
71
+ }
72
+
73
+ static VALUE
74
+ pg_copycoder_decoder_allocate( VALUE klass )
75
+ {
76
+ t_pg_copycoder *this;
77
+ VALUE self = TypedData_Make_Struct( klass, t_pg_copycoder, &pg_copycoder_type, this );
78
+ pg_coder_init_decoder( self );
79
+ RB_OBJ_WRITE(self, &this->typemap, pg_typemap_all_strings);
80
+ this->delimiter = '\t';
81
+ RB_OBJ_WRITE(self, &this->null_string, rb_str_new_cstr("\\N"));
82
+ return self;
83
+ }
84
+
85
+ /*
86
+ * call-seq:
87
+ * coder.delimiter = String
88
+ *
89
+ * Specifies the character that separates columns within each row (line) of the file.
90
+ * The default is a tab character in text format.
91
+ * This must be a single one-byte character.
92
+ *
93
+ * This option is ignored when using binary format.
94
+ */
95
+ static VALUE
96
+ pg_copycoder_delimiter_set(VALUE self, VALUE delimiter)
97
+ {
98
+ t_pg_copycoder *this = RTYPEDDATA_DATA(self);
99
+ rb_check_frozen(self);
100
+ StringValue(delimiter);
101
+ if(RSTRING_LEN(delimiter) != 1)
102
+ rb_raise( rb_eArgError, "delimiter size must be one byte");
103
+ this->delimiter = *RSTRING_PTR(delimiter);
104
+ return delimiter;
105
+ }
106
+
107
+ /*
108
+ * call-seq:
109
+ * coder.delimiter -> String
110
+ *
111
+ * The character that separates columns within each row (line) of the file.
112
+ */
113
+ static VALUE
114
+ pg_copycoder_delimiter_get(VALUE self)
115
+ {
116
+ t_pg_copycoder *this = RTYPEDDATA_DATA(self);
117
+ return rb_str_new(&this->delimiter, 1);
118
+ }
119
+
120
+ /*
121
+ * Specifies the string that represents a null value.
122
+ * The default is \\N (backslash-N) in text format.
123
+ * You might prefer an empty string even in text format for cases where you don't want to distinguish nulls from empty strings.
124
+ *
125
+ * This option is ignored when using binary format.
126
+ */
127
+ static VALUE
128
+ pg_copycoder_null_string_set(VALUE self, VALUE null_string)
129
+ {
130
+ t_pg_copycoder *this = RTYPEDDATA_DATA(self);
131
+ rb_check_frozen(self);
132
+ StringValue(null_string);
133
+ RB_OBJ_WRITE(self, &this->null_string, null_string);
134
+ return null_string;
135
+ }
136
+
137
+ /*
138
+ * The string that represents a null value.
139
+ */
140
+ static VALUE
141
+ pg_copycoder_null_string_get(VALUE self)
142
+ {
143
+ t_pg_copycoder *this = RTYPEDDATA_DATA(self);
144
+ return this->null_string;
145
+ }
146
+
147
+ /*
148
+ * call-seq:
149
+ * coder.type_map = map
150
+ *
151
+ * Defines how single columns are encoded or decoded.
152
+ * +map+ must be a kind of PG::TypeMap .
153
+ *
154
+ * Defaults to a PG::TypeMapAllStrings , so that PG::TextEncoder::String respectively
155
+ * PG::TextDecoder::String is used for encoding/decoding of each column.
156
+ *
157
+ */
158
+ static VALUE
159
+ pg_copycoder_type_map_set(VALUE self, VALUE type_map)
160
+ {
161
+ t_pg_copycoder *this = RTYPEDDATA_DATA( self );
162
+
163
+ rb_check_frozen(self);
164
+ if ( !rb_obj_is_kind_of(type_map, rb_cTypeMap) ){
165
+ rb_raise( rb_eTypeError, "wrong elements type %s (expected some kind of PG::TypeMap)",
166
+ rb_obj_classname( type_map ) );
167
+ }
168
+ RB_OBJ_WRITE(self, &this->typemap, type_map);
169
+
170
+ return type_map;
171
+ }
172
+
173
+ /*
174
+ * call-seq:
175
+ * coder.type_map -> PG::TypeMap
176
+ *
177
+ * The PG::TypeMap that will be used for encoding and decoding of columns.
178
+ */
179
+ static VALUE
180
+ pg_copycoder_type_map_get(VALUE self)
181
+ {
182
+ t_pg_copycoder *this = RTYPEDDATA_DATA( self );
183
+
184
+ return this->typemap;
185
+ }
186
+
187
+
188
+ /*
189
+ * Document-class: PG::TextEncoder::CopyRow < PG::CopyEncoder
190
+ *
191
+ * This class encodes one row of arbitrary columns for transmission as COPY data in text format.
192
+ * See the {COPY command}[http://www.postgresql.org/docs/current/static/sql-copy.html]
193
+ * for description of the format.
194
+ *
195
+ * It is intended to be used in conjunction with PG::Connection#put_copy_data .
196
+ *
197
+ * The columns are expected as Array of values. The single values are encoded as defined
198
+ * in the assigned #type_map. If no type_map was assigned, all values are converted to
199
+ * strings by PG::TextEncoder::String.
200
+ *
201
+ * Example with default type map ( TypeMapAllStrings ):
202
+ * conn.exec "create table my_table (a text,b int,c bool)"
203
+ * enco = PG::TextEncoder::CopyRow.new
204
+ * conn.copy_data "COPY my_table FROM STDIN", enco do
205
+ * conn.put_copy_data ["astring", 7, false]
206
+ * conn.put_copy_data ["string2", 42, true]
207
+ * end
208
+ * This creates +my_table+ and inserts two rows.
209
+ *
210
+ * It is possible to manually assign a type encoder for each column per PG::TypeMapByColumn,
211
+ * or to make use of PG::BasicTypeMapBasedOnResult to assign them based on the table OIDs.
212
+ *
213
+ * See also PG::TextDecoder::CopyRow for the decoding direction with
214
+ * PG::Connection#get_copy_data .
215
+ */
216
+ static int
217
+ pg_text_enc_copy_row(t_pg_coder *conv, VALUE value, char *out, VALUE *intermediate, int enc_idx)
218
+ {
219
+ t_pg_copycoder *this = (t_pg_copycoder *)conv;
220
+ t_pg_coder_enc_func enc_func;
221
+ static t_pg_coder *p_elem_coder;
222
+ int i;
223
+ t_typemap *p_typemap;
224
+ char *current_out;
225
+ char *end_capa_ptr;
226
+
227
+ p_typemap = RTYPEDDATA_DATA( this->typemap );
228
+ p_typemap->funcs.fit_to_query( this->typemap, value );
229
+
230
+ /* Allocate a new string with embedded capacity and realloc exponential when needed. */
231
+ PG_RB_STR_NEW( *intermediate, current_out, end_capa_ptr );
232
+ PG_ENCODING_SET_NOCHECK(*intermediate, enc_idx);
233
+
234
+ for( i=0; i<RARRAY_LEN(value); i++){
235
+ char *ptr1;
236
+ char *ptr2;
237
+ int strlen;
238
+ int backslashs;
239
+ VALUE subint;
240
+ VALUE entry;
241
+
242
+ entry = rb_ary_entry(value, i);
243
+
244
+ if( i > 0 ){
245
+ PG_RB_STR_ENSURE_CAPA( *intermediate, 1, current_out, end_capa_ptr );
246
+ *current_out++ = this->delimiter;
247
+ }
248
+
249
+ switch(TYPE(entry)){
250
+ case T_NIL:
251
+ PG_RB_STR_ENSURE_CAPA( *intermediate, RSTRING_LEN(this->null_string), current_out, end_capa_ptr );
252
+ memcpy( current_out, RSTRING_PTR(this->null_string), RSTRING_LEN(this->null_string) );
253
+ current_out += RSTRING_LEN(this->null_string);
254
+ break;
255
+ default:
256
+ p_elem_coder = p_typemap->funcs.typecast_query_param(p_typemap, entry, i);
257
+ enc_func = pg_coder_enc_func(p_elem_coder);
258
+
259
+ /* 1st pass for retiving the required memory space */
260
+ strlen = enc_func(p_elem_coder, entry, NULL, &subint, enc_idx);
261
+
262
+ if( strlen == -1 ){
263
+ /* we can directly use String value in subint */
264
+ strlen = RSTRING_LENINT(subint);
265
+
266
+ /* size of string assuming the worst case, that every character must be escaped. */
267
+ PG_RB_STR_ENSURE_CAPA( *intermediate, strlen * 2, current_out, end_capa_ptr );
268
+
269
+ /* Copy string from subint with backslash escaping */
270
+ for(ptr1 = RSTRING_PTR(subint); ptr1 < RSTRING_PTR(subint) + strlen; ptr1++) {
271
+ /* Escape backslash itself, newline, carriage return, and the current delimiter character. */
272
+ if(*ptr1 == '\\' || *ptr1 == '\n' || *ptr1 == '\r' || *ptr1 == this->delimiter){
273
+ *current_out++ = '\\';
274
+ }
275
+ *current_out++ = *ptr1;
276
+ }
277
+ } else {
278
+ /* 2nd pass for writing the data to prepared buffer */
279
+ /* size of string assuming the worst case, that every character must be escaped. */
280
+ PG_RB_STR_ENSURE_CAPA( *intermediate, strlen * 2, current_out, end_capa_ptr );
281
+
282
+ /* Place the unescaped string at current output position. */
283
+ strlen = enc_func(p_elem_coder, entry, current_out, &subint, enc_idx);
284
+
285
+ ptr1 = current_out;
286
+ ptr2 = current_out + strlen;
287
+
288
+ /* count required backlashs */
289
+ for(backslashs = 0; ptr1 != ptr2; ptr1++) {
290
+ /* Escape backslash itself, newline, carriage return, and the current delimiter character. */
291
+ if(*ptr1 == '\\' || *ptr1 == '\n' || *ptr1 == '\r' || *ptr1 == this->delimiter){
292
+ backslashs++;
293
+ }
294
+ }
295
+
296
+ ptr1 = current_out + strlen;
297
+ ptr2 = current_out + strlen + backslashs;
298
+ current_out = ptr2;
299
+
300
+ /* Then store the escaped string on the final position, walking
301
+ * right to left, until all backslashs are placed. */
302
+ while( ptr1 != ptr2 ) {
303
+ *--ptr2 = *--ptr1;
304
+ if(*ptr1 == '\\' || *ptr1 == '\n' || *ptr1 == '\r' || *ptr1 == this->delimiter){
305
+ *--ptr2 = '\\';
306
+ }
307
+ }
308
+ }
309
+ }
310
+ }
311
+ PG_RB_STR_ENSURE_CAPA( *intermediate, 1, current_out, end_capa_ptr );
312
+ *current_out++ = '\n';
313
+
314
+ rb_str_set_len( *intermediate, current_out - RSTRING_PTR(*intermediate) );
315
+
316
+ return -1;
317
+ }
318
+
319
+
320
+ /*
321
+ * Document-class: PG::BinaryEncoder::CopyRow < PG::CopyEncoder
322
+ *
323
+ * This class encodes one row of arbitrary columns for transmission as COPY data in binary format.
324
+ * See the {COPY command}[http://www.postgresql.org/docs/current/static/sql-copy.html]
325
+ * for description of the format.
326
+ *
327
+ * It is intended to be used in conjunction with PG::Connection#put_copy_data .
328
+ *
329
+ * The columns are expected as Array of values. The single values are encoded as defined
330
+ * in the assigned #type_map. If no type_map was assigned, all values are converted to
331
+ * strings by PG::BinaryEncoder::String.
332
+ *
333
+ * Example with default type map ( TypeMapAllStrings ):
334
+ * conn.exec "create table my_table (a text,b int,c bool)"
335
+ * enco = PG::BinaryEncoder::CopyRow.new
336
+ * conn.copy_data "COPY my_table FROM STDIN WITH (FORMAT binary)", enco do
337
+ * conn.put_copy_data ["astring", "\x00\x00\x00\a", "\x00"]
338
+ * conn.put_copy_data ["string2", "\x00\x00\x00*", "\x01"]
339
+ * end
340
+ * This creates +my_table+ and inserts two rows with binary fields.
341
+ *
342
+ * The binary format is less portable and less readable than the text format.
343
+ * It is therefore recommended to either manually assign a type encoder for each column per PG::TypeMapByColumn,
344
+ * or to make use of PG::BasicTypeMapBasedOnResult to assign them based on the table OIDs.
345
+ *
346
+ * Manually assigning a type encoder works per type map like so:
347
+ *
348
+ * conn.exec "create table my_table (a text,b int,c bool)"
349
+ * tm = PG::TypeMapByColumn.new( [
350
+ * PG::BinaryEncoder::String.new,
351
+ * PG::BinaryEncoder::Int4.new,
352
+ * PG::BinaryEncoder::Boolean.new] )
353
+ * enco = PG::BinaryEncoder::CopyRow.new( type_map: tm )
354
+ * conn.copy_data "COPY my_table FROM STDIN WITH (FORMAT binary)", enco do
355
+ * conn.put_copy_data ["astring", 7, false]
356
+ * conn.put_copy_data ["string2", 42, true]
357
+ * end
358
+ *
359
+ * See also PG::BinaryDecoder::CopyRow for the decoding direction with
360
+ * PG::Connection#get_copy_data .
361
+ */
362
+ static int
363
+ pg_bin_enc_copy_row(t_pg_coder *conv, VALUE value, char *out, VALUE *intermediate, int enc_idx)
364
+ {
365
+ t_pg_copycoder *this = (t_pg_copycoder *)conv;
366
+ int i;
367
+ t_typemap *p_typemap;
368
+ char *current_out;
369
+ char *end_capa_ptr;
370
+
371
+ p_typemap = RTYPEDDATA_DATA( this->typemap );
372
+ p_typemap->funcs.fit_to_query( this->typemap, value );
373
+
374
+ /* Allocate a new string with embedded capacity and realloc exponential when needed. */
375
+ PG_RB_STR_NEW( *intermediate, current_out, end_capa_ptr );
376
+ PG_ENCODING_SET_NOCHECK(*intermediate, enc_idx);
377
+
378
+ /* 2 bytes for number of fields */
379
+ PG_RB_STR_ENSURE_CAPA( *intermediate, 2, current_out, end_capa_ptr );
380
+ write_nbo16(RARRAY_LEN(value), current_out);
381
+ current_out += 2;
382
+
383
+ for( i=0; i<RARRAY_LEN(value); i++){
384
+ int strlen;
385
+ VALUE subint;
386
+ VALUE entry;
387
+ t_pg_coder_enc_func enc_func;
388
+ static t_pg_coder *p_elem_coder;
389
+
390
+ entry = rb_ary_entry(value, i);
391
+
392
+ switch(TYPE(entry)){
393
+ case T_NIL:
394
+ /* 4 bytes for -1 indicationg a NULL value */
395
+ PG_RB_STR_ENSURE_CAPA( *intermediate, 4, current_out, end_capa_ptr );
396
+ write_nbo32(-1, current_out);
397
+ current_out += 4;
398
+ break;
399
+ default:
400
+ p_elem_coder = p_typemap->funcs.typecast_query_param(p_typemap, entry, i);
401
+ enc_func = pg_coder_enc_func(p_elem_coder);
402
+
403
+ /* 1st pass for retiving the required memory space */
404
+ strlen = enc_func(p_elem_coder, entry, NULL, &subint, enc_idx);
405
+
406
+ if( strlen == -1 ){
407
+ /* we can directly use String value in subint */
408
+ strlen = RSTRING_LENINT(subint);
409
+
410
+ PG_RB_STR_ENSURE_CAPA( *intermediate, 4 + strlen, current_out, end_capa_ptr );
411
+ /* 4 bytes length */
412
+ write_nbo32(strlen, current_out);
413
+ current_out += 4;
414
+
415
+ memcpy( current_out, RSTRING_PTR(subint), strlen );
416
+ current_out += strlen;
417
+ } else {
418
+ /* 2nd pass for writing the data to prepared buffer */
419
+ PG_RB_STR_ENSURE_CAPA( *intermediate, 4 + strlen, current_out, end_capa_ptr );
420
+ /* 4 bytes length */
421
+ write_nbo32(strlen, current_out);
422
+ current_out += 4;
423
+
424
+ /* Place the string at current output position. */
425
+ strlen = enc_func(p_elem_coder, entry, current_out, &subint, enc_idx);
426
+ current_out += strlen;
427
+ }
428
+ }
429
+ }
430
+
431
+ rb_str_set_len( *intermediate, current_out - RSTRING_PTR(*intermediate) );
432
+
433
+ return -1;
434
+ }
435
+
436
+
437
+ /*
438
+ * Return decimal value for a hexadecimal digit
439
+ */
440
+ static int
441
+ GetDecimalFromHex(char hex)
442
+ {
443
+ if (hex >= '0' && hex <= '9')
444
+ return hex - '0';
445
+ else if (hex >= 'a' && hex <= 'f')
446
+ return hex - 'a' + 10;
447
+ else if (hex >= 'A' && hex <= 'F')
448
+ return hex - 'A' + 10;
449
+ else
450
+ return -1;
451
+ }
452
+
453
+ /*
454
+ * Document-class: PG::TextDecoder::CopyRow < PG::CopyDecoder
455
+ *
456
+ * This class decodes one row of arbitrary columns received as COPY data in text format.
457
+ * See the {COPY command}[http://www.postgresql.org/docs/current/static/sql-copy.html]
458
+ * for description of the format.
459
+ *
460
+ * It is intended to be used in conjunction with PG::Connection#get_copy_data .
461
+ *
462
+ * The columns are retrieved as Array of values. The single values are decoded as defined
463
+ * in the assigned #type_map. If no type_map was assigned, all values are converted to
464
+ * strings by PG::TextDecoder::String.
465
+ *
466
+ * Example with default type map ( TypeMapAllStrings ):
467
+ * conn.exec("CREATE TABLE my_table AS VALUES('astring', 7, FALSE), ('string2', 42, TRUE) ")
468
+ *
469
+ * deco = PG::TextDecoder::CopyRow.new
470
+ * conn.copy_data "COPY my_table TO STDOUT", deco do
471
+ * while row=conn.get_copy_data
472
+ * p row
473
+ * end
474
+ * end
475
+ * This prints all rows of +my_table+ :
476
+ * ["astring", "7", "f"]
477
+ * ["string2", "42", "t"]
478
+ *
479
+ * Example with column based type map:
480
+ * tm = PG::TypeMapByColumn.new( [
481
+ * PG::TextDecoder::String.new,
482
+ * PG::TextDecoder::Integer.new,
483
+ * PG::TextDecoder::Boolean.new] )
484
+ * deco = PG::TextDecoder::CopyRow.new( type_map: tm )
485
+ * conn.copy_data "COPY my_table TO STDOUT", deco do
486
+ * while row=conn.get_copy_data
487
+ * p row
488
+ * end
489
+ * end
490
+ * This prints the rows with type casted columns:
491
+ * ["astring", 7, false]
492
+ * ["string2", 42, true]
493
+ *
494
+ * Instead of manually assigning a type decoder for each column, PG::BasicTypeMapForResults
495
+ * can be used to assign them based on the table OIDs.
496
+ *
497
+ * See also PG::TextEncoder::CopyRow for the encoding direction with
498
+ * PG::Connection#put_copy_data .
499
+ */
500
+ /*
501
+ * Parse the current line into separate attributes (fields),
502
+ * performing de-escaping as needed.
503
+ *
504
+ * All fields are gathered into a ruby Array. The de-escaped field data is written
505
+ * into to a ruby String. This object is reused for non string columns.
506
+ * For String columns the field value is directly used as return value and no
507
+ * reuse of the memory is done.
508
+ *
509
+ * The parser is thankfully borrowed from the PostgreSQL sources:
510
+ * src/backend/commands/copy.c
511
+ */
512
+ static VALUE
513
+ pg_text_dec_copy_row(t_pg_coder *conv, const char *input_line, int len, int _tuple, int _field, int enc_idx)
514
+ {
515
+ t_pg_copycoder *this = (t_pg_copycoder *)conv;
516
+
517
+ /* Return value: array */
518
+ VALUE array;
519
+
520
+ /* Current field */
521
+ VALUE field_str;
522
+
523
+ char delimc = this->delimiter;
524
+ int fieldno;
525
+ int expected_fields;
526
+ char *output_ptr;
527
+ const char *cur_ptr;
528
+ const char *line_end_ptr;
529
+ char *end_capa_ptr;
530
+ t_typemap *p_typemap;
531
+
532
+ p_typemap = RTYPEDDATA_DATA( this->typemap );
533
+ expected_fields = p_typemap->funcs.fit_to_copy_get( this->typemap );
534
+
535
+ /* The received input string will probably have this->nfields fields. */
536
+ array = rb_ary_new2(expected_fields);
537
+
538
+ /* Allocate a new string with embedded capacity and realloc later with
539
+ * exponential growing size when needed. */
540
+ PG_RB_STR_NEW( field_str, output_ptr, end_capa_ptr );
541
+
542
+ /* set pointer variables for loop */
543
+ cur_ptr = input_line;
544
+ line_end_ptr = input_line + len;
545
+
546
+ /* Outer loop iterates over fields */
547
+ fieldno = 0;
548
+ for (;;)
549
+ {
550
+ int found_delim = 0;
551
+ const char *start_ptr;
552
+ const char *end_ptr;
553
+ long input_len;
554
+
555
+ /* Remember start of field on input side */
556
+ start_ptr = cur_ptr;
557
+
558
+ /*
559
+ * Scan data for field.
560
+ *
561
+ * Note that in this loop, we are scanning to locate the end of field
562
+ * and also speculatively performing de-escaping. Once we find the
563
+ * end-of-field, we can match the raw field contents against the null
564
+ * marker string. Only after that comparison fails do we know that
565
+ * de-escaping is actually the right thing to do; therefore we *must
566
+ * not* throw any syntax errors before we've done the null-marker
567
+ * check.
568
+ */
569
+ for (;;)
570
+ {
571
+ /* The current character in the input string. */
572
+ char c;
573
+
574
+ end_ptr = cur_ptr;
575
+ if (cur_ptr >= line_end_ptr)
576
+ break;
577
+ c = *cur_ptr++;
578
+ if (c == delimc){
579
+ found_delim = 1;
580
+ break;
581
+ }
582
+ if (c == '\n'){
583
+ break;
584
+ }
585
+ if (c == '\\'){
586
+ if (cur_ptr >= line_end_ptr)
587
+ break;
588
+
589
+ c = *cur_ptr++;
590
+ switch (c){
591
+ case '0':
592
+ case '1':
593
+ case '2':
594
+ case '3':
595
+ case '4':
596
+ case '5':
597
+ case '6':
598
+ case '7':
599
+ {
600
+ /* handle \013 */
601
+ int val;
602
+
603
+ val = OCTVALUE(c);
604
+ if (cur_ptr < line_end_ptr)
605
+ {
606
+ c = *cur_ptr;
607
+ if (ISOCTAL(c))
608
+ {
609
+ cur_ptr++;
610
+ val = (val << 3) + OCTVALUE(c);
611
+ if (cur_ptr < line_end_ptr)
612
+ {
613
+ c = *cur_ptr;
614
+ if (ISOCTAL(c))
615
+ {
616
+ cur_ptr++;
617
+ val = (val << 3) + OCTVALUE(c);
618
+ }
619
+ }
620
+ }
621
+ }
622
+ c = val & 0377;
623
+ }
624
+ break;
625
+ case 'x':
626
+ /* Handle \x3F */
627
+ if (cur_ptr < line_end_ptr)
628
+ {
629
+ char hexchar = *cur_ptr;
630
+ int val = GetDecimalFromHex(hexchar);;
631
+
632
+ if (val >= 0)
633
+ {
634
+ cur_ptr++;
635
+ if (cur_ptr < line_end_ptr)
636
+ {
637
+ int val2;
638
+ hexchar = *cur_ptr;
639
+ val2 = GetDecimalFromHex(hexchar);
640
+
641
+ if (val2 >= 0)
642
+ {
643
+ cur_ptr++;
644
+ val = (val << 4) + val2;
645
+ }
646
+ }
647
+ c = val & 0xff;
648
+ }
649
+ }
650
+ break;
651
+ case 'b':
652
+ c = '\b';
653
+ break;
654
+ case 'f':
655
+ c = '\f';
656
+ break;
657
+ case 'n':
658
+ c = '\n';
659
+ break;
660
+ case 'r':
661
+ c = '\r';
662
+ break;
663
+ case 't':
664
+ c = '\t';
665
+ break;
666
+ case 'v':
667
+ c = '\v';
668
+ break;
669
+
670
+ /*
671
+ * in all other cases, take the char after '\'
672
+ * literally
673
+ */
674
+ }
675
+ }
676
+
677
+ PG_RB_STR_ENSURE_CAPA( field_str, 1, output_ptr, end_capa_ptr );
678
+ /* Add c to output string */
679
+ *output_ptr++ = c;
680
+ }
681
+
682
+ if (!found_delim && cur_ptr < line_end_ptr)
683
+ rb_raise( rb_eArgError, "trailing data after linefeed at position: %ld", (long)(cur_ptr - input_line) + 1 );
684
+
685
+
686
+ /* Check whether raw input matched null marker */
687
+ input_len = end_ptr - start_ptr;
688
+ if (input_len == RSTRING_LEN(this->null_string) &&
689
+ strncmp(start_ptr, RSTRING_PTR(this->null_string), input_len) == 0) {
690
+ rb_ary_push(array, Qnil);
691
+ } else {
692
+ VALUE field_value;
693
+
694
+ rb_str_set_len( field_str, output_ptr - RSTRING_PTR(field_str) );
695
+ field_value = p_typemap->funcs.typecast_copy_get( p_typemap, field_str, fieldno, 0, enc_idx );
696
+
697
+ rb_ary_push(array, field_value);
698
+
699
+ if( field_value == field_str ){
700
+ /* Our output string will be send to the user, so we can not reuse
701
+ * it for the next field. */
702
+ PG_RB_STR_NEW( field_str, output_ptr, end_capa_ptr );
703
+ }
704
+ }
705
+ /* Reset the pointer to the start of the output/buffer string. */
706
+ output_ptr = RSTRING_PTR(field_str);
707
+
708
+ fieldno++;
709
+ /* Done if we hit EOL instead of a delim */
710
+ if (!found_delim)
711
+ break;
712
+ }
713
+
714
+ return array;
715
+ }
716
+
717
+
718
+ static const char BinarySignature[11] = "PGCOPY\n\377\r\n\0";
719
+
720
+ /*
721
+ * Document-class: PG::BinaryDecoder::CopyRow < PG::CopyDecoder
722
+ *
723
+ * This class decodes one row of arbitrary columns received as COPY data in binary format.
724
+ * See the {COPY command}[http://www.postgresql.org/docs/current/static/sql-copy.html]
725
+ * for description of the format.
726
+ *
727
+ * It is intended to be used in conjunction with PG::Connection#get_copy_data .
728
+ *
729
+ * The columns are retrieved as Array of values. The single values are decoded as defined
730
+ * in the assigned #type_map. If no type_map was assigned, all values are converted to
731
+ * strings by PG::BinaryDecoder::String.
732
+ *
733
+ * Example with default type map ( TypeMapAllStrings ):
734
+ * conn.exec("CREATE TABLE my_table AS VALUES('astring', 7, FALSE), ('string2', 42, TRUE) ")
735
+ *
736
+ * deco = PG::BinaryDecoder::CopyRow.new
737
+ * conn.copy_data "COPY my_table TO STDOUT WITH (FORMAT binary)", deco do
738
+ * while row=conn.get_copy_data
739
+ * p row
740
+ * end
741
+ * end
742
+ * This prints all rows of +my_table+ in binary format:
743
+ * ["astring", "\x00\x00\x00\a", "\x00"]
744
+ * ["string2", "\x00\x00\x00*", "\x01"]
745
+ *
746
+ * Example with column based type map:
747
+ * tm = PG::TypeMapByColumn.new( [
748
+ * PG::BinaryDecoder::String.new,
749
+ * PG::BinaryDecoder::Integer.new,
750
+ * PG::BinaryDecoder::Boolean.new] )
751
+ * deco = PG::BinaryDecoder::CopyRow.new( type_map: tm )
752
+ * conn.copy_data "COPY my_table TO STDOUT WITH (FORMAT binary)", deco do
753
+ * while row=conn.get_copy_data
754
+ * p row
755
+ * end
756
+ * end
757
+ * This prints the rows with type casted columns:
758
+ * ["astring", 7, false]
759
+ * ["string2", 42, true]
760
+ *
761
+ * Instead of manually assigning a type decoder for each column, PG::BasicTypeMapForResults
762
+ * can be used to assign them based on the table OIDs.
763
+ *
764
+ * See also PG::BinaryEncoder::CopyRow for the encoding direction with
765
+ * PG::Connection#put_copy_data .
766
+ */
767
+ static VALUE
768
+ pg_bin_dec_copy_row(t_pg_coder *conv, const char *input_line, int len, int _tuple, int _field, int enc_idx)
769
+ {
770
+ t_pg_copycoder *this = (t_pg_copycoder *)conv;
771
+
772
+ /* Return value: array */
773
+ VALUE array;
774
+
775
+ /* Current field */
776
+ VALUE field_str;
777
+
778
+ int nfields;
779
+ int expected_fields;
780
+ int fieldno;
781
+ char *output_ptr;
782
+ const char *cur_ptr;
783
+ const char *line_end_ptr;
784
+ char *end_capa_ptr;
785
+ t_typemap *p_typemap;
786
+
787
+ p_typemap = RTYPEDDATA_DATA( this->typemap );
788
+ expected_fields = p_typemap->funcs.fit_to_copy_get( this->typemap );
789
+
790
+ /* Allocate a new string with embedded capacity and realloc later with
791
+ * exponential growing size when needed. */
792
+ PG_RB_STR_NEW( field_str, output_ptr, end_capa_ptr );
793
+
794
+ /* set pointer variables for loop */
795
+ cur_ptr = input_line;
796
+ line_end_ptr = input_line + len;
797
+
798
+ if (line_end_ptr - cur_ptr >= 11 && memcmp(cur_ptr, BinarySignature, 11) == 0){
799
+ /* binary COPY header signature detected -> just drop it */
800
+ int ext_bytes;
801
+ cur_ptr += 11;
802
+
803
+ /* read flags */
804
+ if (line_end_ptr - cur_ptr < 4 ) goto length_error;
805
+ cur_ptr += 4;
806
+
807
+ /* read header extensions */
808
+ if (line_end_ptr - cur_ptr < 4 ) goto length_error;
809
+ ext_bytes = read_nbo32(cur_ptr);
810
+ if (ext_bytes < 0) goto length_error;
811
+ cur_ptr += 4;
812
+ if (line_end_ptr - cur_ptr < ext_bytes ) goto length_error;
813
+ cur_ptr += ext_bytes;
814
+ }
815
+
816
+ /* read row header */
817
+ if (line_end_ptr - cur_ptr < 2 ) goto length_error;
818
+ nfields = read_nbo16(cur_ptr);
819
+ cur_ptr += 2;
820
+
821
+ /* COPY data trailer? */
822
+ if (nfields < 0) {
823
+ if (nfields != -1) goto length_error;
824
+ array = Qnil;
825
+ } else {
826
+ array = rb_ary_new2(expected_fields);
827
+
828
+ for( fieldno = 0; fieldno < nfields; fieldno++){
829
+ long input_len;
830
+ VALUE field_value;
831
+
832
+ /* read field size */
833
+ if (line_end_ptr - cur_ptr < 4 ) goto length_error;
834
+ input_len = read_nbo32(cur_ptr);
835
+ cur_ptr += 4;
836
+
837
+ if (input_len < 0) {
838
+ if (input_len != -1) goto length_error;
839
+ /* NULL indicator */
840
+ rb_ary_push(array, Qnil);
841
+ } else {
842
+ if (line_end_ptr - cur_ptr < input_len ) goto length_error;
843
+
844
+ /* copy input data to field_str */
845
+ PG_RB_STR_ENSURE_CAPA( field_str, input_len, output_ptr, end_capa_ptr );
846
+ memcpy(output_ptr, cur_ptr, input_len);
847
+ cur_ptr += input_len;
848
+ output_ptr += input_len;
849
+ /* convert field_str through the type map */
850
+ rb_str_set_len( field_str, output_ptr - RSTRING_PTR(field_str) );
851
+ field_value = p_typemap->funcs.typecast_copy_get( p_typemap, field_str, fieldno, 1, enc_idx );
852
+
853
+ rb_ary_push(array, field_value);
854
+
855
+ if( field_value == field_str ){
856
+ /* Our output string will be send to the user, so we can not reuse
857
+ * it for the next field. */
858
+ PG_RB_STR_NEW( field_str, output_ptr, end_capa_ptr );
859
+ }
860
+ }
861
+
862
+ /* Reset the pointer to the start of the output/buffer string. */
863
+ output_ptr = RSTRING_PTR(field_str);
864
+ }
865
+ }
866
+
867
+ if (cur_ptr < line_end_ptr)
868
+ rb_raise( rb_eArgError, "trailing data after row data at position: %ld", (long)(cur_ptr - input_line) + 1 );
869
+
870
+ return array;
871
+
872
+ length_error:
873
+ rb_raise( rb_eArgError, "premature end of COPY data at position: %ld", (long)(cur_ptr - input_line) + 1 );
874
+ }
875
+
876
+ void
877
+ init_pg_copycoder(void)
878
+ {
879
+ VALUE coder;
880
+ /* Document-class: PG::CopyCoder < PG::Coder
881
+ *
882
+ * This is the base class for all type cast classes for COPY data,
883
+ */
884
+ rb_cPG_CopyCoder = rb_define_class_under( rb_mPG, "CopyCoder", rb_cPG_Coder );
885
+ rb_define_method( rb_cPG_CopyCoder, "type_map=", pg_copycoder_type_map_set, 1 );
886
+ rb_define_method( rb_cPG_CopyCoder, "type_map", pg_copycoder_type_map_get, 0 );
887
+ rb_define_method( rb_cPG_CopyCoder, "delimiter=", pg_copycoder_delimiter_set, 1 );
888
+ rb_define_method( rb_cPG_CopyCoder, "delimiter", pg_copycoder_delimiter_get, 0 );
889
+ rb_define_method( rb_cPG_CopyCoder, "null_string=", pg_copycoder_null_string_set, 1 );
890
+ rb_define_method( rb_cPG_CopyCoder, "null_string", pg_copycoder_null_string_get, 0 );
891
+
892
+ /* Document-class: PG::CopyEncoder < PG::CopyCoder */
893
+ rb_cPG_CopyEncoder = rb_define_class_under( rb_mPG, "CopyEncoder", rb_cPG_CopyCoder );
894
+ rb_define_alloc_func( rb_cPG_CopyEncoder, pg_copycoder_encoder_allocate );
895
+ /* Document-class: PG::CopyDecoder < PG::CopyCoder */
896
+ rb_cPG_CopyDecoder = rb_define_class_under( rb_mPG, "CopyDecoder", rb_cPG_CopyCoder );
897
+ rb_define_alloc_func( rb_cPG_CopyDecoder, pg_copycoder_decoder_allocate );
898
+
899
+ /* Make RDoc aware of the encoder classes... */
900
+ /* rb_mPG_TextEncoder = rb_define_module_under( rb_mPG, "TextEncoder" ); */
901
+ /* dummy = rb_define_class_under( rb_mPG_TextEncoder, "CopyRow", rb_cPG_CopyEncoder ); */
902
+ coder = pg_define_coder( "CopyRow", pg_text_enc_copy_row, rb_cPG_CopyEncoder, rb_mPG_TextEncoder );
903
+ rb_include_module( coder, rb_mPG_BinaryFormatting );
904
+ /* rb_mPG_BinaryEncoder = rb_define_module_under( rb_mPG, "BinaryEncoder" ); */
905
+ /* dummy = rb_define_class_under( rb_mPG_BinaryEncoder, "CopyRow", rb_cPG_CopyEncoder ); */
906
+ pg_define_coder( "CopyRow", pg_bin_enc_copy_row, rb_cPG_CopyEncoder, rb_mPG_BinaryEncoder );
907
+
908
+ /* rb_mPG_TextDecoder = rb_define_module_under( rb_mPG, "TextDecoder" ); */
909
+ /* dummy = rb_define_class_under( rb_mPG_TextDecoder, "CopyRow", rb_cPG_CopyDecoder ); */
910
+ coder = pg_define_coder( "CopyRow", pg_text_dec_copy_row, rb_cPG_CopyDecoder, rb_mPG_TextDecoder );
911
+ /* Although CopyRow is a text decoder, data can contain zero bytes and are not zero terminated.
912
+ * They are handled like binaries. So format is set to 1 (binary). */
913
+ rb_include_module( coder, rb_mPG_BinaryFormatting );
914
+ /* rb_mPG_BinaryDecoder = rb_define_module_under( rb_mPG, "BinaryDecoder" ); */
915
+ /* dummy = rb_define_class_under( rb_mPG_BinaryDecoder, "CopyRow", rb_cPG_CopyDecoder ); */
916
+ pg_define_coder( "CopyRow", pg_bin_dec_copy_row, rb_cPG_CopyDecoder, rb_mPG_BinaryDecoder );
917
+ }