2016-12-20 18:35:22 +00:00
|
|
|
package mysql
|
|
|
|
|
|
2017-01-18 15:40:51 +00:00
|
|
|
import (
|
2021-12-14 21:34:11 +00:00
|
|
|
"database/sql"
|
2017-01-18 15:40:51 +00:00
|
|
|
"fmt"
|
2021-11-24 17:16:42 +00:00
|
|
|
"strconv"
|
2017-01-18 15:40:51 +00:00
|
|
|
|
|
|
|
|
"github.com/VividCortex/mysqlerr"
|
2021-11-15 14:11:38 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/contexts/ctxerr"
|
2022-01-18 01:52:09 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/fleet"
|
2017-01-18 15:40:51 +00:00
|
|
|
"github.com/go-sql-driver/mysql"
|
|
|
|
|
)
|
2016-12-20 18:35:22 +00:00
|
|
|
|
|
|
|
|
type notFoundError struct {
|
|
|
|
|
ID uint
|
2018-05-04 18:05:55 +00:00
|
|
|
Name string
|
2016-12-20 18:35:22 +00:00
|
|
|
Message string
|
|
|
|
|
ResourceType string
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-18 01:52:09 +00:00
|
|
|
var _ fleet.NotFoundError = (*notFoundError)(nil)
|
|
|
|
|
|
2016-12-20 18:35:22 +00:00
|
|
|
func notFound(kind string) *notFoundError {
|
|
|
|
|
return ¬FoundError{
|
|
|
|
|
ResourceType: kind,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *notFoundError) Error() string {
|
2016-12-20 21:31:09 +00:00
|
|
|
if e.ID != 0 {
|
|
|
|
|
return fmt.Sprintf("%s %d was not found in the datastore", e.ResourceType, e.ID)
|
2016-12-20 18:35:22 +00:00
|
|
|
}
|
2018-05-04 18:05:55 +00:00
|
|
|
if e.Name != "" {
|
|
|
|
|
return fmt.Sprintf("%s %s was not found in the datastore", e.ResourceType, e.Name)
|
|
|
|
|
}
|
2016-12-20 18:35:22 +00:00
|
|
|
if e.Message != "" {
|
2016-12-20 21:31:09 +00:00
|
|
|
return fmt.Sprintf("%s %s was not found in the datastore", e.ResourceType, e.Message)
|
2016-12-20 18:35:22 +00:00
|
|
|
}
|
2016-12-20 21:31:09 +00:00
|
|
|
return fmt.Sprintf("%s was not found in the datastore", e.ResourceType)
|
2016-12-20 18:35:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *notFoundError) WithID(id uint) error {
|
|
|
|
|
e.ID = id
|
|
|
|
|
return e
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-04 18:05:55 +00:00
|
|
|
func (e *notFoundError) WithName(name string) error {
|
|
|
|
|
e.Name = name
|
|
|
|
|
return e
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-20 18:35:22 +00:00
|
|
|
func (e *notFoundError) WithMessage(msg string) error {
|
|
|
|
|
e.Message = msg
|
|
|
|
|
return e
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *notFoundError) IsNotFound() bool {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-14 21:34:11 +00:00
|
|
|
// Implement Is so that errors.Is(err, sql.ErrNoRows) returns true for an
|
|
|
|
|
// error of type *notFoundError, without having to wrap sql.ErrNoRows
|
|
|
|
|
// explicitly.
|
|
|
|
|
func (e *notFoundError) Is(other error) bool {
|
|
|
|
|
return other == sql.ErrNoRows
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-20 18:35:22 +00:00
|
|
|
type existsError struct {
|
2021-04-05 20:28:43 +00:00
|
|
|
Identifier interface{}
|
2016-12-20 18:35:22 +00:00
|
|
|
ResourceType string
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-05 20:28:43 +00:00
|
|
|
func alreadyExists(kind string, identifier interface{}) error {
|
2021-11-24 17:16:42 +00:00
|
|
|
if s, ok := identifier.(string); ok {
|
|
|
|
|
identifier = strconv.Quote(s)
|
|
|
|
|
}
|
2016-12-20 18:35:22 +00:00
|
|
|
return &existsError{
|
2021-04-05 20:28:43 +00:00
|
|
|
Identifier: identifier,
|
2016-12-20 18:35:22 +00:00
|
|
|
ResourceType: kind,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *existsError) Error() string {
|
2021-04-05 20:28:43 +00:00
|
|
|
return fmt.Sprintf("%s %v already exists", e.ResourceType, e.Identifier)
|
2016-12-20 18:35:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *existsError) IsExists() bool {
|
|
|
|
|
return true
|
|
|
|
|
}
|
2017-01-18 15:40:51 +00:00
|
|
|
|
|
|
|
|
func isDuplicate(err error) bool {
|
2021-11-15 14:11:38 +00:00
|
|
|
err = ctxerr.Cause(err)
|
2017-01-18 15:40:51 +00:00
|
|
|
if driverErr, ok := err.(*mysql.MySQLError); ok {
|
|
|
|
|
if driverErr.Number == mysqlerr.ER_DUP_ENTRY {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
2018-06-15 14:13:11 +00:00
|
|
|
|
|
|
|
|
type foreignKeyError struct {
|
|
|
|
|
Name string
|
|
|
|
|
ResourceType string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func foreignKey(kind string, name string) error {
|
|
|
|
|
return &foreignKeyError{
|
|
|
|
|
Name: name,
|
|
|
|
|
ResourceType: kind,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *foreignKeyError) Error() string {
|
|
|
|
|
return fmt.Sprintf("the operation violates a foreign key constraint on %s: %s", e.ResourceType, e.Name)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (e *foreignKeyError) IsForeignKey() bool {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func isMySQLForeignKey(err error) bool {
|
2021-11-15 14:11:38 +00:00
|
|
|
err = ctxerr.Cause(err)
|
2018-06-15 14:13:11 +00:00
|
|
|
if driverErr, ok := err.(*mysql.MySQLError); ok {
|
|
|
|
|
if driverErr.Number == mysqlerr.ER_ROW_IS_REFERENCED_2 {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|