Class: PG::TypeMapByColumn

Inherits:
TypeMap
  • Object
show all
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 #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::TypeMapByColumns 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 inherited from TypeMap

#fit_to_query, #fit_to_result

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 cast the given field to a String object.



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'ext/pg_type_map_by_column.c', line 165

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

	Check_Type(self, T_DATA);
	Check_Type(conv_ary, T_ARRAY);
	conv_ary_len = RARRAY_LEN(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 = pg_tmbc_default_typemap;
	DATA_PTR(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 if( rb_obj_is_kind_of(obj, rb_cPG_Coder) ){
			Data_Get_Struct(obj, t_pg_coder, this->convs[i].cconv);
		} else {
			rb_raise(rb_eArgError, "argument %d has invalid type %s (should be nil or some kind of PG::Coder)",
							 i+1, rb_obj_classname( obj ));
		}
	}

	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)


208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'ext/pg_type_map_by_column.c', line 208

static VALUE
pg_tmbc_coders(VALUE self)
{
	int i;
	t_tmbc *this = DATA_PTR( 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



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

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

#oidsObject

Returns the type oids of the assigned coders.



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

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