feat: added CreateTableIfNotExists

This commit is contained in:
Reinaldy Rafli 2021-07-09 13:39:30 +07:00
parent 724b9432be
commit e675c80934
5 changed files with 39 additions and 50 deletions

View File

@ -1,42 +0,0 @@
package bob
import "io"
// appendToSQL - Documentation coming soon
func appendToSQL(parts []BobBuilder, w io.Writer, sep string, args []interface{}) ([]interface{}, error) {
for i, p := range parts {
partSQL, partArgs, err := p.ToSQL()
if err != nil {
return nil, err
} else if len(partSQL) == 0 {
continue
}
if i > 0 {
_, err := io.WriteString(w, sep)
if err != nil {
return nil, err
}
}
_, err = io.WriteString(w, partSQL)
if err != nil {
return nil, err
}
args = append(args, partArgs...)
}
return args, nil
}
// createArgs should create an argument []interface{} for SQL query
// I'm using the idiot approach for creating args
func createArgs(keys ...string) []interface{} {
var args []interface{}
for _, v := range keys {
if v == "" {
continue
}
args = append(args, v)
}
return args
}

9
bob.go
View File

@ -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)
@ -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)

View File

@ -13,6 +13,7 @@ type CreateBuilder builder.Builder
type createData struct {
TableName string
IfNotExists bool
Schema string
Columns []string
Types []string
@ -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)
@ -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 + "\".")
}

View File

@ -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
has.go
View File

@ -4,6 +4,7 @@ import (
"bytes"
"errors"
"github.com/aldy505/bob/util"
"github.com/lann/builder"
)
@ -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
}