From 9266a262d5e3d4f812c3fa04b22f6d04fdf586ae Mon Sep 17 00:00:00 2001 From: Jeremy Evans Date: Mon, 13 Apr 2020 09:02:24 -0700 Subject: [PATCH] Simplify inet/cidr array type conversion In spg__col_value, allow overridding of inet/cidr type conversion using conversion procs. Use goto to jump to the default case if a conversion proc is set for the array type. In spg__array_col_value, use convertor argument, and fallthrough to keep things simpler and avoid encoding issues. The fallthrough will probably not happen in the typical case due to changes in spg__col_value, but if parse_pg_array is called directly, it probably could be reached. --- ext/sequel_pg/sequel_pg.c | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/ext/sequel_pg/sequel_pg.c b/ext/sequel_pg/sequel_pg.c index 4364a82..16f3e41 100644 --- a/ext/sequel_pg/sequel_pg.c +++ b/ext/sequel_pg/sequel_pg.c @@ -872,8 +872,6 @@ static VALUE spg_fetch_rows_set_cols(VALUE self, VALUE ignore) { static VALUE spg__array_col_value(char *v, size_t length, VALUE converter, int enc_index, int oid, VALUE db) { VALUE rv; - VALUE conv_procs = 0; - VALUE conversion_proc = 0; struct spg_blob_initialization bi; switch(oid) { @@ -933,17 +931,10 @@ static VALUE spg__array_col_value(char *v, size_t length, VALUE converter, int e break; case 869: /* inet */ case 650: /* cidr */ - if (conv_procs == 0) { - conv_procs = rb_funcall(rb_funcall(db, spg_id_db, 0), spg_id_conversion_procs, 0); - } - conversion_proc = rb_funcall(conv_procs, spg_id_get, 1, INT2NUM(oid)); - if (conversion_proc != Qnil) { - rv = rb_tainted_str_new(v, length); - rv = rb_funcall(conversion_proc, spg_id_call, 1, rv); - } else { + if (!RTEST(converter)) { rv = spg_inet(v, length); + break; } - break; default: rv = rb_tainted_str_new(v, length); PG_ENCODING_SET_NOCHECK(rv, enc_index); @@ -1227,10 +1218,16 @@ static VALUE spg__col_value(VALUE self, PGresult *res, long i, long j, VALUE* co scalar_oid = 22; break; case 1041: + if (RTEST(colconvert[j])) { + goto default_cond; + } array_type = spg_sym_inet; scalar_oid = 869; break; case 651: + if (RTEST(colconvert[j])) { + goto default_cond; + } array_type = spg_sym_cidr; scalar_oid = 650; break; @@ -1244,6 +1241,7 @@ static VALUE spg__col_value(VALUE self, PGresult *res, long i, long j, VALUE* co break; } default: + default_cond: rv = rb_tainted_str_new(v, PQgetlength(res, i, j)); PG_ENCODING_SET_NOCHECK(rv, enc_index); if (colconvert[j] != Qnil) {