Skip to content

Commit f1008bd

Browse files
author
Reinaldy Rafli
authored
Merge pull request #7 from aldy505/feat/drop_cascade
feat: drop cascade & restrict
2 parents efa752f + 8a461fe commit f1008bd

File tree

10 files changed

+135
-78
lines changed

10 files changed

+135
-78
lines changed

.github/workflows/build.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jobs:
99
strategy:
1010
matrix:
1111
os: [ubuntu-latest, macos-latest, windows-latest]
12-
go-version: [1.15.x, 1.16.x]
12+
go-version: [1.16.x, 1.17.x]
1313
steps:
1414
- name: Checkout code
1515
uses: actions/checkout@v2

.github/workflows/coverage.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
- name: Install Go
1515
uses: actions/setup-go@v2
1616
with:
17-
go-version: 1.16.x
17+
go-version: 1.17.x
1818

1919
- name: Run coverage
2020
run: go test -v -race -coverprofile=coverage.out -covermode=atomic -failfast

README.md

+19-2
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,28 @@ func main() {
8989
if err != nil {
9090
log.Fatal(err)
9191
}
92+
// sql = "DROP TABLE users;"
93+
94+
sql, _, err = bob.DropTableIfExists("users").ToSql()
95+
if err != nil {
96+
log.Fatal(err)
97+
}
98+
// sql = "DROP TABLE IF EXISTS users;"
99+
100+
sql, _, err = bob.DropTable("users").Cascade().ToSql()
101+
if err != nil {
102+
log.Fatal(err)
103+
}
104+
// sql = "DROP TABLE users CASCADE;"
105+
106+
sql, _, err = bob.DropTable("users").Restrict().ToSql()
107+
if err != nil {
108+
log.Fatal(err)
109+
}
110+
// sql = "DROP TABLE users RESTRICT;"
92111
}
93112
```
94113

95-
You could also do `bob.DropTableIfExists("users")` to output a `DROP TABLE IF EXISTS "users"` query.
96-
97114
### Truncate table
98115

99116
```go

drop.go

+21-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package bob
22

33
import (
4-
"bytes"
54
"errors"
5+
"strings"
66

77
"github.com/lann/builder"
88
)
@@ -12,6 +12,8 @@ type DropBuilder builder.Builder
1212
type dropData struct {
1313
TableName string
1414
IfExists bool
15+
Cascade bool
16+
Restrict bool
1517
}
1618

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

32+
func (b DropBuilder) Cascade() DropBuilder {
33+
return builder.Set(b, "Cascade", true).(DropBuilder)
34+
}
35+
36+
func (b DropBuilder) Restrict() DropBuilder {
37+
return builder.Set(b, "Restrict", true).(DropBuilder)
38+
}
39+
3040
// ToSql returns 3 variables filled out with the correct values based on bindings, etc.
3141
func (b DropBuilder) ToSql() (string, []interface{}, error) {
3242
data := builder.GetStruct(b).(dropData)
@@ -38,15 +48,23 @@ func (d *dropData) ToSql() (sqlStr string, args []interface{}, err error) {
3848
if len(d.TableName) == 0 || d.TableName == "" {
3949
err = errors.New("drop statement must specify a table")
4050
}
41-
sql := &bytes.Buffer{}
51+
var sql strings.Builder
4252

4353
sql.WriteString("DROP TABLE ")
4454

4555
if d.IfExists {
4656
sql.WriteString("IF EXISTS ")
4757
}
4858

49-
sql.WriteString("\"" + d.TableName + "\";")
59+
sql.WriteString("\"" + d.TableName + "\"")
60+
61+
if d.Cascade {
62+
sql.WriteString(" CASCADE")
63+
} else if d.Restrict {
64+
sql.WriteString(" RESTRICT")
65+
}
66+
67+
sql.WriteString(";")
5068

5169
sqlStr = sql.String()
5270
return

drop_test.go

+53-31
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,57 @@ import (
66
"github.com/aldy505/bob"
77
)
88

9-
func TestDrop(t *testing.T) {
10-
t.Run("should be able to create regular drop query", func(t *testing.T) {
11-
sql, _, err := bob.DropTable("users").ToSql()
12-
if err != nil {
13-
t.Error(err)
14-
}
15-
16-
result := "DROP TABLE \"users\";"
17-
if sql != result {
18-
t.Error("sql is not the same as result: ", sql)
19-
}
20-
})
21-
22-
t.Run("should be able to create drop if exists query", func(t *testing.T) {
23-
sql, _, err := bob.DropTableIfExists("users").ToSql()
24-
if err != nil {
25-
t.Error(err)
26-
}
27-
28-
result := "DROP TABLE IF EXISTS \"users\";"
29-
if sql != result {
30-
t.Error("sql is not the same as result: ", sql)
31-
}
32-
})
33-
34-
t.Run("should expect an error for no table name", func(t *testing.T) {
35-
_, _, err := bob.DropTableIfExists("").ToSql()
36-
if err == nil && err.Error() != "drop statement must specify a table" {
37-
t.Error(err)
38-
}
39-
})
9+
func TestDrop_Regular(t *testing.T) {
10+
sql, _, err := bob.DropTable("users").ToSql()
11+
if err != nil {
12+
t.Error(err)
13+
}
14+
15+
result := "DROP TABLE \"users\";"
16+
if sql != result {
17+
t.Error("sql is not the same as result: ", sql)
18+
}
19+
}
20+
21+
func TestDrop_IfExists(t *testing.T) {
22+
sql, _, err := bob.DropTableIfExists("users").ToSql()
23+
if err != nil {
24+
t.Error(err)
25+
}
26+
27+
result := "DROP TABLE IF EXISTS \"users\";"
28+
if sql != result {
29+
t.Error("sql is not the same as result: ", sql)
30+
}
31+
}
32+
33+
func TestDrop_Cascade(t *testing.T) {
34+
sql, _, err := bob.DropTable("users").Cascade().ToSql()
35+
if err != nil {
36+
t.Error(err)
37+
}
38+
39+
result := "DROP TABLE \"users\" CASCADE;"
40+
if sql != result {
41+
t.Error("sql is not the same as result: ", sql)
42+
}
43+
}
44+
45+
func TestDrop_Restrict(t *testing.T) {
46+
sql, _, err := bob.DropTable("users").Restrict().ToSql()
47+
if err != nil {
48+
t.Error(err)
49+
}
50+
51+
result := "DROP TABLE \"users\" RESTRICT;"
52+
if sql != result {
53+
t.Error("sql is not the same as result: ", sql)
54+
}
55+
}
56+
57+
func TestDrop_ErrNoTable(t *testing.T) {
58+
_, _, err := bob.DropTableIfExists("").ToSql()
59+
if err == nil && err.Error() != "drop statement must specify a table" {
60+
t.Error(err)
61+
}
4062
}

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/aldy505/bob
22

3-
go 1.16
3+
go 1.17
44

55
require (
66
github.com/lann/builder v0.0.0-20180802200727-47ae307949d0

has.go

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

7-
"github.com/aldy505/bob/util"
87
"github.com/lann/builder"
98
)
109

@@ -21,12 +20,14 @@ func init() {
2120
builder.Register(HasBuilder{}, hasData{})
2221
}
2322

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

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

7172
sqlStr = ReplacePlaceholder(sql.String(), d.Placeholder)
72-
args = util.CreateArgs(d.Name, d.Column, d.Schema)
73+
args = createArgs(d.Name, d.Column, d.Schema)
7374
return
7475
}

internal.go

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package bob
2+
3+
// createArgs should create an argument []interface{} for SQL query
4+
// I'm using the idiot approach for creating args
5+
func createArgs(keys ...interface{}) []interface{} {
6+
var args []interface{}
7+
for _, v := range keys {
8+
if v == "" {
9+
continue
10+
}
11+
args = append(args, v)
12+
}
13+
return args
14+
}
15+
16+
// isIn checks if an array have a value
17+
func isIn(arr []string, value string) bool {
18+
for _, item := range arr {
19+
if item == value {
20+
return true
21+
}
22+
}
23+
return false
24+
}
25+
26+
// findPosition search for value position on an array
27+
func findPosition(arr []string, value string) int {
28+
for i, item := range arr {
29+
if item == value {
30+
return i
31+
}
32+
}
33+
return -1
34+
}

util/arguments.go

-14
This file was deleted.

util/check.go

-21
This file was deleted.

0 commit comments

Comments
 (0)