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
|
2021-07-31 04:48:40 +00:00
|
|
|
To string
|
2021-07-24 18:35:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
builder.Register(RenameBuilder{}, renameData{})
|
|
|
|
}
|
|
|
|
|
2021-07-31 04:17:48 +00:00
|
|
|
// from sets existing table name
|
|
|
|
func (b RenameBuilder) from(name string) RenameBuilder {
|
2021-07-24 18:35:12 +00:00
|
|
|
return builder.Set(b, "From", name).(RenameBuilder)
|
|
|
|
}
|
|
|
|
|
2021-07-31 04:17:48 +00:00
|
|
|
// to sets desired table name
|
|
|
|
func (b RenameBuilder) to(name string) RenameBuilder {
|
2021-07-24 18:35:12 +00:00
|
|
|
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")
|
|
|
|
}
|
2021-07-31 04:48:40 +00:00
|
|
|
sqlStr = "RENAME TABLE \"" + d.From + "\" TO \"" + d.To + "\";"
|
2021-07-24 18:35:12 +00:00
|
|
|
return
|
2021-07-31 04:48:40 +00:00
|
|
|
}
|