123456789_123456789_123456789_123456789_123456789_

Class: PG::TypeMapByOid

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Subclasses:
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, TypeMap
Instance Chain:
Inherits: PG::TypeMap
Defined in: ext/pg_type_map_by_oid.c

Overview

This type map casts values based on the type OID of the given column in the result set.

This type map is only suitable to cast values from Result objects. Therefore only decoders might be assigned by the #add_coder method.

Fields with no match to any of the registered type OID / format combination are forwarded to the #default_type_map .

Instance Attribute Summary

TypeMap::DefaultTypeMappable - Included

#default_type_map

Returns the default TypeMap that is currently set for values that could not be casted by this type map.

#default_type_map=

Set the default TypeMap that is used for values that could not be casted by this type map.

Instance Method Summary

TypeMap::DefaultTypeMappable - Included

#with_default_type_map

Set the default TypeMap that is used for values that could not be casted by this type map.

Instance Attribute Details

#max_rows_for_online_lookupInteger (rw)

[ GitHub ]

  
# File 'ext/pg_type_map_by_oid.c', line 332

static VALUE
pg_tmbo_max_rows_for_online_lookup_get( VALUE self )
{
	t_tmbo *this = RTYPEDDATA_DATA( self );
	return INT2NUM(this->max_rows_for_online_lookup);
}

#max_rows_for_online_lookup=(number) (rw)

Threshold for doing Hash lookups versus creation of a dedicated TypeMapByColumn. The type map will do Hash lookups for each result value, if the number of rows is below or equal number.

[ GitHub ]

  
# File 'ext/pg_type_map_by_oid.c', line 319

static VALUE
pg_tmbo_max_rows_for_online_lookup_set( VALUE self, VALUE value )
{
	t_tmbo *this = RTYPEDDATA_DATA( self );
	rb_check_frozen(self);
	this->max_rows_for_online_lookup = NUM2INT(value);
	return value;
}

Instance Method Details

#add_coder(coder)

Assigns a new Coder object to the type map. The decoder is registered for type casts based on it’s Coder#oid and Coder#format attributes.

Later changes of the oid or format code within the coder object will have no effect to the type map.

[ GitHub ]

  
# File 'ext/pg_type_map_by_oid.c', line 237

static VALUE
pg_tmbo_add_coder( VALUE self, VALUE coder )
{
	VALUE hash;
	t_tmbo *this = RTYPEDDATA_DATA( self );
	t_pg_coder *p_coder;
	struct pg_tmbo_oid_cache_entry *p_ce;

	rb_check_frozen(self);
	TypedData_Get_Struct(coder, t_pg_coder, &pg_coder_type, p_coder);

	if( p_coder->format < 0 || p_coder->format > 1 )
		rb_raise(rb_eArgError, "invalid format code %d", p_coder->format);

	/* Update cache entry */
	p_ce = CACHE_LOOKUP(this, p_coder->format, p_coder->oid);
	p_ce->oid = p_coder->oid;
	p_ce->p_coder = p_coder;
	/* Write coder into the hash of the given format */
	hash = this->format[p_coder->format].oid_to_coder;
	rb_hash_aset( hash, UINT2NUM(p_coder->oid), coder);

	return self;
}

#build_column_map(result)

This builds a TypeMapByColumn that fits to the given Result object based on it’s type OIDs and binary/text format.

[ GitHub ]

  
# File 'ext/pg_type_map_by_oid.c', line 347

static VALUE
pg_tmbo_build_column_map( VALUE self, VALUE result )
{
	t_tmbo *this = RTYPEDDATA_DATA( self );

	if ( !rb_obj_is_kind_of(result, rb_cPGresult) ) {
		rb_raise( rb_eTypeError, "wrong argument type %s (expected kind of PG::Result)",
				rb_obj_classname( result ) );
	}

	return pg_tmbo_build_type_map_for_result2( this, pgresult_get(result) );
}

#codersArray

Array of all assigned Coder objects.

[ GitHub ]

  
# File 'ext/pg_type_map_by_oid.c', line 300

static VALUE
pg_tmbo_coders( VALUE self )
{
	t_tmbo *this = RTYPEDDATA_DATA( self );

	return rb_ary_concat(
			rb_funcall(this->format[0].oid_to_coder, rb_intern("values"), 0),
			rb_funcall(this->format[1].oid_to_coder, rb_intern("values"), 0));
}

#rm_coder(format, oid)

Removes a Coder object from the type map based on the given oid and format codes.

Returns the removed coder object.

[ GitHub ]

  
# File 'ext/pg_type_map_by_oid.c', line 271

static VALUE
pg_tmbo_rm_coder( VALUE self, VALUE format, VALUE oid )
{
	VALUE hash;
	VALUE coder;
	t_tmbo *this = RTYPEDDATA_DATA( self );
	int i_format = NUM2INT(format);
	struct pg_tmbo_oid_cache_entry *p_ce;

	rb_check_frozen(self);
	if( i_format < 0 || i_format > 1 )
		rb_raise(rb_eArgError, "invalid format code %d", i_format);

	/* Mark the cache entry as empty */
	p_ce = CACHE_LOOKUP(this, i_format, NUM2UINT(oid));
	p_ce->oid = 0;
	p_ce->p_coder = NULL;
	hash = this->format[i_format].oid_to_coder;
	coder = rb_hash_delete( hash, oid );

	return coder;
}