From ef78c0b03a4f68ebb035596fe5de842e4122113d Mon Sep 17 00:00:00 2001 From: Reinaldy Rafli Date: Fri, 9 Jul 2021 13:41:34 +0700 Subject: [PATCH] fix(breaking): ToSQL > ToSql for compat with Squirrel --- README.md | 4 ++-- bob.go | 4 ++-- create.go | 10 +++++----- create_test.go | 16 ++++++++-------- has.go | 10 +++++----- has_test.go | 12 ++++++------ 6 files changed, 28 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index 5be13d1..3e45ae7 100644 --- a/README.md +++ b/README.md @@ -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) } @@ -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) } diff --git a/bob.go b/bob.go index a93172e..420e1e9 100644 --- a/bob.go +++ b/bob.go @@ -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 diff --git a/create.go b/create.go index bf4365f..4017b65 100644 --- a/create.go +++ b/create.go @@ -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 diff --git a/create_test.go b/create_test.go index d46f9d2..988f6e3 100644 --- a/create_test.go +++ b/create_test.go @@ -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()) } @@ -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()) } @@ -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()) } @@ -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()) } diff --git a/has.go b/has.go index 9ca6cc0..3f1616f 100644 --- a/has.go +++ b/has.go @@ -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") diff --git a/has_test.go b/has_test.go index 9ce92a1..02a4583 100644 --- a/has_test.go +++ b/has_test.go @@ -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()) } @@ -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()) } @@ -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()) } @@ -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()) } @@ -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()) } @@ -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()) }