Skip to content

Commit

Permalink
Adding mapping to error code 1049 for ErrDatabaseNotFound errors. Map…
Browse files Browse the repository at this point in the history
…ping errors from ComInitDB to MySQL errors with error codes.

dolthub/dolt#7890
  • Loading branch information
fulghum committed May 23, 2024
1 parent a6e5789 commit 7686260
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
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

0 comments on commit 7686260

Please sign in to comment.