// Copyright 2022 The Xorm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package statements

import (
	"fmt"

	"xorm.io/builder"
)

// TableName return current tableName
func (statement *Statement) TableName() string {
	if statement.AltTableName != "" {
		return statement.AltTableName
	}

	return statement.tableName
}

// Alias set the table alias
func (statement *Statement) Alias(alias string) *Statement {
	statement.TableAlias = alias
	return statement
}

func (statement *Statement) writeAlias(w *builder.BytesWriter) error {
	if statement.TableAlias != "" {
		if _, err := fmt.Fprint(w, " AS ", statement.quote(statement.TableAlias)); err != nil {
			return err
		}
	}
	return nil
}

func (statement *Statement) writeTableName(w *builder.BytesWriter) error {
	if _, err := fmt.Fprint(w, statement.quote(statement.TableName())); err != nil {
		return err
	}
	return nil
}
