Skip to content

Commit

Permalink
fix(breaking): ToSQL > ToSql for compat with Squirrel
Browse files Browse the repository at this point in the history
  • Loading branch information
Reinaldy Rafli committed Jul 9, 2021
1 parent e675c80 commit ef78c0b
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 28 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func main() {
db := pgx.Connect()

// 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 {
log.Fatal(err)
}
Expand All @@ -47,7 +47,7 @@ func main() {
Types("varchar(36)", "varchar(255)", "varchar(255)", "text", "date").
Primary("id").
Unique("email")
ToSQL()
ToSql()
if err != nil {
log.Fatal(err)
}
Expand Down
4 changes: 2 additions & 2 deletions bob.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import "github.com/lann/builder"
// BobBuilderType is the type for BobBuilder
type BobBuilderType builder.Builder

// BobBuilder interface wraps the ToSQL method
// BobBuilder interface wraps the ToSql method
type BobBuilder interface {
ToSQL() (string, []interface{}, error)
ToSql() (string, []interface{}, error)
}

// CreateTable creates a table with CreateBuilder interface
Expand Down
10 changes: 5 additions & 5 deletions create.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@ func (b CreateBuilder) Unique(column string) CreateBuilder {
return builder.Set(b, "Unique", column).(CreateBuilder)
}

// ToSQL returns 3 variables filled out with the correct values based on bindings, etc.
func (b CreateBuilder) ToSQL() (string, []interface{}, error) {
// ToSql returns 3 variables filled out with the correct values based on bindings, etc.
func (b CreateBuilder) ToSql() (string, []interface{}, error) {
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.
func (d *createData) ToSQL() (sqlStr string, args []interface{}, err error) {
// ToSql returns 3 variables filled out with the correct values based on bindings, etc.
func (d *createData) ToSql() (sqlStr string, args []interface{}, err error) {
if len(d.TableName) == 0 || d.TableName == "" {
err = errors.New("create statements must specify a table")
return
Expand Down
16 changes: 8 additions & 8 deletions create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

func TestCreate(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 {
t.Fatal(err.Error())
}
Expand All @@ -24,7 +24,7 @@ func TestCreate(t *testing.T) {
Types("uuid", "varchar(255)", "varchar(255)", "text", "date").
Primary("id").
Unique("email").
ToSQL()
ToSql()
if err != nil {
t.Fatal(err.Error())
}
Expand All @@ -35,7 +35,7 @@ func TestCreate(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 {
t.Fatal(err.Error())
}
Expand All @@ -49,35 +49,35 @@ func TestCreate(t *testing.T) {
_, _, err := bob.CreateTable("users").
Columns("id", "name", "email", "password", "date").
Types("uuid", "varchar(255)", "varchar(255)", "date").
ToSQL()
ToSql()
if err.Error() != "columns and types should have equal length" {
t.Fatal("should throw an error, it didn't:", err.Error())
}
})

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" {
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) {
_, _, 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" {
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) {
_, _, 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" {
t.Fatal("should throw an error, it didn't:", err.Error())
}
})

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 {
t.Fatal(err.Error())
}
Expand Down
10 changes: 5 additions & 5 deletions has.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ func (h HasBuilder) PlaceholderFormat(f string) HasBuilder {
return builder.Set(h, "Placeholder", f).(HasBuilder)
}

// ToSQL returns 3 variables filled out with the correct values based on bindings, etc.
func (h HasBuilder) ToSQL() (string, []interface{}, error) {
// ToSql returns 3 variables filled out with the correct values based on bindings, etc.
func (h HasBuilder) ToSql() (string, []interface{}, error) {
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.
func (d *hasData) ToSQL() (sqlStr string, args []interface{}, err error) {
// ToSql returns 3 variables filled out with the correct values based on bindings, etc.
func (d *hasData) ToSql() (sqlStr string, args []interface{}, err error) {
sql := &bytes.Buffer{}
if d.Name == "" {
err = errors.New("has statement should have a table name")
Expand Down
12 changes: 6 additions & 6 deletions has_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

func TestHas(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 {
t.Fatal(err.Error())
}
Expand All @@ -26,7 +26,7 @@ func TestHas(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 {
t.Fatal(err.Error())
}
Expand All @@ -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) {
sql, args, err := bob.HasColumn("name").HasTable("users").ToSQL()
sql, args, err := bob.HasColumn("name").HasTable("users").ToSql()
if err != nil {
t.Fatal(err.Error())
}
Expand All @@ -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) {
sql, args, err := bob.HasTable("users").WithSchema("private").ToSQL()
sql, args, err := bob.HasTable("users").WithSchema("private").ToSql()
if err != nil {
t.Fatal(err.Error())
}
Expand All @@ -74,7 +74,7 @@ func TestHas(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 {
t.Fatal(err.Error())
}
Expand All @@ -90,7 +90,7 @@ func TestHas(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" {
t.Fatal("error is different:", err.Error())
}
Expand Down

0 comments on commit ef78c0b

Please sign in to comment.