Skip to content

Commit fae1af9

Browse files
author
James Cor
committed
declare
1 parent b0e0452 commit fae1af9

File tree

3 files changed

+52
-16
lines changed

3 files changed

+52
-16
lines changed

enginetest/memory_engine_test.go

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -201,28 +201,55 @@ func TestSingleScript(t *testing.T) {
201201
//t.Skip()
202202
var scripts = []queries.ScriptTest{
203203
{
204-
Name: "Subquery on SET user variable captures parameter",
204+
Name: "DECLARE CONDITION",
205205
SetUpScript: []string{
206-
`
207-
CREATE PROCEDURE p1(x VARCHAR(20))
208-
BEGIN
209-
SET @randomvar = (SELECT LENGTH(x));
210-
SELECT @randomvar;
206+
`CREATE PROCEDURE p1(x INT)
207+
BEGIN
208+
DECLARE specialty CONDITION FOR SQLSTATE '45000';
209+
DECLARE specialty2 CONDITION FOR SQLSTATE '02000';
210+
IF x = 0 THEN
211+
SIGNAL SQLSTATE '01000';
212+
ELSEIF x = 1 THEN
213+
SIGNAL SQLSTATE '45000'
214+
SET MESSAGE_TEXT = 'A custom error occurred 1';
215+
ELSEIF x = 2 THEN
216+
SIGNAL specialty
217+
SET MESSAGE_TEXT = 'A custom error occurred 2', MYSQL_ERRNO = 1002;
218+
ELSEIF x = 3 THEN
219+
SIGNAL specialty;
220+
ELSEIF x = 4 THEN
221+
SIGNAL specialty2;
222+
ELSE
223+
SIGNAL SQLSTATE '01000'
224+
SET MESSAGE_TEXT = 'A warning occurred', MYSQL_ERRNO = 1000;
225+
SIGNAL SQLSTATE '45000'
226+
SET MESSAGE_TEXT = 'An error occurred', MYSQL_ERRNO = 1001;
227+
END IF;
228+
BEGIN
229+
DECLARE specialty3 CONDITION FOR SQLSTATE '45000';
230+
END;
211231
END;`,
212232
},
213233
Assertions: []queries.ScriptTestAssertion{
214234
{
215-
SkipResultCheckOnServerEngine: true, // the user var has null type, which returns nil value over the wire.
216-
Query: "CALL p1('hi')",
217-
Expected: []sql.Row{
218-
{int64(2)},
219-
},
235+
Query: "CALL p1(0)",
236+
ExpectedErrStr: "warnings not yet implemented",
220237
},
221238
{
222-
Query: "CALL p1('hello')",
223-
Expected: []sql.Row{
224-
{int64(5)},
225-
},
239+
Query: "CALL p1(1)",
240+
ExpectedErrStr: "A custom error occurred 1 (errno 1644) (sqlstate 45000)",
241+
},
242+
{
243+
Query: "CALL p1(2)",
244+
ExpectedErrStr: "A custom error occurred 2 (errno 1002) (sqlstate 45000)",
245+
},
246+
{
247+
Query: "CALL p1(3)",
248+
ExpectedErrStr: "Unhandled user-defined exception condition (errno 1644) (sqlstate 45000)",
249+
},
250+
{
251+
Query: "CALL p1(4)",
252+
ExpectedErrStr: "Unhandled user-defined not found condition (errno 1643) (sqlstate 02000)",
226253
},
227254
},
228255
},

enginetest/queries/procedure_queries.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -989,7 +989,9 @@ END;`,
989989
},
990990
{
991991
Query: "CALL p1(@x);",
992-
Expected: []sql.Row{{}},
992+
Expected: []sql.Row{
993+
{types.NewOkResult(0)},
994+
},
993995
},
994996
{
995997
Query: "SELECT @x;",

sql/procedures/interpreter_logic.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,13 @@ func replaceVariablesInExpr(stack *InterpreterStack, expr ast.SQLNode) (ast.SQLN
142142
}
143143
e.Into = newExpr.(*ast.Into)
144144
}
145+
if e.Where != nil {
146+
newExpr, err := replaceVariablesInExpr(stack, e.Where.Expr)
147+
if err != nil {
148+
return nil, err
149+
}
150+
e.Where.Expr = newExpr.(ast.Expr)
151+
}
145152
case *ast.Subquery:
146153
newExpr, err := replaceVariablesInExpr(stack, e.Select)
147154
if err != nil {

0 commit comments

Comments
 (0)