Skip to content

Commit

Permalink
bonsai
Browse files Browse the repository at this point in the history
  • Loading branch information
k1LoW committed Jun 5, 2018
1 parent decaefb commit 3fc4e63
Show file tree
Hide file tree
Showing 17 changed files with 257 additions and 5 deletions.
22 changes: 21 additions & 1 deletion drivers/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,26 @@ SELECT table_name, table_type, table_comment FROM information_schema.tables WHER
Comment: tableComment,
}

// table definition
if tableType == "BASE TABLE" {
tableDefRows, err := db.Query(fmt.Sprintf("SHOW CREATE TABLE %s", tableName))
defer tableDefRows.Close()
if err != nil {
return err
}
for tableDefRows.Next() {
var (
tableName string
tableDef string
)
err := tableDefRows.Scan(&tableName, &tableDef)
if err != nil {
return err
}
table.Def = tableDef
}
}

// view definition
if tableType == "VIEW" {
viewDefRows, err := db.Query(`
Expand All @@ -59,7 +79,7 @@ AND table_name = ?;
if err != nil {
return err
}
table.Def = tableDef
table.Def = fmt.Sprintf("CREATE VIEW %s AS (%s)", tableName, tableDef)
}
}

Expand Down
2 changes: 1 addition & 1 deletion drivers/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ AND table_name = $2;
if err != nil {
return err
}
table.Def = tableDef
table.Def = fmt.Sprintf("CREATE VIEW %s AS (\n%s\n)", tableName, strings.TrimRight(tableDef, ";"))
}
}

Expand Down
13 changes: 13 additions & 0 deletions sample/mysql/CamelizeTable.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@
## 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

Expand Down
22 changes: 22 additions & 0 deletions sample/mysql/comment_stars.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,28 @@
## 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

Expand Down
22 changes: 22 additions & 0 deletions sample/mysql/comments.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,28 @@
## Description


<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,
`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
```

</details>


## Columns

Expand Down
18 changes: 18 additions & 0 deletions sample/mysql/logs.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,24 @@
## Description

audit log table
<details>
<summary><strong>Table Definition</strong></summary>

```sql
CREATE TABLE `logs` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`post_id` bigint(20) DEFAULT NULL,
`comment_id` bigint(20) DEFAULT NULL,
`comment_star_id` bigint(20) DEFAULT NULL,
`payload` text,
`created` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
```

</details>


## Columns

Expand Down
2 changes: 1 addition & 1 deletion sample/mysql/post_comments.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ post and comments View table
<summary><strong>Table Definition</strong></summary>

```sql
(select `c`.`id` AS `id`,`p`.`title` AS `title`,`u2`.`username` AS `post_user`,`c`.`comment` AS `comment`,`u2`.`username` AS `comment_user`,`c`.`created` AS `created`,`c`.`updated` AS `updated` from (((`testdb`.`posts` `p` left join `testdb`.`comments` `c` on((`p`.`id` = `c`.`post_id`))) left join `testdb`.`users` `u` on((`u`.`id` = `p`.`user_id`))) left join `testdb`.`users` `u2` on((`u2`.`id` = `c`.`user_id`))))
CREATE VIEW post_comments AS ((select `c`.`id` AS `id`,`p`.`title` AS `title`,`u2`.`username` AS `post_user`,`c`.`comment` AS `comment`,`u2`.`username` AS `comment_user`,`c`.`created` AS `created`,`c`.`updated` AS `updated` from (((`testdb`.`posts` `p` left join `testdb`.`comments` `c` on((`p`.`id` = `c`.`post_id`))) left join `testdb`.`users` `u` on((`u`.`id` = `p`.`user_id`))) left join `testdb`.`users` `u2` on((`u2`.`id` = `c`.`user_id`)))))
```

</details>
Expand Down
21 changes: 21 additions & 0 deletions sample/mysql/posts.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,27 @@
## Description

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

```sql
CREATE TABLE `posts` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`title` varchar(255) NOT NULL,
`body` text NOT NULL,
`post_type` enum('public','private','draft') NOT NULL COMMENT 'public/private/draft',
`created` datetime NOT NULL,
`updated` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `user_id` (`user_id`,`title`),
KEY `posts_user_id_idx` (`id`) USING BTREE,
CONSTRAINT `posts_user_id_fk` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='Posts table'
```

</details>


## Columns

Expand Down
19 changes: 19 additions & 0 deletions sample/mysql/users.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,25 @@
## Description

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

```sql
CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(50) NOT NULL,
`password` varchar(50) NOT NULL,
`email` varchar(355) NOT NULL COMMENT 'ex. [email protected]',
`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 `username` (`username`),
UNIQUE KEY `email` (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='Users table'
```

</details>


## Columns

Expand Down
13 changes: 13 additions & 0 deletions sample/mysql8/CamelizeTable.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@
## 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=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
```

</details>


## Columns

Expand Down
22 changes: 22 additions & 0 deletions sample/mysql8/comment_stars.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,28 @@
## 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,
`updated` timestamp NULL DEFAULT NULL,
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=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
```

</details>


## Columns

Expand Down
22 changes: 22 additions & 0 deletions sample/mysql8/comments.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,28 @@
## Description


<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,
`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`),
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=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
```

</details>


## Columns

Expand Down
18 changes: 18 additions & 0 deletions sample/mysql8/logs.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,24 @@
## Description

audit log table
<details>
<summary><strong>Table Definition</strong></summary>

```sql
CREATE TABLE `logs` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`post_id` bigint(20) DEFAULT NULL,
`comment_id` bigint(20) DEFAULT NULL,
`comment_star_id` bigint(20) DEFAULT NULL,
`payload` text,
`created` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
```

</details>


## Columns

Expand Down
2 changes: 1 addition & 1 deletion sample/mysql8/post_comments.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ post and comments View table
<summary><strong>Table Definition</strong></summary>

```sql
select `c`.`id` AS `id`,`p`.`title` AS `title`,`u2`.`username` AS `post_user`,`c`.`comment` AS `comment`,`u2`.`username` AS `comment_user`,`c`.`created` AS `created`,`c`.`updated` AS `updated` from (((`testdb`.`posts` `p` left join `testdb`.`comments` `c` on((`p`.`id` = `c`.`post_id`))) left join `testdb`.`users` `u` on((`u`.`id` = `p`.`user_id`))) left join `testdb`.`users` `u2` on((`u2`.`id` = `c`.`user_id`)))
CREATE VIEW post_comments AS (select `c`.`id` AS `id`,`p`.`title` AS `title`,`u2`.`username` AS `post_user`,`c`.`comment` AS `comment`,`u2`.`username` AS `comment_user`,`c`.`created` AS `created`,`c`.`updated` AS `updated` from (((`testdb`.`posts` `p` left join `testdb`.`comments` `c` on((`p`.`id` = `c`.`post_id`))) left join `testdb`.`users` `u` on((`u`.`id` = `p`.`user_id`))) left join `testdb`.`users` `u2` on((`u2`.`id` = `c`.`user_id`))))
```

</details>
Expand Down
21 changes: 21 additions & 0 deletions sample/mysql8/posts.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,27 @@
## Description

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

```sql
CREATE TABLE `posts` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`title` varchar(255) NOT NULL,
`body` text NOT NULL,
`post_type` enum('public','private','draft') NOT NULL COMMENT 'public/private/draft',
`created` datetime NOT NULL,
`updated` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `user_id` (`user_id`,`title`),
KEY `posts_user_id_idx` (`id`) USING BTREE,
CONSTRAINT `posts_user_id_fk` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='Posts table'
```

</details>


## Columns

Expand Down
19 changes: 19 additions & 0 deletions sample/mysql8/users.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,25 @@
## Description

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

```sql
CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(50) NOT NULL,
`password` varchar(50) NOT NULL,
`email` varchar(355) NOT NULL COMMENT 'ex. [email protected]',
`created` timestamp NOT NULL,
`updated` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`),
UNIQUE KEY `email` (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='Users table'
```

</details>


## Columns

Expand Down
4 changes: 3 additions & 1 deletion sample/postgres/post_comments.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ post and comments View table
<summary><strong>Table Definition</strong></summary>

```sql
CREATE VIEW post_comments AS (
SELECT c.id,
p.title,
u2.username AS post_user,
Expand All @@ -17,7 +18,8 @@ post and comments View table
FROM (((posts p
LEFT JOIN comments c ON ((p.id = c.post_id)))
LEFT JOIN users u ON ((u.id = p.user_id)))
LEFT JOIN users u2 ON ((u2.id = c.user_id)));
LEFT JOIN users u2 ON ((u2.id = c.user_id)))
)
```

</details>
Expand Down

0 comments on commit 3fc4e63

Please sign in to comment.