package utils

import "strings"

// TiDB Handle
// parimary key <-> handler
// 1. 聚簇索引，主键不是 INT 类型 -> common handle
// 2. clutered index ,int 主键 -> int handle, pk_is_handle
// 3. 其他情况 -> _tidb_rowid 隐式主键

// common handle
// clustered index
// (int,varchar) primary
// varchar primary
var Tables = []string{CommonHandleTable1, CommonHandleTable2,
	PKIsHandle1, PKIsHandle2, PKIsHandle3,
	OtherHandle1, OtherHandle2, OtherHandle4, OtherHandle5, OtherHandle6,
}

var CommonHandleTable1 = `CREATE TABLE common_handle1 (
	a bigint(20) DEFAULT NULL,
	b int(11) DEFAULT NULL,
	c varchar(500) NOT NULL,
	d char(100) DEFAULT NULL,
	e float DEFAULT NULL,
	g double DEFAULT NULL,
	h decimal(8,4) DEFAULT NULL,
	i date DEFAULT NULL,
	j datetime DEFAULT NULL,
	PRIMARY KEY (c) CLUSTERED )`

var CommonHandleTable2 = `CREATE TABLE common_handle2 (
	a bigint(20) NOT NULL,
	b int(11) DEFAULT NULL,
	c varchar(100) NOT NULL,
	d char(100) DEFAULT NULL,
	e float DEFAULT NULL,
	g double DEFAULT NULL,
	h decimal(8,4) DEFAULT NULL,
	i date DEFAULT NULL,
	j datetime DEFAULT NULL,
	PRIMARY KEY (a,c)  CLUSTERED )`

// pk is handle
// clustered index
// int primary
// auto_increment, auto_random
var PKIsHandle1 = `CREATE TABLE pk_is_handle1 (
	a bigint(20) NOT NULL,
	b int(11) DEFAULT NULL,
	c varchar(100) NOT NULL,
	d char(100) DEFAULT NULL,
	e float DEFAULT NULL,
	g double DEFAULT NULL,
	h decimal(8,4) DEFAULT NULL,
	i date DEFAULT NULL,
	j datetime DEFAULT NULL,
	PRIMARY KEY (a)  CLUSTERED )`

var PKIsHandle2 = `CREATE TABLE pk_is_handle2 (
	a bigint(20) auto_increment,
	b int(11) DEFAULT NULL,
	c varchar(100) NOT NULL,
	d char(100) DEFAULT NULL,
	e float DEFAULT NULL,
	g double DEFAULT NULL,
	h decimal(8,4) DEFAULT NULL,
	i date DEFAULT NULL,
	j datetime DEFAULT NULL,
	PRIMARY KEY (a)  CLUSTERED )`

var PKIsHandle3 = `CREATE TABLE pk_is_handle3 (
	a bigint(20) auto_random,
	b int(11) DEFAULT NULL,
	c varchar(100) NOT NULL,
	d char(100) DEFAULT NULL,
	e float DEFAULT NULL,
	g double DEFAULT NULL,
	h decimal(8,4) DEFAULT NULL,
	i date DEFAULT NULL,
	j datetime DEFAULT NULL,
	PRIMARY KEY (a)  CLUSTERED )`

// other
var OtherHandle1 = `CREATE TABLE other_handle1 (
	a bigint(20) NOT NULL,
	b int(11) DEFAULT NULL,
	c varchar(100) NOT NULL,
	d char(100) DEFAULT NULL,
	e float DEFAULT NULL,
	g double DEFAULT NULL,
	h decimal(8,4) DEFAULT NULL,
	i date DEFAULT NULL,
	j datetime DEFAULT NULL,
	PRIMARY KEY (a)  NONCLUSTERED )`

var OtherHandle2 = `CREATE TABLE other_handle2 (
	a bigint(20) auto_increment,
	b int(11) DEFAULT NULL,
	c varchar(100) NOT NULL,
	d char(100) DEFAULT NULL,
	e float DEFAULT NULL,
	g double DEFAULT NULL,
	h decimal(8,4) DEFAULT NULL,
	i date DEFAULT NULL,
	j datetime DEFAULT NULL,
	PRIMARY KEY (a)  NONCLUSTERED )`

// ERROR 8216 (HY000): Invalid auto random: column a is not the integer primary key, or the primary key is nonclustered
var OtherHandle3 = `CREATE TABLE other_handle3 (
	a bigint(20) auto_random,
	b int(11) DEFAULT NULL,
	c varchar(100) NOT NULL,
	d char(100) DEFAULT NULL,
	e float DEFAULT NULL,
	g double DEFAULT NULL,
	h decimal(8,4) DEFAULT NULL,
	i date DEFAULT NULL,
	j datetime DEFAULT NULL,
	PRIMARY KEY (a)  NONCLUSTERED )`

var OtherHandle4 = `CREATE TABLE other_handle4 (
	a bigint(20) DEFAULT NULL,
	b int(11) DEFAULT NULL,
	c varchar(100) NOT NULL,
	d char(100) DEFAULT NULL,
	e float DEFAULT NULL,
	g double DEFAULT NULL,
	h decimal(8,4) DEFAULT NULL,
	i date DEFAULT NULL,
	j datetime DEFAULT NULL)`

var OtherHandle5 = `CREATE TABLE other_handle5 (
	a bigint(20) DEFAULT NULL,
	b int(11) DEFAULT NULL,
	c varchar(500) NOT NULL,
	d char(100) DEFAULT NULL,
	e float DEFAULT NULL,
	g double DEFAULT NULL,
	h decimal(8,4) DEFAULT NULL,
	i date DEFAULT NULL,
	j datetime DEFAULT NULL,
	PRIMARY KEY (c) NONCLUSTERED )`

var OtherHandle6 = `CREATE TABLE other_handle6 (
	a bigint(20) NOT NULL,
	b int(11) DEFAULT NULL,
	c varchar(100) NOT NULL,
	d char(100) DEFAULT NULL,
	e float DEFAULT NULL,
	g double DEFAULT NULL,
	h decimal(8,4) DEFAULT NULL,
	i date DEFAULT NULL,
	j datetime DEFAULT NULL,
	PRIMARY KEY (a,c)  NONCLUSTERED )`

func IgnoreDupEntry(err error) error {
	if err != nil {
		if strings.Contains(err.Error(), "Duplicate entry") {
			return nil
		}
	}
	return err
}

func IgnorePessimisticLock(err error) error {
	if err != nil {
		if strings.Contains(err.Error(), "pessimistic lock retry limit reached") {
			return nil
		}
	}
	return err
}

func IgnoreLockWait(err error) error {
	if err != nil {
		if strings.Contains(err.Error(), "Lock wait timeout exceeded") {
			return nil
		}
	}
	return err
}

