diff --git a/append.go b/append.go deleted file mode 100644 index a46a69d..0000000 --- a/append.go +++ /dev/null @@ -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 -} diff --git a/bob.go b/bob.go index caee221..a93172e 100644 --- a/bob.go +++ b/bob.go @@ -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) diff --git a/create.go b/create.go index 35dcd6e..bf4365f 100644 --- a/create.go +++ b/create.go @@ -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() { @@ -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 + "\".") } diff --git a/create_test.go b/create_test.go index be570f9..d46f9d2 100644 --- a/create_test.go +++ b/create_test.go @@ -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) + } + }) } diff --git a/has.go b/has.go index deccb71..9ca6cc0 100644 --- a/has.go +++ b/has.go @@ -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 }