Class: PG::TypeMapInRuby

Inherits:
TypeMap show all
Includes:
PG::TypeMap::DefaultTypeMappable
Defined in:
ext/pg_type_map_in_ruby.c,
ext/pg_type_map_in_ruby.c

Overview

This class can be used to implement a type map in ruby, typically as a #default_type_map in a type map chain.

This API is EXPERIMENTAL and could change in the future.

Direct Known Subclasses

BasicTypeMapForResults::WarningTypeMap

Instance Method Summary collapse

Methods included from PG::TypeMap::DefaultTypeMappable

#default_type_map, #default_type_map=, #with_default_type_map

Instance Method Details

#typecast_copy_get(field_str, fieldno, format, encoding) ⇒ Object

Cast a field string received by PG::Connection#get_copy_data.

This method implementation uses the #default_type_map to cast field_str. It can be derived to change this behaviour.

Parameters:

  • field_str : The String received from the server.

  • fieldno : The field number from left to right.

  • format : The format code (0 = text, 1 = binary)

  • encoding : The encoding of the connection and encoding the returned value should get.


263
264
265
266
267
268
269
270
271
# File 'ext/pg_type_map_in_ruby.c', line 263

static VALUE
pg_tmir_typecast_copy_get( VALUE self, VALUE field_str, VALUE fieldno, VALUE format, VALUE enc )
{
	t_tmir *this = RTYPEDDATA_DATA( self );
	t_typemap *default_tm = RTYPEDDATA_DATA( this->typemap.default_typemap );
	int enc_idx = rb_to_encoding_index( enc );

	return default_tm->funcs.typecast_copy_get( default_tm, field_str, NUM2INT(fieldno), NUM2INT(format), enc_idx );
}

#typecast_query_param(param_value, field) ⇒ Object

Cast a field string for transmission to the server.

This method implementation uses the #default_type_map to cast param_value. It can be derived to change this behaviour.

Parameters:

  • param_value : The value from the user.

  • field : The field number from left to right.


185
186
187
188
189
190
191
192
193
# File 'ext/pg_type_map_in_ruby.c', line 185

static VALUE
pg_tmir_typecast_query_param( VALUE self, VALUE param_value, VALUE field )
{
	t_tmir *this = RTYPEDDATA_DATA( self );
	t_typemap *default_tm = RTYPEDDATA_DATA( this->typemap.default_typemap );
	t_pg_coder *p_coder = default_tm->funcs.typecast_query_param( default_tm, param_value, NUM2INT(field) );

	return p_coder ? p_coder->coder_obj : Qnil;
}

#typecast_result_value(result, tuple, field) ⇒ Object

Retrieve and cast a field of the given result.

This method implementation uses the #default_type_map to get the field value. It can be derived to change this behaviour.

Parameters:

  • result : The PG::Result received from the database.

  • tuple : The row number to retrieve.

  • field : The column number to retrieve.

Note: Calling any value retrieving methods of result will result in an (endless) recursion. Instead super() can be used to retrieve the value using the default_typemap.


119
120
121
122
123
124
125
# File 'ext/pg_type_map_in_ruby.c', line 119

static VALUE
pg_tmir_typecast_result_value( VALUE self, VALUE result, VALUE tuple, VALUE field )
{
	t_tmir *this = RTYPEDDATA_DATA( self );
	t_typemap *default_tm = RTYPEDDATA_DATA( this->typemap.default_typemap );
	return default_tm->funcs.typecast_result_value( default_tm, result, NUM2INT(tuple), NUM2INT(field) );
}