pg 1.0.0 → 1.1.0.pre20180730144600
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +0 -0
- data/ChangeLog +0 -6595
- data/History.rdoc +52 -0
- data/README.rdoc +11 -0
- data/Rakefile +1 -1
- data/Rakefile.cross +1 -1
- data/ext/errorcodes.rb +1 -1
- data/ext/extconf.rb +2 -0
- data/ext/pg.c +3 -2
- data/ext/pg.h +33 -5
- data/ext/pg_binary_decoder.c +69 -6
- data/ext/pg_binary_encoder.c +1 -1
- data/ext/pg_coder.c +52 -3
- data/ext/pg_connection.c +290 -103
- data/ext/pg_copy_coder.c +10 -5
- data/ext/pg_result.c +339 -113
- data/ext/pg_text_decoder.c +597 -37
- data/ext/pg_text_encoder.c +1 -1
- data/ext/pg_tuple.c +540 -0
- data/ext/pg_type_map.c +1 -1
- data/ext/pg_type_map_all_strings.c +1 -1
- data/ext/pg_type_map_by_class.c +1 -1
- data/ext/pg_type_map_by_column.c +1 -1
- data/ext/pg_type_map_by_mri_type.c +1 -1
- data/ext/pg_type_map_by_oid.c +1 -1
- data/ext/pg_type_map_in_ruby.c +1 -1
- data/ext/util.c +6 -6
- data/ext/util.h +2 -2
- data/lib/pg.rb +5 -3
- data/lib/pg/basic_type_mapping.rb +40 -7
- data/lib/pg/coder.rb +1 -1
- data/lib/pg/connection.rb +20 -1
- data/lib/pg/constants.rb +1 -1
- data/lib/pg/exceptions.rb +1 -1
- data/lib/pg/result.rb +1 -1
- data/lib/pg/text_decoder.rb +19 -23
- data/lib/pg/text_encoder.rb +35 -1
- data/lib/pg/type_map_by_column.rb +1 -1
- data/spec/helpers.rb +39 -7
- data/spec/pg/basic_type_mapping_spec.rb +230 -27
- data/spec/pg/connection_spec.rb +116 -77
- data/spec/pg/result_spec.rb +46 -11
- data/spec/pg/type_map_by_class_spec.rb +1 -1
- data/spec/pg/type_map_by_column_spec.rb +1 -1
- data/spec/pg/type_map_by_mri_type_spec.rb +1 -1
- data/spec/pg/type_map_by_oid_spec.rb +1 -1
- data/spec/pg/type_map_in_ruby_spec.rb +1 -1
- data/spec/pg/type_map_spec.rb +1 -1
- data/spec/pg/type_spec.rb +177 -11
- data/spec/pg_spec.rb +1 -1
- metadata +24 -28
- metadata.gz.sig +0 -0
data/ext/pg_text_encoder.c
CHANGED
data/ext/pg_tuple.c
ADDED
@@ -0,0 +1,540 @@
|
|
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( 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
|
+
return Qfalse;
|
62
|
+
}
|
63
|
+
}
|
64
|
+
|
65
|
+
static void
|
66
|
+
pg_tuple_gc_mark( t_pg_tuple *this )
|
67
|
+
{
|
68
|
+
int i;
|
69
|
+
|
70
|
+
if( !this ) return;
|
71
|
+
rb_gc_mark( this->result );
|
72
|
+
rb_gc_mark( this->typemap );
|
73
|
+
rb_gc_mark( this->field_map );
|
74
|
+
|
75
|
+
for( i = 0; i < this->num_fields; i++ ){
|
76
|
+
rb_gc_mark( this->values[i] );
|
77
|
+
}
|
78
|
+
rb_gc_mark( pg_tuple_get_field_names(this) );
|
79
|
+
}
|
80
|
+
|
81
|
+
static void
|
82
|
+
pg_tuple_gc_free( t_pg_tuple *this )
|
83
|
+
{
|
84
|
+
if( !this ) return;
|
85
|
+
xfree(this);
|
86
|
+
}
|
87
|
+
|
88
|
+
static size_t
|
89
|
+
pg_tuple_memsize( t_pg_tuple *this )
|
90
|
+
{
|
91
|
+
if( this==NULL ) return 0;
|
92
|
+
return sizeof(*this) + sizeof(*this->values) * this->num_fields;
|
93
|
+
}
|
94
|
+
|
95
|
+
static const rb_data_type_t pg_tuple_type = {
|
96
|
+
"pg",
|
97
|
+
{
|
98
|
+
(void (*)(void*))pg_tuple_gc_mark,
|
99
|
+
(void (*)(void*))pg_tuple_gc_free,
|
100
|
+
(size_t (*)(const void *))pg_tuple_memsize,
|
101
|
+
},
|
102
|
+
0, 0,
|
103
|
+
#ifdef RUBY_TYPED_FREE_IMMEDIATELY
|
104
|
+
RUBY_TYPED_FREE_IMMEDIATELY,
|
105
|
+
#endif
|
106
|
+
};
|
107
|
+
|
108
|
+
/*
|
109
|
+
* Document-method: allocate
|
110
|
+
*
|
111
|
+
* call-seq:
|
112
|
+
* PG::VeryTuple.allocate -> obj
|
113
|
+
*/
|
114
|
+
static VALUE
|
115
|
+
pg_tuple_s_allocate( VALUE klass )
|
116
|
+
{
|
117
|
+
return TypedData_Wrap_Struct( klass, &pg_tuple_type, NULL );
|
118
|
+
}
|
119
|
+
|
120
|
+
VALUE
|
121
|
+
pg_tuple_new(VALUE result, int row_num)
|
122
|
+
{
|
123
|
+
t_pg_tuple *this;
|
124
|
+
VALUE self = pg_tuple_s_allocate( rb_cPG_Tuple );
|
125
|
+
t_pg_result *p_result = pgresult_get_this(result);
|
126
|
+
int num_fields = p_result->nfields;
|
127
|
+
int i;
|
128
|
+
VALUE field_map = p_result->field_map;
|
129
|
+
int dup_names = num_fields != (int)RHASH_SIZE(field_map);
|
130
|
+
|
131
|
+
this = (t_pg_tuple *)xmalloc(
|
132
|
+
sizeof(*this) +
|
133
|
+
sizeof(*this->values) * num_fields +
|
134
|
+
sizeof(*this->values) * (dup_names ? 1 : 0));
|
135
|
+
RTYPEDDATA_DATA(self) = this;
|
136
|
+
|
137
|
+
this->result = result;
|
138
|
+
this->typemap = p_result->typemap;
|
139
|
+
this->field_map = field_map;
|
140
|
+
this->row_num = row_num;
|
141
|
+
this->num_fields = num_fields;
|
142
|
+
|
143
|
+
for( i = 0; i < num_fields; i++ ){
|
144
|
+
this->values[i] = Qundef;
|
145
|
+
}
|
146
|
+
|
147
|
+
if( dup_names ){
|
148
|
+
/* Some of the column names are duplicated -> we need the keys as Array in addition.
|
149
|
+
* Store it behind the values to save the space in the common case of no dups.
|
150
|
+
*/
|
151
|
+
this->values[num_fields] = rb_obj_freeze(rb_ary_new4(num_fields, p_result->fnames));
|
152
|
+
}
|
153
|
+
|
154
|
+
return self;
|
155
|
+
}
|
156
|
+
|
157
|
+
static inline t_pg_tuple *
|
158
|
+
pg_tuple_get_this( VALUE self )
|
159
|
+
{
|
160
|
+
t_pg_tuple *this;
|
161
|
+
TypedData_Get_Struct(self, t_pg_tuple, &pg_tuple_type, this);
|
162
|
+
if (this == NULL)
|
163
|
+
rb_raise(rb_eTypeError, "tuple is empty");
|
164
|
+
|
165
|
+
return this;
|
166
|
+
}
|
167
|
+
|
168
|
+
static VALUE
|
169
|
+
pg_tuple_materialize_field(t_pg_tuple *this, int col)
|
170
|
+
{
|
171
|
+
VALUE value = this->values[col];
|
172
|
+
|
173
|
+
if( value == Qundef ){
|
174
|
+
t_typemap *p_typemap = DATA_PTR( this->typemap );
|
175
|
+
|
176
|
+
pgresult_get(this->result); /* make sure we have a valid PGresult object */
|
177
|
+
value = p_typemap->funcs.typecast_result_value(p_typemap, this->result, this->row_num, col);
|
178
|
+
this->values[col] = value;
|
179
|
+
}
|
180
|
+
|
181
|
+
return value;
|
182
|
+
}
|
183
|
+
|
184
|
+
static void
|
185
|
+
pg_tuple_detach(t_pg_tuple *this)
|
186
|
+
{
|
187
|
+
this->result = Qnil;
|
188
|
+
this->typemap = Qnil;
|
189
|
+
this->row_num = -1;
|
190
|
+
}
|
191
|
+
|
192
|
+
static void
|
193
|
+
pg_tuple_materialize(t_pg_tuple *this)
|
194
|
+
{
|
195
|
+
int field_num;
|
196
|
+
for(field_num = 0; field_num < this->num_fields; field_num++) {
|
197
|
+
pg_tuple_materialize_field(this, field_num);
|
198
|
+
}
|
199
|
+
|
200
|
+
pg_tuple_detach(this);
|
201
|
+
}
|
202
|
+
|
203
|
+
/*
|
204
|
+
* call-seq:
|
205
|
+
* tup.fetch(key) → value
|
206
|
+
* tup.fetch(key, default) → value
|
207
|
+
* tup.fetch(key) { |key| block } → value
|
208
|
+
*
|
209
|
+
* Returns a field value by either column index or column name.
|
210
|
+
*
|
211
|
+
* An integer +key+ is interpreted as column index.
|
212
|
+
* Negative values of index count from the end of the array.
|
213
|
+
*
|
214
|
+
* A string +key+ is interpreted as column name.
|
215
|
+
*
|
216
|
+
* If the key can't be found, there are several options:
|
217
|
+
* With no other arguments, it will raise a IndexError exception;
|
218
|
+
* if default is given, then that will be returned;
|
219
|
+
* if the optional code block is specified, then that will be run and its result returned.
|
220
|
+
*/
|
221
|
+
static VALUE
|
222
|
+
pg_tuple_fetch(int argc, VALUE *argv, VALUE self)
|
223
|
+
{
|
224
|
+
VALUE key;
|
225
|
+
long block_given;
|
226
|
+
VALUE index;
|
227
|
+
int field_num;
|
228
|
+
t_pg_tuple *this = pg_tuple_get_this(self);
|
229
|
+
|
230
|
+
rb_check_arity(argc, 1, 2);
|
231
|
+
key = argv[0];
|
232
|
+
|
233
|
+
block_given = rb_block_given_p();
|
234
|
+
if (block_given && argc == 2) {
|
235
|
+
rb_warn("block supersedes default value argument");
|
236
|
+
}
|
237
|
+
|
238
|
+
switch(rb_type(key)){
|
239
|
+
case T_FIXNUM:
|
240
|
+
case T_BIGNUM:
|
241
|
+
field_num = NUM2INT(key);
|
242
|
+
if ( field_num < 0 )
|
243
|
+
field_num = this->num_fields + field_num;
|
244
|
+
if ( field_num < 0 || field_num >= this->num_fields ){
|
245
|
+
if (block_given) return rb_yield(key);
|
246
|
+
if (argc == 1) rb_raise( rb_eIndexError, "Index %d is out of range", field_num );
|
247
|
+
return argv[1];
|
248
|
+
}
|
249
|
+
break;
|
250
|
+
default:
|
251
|
+
index = rb_hash_aref(this->field_map, key);
|
252
|
+
|
253
|
+
if (index == Qnil) {
|
254
|
+
if (block_given) return rb_yield(key);
|
255
|
+
if (argc == 1) rb_raise( rb_eKeyError, "column not found" );
|
256
|
+
return argv[1];
|
257
|
+
}
|
258
|
+
|
259
|
+
field_num = NUM2INT(index);
|
260
|
+
}
|
261
|
+
|
262
|
+
return pg_tuple_materialize_field(this, field_num);
|
263
|
+
}
|
264
|
+
|
265
|
+
/*
|
266
|
+
* call-seq:
|
267
|
+
* res[ name ] -> value
|
268
|
+
*
|
269
|
+
* Returns field _name_.
|
270
|
+
*/
|
271
|
+
static VALUE
|
272
|
+
pg_tuple_aref(VALUE self, VALUE key)
|
273
|
+
{
|
274
|
+
VALUE index;
|
275
|
+
int field_num;
|
276
|
+
t_pg_tuple *this = pg_tuple_get_this(self);
|
277
|
+
|
278
|
+
switch(rb_type(key)){
|
279
|
+
case T_FIXNUM:
|
280
|
+
case T_BIGNUM:
|
281
|
+
field_num = NUM2INT(key);
|
282
|
+
if ( field_num < 0 )
|
283
|
+
field_num = this->num_fields + field_num;
|
284
|
+
if ( field_num < 0 || field_num >= this->num_fields )
|
285
|
+
return Qnil;
|
286
|
+
break;
|
287
|
+
default:
|
288
|
+
index = rb_hash_aref(this->field_map, key);
|
289
|
+
if( index == Qnil ) return Qnil;
|
290
|
+
field_num = NUM2INT(index);
|
291
|
+
}
|
292
|
+
|
293
|
+
return pg_tuple_materialize_field(this, field_num);
|
294
|
+
}
|
295
|
+
|
296
|
+
static VALUE
|
297
|
+
pg_tuple_num_fields_for_enum(VALUE self, VALUE args, VALUE eobj)
|
298
|
+
{
|
299
|
+
t_pg_tuple *this = pg_tuple_get_this(self);
|
300
|
+
return INT2NUM(this->num_fields);
|
301
|
+
}
|
302
|
+
|
303
|
+
static int
|
304
|
+
pg_tuple_yield_key_value(VALUE key, VALUE index, VALUE _this)
|
305
|
+
{
|
306
|
+
t_pg_tuple *this = (t_pg_tuple *)_this;
|
307
|
+
VALUE value = pg_tuple_materialize_field(this, NUM2INT(index));
|
308
|
+
rb_yield_values(2, key, value);
|
309
|
+
return ST_CONTINUE;
|
310
|
+
}
|
311
|
+
|
312
|
+
/*
|
313
|
+
* call-seq:
|
314
|
+
* tup.each{ |value| ... }
|
315
|
+
*
|
316
|
+
* Invokes block for each field value in the tuple.
|
317
|
+
*/
|
318
|
+
static VALUE
|
319
|
+
pg_tuple_each(VALUE self)
|
320
|
+
{
|
321
|
+
t_pg_tuple *this = pg_tuple_get_this(self);
|
322
|
+
VALUE field_names;
|
323
|
+
|
324
|
+
RETURN_SIZED_ENUMERATOR(self, 0, NULL, pg_tuple_num_fields_for_enum);
|
325
|
+
|
326
|
+
field_names = pg_tuple_get_field_names(this);
|
327
|
+
|
328
|
+
if( field_names == Qfalse ){
|
329
|
+
rb_hash_foreach(this->field_map, pg_tuple_yield_key_value, (VALUE)this);
|
330
|
+
} else {
|
331
|
+
int i;
|
332
|
+
for( i = 0; i < this->num_fields; i++ ){
|
333
|
+
VALUE value = pg_tuple_materialize_field(this, i);
|
334
|
+
rb_yield_values(2, RARRAY_AREF(field_names, i), value);
|
335
|
+
}
|
336
|
+
}
|
337
|
+
|
338
|
+
pg_tuple_detach(this);
|
339
|
+
return self;
|
340
|
+
}
|
341
|
+
|
342
|
+
/*
|
343
|
+
* call-seq:
|
344
|
+
* tup.each_value{ |value| ... }
|
345
|
+
*
|
346
|
+
* Invokes block for each field value in the tuple.
|
347
|
+
*/
|
348
|
+
static VALUE
|
349
|
+
pg_tuple_each_value(VALUE self)
|
350
|
+
{
|
351
|
+
t_pg_tuple *this = pg_tuple_get_this(self);
|
352
|
+
int field_num;
|
353
|
+
|
354
|
+
RETURN_SIZED_ENUMERATOR(self, 0, NULL, pg_tuple_num_fields_for_enum);
|
355
|
+
|
356
|
+
for(field_num = 0; field_num < this->num_fields; field_num++) {
|
357
|
+
rb_yield(pg_tuple_aref(self, INT2NUM(field_num)));
|
358
|
+
}
|
359
|
+
|
360
|
+
pg_tuple_detach(this);
|
361
|
+
return self;
|
362
|
+
}
|
363
|
+
|
364
|
+
|
365
|
+
/*
|
366
|
+
* call-seq:
|
367
|
+
* tup.values -> Array
|
368
|
+
*
|
369
|
+
* Returns the values of this tuple as Array.
|
370
|
+
* +res.tuple(i).values+ is equal to +res.tuple_values(i)+ .
|
371
|
+
*/
|
372
|
+
static VALUE
|
373
|
+
pg_tuple_values(VALUE self)
|
374
|
+
{
|
375
|
+
t_pg_tuple *this = pg_tuple_get_this(self);
|
376
|
+
|
377
|
+
pg_tuple_materialize(this);
|
378
|
+
return rb_ary_new4(this->num_fields, &this->values[0]);
|
379
|
+
}
|
380
|
+
|
381
|
+
static VALUE
|
382
|
+
pg_tuple_field_map(VALUE self)
|
383
|
+
{
|
384
|
+
t_pg_tuple *this = pg_tuple_get_this(self);
|
385
|
+
return this->field_map;
|
386
|
+
}
|
387
|
+
|
388
|
+
static VALUE
|
389
|
+
pg_tuple_field_names(VALUE self)
|
390
|
+
{
|
391
|
+
t_pg_tuple *this = pg_tuple_get_this(self);
|
392
|
+
return pg_tuple_get_field_names(this);
|
393
|
+
}
|
394
|
+
|
395
|
+
/*
|
396
|
+
* call-seq:
|
397
|
+
* tup.length → integer
|
398
|
+
*
|
399
|
+
* Returns number of fields of this tuple.
|
400
|
+
*/
|
401
|
+
static VALUE
|
402
|
+
pg_tuple_length(VALUE self)
|
403
|
+
{
|
404
|
+
t_pg_tuple *this = pg_tuple_get_this(self);
|
405
|
+
return INT2NUM(this->num_fields);
|
406
|
+
}
|
407
|
+
|
408
|
+
/*
|
409
|
+
* call-seq:
|
410
|
+
* tup.index(key) → integer
|
411
|
+
*
|
412
|
+
* Returns the field number which matches the given column name.
|
413
|
+
*/
|
414
|
+
static VALUE
|
415
|
+
pg_tuple_index(VALUE self, VALUE key)
|
416
|
+
{
|
417
|
+
t_pg_tuple *this = pg_tuple_get_this(self);
|
418
|
+
return rb_hash_aref(this->field_map, key);
|
419
|
+
}
|
420
|
+
|
421
|
+
|
422
|
+
static VALUE
|
423
|
+
pg_tuple_dump(VALUE self)
|
424
|
+
{
|
425
|
+
VALUE field_names;
|
426
|
+
VALUE values;
|
427
|
+
VALUE a;
|
428
|
+
t_pg_tuple *this = pg_tuple_get_this(self);
|
429
|
+
|
430
|
+
pg_tuple_materialize(this);
|
431
|
+
|
432
|
+
field_names = pg_tuple_get_field_names(this);
|
433
|
+
if( field_names == Qfalse )
|
434
|
+
field_names = rb_funcall(this->field_map, rb_intern("keys"), 0);
|
435
|
+
|
436
|
+
values = rb_ary_new4(this->num_fields, &this->values[0]);
|
437
|
+
a = rb_ary_new3(2, field_names, values);
|
438
|
+
|
439
|
+
if (FL_TEST(self, FL_EXIVAR)) {
|
440
|
+
rb_copy_generic_ivar(a, self);
|
441
|
+
FL_SET(a, FL_EXIVAR);
|
442
|
+
}
|
443
|
+
|
444
|
+
return a;
|
445
|
+
}
|
446
|
+
|
447
|
+
static VALUE
|
448
|
+
pg_tuple_load(VALUE self, VALUE a)
|
449
|
+
{
|
450
|
+
int num_fields;
|
451
|
+
int i;
|
452
|
+
t_pg_tuple *this;
|
453
|
+
VALUE values;
|
454
|
+
VALUE field_names;
|
455
|
+
VALUE field_map;
|
456
|
+
int dup_names;
|
457
|
+
|
458
|
+
rb_check_frozen(self);
|
459
|
+
rb_check_trusted(self);
|
460
|
+
|
461
|
+
TypedData_Get_Struct(self, t_pg_tuple, &pg_tuple_type, this);
|
462
|
+
if (this)
|
463
|
+
rb_raise(rb_eTypeError, "tuple is not empty");
|
464
|
+
|
465
|
+
Check_Type(a, T_ARRAY);
|
466
|
+
if (RARRAY_LEN(a) != 2)
|
467
|
+
rb_raise(rb_eTypeError, "expected an array of 2 elements");
|
468
|
+
|
469
|
+
field_names = RARRAY_AREF(a, 0);
|
470
|
+
Check_Type(field_names, T_ARRAY);
|
471
|
+
rb_obj_freeze(field_names);
|
472
|
+
values = RARRAY_AREF(a, 1);
|
473
|
+
Check_Type(values, T_ARRAY);
|
474
|
+
num_fields = RARRAY_LEN(values);
|
475
|
+
|
476
|
+
if (RARRAY_LEN(field_names) != num_fields)
|
477
|
+
rb_raise(rb_eTypeError, "different number of fields and values");
|
478
|
+
|
479
|
+
field_map = rb_hash_new();
|
480
|
+
for( i = 0; i < num_fields; i++ ){
|
481
|
+
rb_hash_aset(field_map, RARRAY_AREF(field_names, i), INT2FIX(i));
|
482
|
+
}
|
483
|
+
rb_obj_freeze(field_map);
|
484
|
+
|
485
|
+
dup_names = num_fields != (int)RHASH_SIZE(field_map);
|
486
|
+
|
487
|
+
this = (t_pg_tuple *)xmalloc(
|
488
|
+
sizeof(*this) +
|
489
|
+
sizeof(*this->values) * num_fields +
|
490
|
+
sizeof(*this->values) * (dup_names ? 1 : 0));
|
491
|
+
RTYPEDDATA_DATA(self) = this;
|
492
|
+
|
493
|
+
this->result = Qnil;
|
494
|
+
this->typemap = Qnil;
|
495
|
+
this->row_num = -1;
|
496
|
+
this->num_fields = num_fields;
|
497
|
+
this->field_map = field_map;
|
498
|
+
|
499
|
+
for( i = 0; i < num_fields; i++ ){
|
500
|
+
VALUE v = RARRAY_AREF(values, i);
|
501
|
+
if( v == Qundef )
|
502
|
+
rb_raise(rb_eTypeError, "field %d is not materialized", i);
|
503
|
+
this->values[i] = v;
|
504
|
+
}
|
505
|
+
|
506
|
+
if( dup_names ){
|
507
|
+
this->values[num_fields] = field_names;
|
508
|
+
}
|
509
|
+
|
510
|
+
if (FL_TEST(a, FL_EXIVAR)) {
|
511
|
+
rb_copy_generic_ivar(self, a);
|
512
|
+
FL_SET(self, FL_EXIVAR);
|
513
|
+
}
|
514
|
+
|
515
|
+
return self;
|
516
|
+
}
|
517
|
+
|
518
|
+
void
|
519
|
+
init_pg_tuple()
|
520
|
+
{
|
521
|
+
rb_cPG_Tuple = rb_define_class_under( rb_mPG, "Tuple", rb_cObject );
|
522
|
+
rb_define_alloc_func( rb_cPG_Tuple, pg_tuple_s_allocate );
|
523
|
+
rb_include_module(rb_cPG_Tuple, rb_mEnumerable);
|
524
|
+
|
525
|
+
rb_define_method(rb_cPG_Tuple, "fetch", pg_tuple_fetch, -1);
|
526
|
+
rb_define_method(rb_cPG_Tuple, "[]", pg_tuple_aref, 1);
|
527
|
+
rb_define_method(rb_cPG_Tuple, "each", pg_tuple_each, 0);
|
528
|
+
rb_define_method(rb_cPG_Tuple, "each_value", pg_tuple_each_value, 0);
|
529
|
+
rb_define_method(rb_cPG_Tuple, "values", pg_tuple_values, 0);
|
530
|
+
rb_define_method(rb_cPG_Tuple, "length", pg_tuple_length, 0);
|
531
|
+
rb_define_alias(rb_cPG_Tuple, "size", "length");
|
532
|
+
rb_define_method(rb_cPG_Tuple, "index", pg_tuple_index, 1);
|
533
|
+
|
534
|
+
rb_define_private_method(rb_cPG_Tuple, "field_map", pg_tuple_field_map, 0);
|
535
|
+
rb_define_private_method(rb_cPG_Tuple, "field_names", pg_tuple_field_names, 0);
|
536
|
+
|
537
|
+
/* methods for marshaling */
|
538
|
+
rb_define_private_method(rb_cPG_Tuple, "marshal_dump", pg_tuple_dump, 0);
|
539
|
+
rb_define_private_method(rb_cPG_Tuple, "marshal_load", pg_tuple_load, 1);
|
540
|
+
}
|