Skip to content

Commit

Permalink
feat: added CreateTableIfNotExists
Browse files Browse the repository at this point in the history
  • Loading branch information
aldy505 committed Jul 9, 2021
1 parent 724b943 commit e675c80
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 50 deletions.
42 changes: 0 additions & 42 deletions append.go

This file was deleted.

9 changes: 9 additions & 0 deletions bob.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ func (b BobBuilderType) CreateTable(table string) CreateBuilder {
return CreateBuilder(b).Name(table)
}

func (b BobBuilderType) CreateTableIfNotExists(table string) CreateBuilder {
return CreateBuilder(b).Name(table).IfNotExists()
}

// HasTable checks if a table exists with HasBuilder interface
func (b BobBuilderType) HasTable(table string) HasBuilder {
return HasBuilder(b).HasTable(table)
Expand All @@ -33,6 +37,11 @@ func CreateTable(table string) CreateBuilder {
return BobStmtBuilder.CreateTable(table)
}

// CreateTableIfNotExists creates a table with CreateBuilder interface, if the table doesn't exists
func CreateTableIfNotExists(table string) CreateBuilder {
return BobStmtBuilder.CreateTableIfNotExists(table)
}

// HasTable checks if a table exists with HasBuilder interface
func HasTable(table string) HasBuilder {
return BobStmtBuilder.HasTable(table)
Expand Down
24 changes: 17 additions & 7 deletions create.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ import (
type CreateBuilder builder.Builder

type createData struct {
TableName string
Schema string
Columns []string
Types []string
Primary string
Unique string
NotNull []string
TableName string
IfNotExists bool
Schema string
Columns []string
Types []string
Primary string
Unique string
NotNull []string
}

func init() {
Expand All @@ -30,6 +31,11 @@ func (b CreateBuilder) Name(name string) CreateBuilder {
return builder.Set(b, "TableName", name).(CreateBuilder)
}

// IfNotExists adds IF NOT EXISTS to the query
func (b CreateBuilder) IfNotExists() CreateBuilder {
return builder.Set(b, "IfNotExists", true).(CreateBuilder)
}

// WithSchema specifies the schema to be used when using the schema-building commands.
func (b CreateBuilder) WithSchema(name string) CreateBuilder {
return builder.Set(b, "Schema", name).(CreateBuilder)
Expand Down Expand Up @@ -77,6 +83,10 @@ func (d *createData) ToSQL() (sqlStr string, args []interface{}, err error) {

sql.WriteString("CREATE TABLE ")

if d.IfNotExists {
sql.WriteString("IF NOT EXISTS ")
}

if d.Schema != "" {
sql.WriteString("\"" + d.Schema + "\".")
}
Expand Down
11 changes: 11 additions & 0 deletions create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,15 @@ func TestCreate(t *testing.T) {
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()
if err != nil {
t.Fatal(err.Error())
}
result := "CREATE TABLE IF NOT EXISTS \"users\" (\"name\" text);"
if sql != result {
t.Fatal("sql is not equal to result: ", sql)
}
})
}
3 changes: 2 additions & 1 deletion has.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"errors"

"github.com/aldy505/bob/util"
"github.com/lann/builder"
)

Expand Down Expand Up @@ -71,6 +72,6 @@ func (d *hasData) ToSQL() (sqlStr string, args []interface{}, err error) {
}

sqlStr = ReplacePlaceholder(sql.String(), d.Placeholder)
args = createArgs(d.Name, d.Column, d.Schema)
args = util.CreateArgs(d.Name, d.Column, d.Schema)
return
}

0 comments on commit e675c80

Please sign in to comment.