Class: PG::TypeMapByColumn

Inherits:
TypeMap
  • Object
show all
Includes:
PG::TypeMap::DefaultTypeMappable
Defined in:
ext/pg_type_map_by_column.c,
lib/pg/type_map_by_column.rb,
ext/pg_type_map_by_column.c

Overview

This type map casts values by a coder assigned per field/column.

Each PG::TypeMapByColumn has a fixed list of either encoders or decoders, that is defined at TypeMapByColumn.new . A type map with encoders is usable for type casting query bind parameters and COPY data for PG::Connection#put_copy_data . A type map with decoders is usable for type casting of result values and COPY data from PG::Connection#get_copy_data .

PG::TypeMapByColumn objects are in particular useful in conjunction with prepared statements, since they can be cached alongside with the statement handle.

This type map strategy is also used internally by PG::TypeMapByOid, when the number of rows of a result set exceeds a given limit.

Instance Method Summary collapse

Methods included from PG::TypeMap::DefaultTypeMappable

#default_type_map, #default_type_map=, #with_default_type_map

Constructor Details

#PG::TypeMapByColumn.new(coders) ⇒ Object

Builds a new type map and assigns a list of coders for the given column. coders must be an Array of PG::Coder objects or nil values. The length of the Array corresponds to the number of columns or bind parameters this type map is usable for.

A nil value will forward the given field to the #default_type_map .



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# File 'ext/pg_type_map_by_column.c', line 262

static VALUE
pg_tmbc_init(VALUE self, VALUE conv_ary)
{
	long i;
	t_tmbc *this;
	int conv_ary_len;

	rb_check_frozen(self);
	Check_Type(conv_ary, T_ARRAY);
	conv_ary_len = RARRAY_LENINT(conv_ary);
	this = xmalloc(sizeof(t_tmbc) + sizeof(struct pg_tmbc_converter) * conv_ary_len);
	/* Set nfields to 0 at first, so that GC mark function doesn't access uninitialized memory. */
	this->nfields = 0;
	this->typemap.funcs = pg_tmbc_funcs;
	RB_OBJ_WRITE(self, &this->typemap.default_typemap, pg_typemap_all_strings);
	RTYPEDDATA_DATA(self) = this;

	for(i=0; i<conv_ary_len; i++)
	{
		VALUE obj = rb_ary_entry(conv_ary, i);

		if( obj == Qnil ){
			/* no type cast */
			this->convs[i].cconv = NULL;
		} else {
			t_pg_coder *p_coder;
			/* Check argument type and store the coder pointer */
			TypedData_Get_Struct(obj, t_pg_coder, &pg_coder_type, p_coder);
			RB_OBJ_WRITTEN(self, Qnil, p_coder->coder_obj);
			this->convs[i].cconv = p_coder;
		}
	}

	this->nfields = conv_ary_len;

	return self;
}

Instance Method Details

#codersArray

Array of PG::Coder objects. The length of the Array corresponds to the number of columns or bind parameters this type map is usable for.

Returns:

  • (Array)


307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# File 'ext/pg_type_map_by_column.c', line 307

static VALUE
pg_tmbc_coders(VALUE self)
{
	int i;
	t_tmbc *this = RTYPEDDATA_DATA( self );
	VALUE ary_coders = rb_ary_new();

	for( i=0; i<this->nfields; i++){
		t_pg_coder *conv = this->convs[i].cconv;
		if( conv ) {
			rb_ary_push( ary_coders, conv->coder_obj );
		} else {
			rb_ary_push( ary_coders, Qnil );
		}
	}

	return rb_obj_freeze(ary_coders);
}

#inspectObject



12
13
14
15
# File 'lib/pg/type_map_by_column.rb', line 12

def inspect
	type_strings = coders.map{|c| c ? c.inspect_short : 'nil' }
	"#<#{self.class} #{type_strings.join(' ')}>"
end

#oidsObject

Returns the type oids of the assigned coders.



8
9
10
# File 'lib/pg/type_map_by_column.rb', line 8

def oids
	coders.map{|c| c.oid if c }
end