@@ -40,7 +40,7 @@ const (
4040
4141type procCtx struct {
4242 s * scope
43- handler * plan.DeclareHandler
43+ handlers [] * plan.DeclareHandler
4444 conditions map [string ]* plan.DeclareCondition
4545 vars map [string ]scopeColumn
4646 cursors map [string ]struct {}
@@ -119,15 +119,11 @@ func (p *procCtx) HasCursor(name string) bool {
119119
120120func (p * procCtx ) AddHandler (h * plan.DeclareHandler ) {
121121 p .NewState (dsHandler )
122- if p .handler != nil {
123- err := sql .ErrDeclareHandlerDuplicate .New ()
124- p .s .b .handleErr (err )
125- }
126- p .handler = h
122+ p .handlers = append (p .handlers , h )
127123}
128124
129125func (p * procCtx ) HasHandler (name string ) bool {
130- return p .handler != nil
126+ return p .handlers != nil
131127}
132128
133129func (p * procCtx ) AddLabel (label string , isLoop bool ) {
@@ -347,33 +343,46 @@ func (b *Builder) buildDeclareCursor(inScope *scope, d *ast.Declare) (outScope *
347343func (b * Builder ) buildDeclareHandler (inScope * scope , d * ast.Declare , query string ) (outScope * scope ) {
348344 outScope = inScope .push ()
349345 dHandler := d .Handler
350- //TODO: support other condition values besides NOT FOUND
351- if len (dHandler .ConditionValues ) != 1 || dHandler .ConditionValues [0 ].ValueType != ast .DeclareHandlerCondition_NotFound {
346+ if len (dHandler .ConditionValues ) != 1 {
352347 err := sql .ErrUnsupportedSyntax .New (ast .String (d ))
353348 b .handleErr (err )
354349 }
350+
351+ var cond expression.HandlerCondition
352+
353+ switch dHandler .ConditionValues [0 ].ValueType {
354+ case ast .DeclareHandlerCondition_NotFound :
355+ cond = expression.HandlerCondition {Type : expression .HandlerConditionNotFound }
356+ case ast .DeclareHandlerCondition_SqlException :
357+ cond = expression.HandlerCondition {Type : expression .HandlerConditionSqlException }
358+ default :
359+ err := sql .ErrUnsupportedSyntax .New (ast .String (d ))
360+ b .handleErr (err )
361+ }
362+
355363 stmtScope := b .build (inScope , dHandler .Statement , query )
356364
357- var action plan .DeclareHandlerAction
365+ var action expression .DeclareHandlerAction
358366 switch dHandler .Action {
359367 case ast .DeclareHandlerAction_Continue :
360- action = plan .DeclareHandlerAction_Continue
368+ action = expression .DeclareHandlerAction_Continue
361369 case ast .DeclareHandlerAction_Exit :
362- action = plan .DeclareHandlerAction_Exit
370+ action = expression .DeclareHandlerAction_Exit
363371 case ast .DeclareHandlerAction_Undo :
364- action = plan .DeclareHandlerAction_Undo
372+ action = expression .DeclareHandlerAction_Undo
365373 default :
366374 err := fmt .Errorf ("unknown DECLARE ... HANDLER action: %v" , dHandler .Action )
367375 b .handleErr (err )
368376 }
369- if action == plan .DeclareHandlerAction_Undo {
377+ if action == expression .DeclareHandlerAction_Undo {
370378 err := sql .ErrDeclareHandlerUndo .New ()
371379 b .handleErr (err )
372380 }
373381
374382 handler := & plan.DeclareHandler {
375383 Action : action ,
376384 Statement : stmtScope .node ,
385+ Condition : cond ,
377386 }
378387
379388 inScope .proc .AddHandler (handler )
0 commit comments