Skip to content

Commit

Permalink
Merge pull request #51 from k1LoW/fix-sqlite-relation
Browse files Browse the repository at this point in the history
Fix SQLite relation support
  • Loading branch information
k1LoW authored Aug 7, 2018
2 parents 9349445 + b62db1d commit 4616d3c
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 13 deletions.
33 changes: 33 additions & 0 deletions drivers/sqlite/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"regexp"
)

var reFK = regexp.MustCompile(`FOREIGN KEY \((.+)\) REFERENCES ([^\s]+)\s?\((.+)\)`)

// Sqlite struct
type Sqlite struct{}

Expand Down Expand Up @@ -301,6 +303,37 @@ SELECT name, sql FROM sqlite_master WHERE type = 'trigger' AND tbl_name = ?;

s.Tables = tables

// Relations
for _, r := range relations {
result := reFK.FindAllStringSubmatch(r.Def, -1)
strColumns := strings.Split(result[0][1], ", ")
strParentTable := result[0][2]
strParentColumns := strings.Split(result[0][3], ", ")
for _, c := range strColumns {
column, err := r.Table.FindColumnByName(c)
if err != nil {
return err
}
r.Columns = append(r.Columns, column)
column.ParentRelations = append(column.ParentRelations, r)
}
parentTable, err := s.FindTableByName(strParentTable)
if err != nil {
return err
}
r.ParentTable = parentTable
for _, c := range strParentColumns {
column, err := parentTable.FindColumnByName(c)
if err != nil {
return err
}
r.ParentColumns = append(r.ParentColumns, column)
column.ChildRelations = append(column.ChildRelations, r)
}
}

s.Relations = relations

return nil
}

Expand Down
12 changes: 6 additions & 6 deletions output/md/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,7 @@ 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{
"/": &assets.File{
Path: "/",
FileMode: 0x800001ed,
Mtime: time.Unix(1532785399, 1532785399000000000),
Data: nil,
}, "/index.md.tmpl": &assets.File{
"/index.md.tmpl": &assets.File{
Path: "/index.md.tmpl",
FileMode: 0x1a4,
Mtime: time.Unix(1532239511, 1532239511000000000),
Expand All @@ -26,4 +21,9 @@ 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,
}}, "")
4 changes: 2 additions & 2 deletions sample/sqlite/comment_stars.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ CREATE TABLE comment_stars (
| ---- | ---- | ------- | -------- | -------- | ------- | ------- |
| id | INTEGER | | true | [logs](logs.md) | | |
| user_id | INTEGER | | false | | | |
| comment_post_id | INTEGER | | false | | | |
| comment_user_id | INTEGER | | false | | | |
| comment_post_id | INTEGER | | false | | [comments](comments.md) | |
| comment_user_id | INTEGER | | false | | [users](users.md) [comments](comments.md) | |
| created | NUMERIC | | false | | | |
| updated | NUMERIC | | true | | | |

Expand Down
Binary file modified sample/sqlite/comment_stars.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions sample/sqlite/comments.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ CREATE TABLE comments (
| Name | Type | Default | Nullable | Children | Parents | Comment |
| ---- | ---- | ------- | -------- | -------- | ------- | ------- |
| id | INTEGER | | true | [logs](logs.md) | | |
| post_id | INTEGER | | false | | | |
| user_id | INTEGER | | false | | | |
| post_id | INTEGER | | false | [comment_stars](comment_stars.md) | [posts](posts.md) | |
| user_id | INTEGER | | false | [comment_stars](comment_stars.md) | [users](users.md) | |
| comment | TEXT | | false | | | |
| created | NUMERIC | | false | | | |
| updated | NUMERIC | | true | | | |
Expand Down
Binary file modified sample/sqlite/comments.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions sample/sqlite/posts.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ CREATE TABLE posts (

| Name | Type | Default | Nullable | Children | Parents | Comment |
| ---- | ---- | ------- | -------- | -------- | ------- | ------- |
| id | INTEGER | | true | [logs](logs.md) | | |
| user_id | INTEGER | | false | | | |
| id | INTEGER | | true | [comments](comments.md) [logs](logs.md) | | |
| user_id | INTEGER | | false | | [users](users.md) | |
| title | TEXT | | false | | | |
| body | TEXT | | false | | | post body |
| created | NUMERIC | | false | | | |
Expand Down
Binary file modified sample/sqlite/posts.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified sample/sqlite/schema.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion sample/sqlite/users.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ CREATE TABLE users (

| Name | Type | Default | Nullable | Children | Parents | Comment |
| ---- | ---- | ------- | -------- | -------- | ------- | ------- |
| id | INTEGER | | true | [logs](logs.md) | | |
| id | INTEGER | | true | [posts](posts.md) [comments](comments.md) [comment_stars](comment_stars.md) [logs](logs.md) | | |
| username | TEXT | | false | | | |
| password | TEXT | | false | | | |
| email | TEXT | | false | | | |
Expand Down
Binary file modified sample/sqlite/users.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 4616d3c

Please sign in to comment.