Skip to content

Commit

Permalink
sql: rework diag_set() in OP_Halt
Browse files Browse the repository at this point in the history
Before this patch it was possible to have an error code with wrong
error description. This patch fixes it.

Part of #4074
  • Loading branch information
ImeevMA committed Apr 12, 2019
1 parent c61874a commit 79e3d0e
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 49 deletions.
9 changes: 2 additions & 7 deletions src/box/sql/build.c
Expand Up @@ -1013,12 +1013,10 @@ vdbe_emit_fk_constraint_create(struct Parse *parse_context,
* Lets check that constraint with this name hasn't
* been created before.
*/
const char *error_msg =
tt_sprintf(tnt_errcode_desc(ER_CONSTRAINT_EXISTS), name_copy);
if (vdbe_emit_halt_with_presence_test(parse_context,
BOX_FK_CONSTRAINT_ID, 0,
constr_tuple_reg, 2,
ER_CONSTRAINT_EXISTS, error_msg,
ER_CONSTRAINT_EXISTS, name_copy,
false, OP_NoConflict) != 0)
return;
sqlVdbeAddOp2(vdbe, OP_Bool, 0, constr_tuple_reg + 3);
Expand Down Expand Up @@ -1400,13 +1398,10 @@ vdbe_emit_fk_constraint_drop(struct Parse *parse_context, char *constraint_name,
sqlVdbeAddOp4(vdbe, OP_String8, 0, key_reg, 0, constraint_name,
P4_DYNAMIC);
sqlVdbeAddOp2(vdbe, OP_Integer, child_id, key_reg + 1);
const char *error_msg =
tt_sprintf(tnt_errcode_desc(ER_NO_SUCH_CONSTRAINT),
constraint_name);
if (vdbe_emit_halt_with_presence_test(parse_context,
BOX_FK_CONSTRAINT_ID, 0,
key_reg, 2, ER_NO_SUCH_CONSTRAINT,
error_msg, false,
constraint_name, false,
OP_Found) != 0) {
sqlDbFree(parse_context->db, constraint_name);
return;
Expand Down
17 changes: 8 additions & 9 deletions src/box/sql/select.c
Expand Up @@ -2113,6 +2113,7 @@ computeLimitRegisters(Parse * pParse, Select * p, int iBreak)
0, 0,
wrong_limit_error,
P4_STATIC);
sqlVdbeChangeP5(v, ER_SQL_EXECUTE);

sqlVdbeResolveLabel(v, positive_limit_label);
sqlReleaseTempReg(pParse, r1);
Expand Down Expand Up @@ -2140,13 +2141,12 @@ computeLimitRegisters(Parse * pParse, Select * p, int iBreak)
sqlVdbeAddOp2(v, OP_Integer, 1, r1);
int no_err = sqlVdbeMakeLabel(v);
sqlVdbeAddOp3(v, OP_Eq, iLimit, no_err, r1);
const char *error =
"SQL error: Expression subquery could "
"be limited only with 1";
const char *error = "Expression subquery could "
"be limited only with 1";
sqlVdbeAddOp4(v, OP_Halt,
SQL_TARANTOOL_ERROR,
0, 0, error, P4_STATIC);
sqlVdbeChangeP5(v, ER_SQL_EXECUTE);
sqlVdbeChangeP5(v, ER_SQL);
sqlVdbeResolveLabel(v, no_err);
sqlReleaseTempReg(pParse, r1);

Expand Down Expand Up @@ -2176,6 +2176,7 @@ computeLimitRegisters(Parse * pParse, Select * p, int iBreak)
0, 0,
wrong_offset_error,
P4_STATIC);
sqlVdbeChangeP5(v, ER_SQL_EXECUTE);

sqlVdbeResolveLabel(v, positive_offset_label);
sqlReleaseTempReg(pParse, r1);
Expand Down Expand Up @@ -5443,11 +5444,9 @@ vdbe_code_raise_on_multiple_rows(struct Parse *parser, int limit_reg, int end_ma
int r1 = sqlGetTempReg(parser);
sqlVdbeAddOp2(v, OP_Integer, 0, r1);
sqlVdbeAddOp3(v, OP_Ne, r1, end_mark, limit_reg);
const char *error =
"SQL error: Expression subquery returned more than 1 row";
sqlVdbeAddOp4(v, OP_Halt, SQL_TARANTOOL_ERROR, 0, 0, error,
P4_STATIC);
sqlVdbeChangeP5(v, ER_SQL_EXECUTE);
const char *error = "Expression subquery returned more than 1 row";
sqlVdbeAddOp4(v, OP_Halt, SQL_TARANTOOL_ERROR, 0, 0, error, P4_STATIC);
sqlVdbeChangeP5(v, ER_SQL);
sqlReleaseTempReg(parser, r1);
}

Expand Down
12 changes: 4 additions & 8 deletions src/box/sql/trigger.c
Expand Up @@ -100,9 +100,6 @@ sql_trigger_begin(struct Parse *parse)
struct Vdbe *v = sqlGetVdbe(parse);
if (v != NULL)
sqlVdbeCountChanges(v);
const char *error_msg =
tt_sprintf(tnt_errcode_desc(ER_TRIGGER_EXISTS),
trigger_name);
char *name_copy = sqlDbStrDup(db, trigger_name);
if (name_copy == NULL)
goto trigger_cleanup;
Expand All @@ -113,7 +110,8 @@ sql_trigger_begin(struct Parse *parse)
if (vdbe_emit_halt_with_presence_test(parse, BOX_TRIGGER_ID, 0,
name_reg, 1,
ER_TRIGGER_EXISTS,
error_msg, (no_err != 0),
trigger_name,
(no_err != 0),
OP_NoConflict) != 0)
goto trigger_cleanup;
}
Expand Down Expand Up @@ -412,17 +410,15 @@ sql_drop_trigger(struct Parse *parser)

assert(name->nSrc == 1);
const char *trigger_name = name->a[0].zName;
const char *error_msg =
tt_sprintf(tnt_errcode_desc(ER_NO_SUCH_TRIGGER),
trigger_name);
char *name_copy = sqlDbStrDup(db, trigger_name);
if (name_copy == NULL)
goto drop_trigger_cleanup;
int name_reg = ++parser->nMem;
sqlVdbeAddOp4(v, OP_String8, 0, name_reg, 0, name_copy, P4_DYNAMIC);
if (vdbe_emit_halt_with_presence_test(parser, BOX_TRIGGER_ID, 0,
name_reg, 1, ER_NO_SUCH_TRIGGER,
error_msg, no_err, OP_Found) != 0)
trigger_name,
no_err, OP_Found) != 0)
goto drop_trigger_cleanup;

vdbe_code_drop_trigger(parser, trigger_name, true);
Expand Down
3 changes: 1 addition & 2 deletions src/box/sql/vdbe.c
Expand Up @@ -1073,8 +1073,7 @@ case OP_Halt: {
if (pOp->p4.z == NULL) {
assert(! diag_is_empty(diag_get()));
} else {
box_error_set(__FILE__, __LINE__, pOp->p5,
pOp->p4.z);
diag_set(ClientError, pOp->p5, pOp->p4.z);
}
} else if (pOp->p5 != 0) {
static const char * const azType[] = { "NOT NULL", "UNIQUE", "CHECK",
Expand Down
4 changes: 2 additions & 2 deletions test/sql-tap/e_select1.test.lua
Expand Up @@ -2172,7 +2172,7 @@ for _, val in ipairs({
"e_select-9.2."..tn,
select,
{
1, "Only positive integers are allowed in the LIMIT clause"})
1, "Failed to execute SQL statement: Only positive integers are allowed in the LIMIT clause"})
end

-- EVIDENCE-OF: R-03014-26414 If the LIMIT expression evaluates to a
Expand Down Expand Up @@ -2226,7 +2226,7 @@ for _, val in ipairs({
test:do_catchsql_test(
"e_select-9.7."..tn,
select, {
1, "Only positive integers are allowed in the OFFSET clause"
1, "Failed to execute SQL statement: Only positive integers are allowed in the OFFSET clause"
})

end
Expand Down
26 changes: 13 additions & 13 deletions test/sql-tap/limit.test.lua
Expand Up @@ -84,7 +84,7 @@ test:do_catchsql_test(
SELECT x FROM t1 ORDER BY x+1 LIMIT 5 OFFSET -2
]], {
-- <limit-1.2.13>
1 ,"Only positive integers are allowed in the OFFSET clause"
1 ,"Failed to execute SQL statement: Only positive integers are allowed in the OFFSET clause"
-- </limit-1.2.13>
})

Expand All @@ -94,7 +94,7 @@ test:do_catchsql_test(
SELECT x FROM t1 ORDER BY x+1 LIMIT 2, -5
]], {
-- <limit-1.2.4>
1, "Only positive integers are allowed in the LIMIT clause"
1, "Failed to execute SQL statement: Only positive integers are allowed in the LIMIT clause"
-- </limit-1.2.4>
})

Expand All @@ -115,7 +115,7 @@ test:do_catchsql_test(
SELECT x FROM t1 ORDER BY x+1 LIMIT -2, 5
]], {
-- <limit-1.2.6>
1, "Only positive integers are allowed in the OFFSET clause"
1, "Failed to execute SQL statement: Only positive integers are allowed in the OFFSET clause"
-- </limit-1.2.6>
})

Expand All @@ -135,7 +135,7 @@ test:do_catchsql_test(
SELECT x FROM t1 ORDER BY x+1 LIMIT -2, -5
]], {
-- <limit-1.2.8>
1, "Only positive integers are allowed in the LIMIT clause"
1, "Failed to execute SQL statement: Only positive integers are allowed in the LIMIT clause"
-- </limit-1.2.8>
})

Expand Down Expand Up @@ -384,7 +384,7 @@ test:do_catchsql_test(
SELECT * FROM t6 LIMIT -1 OFFSET -1;
]], {
-- <limit-6.2>
1, "Only positive integers are allowed in the LIMIT clause"
1, "Failed to execute SQL statement: Only positive integers are allowed in the LIMIT clause"
-- </limit-6.2>
})

Expand All @@ -394,7 +394,7 @@ test:do_catchsql_test(
SELECT * FROM t6 LIMIT 2 OFFSET -123;
]], {
-- <limit-6.3>
1, "Only positive integers are allowed in the OFFSET clause"
1, "Failed to execute SQL statement: Only positive integers are allowed in the OFFSET clause"
-- </limit-6.3>
})

Expand All @@ -414,7 +414,7 @@ test:do_catchsql_test(
SELECT * FROM t6 LIMIT -432 OFFSET 2;
]], {
-- <limit-6.4>
1, "Only positive integers are allowed in the LIMIT clause"
1, "Failed to execute SQL statement: Only positive integers are allowed in the LIMIT clause"
-- </limit-6.4>
})

Expand All @@ -434,7 +434,7 @@ test:do_catchsql_test(
SELECT * FROM t6 LIMIT -1
]], {
-- <limit-6.5>
1, "Only positive integers are allowed in the LIMIT clause"
1, "Failed to execute SQL statement: Only positive integers are allowed in the LIMIT clause"
-- </limit-6.5>
})

Expand All @@ -454,7 +454,7 @@ test:do_catchsql_test(
SELECT * FROM t6 LIMIT -1 OFFSET 1
]], {
-- <limit-6.6>
1, "Only positive integers are allowed in the LIMIT clause"
1, "Failed to execute SQL statement: Only positive integers are allowed in the LIMIT clause"
-- </limit-6.6>
})

Expand Down Expand Up @@ -734,7 +734,7 @@ test:do_test(
return test:catchsql("SELECT x FROM t1 WHERE x<10 LIMIT "..limit)
end, {
-- <limit-10.4>
1, "Only positive integers are allowed in the LIMIT clause"
1, "Failed to execute SQL statement: Only positive integers are allowed in the LIMIT clause"
-- </limit-10.4>
})

Expand All @@ -745,7 +745,7 @@ test:do_test(
return test:catchsql("SELECT x FROM t1 WHERE x<10 LIMIT "..limit)
end, {
-- <limit-10.5>
1, "Only positive integers are allowed in the LIMIT clause"
1, "Failed to execute SQL statement: Only positive integers are allowed in the LIMIT clause"
-- </limit-10.5>
})

Expand Down Expand Up @@ -1320,7 +1320,7 @@ test:do_catchsql_test(
SELECT 123 LIMIT -1 OFFSET 0
]], {
-- <limit-14.6.1>
1, "Only positive integers are allowed in the LIMIT clause"
1, "Failed to execute SQL statement: Only positive integers are allowed in the LIMIT clause"
-- </limit-14.6.1>
})

Expand All @@ -1340,7 +1340,7 @@ test:do_catchsql_test(
SELECT 123 LIMIT -1 OFFSET 1
]], {
-- <limit-14.7.1>
1, "Only positive integers are allowed in the LIMIT clause"
1, "Failed to execute SQL statement: Only positive integers are allowed in the LIMIT clause"
-- </limit-14.7.1>
})

Expand Down
4 changes: 2 additions & 2 deletions test/sql-tap/select4.test.lua
Expand Up @@ -990,7 +990,7 @@ test:do_catchsql_test(
SELECT DISTINCT log FROM t1 ORDER BY log LIMIT -1
]], {
-- <select4-10.4.1>
1,"Only positive integers are allowed in the LIMIT clause"
1,"Failed to execute SQL statement: Only positive integers are allowed in the LIMIT clause"
-- </select4-10.4.1>
})
test:do_execsql_test(
Expand All @@ -1009,7 +1009,7 @@ test:do_catchsql_test(
SELECT DISTINCT log FROM t1 ORDER BY log LIMIT -1 OFFSET 2
]], {
-- <select4-10.5.1>
1,"Only positive integers are allowed in the LIMIT clause"
1,"Failed to execute SQL statement: Only positive integers are allowed in the LIMIT clause"
-- </select4-10.5.1>
})
test:do_execsql_test(
Expand Down
18 changes: 12 additions & 6 deletions test/sql/iproto.result
Expand Up @@ -164,15 +164,18 @@ cn:execute('select * from test limit ?', {2})
...
cn:execute('select * from test limit ?', {-2})
---
- error: Only positive integers are allowed in the LIMIT clause
- error: 'Failed to execute SQL statement: Only positive integers are allowed in the
LIMIT clause'
...
cn:execute('select * from test limit ?', {2.7})
---
- error: Only positive integers are allowed in the LIMIT clause
- error: 'Failed to execute SQL statement: Only positive integers are allowed in the
LIMIT clause'
...
cn:execute('select * from test limit ?', {'Hello'})
---
- error: Only positive integers are allowed in the LIMIT clause
- error: 'Failed to execute SQL statement: Only positive integers are allowed in the
LIMIT clause'
...
cn:execute('select * from test limit 1 offset ?', {2})
---
Expand All @@ -188,15 +191,18 @@ cn:execute('select * from test limit 1 offset ?', {2})
...
cn:execute('select * from test limit 1 offset ?', {-2})
---
- error: Only positive integers are allowed in the OFFSET clause
- error: 'Failed to execute SQL statement: Only positive integers are allowed in the
OFFSET clause'
...
cn:execute('select * from test limit 1 offset ?', {2.7})
---
- error: Only positive integers are allowed in the OFFSET clause
- error: 'Failed to execute SQL statement: Only positive integers are allowed in the
OFFSET clause'
...
cn:execute('select * from test limit 1 offset ?', {'Hello'})
---
- error: Only positive integers are allowed in the OFFSET clause
- error: 'Failed to execute SQL statement: Only positive integers are allowed in the
OFFSET clause'
...
-- gh-2608 SQL iproto DDL
cn:execute('create table test2(id int primary key, a int, b int, c int)')
Expand Down

0 comments on commit 79e3d0e

Please sign in to comment.