pg 1.1.4 → 1.2.0

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