Skip to content

Commit

Permalink
sql: set errors in VDBE using diag_set()
Browse files Browse the repository at this point in the history
After this patch, all errors in VDBE will be set using diag_set().

Closes #4074
  • Loading branch information
ImeevMA committed Jun 3, 2019
1 parent 41c7c10 commit 1bbdbe4
Show file tree
Hide file tree
Showing 12 changed files with 153 additions and 323 deletions.
23 changes: 4 additions & 19 deletions src/box/execute.c
Expand Up @@ -410,8 +410,7 @@ port_sql_dump_msgpack(struct port *port, struct obuf *out)
* @retval -1 Error.
*/
static inline int
sql_execute(sql *db, struct sql_stmt *stmt, struct port *port,
struct region *region)
sql_execute(struct sql_stmt *stmt, struct port *port, struct region *region)
{
int rc, column_count = sql_column_count(stmt);
if (column_count > 0) {
Expand All @@ -427,15 +426,8 @@ sql_execute(sql *db, struct sql_stmt *stmt, struct port *port,
rc = sql_step(stmt);
assert(rc != SQL_ROW && rc != SQL_OK);
}
if (rc != SQL_DONE) {
if (db->errCode != SQL_TARANTOOL_ERROR) {
const char *err = (char *)sql_value_text(db->pErr);
if (err == NULL)
err = sqlErrStr(db->errCode);
diag_set(ClientError, ER_SQL_EXECUTE, err);
}
if (rc != SQL_DONE)
return -1;
}
return 0;
}

Expand All @@ -446,19 +438,12 @@ sql_prepare_and_execute(const char *sql, int len, const struct sql_bind *bind,
{
struct sql_stmt *stmt;
struct sql *db = sql_get();
if (sql_prepare_v2(db, sql, len, &stmt, NULL) != SQL_OK) {
if (db->errCode != SQL_TARANTOOL_ERROR) {
const char *err = (char *)sql_value_text(db->pErr);
if (err == NULL)
err = sqlErrStr(db->errCode);
diag_set(ClientError, ER_SQL_EXECUTE, err);
}
if (sql_prepare_v2(db, sql, len, &stmt, NULL) != SQL_OK)
return -1;
}
assert(stmt != NULL);
port_sql_create(port, stmt);
if (sql_bind(stmt, bind, bind_count) == 0 &&
sql_execute(db, stmt, port, region) == 0)
sql_execute(stmt, port, region) == 0)
return 0;
port_destroy(port);
return -1;
Expand Down
37 changes: 16 additions & 21 deletions src/box/sql.c
Expand Up @@ -325,21 +325,19 @@ int tarantoolsqlMovetoUnpacked(BtCursor *pCur, UnpackedRecord *pIdxKey,
*
* @retval SQL_OK
*/
int tarantoolsqlEphemeralCount(struct BtCursor *pCur, i64 *pnEntry)
int64_t
tarantoolsqlEphemeralCount(struct BtCursor *pCur)
{
assert(pCur->curFlags & BTCF_TEphemCursor);

struct index *primary_index = space_index(pCur->space, 0 /* PK */);
*pnEntry = index_count(primary_index, pCur->iter_type, NULL, 0);
return SQL_OK;
return index_count(primary_index, pCur->iter_type, NULL, 0);
}

int tarantoolsqlCount(BtCursor *pCur, i64 *pnEntry)
int64_t
tarantoolsqlCount(struct BtCursor *pCur)
{
assert(pCur->curFlags & BTCF_TaCursor);

*pnEntry = index_count(pCur->index, pCur->iter_type, NULL, 0);
return SQL_OK;
return index_count(pCur->index, pCur->iter_type, NULL, 0);
}

struct space *
Expand Down Expand Up @@ -621,12 +619,12 @@ int tarantoolsqlRenameTrigger(const char *trig_name,
char *key_begin = (char*) region_alloc(&fiber()->gc, key_len);
if (key_begin == NULL) {
diag_set(OutOfMemory, key_len, "region_alloc", "key_begin");
return SQL_TARANTOOL_ERROR;
return -1;
}
char *key = mp_encode_array(key_begin, 1);
key = mp_encode_str(key, trig_name, trig_name_len);
if (box_index_get(BOX_TRIGGER_ID, 0, key_begin, key, &tuple) != 0)
return SQL_TARANTOOL_ERROR;
return -1;
assert(tuple != NULL);
assert(tuple_field_count(tuple) == 3);
const char *field = tuple_field(tuple, BOX_TRIGGER_FIELD_SPACE_ID);
Expand All @@ -645,7 +643,7 @@ int tarantoolsqlRenameTrigger(const char *trig_name,
if (trigger_stmt == NULL) {
diag_set(OutOfMemory, trigger_stmt_len + 1, "region_alloc",
"trigger_stmt");
return SQL_TARANTOOL_ERROR;
return -1;
}
memcpy(trigger_stmt, trigger_stmt_old, trigger_stmt_len);
trigger_stmt[trigger_stmt_len] = '\0';
Expand All @@ -662,7 +660,7 @@ int tarantoolsqlRenameTrigger(const char *trig_name,
char *new_tuple = (char*)region_alloc(&fiber()->gc, key_len);
if (new_tuple == NULL) {
diag_set(OutOfMemory, key_len, "region_alloc", "new_tuple");
return SQL_TARANTOOL_ERROR;
return -1;
}
char *new_tuple_end = mp_encode_array(new_tuple, 3);
new_tuple_end = mp_encode_str(new_tuple_end, trig_name, trig_name_len);
Expand All @@ -672,15 +670,12 @@ int tarantoolsqlRenameTrigger(const char *trig_name,
new_tuple_end = mp_encode_str(new_tuple_end, trigger_stmt,
trigger_stmt_new_len);

if (box_replace(BOX_TRIGGER_ID, new_tuple, new_tuple_end, NULL) != 0)
return SQL_TARANTOOL_ERROR;
else
return SQL_OK;
return box_replace(BOX_TRIGGER_ID, new_tuple, new_tuple_end, NULL);

rename_fail:
diag_set(ClientError, ER_SQL_EXECUTE, "can't modify name of space "
"created not via SQL facilities");
return SQL_TARANTOOL_ERROR;
return -1;
}

int
Expand All @@ -695,7 +690,7 @@ sql_rename_table(uint32_t space_id, const char *new_name)
char *raw = (char *) region_alloc(region, size);
if (raw == NULL) {
diag_set(OutOfMemory, size, "region_alloc", "raw");
return SQL_TARANTOOL_ERROR;
return -1;
}
/* Encode key. */
char *pos = mp_encode_array(raw, 1);
Expand All @@ -709,7 +704,7 @@ sql_rename_table(uint32_t space_id, const char *new_name)
pos = mp_encode_uint(pos, BOX_SPACE_FIELD_NAME);
pos = mp_encode_str(pos, new_name, name_len);
if (box_update(BOX_SPACE_ID, 0, raw, ops, ops, pos, 0, NULL) != 0)
return SQL_TARANTOOL_ERROR;
return -1;
return 0;
}

Expand Down Expand Up @@ -834,8 +829,8 @@ tarantoolsqlIncrementMaxid(uint64_t *space_max_id)
request.space_id = space_schema->def->id;
if (box_process_rw(&request, space_schema, &res) != 0 || res == NULL ||
tuple_field_u64(res, 1, space_max_id) != 0)
return SQL_TARANTOOL_ERROR;
return SQL_OK;
return -1;
return 0;
}

/*
Expand Down
13 changes: 5 additions & 8 deletions src/box/sql/analyze.c
Expand Up @@ -1691,7 +1691,7 @@ sql_analysis_load(struct sql *db)
assert(stat_space != NULL);
ssize_t index_count = box_index_len(stat_space->def->id, 0);
if (index_count < 0)
return SQL_TARANTOOL_ERROR;
return -1;
if (box_txn_begin() != 0)
goto fail;
size_t stats_size = index_count * sizeof(struct index_stat);
Expand All @@ -1712,7 +1712,7 @@ sql_analysis_load(struct sql *db)
goto fail;
if (info.index_count == 0) {
box_txn_commit();
return SQL_OK;
return 0;
}
/*
* This query is used to allocate enough memory for
Expand Down Expand Up @@ -1768,12 +1768,9 @@ sql_analysis_load(struct sql *db)
*/
const char *order_query = "SELECT \"tbl\",\"idx\" FROM "
"\"_sql_stat4\" GROUP BY \"tbl\",\"idx\"";
if (load_stat_to_index(db, order_query, heap_stats) != 0)
goto fail;
if (box_txn_commit() != 0)
return SQL_TARANTOOL_ERROR;
return SQL_OK;
if (load_stat_to_index(db, order_query, heap_stats) == 0)
return box_txn_commit();
fail:
box_txn_rollback();
return SQL_TARANTOOL_ERROR;
return -1;
}
3 changes: 2 additions & 1 deletion src/box/sql/sqlInt.h
Expand Up @@ -4591,7 +4591,8 @@ sql_index_tuple_size(struct space *space, struct index *idx);
* samples[] arrays.
*
* @param db Database handler.
* @retval sql_OK on success, smth else otherwise.
* @retval 0 Success.
* @retval -1 Error.
*/
int
sql_analysis_load(struct sql *db);
Expand Down
9 changes: 6 additions & 3 deletions src/box/sql/tarantoolInt.h
Expand Up @@ -34,7 +34,8 @@ int tarantoolsqlNext(BtCursor * pCur, int *pRes);
int tarantoolsqlPrevious(BtCursor * pCur, int *pRes);
int tarantoolsqlMovetoUnpacked(BtCursor * pCur, UnpackedRecord * pIdxKey,
int *pRes);
int tarantoolsqlCount(BtCursor * pCur, i64 * pnEntry);
int64_t
tarantoolsqlCount(struct BtCursor *pCur);
int tarantoolsqlInsert(struct space *space, const char *tuple,
const char *tuple_end);
int tarantoolsqlReplace(struct space *space, const char *tuple,
Expand Down Expand Up @@ -62,7 +63,8 @@ int tarantoolsqlClearTable(struct space *space, uint32_t *tuple_count);
* @param space_id Table's space identifier.
* @param new_name new name of table
*
* @retval 0 on success, SQL_TARANTOOL_ERROR otherwise.
* @retval 0 Success.
* @retval -1 Error.
*/
int
sql_rename_table(uint32_t space_id, const char *new_name);
Expand Down Expand Up @@ -100,7 +102,8 @@ sql_ephemeral_space_create(uint32_t filed_count, struct sql_key_info *key_info);
int tarantoolsqlEphemeralInsert(struct space *space, const char *tuple,
const char *tuple_end);
int tarantoolsqlEphemeralDelete(BtCursor * pCur);
int tarantoolsqlEphemeralCount(BtCursor * pCur, i64 * pnEntry);
int64_t
tarantoolsqlEphemeralCount(struct BtCursor *pCur);
int tarantoolsqlEphemeralDrop(BtCursor * pCur);
int tarantoolsqlEphemeralClearTable(BtCursor * pCur);

Expand Down

0 comments on commit 1bbdbe4

Please sign in to comment.