Skip to content

Commit

Permalink
issue enilu#4 支持PostgreSQL数据库文档生成
Browse files Browse the repository at this point in the history
  • Loading branch information
enilu committed Jan 8, 2019
1 parent 1774262 commit 3fd18c2
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 12 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,19 @@
choose database:
1:mysql
2:oracle
3:PostgreSQL
Select the appropriate numbers choose database type
(Enter 'c' to cancel): 2
input service name:
XE
(Enter 'c' to cancel): 3
input database name:
gunslite
input host:
localhost
input port:
1521
5432
input username:
guns
enilu
input password:
guns
123456

```
- 输入完成后回车,即可生成数据库文档目录${dbname}-doc,目录中文档以markdown文件为载体:
Expand Down
2 changes: 1 addition & 1 deletion bin/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ do
CP=$CP:$jar
done

java -Xms256M -Xmx512M -cp $CP com.xinshucredit.icbc.Main
java -Xms256M -Xmx512M -cp $CP cn.enilu.tool.database.doc.generator.Main
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.5</version>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
Expand Down
12 changes: 9 additions & 3 deletions src/main/java/cn/enilu/tool/database/doc/generator/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import cn.enilu.tool.database.doc.generator.database.Generator;
import cn.enilu.tool.database.doc.generator.database.MySQL;
import cn.enilu.tool.database.doc.generator.database.Oracle;
import cn.enilu.tool.database.doc.generator.database.PostgreSQL;
import org.nutz.dao.impl.SimpleDataSource;

import java.util.Scanner;
Expand All @@ -16,14 +17,14 @@
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.print("choose database:\n1:mysql\n2:oracle\n" +
System.out.print("choose database:\n1:mysql\n2:oracle\n3:PostgreSQL\n" +
"Select the appropriate numbers choose database type\n" +
"(Enter 'c' to cancel): ");
String dbType = sc.nextLine();
if("c".equals(dbType)){
System.exit(-1);
}
if( !("1").equals(dbType) && !"2".equals(dbType)){
if( !("1").equals(dbType) && !"2".equals(dbType) && !"3".equals(dbType)){
System.out.println("wrong number,will exit");
System.exit(-1);
}
Expand All @@ -33,7 +34,7 @@ public static void main(String[] args) {
serviceName = sc.nextLine();
}
String dbName = null;
if("1".equals(dbType)){
if("1".equals(dbType) || "3".equals(dbType)){
System.out.println("input database name:");
dbName = sc.nextLine();
}
Expand All @@ -54,6 +55,8 @@ public static void main(String[] args) {
dataSource.setJdbcUrl("jdbc:mysql://" + ip + ":" + port + "/" + dbName);
}else if("2".equals(dbType)){
dataSource.setJdbcUrl("jdbc:oracle:thin:@"+ip+":"+port+":"+serviceName);
}else if("3".equals(dbType)){
dataSource.setJdbcUrl("jdbc:postgresql://"+ip+":"+port+"/"+dbName);
}
dataSource.setUsername(username);
dataSource.setPassword(passowrd);
Expand All @@ -64,6 +67,9 @@ public static void main(String[] args) {
break;
case "2":
generator = new Oracle(username,dataSource);
break;
case "3":
generator = new PostgreSQL(dbName,dataSource);
}

generator.generateDoc();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public TableVo getTableInfo(String table,String comment){
column.setName(record.getString("column_name"));
column.setType(record.getString("column_type"));
column.setKey(record.getString("column_key"));
column.setIsNullable(record.getString("is_nullable"));
column.setIsNullable(record.getString("is_nullable").equals("NO")?"否":"是");
column.setComment(record.getString("column_comment"));
columns.add(column);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public TableVo getTableInfo(String table,String tableComment){
ColumnVo column = new ColumnVo();
column.setName(record.getString("column_name"));
column.setType(record.getString("data_type"));
column.setIsNullable(record.getString("nullable"));
column.setIsNullable(record.getString("nullable").equals("Y")?"是":"否");

for(Record comment:columnComments){
if(comment.getString("column_name").equals(column.getName())){
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package cn.enilu.tool.database.doc.generator.database;

import cn.enilu.tool.database.doc.generator.bean.ColumnVo;
import cn.enilu.tool.database.doc.generator.bean.TableVo;
import org.nutz.dao.entity.Record;
import org.nutz.dao.impl.SimpleDataSource;

import java.util.ArrayList;
import java.util.List;

/**
* PostgreSQL
*
* @author zt
* @version 2019/1/8 0008
*/
public class PostgreSQL extends Generator {
private String sqlTables = "SELECT A .oid, A .relname AS NAME, b.description AS COMMENT " +
"FROM pg_class A LEFT OUTER JOIN pg_description b ON b.objsubid = 0 AND A .oid = b.objoid " +
"WHERE A .relnamespace = ( SELECT oid FROM pg_namespace WHERE nspname = 'public' ) AND A .relkind = 'r' ORDER BY A .relname";
private String sqlColumns = "SELECT a.attname AS field,t.typname AS type,a.attlen AS length,a.atttypmod AS lengthvar,a.attnotnull AS notnull,b.description AS comment" +
" FROM pg_class c,pg_attribute a LEFT OUTER JOIN pg_description b ON a.attrelid=b.objoid AND a.attnum = b.objsubid,pg_type t" +
" WHERE c.relname = '@tablename' and a.attnum > 0 and a.attrelid = c.oid and a.atttypid = t.oid" +
" ORDER BY a.attnum";

public PostgreSQL(String dbName, SimpleDataSource dataSource) {
super(dbName, dataSource);
}

@Override
public List<TableVo> getTableData() {
List<Record> list = getList(sqlTables);
List<TableVo> tables = new ArrayList<>();
for(int i=0;i<list.size();i++){
Record record = list.get(i);
String table = record.getString("name");
String comment =record.getString("comment");
TableVo tableVo = getTableInfo(table,comment);
tables.add(tableVo);
}
return tables;
}
public TableVo getTableInfo(String table,String tableComment){
TableVo tableVo = new TableVo();
tableVo.setTable(table);
tableVo.setComment(tableComment);
String sql = sqlColumns.replace("@tablename",table);

List<Record> columns = getList(sql);

List<ColumnVo> columnVoList = new ArrayList<>();
for(int i=0;i<columns.size();i++){
Record record = columns.get(i);
ColumnVo column = new ColumnVo();
column.setName(record.getString("field"));
column.setType(record.getString("type"));
column.setIsNullable(record.getString("notnull").equals("true")?"否":"是");
column.setComment(record.getString("comment"));
columnVoList.add(column);
}
tableVo.setColumns(columnVoList);
return tableVo;
}
}

0 comments on commit 3fd18c2

Please sign in to comment.