Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encode column names with encoding of context #210

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions contrib/ruby/ext/trilogy-ruby/cext.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ struct trilogy_ctx {
char server_version[TRILOGY_SERVER_VERSION_SIZE + 1];
unsigned int query_flags;
VALUE encoding;
rb_encoding *conn_encoding;
};

static void mark_trilogy(void *ptr)
Expand Down Expand Up @@ -456,6 +457,7 @@ static VALUE rb_trilogy_connect(VALUE self, VALUE encoding, VALUE charset, VALUE

RB_OBJ_WRITE(self, &ctx->encoding, encoding);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
RB_OBJ_WRITE(self, &ctx->encoding, encoding);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, you're keeping both. I think we can get rid of the reference to encoding. Just turn encoding into a rb_encoding *, so we don't need to mark it or antyhing.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I remove encoding from the ctx (and also from the trilogy_ctx struct) alltogether?

It's only used in the RB_OBJ_WRITE call (which, if I understand correctly, writes the encoding value to the ctx->encoding) and in the rb_to_encoding call, which probably makes storing this obsolete as we have the rb_encoding pointer in the ctx struct which can be used to encode values.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's what I'm suggesting yes. That member is no longer used, so no point keeping it.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah sorry, didn't see your other comment in the conversation tab 😅 I applied these changes, now encoding in the ctx struct is the rb_encoding * value which can be directly used :)

connopt.encoding = NUM2INT(charset);
ctx->conn_encoding = rb_to_encoding(ctx->encoding);

Check_Type(opts, T_HASH);

Expand Down Expand Up @@ -811,10 +813,10 @@ static VALUE read_query_response(VALUE vargs)
}
}

#ifdef HAVE_RB_INTERNED_STR
VALUE column_name = rb_interned_str(column.name, column.name_len);
#ifdef HAVE_RB_ENC_INTERNED_STR
VALUE column_name = rb_enc_interned_str(column.name, column.name_len, ctx->conn_encoding);
#else
VALUE column_name = rb_str_new(column.name, column.name_len);
VALUE column_name = rb_enc_str_new(column.name, column.name_len, ctx->conn_encoding);
OBJ_FREEZE(column_name);
#endif

Expand Down Expand Up @@ -922,7 +924,7 @@ static VALUE rb_trilogy_query(VALUE self, VALUE query)
struct trilogy_ctx *ctx = get_open_ctx(self);

StringValue(query);
query = rb_str_export_to_enc(query, rb_to_encoding(ctx->encoding));
query = rb_str_export_to_enc(query, ctx->conn_encoding);

int rc = trilogy_query_send(&ctx->conn, RSTRING_PTR(query), RSTRING_LEN(query));

Expand Down
1 change: 1 addition & 0 deletions contrib/ruby/ext/trilogy-ruby/extconf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@
have_library("crypto", "CRYPTO_malloc")
have_library("ssl", "SSL_new")
have_func("rb_interned_str", "ruby.h")
have_func("rb_enc_interned_str", "ruby.h")

create_makefile "trilogy/cext"