Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support view table #13

Merged
merged 2 commits into from
May 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions drivers/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ func (p *Postgres) Analyze(db *sql.DB, s *schema.Schema) error {

// tables
tableRows, err := db.Query(`
SELECT relname, table_type FROM pg_stat_user_tables AS p
LEFT JOIN information_schema.tables AS i ON p.relname = i.table_name
WHERE p.schemaname = i.table_schema
ORDER BY relid
`)
SELECT CONCAT(table_schema, '.', table_name)::regclass::oid AS oid, table_name, table_type
FROM information_schema.tables
WHERE table_schema != 'pg_catalog' AND table_schema != 'information_schema'
AND table_catalog = $1
ORDER BY oid`, s.Name)
defer tableRows.Close()
if err != nil {
return err
Expand All @@ -33,10 +33,11 @@ ORDER BY relid
tables := []*schema.Table{}
for tableRows.Next() {
var (
tableOid string
tableName string
tableType string
)
err := tableRows.Scan(&tableName, &tableType)
err := tableRows.Scan(&tableOid, &tableName, &tableType)
if err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions sample/mysql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
| [comment_stars](comment_stars.md) | 6 | | BASE TABLE |
| [comments](comments.md) | 6 | | BASE TABLE |
| [logs](logs.md) | 7 | | BASE TABLE |
| [post_comments](post_comments.md) | 7 | VIEW | VIEW |
| [posts](posts.md) | 7 | Posts table | BASE TABLE |
| [users](users.md) | 6 | Users table | BASE TABLE |

Expand Down
31 changes: 31 additions & 0 deletions sample/mysql/post_comments.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# post_comments

## Description

VIEW

## Columns

| Name | Type | Default | Nullable | Children | Parents | Comment |
| ---- | ---- | ------- | -------- | -------- | ------- | ------- |
| id | bigint(20) | 0 | true | | | |
| title | varchar(255) | | false | | | |
| post_user | varchar(50) | | true | | | |
| comment | text | | true | | | |
| comment_user | varchar(50) | | true | | | |
| created | datetime | | true | | | |
| updated | datetime | | true | | | |

## Constraints

| Name | Type | Def |
| ---- | ---- | --- |

## Indexes

| Name | Def |
| ---- | --- |

---

> Generated by [tbls](https://github.com/k1LoW/tbls)
1 change: 1 addition & 0 deletions sample/mysql8/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
| [comment_stars](comment_stars.md) | 6 | | BASE TABLE |
| [comments](comments.md) | 6 | | BASE TABLE |
| [logs](logs.md) | 7 | | BASE TABLE |
| [post_comments](post_comments.md) | 7 | VIEW | VIEW |
| [posts](posts.md) | 7 | Posts table | BASE TABLE |
| [users](users.md) | 6 | Users table | BASE TABLE |

Expand Down
31 changes: 31 additions & 0 deletions sample/mysql8/post_comments.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# post_comments

## Description

VIEW

## Columns

| Name | Type | Default | Nullable | Children | Parents | Comment |
| ---- | ---- | ------- | -------- | -------- | ------- | ------- |
| id | bigint(20) | 0 | true | | | |
| title | varchar(255) | | false | | | |
| post_user | varchar(50) | | true | | | |
| comment | text | | true | | | |
| comment_user | varchar(50) | | true | | | |
| created | datetime | | true | | | |
| updated | datetime | | true | | | |

## Constraints

| Name | Type | Def |
| ---- | ---- | --- |

## Indexes

| Name | Def |
| ---- | --- |

---

> Generated by [tbls](https://github.com/k1LoW/tbls)
1 change: 1 addition & 0 deletions sample/postgres/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
| [comments](comments.md) | 6 | | BASE TABLE |
| [comment_stars](comment_stars.md) | 6 | | BASE TABLE |
| [logs](logs.md) | 7 | | BASE TABLE |
| [post_comments](post_comments.md) | 7 | | VIEW |

---

Expand Down
31 changes: 31 additions & 0 deletions sample/postgres/post_comments.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# post_comments

## Description



## Columns

| Name | Type | Default | Nullable | Children | Parents | Comment |
| ---- | ---- | ------- | -------- | -------- | ------- | ------- |
| id | bigint | | true | | | |
| title | varchar(255) | | true | | | |
| post_user | varchar(50) | | true | | | |
| comment | text | | true | | | |
| comment_user | varchar(50) | | true | | | |
| created | timestamp without time zone | | true | | | |
| updated | timestamp without time zone | | true | | | |

## Constraints

| Name | Type | Def |
| ---- | ---- | --- |

## Indexes

| Name | Def |
| ---- | --- |

---

> Generated by [tbls](https://github.com/k1LoW/tbls)
9 changes: 9 additions & 0 deletions test/my.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
DROP VIEW IF EXISTS post_comments;
DROP TABLE IF EXISTS logs;
DROP TABLE IF EXISTS comment_stars;
DROP TABLE IF EXISTS comments;
Expand Down Expand Up @@ -63,3 +64,11 @@ CREATE TABLE logs (
payload text,
created datetime NOT NULL
);

CREATE VIEW post_comments AS (
SELECT c.id, p.title, u2.username AS post_user, c.comment, u2.username AS comment_user, c.created, c.updated
FROM posts AS p
LEFT JOIN comments AS c on p.id = c.post_id
LEFT JOIN users AS u on u.id = p.user_id
LEFT JOIN users AS u2 on u2.id = c.user_id
);
9 changes: 9 additions & 0 deletions test/pg.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
DROP VIEW IF EXISTS post_comments;
DROP TABLE IF EXISTS logs;
DROP TABLE IF EXISTS comment_stars;
DROP TABLE IF EXISTS comments;
Expand Down Expand Up @@ -76,3 +77,11 @@ CREATE TABLE logs (
payload text,
created timestamp NOT NULL
);

CREATE VIEW post_comments AS (
SELECT c.id, p.title, u2.username AS post_user, c.comment, u2.username AS comment_user, c.created, c.updated
FROM posts AS p
LEFT JOIN comments AS c on p.id = c.post_id
LEFT JOIN users AS u on u.id = p.user_id
LEFT JOIN users AS u2 on u2.id = c.user_id
);