Skip to content

Commit

Permalink
Merge pull request k1LoW#243 from k1LoW/mplus
Browse files Browse the repository at this point in the history
Add er.font option for png/jpg ER diagram
  • Loading branch information
k1LoW authored Jul 8, 2020
2 parents 1dc5cde + c646557 commit ecad363
Show file tree
Hide file tree
Showing 137 changed files with 1,592 additions and 1,077 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ doc: build doc_sqlite
./tbls doc my://root:mypass@localhost:33306/testdb -c testdata/test_tbls.yml -t svg -f sample/svg
./tbls doc my://root:mypass@localhost:33306/testdb -c testdata/exclude_test_tbls.yml -f sample/exclude
./tbls doc my://root:mypass@localhost:33306/testdb -c testdata/dict_test_tbls.yml -f sample/dict
./tbls doc my://root:mypass@localhost:33306/testdb -c testdata/font_test_tbls.yml -f sample/font

doc_sqlite:
./tbls doc sq://$(PWD)/testdata/testdb.sqlite3 -c testdata/test_tbls.yml -f sample/sqlite
Expand All @@ -61,6 +62,7 @@ testdoc: testdoc_sqlite
./tbls diff my://root:mypass@localhost:33306/testdb -c testdata/test_tbls.yml -t svg sample/svg
./tbls diff my://root:mypass@localhost:33306/testdb -c testdata/exclude_test_tbls.yml sample/exclude
./tbls diff my://root:mypass@localhost:33306/testdb -c testdata/dict_test_tbls.yml sample/dict
./tbls diff my://root:mypass@localhost:33306/testdb -c testdata/font_test_tbls.yml sample/font

testdoc_sqlite:
./tbls diff sq://$(PWD)/testdata/testdb.sqlite3 -c testdata/test_tbls.yml sample/sqlite
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ er:
# Skip generation of ER diagram
# Default is false
skip: false
# ER diagram format
# ER diagram image format
# Default is `png`
format: svg
# Add table/column comment to ER diagram
Expand All @@ -531,6 +531,9 @@ er:
# Distance between tables that display relations in the ER
# Default is 1
distance: 2
# ER diagram font (font name, font file, font path or keyword)
# Default is "" ( system default )
font: M+
```
### Lint
Expand Down
3 changes: 2 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ type ER struct {
Format string `yaml:"format,omitempty"`
Comment bool `yaml:"comment,omitempty"`
Distance *int `yaml:"distance,omitempty"`
Font string `yaml:"font,omitempty"`
}

// AdditionalRelation is the struct for table relation from yaml
Expand Down Expand Up @@ -186,7 +187,7 @@ func (c *Config) Load(configPath string, options ...Option) error {

// LoadOptions load options
func (c *Config) LoadOption(options ...Option) error {
for _, option := range options {
for _, option := range options {
if err := option(c); err != nil {
return err
}
Expand Down
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ require (
cloud.google.com/go/bigquery v1.6.0
cloud.google.com/go/spanner v1.5.1
github.com/aws/aws-sdk-go v1.30.9
github.com/beta/freetype v0.0.1
github.com/denisenkom/go-mssqldb v0.0.0-20200206145737-bbfc9a55622e
github.com/fatih/color v1.9.0 // indirect
github.com/go-sql-driver/mysql v1.5.0
github.com/gobuffalo/packr/v2 v2.8.0
github.com/gocarina/gocsv v0.0.0-20200330101823-46266ca37bd3
github.com/goccy/go-graphviz v0.0.5
github.com/goccy/go-graphviz v0.0.6
github.com/goccy/go-yaml v1.4.3
github.com/google/go-cmp v0.4.0
github.com/k1LoW/ffff v0.1.1
github.com/karrick/godirwalk v1.15.6 // indirect
github.com/labstack/gommon v0.3.0
github.com/lib/pq v1.3.0
Expand All @@ -26,6 +28,7 @@ require (
github.com/spf13/pflag v1.0.5
github.com/xo/dburl v0.0.0-20200124232849-e9ec94f52bc3
golang.org/x/crypto v0.0.0-20200414173820-0848c9571904 // indirect
golang.org/x/image v0.0.0-20200618115811-c13761719519
golang.org/x/tools v0.0.0-20200417140056-c07e33ef3290 // indirect
google.golang.org/api v0.21.0
google.golang.org/genproto v0.0.0-20200417142217-fb6d0575620b // indirect
Expand Down
44 changes: 13 additions & 31 deletions go.sum

Large diffs are not rendered by default.

73 changes: 73 additions & 0 deletions output/gviz/gviz.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,21 @@ package gviz
import (
"bytes"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"

"github.com/beta/freetype/truetype"
"github.com/goccy/go-graphviz"
"github.com/k1LoW/ffff"
"github.com/k1LoW/tbls/config"
"github.com/k1LoW/tbls/output/dot"
"github.com/k1LoW/tbls/schema"
"github.com/pkg/errors"
"golang.org/x/image/font"
"golang.org/x/image/font/opentype"
"golang.org/x/image/font/sfnt"
)

// Gviz struct
Expand Down Expand Up @@ -47,6 +56,13 @@ func (g *Gviz) OutputTable(wr io.Writer, t *schema.Table) error {

func (g *Gviz) render(wr io.Writer, b []byte) (e error) {
gviz := graphviz.New()
if g.config.ER.Font != "" {
faceFunc, err := getFaceFunc(g.config.ER.Font)
if err != nil {
return errors.WithStack(err)
}
gviz.SetFontFace(faceFunc)
}
graph, err := graphviz.ParseBytes(b)
if err != nil {
return errors.WithStack(err)
Expand All @@ -64,3 +80,60 @@ func (g *Gviz) render(wr io.Writer, b []byte) (e error) {
}
return nil
}

// getFaceFunc
func getFaceFunc(keyword string) (func(size float64) (font.Face, error), error) {
var (
faceFunc func(size float64) (font.Face, error)
path string
)

fi, err := os.Stat(keyword)
if err == nil && !fi.IsDir() {
path = keyword
} else {
path, err = ffff.FuzzyFindPath(keyword)
if err != nil {
return faceFunc, errors.WithStack(err)
}
}

fb, err := ioutil.ReadFile(filepath.Clean(path))
if err != nil {
return faceFunc, errors.WithStack(err)
}

if strings.HasSuffix(path, ".otf") {
// OpenType
ft, err := sfnt.Parse(fb)
if err != nil {
return faceFunc, errors.WithStack(err)
}
faceFunc = func(size float64) (font.Face, error) {
opt := &opentype.FaceOptions{
Size: size,
DPI: 0,
Hinting: 0,
}
return opentype.NewFace(ft, opt)
}
} else {
// TrueType
ft, err := truetype.Parse(fb)
if err != nil {
return faceFunc, errors.WithStack(err)
}
faceFunc = func(size float64) (font.Face, error) {
opt := &truetype.Options{
Size: size,
DPI: 0,
Hinting: 0,
GlyphCacheEntries: 0,
SubPixelsX: 0,
SubPixelsY: 0,
}
return truetype.NewFace(ft, opt), nil
}
}
return faceFunc, nil
}
Binary file modified sample/adjust/administrator.blogs.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/adjust/backup.blog_options.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/adjust/backup.blogs.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/adjust/public.CamelizeTable.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/adjust/public.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.
Binary file modified sample/adjust/public.comments.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/adjust/public.hyphen-table.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/adjust/public.logs.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/adjust/public.post_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.
Binary file modified sample/adjust/public.post_comments.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/adjust/public.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/adjust/public.user_options.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/adjust/public.users.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/adjust/schema.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/adjust/time.bar.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/adjust/time.hyphenated-table.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/adjust/time.referencing.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/dict/CamelizeTable.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/dict/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.
Binary file modified sample/dict/comments.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/dict/hyphen-table.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/dict/logs.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/dict/post_comments.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/dict/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/dict/schema.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/dict/user_options.png
Binary file modified sample/dict/users.png
Binary file modified sample/dynamodb/Forum.png
Binary file modified sample/dynamodb/ProductCatalog.png
Binary file modified sample/dynamodb/Reply.png
Binary file modified sample/dynamodb/Thread.png
Binary file modified sample/dynamodb/schema.png
Binary file modified sample/exclude/comment_stars.png
Binary file modified sample/exclude/comments.png
Binary file modified sample/exclude/hyphen-table.png
Binary file modified sample/exclude/post_comments.png
Binary file modified sample/exclude/posts.png
Binary file modified sample/exclude/schema.png
Binary file modified sample/exclude/user_options.png
Binary file modified sample/exclude/users.png
43 changes: 43 additions & 0 deletions sample/font/CamelizeTable.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# CamelizeTable

## Description

<details>
<summary><strong>Table Definition</strong></summary>

```sql
CREATE TABLE `CamelizeTable` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`created` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
```

</details>

## Columns

| Name | Type | Default | Nullable | Children | Parents | Comment |
| ---- | ---- | ------- | -------- | -------- | ------- | ------- |
| id | bigint(20) | | false | | | |
| created | datetime | | false | | | |

## Constraints

| Name | Type | Definition |
| ---- | ---- | ---------- |
| PRIMARY | PRIMARY KEY | PRIMARY KEY (id) |

## Indexes

| Name | Definition |
| ---- | ---------- |
| PRIMARY | PRIMARY KEY (id) USING BTREE |

## Relations

![er](CamelizeTable.png)

---

> Generated by [tbls](https://github.com/k1LoW/tbls)
Binary file added sample/font/CamelizeTable.png
27 changes: 27 additions & 0 deletions sample/font/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# testdb

## Labels

`サンプル` `tbls`

## Tables

| Name | Columns | Comment | Type |
| ---- | ------- | ------- | ---- |
| [CamelizeTable](CamelizeTable.md) | 2 | | BASE TABLE |
| [comment_stars](comment_stars.md) | 6 | | BASE TABLE |
| [comments](comments.md) | 6 | Comments<br>Multi-line<br>table<br>comment | BASE TABLE |
| [hyphen-table](hyphen-table.md) | 3 | | BASE TABLE |
| [logs](logs.md) | 7 | Auditログ | BASE TABLE |
| [post_comments](post_comments.md) | 7 | post and comments View table | VIEW |
| [posts](posts.md) | 7 | エントリ | BASE TABLE |
| [user_options](user_options.md) | 4 | User options table | BASE TABLE |
| [users](users.md) | 6 | Users table | BASE TABLE |

## Relations

![er](schema.png)

---

> Generated by [tbls](https://github.com/k1LoW/tbls)
62 changes: 62 additions & 0 deletions sample/font/comment_stars.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# comment_stars

## Description

<details>
<summary><strong>Table Definition</strong></summary>

```sql
CREATE TABLE `comment_stars` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`comment_post_id` bigint(20) NOT NULL,
`comment_user_id` int(11) NOT NULL,
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`updated` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY (`id`),
UNIQUE KEY `user_id` (`user_id`,`comment_post_id`,`comment_user_id`),
KEY `comment_stars_user_id_post_id_fk` (`comment_post_id`,`comment_user_id`),
KEY `comment_stars_user_id_fk` (`comment_user_id`),
CONSTRAINT `comment_stars_user_id_fk` FOREIGN KEY (`comment_user_id`) REFERENCES `users` (`id`),
CONSTRAINT `comment_stars_user_id_post_id_fk` FOREIGN KEY (`comment_post_id`, `comment_user_id`) REFERENCES `comments` (`post_id`, `user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
```

</details>

## Columns

| Name | Type | Default | Nullable | Children | Parents | Comment |
| ---- | ---- | ------- | -------- | -------- | ------- | ------- |
| id | bigint(20) | | false | | | |
| user_id | int(11) | | false | | | |
| comment_post_id | bigint(20) | | false | | [comments](comments.md) | |
| comment_user_id | int(11) | | false | | [users](users.md) [comments](comments.md) | |
| created | timestamp | CURRENT_TIMESTAMP | false | | | |
| updated | timestamp | 0000-00-00 00:00:00 | false | | | |

## Constraints

| Name | Type | Definition |
| ---- | ---- | ---------- |
| comment_stars_user_id_fk | FOREIGN KEY | FOREIGN KEY (comment_user_id) REFERENCES users (id) |
| comment_stars_user_id_post_id_fk | FOREIGN KEY | FOREIGN KEY (comment_post_id, comment_user_id) REFERENCES comments (post_id, user_id) |
| PRIMARY | PRIMARY KEY | PRIMARY KEY (id) |
| user_id | UNIQUE | UNIQUE KEY user_id (user_id, comment_post_id, comment_user_id) |

## Indexes

| Name | Definition |
| ---- | ---------- |
| comment_stars_user_id_fk | KEY comment_stars_user_id_fk (comment_user_id) USING BTREE |
| comment_stars_user_id_post_id_fk | KEY comment_stars_user_id_post_id_fk (comment_post_id, comment_user_id) USING BTREE |
| PRIMARY | PRIMARY KEY (id) USING BTREE |
| user_id | UNIQUE KEY user_id (user_id, comment_post_id, comment_user_id) USING BTREE |

## Relations

![er](comment_stars.png)

---

> Generated by [tbls](https://github.com/k1LoW/tbls)
Binary file added sample/font/comment_stars.png
67 changes: 67 additions & 0 deletions sample/font/comments.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# comments

## Description

Comments
Multi-line
table
comment

<details>
<summary><strong>Table Definition</strong></summary>

```sql
CREATE TABLE `comments` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`post_id` bigint(20) NOT NULL,
`user_id` int(11) NOT NULL,
`comment` text NOT NULL COMMENT 'Comment\nMulti-line\r\ncolumn\rcomment',
`created` datetime NOT NULL,
`updated` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `post_id` (`post_id`,`user_id`),
KEY `comments_user_id_fk` (`user_id`),
KEY `comments_post_id_user_id_idx` (`post_id`,`user_id`) USING HASH,
CONSTRAINT `comments_post_id_fk` FOREIGN KEY (`post_id`) REFERENCES `posts` (`id`),
CONSTRAINT `comments_user_id_fk` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='Comments\nMulti-line\r\ntable\rcomment'
```

</details>

## Columns

| Name | Type | Default | Nullable | Children | Parents | Comment |
| ---- | ---- | ------- | -------- | -------- | ------- | ------- |
| id | bigint(20) | | false | | | |
| post_id | bigint(20) | | false | [comment_stars](comment_stars.md) | [posts](posts.md) | |
| user_id | int(11) | | false | [comment_stars](comment_stars.md) | [users](users.md) | |
| comment | text | | false | | | Comment<br>Multi-line<br>column<br>comment |
| created | datetime | | false | | | |
| updated | datetime | | true | | | |

## Constraints

| Name | Type | Definition |
| ---- | ---- | ---------- |
| comments_post_id_fk | FOREIGN KEY | FOREIGN KEY (post_id) REFERENCES posts (id) |
| comments_user_id_fk | FOREIGN KEY | FOREIGN KEY (user_id) REFERENCES users (id) |
| post_id | UNIQUE | UNIQUE KEY post_id (post_id, user_id) |
| PRIMARY | PRIMARY KEY | PRIMARY KEY (id) |

## Indexes

| Name | Definition |
| ---- | ---------- |
| comments_post_id_user_id_idx | KEY comments_post_id_user_id_idx (post_id, user_id) USING BTREE |
| comments_user_id_fk | KEY comments_user_id_fk (user_id) USING BTREE |
| PRIMARY | PRIMARY KEY (id) USING BTREE |
| post_id | UNIQUE KEY post_id (post_id, user_id) USING BTREE |

## Relations

![er](comments.png)

---

> Generated by [tbls](https://github.com/k1LoW/tbls)
Binary file added sample/font/comments.png
Loading

0 comments on commit ecad363

Please sign in to comment.