Skip to content

Commit

Permalink
*: Add a function GenByArgs for Error (pingcap#2033)
Browse files Browse the repository at this point in the history
*: Add a function GenByArgs for Error
  • Loading branch information
hanfei1991 authored and zimulala committed Nov 27, 2016
1 parent 2225c90 commit ea894e8
Show file tree
Hide file tree
Showing 18 changed files with 93 additions and 82 deletions.
6 changes: 3 additions & 3 deletions ddl/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (d *ddl) createColumnInfo(tblInfo *model.TableInfo, colInfo *model.ColumnIn
} else if pos.Tp == ast.ColumnPositionAfter {
c := findCol(cols, pos.RelativeColumn.Name.L)
if c == nil {
return nil, 0, infoschema.ErrColumnNotExists.Gen("no such column: %v", pos.RelativeColumn)
return nil, 0, infoschema.ErrColumnNotExists.GenByArgs(pos.RelativeColumn, tblInfo.Name)
}

// Insert position is after the mentioned column.
Expand Down Expand Up @@ -113,7 +113,7 @@ func (d *ddl) onAddColumn(t *meta.Meta, job *model.Job) error {
if columnInfo.State == model.StatePublic {
// We already have a column with the same column name.
job.State = model.JobCancelled
return infoschema.ErrColumnExists.Gen("column already exist %s", col.Name)
return infoschema.ErrColumnExists.GenByArgs(col.Name)
}
} else {
columnInfo, offset, err = d.createColumnInfo(tblInfo, col, pos)
Expand Down Expand Up @@ -432,7 +432,7 @@ func (d *ddl) onModifyColumn(t *meta.Meta, job *model.Job) error {
oldCol := findCol(tblInfo.Columns, newCol.Name.L)
if oldCol == nil || oldCol.State != model.StatePublic {
job.State = model.JobCancelled
return infoschema.ErrColumnNotExists.Gen("column %s doesn't exist", newCol.Name)
return infoschema.ErrColumnNotExists.GenByArgs(newCol.Name, tblInfo.Name)
}
*oldCol = *newCol
err = t.UpdateTable(job.SchemaID, tblInfo)
Expand Down
33 changes: 17 additions & 16 deletions ddl/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ func (d *ddl) CreateSchema(ctx context.Context, schema model.CIStr, charsetInfo
is := d.GetInformationSchema()
_, ok := is.SchemaByName(schema)
if ok {
return errors.Trace(infoschema.ErrDatabaseExists)
return errors.Trace(infoschema.ErrDatabaseExists.GenByArgs(schema))
}

if err = checkTooLongSchema(schema); err != nil {
Expand Down Expand Up @@ -661,7 +661,7 @@ func checkDuplicateColumn(colDefs []*ast.ColumnDef) error {
for _, colDef := range colDefs {
nameLower := colDef.Name.Name.O
if colNames[nameLower] {
return infoschema.ErrColumnExists.Gen("duplicate column %s", colDef.Name.Name)
return infoschema.ErrColumnExists.GenByArgs(colDef.Name.Name)
}
colNames[nameLower] = true
}
Expand All @@ -684,7 +684,7 @@ func checkDuplicateConstraint(namesMap map[string]bool, name string, foreign boo
nameLower := strings.ToLower(name)
if namesMap[nameLower] {
if foreign {
return infoschema.ErrForeignKeyExists.Gen("duplicate foreign key %s", name)
return infoschema.ErrCannotAddForeign
}
return errDupKeyName.Gen("duplicate key name %s", name)
}
Expand Down Expand Up @@ -757,7 +757,7 @@ func (d *ddl) buildTableInfo(tableName model.CIStr, cols []*table.Column, constr
if constr.Tp == ast.ConstraintForeignKey {
for _, fk := range tbInfo.ForeignKeys {
if fk.Name.L == strings.ToLower(constr.Name) {
return nil, infoschema.ErrForeignKeyExists.Gen("foreign key %s already exists.", constr.Name)
return nil, infoschema.ErrCannotAddForeign
}
}
var fk model.FKInfo
Expand All @@ -773,10 +773,11 @@ func (d *ddl) buildTableInfo(tableName model.CIStr, cols []*table.Column, constr
fk.OnDelete = int(constr.Refer.OnDelete.ReferOpt)
fk.OnUpdate = int(constr.Refer.OnUpdate.ReferOpt)
if len(fk.Cols) != len(fk.RefCols) {
return nil, infoschema.ErrForeignKeyNotMatch.Gen("foreign key not match keys len %d, refkeys len %d .", len(fk.Cols), len(fk.RefCols))
return nil, infoschema.ErrForeignKeyNotMatch
}
if len(fk.Cols) == 0 {
return nil, infoschema.ErrForeignKeyNotMatch.Gen("foreign key should have one key at least.")
// TODO: In MySQL, this case will report a parse error.
return nil, infoschema.ErrCannotAddForeign
}
tbInfo.ForeignKeys = append(tbInfo.ForeignKeys, &fk)
continue
Expand Down Expand Up @@ -846,10 +847,10 @@ func (d *ddl) CreateTable(ctx context.Context, ident ast.Ident, colDefs []*ast.C
is := d.GetInformationSchema()
schema, ok := is.SchemaByName(ident.Schema)
if !ok {
return infoschema.ErrDatabaseNotExists.Gen("database %s not exists", ident.Schema)
return infoschema.ErrDatabaseNotExists.GenByArgs(ident.Schema)
}
if is.TableExists(ident.Schema, ident.Name) {
return errors.Trace(infoschema.ErrTableExists)
return errors.Trace(infoschema.ErrTableExists.GenByArgs(ident))
}
if err = checkTooLongTable(ident.Name); err != nil {
return errors.Trace(err)
Expand Down Expand Up @@ -1005,7 +1006,7 @@ func (d *ddl) AddColumn(ctx context.Context, ti ast.Ident, spec *ast.AlterTableS
colName := spec.Column.Name.Name.O
col := table.FindCol(t.Cols(), colName)
if col != nil {
return infoschema.ErrColumnExists.Gen("column %s already exists", colName)
return infoschema.ErrColumnExists.GenByArgs(colName)
}

if len(colName) > mysql.MaxColumnNameLength {
Expand Down Expand Up @@ -1117,7 +1118,7 @@ func (d *ddl) ModifyColumn(ctx context.Context, ident ast.Ident, spec *ast.Alter
colName := spec.Column.Name.Name
col := table.FindCol(t.Cols(), colName.L)
if col == nil {
return infoschema.ErrColumnNotExists.Gen("column %s doesn't exist", colName.O)
return infoschema.ErrColumnNotExists.GenByArgs(colName, ident.Name)
}
if spec.Constraint != nil || spec.Position.Tp != ast.ColumnPositionNone ||
len(spec.Column.Options) != 0 || spec.Column.Tp == nil {
Expand Down Expand Up @@ -1146,12 +1147,12 @@ func (d *ddl) DropTable(ctx context.Context, ti ast.Ident) (err error) {
is := d.GetInformationSchema()
schema, ok := is.SchemaByName(ti.Schema)
if !ok {
return infoschema.ErrDatabaseNotExists.Gen("database %s doesn't exist", ti.Schema)
return infoschema.ErrDatabaseNotExists.GenByArgs(ti.Schema)
}

tb, err := is.TableByName(ti.Schema, ti.Name)
if err != nil {
return errors.Trace(infoschema.ErrTableNotExists)
return errors.Trace(infoschema.ErrTableNotExists.GenByArgs(ti))
}

job := &model.Job{
Expand All @@ -1169,7 +1170,7 @@ func (d *ddl) TruncateTable(ctx context.Context, ti ast.Ident) error {
is := d.GetInformationSchema()
schema, ok := is.SchemaByName(ti.Schema)
if !ok {
return infoschema.ErrDatabaseNotExists.Gen("database %s doesn't exist", ti.Schema)
return infoschema.ErrDatabaseNotExists.GenByArgs(ti.Schema)
}
tb, err := is.TableByName(ti.Schema, ti.Name)
if err != nil {
Expand Down Expand Up @@ -1208,7 +1209,7 @@ func (d *ddl) CreateIndex(ctx context.Context, ti ast.Ident, unique bool, indexN
is := d.infoHandle.Get()
schema, ok := is.SchemaByName(ti.Schema)
if !ok {
return infoschema.ErrDatabaseNotExists.Gen("database %s not exists", ti.Schema)
return infoschema.ErrDatabaseNotExists.GenByArgs(ti.Schema)
}

t, err := is.TableByName(ti.Schema, ti.Name)
Expand Down Expand Up @@ -1269,7 +1270,7 @@ func (d *ddl) CreateForeignKey(ctx context.Context, ti ast.Ident, fkName model.C
is := d.infoHandle.Get()
schema, ok := is.SchemaByName(ti.Schema)
if !ok {
return infoschema.ErrDatabaseNotExists.Gen("database %s not exists", ti.Schema)
return infoschema.ErrDatabaseNotExists.GenByArgs(ti.Schema)
}

t, err := is.TableByName(ti.Schema, ti.Name)
Expand Down Expand Up @@ -1299,7 +1300,7 @@ func (d *ddl) DropForeignKey(ctx context.Context, ti ast.Ident, fkName model.CIS
is := d.infoHandle.Get()
schema, ok := is.SchemaByName(ti.Schema)
if !ok {
return infoschema.ErrDatabaseNotExists.Gen("database %s not exists", ti.Schema)
return infoschema.ErrDatabaseNotExists.GenByArgs(ti.Schema)
}

t, err := is.TableByName(ti.Schema, ti.Name)
Expand Down
2 changes: 1 addition & 1 deletion ddl/foreign_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (d *ddl) onDropForeignKey(t *meta.Meta, job *model.Job) error {

if !found {
job.State = model.JobCancelled
return infoschema.ErrForeignKeyNotExists.Gen("foreign key %s doesn't exist", fkName)
return infoschema.ErrForeignKeyNotExists.GenByArgs(fkName)
}

nfks := tblInfo.ForeignKeys[:0]
Expand Down
2 changes: 1 addition & 1 deletion ddl/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (d *ddl) onCreateSchema(t *meta.Meta, job *model.Job) error {
if db.ID != schemaID {
// The database already exists, can't create it, we should cancel this job now.
job.State = model.JobCancelled
return errors.Trace(infoschema.ErrDatabaseExists)
return errors.Trace(infoschema.ErrDatabaseExists.GenByArgs(db.Name))
}
dbInfo = db
}
Expand Down
2 changes: 1 addition & 1 deletion ddl/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (d *ddl) onCreateTable(t *meta.Meta, job *model.Job) error {
if tbl.Name.L == tbInfo.Name.L {
// This table already exists and can't be created, we should cancel this job now.
job.State = model.JobCancelled
return errors.Trace(infoschema.ErrTableExists)
return errors.Trace(infoschema.ErrTableExists.GenByArgs(tbl.Name))
}
}

Expand Down
2 changes: 1 addition & 1 deletion evaluator/evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ func (e *Evaluator) variable(v *ast.VariableExpr) bool {
sysVar, ok := variable.SysVars[name]
if !ok {
// select null sys vars is not permitted
e.err = variable.UnknownSystemVar.Gen("Unknown system variable '%s'", name)
e.err = variable.UnknownSystemVar.GenByArgs(name)
return false
}
if sysVar.Scope == variable.ScopeNone {
Expand Down
8 changes: 4 additions & 4 deletions executor/executor_ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (e *DDLExec) executeCreateTable(s *ast.CreateTableStmt) error {
if s.IfNotExists {
return nil
}
return infoschema.ErrTableExists.Gen("CREATE TABLE: table exists %s", ident)
return err
}
return errors.Trace(err)
}
Expand All @@ -134,7 +134,7 @@ func (e *DDLExec) executeDropDatabase(s *ast.DropDatabaseStmt) error {
if s.IfExists {
err = nil
} else {
err = infoschema.ErrDatabaseDropExists.Gen("Can't drop database '%s'; database doesn't exist", s.Name)
err = infoschema.ErrDatabaseDropExists.GenByArgs(s.Name)
}
}
sessionVars := e.ctx.GetSessionVars()
Expand Down Expand Up @@ -164,7 +164,7 @@ func (e *DDLExec) executeDropTable(s *ast.DropTableStmt) error {
continue
}
tb, err := e.is.TableByName(tn.Schema, tn.Name)
if err != nil && strings.HasSuffix(err.Error(), "not exist") {
if err != nil && infoschema.ErrTableNotExists.Equal(err) {
notExistTables = append(notExistTables, fullti.String())
continue
} else if err != nil {
Expand All @@ -188,7 +188,7 @@ func (e *DDLExec) executeDropTable(s *ast.DropTableStmt) error {
}
}
if len(notExistTables) > 0 && !s.IfExists {
return infoschema.ErrTableDropExists.Gen("DROP TABLE: table %s does not exist", strings.Join(notExistTables, ","))
return infoschema.ErrTableDropExists.GenByArgs(strings.Join(notExistTables, ","))
}
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion executor/executor_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (e *SetExecutor) executeSet() error {
// Set system variable
sysVar := variable.GetSysVar(name)
if sysVar == nil {
return variable.UnknownSystemVar.Gen("Unknown system variable '%s'", name)
return variable.UnknownSystemVar.GenByArgs(name)
}
if sysVar.Scope == variable.ScopeNone {
return errors.Errorf("Variable '%s' is a read only variable", name)
Expand Down
2 changes: 1 addition & 1 deletion executor/executor_simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func (e *SimpleExec) executeUse(s *ast.UseStmt) error {
dbname := model.NewCIStr(s.DBName)
dbinfo, exists := sessionctx.GetDomain(e.ctx).InfoSchema().SchemaByName(dbname)
if !exists {
return infoschema.ErrDatabaseNotExists.Gen("database %s not exists", dbname)
return infoschema.ErrDatabaseNotExists.GenByArgs(dbname)
}
e.ctx.GetSessionVars().CurrentDB = dbname.O
// character_set_database is the character set used by the default database.
Expand Down
2 changes: 1 addition & 1 deletion executor/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ func (e *ShowExec) fetchShowCreateTable() error {
func (e *ShowExec) fetchShowCreateDatabase() error {
db, ok := e.is.SchemaByName(e.DBName)
if !ok {
return infoschema.ErrDatabaseNotExists.Gen("Unknown database '%s'", e.DBName.O)
return infoschema.ErrDatabaseNotExists.GenByArgs(e.DBName.O)
}

var buf bytes.Buffer
Expand Down
28 changes: 15 additions & 13 deletions infoschema/infoschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,27 @@ import (

var (
// ErrDatabaseDropExists returns for dropping a non-existent database.
ErrDatabaseDropExists = terror.ClassSchema.New(codeDBDropExists, "database doesn't exist")
ErrDatabaseDropExists = terror.ClassSchema.New(codeDBDropExists, "Can't drop database '%s'; database doesn't exist")
// ErrDatabaseNotExists returns for database not exists.
ErrDatabaseNotExists = terror.ClassSchema.New(codeDatabaseNotExists, "database not exists")
ErrDatabaseNotExists = terror.ClassSchema.New(codeDatabaseNotExists, "Unknown database '%s'")
// ErrTableNotExists returns for table not exists.
ErrTableNotExists = terror.ClassSchema.New(codeTableNotExists, "table not exists")
ErrTableNotExists = terror.ClassSchema.New(codeTableNotExists, "Table '%s.%s' doesn't exist")
// ErrColumnNotExists returns for column not exists.
ErrColumnNotExists = terror.ClassSchema.New(codeColumnNotExists, "field not exists")
ErrColumnNotExists = terror.ClassSchema.New(codeColumnNotExists, "Unknown column '%s' in '%s'")
// ErrForeignKeyNotMatch returns for foreign key not match.
ErrForeignKeyNotMatch = terror.ClassSchema.New(codeCannotAddForeign, "foreign key not match")
// ErrForeignKeyExists returns for foreign key exists.
ErrForeignKeyExists = terror.ClassSchema.New(codeCannotAddForeign, "foreign key already exists")
ErrForeignKeyNotMatch = terror.ClassSchema.New(codeWrongFkDef, "Incorrect foreign key definition for '%s': Key reference and table reference don't match")
// ErrCannotAddForeign returns for foreign key exists.
ErrCannotAddForeign = terror.ClassSchema.New(codeCannotAddForeign, "Cannot add foreign key constraint")
// ErrForeignKeyNotExists returns for foreign key not exists.
ErrForeignKeyNotExists = terror.ClassSchema.New(codeForeignKeyNotExists, "foreign key not exists")
ErrForeignKeyNotExists = terror.ClassSchema.New(codeForeignKeyNotExists, "Can't DROP '%s'; check that column/key exists")
// ErrDatabaseExists returns for database already exists.
ErrDatabaseExists = terror.ClassSchema.New(codeDatabaseExists, "database already exists")
ErrDatabaseExists = terror.ClassSchema.New(codeDatabaseExists, "Can't create database '%s'; database exists")
// ErrTableExists returns for table already exists.
ErrTableExists = terror.ClassSchema.New(codeTableExists, "table already exists")
ErrTableExists = terror.ClassSchema.New(codeTableExists, "Table '%s' already exists")
// ErrTableDropExists returns for dropping a non-existent table.
ErrTableDropExists = terror.ClassSchema.New(codeBadTable, "unknown table")
ErrTableDropExists = terror.ClassSchema.New(codeBadTable, "Unknown table '%s'")
// ErrColumnExists returns for column already exists.
ErrColumnExists = terror.ClassSchema.New(codeColumnExists, "Duplicate column")
ErrColumnExists = terror.ClassSchema.New(codeColumnExists, "Duplicate column name '%s'")
// ErrIndexExists returns for index already exists.
ErrIndexExists = terror.ClassSchema.New(codeIndexExists, "Duplicate Index")
)
Expand Down Expand Up @@ -167,7 +167,7 @@ func (is *infoSchema) TableByName(schema, table model.CIStr) (t table.Table, err
return
}
}
return nil, ErrTableNotExists.Gen("table %s.%s does not exist", schema, table)
return nil, ErrTableNotExists.GenByArgs(schema, table)
}

func (is *infoSchema) TableExists(schema, table model.CIStr) bool {
Expand Down Expand Up @@ -291,6 +291,7 @@ const (

codeCannotAddForeign = 1215
codeForeignKeyNotExists = 1091
codeWrongFkDef = 1239

codeDatabaseExists = 1007
codeTableExists = 1050
Expand All @@ -306,6 +307,7 @@ func init() {
codeTableNotExists: mysql.ErrNoSuchTable,
codeColumnNotExists: mysql.ErrBadField,
codeCannotAddForeign: mysql.ErrCannotAddForeign,
codeWrongFkDef: mysql.ErrWrongFkDef,
codeForeignKeyNotExists: mysql.ErrCantDropFieldOrKey,
codeDatabaseExists: mysql.ErrDBCreateExists,
codeTableExists: mysql.ErrTableExists,
Expand Down
Loading

0 comments on commit ea894e8

Please sign in to comment.