bob/rename.go

43 lines
1.1 KiB
Go
Raw Normal View History

2021-07-24 18:35:12 +00:00
package bob
import (
"errors"
"github.com/lann/builder"
)
type RenameBuilder builder.Builder
type renameData struct {
From string
To string
}
func init() {
builder.Register(RenameBuilder{}, renameData{})
}
// From sets existing table name
func (b RenameBuilder) From(name string) RenameBuilder {
return builder.Set(b, "From", name).(RenameBuilder)
}
// To sets desired table name
func (b RenameBuilder) To(name string) RenameBuilder {
return builder.Set(b, "To", name).(RenameBuilder)
}
// ToSql returns 3 variables filled out with the correct values based on bindings, etc.
func (b RenameBuilder) ToSql() (string, []interface{}, error) {
data := builder.GetStruct(b).(renameData)
return data.ToSql()
}
// ToSql returns 3 variables filled out with the correct values based on bindings, etc.
func (d *renameData) ToSql() (sqlStr string, args []interface{}, err error) {
if len(d.From) == 0 || d.From == "" || len(d.To) == 0 || d.To == "" {
err = errors.New("rename statement must specify a table")
}
sqlStr = "RENAME TABLE \""+d.From+"\" TO \""+d.To+"\";"
return
}