Skip to content

Commit

Permalink
Support set option in blocking API
Browse files Browse the repository at this point in the history
  • Loading branch information
paarthmadan committed Mar 15, 2023
1 parent 610e1b7 commit a06f6bf
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
16 changes: 16 additions & 0 deletions inc/trilogy/blocking.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,22 @@ int trilogy_connect_sock(trilogy_conn_t *conn, trilogy_sock_t *sock);
*/
int trilogy_change_db(trilogy_conn_t *conn, const char *name, size_t name_len);

/* trilogy_set_option - Set server options for the connection.
*
* conn - A connected trilogy_conn_t pointer. Using a disconnected
* trilogy_conn_t is undefined.
* option - The server option to set. See: TRILOGY_SET_SERVER_OPTION_TYPE_t;
*
* Return values
* TRILOGY_OK - The change db command completed successfully.
* TRILOGY_ERR - The server returned an error.
* TRILOGY_SYSERR - A system error occurred, check errno.
* TRILOGY_CLOSED_CONNECTION - The connection is closed.
* TRILOGY_PROTOCOL_VIOLATION - An error occurred while processing a network
* packet.
*/
int trilogy_set_option(trilogy_conn_t *conn, const uint16_t option);

/* trilogy_query - Send and execute a query.
*
* conn - A connected trilogy_conn_t pointer. Using a disconnected
Expand Down
23 changes: 23 additions & 0 deletions src/blocking.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,29 @@ int trilogy_change_db(trilogy_conn_t *conn, const char *name, size_t name_len)
}
}

int trilogy_set_option(trilogy_conn_t *conn, const uint16_t option)
{
int rc = trilogy_set_option_send(conn, option);

if (rc == TRILOGY_AGAIN) {
rc = flush_full(conn);
}

if (rc < 0) {
return rc;
}

while (1) {
rc = trilogy_set_option_recv(conn);

if (rc != TRILOGY_AGAIN) {
return rc;
}

CHECKED(trilogy_sock_wait_read(conn->socket));
}
}

int trilogy_ping(trilogy_conn_t *conn)
{
int rc = trilogy_ping_send(conn);
Expand Down
19 changes: 19 additions & 0 deletions test/blocking_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,24 @@ TEST test_blocking_change_db()
PASS();
}

TEST test_blocking_set_option()
{
trilogy_conn_t conn;

connect_conn(&conn);

const uint16_t option = 1;

int err = trilogy_set_option(&conn, option);
ASSERT_OK(err);

err = trilogy_close(&conn);
ASSERT_OK(err);

trilogy_free(&conn);
PASS();
}

TEST test_blocking_ping()
{
trilogy_conn_t conn;
Expand Down Expand Up @@ -151,6 +169,7 @@ int blocking_test()
{
RUN_TEST(test_blocking_connect);
RUN_TEST(test_blocking_change_db);
RUN_TEST(test_blocking_set_option);
RUN_TEST(test_blocking_ping);
RUN_TEST(test_blocking_query);
RUN_TEST(test_blocking_query_error);
Expand Down

0 comments on commit a06f6bf

Please sign in to comment.