// Copyright 2017 PingCAP, 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,
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
	"github.com/go-sql-driver/mysql"
	"github.com/juju/errors"
	tmysql "github.com/pingcap/parser/mysql"
	"github.com/pingcap/parser/terror"
	tddl "github.com/pingcap/tidb/ddl"
	"github.com/pingcap/tidb/infoschema"
	gmysql "github.com/siddontang/go-mysql/mysql"
)

func ignoreDDLError(err error) bool {
	err = originError(err)
	mysqlErr, ok := err.(*mysql.MySQLError)
	if !ok {
		return false
	}

	errCode := terror.ErrCode(mysqlErr.Number)
	switch errCode {
	case infoschema.ErrDatabaseExists.Code(), infoschema.ErrDatabaseDropExists.Code(),
		infoschema.ErrTableExists.Code(), infoschema.ErrTableDropExists.Code(),
		infoschema.ErrColumnExists.Code(), infoschema.ErrIndexExists.Code(),
		tddl.ErrCantDropFieldOrKey.Code():
		return true
	case tmysql.ErrDupKeyName:
		return true
	default:
		return false
	}
}

func needRetryReplicate(err error) bool {
	err = originError(err)
	return err == gmysql.ErrBadConn
}

func isBinlogPurgedError(err error) bool {
	return isMysqlError(err, tmysql.ErrMasterFatalErrorReadingBinlog)
}

func isAccessDeniedError(err error) bool {
	return isMysqlError(err, tmysql.ErrSpecificAccessDenied)
}

func isNoSuchThreadError(err error) bool {
	return isMysqlError(err, tmysql.ErrNoSuchThread)
}

func isMysqlError(err error, code uint16) bool {
	err = originError(err)
	mysqlErr, ok := err.(*mysql.MySQLError)
	return ok && mysqlErr.Number == code
}

func isErrTableNotExists(err error) bool {
	return isMysqlError(err, tmysql.ErrNoSuchTable)
}

// originError return original error
func originError(err error) error {
	for {
		e := errors.Cause(err)
		if e == err {
			break
		}
		err = e
	}
	return err
}
