Skip to content

Commit e675c80

Browse files
author
Reinaldy Rafli
committed
feat: added CreateTableIfNotExists
1 parent 724b943 commit e675c80

File tree

5 files changed

+39
-50
lines changed

5 files changed

+39
-50
lines changed

append.go

-42
This file was deleted.

bob.go

+9
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ func (b BobBuilderType) CreateTable(table string) CreateBuilder {
1515
return CreateBuilder(b).Name(table)
1616
}
1717

18+
func (b BobBuilderType) CreateTableIfNotExists(table string) CreateBuilder {
19+
return CreateBuilder(b).Name(table).IfNotExists()
20+
}
21+
1822
// HasTable checks if a table exists with HasBuilder interface
1923
func (b BobBuilderType) HasTable(table string) HasBuilder {
2024
return HasBuilder(b).HasTable(table)
@@ -33,6 +37,11 @@ func CreateTable(table string) CreateBuilder {
3337
return BobStmtBuilder.CreateTable(table)
3438
}
3539

40+
// CreateTableIfNotExists creates a table with CreateBuilder interface, if the table doesn't exists
41+
func CreateTableIfNotExists(table string) CreateBuilder {
42+
return BobStmtBuilder.CreateTableIfNotExists(table)
43+
}
44+
3645
// HasTable checks if a table exists with HasBuilder interface
3746
func HasTable(table string) HasBuilder {
3847
return BobStmtBuilder.HasTable(table)

create.go

+17-7
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@ import (
1212
type CreateBuilder builder.Builder
1313

1414
type createData struct {
15-
TableName string
16-
Schema string
17-
Columns []string
18-
Types []string
19-
Primary string
20-
Unique string
21-
NotNull []string
15+
TableName string
16+
IfNotExists bool
17+
Schema string
18+
Columns []string
19+
Types []string
20+
Primary string
21+
Unique string
22+
NotNull []string
2223
}
2324

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

34+
// IfNotExists adds IF NOT EXISTS to the query
35+
func (b CreateBuilder) IfNotExists() CreateBuilder {
36+
return builder.Set(b, "IfNotExists", true).(CreateBuilder)
37+
}
38+
3339
// WithSchema specifies the schema to be used when using the schema-building commands.
3440
func (b CreateBuilder) WithSchema(name string) CreateBuilder {
3541
return builder.Set(b, "Schema", name).(CreateBuilder)
@@ -77,6 +83,10 @@ func (d *createData) ToSQL() (sqlStr string, args []interface{}, err error) {
7783

7884
sql.WriteString("CREATE TABLE ")
7985

86+
if d.IfNotExists {
87+
sql.WriteString("IF NOT EXISTS ")
88+
}
89+
8090
if d.Schema != "" {
8191
sql.WriteString("\"" + d.Schema + "\".")
8292
}

create_test.go

+11
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,15 @@ func TestCreate(t *testing.T) {
7575
t.Fatal("should throw an error, it didn't:", err.Error())
7676
}
7777
})
78+
79+
t.Run("should emit create if not exists", func(t *testing.T) {
80+
sql, _, err := bob.CreateTableIfNotExists("users").Columns("name").Types("text").ToSQL()
81+
if err != nil {
82+
t.Fatal(err.Error())
83+
}
84+
result := "CREATE TABLE IF NOT EXISTS \"users\" (\"name\" text);"
85+
if sql != result {
86+
t.Fatal("sql is not equal to result: ", sql)
87+
}
88+
})
7889
}

has.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"errors"
66

7+
"github.com/aldy505/bob/util"
78
"github.com/lann/builder"
89
)
910

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

7374
sqlStr = ReplacePlaceholder(sql.String(), d.Placeholder)
74-
args = createArgs(d.Name, d.Column, d.Schema)
75+
args = util.CreateArgs(d.Name, d.Column, d.Schema)
7576
return
7677
}

0 commit comments

Comments
 (0)