Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding mapping to error code 1049 for ErrDatabaseNotFound errors #2511

Merged
merged 1 commit into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion server/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,12 @@ func (h *Handler) NewConnection(c *mysql.Conn) {
}

func (h *Handler) ComInitDB(c *mysql.Conn, schemaName string) error {
return h.sm.SetDB(c, schemaName)
err := h.sm.SetDB(c, schemaName)
if err != nil {
logrus.WithField("database", schemaName).Errorf("unable to process ComInitDB: %s", err.Error())
err = sql.CastSQLError(err)
}
return err
}

// ComPrepare parses, partially analyzes, and caches a prepared statement's plan
Expand Down
5 changes: 5 additions & 0 deletions server/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,11 @@ func TestHandlerErrors(t *testing.T) {
query: "INSERT INTO `test`.`test_table` (`id`, `id`, `v`) VALUES (1, 2, 3)",
expectedErrorCode: mysql.ERFieldSpecifiedTwice,
},
{
name: "use database that doesn't exist'",
query: "USE does_not_exist_db;",
expectedErrorCode: mysql.ERBadDb,
},
}

handler.ComInitDB(dummyConn, "test")
Expand Down
2 changes: 2 additions & 0 deletions sql/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -936,6 +936,8 @@ func CastSQLError(err error) *mysql.SQLError {
code = mysql.ERNoSuchTable
case ErrDatabaseExists.Is(err):
code = mysql.ERDbCreateExists
case ErrDatabaseNotFound.Is(err):
code = mysql.ERBadDb
case ErrExpectedSingleRow.Is(err):
code = mysql.ERSubqueryNo1Row
case ErrInvalidOperandColumns.Is(err):
Expand Down