Skip to content

Commit 612f4ab

Browse files
authored
Merge pull request trilogy-libraries#264 from byroot/abandon_results
Implement Trilogy#abandon_results!
2 parents 8d04c04 + 0f7c88f commit 612f4ab

File tree

4 files changed

+43
-0
lines changed

4 files changed

+43
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
99

1010
- Support `caching_sha2_password` over TCP without TLS by requesting the server RSA public key when needed. #26
1111
- Now raise an explicit error when a single connection is being used concurrently by multiple threads or fibers. #226.
12+
- `Trilogy#abandon_results!` as an optimized alternative to `client.next_result while client.more_results_exist?`.
1213

1314
### Fixed
1415

contrib/ruby/ext/trilogy-ruby/cext.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,6 +1092,31 @@ static VALUE rb_trilogy_more_results_exist(VALUE self)
10921092
}
10931093
}
10941094

1095+
static VALUE rb_trilogy_abandon_results(VALUE self)
1096+
{
1097+
struct trilogy_ctx *ctx = get_open_ctx(self);
1098+
1099+
long count = 0;
1100+
while (ctx->conn.server_status & TRILOGY_SERVER_STATUS_MORE_RESULTS_EXISTS) {
1101+
count++;
1102+
int rc = trilogy_drain_results(&ctx->conn);
1103+
while (rc == TRILOGY_AGAIN) {
1104+
rc = trilogy_sock_wait_read(ctx->conn.socket);
1105+
if (rc != TRILOGY_OK) {
1106+
handle_trilogy_error(ctx, rc, "trilogy_sock_wait_read");
1107+
}
1108+
1109+
rc = trilogy_drain_results(&ctx->conn);
1110+
}
1111+
1112+
if (rc != TRILOGY_OK) {
1113+
handle_trilogy_error(ctx, rc, "trilogy_drain_results");
1114+
}
1115+
}
1116+
1117+
return LONG2NUM(count);
1118+
}
1119+
10951120
static VALUE rb_trilogy_query(VALUE self, VALUE query)
10961121
{
10971122
struct trilogy_ctx *ctx = get_open_ctx(self);
@@ -1356,6 +1381,7 @@ RUBY_FUNC_EXPORTED void Init_cext(void)
13561381
rb_define_method(Trilogy, "server_version", rb_trilogy_server_version, 0);
13571382
rb_define_method(Trilogy, "more_results_exist?", rb_trilogy_more_results_exist, 0);
13581383
rb_define_method(Trilogy, "next_result", rb_trilogy_next_result, 0);
1384+
rb_define_method(Trilogy, "abandon_results!", rb_trilogy_abandon_results, 0);
13591385
rb_define_method(Trilogy, "set_server_option", rb_trilogy_set_server_option, 1);
13601386
rb_define_const(Trilogy, "TLS_VERSION_10", INT2NUM(TRILOGY_TLS_VERSION_10));
13611387
rb_define_const(Trilogy, "TLS_VERSION_11", INT2NUM(TRILOGY_TLS_VERSION_11));

contrib/ruby/test/client_test.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,21 @@ def test_trilogy_multiple_results
210210
refute client.more_results_exist?
211211
end
212212

213+
def test_trilogy_abandon_results
214+
client = new_tcp_client(multi_statement: true)
215+
create_test_table(client)
216+
217+
client.query("INSERT INTO trilogy_test (int_test) VALUES ('4')")
218+
client.query("INSERT INTO trilogy_test (int_test) VALUES ('3')")
219+
client.query("INSERT INTO trilogy_test (int_test) VALUES ('1')")
220+
221+
results = []
222+
223+
assert_equal [[1, 4]], client.query("SELECT id, int_test FROM trilogy_test WHERE id = 1; SELECT id, int_test FROM trilogy_test WHERE id IN (2, 3); SELECT id, int_test FROM trilogy_test").to_a
224+
assert_equal 2, client.abandon_results!
225+
assert_equal [[2]], client.query("SELECT 2").to_a
226+
end
227+
213228
def test_trilogy_multiple_results_doesnt_allow_multi_statement_queries
214229
client = new_tcp_client
215230
create_test_table(client)

src/client.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,6 +1018,7 @@ int trilogy_drain_results(trilogy_conn_t *conn)
10181018
}
10191019

10201020
if (current_packet_type(conn) == TRILOGY_PACKET_EOF && conn->packet_buffer.len < 9) {
1021+
read_eof_packet(conn);
10211022
return TRILOGY_OK;
10221023
}
10231024
}

0 commit comments

Comments
 (0)