Skip to content

Commit

Permalink
Support CHECK constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
k1LoW committed Aug 6, 2018
1 parent 9508ad8 commit 2575ccb
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 8 deletions.
46 changes: 45 additions & 1 deletion drivers/sqlite/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/k1LoW/tbls/schema"
"github.com/pkg/errors"
"regexp"
)

// Sqlite struct
Expand Down Expand Up @@ -311,7 +312,50 @@ func convertColumnNullable(str string) bool {
}

func parseCheckConstraints(sql string) []*schema.Constraint {
// TODO
// tokenize
re := regexp.MustCompile(`\s+`)
separator := "__SEP__"
space := "__SP__"
r1 := strings.NewReplacer("(", fmt.Sprintf("%s(%s", separator, separator), ")", fmt.Sprintf("%s)%s", separator, separator), ",", fmt.Sprintf("%s,%s", separator, separator))
r2 := strings.NewReplacer(" ", fmt.Sprintf("%s%s%s", separator, space, separator))
tokens := strings.Split(r1.Replace(r2.Replace(re.ReplaceAllString(sql, " "))), separator)

r3 := strings.NewReplacer(space, " ")
constraints := []*schema.Constraint{}
def := ""
counter := 0
for _, v := range tokens {
if counter == 0 && (v == "CHECK" || v == "check") {
def = v
continue
}
if def != "" && v == space {
def = def + v
continue
}
if def != "" && v == "(" {
def = def + v
counter = counter + 1
continue
}
if def != "" && v == ")" {
def = def + v
counter = counter - 1
if counter == 0 {
constraint := &schema.Constraint{
Name: "-",
Type: "CHECK",
Def: r3.Replace(def),
}
constraints = append(constraints, constraint)
def = ""
}
continue
}
if def != "" && counter > 0 {
def = def + v
}
}

return constraints
}
2 changes: 1 addition & 1 deletion drivers/sqlite/sqlite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,6 @@ func TestParseCheckConstraints(t *testing.T) {
}
actual := parseCheckConstraints(sql)
if !reflect.DeepEqual(actual, expected) {
t.Errorf("got: %v\nwant: %v", actual, expected)
t.Errorf("got: %#v\nwant: %#v", actual, expected)
}
}
12 changes: 6 additions & 6 deletions output/md/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ var _Assetsac44302fb6150a621aa9d04a0350aac972bf7e18 = "# {{ .Table.Name }}\n\n##

// Assets returns go-assets FileSystem
var Assets = assets.NewFileSystem(map[string][]string{"/": []string{"index.md.tmpl", "table.md.tmpl"}}, map[string]*assets.File{
"/index.md.tmpl": &assets.File{
"/": &assets.File{
Path: "/",
FileMode: 0x800001ed,
Mtime: time.Unix(1532785399, 1532785399000000000),
Data: nil,
}, "/index.md.tmpl": &assets.File{
Path: "/index.md.tmpl",
FileMode: 0x1a4,
Mtime: time.Unix(1532239511, 1532239511000000000),
Expand All @@ -21,9 +26,4 @@ var Assets = assets.NewFileSystem(map[string][]string{"/": []string{"index.md.tm
FileMode: 0x1a4,
Mtime: time.Unix(1532785399, 1532785399000000000),
Data: []byte(_Assetsac44302fb6150a621aa9d04a0350aac972bf7e18),
}, "/": &assets.File{
Path: "/",
FileMode: 0x800001ed,
Mtime: time.Unix(1532785399, 1532785399000000000),
Data: nil,
}}, "")
5 changes: 5 additions & 0 deletions sample/sqlite/check_constraints.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ CREATE TABLE check_constraints (
| sqlite_autoindex_check_constraints_3 | UNIQUE | UNIQUE (downcase) |
| sqlite_autoindex_check_constraints_2 | UNIQUE | UNIQUE (checkcheck) |
| sqlite_autoindex_check_constraints_1 | UNIQUE | UNIQUE (brackets) |
| - | CHECK | CHECK(length(col) > 4) |
| - | CHECK | CHECK(((length(brackets) > 4))) |
| - | CHECK | CHECK(length(checkcheck) > 4) |
| - | CHECK | check(length(downcase) > 4) |
| - | CHECK | check(length(nl) > 4 OR nl != 'ln') |

## Indexes

Expand Down
1 change: 1 addition & 0 deletions sample/sqlite/users.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ CREATE TABLE users (
| id | PRIMARY KEY | PRIMARY KEY (id) |
| sqlite_autoindex_users_2 | UNIQUE | UNIQUE (email) |
| sqlite_autoindex_users_1 | UNIQUE | UNIQUE (username) |
| - | CHECK | CHECK(length(username) > 4) |

## Indexes

Expand Down

0 comments on commit 2575ccb

Please sign in to comment.