Class: PG::TypeMapByOid

Inherits:
TypeMap show all
Defined in:
ext/pg_type_map_by_oid.c,
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 PG::Result objects. Therefore only decoders might be assigned by the #add_coder method.

Instance Method Summary collapse

Methods inherited from TypeMap

#fit_to_query

Instance Method Details

#add_coder(coder) ⇒ Object

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

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



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'ext/pg_type_map_by_oid.c', line 176

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

  if( !rb_obj_is_kind_of(coder, rb_cPG_Coder) )
    rb_raise(rb_eArgError, "invalid type %s (should be some kind of PG::Coder)",
              rb_obj_classname( coder ));

  Data_Get_Struct(coder, t_pg_coder, 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;
}

#codersArray

Array of all assigned PG::Coder objects.

Returns:

  • (Array)


241
242
243
244
245
246
247
248
249
# File 'ext/pg_type_map_by_oid.c', line 241

static VALUE
pg_tmbo_coders( VALUE self )
{
  t_tmbo *this = DATA_PTR( 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));
}

#fit_to_result(result, online_lookup = nil) ⇒ Object

This is an extended version of PG::TypeMap#fit_to_result that allows explicit selection of online lookup ( online_lookup=true ) or building of a new PG::TypeMapByColumn ( online_lookup=false ).



289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'ext/pg_type_map_by_oid.c', line 289

static VALUE
pg_tmbo_fit_to_result_ext( int argc, VALUE *argv, VALUE self )
{
  t_tmbo *this = DATA_PTR( self );
  VALUE result;
  VALUE online_lookup;

  rb_scan_args(argc, argv, "11", &result, &online_lookup);

  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 ) );
  }

  if( NIL_P( online_lookup ) ){
    /* call super */
    return this->typemap.fit_to_result(self, result);
  } else if( RB_TYPE_P( online_lookup, T_TRUE ) ){
    return self;
  } else if( RB_TYPE_P( online_lookup, T_FALSE ) ){
    PGresult *pgresult = pgresult_get( result );

    return pg_tmbo_build_type_map_for_result2( this, pgresult );
  } else {
    rb_raise( rb_eArgError, "argument online_lookup %s should be true, false or nil instead",
        rb_obj_classname( result ) );
  }
}

#max_rows_for_online_lookupInteger

Returns:

  • (Integer)


272
273
274
275
276
277
# File 'ext/pg_type_map_by_oid.c', line 272

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

#max_rows_for_online_lookup=(number) ⇒ Object

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



260
261
262
263
264
265
266
# File 'ext/pg_type_map_by_oid.c', line 260

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

#rm_coder(format, oid) ⇒ Object

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

Returns the removed coder object.



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'ext/pg_type_map_by_oid.c', line 213

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

  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;
}