Skip to content

Commit

Permalink
Merge pull request #1138 from dolthub/fulghum/stored-proc-error-message
Browse files Browse the repository at this point in the history
Improving error message when a stored procedure isn't found and no DB is selected
  • Loading branch information
fulghum authored Jul 28, 2022
2 parents cd27ab8 + baa47ce commit 3625099
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
6 changes: 5 additions & 1 deletion sql/analyzer/stored_procedures.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,11 @@ func applyProceduresCall(ctx *sql.Context, a *Analyzer, call *plan.Call, scope *

procedure := scope.procedures.Get(ctx.GetCurrentDatabase(), call.Name, len(call.Params))
if procedure == nil {
return nil, transform.SameTree, sql.ErrStoredProcedureDoesNotExist.New(call.Name)
err := sql.ErrStoredProcedureDoesNotExist.New(call.Name)
if ctx.GetCurrentDatabase() == "" {
return nil, transform.SameTree, fmt.Errorf("%w; this might be because no database is selected", err)
}
return nil, transform.SameTree, err
}
if procedure.HasVariadicParameter() {
procedure = procedure.ExtendVariadic(ctx, len(call.Params))
Expand Down
44 changes: 44 additions & 0 deletions sql/analyzer/stored_procedures_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2022 Dolthub, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package analyzer

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/dolthub/go-mysql-server/memory"
"github.com/dolthub/go-mysql-server/sql"
"github.com/dolthub/go-mysql-server/sql/plan"
"github.com/dolthub/go-mysql-server/sql/transform"
)

func TestStoredProcedureNotFoundWithNoDatabaseSelected(t *testing.T) {
db := memory.NewDatabase("mydb")
analyzer := NewBuilder(sql.NewDatabaseProvider(db)).Build()
ctx := sql.NewContext(context.Background(), sql.WithSession(sql.NewBaseSession()))

call := plan.NewCall("non_existent_procedure", []sql.Expression{})
scope, err := loadStoredProcedures(ctx, analyzer, call, newScope(call), DefaultRuleSelector)
require.NoError(t, err)

node, identity, err := applyProceduresCall(ctx, analyzer, call, scope, DefaultRuleSelector)
assert.Nil(t, node)
assert.Equal(t, transform.SameTree, identity)
assert.Contains(t, err.Error(), "stored procedure \"non_existent_procedure\" does not exist")
assert.Contains(t, err.Error(), "this might be because no database is selected")
}

0 comments on commit 3625099

Please sign in to comment.