Skip to content

Commit

Permalink
Ruby: Implement #set_server_option
Browse files Browse the repository at this point in the history
  • Loading branch information
paarthmadan committed Mar 15, 2023
1 parent ba884c0 commit 610e1b7
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
40 changes: 40 additions & 0 deletions contrib/ruby/ext/trilogy-ruby/cext.c
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,40 @@ static VALUE rb_trilogy_change_db(VALUE self, VALUE database)
return Qtrue;
}

static VALUE rb_trilogy_set_server_option(VALUE self, VALUE option)
{
struct trilogy_ctx *ctx = get_open_ctx(self);

int rc = trilogy_set_option_send(&ctx->conn, NUM2INT(option));

if (rc == TRILOGY_AGAIN) {
rc = flush_writes(ctx);
}

if (rc != TRILOGY_OK) {
handle_trilogy_error(ctx, rc, "trilogy_set_option_send");
}

while (1) {
rc = trilogy_set_option_recv(&ctx->conn);

if (rc == TRILOGY_OK) {
break;
}

if (rc != TRILOGY_AGAIN) {
handle_trilogy_error(ctx, rc, "trilogy_set_option_recv");
}

if (trilogy_sock_wait_read(ctx->conn.socket) < 0) {
rb_raise(Trilogy_TimeoutError, "trilogy_set_option_recv");
}
}

return Qtrue;
}


static void load_query_options(unsigned int query_flags, struct rb_trilogy_cast_options *cast_options)
{
cast_options->cast = (query_flags & TRILOGY_FLAGS_CAST) != 0;
Expand Down Expand Up @@ -989,6 +1023,7 @@ void Init_cext()
rb_define_method(Trilogy, "server_version", rb_trilogy_server_version, 0);
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_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));
Expand Down Expand Up @@ -1081,4 +1116,9 @@ void Init_cext()
#define XX(name, code) rb_const_set(Trilogy, rb_intern((char *)#name + strlen("TRILOGY_")), LONG2NUM(name));
TRILOGY_SERVER_STATUS(XX)
#undef XX

// set_server_option options
#define XX(name, code) rb_const_set(Trilogy, rb_intern((char *)#name + strlen("TRILOGY_")), LONG2NUM(name));
TRILOGY_SET_SERVER_OPTION(XX)
#undef XX
}
51 changes: 51 additions & 0 deletions contrib/ruby/test/client_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,57 @@ def test_trilogy_query_values
assert_equal [1, 4, 2, 3, 3, 1], result.rows
end

def test_trilogy_set_server_option
client = new_tcp_client
create_test_table(client)

client.set_server_option(Trilogy::SET_SERVER_MULTI_STATEMENTS_ON)
client.set_server_option(Trilogy::SET_SERVER_MULTI_STATEMENTS_OFF)
end

def test_trilogy_set_server_option_with_invalid_option
client = new_tcp_client
create_test_table(client)

e = assert_raises do
client.set_server_option(42)
end

assert_instance_of(Trilogy::ProtocolError, e)
assert_match(/1047: Unknown command/, e.message)
end

def test_trilogy_set_server_option_multi_statement
# Start with multi_statement disabled, enable it during connection
client = new_tcp_client
create_test_table(client)

e = assert_raises do
client.query("INSERT INTO trilogy_test (int_test) VALUES ('4'); INSERT INTO trilogy_test (int_test) VALUES ('1')")
end

assert_instance_of(Trilogy::QueryError, e)
assert_match(/1064: You have an error in your SQL syntax/, e.message)

client.set_server_option(Trilogy::SET_SERVER_MULTI_STATEMENTS_ON)
client.query("INSERT INTO trilogy_test (int_test) VALUES ('4'); INSERT INTO trilogy_test (int_test) VALUES ('1')")
client.next_result while client.more_results_exist?

# Start with multi_statement enabled, disable it during connection
client = new_tcp_client(multi_statement: true)
create_test_table(client)
client.query("INSERT INTO trilogy_test (int_test) VALUES ('4'); INSERT INTO trilogy_test (int_test) VALUES ('1')")
client.next_result while client.more_results_exist?
client.set_server_option(Trilogy::SET_SERVER_MULTI_STATEMENTS_OFF)

e = assert_raises do
client.query("INSERT INTO trilogy_test (int_test) VALUES ('4'); INSERT INTO trilogy_test (int_test) VALUES ('1')")
end

assert_instance_of(Trilogy::QueryError, e)
assert_match(/1064: You have an error in your SQL syntax/, e.message)
end

def test_trilogy_query_result_object
client = new_tcp_client

Expand Down
10 changes: 10 additions & 0 deletions inc/trilogy/protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,16 @@ typedef enum {
#undef XX
} TRILOGY_SESSION_TRACK_TYPE_t;

#define TRILOGY_SET_SERVER_OPTION(XX) \
XX(TRILOGY_SET_SERVER_MULTI_STATEMENTS_ON, 0x00) \
XX(TRILOGY_SET_SERVER_MULTI_STATEMENTS_OFF, 0x01) \

typedef enum {
#define XX(name, code) name = code,
TRILOGY_SET_SERVER_OPTION(XX)
#undef XX
} TRILOGY_SET_SERVER_OPTION_TYPE_t;

/* trilogy_build_auth_packet - Build a handshake response (or authentication)
* packet.
*
Expand Down

0 comments on commit 610e1b7

Please sign in to comment.