mirror of https://github.com/aldy505/bob.git
feat: added CreateTableIfNotExists
This commit is contained in:
parent
724b9432be
commit
e675c80934
42
append.go
42
append.go
|
@ -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
9
bob.go
|
@ -15,6 +15,10 @@ func (b BobBuilderType) CreateTable(table string) CreateBuilder {
|
||||||
return CreateBuilder(b).Name(table)
|
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
|
// HasTable checks if a table exists with HasBuilder interface
|
||||||
func (b BobBuilderType) HasTable(table string) HasBuilder {
|
func (b BobBuilderType) HasTable(table string) HasBuilder {
|
||||||
return HasBuilder(b).HasTable(table)
|
return HasBuilder(b).HasTable(table)
|
||||||
|
@ -33,6 +37,11 @@ func CreateTable(table string) CreateBuilder {
|
||||||
return BobStmtBuilder.CreateTable(table)
|
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
|
// HasTable checks if a table exists with HasBuilder interface
|
||||||
func HasTable(table string) HasBuilder {
|
func HasTable(table string) HasBuilder {
|
||||||
return BobStmtBuilder.HasTable(table)
|
return BobStmtBuilder.HasTable(table)
|
||||||
|
|
10
create.go
10
create.go
|
@ -13,6 +13,7 @@ type CreateBuilder builder.Builder
|
||||||
|
|
||||||
type createData struct {
|
type createData struct {
|
||||||
TableName string
|
TableName string
|
||||||
|
IfNotExists bool
|
||||||
Schema string
|
Schema string
|
||||||
Columns []string
|
Columns []string
|
||||||
Types []string
|
Types []string
|
||||||
|
@ -30,6 +31,11 @@ func (b CreateBuilder) Name(name string) CreateBuilder {
|
||||||
return builder.Set(b, "TableName", name).(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.
|
// WithSchema specifies the schema to be used when using the schema-building commands.
|
||||||
func (b CreateBuilder) WithSchema(name string) CreateBuilder {
|
func (b CreateBuilder) WithSchema(name string) CreateBuilder {
|
||||||
return builder.Set(b, "Schema", name).(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 ")
|
sql.WriteString("CREATE TABLE ")
|
||||||
|
|
||||||
|
if d.IfNotExists {
|
||||||
|
sql.WriteString("IF NOT EXISTS ")
|
||||||
|
}
|
||||||
|
|
||||||
if d.Schema != "" {
|
if d.Schema != "" {
|
||||||
sql.WriteString("\"" + d.Schema + "\".")
|
sql.WriteString("\"" + d.Schema + "\".")
|
||||||
}
|
}
|
||||||
|
|
|
@ -75,4 +75,15 @@ func TestCreate(t *testing.T) {
|
||||||
t.Fatal("should throw an error, it didn't:", err.Error())
|
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
3
has.go
|
@ -4,6 +4,7 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
|
"github.com/aldy505/bob/util"
|
||||||
"github.com/lann/builder"
|
"github.com/lann/builder"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -71,6 +72,6 @@ func (d *hasData) ToSQL() (sqlStr string, args []interface{}, err error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
sqlStr = ReplacePlaceholder(sql.String(), d.Placeholder)
|
sqlStr = ReplacePlaceholder(sql.String(), d.Placeholder)
|
||||||
args = createArgs(d.Name, d.Column, d.Schema)
|
args = util.CreateArgs(d.Name, d.Column, d.Schema)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue