diff --git a/drivers/mysql/mysql.go b/drivers/mysql/mysql.go index e618026f..69b3db71 100644 --- a/drivers/mysql/mysql.go +++ b/drivers/mysql/mysql.go @@ -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(` @@ -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) } } diff --git a/drivers/postgres/postgres.go b/drivers/postgres/postgres.go index c719e09d..26143d54 100644 --- a/drivers/postgres/postgres.go +++ b/drivers/postgres/postgres.go @@ -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, ";")) } } diff --git a/sample/mysql/CamelizeTable.md b/sample/mysql/CamelizeTable.md index f59b41a3..b25baaa4 100644 --- a/sample/mysql/CamelizeTable.md +++ b/sample/mysql/CamelizeTable.md @@ -3,6 +3,19 @@ ## Description +
+Table Definition + +```sql +CREATE TABLE `CamelizeTable` ( + `id` bigint(20) NOT NULL AUTO_INCREMENT, + `created` datetime NOT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=latin1 +``` + +
+ ## Columns diff --git a/sample/mysql/comment_stars.md b/sample/mysql/comment_stars.md index eda5010e..4c20eca8 100644 --- a/sample/mysql/comment_stars.md +++ b/sample/mysql/comment_stars.md @@ -3,6 +3,28 @@ ## Description +
+Table Definition + +```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 +``` + +
+ ## Columns diff --git a/sample/mysql/comments.md b/sample/mysql/comments.md index 32195aba..1deb6b81 100644 --- a/sample/mysql/comments.md +++ b/sample/mysql/comments.md @@ -3,6 +3,28 @@ ## Description +
+Table Definition + +```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 +``` + +
+ ## Columns diff --git a/sample/mysql/logs.md b/sample/mysql/logs.md index c5dcdbf3..505bccc5 100644 --- a/sample/mysql/logs.md +++ b/sample/mysql/logs.md @@ -3,6 +3,24 @@ ## Description audit log table +
+Table Definition + +```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 +``` + +
+ ## Columns diff --git a/sample/mysql/post_comments.md b/sample/mysql/post_comments.md index cb4d4080..26d8b339 100644 --- a/sample/mysql/post_comments.md +++ b/sample/mysql/post_comments.md @@ -7,7 +7,7 @@ post and comments View table Table Definition ```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`))))) ``` diff --git a/sample/mysql/posts.md b/sample/mysql/posts.md index bbd2ab40..a63522cc 100644 --- a/sample/mysql/posts.md +++ b/sample/mysql/posts.md @@ -3,6 +3,27 @@ ## Description Posts table +
+Table Definition + +```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' +``` + +
+ ## Columns diff --git a/sample/mysql/users.md b/sample/mysql/users.md index b5c8a6f8..9a9ff5be 100644 --- a/sample/mysql/users.md +++ b/sample/mysql/users.md @@ -3,6 +3,25 @@ ## Description Users table +
+Table Definition + +```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. user@example.com', + `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' +``` + +
+ ## Columns diff --git a/sample/mysql8/CamelizeTable.md b/sample/mysql8/CamelizeTable.md index f59b41a3..062ec323 100644 --- a/sample/mysql8/CamelizeTable.md +++ b/sample/mysql8/CamelizeTable.md @@ -3,6 +3,19 @@ ## Description +
+Table Definition + +```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 +``` + +
+ ## Columns diff --git a/sample/mysql8/comment_stars.md b/sample/mysql8/comment_stars.md index 7b329795..707ddafd 100644 --- a/sample/mysql8/comment_stars.md +++ b/sample/mysql8/comment_stars.md @@ -3,6 +3,28 @@ ## Description +
+Table Definition + +```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 +``` + +
+ ## Columns diff --git a/sample/mysql8/comments.md b/sample/mysql8/comments.md index 32195aba..9871f43a 100644 --- a/sample/mysql8/comments.md +++ b/sample/mysql8/comments.md @@ -3,6 +3,28 @@ ## Description +
+Table Definition + +```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 +``` + +
+ ## Columns diff --git a/sample/mysql8/logs.md b/sample/mysql8/logs.md index c5dcdbf3..dfb85056 100644 --- a/sample/mysql8/logs.md +++ b/sample/mysql8/logs.md @@ -3,6 +3,24 @@ ## Description audit log table +
+Table Definition + +```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 +``` + +
+ ## Columns diff --git a/sample/mysql8/post_comments.md b/sample/mysql8/post_comments.md index 0a586da0..78d4f6fa 100644 --- a/sample/mysql8/post_comments.md +++ b/sample/mysql8/post_comments.md @@ -7,7 +7,7 @@ post and comments View table Table Definition ```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`)))) ``` diff --git a/sample/mysql8/posts.md b/sample/mysql8/posts.md index bbd2ab40..b0a833ef 100644 --- a/sample/mysql8/posts.md +++ b/sample/mysql8/posts.md @@ -3,6 +3,27 @@ ## Description Posts table +
+Table Definition + +```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' +``` + +
+ ## Columns diff --git a/sample/mysql8/users.md b/sample/mysql8/users.md index e68358ae..c18f0722 100644 --- a/sample/mysql8/users.md +++ b/sample/mysql8/users.md @@ -3,6 +3,25 @@ ## Description Users table +
+Table Definition + +```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. user@example.com', + `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' +``` + +
+ ## Columns diff --git a/sample/postgres/post_comments.md b/sample/postgres/post_comments.md index 4e9b339c..9f674014 100644 --- a/sample/postgres/post_comments.md +++ b/sample/postgres/post_comments.md @@ -7,6 +7,7 @@ post and comments View table Table Definition ```sql +CREATE VIEW post_comments AS ( SELECT c.id, p.title, u2.username AS post_user, @@ -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))) +) ```