func (t *TableNameDecorator) Text() string {
	return t.origin.Text()
}
Text implement ast.Node
package plan
import (
	"fmt"
	"github.com/nooncall/shazam/parser/ast"
	"github.com/nooncall/shazam/parser/format"
	"github.com/nooncall/shazam/proxy/router"
)
type TableNameDecorator struct {
	origin *ast.TableName
	rule   router.Rule
	result *RouteResult
}
func NeedCreateTableNameDecoratorWithoutAlias(p *StmtInfo, n *ast.TableName) (router.Rule, bool, error) {
	db, table := getTableInfoFromTableName(n)
	rule, err := p.RecordShardTable(db, table)
	if err != nil {
		return nil, false, fmt.Errorf("record shard table in SelectPlan error, db: %s, table, %s, err: %v", db, table, err)
	}
	return rule, true, nil
}
func NeedCreateTableNameDecorator(p *TableAliasStmtInfo, n *ast.TableName, alias string) (router.Rule, bool, error) {
	db, table := getTableInfoFromTableName(n)
	rule, err := p.RecordShardTable(db, table, alias)
	if err != nil {
		return nil, false, fmt.Errorf("record shard table in SelectPlan error, db: %s, table, %s, alias: %s, err: %v", db, table, alias, err)
	}
	return rule, true, nil
}
func CreateTableNameDecorator(n *ast.TableName, rule router.Rule, result *RouteResult) (*TableNameDecorator, error) {
	if len(n.PartitionNames) != 0 {
		return nil, fmt.Errorf("TableName does not support PartitionNames in sharding")
	}
	if len(n.IndexHints) != 0 {
		return nil, fmt.Errorf("TableName does not support IndexHints in sharding")
	}
	ret := &TableNameDecorator{
		origin: n,
		rule:   rule,
		result: result,
	}
	return ret, nil
}
func (t *TableNameDecorator) Restore(ctx *format.RestoreCtx) error {
	tableIndex, err := t.result.GetCurrentTableIndex()
	if err != nil {
		return err
	}
	ruleType := t.rule.GetType()
	if t.origin.Schema.String() != "" {
		if ruleType == router.GlobalTableRuleType {
			dbName, err := t.rule.GetDatabaseNameByTableIndex(tableIndex)
			if err != nil {
				return fmt.Errorf("get mycat database name error: %v", err)
			}
			ctx.WriteName(dbName)
			ctx.WritePlain(".")
		} else if router.IsMycatShardingRule(ruleType) {
			dbName, err := t.rule.GetDatabaseNameByTableIndex(tableIndex)
			if err != nil {
				return fmt.Errorf("get mycat database name error: %v", err)
			}
			ctx.WriteName(dbName)
			ctx.WritePlain(".")
		} else {
			ctx.WriteName(t.origin.Schema.String())
			ctx.WritePlain(".")
		}
	}
	if ruleType == router.GlobalTableRuleType {
		ctx.WriteName(t.origin.Name.String())
	} else if router.IsMycatShardingRule(ruleType) {
		ctx.WriteName(t.origin.Name.String())
	} else {
		ctx.WriteName(fmt.Sprintf("%s_%04d", t.origin.Name.String(), tableIndex))
	}
	return nil
}
func (t *TableNameDecorator) Accept(v ast.Visitor) (ast.Node, bool) {
	return t, true
}