pg 1.3.0.rc2-x64-mingw-ucrt
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- checksums.yaml.gz.sig +3 -0
- data/.appveyor.yml +36 -0
- data/.gems +6 -0
- data/.gemtest +0 -0
- data/.github/workflows/binary-gems.yml +85 -0
- data/.github/workflows/source-gem.yml +130 -0
- data/.gitignore +13 -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 +22 -0
- data/Contributors.rdoc +46 -0
- data/Gemfile +14 -0
- data/History.rdoc +648 -0
- data/LICENSE +56 -0
- data/Manifest.txt +72 -0
- data/POSTGRES +23 -0
- data/README-OS_X.rdoc +68 -0
- data/README-Windows.rdoc +56 -0
- data/README.ja.rdoc +13 -0
- data/README.rdoc +214 -0
- data/Rakefile +106 -0
- data/Rakefile.cross +300 -0
- data/certs/ged.pem +24 -0
- data/ext/errorcodes.def +1040 -0
- data/ext/errorcodes.rb +45 -0
- data/ext/errorcodes.txt +496 -0
- data/ext/extconf.rb +165 -0
- data/ext/gvl_wrappers.c +21 -0
- data/ext/gvl_wrappers.h +264 -0
- data/ext/pg.c +732 -0
- data/ext/pg.h +385 -0
- data/ext/pg_binary_decoder.c +229 -0
- data/ext/pg_binary_encoder.c +163 -0
- data/ext/pg_coder.c +615 -0
- data/ext/pg_connection.c +4415 -0
- data/ext/pg_copy_coder.c +628 -0
- data/ext/pg_errors.c +95 -0
- data/ext/pg_record_coder.c +519 -0
- data/ext/pg_result.c +1683 -0
- data/ext/pg_text_decoder.c +987 -0
- data/ext/pg_text_encoder.c +814 -0
- data/ext/pg_tuple.c +575 -0
- data/ext/pg_type_map.c +199 -0
- data/ext/pg_type_map_all_strings.c +129 -0
- data/ext/pg_type_map_by_class.c +269 -0
- data/ext/pg_type_map_by_column.c +349 -0
- data/ext/pg_type_map_by_mri_type.c +313 -0
- data/ext/pg_type_map_by_oid.c +385 -0
- data/ext/pg_type_map_in_ruby.c +330 -0
- data/ext/pg_util.c +149 -0
- data/ext/pg_util.h +65 -0
- data/ext/vc/pg.sln +26 -0
- data/ext/vc/pg_18/pg.vcproj +216 -0
- data/ext/vc/pg_19/pg_19.vcproj +209 -0
- data/lib/3.1/pg_ext.so +0 -0
- data/lib/pg/basic_type_map_based_on_result.rb +47 -0
- data/lib/pg/basic_type_map_for_queries.rb +193 -0
- data/lib/pg/basic_type_map_for_results.rb +81 -0
- data/lib/pg/basic_type_registry.rb +296 -0
- data/lib/pg/binary_decoder.rb +23 -0
- data/lib/pg/coder.rb +104 -0
- data/lib/pg/connection.rb +813 -0
- data/lib/pg/constants.rb +12 -0
- data/lib/pg/exceptions.rb +12 -0
- data/lib/pg/result.rb +43 -0
- data/lib/pg/text_decoder.rb +46 -0
- data/lib/pg/text_encoder.rb +59 -0
- data/lib/pg/tuple.rb +30 -0
- data/lib/pg/type_map_by_column.rb +16 -0
- data/lib/pg/version.rb +4 -0
- data/lib/pg.rb +87 -0
- data/lib/x64-mingw-ucrt/libpq.dll +0 -0
- 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 +32 -0
- data/sample/array_insert.rb +20 -0
- data/sample/async_api.rb +106 -0
- data/sample/async_copyto.rb +39 -0
- data/sample/async_mixed.rb +56 -0
- data/sample/check_conn.rb +21 -0
- data/sample/copydata.rb +71 -0
- data/sample/copyfrom.rb +81 -0
- data/sample/copyto.rb +19 -0
- data/sample/cursor.rb +21 -0
- data/sample/disk_usage_report.rb +177 -0
- data/sample/issue-119.rb +94 -0
- data/sample/losample.rb +69 -0
- data/sample/minimal-testcase.rb +17 -0
- data/sample/notify_wait.rb +72 -0
- data/sample/pg_statistics.rb +285 -0
- data/sample/replication_monitor.rb +222 -0
- data/sample/test_binary_values.rb +33 -0
- data/sample/wal_shipper.rb +434 -0
- data/sample/warehouse_partitions.rb +311 -0
- data.tar.gz.sig +0 -0
- metadata +188 -0
- metadata.gz.sig +0 -0
data/ext/pg_tuple.c
ADDED
@@ -0,0 +1,575 @@
|
|
1
|
+
#include "pg.h"
|
2
|
+
|
3
|
+
/********************************************************************
|
4
|
+
*
|
5
|
+
* Document-class: PG::Tuple
|
6
|
+
*
|
7
|
+
* The class to represent one query result tuple (row).
|
8
|
+
* An instance of this class can be created by PG::Result#tuple .
|
9
|
+
*
|
10
|
+
* All field values of the tuple are retrieved on demand from the underlying PGresult object and converted to a Ruby object.
|
11
|
+
* Subsequent access to the same field returns the same object, since they are cached when materialized.
|
12
|
+
* Each PG::Tuple holds a reference to the related PG::Result object, but gets detached, when all fields are materialized.
|
13
|
+
*
|
14
|
+
* Example:
|
15
|
+
* require 'pg'
|
16
|
+
* conn = PG.connect(:dbname => 'test')
|
17
|
+
* res = conn.exec('VALUES(1,2), (3,4)')
|
18
|
+
* t0 = res.tuple(0) # => #<PG::Tuple column1: "1", column2: "2">
|
19
|
+
* t1 = res.tuple(1) # => #<PG::Tuple column1: "3", column2: "4">
|
20
|
+
* t1[0] # => "3"
|
21
|
+
* t1["column2"] # => "4"
|
22
|
+
*/
|
23
|
+
|
24
|
+
static VALUE rb_cPG_Tuple;
|
25
|
+
|
26
|
+
typedef struct {
|
27
|
+
/* PG::Result object this tuple was retrieved from.
|
28
|
+
* Qnil when all fields are materialized.
|
29
|
+
*/
|
30
|
+
VALUE result;
|
31
|
+
|
32
|
+
/* Store the typemap of the result.
|
33
|
+
* It's not enough to reference the PG::TypeMap object through the result,
|
34
|
+
* since it could be exchanged after the tuple has been created.
|
35
|
+
*/
|
36
|
+
VALUE typemap;
|
37
|
+
|
38
|
+
/* Hash with maps field names to index into values[]
|
39
|
+
* Shared between all instances retrieved from one PG::Result.
|
40
|
+
*/
|
41
|
+
VALUE field_map;
|
42
|
+
|
43
|
+
/* Row number within the result set. */
|
44
|
+
int row_num;
|
45
|
+
|
46
|
+
/* Number of fields in the result set. */
|
47
|
+
int num_fields;
|
48
|
+
|
49
|
+
/* Materialized values.
|
50
|
+
* And in case of dup column names, a field_names Array subsequently.
|
51
|
+
*/
|
52
|
+
VALUE values[0];
|
53
|
+
} t_pg_tuple;
|
54
|
+
|
55
|
+
static inline VALUE *
|
56
|
+
pg_tuple_get_field_names_ptr( t_pg_tuple *this )
|
57
|
+
{
|
58
|
+
if( this->num_fields != (int)RHASH_SIZE(this->field_map) ){
|
59
|
+
return &this->values[this->num_fields];
|
60
|
+
} else {
|
61
|
+
static VALUE f = Qfalse;
|
62
|
+
return &f;
|
63
|
+
}
|
64
|
+
}
|
65
|
+
|
66
|
+
static inline VALUE
|
67
|
+
pg_tuple_get_field_names( t_pg_tuple *this )
|
68
|
+
{
|
69
|
+
return *pg_tuple_get_field_names_ptr(this);
|
70
|
+
}
|
71
|
+
|
72
|
+
static void
|
73
|
+
pg_tuple_gc_mark( void *_this )
|
74
|
+
{
|
75
|
+
t_pg_tuple *this = (t_pg_tuple *)_this;
|
76
|
+
int i;
|
77
|
+
|
78
|
+
if( !this ) return;
|
79
|
+
rb_gc_mark_movable( this->result );
|
80
|
+
rb_gc_mark_movable( this->typemap );
|
81
|
+
rb_gc_mark_movable( this->field_map );
|
82
|
+
|
83
|
+
for( i = 0; i < this->num_fields; i++ ){
|
84
|
+
rb_gc_mark_movable( this->values[i] );
|
85
|
+
}
|
86
|
+
rb_gc_mark_movable( pg_tuple_get_field_names(this) );
|
87
|
+
}
|
88
|
+
|
89
|
+
static void
|
90
|
+
pg_tuple_gc_compact( void *_this )
|
91
|
+
{
|
92
|
+
t_pg_tuple *this = (t_pg_tuple *)_this;
|
93
|
+
int i;
|
94
|
+
|
95
|
+
if( !this ) return;
|
96
|
+
pg_gc_location( this->result );
|
97
|
+
pg_gc_location( this->typemap );
|
98
|
+
pg_gc_location( this->field_map );
|
99
|
+
|
100
|
+
for( i = 0; i < this->num_fields; i++ ){
|
101
|
+
pg_gc_location( this->values[i] );
|
102
|
+
}
|
103
|
+
pg_gc_location( *pg_tuple_get_field_names_ptr(this) );
|
104
|
+
}
|
105
|
+
|
106
|
+
static void
|
107
|
+
pg_tuple_gc_free( void *_this )
|
108
|
+
{
|
109
|
+
t_pg_tuple *this = (t_pg_tuple *)_this;
|
110
|
+
if( !this ) return;
|
111
|
+
xfree(this);
|
112
|
+
}
|
113
|
+
|
114
|
+
static size_t
|
115
|
+
pg_tuple_memsize( const void *_this )
|
116
|
+
{
|
117
|
+
const t_pg_tuple *this = (const t_pg_tuple *)_this;
|
118
|
+
if( this==NULL ) return 0;
|
119
|
+
return sizeof(*this) + sizeof(*this->values) * this->num_fields;
|
120
|
+
}
|
121
|
+
|
122
|
+
static const rb_data_type_t pg_tuple_type = {
|
123
|
+
"PG::Tuple",
|
124
|
+
{
|
125
|
+
pg_tuple_gc_mark,
|
126
|
+
pg_tuple_gc_free,
|
127
|
+
pg_tuple_memsize,
|
128
|
+
pg_compact_callback(pg_tuple_gc_compact),
|
129
|
+
},
|
130
|
+
0, 0,
|
131
|
+
RUBY_TYPED_FREE_IMMEDIATELY,
|
132
|
+
};
|
133
|
+
|
134
|
+
/*
|
135
|
+
* Document-method: allocate
|
136
|
+
*
|
137
|
+
* call-seq:
|
138
|
+
* PG::VeryTuple.allocate -> obj
|
139
|
+
*/
|
140
|
+
static VALUE
|
141
|
+
pg_tuple_s_allocate( VALUE klass )
|
142
|
+
{
|
143
|
+
return TypedData_Wrap_Struct( klass, &pg_tuple_type, NULL );
|
144
|
+
}
|
145
|
+
|
146
|
+
VALUE
|
147
|
+
pg_tuple_new(VALUE result, int row_num)
|
148
|
+
{
|
149
|
+
t_pg_tuple *this;
|
150
|
+
VALUE self = pg_tuple_s_allocate( rb_cPG_Tuple );
|
151
|
+
t_pg_result *p_result = pgresult_get_this(result);
|
152
|
+
int num_fields = p_result->nfields;
|
153
|
+
int i;
|
154
|
+
VALUE field_map = p_result->field_map;
|
155
|
+
int dup_names = num_fields != (int)RHASH_SIZE(field_map);
|
156
|
+
|
157
|
+
this = (t_pg_tuple *)xmalloc(
|
158
|
+
sizeof(*this) +
|
159
|
+
sizeof(*this->values) * num_fields +
|
160
|
+
sizeof(*this->values) * (dup_names ? 1 : 0));
|
161
|
+
|
162
|
+
this->result = result;
|
163
|
+
this->typemap = p_result->typemap;
|
164
|
+
this->field_map = field_map;
|
165
|
+
this->row_num = row_num;
|
166
|
+
this->num_fields = num_fields;
|
167
|
+
|
168
|
+
for( i = 0; i < num_fields; i++ ){
|
169
|
+
this->values[i] = Qundef;
|
170
|
+
}
|
171
|
+
|
172
|
+
if( dup_names ){
|
173
|
+
/* Some of the column names are duplicated -> we need the keys as Array in addition.
|
174
|
+
* Store it behind the values to save the space in the common case of no dups.
|
175
|
+
*/
|
176
|
+
this->values[num_fields] = rb_obj_freeze(rb_ary_new4(num_fields, p_result->fnames));
|
177
|
+
}
|
178
|
+
|
179
|
+
RTYPEDDATA_DATA(self) = this;
|
180
|
+
|
181
|
+
return self;
|
182
|
+
}
|
183
|
+
|
184
|
+
static inline t_pg_tuple *
|
185
|
+
pg_tuple_get_this( VALUE self )
|
186
|
+
{
|
187
|
+
t_pg_tuple *this;
|
188
|
+
TypedData_Get_Struct(self, t_pg_tuple, &pg_tuple_type, this);
|
189
|
+
if (this == NULL)
|
190
|
+
rb_raise(rb_eTypeError, "tuple is empty");
|
191
|
+
|
192
|
+
return this;
|
193
|
+
}
|
194
|
+
|
195
|
+
static VALUE
|
196
|
+
pg_tuple_materialize_field(t_pg_tuple *this, int col)
|
197
|
+
{
|
198
|
+
VALUE value = this->values[col];
|
199
|
+
|
200
|
+
if( value == Qundef ){
|
201
|
+
t_typemap *p_typemap = RTYPEDDATA_DATA( this->typemap );
|
202
|
+
|
203
|
+
pgresult_get(this->result); /* make sure we have a valid PGresult object */
|
204
|
+
value = p_typemap->funcs.typecast_result_value(p_typemap, this->result, this->row_num, col);
|
205
|
+
this->values[col] = value;
|
206
|
+
}
|
207
|
+
|
208
|
+
return value;
|
209
|
+
}
|
210
|
+
|
211
|
+
static void
|
212
|
+
pg_tuple_detach(t_pg_tuple *this)
|
213
|
+
{
|
214
|
+
this->result = Qnil;
|
215
|
+
this->typemap = Qnil;
|
216
|
+
this->row_num = -1;
|
217
|
+
}
|
218
|
+
|
219
|
+
static void
|
220
|
+
pg_tuple_materialize(t_pg_tuple *this)
|
221
|
+
{
|
222
|
+
int field_num;
|
223
|
+
for(field_num = 0; field_num < this->num_fields; field_num++) {
|
224
|
+
pg_tuple_materialize_field(this, field_num);
|
225
|
+
}
|
226
|
+
|
227
|
+
pg_tuple_detach(this);
|
228
|
+
}
|
229
|
+
|
230
|
+
/*
|
231
|
+
* call-seq:
|
232
|
+
* tup.fetch(key) → value
|
233
|
+
* tup.fetch(key, default) → value
|
234
|
+
* tup.fetch(key) { |key| block } → value
|
235
|
+
*
|
236
|
+
* Returns a field value by either column index or column name.
|
237
|
+
*
|
238
|
+
* An integer +key+ is interpreted as column index.
|
239
|
+
* Negative values of index count from the end of the array.
|
240
|
+
*
|
241
|
+
* Depending on Result#field_name_type= a string or symbol +key+ is interpreted as column name.
|
242
|
+
*
|
243
|
+
* If the key can't be found, there are several options:
|
244
|
+
* With no other arguments, it will raise a IndexError exception;
|
245
|
+
* if default is given, then that will be returned;
|
246
|
+
* if the optional code block is specified, then that will be run and its result returned.
|
247
|
+
*/
|
248
|
+
static VALUE
|
249
|
+
pg_tuple_fetch(int argc, VALUE *argv, VALUE self)
|
250
|
+
{
|
251
|
+
VALUE key;
|
252
|
+
long block_given;
|
253
|
+
VALUE index;
|
254
|
+
int field_num;
|
255
|
+
t_pg_tuple *this = pg_tuple_get_this(self);
|
256
|
+
|
257
|
+
rb_check_arity(argc, 1, 2);
|
258
|
+
key = argv[0];
|
259
|
+
|
260
|
+
block_given = rb_block_given_p();
|
261
|
+
if (block_given && argc == 2) {
|
262
|
+
rb_warn("block supersedes default value argument");
|
263
|
+
}
|
264
|
+
|
265
|
+
switch(rb_type(key)){
|
266
|
+
case T_FIXNUM:
|
267
|
+
case T_BIGNUM:
|
268
|
+
field_num = NUM2INT(key);
|
269
|
+
if ( field_num < 0 )
|
270
|
+
field_num = this->num_fields + field_num;
|
271
|
+
if ( field_num < 0 || field_num >= this->num_fields ){
|
272
|
+
if (block_given) return rb_yield(key);
|
273
|
+
if (argc == 1) rb_raise( rb_eIndexError, "Index %d is out of range", field_num );
|
274
|
+
return argv[1];
|
275
|
+
}
|
276
|
+
break;
|
277
|
+
default:
|
278
|
+
index = rb_hash_aref(this->field_map, key);
|
279
|
+
|
280
|
+
if (index == Qnil) {
|
281
|
+
if (block_given) return rb_yield(key);
|
282
|
+
if (argc == 1) rb_raise( rb_eKeyError, "column not found" );
|
283
|
+
return argv[1];
|
284
|
+
}
|
285
|
+
|
286
|
+
field_num = NUM2INT(index);
|
287
|
+
}
|
288
|
+
|
289
|
+
return pg_tuple_materialize_field(this, field_num);
|
290
|
+
}
|
291
|
+
|
292
|
+
/*
|
293
|
+
* call-seq:
|
294
|
+
* tup[ key ] -> value
|
295
|
+
*
|
296
|
+
* Returns a field value by either column index or column name.
|
297
|
+
*
|
298
|
+
* An integer +key+ is interpreted as column index.
|
299
|
+
* Negative values of index count from the end of the array.
|
300
|
+
*
|
301
|
+
* Depending on Result#field_name_type= a string or symbol +key+ is interpreted as column name.
|
302
|
+
*
|
303
|
+
* If the key can't be found, it returns +nil+ .
|
304
|
+
*/
|
305
|
+
static VALUE
|
306
|
+
pg_tuple_aref(VALUE self, VALUE key)
|
307
|
+
{
|
308
|
+
VALUE index;
|
309
|
+
int field_num;
|
310
|
+
t_pg_tuple *this = pg_tuple_get_this(self);
|
311
|
+
|
312
|
+
switch(rb_type(key)){
|
313
|
+
case T_FIXNUM:
|
314
|
+
case T_BIGNUM:
|
315
|
+
field_num = NUM2INT(key);
|
316
|
+
if ( field_num < 0 )
|
317
|
+
field_num = this->num_fields + field_num;
|
318
|
+
if ( field_num < 0 || field_num >= this->num_fields )
|
319
|
+
return Qnil;
|
320
|
+
break;
|
321
|
+
default:
|
322
|
+
index = rb_hash_aref(this->field_map, key);
|
323
|
+
if( index == Qnil ) return Qnil;
|
324
|
+
field_num = NUM2INT(index);
|
325
|
+
}
|
326
|
+
|
327
|
+
return pg_tuple_materialize_field(this, field_num);
|
328
|
+
}
|
329
|
+
|
330
|
+
static VALUE
|
331
|
+
pg_tuple_num_fields_for_enum(VALUE self, VALUE args, VALUE eobj)
|
332
|
+
{
|
333
|
+
t_pg_tuple *this = pg_tuple_get_this(self);
|
334
|
+
return INT2NUM(this->num_fields);
|
335
|
+
}
|
336
|
+
|
337
|
+
static int
|
338
|
+
pg_tuple_yield_key_value(VALUE key, VALUE index, VALUE _this)
|
339
|
+
{
|
340
|
+
t_pg_tuple *this = (t_pg_tuple *)_this;
|
341
|
+
VALUE value = pg_tuple_materialize_field(this, NUM2INT(index));
|
342
|
+
rb_yield_values(2, key, value);
|
343
|
+
return ST_CONTINUE;
|
344
|
+
}
|
345
|
+
|
346
|
+
/*
|
347
|
+
* call-seq:
|
348
|
+
* tup.each{ |key, value| ... }
|
349
|
+
*
|
350
|
+
* Invokes block for each field name and value in the tuple.
|
351
|
+
*/
|
352
|
+
static VALUE
|
353
|
+
pg_tuple_each(VALUE self)
|
354
|
+
{
|
355
|
+
t_pg_tuple *this = pg_tuple_get_this(self);
|
356
|
+
VALUE field_names;
|
357
|
+
|
358
|
+
RETURN_SIZED_ENUMERATOR(self, 0, NULL, pg_tuple_num_fields_for_enum);
|
359
|
+
|
360
|
+
field_names = pg_tuple_get_field_names(this);
|
361
|
+
|
362
|
+
if( field_names == Qfalse ){
|
363
|
+
rb_hash_foreach(this->field_map, pg_tuple_yield_key_value, (VALUE)this);
|
364
|
+
} else {
|
365
|
+
int i;
|
366
|
+
for( i = 0; i < this->num_fields; i++ ){
|
367
|
+
VALUE value = pg_tuple_materialize_field(this, i);
|
368
|
+
rb_yield_values(2, RARRAY_AREF(field_names, i), value);
|
369
|
+
}
|
370
|
+
}
|
371
|
+
|
372
|
+
pg_tuple_detach(this);
|
373
|
+
return self;
|
374
|
+
}
|
375
|
+
|
376
|
+
/*
|
377
|
+
* call-seq:
|
378
|
+
* tup.each_value{ |value| ... }
|
379
|
+
*
|
380
|
+
* Invokes block for each field value in the tuple.
|
381
|
+
*/
|
382
|
+
static VALUE
|
383
|
+
pg_tuple_each_value(VALUE self)
|
384
|
+
{
|
385
|
+
t_pg_tuple *this = pg_tuple_get_this(self);
|
386
|
+
int field_num;
|
387
|
+
|
388
|
+
RETURN_SIZED_ENUMERATOR(self, 0, NULL, pg_tuple_num_fields_for_enum);
|
389
|
+
|
390
|
+
for(field_num = 0; field_num < this->num_fields; field_num++) {
|
391
|
+
VALUE value = pg_tuple_materialize_field(this, field_num);
|
392
|
+
rb_yield(value);
|
393
|
+
}
|
394
|
+
|
395
|
+
pg_tuple_detach(this);
|
396
|
+
return self;
|
397
|
+
}
|
398
|
+
|
399
|
+
|
400
|
+
/*
|
401
|
+
* call-seq:
|
402
|
+
* tup.values -> Array
|
403
|
+
*
|
404
|
+
* Returns the values of this tuple as Array.
|
405
|
+
* +res.tuple(i).values+ is equal to +res.tuple_values(i)+ .
|
406
|
+
*/
|
407
|
+
static VALUE
|
408
|
+
pg_tuple_values(VALUE self)
|
409
|
+
{
|
410
|
+
t_pg_tuple *this = pg_tuple_get_this(self);
|
411
|
+
|
412
|
+
pg_tuple_materialize(this);
|
413
|
+
return rb_ary_new4(this->num_fields, &this->values[0]);
|
414
|
+
}
|
415
|
+
|
416
|
+
static VALUE
|
417
|
+
pg_tuple_field_map(VALUE self)
|
418
|
+
{
|
419
|
+
t_pg_tuple *this = pg_tuple_get_this(self);
|
420
|
+
return this->field_map;
|
421
|
+
}
|
422
|
+
|
423
|
+
static VALUE
|
424
|
+
pg_tuple_field_names(VALUE self)
|
425
|
+
{
|
426
|
+
t_pg_tuple *this = pg_tuple_get_this(self);
|
427
|
+
return pg_tuple_get_field_names(this);
|
428
|
+
}
|
429
|
+
|
430
|
+
/*
|
431
|
+
* call-seq:
|
432
|
+
* tup.length → integer
|
433
|
+
*
|
434
|
+
* Returns number of fields of this tuple.
|
435
|
+
*/
|
436
|
+
static VALUE
|
437
|
+
pg_tuple_length(VALUE self)
|
438
|
+
{
|
439
|
+
t_pg_tuple *this = pg_tuple_get_this(self);
|
440
|
+
return INT2NUM(this->num_fields);
|
441
|
+
}
|
442
|
+
|
443
|
+
/*
|
444
|
+
* call-seq:
|
445
|
+
* tup.index(key) → integer
|
446
|
+
*
|
447
|
+
* Returns the field number which matches the given column name.
|
448
|
+
*/
|
449
|
+
static VALUE
|
450
|
+
pg_tuple_index(VALUE self, VALUE key)
|
451
|
+
{
|
452
|
+
t_pg_tuple *this = pg_tuple_get_this(self);
|
453
|
+
return rb_hash_aref(this->field_map, key);
|
454
|
+
}
|
455
|
+
|
456
|
+
|
457
|
+
static VALUE
|
458
|
+
pg_tuple_dump(VALUE self)
|
459
|
+
{
|
460
|
+
VALUE field_names;
|
461
|
+
VALUE values;
|
462
|
+
VALUE a;
|
463
|
+
t_pg_tuple *this = pg_tuple_get_this(self);
|
464
|
+
|
465
|
+
pg_tuple_materialize(this);
|
466
|
+
|
467
|
+
field_names = pg_tuple_get_field_names(this);
|
468
|
+
if( field_names == Qfalse )
|
469
|
+
field_names = rb_funcall(this->field_map, rb_intern("keys"), 0);
|
470
|
+
|
471
|
+
values = rb_ary_new4(this->num_fields, &this->values[0]);
|
472
|
+
a = rb_ary_new3(2, field_names, values);
|
473
|
+
|
474
|
+
if (FL_TEST(self, FL_EXIVAR)) {
|
475
|
+
rb_copy_generic_ivar(a, self);
|
476
|
+
FL_SET(a, FL_EXIVAR);
|
477
|
+
}
|
478
|
+
|
479
|
+
return a;
|
480
|
+
}
|
481
|
+
|
482
|
+
static VALUE
|
483
|
+
pg_tuple_load(VALUE self, VALUE a)
|
484
|
+
{
|
485
|
+
int num_fields;
|
486
|
+
int i;
|
487
|
+
t_pg_tuple *this;
|
488
|
+
VALUE values;
|
489
|
+
VALUE field_names;
|
490
|
+
VALUE field_map;
|
491
|
+
int dup_names;
|
492
|
+
|
493
|
+
rb_check_frozen(self);
|
494
|
+
|
495
|
+
TypedData_Get_Struct(self, t_pg_tuple, &pg_tuple_type, this);
|
496
|
+
if (this)
|
497
|
+
rb_raise(rb_eTypeError, "tuple is not empty");
|
498
|
+
|
499
|
+
Check_Type(a, T_ARRAY);
|
500
|
+
if (RARRAY_LEN(a) != 2)
|
501
|
+
rb_raise(rb_eTypeError, "expected an array of 2 elements");
|
502
|
+
|
503
|
+
field_names = RARRAY_AREF(a, 0);
|
504
|
+
Check_Type(field_names, T_ARRAY);
|
505
|
+
rb_obj_freeze(field_names);
|
506
|
+
values = RARRAY_AREF(a, 1);
|
507
|
+
Check_Type(values, T_ARRAY);
|
508
|
+
num_fields = RARRAY_LENINT(values);
|
509
|
+
|
510
|
+
if (RARRAY_LENINT(field_names) != num_fields)
|
511
|
+
rb_raise(rb_eTypeError, "different number of fields and values");
|
512
|
+
|
513
|
+
field_map = rb_hash_new();
|
514
|
+
for( i = 0; i < num_fields; i++ ){
|
515
|
+
rb_hash_aset(field_map, RARRAY_AREF(field_names, i), INT2FIX(i));
|
516
|
+
}
|
517
|
+
rb_obj_freeze(field_map);
|
518
|
+
|
519
|
+
dup_names = num_fields != (int)RHASH_SIZE(field_map);
|
520
|
+
|
521
|
+
this = (t_pg_tuple *)xmalloc(
|
522
|
+
sizeof(*this) +
|
523
|
+
sizeof(*this->values) * num_fields +
|
524
|
+
sizeof(*this->values) * (dup_names ? 1 : 0));
|
525
|
+
|
526
|
+
this->result = Qnil;
|
527
|
+
this->typemap = Qnil;
|
528
|
+
this->row_num = -1;
|
529
|
+
this->num_fields = num_fields;
|
530
|
+
this->field_map = field_map;
|
531
|
+
|
532
|
+
for( i = 0; i < num_fields; i++ ){
|
533
|
+
VALUE v = RARRAY_AREF(values, i);
|
534
|
+
if( v == Qundef )
|
535
|
+
rb_raise(rb_eTypeError, "field %d is not materialized", i);
|
536
|
+
this->values[i] = v;
|
537
|
+
}
|
538
|
+
|
539
|
+
if( dup_names ){
|
540
|
+
this->values[num_fields] = field_names;
|
541
|
+
}
|
542
|
+
|
543
|
+
RTYPEDDATA_DATA(self) = this;
|
544
|
+
|
545
|
+
if (FL_TEST(a, FL_EXIVAR)) {
|
546
|
+
rb_copy_generic_ivar(self, a);
|
547
|
+
FL_SET(self, FL_EXIVAR);
|
548
|
+
}
|
549
|
+
|
550
|
+
return self;
|
551
|
+
}
|
552
|
+
|
553
|
+
void
|
554
|
+
init_pg_tuple()
|
555
|
+
{
|
556
|
+
rb_cPG_Tuple = rb_define_class_under( rb_mPG, "Tuple", rb_cObject );
|
557
|
+
rb_define_alloc_func( rb_cPG_Tuple, pg_tuple_s_allocate );
|
558
|
+
rb_include_module(rb_cPG_Tuple, rb_mEnumerable);
|
559
|
+
|
560
|
+
rb_define_method(rb_cPG_Tuple, "fetch", pg_tuple_fetch, -1);
|
561
|
+
rb_define_method(rb_cPG_Tuple, "[]", pg_tuple_aref, 1);
|
562
|
+
rb_define_method(rb_cPG_Tuple, "each", pg_tuple_each, 0);
|
563
|
+
rb_define_method(rb_cPG_Tuple, "each_value", pg_tuple_each_value, 0);
|
564
|
+
rb_define_method(rb_cPG_Tuple, "values", pg_tuple_values, 0);
|
565
|
+
rb_define_method(rb_cPG_Tuple, "length", pg_tuple_length, 0);
|
566
|
+
rb_define_alias(rb_cPG_Tuple, "size", "length");
|
567
|
+
rb_define_method(rb_cPG_Tuple, "index", pg_tuple_index, 1);
|
568
|
+
|
569
|
+
rb_define_private_method(rb_cPG_Tuple, "field_map", pg_tuple_field_map, 0);
|
570
|
+
rb_define_private_method(rb_cPG_Tuple, "field_names", pg_tuple_field_names, 0);
|
571
|
+
|
572
|
+
/* methods for marshaling */
|
573
|
+
rb_define_private_method(rb_cPG_Tuple, "marshal_dump", pg_tuple_dump, 0);
|
574
|
+
rb_define_private_method(rb_cPG_Tuple, "marshal_load", pg_tuple_load, 1);
|
575
|
+
}
|