From a08d35225f31d3226e3bed7cf1cade8bad0d75a0 Mon Sep 17 00:00:00 2001 From: Josh Faigan Date: Tue, 4 Jun 2024 20:38:15 +0000 Subject: [PATCH] Add `last_result_size` to ruby connection Add a new method `last_result_size` to the Trilogy gem's Ruby bindings that allows users to retrieve the exact byte size of the last result received from a query. --- contrib/ruby/ext/trilogy-ruby/cext.c | 3 +++ contrib/ruby/test/client_test.rb | 14 ++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/contrib/ruby/ext/trilogy-ruby/cext.c b/contrib/ruby/ext/trilogy-ruby/cext.c index 69f84511..e2d62953 100644 --- a/contrib/ruby/ext/trilogy-ruby/cext.c +++ b/contrib/ruby/ext/trilogy-ruby/cext.c @@ -1131,6 +1131,8 @@ static VALUE rb_trilogy_server_status(VALUE self) { return LONG2FIX(get_open_ctx static VALUE rb_trilogy_server_version(VALUE self) { return rb_str_new_cstr(get_open_ctx(self)->server_version); } +static VALUE rb_trilogy_last_result_size(VALUE self) { return rb_int_new(get_open_ctx(self)->conn.recv_buff_len); } + RUBY_FUNC_EXPORTED void Init_cext(void) { VALUE Trilogy = rb_const_get(rb_cObject, rb_intern("Trilogy")); @@ -1161,6 +1163,7 @@ RUBY_FUNC_EXPORTED void Init_cext(void) rb_define_method(Trilogy, "more_results_exist?", rb_trilogy_more_results_exist, 0); rb_define_method(Trilogy, "next_result", rb_trilogy_next_result, 0); rb_define_method(Trilogy, "set_server_option", rb_trilogy_set_server_option, 1); + rb_define_method(Trilogy, "last_result_size", rb_trilogy_last_result_size, 0); rb_define_const(Trilogy, "TLS_VERSION_10", INT2NUM(TRILOGY_TLS_VERSION_10)); rb_define_const(Trilogy, "TLS_VERSION_11", INT2NUM(TRILOGY_TLS_VERSION_11)); rb_define_const(Trilogy, "TLS_VERSION_12", INT2NUM(TRILOGY_TLS_VERSION_12)); diff --git a/contrib/ruby/test/client_test.rb b/contrib/ruby/test/client_test.rb index f9aac0e7..b3f8632a 100644 --- a/contrib/ruby/test/client_test.rb +++ b/contrib/ruby/test/client_test.rb @@ -272,6 +272,20 @@ def test_trilogy_set_server_option_with_invalid_option assert_match(/1047: Unknown command/, e.message) end + def test_trilogy_last_result_size + client = new_tcp_client + create_test_table(client) + + client.query("INSERT INTO trilogy_test (int_test) VALUES ('4')") + client.query("INSERT INTO trilogy_test (int_test) VALUES ('3')") + + result = client.query("SELECT id, int_test FROM trilogy_test") + result_size = client.last_result_size + + assert_equal [1, 4, 2, 3], result.rows.flatten + assert_includes 150..170, result_size, "Expected last_result_size to be between 150 and 170, but was #{result_size}" + end + def test_trilogy_set_server_option_multi_statement # Start with multi_statement disabled, enable it during connection client = new_tcp_client