From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: From: Vladislav Shpilevoy Subject: [PATCH 4/4] sql: move sql_request and xrow_decode_sql to xrow lib Date: Tue, 11 Dec 2018 00:40:41 +0300 Message-Id: In-Reply-To: References: In-Reply-To: References: To: tarantool-patches@freelists.org Cc: vdavydov.dev@gmail.com List-ID: All binary struct *_request are stored in xrow.h/.c together with their decoders. The only reason why xrow_decode_sql was implemented in execute.c was a dependency on struct sql_bind. Now xrow_decode_sql and struct sql_request operate only by MessagePack and some iproto constants and are moved to their true home. Follow up #3828 --- src/box/execute.c | 46 ---------------------------------------------- src/box/execute.h | 20 -------------------- src/box/xrow.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ src/box/xrow.h | 19 +++++++++++++++++++ 4 files changed, 65 insertions(+), 66 deletions(-) diff --git a/src/box/execute.c b/src/box/execute.c index 25a594d7d..7fff5fdff 100644 --- a/src/box/execute.c +++ b/src/box/execute.c @@ -228,52 +228,6 @@ sql_bind_list_decode(const char *data, struct sql_bind **out_bind) return bind_count; } -int -xrow_decode_sql(const struct xrow_header *row, struct sql_request *request) -{ - if (row->bodycnt == 0) { - diag_set(ClientError, ER_INVALID_MSGPACK, "missing request body"); - return 1; - } - assert(row->bodycnt == 1); - const char *data = (const char *) row->body[0].iov_base; - const char *end = data + row->body[0].iov_len; - assert((end - data) > 0); - - if (mp_typeof(*data) != MP_MAP || mp_check_map(data, end) > 0) { -error: - diag_set(ClientError, ER_INVALID_MSGPACK, "packet body"); - return -1; - } - - uint32_t map_size = mp_decode_map(&data); - request->sql_text = NULL; - request->bind = NULL; - for (uint32_t i = 0; i < map_size; ++i) { - uint8_t key = *data; - if (key != IPROTO_SQL_BIND && key != IPROTO_SQL_TEXT) { - mp_check(&data, end); /* skip the key */ - mp_check(&data, end); /* skip the value */ - continue; - } - const char *value = ++data; /* skip the key */ - if (mp_check(&data, end) != 0) /* check the value */ - goto error; - if (key == IPROTO_SQL_BIND) - request->bind = value; - else - request->sql_text = value; - } - if (request->sql_text == NULL) { - diag_set(ClientError, ER_MISSING_REQUEST_FIELD, - iproto_key_name(IPROTO_SQL_TEXT)); - return -1; - } - if (data != end) - goto error; - return 0; -} - /** * Serialize a single column of a result set row. * @param stmt Prepared and started statement. At least one diff --git a/src/box/execute.h b/src/box/execute.h index 025695ea9..9c1bc4f05 100644 --- a/src/box/execute.h +++ b/src/box/execute.h @@ -51,15 +51,6 @@ extern const char *sql_info_key_strs[]; struct obuf; struct region; struct sql_bind; -struct xrow_header; - -/** EXECUTE request. */ -struct sql_request { - /** SQL statement text. */ - const char *sql_text; - /** MessagePack array of parameters. */ - const char *bind; -}; /** Response on EXECUTE request. */ struct sql_response { @@ -123,17 +114,6 @@ sql_bind_list_decode(const char *data, struct sql_bind **out_bind); int sql_response_dump(struct sql_response *response, int *keys, struct obuf *out); -/** - * Parse the EXECUTE request. - * @param row Encoded data. - * @param[out] request Request to decode to. - * - * @retval 0 Sucess. - * @retval -1 Format or memory error. - */ -int -xrow_decode_sql(const struct xrow_header *row, struct sql_request *request); - /** * Prepare and execute an SQL statement. * @param sql SQL statement. diff --git a/src/box/xrow.c b/src/box/xrow.c index 76c6f8166..67019a68d 100644 --- a/src/box/xrow.c +++ b/src/box/xrow.c @@ -460,6 +460,52 @@ iproto_reply_select(struct obuf *buf, struct obuf_svp *svp, uint64_t sync, memcpy(pos + IPROTO_HEADER_LEN, &body, sizeof(body)); } +int +xrow_decode_sql(const struct xrow_header *row, struct sql_request *request) +{ + if (row->bodycnt == 0) { + diag_set(ClientError, ER_INVALID_MSGPACK, "missing request body"); + return 1; + } + assert(row->bodycnt == 1); + const char *data = (const char *) row->body[0].iov_base; + const char *end = data + row->body[0].iov_len; + assert((end - data) > 0); + + if (mp_typeof(*data) != MP_MAP || mp_check_map(data, end) > 0) { +error: + diag_set(ClientError, ER_INVALID_MSGPACK, "packet body"); + return -1; + } + + uint32_t map_size = mp_decode_map(&data); + request->sql_text = NULL; + request->bind = NULL; + for (uint32_t i = 0; i < map_size; ++i) { + uint8_t key = *data; + if (key != IPROTO_SQL_BIND && key != IPROTO_SQL_TEXT) { + mp_check(&data, end); /* skip the key */ + mp_check(&data, end); /* skip the value */ + continue; + } + const char *value = ++data; /* skip the key */ + if (mp_check(&data, end) != 0) /* check the value */ + goto error; + if (key == IPROTO_SQL_BIND) + request->bind = value; + else + request->sql_text = value; + } + if (request->sql_text == NULL) { + diag_set(ClientError, ER_MISSING_REQUEST_FIELD, + iproto_key_name(IPROTO_SQL_TEXT)); + return -1; + } + if (data != end) + goto error; + return 0; +} + void iproto_reply_sql(struct obuf *buf, struct obuf_svp *svp, uint64_t sync, uint32_t schema_version, int keys) diff --git a/src/box/xrow.h b/src/box/xrow.h index ca8d04d44..6bab0a1fd 100644 --- a/src/box/xrow.h +++ b/src/box/xrow.h @@ -466,6 +466,25 @@ int iproto_reply_error(struct obuf *out, const struct error *e, uint64_t sync, uint32_t schema_version); +/** EXECUTE request. */ +struct sql_request { + /** SQL statement text. */ + const char *sql_text; + /** MessagePack array of parameters. */ + const char *bind; +}; + +/** + * Parse the EXECUTE request. + * @param row Encoded data. + * @param[out] request Request to decode to. + * + * @retval 0 Sucess. + * @retval -1 Format or memory error. + */ +int +xrow_decode_sql(const struct xrow_header *row, struct sql_request *request); + /** * Write the SQL header. * @param buf Out buffer. -- 2.17.2 (Apple Git-113)