Skip to content

Commit

Permalink
Use symbols for type, charset
Browse files Browse the repository at this point in the history
We want to set some of these values as symbols instead of ints
on the Column to make it easier to work with. In this commit, we
created loopups for int to string using macros, then changed the
type to a symbol via `rb_intern` + `rb_id2sym`. It's possible there's
a single function to do this, but we couldn't find one.

I also added a downcase function to make sure we get the values in
lower instead of uppercase. I'm sure there is a better way to do this.
I have no idea how to write C.

Co-authored-by: Daniel Colson <composerinteralia@github.com>
  • Loading branch information
ipc103 and composerinteralia committed Sep 29, 2023
1 parent 2e9e86b commit 33dff02
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
30 changes: 27 additions & 3 deletions contrib/ruby/ext/trilogy-ruby/cext.c
Original file line number Diff line number Diff line change
Expand Up @@ -1144,16 +1144,40 @@ static VALUE rb_trilogy_write_timeout_set(VALUE self, VALUE write_timeout)
return write_timeout;
}

const char *const trilogy_type_names[] = {
#define XX(name, code) [name] = (char *)#name + strlen("TRILOGY_TYPE_"),
TRILOGY_TYPES(XX)
#undef XX
};

const char *const trilogy_charset_names[] = {
#define XX(name, code) [name] = (char *)#name + strlen("TRILOGY_CHARSET_"),
TRILOGY_CHARSETS(XX)
#undef XX
};

static char *downcase(const char *str)
{
char *new_str = ALLOC_N(char, strlen(str) + 1);
char *p = new_str;
while (*str) {
*p++ = tolower(*str++);
}
*p = '\0';
return new_str;
}

static VALUE rb_trilogy_result_columns(VALUE self)
{
struct trilogy_result_ctx *trilogy_result_ctx = get_trilogy_result_ctx(self);
VALUE cols = rb_ary_new();
for (uint64_t i = 0; i < trilogy_result_ctx->column_count; i++) {
VALUE obj = rb_funcall(
Trilogy_Result_Column, rb_intern("new"), 6, trilogy_result_ctx->column_info[i].name,
rb_int_new(trilogy_result_ctx->column_info[i].type), rb_int_new(trilogy_result_ctx->column_info[i].len),
rb_int_new(trilogy_result_ctx->column_info[i].flags),
rb_int_new(trilogy_result_ctx->column_info[i].charset), rb_int_new(trilogy_result_ctx->column_info[i].decimals));
rb_id2sym(rb_intern(downcase(trilogy_type_names[trilogy_result_ctx->column_info[i].type]))),
rb_int_new(trilogy_result_ctx->column_info[i].len), rb_int_new(trilogy_result_ctx->column_info[i].flags),
rb_id2sym(rb_intern(downcase(trilogy_charset_names[trilogy_result_ctx->column_info[i].charset]))),
rb_int_new(trilogy_result_ctx->column_info[i].decimals));
rb_ary_push(cols, obj);
}
return cols;
Expand Down
2 changes: 2 additions & 0 deletions contrib/ruby/test/client_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,8 @@ def test_trilogy_query_values
assert_equal ["id", "int_test"], result.fields
assert_equal 2, result.columns.count
assert_equal ["id", "int_test"], result.columns.map(&:name)
assert_equal :long, result.columns.first.type
assert_equal :binary, result.columns.first.charset
assert_equal [1, 4, 2, 3, 3, 1], result.rows
end

Expand Down

0 comments on commit 33dff02

Please sign in to comment.