pg 0.18.4 → 1.2.3

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 (85) hide show
  1. checksums.yaml +5 -5
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/BSDL +2 -2
  5. data/ChangeLog +0 -5911
  6. data/History.rdoc +240 -0
  7. data/Manifest.txt +8 -20
  8. data/README-Windows.rdoc +4 -4
  9. data/README.ja.rdoc +1 -2
  10. data/README.rdoc +64 -15
  11. data/Rakefile +20 -21
  12. data/Rakefile.cross +67 -69
  13. data/ext/errorcodes.def +101 -0
  14. data/ext/errorcodes.rb +1 -1
  15. data/ext/errorcodes.txt +33 -2
  16. data/ext/extconf.rb +26 -36
  17. data/ext/gvl_wrappers.c +4 -0
  18. data/ext/gvl_wrappers.h +27 -39
  19. data/ext/pg.c +156 -145
  20. data/ext/pg.h +74 -98
  21. data/ext/pg_binary_decoder.c +82 -15
  22. data/ext/pg_binary_encoder.c +20 -19
  23. data/ext/pg_coder.c +103 -21
  24. data/ext/pg_connection.c +917 -523
  25. data/ext/pg_copy_coder.c +50 -12
  26. data/ext/pg_record_coder.c +491 -0
  27. data/ext/pg_result.c +590 -208
  28. data/ext/pg_text_decoder.c +606 -40
  29. data/ext/pg_text_encoder.c +245 -94
  30. data/ext/pg_tuple.c +549 -0
  31. data/ext/pg_type_map.c +14 -7
  32. data/ext/pg_type_map_all_strings.c +4 -4
  33. data/ext/pg_type_map_by_class.c +9 -4
  34. data/ext/pg_type_map_by_column.c +7 -6
  35. data/ext/pg_type_map_by_mri_type.c +1 -1
  36. data/ext/pg_type_map_by_oid.c +3 -2
  37. data/ext/pg_type_map_in_ruby.c +1 -1
  38. data/ext/{util.c → pg_util.c} +10 -10
  39. data/ext/{util.h → pg_util.h} +2 -2
  40. data/lib/pg.rb +23 -13
  41. data/lib/pg/basic_type_mapping.rb +155 -32
  42. data/lib/pg/binary_decoder.rb +23 -0
  43. data/lib/pg/coder.rb +23 -2
  44. data/lib/pg/connection.rb +73 -13
  45. data/lib/pg/constants.rb +2 -1
  46. data/lib/pg/exceptions.rb +2 -1
  47. data/lib/pg/result.rb +24 -7
  48. data/lib/pg/text_decoder.rb +24 -22
  49. data/lib/pg/text_encoder.rb +40 -8
  50. data/lib/pg/tuple.rb +30 -0
  51. data/lib/pg/type_map_by_column.rb +3 -2
  52. data/spec/helpers.rb +61 -36
  53. data/spec/pg/basic_type_mapping_spec.rb +415 -36
  54. data/spec/pg/connection_spec.rb +732 -327
  55. data/spec/pg/connection_sync_spec.rb +41 -0
  56. data/spec/pg/result_spec.rb +253 -21
  57. data/spec/pg/tuple_spec.rb +333 -0
  58. data/spec/pg/type_map_by_class_spec.rb +4 -4
  59. data/spec/pg/type_map_by_column_spec.rb +6 -2
  60. data/spec/pg/type_map_by_mri_type_spec.rb +2 -2
  61. data/spec/pg/type_map_by_oid_spec.rb +3 -3
  62. data/spec/pg/type_map_in_ruby_spec.rb +1 -1
  63. data/spec/pg/type_map_spec.rb +1 -1
  64. data/spec/pg/type_spec.rb +446 -20
  65. data/spec/pg_spec.rb +2 -2
  66. metadata +63 -72
  67. metadata.gz.sig +0 -0
  68. data/sample/array_insert.rb +0 -20
  69. data/sample/async_api.rb +0 -106
  70. data/sample/async_copyto.rb +0 -39
  71. data/sample/async_mixed.rb +0 -56
  72. data/sample/check_conn.rb +0 -21
  73. data/sample/copyfrom.rb +0 -81
  74. data/sample/copyto.rb +0 -19
  75. data/sample/cursor.rb +0 -21
  76. data/sample/disk_usage_report.rb +0 -186
  77. data/sample/issue-119.rb +0 -94
  78. data/sample/losample.rb +0 -69
  79. data/sample/minimal-testcase.rb +0 -17
  80. data/sample/notify_wait.rb +0 -72
  81. data/sample/pg_statistics.rb +0 -294
  82. data/sample/replication_monitor.rb +0 -231
  83. data/sample/test_binary_values.rb +0 -33
  84. data/sample/wal_shipper.rb +0 -434
  85. data/sample/warehouse_partitions.rb +0 -320
@@ -23,6 +23,7 @@ typedef struct {
23
23
  static void
24
24
  pg_copycoder_mark( t_pg_copycoder *this )
25
25
  {
26
+ pg_coder_mark(&this->comp);
26
27
  rb_gc_mark(this->typemap);
27
28
  rb_gc_mark(this->null_string);
28
29
  }
@@ -112,10 +113,11 @@ pg_copycoder_null_string_get(VALUE self)
112
113
  * call-seq:
113
114
  * coder.type_map = map
114
115
  *
116
+ * Defines how single columns are encoded or decoded.
115
117
  * +map+ must be a kind of PG::TypeMap .
116
118
  *
117
119
  * Defaults to a PG::TypeMapAllStrings , so that PG::TextEncoder::String respectively
118
- * PG::TextDecoder::String is used for encoding/decoding of all columns.
120
+ * PG::TextDecoder::String is used for encoding/decoding of each column.
119
121
  *
120
122
  */
121
123
  static VALUE
@@ -136,6 +138,7 @@ pg_copycoder_type_map_set(VALUE self, VALUE type_map)
136
138
  * call-seq:
137
139
  * coder.type_map -> PG::TypeMap
138
140
  *
141
+ * The PG::TypeMap that will be used for encoding and decoding of columns.
139
142
  */
140
143
  static VALUE
141
144
  pg_copycoder_type_map_get(VALUE self)
@@ -167,9 +170,15 @@ pg_copycoder_type_map_get(VALUE self)
167
170
  * conn.put_copy_data ["string2", 42, true]
168
171
  * end
169
172
  * This creates +my_table+ and inserts two rows.
173
+ *
174
+ * It is possible to manually assign a type encoder for each column per PG::TypeMapByColumn,
175
+ * or to make use of PG::BasicTypeMapBasedOnResult to assign them based on the table OIDs.
176
+ *
177
+ * See also PG::TextDecoder::CopyRow for the decoding direction with
178
+ * PG::Connection#get_copy_data .
170
179
  */
171
180
  static int
172
- pg_text_enc_copy_row(t_pg_coder *conv, VALUE value, char *out, VALUE *intermediate)
181
+ pg_text_enc_copy_row(t_pg_coder *conv, VALUE value, char *out, VALUE *intermediate, int enc_idx)
173
182
  {
174
183
  t_pg_copycoder *this = (t_pg_copycoder *)conv;
175
184
  t_pg_coder_enc_func enc_func;
@@ -184,6 +193,7 @@ pg_text_enc_copy_row(t_pg_coder *conv, VALUE value, char *out, VALUE *intermedia
184
193
 
185
194
  /* Allocate a new string with embedded capacity and realloc exponential when needed. */
186
195
  PG_RB_STR_NEW( *intermediate, current_out, end_capa_ptr );
196
+ PG_ENCODING_SET_NOCHECK(*intermediate, enc_idx);
187
197
 
188
198
  for( i=0; i<RARRAY_LEN(value); i++){
189
199
  char *ptr1;
@@ -211,7 +221,7 @@ pg_text_enc_copy_row(t_pg_coder *conv, VALUE value, char *out, VALUE *intermedia
211
221
  enc_func = pg_coder_enc_func(p_elem_coder);
212
222
 
213
223
  /* 1st pass for retiving the required memory space */
214
- strlen = enc_func(p_elem_coder, entry, NULL, &subint);
224
+ strlen = enc_func(p_elem_coder, entry, NULL, &subint, enc_idx);
215
225
 
216
226
  if( strlen == -1 ){
217
227
  /* we can directly use String value in subint */
@@ -234,7 +244,7 @@ pg_text_enc_copy_row(t_pg_coder *conv, VALUE value, char *out, VALUE *intermedia
234
244
  PG_RB_STR_ENSURE_CAPA( *intermediate, strlen * 2, current_out, end_capa_ptr );
235
245
 
236
246
  /* Place the unescaped string at current output position. */
237
- strlen = enc_func(p_elem_coder, entry, current_out, &subint);
247
+ strlen = enc_func(p_elem_coder, entry, current_out, &subint, enc_idx);
238
248
 
239
249
  ptr1 = current_out;
240
250
  ptr2 = current_out + strlen;
@@ -301,15 +311,38 @@ GetDecimalFromHex(char hex)
301
311
  * strings by PG::TextDecoder::String.
302
312
  *
303
313
  * Example with default type map ( TypeMapAllStrings ):
314
+ * conn.exec("CREATE TABLE my_table AS VALUES('astring', 7, FALSE), ('string2', 42, TRUE) ")
315
+ *
304
316
  * deco = PG::TextDecoder::CopyRow.new
305
317
  * conn.copy_data "COPY my_table TO STDOUT", deco do
306
318
  * while row=conn.get_copy_data
307
319
  * p row
308
320
  * end
309
321
  * end
310
- * This prints all rows of +my_table+ to stdout:
322
+ * This prints all rows of +my_table+ :
311
323
  * ["astring", "7", "f"]
312
324
  * ["string2", "42", "t"]
325
+ *
326
+ * Example with column based type map:
327
+ * tm = PG::TypeMapByColumn.new( [
328
+ * PG::TextDecoder::String.new,
329
+ * PG::TextDecoder::Integer.new,
330
+ * PG::TextDecoder::Boolean.new] )
331
+ * deco = PG::TextDecoder::CopyRow.new( type_map: tm )
332
+ * conn.copy_data "COPY my_table TO STDOUT", deco do
333
+ * while row=conn.get_copy_data
334
+ * p row
335
+ * end
336
+ * end
337
+ * This prints the rows with type casted columns:
338
+ * ["astring", 7, false]
339
+ * ["string2", 42, true]
340
+ *
341
+ * Instead of manually assigning a type decoder for each column, PG::BasicTypeMapForResults
342
+ * can be used to assign them based on the table OIDs.
343
+ *
344
+ * See also PG::TextEncoder::CopyRow for the encoding direction with
345
+ * PG::Connection#put_copy_data .
313
346
  */
314
347
  /*
315
348
  * Parse the current line into separate attributes (fields),
@@ -324,7 +357,7 @@ GetDecimalFromHex(char hex)
324
357
  * src/backend/commands/copy.c
325
358
  */
326
359
  static VALUE
327
- pg_text_dec_copy_row(t_pg_coder *conv, char *input_line, int len, int _tuple, int _field, int enc_idx)
360
+ pg_text_dec_copy_row(t_pg_coder *conv, const char *input_line, int len, int _tuple, int _field, int enc_idx)
328
361
  {
329
362
  t_pg_copycoder *this = (t_pg_copycoder *)conv;
330
363
 
@@ -338,8 +371,8 @@ pg_text_dec_copy_row(t_pg_coder *conv, char *input_line, int len, int _tuple, in
338
371
  int fieldno;
339
372
  int expected_fields;
340
373
  char *output_ptr;
341
- char *cur_ptr;
342
- char *line_end_ptr;
374
+ const char *cur_ptr;
375
+ const char *line_end_ptr;
343
376
  char *end_capa_ptr;
344
377
  t_typemap *p_typemap;
345
378
 
@@ -351,7 +384,7 @@ pg_text_dec_copy_row(t_pg_coder *conv, char *input_line, int len, int _tuple, in
351
384
 
352
385
  /* Allocate a new string with embedded capacity and realloc later with
353
386
  * exponential growing size when needed. */
354
- PG_RB_TAINTED_STR_NEW( field_str, output_ptr, end_capa_ptr );
387
+ PG_RB_STR_NEW( field_str, output_ptr, end_capa_ptr );
355
388
 
356
389
  /* set pointer variables for loop */
357
390
  cur_ptr = input_line;
@@ -362,8 +395,8 @@ pg_text_dec_copy_row(t_pg_coder *conv, char *input_line, int len, int _tuple, in
362
395
  for (;;)
363
396
  {
364
397
  int found_delim = 0;
365
- char *start_ptr;
366
- char *end_ptr;
398
+ const char *start_ptr;
399
+ const char *end_ptr;
367
400
  int input_len;
368
401
 
369
402
  /* Remember start of field on input side */
@@ -513,7 +546,7 @@ pg_text_dec_copy_row(t_pg_coder *conv, char *input_line, int len, int _tuple, in
513
546
  if( field_value == field_str ){
514
547
  /* Our output string will be send to the user, so we can not reuse
515
548
  * it for the next field. */
516
- PG_RB_TAINTED_STR_NEW( field_str, output_ptr, end_capa_ptr );
549
+ PG_RB_STR_NEW( field_str, output_ptr, end_capa_ptr );
517
550
  }
518
551
  }
519
552
  /* Reset the pointer to the start of the output/buffer string. */
@@ -555,7 +588,12 @@ init_pg_copycoder()
555
588
  /* rb_mPG_TextEncoder = rb_define_module_under( rb_mPG, "TextEncoder" ); */
556
589
  /* dummy = rb_define_class_under( rb_mPG_TextEncoder, "CopyRow", rb_cPG_CopyEncoder ); */
557
590
  pg_define_coder( "CopyRow", pg_text_enc_copy_row, rb_cPG_CopyEncoder, rb_mPG_TextEncoder );
591
+ rb_include_module( rb_cPG_CopyEncoder, rb_mPG_BinaryFormatting );
592
+
558
593
  /* rb_mPG_TextDecoder = rb_define_module_under( rb_mPG, "TextDecoder" ); */
559
594
  /* dummy = rb_define_class_under( rb_mPG_TextDecoder, "CopyRow", rb_cPG_CopyDecoder ); */
560
595
  pg_define_coder( "CopyRow", pg_text_dec_copy_row, rb_cPG_CopyDecoder, rb_mPG_TextDecoder );
596
+ /* Although CopyRow is a text decoder, data can contain zero bytes and are not zero terminated.
597
+ * They are handled like binaries. So format is set to 1 (binary). */
598
+ rb_include_module( rb_cPG_CopyDecoder, rb_mPG_BinaryFormatting );
561
599
  }
@@ -0,0 +1,491 @@
1
+ /*
2
+ * pg_record_coder.c - PG::Coder class extension
3
+ *
4
+ */
5
+
6
+ #include "pg.h"
7
+
8
+ VALUE rb_cPG_RecordCoder;
9
+ VALUE rb_cPG_RecordEncoder;
10
+ VALUE rb_cPG_RecordDecoder;
11
+
12
+ typedef struct {
13
+ t_pg_coder comp;
14
+ VALUE typemap;
15
+ } t_pg_recordcoder;
16
+
17
+
18
+ static void
19
+ pg_recordcoder_mark( t_pg_recordcoder *this )
20
+ {
21
+ pg_coder_mark(&this->comp);
22
+ rb_gc_mark(this->typemap);
23
+ }
24
+
25
+ static VALUE
26
+ pg_recordcoder_encoder_allocate( VALUE klass )
27
+ {
28
+ t_pg_recordcoder *this;
29
+ VALUE self = Data_Make_Struct( klass, t_pg_recordcoder, pg_recordcoder_mark, -1, this );
30
+ pg_coder_init_encoder( self );
31
+ this->typemap = pg_typemap_all_strings;
32
+ return self;
33
+ }
34
+
35
+ static VALUE
36
+ pg_recordcoder_decoder_allocate( VALUE klass )
37
+ {
38
+ t_pg_recordcoder *this;
39
+ VALUE self = Data_Make_Struct( klass, t_pg_recordcoder, pg_recordcoder_mark, -1, this );
40
+ pg_coder_init_decoder( self );
41
+ this->typemap = pg_typemap_all_strings;
42
+ return self;
43
+ }
44
+
45
+ /*
46
+ * call-seq:
47
+ * coder.type_map = map
48
+ *
49
+ * Defines how single columns are encoded or decoded.
50
+ * +map+ must be a kind of PG::TypeMap .
51
+ *
52
+ * Defaults to a PG::TypeMapAllStrings , so that PG::TextEncoder::String respectively
53
+ * PG::TextDecoder::String is used for encoding/decoding of each column.
54
+ *
55
+ */
56
+ static VALUE
57
+ pg_recordcoder_type_map_set(VALUE self, VALUE type_map)
58
+ {
59
+ t_pg_recordcoder *this = DATA_PTR( self );
60
+
61
+ if ( !rb_obj_is_kind_of(type_map, rb_cTypeMap) ){
62
+ rb_raise( rb_eTypeError, "wrong elements type %s (expected some kind of PG::TypeMap)",
63
+ rb_obj_classname( type_map ) );
64
+ }
65
+ this->typemap = type_map;
66
+
67
+ return type_map;
68
+ }
69
+
70
+ /*
71
+ * call-seq:
72
+ * coder.type_map -> PG::TypeMap
73
+ *
74
+ * The PG::TypeMap that will be used for encoding and decoding of columns.
75
+ */
76
+ static VALUE
77
+ pg_recordcoder_type_map_get(VALUE self)
78
+ {
79
+ t_pg_recordcoder *this = DATA_PTR( self );
80
+
81
+ return this->typemap;
82
+ }
83
+
84
+
85
+ /*
86
+ * Document-class: PG::TextEncoder::Record < PG::RecordEncoder
87
+ *
88
+ * This class encodes one record of columns for transmission as query parameter in text format.
89
+ * See PostgreSQL {Composite Types}[https://www.postgresql.org/docs/current/rowtypes.html] for a description of the format and how it can be used.
90
+ *
91
+ * PostgreSQL allows composite types to be used in many of the same ways that simple types can be used.
92
+ * For example, a column of a table can be declared to be of a composite type.
93
+ *
94
+ * The encoder expects the record columns as array of values.
95
+ * The single values are encoded as defined in the assigned #type_map.
96
+ * If no type_map was assigned, all values are converted to strings by PG::TextEncoder::String.
97
+ *
98
+ * It is possible to manually assign a type encoder for each column per PG::TypeMapByColumn,
99
+ * or to make use of PG::BasicTypeMapBasedOnResult to assign them based on the table OIDs.
100
+ *
101
+ * Encode a record from an <code>Array<String></code> to a +String+ in PostgreSQL Composite Type format (uses default type map TypeMapAllStrings):
102
+ * PG::TextEncoder::Record.new.encode([1, 2]) # => "(\"1\",\"2\")"
103
+ *
104
+ * Encode a record from <code>Array<Float></code> to +String+ :
105
+ * # Build a type map for two Floats
106
+ * tm = PG::TypeMapByColumn.new([PG::TextEncoder::Float.new]*2)
107
+ * # Use this type map to encode the record:
108
+ * PG::TextEncoder::Record.new(type_map: tm).encode([1,2])
109
+ * # => "(\"1.0000000000000000E+00\",\"2.0000000000000000E+00\")"
110
+ *
111
+ * Records can also be encoded and decoded directly to and from the database.
112
+ * This avoids intermediate string allocations and is very fast.
113
+ * Take the following type and table definitions:
114
+ * conn.exec("CREATE TYPE complex AS (r float, i float) ")
115
+ * conn.exec("CREATE TABLE my_table (v1 complex, v2 complex) ")
116
+ *
117
+ * A record can be encoded by adding a type map to Connection#exec_params and siblings:
118
+ * # Build a type map for the two floats "r" and "i" as in our "complex" type
119
+ * tm = PG::TypeMapByColumn.new([PG::TextEncoder::Float.new]*2)
120
+ * # Build a record encoder to encode this type as a record:
121
+ * enco = PG::TextEncoder::Record.new(type_map: tm)
122
+ * # Insert table data and use the encoder to cast the complex value "v1" from ruby array:
123
+ * conn.exec_params("INSERT INTO my_table VALUES ($1) RETURNING v1", [[1,2]], 0, PG::TypeMapByColumn.new([enco])).to_a
124
+ * # => [{"v1"=>"(1,2)"}]
125
+ *
126
+ * Alternatively the typemap can be build based on database OIDs rather than manually assigning encoders.
127
+ * # Fetch a NULL record of our type to retrieve the OIDs of the two fields "r" and "i"
128
+ * oids = conn.exec( "SELECT (NULL::complex).*" )
129
+ * # Build a type map (PG::TypeMapByColumn) for encoding the "complex" type
130
+ * etm = PG::BasicTypeMapBasedOnResult.new(conn).build_column_map( oids )
131
+ *
132
+ * It's also possible to use the BasicTypeMapForQueries to send records to the database server.
133
+ * In contrast to ORM libraries, PG doesn't have information regarding the type of data the server is expecting.
134
+ * So BasicTypeMapForQueries works based on the class of the values to be sent and it has to be instructed that a ruby array shall be casted to a record.
135
+ * # Retrieve OIDs of all basic types from the database
136
+ * etm = PG::BasicTypeMapForQueries.new(conn)
137
+ * etm.encode_array_as = :record
138
+ * # Apply the basic type registry to all values sent to the server
139
+ * conn.type_map_for_queries = etm
140
+ * # Send a complex number as an array of two integers
141
+ * conn.exec_params("INSERT INTO my_table VALUES ($1) RETURNING v1", [[1,2]]).to_a
142
+ * # => [{"v1"=>"(1,2)"}]
143
+ *
144
+ * Records can also be nested or further wrapped into other encoders like PG::TextEncoder::CopyRow.
145
+ *
146
+ * See also PG::TextDecoder::Record for the decoding direction.
147
+ */
148
+ static int
149
+ pg_text_enc_record(t_pg_coder *conv, VALUE value, char *out, VALUE *intermediate, int enc_idx)
150
+ {
151
+ t_pg_recordcoder *this = (t_pg_recordcoder *)conv;
152
+ t_pg_coder_enc_func enc_func;
153
+ static t_pg_coder *p_elem_coder;
154
+ int i;
155
+ t_typemap *p_typemap;
156
+ char *current_out;
157
+ char *end_capa_ptr;
158
+
159
+ p_typemap = DATA_PTR( this->typemap );
160
+ p_typemap->funcs.fit_to_query( this->typemap, value );
161
+
162
+ /* Allocate a new string with embedded capacity and realloc exponential when needed. */
163
+ PG_RB_STR_NEW( *intermediate, current_out, end_capa_ptr );
164
+ PG_ENCODING_SET_NOCHECK(*intermediate, enc_idx);
165
+ PG_RB_STR_ENSURE_CAPA( *intermediate, 1, current_out, end_capa_ptr );
166
+ *current_out++ = '(';
167
+
168
+ for( i=0; i<RARRAY_LEN(value); i++){
169
+ char *ptr1;
170
+ char *ptr2;
171
+ int strlen;
172
+ int backslashs;
173
+ VALUE subint;
174
+ VALUE entry;
175
+
176
+ entry = rb_ary_entry(value, i);
177
+
178
+ if( i > 0 ){
179
+ PG_RB_STR_ENSURE_CAPA( *intermediate, 1, current_out, end_capa_ptr );
180
+ *current_out++ = ',';
181
+ }
182
+
183
+ switch(TYPE(entry)){
184
+ case T_NIL:
185
+ /* emit nothing... */
186
+ break;
187
+ default:
188
+ p_elem_coder = p_typemap->funcs.typecast_query_param(p_typemap, entry, i);
189
+ enc_func = pg_coder_enc_func(p_elem_coder);
190
+
191
+ /* 1st pass for retiving the required memory space */
192
+ strlen = enc_func(p_elem_coder, entry, NULL, &subint, enc_idx);
193
+
194
+ if( strlen == -1 ){
195
+ /* we can directly use String value in subint */
196
+ strlen = RSTRING_LEN(subint);
197
+
198
+ /* size of string assuming the worst case, that every character must be escaped. */
199
+ PG_RB_STR_ENSURE_CAPA( *intermediate, strlen * 2 + 2, current_out, end_capa_ptr );
200
+
201
+ *current_out++ = '"';
202
+ /* Record string from subint with backslash escaping */
203
+ for(ptr1 = RSTRING_PTR(subint); ptr1 < RSTRING_PTR(subint) + strlen; ptr1++) {
204
+ if (*ptr1 == '"' || *ptr1 == '\\') {
205
+ *current_out++ = *ptr1;
206
+ }
207
+ *current_out++ = *ptr1;
208
+ }
209
+ *current_out++ = '"';
210
+ } else {
211
+ /* 2nd pass for writing the data to prepared buffer */
212
+ /* size of string assuming the worst case, that every character must be escaped. */
213
+ PG_RB_STR_ENSURE_CAPA( *intermediate, strlen * 2 + 2, current_out, end_capa_ptr );
214
+
215
+ *current_out++ = '"';
216
+ /* Place the unescaped string at current output position. */
217
+ strlen = enc_func(p_elem_coder, entry, current_out, &subint, enc_idx);
218
+
219
+ ptr1 = current_out;
220
+ ptr2 = current_out + strlen;
221
+
222
+ /* count required backlashs */
223
+ for(backslashs = 0; ptr1 != ptr2; ptr1++) {
224
+ /* Escape backslash itself, newline, carriage return, and the current delimiter character. */
225
+ if(*ptr1 == '"' || *ptr1 == '\\'){
226
+ backslashs++;
227
+ }
228
+ }
229
+
230
+ ptr1 = current_out + strlen;
231
+ ptr2 = current_out + strlen + backslashs;
232
+ current_out = ptr2;
233
+
234
+ /* Then store the escaped string on the final position, walking
235
+ * right to left, until all backslashs are placed. */
236
+ while( ptr1 != ptr2 ) {
237
+ *--ptr2 = *--ptr1;
238
+ if(*ptr1 == '"' || *ptr1 == '\\'){
239
+ *--ptr2 = *ptr1;
240
+ }
241
+ }
242
+ *current_out++ = '"';
243
+ }
244
+ }
245
+ }
246
+ PG_RB_STR_ENSURE_CAPA( *intermediate, 1, current_out, end_capa_ptr );
247
+ *current_out++ = ')';
248
+
249
+ rb_str_set_len( *intermediate, current_out - RSTRING_PTR(*intermediate) );
250
+
251
+ return -1;
252
+ }
253
+
254
+ /*
255
+ * record_isspace() --- a non-locale-dependent isspace()
256
+ *
257
+ * We used to use isspace() for parsing array values, but that has
258
+ * undesirable results: an array value might be silently interpreted
259
+ * differently depending on the locale setting. Now we just hard-wire
260
+ * the traditional ASCII definition of isspace().
261
+ */
262
+ static int
263
+ record_isspace(char ch)
264
+ {
265
+ if (ch == ' ' ||
266
+ ch == '\t' ||
267
+ ch == '\n' ||
268
+ ch == '\r' ||
269
+ ch == '\v' ||
270
+ ch == '\f')
271
+ return 1;
272
+ return 0;
273
+ }
274
+
275
+ /*
276
+ * Document-class: PG::TextDecoder::Record < PG::RecordDecoder
277
+ *
278
+ * This class decodes one record of values received from a composite type column in text format.
279
+ * See PostgreSQL {Composite Types}[https://www.postgresql.org/docs/current/rowtypes.html] for a description of the format and how it can be used.
280
+ *
281
+ * PostgreSQL allows composite types to be used in many of the same ways that simple types can be used.
282
+ * For example, a column of a table can be declared to be of a composite type.
283
+ *
284
+ * The columns are returned from the decoder as array of values.
285
+ * The single values are decoded as defined in the assigned #type_map.
286
+ * If no type_map was assigned, all values are converted to strings by PG::TextDecoder::String.
287
+ *
288
+ * Decode a record in Composite Type format from +String+ to <code>Array<String></code> (uses default type map TypeMapAllStrings):
289
+ * PG::TextDecoder::Record.new.decode("(1,2)") # => ["1", "2"]
290
+ *
291
+ * Decode a record from +String+ to <code>Array<Float></code> :
292
+ * # Build a type map for two Floats
293
+ * tm = PG::TypeMapByColumn.new([PG::TextDecoder::Float.new]*2)
294
+ * # Use this type map to decode the record:
295
+ * PG::TextDecoder::Record.new(type_map: tm).decode("(1,2)")
296
+ * # => [1.0, 2.0]
297
+ *
298
+ * Records can also be encoded and decoded directly to and from the database.
299
+ * This avoids intermediate String allocations and is very fast.
300
+ * Take the following type and table definitions:
301
+ * conn.exec("CREATE TYPE complex AS (r float, i float) ")
302
+ * conn.exec("CREATE TABLE my_table (v1 complex, v2 complex) ")
303
+ * conn.exec("INSERT INTO my_table VALUES((2,3), (4,5)), ((6,7), (8,9)) ")
304
+ *
305
+ * The record can be decoded by applying a type map to the PG::Result object:
306
+ * # Build a type map for two floats "r" and "i"
307
+ * tm = PG::TypeMapByColumn.new([PG::TextDecoder::Float.new]*2)
308
+ * # Build a record decoder to decode this two-value type:
309
+ * deco = PG::TextDecoder::Record.new(type_map: tm)
310
+ * # Fetch table data and use the decoder to cast the two complex values "v1" and "v2":
311
+ * conn.exec("SELECT * FROM my_table").map_types!(PG::TypeMapByColumn.new([deco]*2)).to_a
312
+ * # => [{"v1"=>[2.0, 3.0], "v2"=>[4.0, 5.0]}, {"v1"=>[6.0, 7.0], "v2"=>[8.0, 9.0]}]
313
+ *
314
+ * It's more very convenient to use the PG::BasicTypeRegistry, which is based on database OIDs.
315
+ * # Fetch a NULL record of our type to retrieve the OIDs of the two fields "r" and "i"
316
+ * oids = conn.exec( "SELECT (NULL::complex).*" )
317
+ * # Build a type map (PG::TypeMapByColumn) for decoding the "complex" type
318
+ * dtm = PG::BasicTypeMapForResults.new(conn).build_column_map( oids )
319
+ * # Register a record decoder for decoding our type "complex"
320
+ * PG::BasicTypeRegistry.register_coder(PG::TextDecoder::Record.new(type_map: dtm, name: "complex"))
321
+ * # Apply the basic type registry to all results retrieved from the server
322
+ * conn.type_map_for_results = PG::BasicTypeMapForResults.new(conn)
323
+ * # Now queries decode the "complex" type (and many basic types) automatically
324
+ * conn.exec("SELECT * FROM my_table").to_a
325
+ * # => [{"v1"=>[2.0, 3.0], "v2"=>[4.0, 5.0]}, {"v1"=>[6.0, 7.0], "v2"=>[8.0, 9.0]}]
326
+ *
327
+ * Records can also be nested or further wrapped into other decoders like PG::TextDecoder::CopyRow.
328
+ *
329
+ * See also PG::TextEncoder::Record for the encoding direction (data sent to the server).
330
+ */
331
+ /*
332
+ * Parse the current line into separate attributes (fields),
333
+ * performing de-escaping as needed.
334
+ *
335
+ * All fields are gathered into a ruby Array. The de-escaped field data is written
336
+ * into to a ruby String. This object is reused for non string columns.
337
+ * For String columns the field value is directly used as return value and no
338
+ * reuse of the memory is done.
339
+ *
340
+ * The parser is thankfully borrowed from the PostgreSQL sources:
341
+ * src/backend/utils/adt/rowtypes.c
342
+ */
343
+ static VALUE
344
+ pg_text_dec_record(t_pg_coder *conv, char *input_line, int len, int _tuple, int _field, int enc_idx)
345
+ {
346
+ t_pg_recordcoder *this = (t_pg_recordcoder *)conv;
347
+
348
+ /* Return value: array */
349
+ VALUE array;
350
+
351
+ /* Current field */
352
+ VALUE field_str;
353
+
354
+ int fieldno;
355
+ int expected_fields;
356
+ char *output_ptr;
357
+ char *cur_ptr;
358
+ char *end_capa_ptr;
359
+ t_typemap *p_typemap;
360
+
361
+ p_typemap = DATA_PTR( this->typemap );
362
+ expected_fields = p_typemap->funcs.fit_to_copy_get( this->typemap );
363
+
364
+ /* The received input string will probably have this->nfields fields. */
365
+ array = rb_ary_new2(expected_fields);
366
+
367
+ /* Allocate a new string with embedded capacity and realloc later with
368
+ * exponential growing size when needed. */
369
+ PG_RB_STR_NEW( field_str, output_ptr, end_capa_ptr );
370
+
371
+ /* set pointer variables for loop */
372
+ cur_ptr = input_line;
373
+
374
+ /*
375
+ * Scan the string. We use "buf" to accumulate the de-quoted data for
376
+ * each column, which is then fed to the appropriate input converter.
377
+ */
378
+ /* Allow leading whitespace */
379
+ while (*cur_ptr && record_isspace(*cur_ptr))
380
+ cur_ptr++;
381
+ if (*cur_ptr++ != '(')
382
+ rb_raise( rb_eArgError, "malformed record literal: \"%s\" - Missing left parenthesis.", input_line );
383
+
384
+ for (fieldno = 0; ; fieldno++)
385
+ {
386
+ /* Check for null: completely empty input means null */
387
+ if (*cur_ptr == ',' || *cur_ptr == ')')
388
+ {
389
+ rb_ary_push(array, Qnil);
390
+ }
391
+ else
392
+ {
393
+ /* Extract string for this column */
394
+ int inquote = 0;
395
+ VALUE field_value;
396
+
397
+ while (inquote || !(*cur_ptr == ',' || *cur_ptr == ')'))
398
+ {
399
+ char ch = *cur_ptr++;
400
+
401
+ if (ch == '\0')
402
+ rb_raise( rb_eArgError, "malformed record literal: \"%s\" - Unexpected end of input.", input_line );
403
+ if (ch == '\\')
404
+ {
405
+ if (*cur_ptr == '\0')
406
+ rb_raise( rb_eArgError, "malformed record literal: \"%s\" - Unexpected end of input.", input_line );
407
+ PG_RB_STR_ENSURE_CAPA( field_str, 1, output_ptr, end_capa_ptr );
408
+ *output_ptr++ = *cur_ptr++;
409
+ }
410
+ else if (ch == '"')
411
+ {
412
+ if (!inquote)
413
+ inquote = 1;
414
+ else if (*cur_ptr == '"')
415
+ {
416
+ /* doubled quote within quote sequence */
417
+ PG_RB_STR_ENSURE_CAPA( field_str, 1, output_ptr, end_capa_ptr );
418
+ *output_ptr++ = *cur_ptr++;
419
+ }
420
+ else
421
+ inquote = 0;
422
+ } else {
423
+ PG_RB_STR_ENSURE_CAPA( field_str, 1, output_ptr, end_capa_ptr );
424
+ /* Add ch to output string */
425
+ *output_ptr++ = ch;
426
+ }
427
+ }
428
+
429
+ /* Convert the column value */
430
+ rb_str_set_len( field_str, output_ptr - RSTRING_PTR(field_str) );
431
+ field_value = p_typemap->funcs.typecast_copy_get( p_typemap, field_str, fieldno, 0, enc_idx );
432
+
433
+ rb_ary_push(array, field_value);
434
+
435
+ if( field_value == field_str ){
436
+ /* Our output string will be send to the user, so we can not reuse
437
+ * it for the next field. */
438
+ PG_RB_STR_NEW( field_str, output_ptr, end_capa_ptr );
439
+ }
440
+ /* Reset the pointer to the start of the output/buffer string. */
441
+ output_ptr = RSTRING_PTR(field_str);
442
+ }
443
+
444
+ /* Skip comma that separates prior field from this one */
445
+ if (*cur_ptr == ',') {
446
+ cur_ptr++;
447
+ } else if (*cur_ptr == ')') {
448
+ cur_ptr++;
449
+ /* Done if we hit closing parenthesis */
450
+ break;
451
+ } else {
452
+ rb_raise( rb_eArgError, "malformed record literal: \"%s\" - Too few columns.", input_line );
453
+ }
454
+ }
455
+
456
+ /* Allow trailing whitespace */
457
+ while (*cur_ptr && record_isspace(*cur_ptr))
458
+ cur_ptr++;
459
+ if (*cur_ptr)
460
+ rb_raise( rb_eArgError, "malformed record literal: \"%s\" - Junk after right parenthesis.", input_line );
461
+
462
+ return array;
463
+ }
464
+
465
+
466
+ void
467
+ init_pg_recordcoder()
468
+ {
469
+ /* Document-class: PG::RecordCoder < PG::Coder
470
+ *
471
+ * This is the base class for all type cast classes for COPY data,
472
+ */
473
+ rb_cPG_RecordCoder = rb_define_class_under( rb_mPG, "RecordCoder", rb_cPG_Coder );
474
+ rb_define_method( rb_cPG_RecordCoder, "type_map=", pg_recordcoder_type_map_set, 1 );
475
+ rb_define_method( rb_cPG_RecordCoder, "type_map", pg_recordcoder_type_map_get, 0 );
476
+
477
+ /* Document-class: PG::RecordEncoder < PG::RecordCoder */
478
+ rb_cPG_RecordEncoder = rb_define_class_under( rb_mPG, "RecordEncoder", rb_cPG_RecordCoder );
479
+ rb_define_alloc_func( rb_cPG_RecordEncoder, pg_recordcoder_encoder_allocate );
480
+ /* Document-class: PG::RecordDecoder < PG::RecordCoder */
481
+ rb_cPG_RecordDecoder = rb_define_class_under( rb_mPG, "RecordDecoder", rb_cPG_RecordCoder );
482
+ rb_define_alloc_func( rb_cPG_RecordDecoder, pg_recordcoder_decoder_allocate );
483
+
484
+ /* Make RDoc aware of the encoder classes... */
485
+ /* rb_mPG_TextEncoder = rb_define_module_under( rb_mPG, "TextEncoder" ); */
486
+ /* dummy = rb_define_class_under( rb_mPG_TextEncoder, "Record", rb_cPG_RecordEncoder ); */
487
+ pg_define_coder( "Record", pg_text_enc_record, rb_cPG_RecordEncoder, rb_mPG_TextEncoder );
488
+ /* rb_mPG_TextDecoder = rb_define_module_under( rb_mPG, "TextDecoder" ); */
489
+ /* dummy = rb_define_class_under( rb_mPG_TextDecoder, "Record", rb_cPG_RecordDecoder ); */
490
+ pg_define_coder( "Record", pg_text_dec_record, rb_cPG_RecordDecoder, rb_mPG_TextDecoder );
491
+ }