Skip to content

Commit

Permalink
Simplify inet/cidr array type conversion
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jeremyevans committed Apr 13, 2020
1 parent 9034693 commit 9266a26
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions ext/sequel_pg/sequel_pg.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand Down

0 comments on commit 9266a26

Please sign in to comment.