Skip to content

Commit

Permalink
Merge pull request #7 from aldy505/feat/drop_cascade
Browse files Browse the repository at this point in the history
feat: drop cascade & restrict
  • Loading branch information
aldy505 committed Nov 5, 2021
2 parents efa752f + 8a461fe commit f1008bd
Show file tree
Hide file tree
Showing 10 changed files with 135 additions and 78 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
go-version: [1.15.x, 1.16.x]
go-version: [1.16.x, 1.17.x]
steps:
- name: Checkout code
uses: actions/checkout@v2
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: 1.16.x
go-version: 1.17.x

- name: Run coverage
run: go test -v -race -coverprofile=coverage.out -covermode=atomic -failfast
Expand Down
21 changes: 19 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,28 @@ func main() {
if err != nil {
log.Fatal(err)
}
// sql = "DROP TABLE users;"

sql, _, err = bob.DropTableIfExists("users").ToSql()
if err != nil {
log.Fatal(err)
}
// sql = "DROP TABLE IF EXISTS users;"

sql, _, err = bob.DropTable("users").Cascade().ToSql()
if err != nil {
log.Fatal(err)
}
// sql = "DROP TABLE users CASCADE;"

sql, _, err = bob.DropTable("users").Restrict().ToSql()
if err != nil {
log.Fatal(err)
}
// sql = "DROP TABLE users RESTRICT;"
}
```

You could also do `bob.DropTableIfExists("users")` to output a `DROP TABLE IF EXISTS "users"` query.

### Truncate table

```go
Expand Down
24 changes: 21 additions & 3 deletions drop.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package bob

import (
"bytes"
"errors"
"strings"

"github.com/lann/builder"
)
Expand All @@ -12,6 +12,8 @@ type DropBuilder builder.Builder
type dropData struct {
TableName string
IfExists bool
Cascade bool
Restrict bool
}

func init() {
Expand All @@ -27,6 +29,14 @@ func (b DropBuilder) ifExists() DropBuilder {
return builder.Set(b, "IfExists", true).(DropBuilder)
}

func (b DropBuilder) Cascade() DropBuilder {
return builder.Set(b, "Cascade", true).(DropBuilder)
}

func (b DropBuilder) Restrict() DropBuilder {
return builder.Set(b, "Restrict", true).(DropBuilder)
}

// ToSql returns 3 variables filled out with the correct values based on bindings, etc.
func (b DropBuilder) ToSql() (string, []interface{}, error) {
data := builder.GetStruct(b).(dropData)
Expand All @@ -38,15 +48,23 @@ func (d *dropData) ToSql() (sqlStr string, args []interface{}, err error) {
if len(d.TableName) == 0 || d.TableName == "" {
err = errors.New("drop statement must specify a table")
}
sql := &bytes.Buffer{}
var sql strings.Builder

sql.WriteString("DROP TABLE ")

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

sql.WriteString("\"" + d.TableName + "\";")
sql.WriteString("\"" + d.TableName + "\"")

if d.Cascade {
sql.WriteString(" CASCADE")
} else if d.Restrict {
sql.WriteString(" RESTRICT")
}

sql.WriteString(";")

sqlStr = sql.String()
return
Expand Down
84 changes: 53 additions & 31 deletions drop_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,57 @@ import (
"github.com/aldy505/bob"
)

func TestDrop(t *testing.T) {
t.Run("should be able to create regular drop query", func(t *testing.T) {
sql, _, err := bob.DropTable("users").ToSql()
if err != nil {
t.Error(err)
}

result := "DROP TABLE \"users\";"
if sql != result {
t.Error("sql is not the same as result: ", sql)
}
})

t.Run("should be able to create drop if exists query", func(t *testing.T) {
sql, _, err := bob.DropTableIfExists("users").ToSql()
if err != nil {
t.Error(err)
}

result := "DROP TABLE IF EXISTS \"users\";"
if sql != result {
t.Error("sql is not the same as result: ", sql)
}
})

t.Run("should expect an error for no table name", func(t *testing.T) {
_, _, err := bob.DropTableIfExists("").ToSql()
if err == nil && err.Error() != "drop statement must specify a table" {
t.Error(err)
}
})
func TestDrop_Regular(t *testing.T) {
sql, _, err := bob.DropTable("users").ToSql()
if err != nil {
t.Error(err)
}

result := "DROP TABLE \"users\";"
if sql != result {
t.Error("sql is not the same as result: ", sql)
}
}

func TestDrop_IfExists(t *testing.T) {
sql, _, err := bob.DropTableIfExists("users").ToSql()
if err != nil {
t.Error(err)
}

result := "DROP TABLE IF EXISTS \"users\";"
if sql != result {
t.Error("sql is not the same as result: ", sql)
}
}

func TestDrop_Cascade(t *testing.T) {
sql, _, err := bob.DropTable("users").Cascade().ToSql()
if err != nil {
t.Error(err)
}

result := "DROP TABLE \"users\" CASCADE;"
if sql != result {
t.Error("sql is not the same as result: ", sql)
}
}

func TestDrop_Restrict(t *testing.T) {
sql, _, err := bob.DropTable("users").Restrict().ToSql()
if err != nil {
t.Error(err)
}

result := "DROP TABLE \"users\" RESTRICT;"
if sql != result {
t.Error("sql is not the same as result: ", sql)
}
}

func TestDrop_ErrNoTable(t *testing.T) {
_, _, err := bob.DropTableIfExists("").ToSql()
if err == nil && err.Error() != "drop statement must specify a table" {
t.Error(err)
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/aldy505/bob

go 1.16
go 1.17

require (
github.com/lann/builder v0.0.0-20180802200727-47ae307949d0
Expand Down
9 changes: 5 additions & 4 deletions has.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"errors"

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

Expand All @@ -21,12 +20,14 @@ func init() {
builder.Register(HasBuilder{}, hasData{})
}

// HasTable checks for a table's existence by tableName, resolving with a boolean to signal if the table exists.
// HasTable checks for a table's existence by tableName,
// resolving with a boolean to signal if the table exists.
func (h HasBuilder) HasTable(table string) HasBuilder {
return builder.Set(h, "Name", table).(HasBuilder)
}

// HasColumn checks if a column exists in the current table, resolves the promise with a boolean, true if the column exists, false otherwise.
// HasColumn checks if a column exists in the current table,
// resolves the promise with a boolean, true if the column exists, false otherwise.
func (h HasBuilder) HasColumn(column string) HasBuilder {
return builder.Set(h, "Column", column).(HasBuilder)
}
Expand Down Expand Up @@ -69,6 +70,6 @@ func (d *hasData) ToSql() (sqlStr string, args []interface{}, err error) {
}

sqlStr = ReplacePlaceholder(sql.String(), d.Placeholder)
args = util.CreateArgs(d.Name, d.Column, d.Schema)
args = createArgs(d.Name, d.Column, d.Schema)
return
}
34 changes: 34 additions & 0 deletions internal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package bob

// createArgs should create an argument []interface{} for SQL query
// I'm using the idiot approach for creating args
func createArgs(keys ...interface{}) []interface{} {
var args []interface{}
for _, v := range keys {
if v == "" {
continue
}
args = append(args, v)
}
return args
}

// isIn checks if an array have a value
func isIn(arr []string, value string) bool {
for _, item := range arr {
if item == value {
return true
}
}
return false
}

// findPosition search for value position on an array
func findPosition(arr []string, value string) int {
for i, item := range arr {
if item == value {
return i
}
}
return -1
}
14 changes: 0 additions & 14 deletions util/arguments.go

This file was deleted.

21 changes: 0 additions & 21 deletions util/check.go

This file was deleted.

0 comments on commit f1008bd

Please sign in to comment.