pg 0.21.0 → 1.2.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (67) hide show
  1. checksums.yaml +5 -5
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/ChangeLog +0 -6595
  5. data/History.rdoc +184 -0
  6. data/Manifest.txt +8 -3
  7. data/README-Windows.rdoc +4 -4
  8. data/README.ja.rdoc +1 -2
  9. data/README.rdoc +58 -13
  10. data/Rakefile +10 -9
  11. data/Rakefile.cross +68 -71
  12. data/ext/errorcodes.def +76 -0
  13. data/ext/errorcodes.rb +1 -1
  14. data/ext/errorcodes.txt +21 -2
  15. data/ext/extconf.rb +18 -36
  16. data/ext/gvl_wrappers.c +4 -0
  17. data/ext/gvl_wrappers.h +23 -39
  18. data/ext/pg.c +154 -144
  19. data/ext/pg.h +68 -95
  20. data/ext/pg_binary_decoder.c +82 -15
  21. data/ext/pg_binary_encoder.c +13 -12
  22. data/ext/pg_coder.c +73 -12
  23. data/ext/pg_connection.c +699 -459
  24. data/ext/pg_copy_coder.c +16 -8
  25. data/ext/pg_record_coder.c +491 -0
  26. data/ext/pg_result.c +571 -195
  27. data/ext/pg_text_decoder.c +606 -40
  28. data/ext/pg_text_encoder.c +185 -54
  29. data/ext/pg_tuple.c +549 -0
  30. data/ext/pg_type_map.c +1 -1
  31. data/ext/pg_type_map_all_strings.c +4 -4
  32. data/ext/pg_type_map_by_class.c +9 -4
  33. data/ext/pg_type_map_by_column.c +7 -6
  34. data/ext/pg_type_map_by_mri_type.c +1 -1
  35. data/ext/pg_type_map_by_oid.c +3 -2
  36. data/ext/pg_type_map_in_ruby.c +1 -1
  37. data/ext/{util.c → pg_util.c} +10 -10
  38. data/ext/{util.h → pg_util.h} +2 -2
  39. data/lib/pg.rb +8 -10
  40. data/lib/pg/basic_type_mapping.rb +121 -25
  41. data/lib/pg/binary_decoder.rb +23 -0
  42. data/lib/pg/coder.rb +23 -2
  43. data/lib/pg/connection.rb +28 -4
  44. data/lib/pg/constants.rb +2 -1
  45. data/lib/pg/exceptions.rb +2 -1
  46. data/lib/pg/result.rb +14 -2
  47. data/lib/pg/text_decoder.rb +21 -26
  48. data/lib/pg/text_encoder.rb +32 -8
  49. data/lib/pg/tuple.rb +30 -0
  50. data/lib/pg/type_map_by_column.rb +3 -2
  51. data/spec/helpers.rb +61 -33
  52. data/spec/pg/basic_type_mapping_spec.rb +362 -37
  53. data/spec/pg/connection_spec.rb +602 -329
  54. data/spec/pg/connection_sync_spec.rb +41 -0
  55. data/spec/pg/result_spec.rb +242 -17
  56. data/spec/pg/tuple_spec.rb +333 -0
  57. data/spec/pg/type_map_by_class_spec.rb +2 -2
  58. data/spec/pg/type_map_by_column_spec.rb +6 -2
  59. data/spec/pg/type_map_by_mri_type_spec.rb +1 -1
  60. data/spec/pg/type_map_by_oid_spec.rb +3 -3
  61. data/spec/pg/type_map_in_ruby_spec.rb +1 -1
  62. data/spec/pg/type_map_spec.rb +1 -1
  63. data/spec/pg/type_spec.rb +364 -18
  64. data/spec/pg_spec.rb +2 -2
  65. metadata +48 -43
  66. metadata.gz.sig +0 -0
  67. data/lib/pg/deprecated_constants.rb +0 -21
data/ext/pg_copy_coder.c CHANGED
@@ -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)
@@ -354,7 +357,7 @@ GetDecimalFromHex(char hex)
354
357
  * src/backend/commands/copy.c
355
358
  */
356
359
  static VALUE
357
- 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)
358
361
  {
359
362
  t_pg_copycoder *this = (t_pg_copycoder *)conv;
360
363
 
@@ -368,8 +371,8 @@ pg_text_dec_copy_row(t_pg_coder *conv, char *input_line, int len, int _tuple, in
368
371
  int fieldno;
369
372
  int expected_fields;
370
373
  char *output_ptr;
371
- char *cur_ptr;
372
- char *line_end_ptr;
374
+ const char *cur_ptr;
375
+ const char *line_end_ptr;
373
376
  char *end_capa_ptr;
374
377
  t_typemap *p_typemap;
375
378
 
@@ -381,7 +384,7 @@ pg_text_dec_copy_row(t_pg_coder *conv, char *input_line, int len, int _tuple, in
381
384
 
382
385
  /* Allocate a new string with embedded capacity and realloc later with
383
386
  * exponential growing size when needed. */
384
- PG_RB_TAINTED_STR_NEW( field_str, output_ptr, end_capa_ptr );
387
+ PG_RB_STR_NEW( field_str, output_ptr, end_capa_ptr );
385
388
 
386
389
  /* set pointer variables for loop */
387
390
  cur_ptr = input_line;
@@ -392,8 +395,8 @@ pg_text_dec_copy_row(t_pg_coder *conv, char *input_line, int len, int _tuple, in
392
395
  for (;;)
393
396
  {
394
397
  int found_delim = 0;
395
- char *start_ptr;
396
- char *end_ptr;
398
+ const char *start_ptr;
399
+ const char *end_ptr;
397
400
  int input_len;
398
401
 
399
402
  /* Remember start of field on input side */
@@ -543,7 +546,7 @@ pg_text_dec_copy_row(t_pg_coder *conv, char *input_line, int len, int _tuple, in
543
546
  if( field_value == field_str ){
544
547
  /* Our output string will be send to the user, so we can not reuse
545
548
  * it for the next field. */
546
- PG_RB_TAINTED_STR_NEW( field_str, output_ptr, end_capa_ptr );
549
+ PG_RB_STR_NEW( field_str, output_ptr, end_capa_ptr );
547
550
  }
548
551
  }
549
552
  /* Reset the pointer to the start of the output/buffer string. */
@@ -585,7 +588,12 @@ init_pg_copycoder()
585
588
  /* rb_mPG_TextEncoder = rb_define_module_under( rb_mPG, "TextEncoder" ); */
586
589
  /* dummy = rb_define_class_under( rb_mPG_TextEncoder, "CopyRow", rb_cPG_CopyEncoder ); */
587
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
+
588
593
  /* rb_mPG_TextDecoder = rb_define_module_under( rb_mPG, "TextDecoder" ); */
589
594
  /* dummy = rb_define_class_under( rb_mPG_TextDecoder, "CopyRow", rb_cPG_CopyDecoder ); */
590
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 );
591
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
+ }