feat: truncate rename and drop

This commit is contained in:
Reinaldy Rafli 2021-07-25 01:35:12 +07:00
parent 0d704c88df
commit c9b01eeb67
7 changed files with 261 additions and 0 deletions

32
bob.go
View File

@ -36,6 +36,22 @@ func (b BobBuilderType) HasColumn(column string) HasBuilder {
return HasBuilder(b).HasColumn(column)
}
func (b BobBuilderType) DropTable(table string) DropBuilder {
return DropBuilder(b).DropTable(table)
}
func (b BobBuilderType) DropTableIfExists(table string) DropBuilder {
return DropBuilder(b).DropTable(table).IfExists()
}
func (b BobBuilderType) RenameTable(from, to string) RenameBuilder {
return RenameBuilder(b).From(from).To(to)
}
func (b BobBuilderType) Truncate(table string) TruncateBuilder {
return TruncateBuilder(b).Truncate(table)
}
// BobStmtBuilder is the parent builder for BobBuilderType
var BobStmtBuilder = BobBuilderType(builder.EmptyBuilder)
@ -58,3 +74,19 @@ func HasTable(table string) HasBuilder {
func HasColumn(col string) HasBuilder {
return BobStmtBuilder.HasColumn(col)
}
func DropTable(table string) DropBuilder {
return BobStmtBuilder.DropTable(table)
}
func DropTableIfExists(table string) DropBuilder {
return BobStmtBuilder.DropTableIfExists(table)
}
func RenameTable(from, to string) RenameBuilder {
return BobStmtBuilder.RenameTable(from, to)
}
func Truncate(table string) TruncateBuilder {
return BobStmtBuilder.Truncate(table)
}

53
drop.go Normal file
View File

@ -0,0 +1,53 @@
package bob
import (
"bytes"
"errors"
"github.com/lann/builder"
)
type DropBuilder builder.Builder
type dropData struct {
TableName string
IfExists bool
}
func init() {
builder.Register(DropBuilder{}, dropData{})
}
// DropTable sets which table to be dropped
func (b DropBuilder) DropTable(name string) DropBuilder {
return builder.Set(b, "TableName", name).(DropBuilder)
}
func (b DropBuilder) IfExists() DropBuilder {
return builder.Set(b, "IfExists", true).(DropBuilder)
}
// ToSql returns 3 variables filled out with the correct values based on bindings, etc.
func (b DropBuilder) ToSql() (string, []interface{}, error) {
data := builder.GetStruct(b).(dropData)
return data.ToSql()
}
// ToSql returns 3 variables filled out with the correct values based on bindings, etc.
func (d *dropData) ToSql() (sqlStr string, args []interface{}, err error) {
if len(d.TableName) == 0 || d.TableName == "" {
err = errors.New("drop statement must specify a table")
}
sql := &bytes.Buffer{}
sql.WriteString("DROP TABLE ")
if d.IfExists {
sql.WriteString("IF EXISTS ")
}
sql.WriteString("\""+d.TableName+"\";")
sqlStr = sql.String()
return
}

40
drop_test.go Normal file
View File

@ -0,0 +1,40 @@
package bob_test
import (
"testing"
"github.com/aldy505/bob"
)
func TestDrop(t *testing.T) {
t.Run("should be able to create regular drop query", func (t *testing.T) {
sql, _, err := bob.DropTable("users").ToSql()
if err != nil {
t.Error(err)
}
result := "DROP TABLE \"users\";"
if sql != result {
t.Error("sql is not the same as result: ", sql)
}
})
t.Run("should be able to create drop if exists query", func(t *testing.T) {
sql, _, err := bob.DropTableIfExists("users").ToSql()
if err != nil {
t.Error(err)
}
result := "DROP TABLE IF EXISTS \"users\";"
if sql != result {
t.Error("sql is not the same as result: ", sql)
}
})
t.Run("should expect an error for no table name", func(t *testing.T) {
_, _, err := bob.DropTableIfExists("").ToSql()
if err == nil && err.Error() != "drop statement must specify a table" {
t.Error(err)
}
})
}

43
rename.go Normal file
View File

@ -0,0 +1,43 @@
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
}

28
rename_test.go Normal file
View File

@ -0,0 +1,28 @@
package bob_test
import (
"testing"
"github.com/aldy505/bob"
)
func TestRename(t *testing.T) {
t.Run("should be able to create rename query", func (t *testing.T) {
sql, _, err := bob.RenameTable("users", "teachers").ToSql()
if err != nil {
t.Error(err)
}
result := "RENAME TABLE \"users\" TO \"teachers\";"
if sql != result {
t.Error("sql is not the same as result: ", sql)
}
})
t.Run("should expect an error for no table name", func(t *testing.T) {
_, _, err := bob.RenameTable("", "").ToSql()
if err == nil && err.Error() != "rename statement must specify a table" {
t.Error(err)
}
})
}

37
truncate.go Normal file
View File

@ -0,0 +1,37 @@
package bob
import (
"errors"
"github.com/lann/builder"
)
type TruncateBuilder builder.Builder
type truncateData struct {
TableName string
}
func init() {
builder.Register(TruncateBuilder{}, truncateData{})
}
// Truncate sets which table to be dropped
func (b TruncateBuilder) Truncate(name string) TruncateBuilder {
return builder.Set(b, "TableName", name).(TruncateBuilder)
}
// ToSql returns 3 variables filled out with the correct values based on bindings, etc.
func (b TruncateBuilder) ToSql() (string, []interface{}, error) {
data := builder.GetStruct(b).(truncateData)
return data.ToSql()
}
// ToSql returns 3 variables filled out with the correct values based on bindings, etc.
func (d *truncateData) ToSql() (sqlStr string, args []interface{}, err error) {
if len(d.TableName) == 0 || d.TableName == "" {
err = errors.New("truncate statement must specify a table")
}
sqlStr = "TRUNCATE \""+d.TableName+"\";"
return
}

28
truncate_test.go Normal file
View File

@ -0,0 +1,28 @@
package bob_test
import (
"testing"
"github.com/aldy505/bob"
)
func TestTruncate(t *testing.T) {
t.Run("should be able to create truncate query", func (t *testing.T) {
sql, _, err := bob.Truncate("users").ToSql()
if err != nil {
t.Error(err)
}
result := "TRUNCATE \"users\";"
if sql != result {
t.Error("sql is not the same as result: ", sql)
}
})
t.Run("should expect an error for no table name", func(t *testing.T) {
_, _, err := bob.Truncate("").ToSql()
if err == nil && err.Error() != "TRUNCATE statement must specify a table" {
t.Error(err)
}
})
}