pg 0.18.1 → 1.5.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- checksums.yaml.gz.sig +0 -0
- data/.appveyor.yml +42 -0
- data/.gems +6 -0
- data/.github/workflows/binary-gems.yml +117 -0
- data/.github/workflows/source-gem.yml +141 -0
- data/.gitignore +22 -0
- data/.hgsigs +34 -0
- data/.hgtags +41 -0
- data/.irbrc +23 -0
- data/.pryrc +23 -0
- data/.tm_properties +21 -0
- data/.travis.yml +49 -0
- data/BSDL +2 -2
- data/Gemfile +17 -0
- data/History.md +901 -0
- data/Manifest.txt +8 -21
- data/README-Windows.rdoc +17 -28
- data/README.ja.md +300 -0
- data/README.md +286 -0
- data/Rakefile +43 -131
- data/Rakefile.cross +89 -70
- data/certs/ged.pem +24 -0
- data/certs/kanis@comcard.de.pem +20 -0
- data/certs/larskanis-2022.pem +26 -0
- data/certs/larskanis-2023.pem +24 -0
- data/certs/larskanis-2024.pem +24 -0
- data/ext/errorcodes.def +113 -0
- data/ext/errorcodes.rb +1 -1
- data/ext/errorcodes.txt +36 -2
- data/ext/extconf.rb +128 -55
- data/ext/gvl_wrappers.c +8 -0
- data/ext/gvl_wrappers.h +44 -33
- data/ext/pg.c +228 -202
- data/ext/pg.h +108 -99
- data/ext/pg_binary_decoder.c +164 -16
- data/ext/pg_binary_encoder.c +249 -22
- data/ext/pg_coder.c +189 -44
- data/ext/pg_connection.c +1889 -1195
- data/ext/pg_copy_coder.c +398 -42
- data/ext/pg_errors.c +1 -1
- data/ext/pg_record_coder.c +522 -0
- data/ext/pg_result.c +729 -234
- data/ext/pg_text_decoder.c +635 -52
- data/ext/pg_text_encoder.c +294 -130
- data/ext/pg_tuple.c +572 -0
- data/ext/pg_type_map.c +64 -23
- data/ext/pg_type_map_all_strings.c +21 -7
- data/ext/pg_type_map_by_class.c +59 -27
- data/ext/pg_type_map_by_column.c +86 -43
- data/ext/pg_type_map_by_mri_type.c +50 -21
- data/ext/pg_type_map_by_oid.c +62 -29
- data/ext/pg_type_map_in_ruby.c +59 -28
- data/ext/{util.c → pg_util.c} +13 -13
- data/ext/{util.h → pg_util.h} +3 -3
- data/lib/pg/basic_type_map_based_on_result.rb +67 -0
- data/lib/pg/basic_type_map_for_queries.rb +202 -0
- data/lib/pg/basic_type_map_for_results.rb +104 -0
- data/lib/pg/basic_type_registry.rb +303 -0
- data/lib/pg/binary_decoder/date.rb +9 -0
- data/lib/pg/binary_decoder/timestamp.rb +26 -0
- data/lib/pg/binary_encoder/timestamp.rb +20 -0
- data/lib/pg/coder.rb +36 -13
- data/lib/pg/connection.rb +813 -74
- data/lib/pg/exceptions.rb +16 -2
- data/lib/pg/result.rb +24 -7
- data/lib/pg/text_decoder/date.rb +18 -0
- data/lib/pg/text_decoder/inet.rb +9 -0
- data/lib/pg/text_decoder/json.rb +14 -0
- data/lib/pg/text_decoder/numeric.rb +9 -0
- data/lib/pg/text_decoder/timestamp.rb +30 -0
- data/lib/pg/text_encoder/date.rb +12 -0
- data/lib/pg/text_encoder/inet.rb +28 -0
- data/lib/pg/text_encoder/json.rb +14 -0
- data/lib/pg/text_encoder/numeric.rb +9 -0
- data/lib/pg/text_encoder/timestamp.rb +24 -0
- data/lib/pg/tuple.rb +30 -0
- data/lib/pg/type_map_by_column.rb +3 -2
- data/lib/pg/version.rb +4 -0
- data/lib/pg.rb +106 -41
- data/misc/openssl-pg-segfault.rb +31 -0
- data/misc/postgres/History.txt +9 -0
- data/misc/postgres/Manifest.txt +5 -0
- data/misc/postgres/README.txt +21 -0
- data/misc/postgres/Rakefile +21 -0
- data/misc/postgres/lib/postgres.rb +16 -0
- data/misc/ruby-pg/History.txt +9 -0
- data/misc/ruby-pg/Manifest.txt +5 -0
- data/misc/ruby-pg/README.txt +21 -0
- data/misc/ruby-pg/Rakefile +21 -0
- data/misc/ruby-pg/lib/ruby/pg.rb +16 -0
- data/pg.gemspec +34 -0
- data/rakelib/task_extension.rb +46 -0
- data/sample/array_insert.rb +1 -1
- data/sample/async_api.rb +4 -8
- data/sample/async_copyto.rb +1 -1
- data/sample/async_mixed.rb +1 -1
- data/sample/check_conn.rb +1 -1
- data/sample/copydata.rb +71 -0
- data/sample/copyfrom.rb +1 -1
- data/sample/copyto.rb +1 -1
- data/sample/cursor.rb +1 -1
- data/sample/disk_usage_report.rb +6 -15
- data/sample/issue-119.rb +2 -2
- data/sample/losample.rb +1 -1
- data/sample/minimal-testcase.rb +2 -2
- data/sample/notify_wait.rb +1 -1
- data/sample/pg_statistics.rb +6 -15
- data/sample/replication_monitor.rb +9 -18
- data/sample/test_binary_values.rb +1 -1
- data/sample/wal_shipper.rb +2 -2
- data/sample/warehouse_partitions.rb +8 -17
- data.tar.gz.sig +0 -0
- metadata +135 -207
- metadata.gz.sig +0 -0
- data/ChangeLog +0 -5378
- data/History.rdoc +0 -297
- data/README.ja.rdoc +0 -14
- data/README.rdoc +0 -161
- data/lib/pg/basic_type_mapping.rb +0 -399
- data/lib/pg/constants.rb +0 -11
- data/lib/pg/text_decoder.rb +0 -42
- data/lib/pg/text_encoder.rb +0 -27
- data/spec/data/expected_trace.out +0 -26
- data/spec/data/random_binary_data +0 -0
- data/spec/helpers.rb +0 -355
- data/spec/pg/basic_type_mapping_spec.rb +0 -251
- data/spec/pg/connection_spec.rb +0 -1459
- data/spec/pg/result_spec.rb +0 -449
- data/spec/pg/type_map_by_class_spec.rb +0 -138
- data/spec/pg/type_map_by_column_spec.rb +0 -222
- data/spec/pg/type_map_by_mri_type_spec.rb +0 -136
- data/spec/pg/type_map_by_oid_spec.rb +0 -149
- data/spec/pg/type_map_in_ruby_spec.rb +0 -164
- data/spec/pg/type_map_spec.rb +0 -22
- data/spec/pg/type_spec.rb +0 -665
- data/spec/pg_spec.rb +0 -50
data/ext/pg_copy_coder.c
CHANGED
@@ -4,6 +4,7 @@
|
|
4
4
|
*/
|
5
5
|
|
6
6
|
#include "pg.h"
|
7
|
+
#include "pg_util.h"
|
7
8
|
|
8
9
|
#define ISOCTAL(c) (((c) >= '0') && ((c) <= '7'))
|
9
10
|
#define OCTVALUE(c) ((c) - '0')
|
@@ -21,21 +22,51 @@ typedef struct {
|
|
21
22
|
|
22
23
|
|
23
24
|
static void
|
24
|
-
pg_copycoder_mark(
|
25
|
+
pg_copycoder_mark( void *_this )
|
25
26
|
{
|
26
|
-
|
27
|
-
|
27
|
+
t_pg_copycoder *this = (t_pg_copycoder *)_this;
|
28
|
+
rb_gc_mark_movable(this->typemap);
|
29
|
+
rb_gc_mark_movable(this->null_string);
|
28
30
|
}
|
29
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
|
+
|
30
61
|
static VALUE
|
31
62
|
pg_copycoder_encoder_allocate( VALUE klass )
|
32
63
|
{
|
33
64
|
t_pg_copycoder *this;
|
34
|
-
VALUE self =
|
65
|
+
VALUE self = TypedData_Make_Struct( klass, t_pg_copycoder, &pg_copycoder_type, this );
|
35
66
|
pg_coder_init_encoder( self );
|
36
|
-
this->typemap
|
67
|
+
RB_OBJ_WRITE(self, &this->typemap, pg_typemap_all_strings);
|
37
68
|
this->delimiter = '\t';
|
38
|
-
this->null_string
|
69
|
+
RB_OBJ_WRITE(self, &this->null_string, rb_str_new_cstr("\\N"));
|
39
70
|
return self;
|
40
71
|
}
|
41
72
|
|
@@ -43,11 +74,11 @@ static VALUE
|
|
43
74
|
pg_copycoder_decoder_allocate( VALUE klass )
|
44
75
|
{
|
45
76
|
t_pg_copycoder *this;
|
46
|
-
VALUE self =
|
77
|
+
VALUE self = TypedData_Make_Struct( klass, t_pg_copycoder, &pg_copycoder_type, this );
|
47
78
|
pg_coder_init_decoder( self );
|
48
|
-
this->typemap
|
79
|
+
RB_OBJ_WRITE(self, &this->typemap, pg_typemap_all_strings);
|
49
80
|
this->delimiter = '\t';
|
50
|
-
this->null_string
|
81
|
+
RB_OBJ_WRITE(self, &this->null_string, rb_str_new_cstr("\\N"));
|
51
82
|
return self;
|
52
83
|
}
|
53
84
|
|
@@ -56,13 +87,16 @@ pg_copycoder_decoder_allocate( VALUE klass )
|
|
56
87
|
* coder.delimiter = String
|
57
88
|
*
|
58
89
|
* Specifies the character that separates columns within each row (line) of the file.
|
59
|
-
* The default is a tab character in text format
|
60
|
-
* This must be a single one-byte character.
|
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.
|
61
94
|
*/
|
62
95
|
static VALUE
|
63
96
|
pg_copycoder_delimiter_set(VALUE self, VALUE delimiter)
|
64
97
|
{
|
65
|
-
t_pg_copycoder *this =
|
98
|
+
t_pg_copycoder *this = RTYPEDDATA_DATA(self);
|
99
|
+
rb_check_frozen(self);
|
66
100
|
StringValue(delimiter);
|
67
101
|
if(RSTRING_LEN(delimiter) != 1)
|
68
102
|
rb_raise( rb_eArgError, "delimiter size must be one byte");
|
@@ -79,22 +113,24 @@ pg_copycoder_delimiter_set(VALUE self, VALUE delimiter)
|
|
79
113
|
static VALUE
|
80
114
|
pg_copycoder_delimiter_get(VALUE self)
|
81
115
|
{
|
82
|
-
t_pg_copycoder *this =
|
116
|
+
t_pg_copycoder *this = RTYPEDDATA_DATA(self);
|
83
117
|
return rb_str_new(&this->delimiter, 1);
|
84
118
|
}
|
85
119
|
|
86
120
|
/*
|
87
|
-
* Specifies the string that represents a null value.
|
88
|
-
*
|
89
|
-
* empty string even in text format for cases where you don't want to distinguish nulls
|
90
|
-
*
|
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.
|
91
126
|
*/
|
92
127
|
static VALUE
|
93
128
|
pg_copycoder_null_string_set(VALUE self, VALUE null_string)
|
94
129
|
{
|
95
|
-
t_pg_copycoder *this =
|
130
|
+
t_pg_copycoder *this = RTYPEDDATA_DATA(self);
|
131
|
+
rb_check_frozen(self);
|
96
132
|
StringValue(null_string);
|
97
|
-
this->null_string
|
133
|
+
RB_OBJ_WRITE(self, &this->null_string, null_string);
|
98
134
|
return null_string;
|
99
135
|
}
|
100
136
|
|
@@ -104,7 +140,7 @@ pg_copycoder_null_string_set(VALUE self, VALUE null_string)
|
|
104
140
|
static VALUE
|
105
141
|
pg_copycoder_null_string_get(VALUE self)
|
106
142
|
{
|
107
|
-
t_pg_copycoder *this =
|
143
|
+
t_pg_copycoder *this = RTYPEDDATA_DATA(self);
|
108
144
|
return this->null_string;
|
109
145
|
}
|
110
146
|
|
@@ -112,22 +148,24 @@ pg_copycoder_null_string_get(VALUE self)
|
|
112
148
|
* call-seq:
|
113
149
|
* coder.type_map = map
|
114
150
|
*
|
151
|
+
* Defines how single columns are encoded or decoded.
|
115
152
|
* +map+ must be a kind of PG::TypeMap .
|
116
153
|
*
|
117
154
|
* Defaults to a PG::TypeMapAllStrings , so that PG::TextEncoder::String respectively
|
118
|
-
* PG::TextDecoder::String is used for encoding/decoding of
|
155
|
+
* PG::TextDecoder::String is used for encoding/decoding of each column.
|
119
156
|
*
|
120
157
|
*/
|
121
158
|
static VALUE
|
122
159
|
pg_copycoder_type_map_set(VALUE self, VALUE type_map)
|
123
160
|
{
|
124
|
-
t_pg_copycoder *this =
|
161
|
+
t_pg_copycoder *this = RTYPEDDATA_DATA( self );
|
125
162
|
|
163
|
+
rb_check_frozen(self);
|
126
164
|
if ( !rb_obj_is_kind_of(type_map, rb_cTypeMap) ){
|
127
165
|
rb_raise( rb_eTypeError, "wrong elements type %s (expected some kind of PG::TypeMap)",
|
128
166
|
rb_obj_classname( type_map ) );
|
129
167
|
}
|
130
|
-
this->typemap
|
168
|
+
RB_OBJ_WRITE(self, &this->typemap, type_map);
|
131
169
|
|
132
170
|
return type_map;
|
133
171
|
}
|
@@ -136,11 +174,12 @@ pg_copycoder_type_map_set(VALUE self, VALUE type_map)
|
|
136
174
|
* call-seq:
|
137
175
|
* coder.type_map -> PG::TypeMap
|
138
176
|
*
|
177
|
+
* The PG::TypeMap that will be used for encoding and decoding of columns.
|
139
178
|
*/
|
140
179
|
static VALUE
|
141
180
|
pg_copycoder_type_map_get(VALUE self)
|
142
181
|
{
|
143
|
-
t_pg_copycoder *this =
|
182
|
+
t_pg_copycoder *this = RTYPEDDATA_DATA( self );
|
144
183
|
|
145
184
|
return this->typemap;
|
146
185
|
}
|
@@ -167,9 +206,15 @@ pg_copycoder_type_map_get(VALUE self)
|
|
167
206
|
* conn.put_copy_data ["string2", 42, true]
|
168
207
|
* end
|
169
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 .
|
170
215
|
*/
|
171
216
|
static int
|
172
|
-
pg_text_enc_copy_row(t_pg_coder *conv, VALUE value, char *out, VALUE *intermediate)
|
217
|
+
pg_text_enc_copy_row(t_pg_coder *conv, VALUE value, char *out, VALUE *intermediate, int enc_idx)
|
173
218
|
{
|
174
219
|
t_pg_copycoder *this = (t_pg_copycoder *)conv;
|
175
220
|
t_pg_coder_enc_func enc_func;
|
@@ -179,11 +224,12 @@ pg_text_enc_copy_row(t_pg_coder *conv, VALUE value, char *out, VALUE *intermedia
|
|
179
224
|
char *current_out;
|
180
225
|
char *end_capa_ptr;
|
181
226
|
|
182
|
-
p_typemap =
|
227
|
+
p_typemap = RTYPEDDATA_DATA( this->typemap );
|
183
228
|
p_typemap->funcs.fit_to_query( this->typemap, value );
|
184
229
|
|
185
230
|
/* Allocate a new string with embedded capacity and realloc exponential when needed. */
|
186
231
|
PG_RB_STR_NEW( *intermediate, current_out, end_capa_ptr );
|
232
|
+
PG_ENCODING_SET_NOCHECK(*intermediate, enc_idx);
|
187
233
|
|
188
234
|
for( i=0; i<RARRAY_LEN(value); i++){
|
189
235
|
char *ptr1;
|
@@ -211,11 +257,11 @@ pg_text_enc_copy_row(t_pg_coder *conv, VALUE value, char *out, VALUE *intermedia
|
|
211
257
|
enc_func = pg_coder_enc_func(p_elem_coder);
|
212
258
|
|
213
259
|
/* 1st pass for retiving the required memory space */
|
214
|
-
strlen = enc_func(p_elem_coder, entry, NULL, &subint);
|
260
|
+
strlen = enc_func(p_elem_coder, entry, NULL, &subint, enc_idx);
|
215
261
|
|
216
262
|
if( strlen == -1 ){
|
217
263
|
/* we can directly use String value in subint */
|
218
|
-
strlen =
|
264
|
+
strlen = RSTRING_LENINT(subint);
|
219
265
|
|
220
266
|
/* size of string assuming the worst case, that every character must be escaped. */
|
221
267
|
PG_RB_STR_ENSURE_CAPA( *intermediate, strlen * 2, current_out, end_capa_ptr );
|
@@ -234,7 +280,7 @@ pg_text_enc_copy_row(t_pg_coder *conv, VALUE value, char *out, VALUE *intermedia
|
|
234
280
|
PG_RB_STR_ENSURE_CAPA( *intermediate, strlen * 2, current_out, end_capa_ptr );
|
235
281
|
|
236
282
|
/* Place the unescaped string at current output position. */
|
237
|
-
strlen = enc_func(p_elem_coder, entry, current_out, &subint);
|
283
|
+
strlen = enc_func(p_elem_coder, entry, current_out, &subint, enc_idx);
|
238
284
|
|
239
285
|
ptr1 = current_out;
|
240
286
|
ptr2 = current_out + strlen;
|
@@ -271,6 +317,123 @@ pg_text_enc_copy_row(t_pg_coder *conv, VALUE value, char *out, VALUE *intermedia
|
|
271
317
|
}
|
272
318
|
|
273
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
|
+
|
274
437
|
/*
|
275
438
|
* Return decimal value for a hexadecimal digit
|
276
439
|
*/
|
@@ -301,15 +464,38 @@ GetDecimalFromHex(char hex)
|
|
301
464
|
* strings by PG::TextDecoder::String.
|
302
465
|
*
|
303
466
|
* Example with default type map ( TypeMapAllStrings ):
|
467
|
+
* conn.exec("CREATE TABLE my_table AS VALUES('astring', 7, FALSE), ('string2', 42, TRUE) ")
|
468
|
+
*
|
304
469
|
* deco = PG::TextDecoder::CopyRow.new
|
305
470
|
* conn.copy_data "COPY my_table TO STDOUT", deco do
|
306
471
|
* while row=conn.get_copy_data
|
307
472
|
* p row
|
308
473
|
* end
|
309
474
|
* end
|
310
|
-
* This prints all rows of +my_table+
|
475
|
+
* This prints all rows of +my_table+ :
|
311
476
|
* ["astring", "7", "f"]
|
312
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 .
|
313
499
|
*/
|
314
500
|
/*
|
315
501
|
* Parse the current line into separate attributes (fields),
|
@@ -324,7 +510,7 @@ GetDecimalFromHex(char hex)
|
|
324
510
|
* src/backend/commands/copy.c
|
325
511
|
*/
|
326
512
|
static VALUE
|
327
|
-
pg_text_dec_copy_row(t_pg_coder *conv, char *input_line, int len, int _tuple, int _field, int enc_idx)
|
513
|
+
pg_text_dec_copy_row(t_pg_coder *conv, const char *input_line, int len, int _tuple, int _field, int enc_idx)
|
328
514
|
{
|
329
515
|
t_pg_copycoder *this = (t_pg_copycoder *)conv;
|
330
516
|
|
@@ -338,12 +524,12 @@ pg_text_dec_copy_row(t_pg_coder *conv, char *input_line, int len, int _tuple, in
|
|
338
524
|
int fieldno;
|
339
525
|
int expected_fields;
|
340
526
|
char *output_ptr;
|
341
|
-
char *cur_ptr;
|
342
|
-
char *line_end_ptr;
|
527
|
+
const char *cur_ptr;
|
528
|
+
const char *line_end_ptr;
|
343
529
|
char *end_capa_ptr;
|
344
530
|
t_typemap *p_typemap;
|
345
531
|
|
346
|
-
p_typemap =
|
532
|
+
p_typemap = RTYPEDDATA_DATA( this->typemap );
|
347
533
|
expected_fields = p_typemap->funcs.fit_to_copy_get( this->typemap );
|
348
534
|
|
349
535
|
/* The received input string will probably have this->nfields fields. */
|
@@ -351,7 +537,7 @@ pg_text_dec_copy_row(t_pg_coder *conv, char *input_line, int len, int _tuple, in
|
|
351
537
|
|
352
538
|
/* Allocate a new string with embedded capacity and realloc later with
|
353
539
|
* exponential growing size when needed. */
|
354
|
-
|
540
|
+
PG_RB_STR_NEW( field_str, output_ptr, end_capa_ptr );
|
355
541
|
|
356
542
|
/* set pointer variables for loop */
|
357
543
|
cur_ptr = input_line;
|
@@ -362,9 +548,9 @@ pg_text_dec_copy_row(t_pg_coder *conv, char *input_line, int len, int _tuple, in
|
|
362
548
|
for (;;)
|
363
549
|
{
|
364
550
|
int found_delim = 0;
|
365
|
-
char *start_ptr;
|
366
|
-
char *end_ptr;
|
367
|
-
|
551
|
+
const char *start_ptr;
|
552
|
+
const char *end_ptr;
|
553
|
+
long input_len;
|
368
554
|
|
369
555
|
/* Remember start of field on input side */
|
370
556
|
start_ptr = cur_ptr;
|
@@ -513,7 +699,7 @@ pg_text_dec_copy_row(t_pg_coder *conv, char *input_line, int len, int _tuple, in
|
|
513
699
|
if( field_value == field_str ){
|
514
700
|
/* Our output string will be send to the user, so we can not reuse
|
515
701
|
* it for the next field. */
|
516
|
-
|
702
|
+
PG_RB_STR_NEW( field_str, output_ptr, end_capa_ptr );
|
517
703
|
}
|
518
704
|
}
|
519
705
|
/* Reset the pointer to the start of the output/buffer string. */
|
@@ -529,9 +715,168 @@ pg_text_dec_copy_row(t_pg_coder *conv, char *input_line, int len, int _tuple, in
|
|
529
715
|
}
|
530
716
|
|
531
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
|
+
|
532
876
|
void
|
533
|
-
init_pg_copycoder()
|
877
|
+
init_pg_copycoder(void)
|
534
878
|
{
|
879
|
+
VALUE coder;
|
535
880
|
/* Document-class: PG::CopyCoder < PG::Coder
|
536
881
|
*
|
537
882
|
* This is the base class for all type cast classes for COPY data,
|
@@ -554,8 +899,19 @@ init_pg_copycoder()
|
|
554
899
|
/* Make RDoc aware of the encoder classes... */
|
555
900
|
/* rb_mPG_TextEncoder = rb_define_module_under( rb_mPG, "TextEncoder" ); */
|
556
901
|
/* dummy = rb_define_class_under( rb_mPG_TextEncoder, "CopyRow", rb_cPG_CopyEncoder ); */
|
557
|
-
pg_define_coder( "CopyRow", pg_text_enc_copy_row, rb_cPG_CopyEncoder, rb_mPG_TextEncoder );
|
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
|
+
|
558
908
|
/* rb_mPG_TextDecoder = rb_define_module_under( rb_mPG, "TextDecoder" ); */
|
559
909
|
/* dummy = rb_define_class_under( rb_mPG_TextDecoder, "CopyRow", rb_cPG_CopyDecoder ); */
|
560
|
-
pg_define_coder( "CopyRow", pg_text_dec_copy_row, rb_cPG_CopyDecoder, rb_mPG_TextDecoder );
|
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 );
|
561
917
|
}
|