fix(breaking): ToSQL > ToSql for compat with Squirrel

This commit is contained in:
Reinaldy Rafli 2021-07-09 13:41:34 +07:00
parent e675c80934
commit ef78c0b03a
6 changed files with 28 additions and 28 deletions

View File

@ -24,7 +24,7 @@ func main() {
db := pgx.Connect() db := pgx.Connect()
// Check if a table is exists // Check if a table is exists
sql, args, err = bob.HasTable("users").PlaceholderFormat(bob.Dollar).ToSQL() sql, args, err = bob.HasTable("users").PlaceholderFormat(bob.Dollar).ToSql()
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@ -47,7 +47,7 @@ func main() {
Types("varchar(36)", "varchar(255)", "varchar(255)", "text", "date"). Types("varchar(36)", "varchar(255)", "varchar(255)", "text", "date").
Primary("id"). Primary("id").
Unique("email") Unique("email")
ToSQL() ToSql()
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }

4
bob.go
View File

@ -5,9 +5,9 @@ import "github.com/lann/builder"
// BobBuilderType is the type for BobBuilder // BobBuilderType is the type for BobBuilder
type BobBuilderType builder.Builder type BobBuilderType builder.Builder
// BobBuilder interface wraps the ToSQL method // BobBuilder interface wraps the ToSql method
type BobBuilder interface { type BobBuilder interface {
ToSQL() (string, []interface{}, error) ToSql() (string, []interface{}, error)
} }
// CreateTable creates a table with CreateBuilder interface // CreateTable creates a table with CreateBuilder interface

View File

@ -61,14 +61,14 @@ func (b CreateBuilder) Unique(column string) CreateBuilder {
return builder.Set(b, "Unique", column).(CreateBuilder) return builder.Set(b, "Unique", column).(CreateBuilder)
} }
// ToSQL returns 3 variables filled out with the correct values based on bindings, etc. // ToSql returns 3 variables filled out with the correct values based on bindings, etc.
func (b CreateBuilder) ToSQL() (string, []interface{}, error) { func (b CreateBuilder) ToSql() (string, []interface{}, error) {
data := builder.GetStruct(b).(createData) data := builder.GetStruct(b).(createData)
return data.ToSQL() return data.ToSql()
} }
// ToSQL returns 3 variables filled out with the correct values based on bindings, etc. // ToSql returns 3 variables filled out with the correct values based on bindings, etc.
func (d *createData) ToSQL() (sqlStr string, args []interface{}, err error) { func (d *createData) ToSql() (sqlStr string, args []interface{}, err error) {
if len(d.TableName) == 0 || d.TableName == "" { if len(d.TableName) == 0 || d.TableName == "" {
err = errors.New("create statements must specify a table") err = errors.New("create statements must specify a table")
return return

View File

@ -8,7 +8,7 @@ import (
func TestCreate(t *testing.T) { func TestCreate(t *testing.T) {
t.Run("should return correct sql string with basic columns and types", func(t *testing.T) { t.Run("should return correct sql string with basic columns and types", func(t *testing.T) {
sql, _, err := bob.CreateTable("users").Columns("name", "password", "date").Types("varchar(255)", "text", "date").ToSQL() sql, _, err := bob.CreateTable("users").Columns("name", "password", "date").Types("varchar(255)", "text", "date").ToSql()
if err != nil { if err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
} }
@ -24,7 +24,7 @@ func TestCreate(t *testing.T) {
Types("uuid", "varchar(255)", "varchar(255)", "text", "date"). Types("uuid", "varchar(255)", "varchar(255)", "text", "date").
Primary("id"). Primary("id").
Unique("email"). Unique("email").
ToSQL() ToSql()
if err != nil { if err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
} }
@ -35,7 +35,7 @@ func TestCreate(t *testing.T) {
}) })
t.Run("should be able to have a schema name", func(t *testing.T) { t.Run("should be able to have a schema name", func(t *testing.T) {
sql, _, err := bob.CreateTable("users").WithSchema("private").Columns("name", "password", "date").Types("varchar(255)", "text", "date").ToSQL() sql, _, err := bob.CreateTable("users").WithSchema("private").Columns("name", "password", "date").Types("varchar(255)", "text", "date").ToSql()
if err != nil { if err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
} }
@ -49,35 +49,35 @@ func TestCreate(t *testing.T) {
_, _, err := bob.CreateTable("users"). _, _, err := bob.CreateTable("users").
Columns("id", "name", "email", "password", "date"). Columns("id", "name", "email", "password", "date").
Types("uuid", "varchar(255)", "varchar(255)", "date"). Types("uuid", "varchar(255)", "varchar(255)", "date").
ToSQL() ToSql()
if err.Error() != "columns and types should have equal length" { if err.Error() != "columns and types should have equal length" {
t.Fatal("should throw an error, it didn't:", err.Error()) t.Fatal("should throw an error, it didn't:", err.Error())
} }
}) })
t.Run("should emit error on empty table name", func(t *testing.T) { t.Run("should emit error on empty table name", func(t *testing.T) {
_, _, err := bob.CreateTable("").Columns("name").Types("text").ToSQL() _, _, err := bob.CreateTable("").Columns("name").Types("text").ToSql()
if err.Error() != "create statements must specify a table" { if err.Error() != "create statements must specify a table" {
t.Fatal("should throw an error, it didn't:", err.Error()) t.Fatal("should throw an error, it didn't:", err.Error())
} }
}) })
t.Run("should emit error for primary key not in columns", func(t *testing.T) { t.Run("should emit error for primary key not in columns", func(t *testing.T) {
_, _, err := bob.CreateTable("users").Columns("name").Types("text").Primary("id").ToSQL() _, _, err := bob.CreateTable("users").Columns("name").Types("text").Primary("id").ToSql()
if err.Error() != "supplied primary column name doesn't exists on columns" { if err.Error() != "supplied primary column name doesn't exists on columns" {
t.Fatal("should throw an error, it didn't:", err.Error()) t.Fatal("should throw an error, it didn't:", err.Error())
} }
}) })
t.Run("should emit error for unique key not in columns", func(t *testing.T) { t.Run("should emit error for unique key not in columns", func(t *testing.T) {
_, _, err := bob.CreateTable("users").Columns("name").Types("text").Unique("id").ToSQL() _, _, err := bob.CreateTable("users").Columns("name").Types("text").Unique("id").ToSql()
if err.Error() != "supplied unique column name doesn't exists on columns" { if err.Error() != "supplied unique column name doesn't exists on columns" {
t.Fatal("should throw an error, it didn't:", err.Error()) t.Fatal("should throw an error, it didn't:", err.Error())
} }
}) })
t.Run("should emit create if not exists", func(t *testing.T) { t.Run("should emit create if not exists", func(t *testing.T) {
sql, _, err := bob.CreateTableIfNotExists("users").Columns("name").Types("text").ToSQL() sql, _, err := bob.CreateTableIfNotExists("users").Columns("name").Types("text").ToSql()
if err != nil { if err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
} }

10
has.go
View File

@ -44,14 +44,14 @@ func (h HasBuilder) PlaceholderFormat(f string) HasBuilder {
return builder.Set(h, "Placeholder", f).(HasBuilder) return builder.Set(h, "Placeholder", f).(HasBuilder)
} }
// ToSQL returns 3 variables filled out with the correct values based on bindings, etc. // ToSql returns 3 variables filled out with the correct values based on bindings, etc.
func (h HasBuilder) ToSQL() (string, []interface{}, error) { func (h HasBuilder) ToSql() (string, []interface{}, error) {
data := builder.GetStruct(h).(hasData) data := builder.GetStruct(h).(hasData)
return data.ToSQL() return data.ToSql()
} }
// ToSQL returns 3 variables filled out with the correct values based on bindings, etc. // ToSql returns 3 variables filled out with the correct values based on bindings, etc.
func (d *hasData) ToSQL() (sqlStr string, args []interface{}, err error) { func (d *hasData) ToSql() (sqlStr string, args []interface{}, err error) {
sql := &bytes.Buffer{} sql := &bytes.Buffer{}
if d.Name == "" { if d.Name == "" {
err = errors.New("has statement should have a table name") err = errors.New("has statement should have a table name")

View File

@ -10,7 +10,7 @@ import (
func TestHas(t *testing.T) { func TestHas(t *testing.T) {
t.Run("should be able to create a hasTable query", func(t *testing.T) { t.Run("should be able to create a hasTable query", func(t *testing.T) {
sql, args, err := bob.HasTable("users").ToSQL() sql, args, err := bob.HasTable("users").ToSql()
if err != nil { if err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
} }
@ -26,7 +26,7 @@ func TestHas(t *testing.T) {
}) })
t.Run("should be able to create a hasColumn query", func(t *testing.T) { t.Run("should be able to create a hasColumn query", func(t *testing.T) {
sql, args, err := bob.HasTable("users").HasColumn("name").ToSQL() sql, args, err := bob.HasTable("users").HasColumn("name").ToSql()
if err != nil { if err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
} }
@ -42,7 +42,7 @@ func TestHas(t *testing.T) {
}) })
t.Run("should be able to create a hasColumn query (but reversed)", func(t *testing.T) { t.Run("should be able to create a hasColumn query (but reversed)", func(t *testing.T) {
sql, args, err := bob.HasColumn("name").HasTable("users").ToSQL() sql, args, err := bob.HasColumn("name").HasTable("users").ToSql()
if err != nil { if err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
} }
@ -58,7 +58,7 @@ func TestHas(t *testing.T) {
}) })
t.Run("should be able to create a hasTable query with schema", func(t *testing.T) { t.Run("should be able to create a hasTable query with schema", func(t *testing.T) {
sql, args, err := bob.HasTable("users").WithSchema("private").ToSQL() sql, args, err := bob.HasTable("users").WithSchema("private").ToSql()
if err != nil { if err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
} }
@ -74,7 +74,7 @@ func TestHas(t *testing.T) {
}) })
t.Run("should be able to have a different placeholder", func(t *testing.T) { t.Run("should be able to have a different placeholder", func(t *testing.T) {
sql, args, err := bob.HasTable("users").HasColumn("name").PlaceholderFormat(bob.Dollar).ToSQL() sql, args, err := bob.HasTable("users").HasColumn("name").PlaceholderFormat(bob.Dollar).ToSql()
if err != nil { if err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
} }
@ -90,7 +90,7 @@ func TestHas(t *testing.T) {
}) })
t.Run("should expect an error for no table name", func(t *testing.T) { t.Run("should expect an error for no table name", func(t *testing.T) {
_, _, err := bob.HasTable("").ToSQL() _, _, err := bob.HasTable("").ToSql()
if err.Error() != "has statement should have a table name" { if err.Error() != "has statement should have a table name" {
t.Fatal("error is different:", err.Error()) t.Fatal("error is different:", err.Error())
} }