diff --git a/screw-core/src/main/java/cn/smallbun/screw/core/mapping/Mapping.java b/screw-core/src/main/java/cn/smallbun/screw/core/mapping/Mapping.java index ee0f135..6f7877d 100644 --- a/screw-core/src/main/java/cn/smallbun/screw/core/mapping/Mapping.java +++ b/screw-core/src/main/java/cn/smallbun/screw/core/mapping/Mapping.java @@ -120,6 +120,48 @@ public class Mapping { return list; } + /** + * @param resultSet {@link ResultSet} 对象 + * @param clazz 领域类型 + * @param 领域泛型 + * @return 领域对象 + * @throws MappingException MappingException + */ + public static List convertListByColumnLabel(ResultSet resultSet, + Class clazz) throws MappingException { + //存放列名和结果 + List> values = new ArrayList<>(16); + //结果集合 + List list = new ArrayList<>(); + try { + //处理 ResultSet + ResultSetMetaData metaData = resultSet.getMetaData(); + int columnCount = metaData.getColumnCount(); + //迭代 + while (resultSet.next()) { + //map object + HashMap value = new HashMap<>(16); + //循环所有的列,获取列名,根据列名获取值 + for (int i = 1; i <= columnCount; i++) { + String columnName = metaData.getColumnLabel(i); + value.put(columnName, resultSet.getString(i)); + } + //add object + values.add(value); + } + //获取类数据 + List fieldMethods = getFieldMethods(clazz); + //循环集合,根据类型反射构建对象 + for (Map map : values) { + T rsp = getObject(clazz, fieldMethods, map); + list.add(rsp); + } + } catch (Exception e) { + throw new MappingException(e); + } + return list; + } + /** * 获取对象 * diff --git a/screw-core/src/main/java/cn/smallbun/screw/core/query/AbstractDatabaseQuery.java b/screw-core/src/main/java/cn/smallbun/screw/core/query/AbstractDatabaseQuery.java index 88e1b84..841c05e 100644 --- a/screw-core/src/main/java/cn/smallbun/screw/core/query/AbstractDatabaseQuery.java +++ b/screw-core/src/main/java/cn/smallbun/screw/core/query/AbstractDatabaseQuery.java @@ -72,6 +72,7 @@ public abstract class AbstractDatabaseQuery implements DatabaseQuery { try { //不为空 if (!Objects.isNull(connection) && !connection.isClosed()) { + System.out.println(connection); return connection; } //同步代码块 @@ -115,7 +116,7 @@ public abstract class AbstractDatabaseQuery implements DatabaseQuery { try { String schema; //获取数据库URL 用于判断数据库类型 - String url = this.getDataSource().getConnection().getMetaData().getURL(); + String url = this.getConnection().getMetaData().getURL(); //获取数据库名称 String name = JdbcUtils.getDbType(url).getName(); if (DatabaseType.CACHEDB.getName().equals(name)) { diff --git a/screw-core/src/main/java/cn/smallbun/screw/core/query/db2/Db2DataBaseQuery.java b/screw-core/src/main/java/cn/smallbun/screw/core/query/db2/Db2DataBaseQuery.java index aae9735..5e1af24 100644 --- a/screw-core/src/main/java/cn/smallbun/screw/core/query/db2/Db2DataBaseQuery.java +++ b/screw-core/src/main/java/cn/smallbun/screw/core/query/db2/Db2DataBaseQuery.java @@ -18,18 +18,36 @@ package cn.smallbun.screw.core.query.db2; import cn.smallbun.screw.core.exception.QueryException; +import cn.smallbun.screw.core.mapping.Mapping; import cn.smallbun.screw.core.metadata.Column; import cn.smallbun.screw.core.metadata.Database; import cn.smallbun.screw.core.metadata.PrimaryKey; import cn.smallbun.screw.core.metadata.Table; import cn.smallbun.screw.core.query.AbstractDatabaseQuery; +import cn.smallbun.screw.core.query.db2.model.Db2ColumnModel; +import cn.smallbun.screw.core.query.db2.model.Db2DatabaseModel; +import cn.smallbun.screw.core.query.db2.model.Db2PrimaryKeyModel; +import cn.smallbun.screw.core.query.db2.model.Db2TableModel; +import cn.smallbun.screw.core.query.mysql.model.MySqlTableModel; +import cn.smallbun.screw.core.query.oracle.model.OracleColumnModel; +import cn.smallbun.screw.core.query.oracle.model.OraclePrimaryKeyModel; +import cn.smallbun.screw.core.query.oracle.model.OracleTableModel; import cn.smallbun.screw.core.util.Assert; +import cn.smallbun.screw.core.util.CollectionUtils; import cn.smallbun.screw.core.util.ExceptionUtils; +import cn.smallbun.screw.core.util.JdbcUtils; +import org.apache.commons.lang3.StringUtils; import javax.sql.DataSource; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; import java.util.List; +import java.util.stream.Collectors; import static cn.smallbun.screw.core.constant.DefaultConstants.NOT_SUPPORTED; +import static cn.smallbun.screw.core.constant.DefaultConstants.PERCENT_SIGN; /** * db2 数据库查询 @@ -54,7 +72,9 @@ public class Db2DataBaseQuery extends AbstractDatabaseQuery { */ @Override public Database getDataBase() throws QueryException { - throw ExceptionUtils.mpe(NOT_SUPPORTED); + Db2DatabaseModel model = new Db2DatabaseModel(); + model.setDatabase(StringUtils.trim(this.getSchema())); + return model; } /** @@ -63,8 +83,19 @@ public class Db2DataBaseQuery extends AbstractDatabaseQuery { * @return {@link List} 所有表信息 */ @Override - public List getTables() { - throw ExceptionUtils.mpe(NOT_SUPPORTED); + public List getTables() { + ResultSet resultSet = null; + try { + //查询 + resultSet = getMetaData().getTables(getCatalog(), getSchema(), PERCENT_SIGN, + new String[] { "TABLE" }); + //映射 + return Mapping.convertList(resultSet, Db2TableModel.class); + } catch (SQLException e) { + throw ExceptionUtils.mpe(e); + } finally { + JdbcUtils.close(resultSet,connection); + } } /** @@ -75,9 +106,59 @@ public class Db2DataBaseQuery extends AbstractDatabaseQuery { * @throws QueryException QueryException */ @Override - public List getTableColumns(String table) throws QueryException { + public List getTableColumns(String table) throws QueryException { Assert.notEmpty(table, "Table name can not be empty!"); - throw ExceptionUtils.mpe(NOT_SUPPORTED); + ResultSet resultSet = null; + try { + //查询所有 + List list = null; + if(PERCENT_SIGN.equals(table)){ + List tableNames = getTables().stream().map(Table::getTableName) + .collect(Collectors.toList()).stream().distinct().collect(Collectors.toList()); + //db2 getMetaData().getColumns 中 无法获取表名,循环单张表获取 + list = new ArrayList<>(); + for (String tableName : tableNames) { + list.addAll(getTableColumns(tableName)); + } + }else { + if(!this.columnsCaching.containsKey(table)){ + //查询 + resultSet = getMetaData().getColumns(getCatalog(), getSchema(), table, PERCENT_SIGN); + //映射 + list = Mapping.convertList(resultSet, Db2ColumnModel.class); + //单表查询 + String sql = "SELECT COLNAME as NAME,TABNAME as TABLE_NAME,REMARKS,LENGTH as COLUMN_LENGTH, TYPENAME ||'('|| LENGTH ||')' as COLUMN_TYPE,SCALE as DECIMAL_DIGITS FROM SYSCAT.COLUMNS WHERE TABSCHEMA='"+StringUtils.trim(getSchema())+"' and TABNAME = '%s' ORDER BY COLNO"; + resultSet = prepareStatement(String.format(sql, table)).executeQuery(); + //db2 ColumnName 获取的不是 as column 值,使用ColumnLabel获取 + List inquires = Mapping.convertListByColumnLabel(resultSet, Db2ColumnModel.class); + for (Db2ColumnModel i : list) { + inquires.forEach(j -> { + //列名一致 + if (i.getColumnName().equals(j.getColumnName())) { + //放入备注 + i.setRemarks(j.getRemarks()); + i.setColumnLength(j.getColumnLength()); + i.setColumnType(j.getColumnType()); + i.setTableName(j.getTableName()); + i.setDecimalDigits(j.getDecimalDigits()); + if("NO".equals(i.getNullable())){ + i.setNullable("0"); + } + } + }); + } + this.columnsCaching.put(table,list.stream() + .filter(i -> StringUtils.isNotBlank(i.getColumnName())).collect(Collectors.toList())); + }else{ + list = this.columnsCaching.get(table).stream().map(c -> (Db2ColumnModel)c).collect(Collectors.toList()); + } + } + return list; + } catch (SQLException e) { + throw ExceptionUtils.mpe(e); + } finally { + JdbcUtils.close(resultSet, this.connection); + } } /** @@ -88,7 +169,7 @@ public class Db2DataBaseQuery extends AbstractDatabaseQuery { */ @Override public List getTableColumns() throws QueryException { - throw ExceptionUtils.mpe(NOT_SUPPORTED); + return getTableColumns(PERCENT_SIGN); } /** @@ -100,7 +181,39 @@ public class Db2DataBaseQuery extends AbstractDatabaseQuery { */ @Override public List getPrimaryKeys(String table) throws QueryException { - throw ExceptionUtils.mpe(NOT_SUPPORTED); + ResultSet resultSet = null; + try { + //查询 + resultSet = getMetaData().getPrimaryKeys(getCatalog(), getSchema(), table); + //映射 + return Mapping.convertList(resultSet, Db2PrimaryKeyModel.class); + } catch (SQLException e) { + throw ExceptionUtils.mpe(e); + } finally { + JdbcUtils.close(resultSet, this.connection); + } } + /** + * 根据表名获取主键 + * + * @return {@link List} + * @throws QueryException QueryException + */ + @Override + public List getPrimaryKeys() throws QueryException { + ResultSet resultSet = null; + try { + // 由于单条循环查询存在性能问题,所以这里通过自定义SQL查询数据库主键信息 + String sql = "SELECT TABSCHEMA as TABLE_SCHEM, TABNAME as TABLE_NAME ,COLNAME as COLUMN_NAME,KEYSEQ as KEY_SEQ FROM SYSCAT.COLUMNS WHERE TABSCHEMA = '%s' AND KEYSEQ IS NOT NULL ORDER BY KEYSEQ"; + resultSet = prepareStatement(String.format(sql, getDataBase().getDatabase())).executeQuery(); + return Mapping.convertListByColumnLabel(resultSet, Db2PrimaryKeyModel.class); + } catch (SQLException e) { + throw new QueryException(e); + } finally { + JdbcUtils.close(resultSet); + } + } + + } diff --git a/screw-core/src/main/java/cn/smallbun/screw/core/query/db2/model/Db2ColumnModel.java b/screw-core/src/main/java/cn/smallbun/screw/core/query/db2/model/Db2ColumnModel.java new file mode 100644 index 0000000..af7bc55 --- /dev/null +++ b/screw-core/src/main/java/cn/smallbun/screw/core/query/db2/model/Db2ColumnModel.java @@ -0,0 +1,175 @@ +/* + * screw-core - 简洁好用的数据库表结构文档生成工具 + * Copyright © 2020 SanLi (qinggang.zuo@gmail.com) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ +package cn.smallbun.screw.core.query.db2.model; + +import cn.smallbun.screw.core.mapping.MappingField; +import cn.smallbun.screw.core.metadata.Column; +import lombok.Data; + +/** + * db2 table column + * + * @author hexw + * Created by 964104818@qq.com on 2021/05/31 8:44 + */ +@Data +public class Db2ColumnModel implements Column { + /** + * 表名称,它是引用属性的作用域(如果 DATA_TYPE 不是 REF,则为 null) + */ + @MappingField(value = "SCOPE_TABLE") + private Object scopeTable; + /** + * 表类别 + */ + @MappingField(value = "TABLE_CAT") + private Object tableCat; + /** + * 未被使用 + */ + @MappingField(value = "BUFFER_LENGTH") + private String bufferLength; + /** + * ISO 规则用于确定列是否包括 null。 + */ + @MappingField(value = "IS_NULLABLE") + private String isNullable; + /** + * 数据源依赖的类型名称,对于 UDT,该类型名称是完全限定的 + */ + @MappingField(value = "TABLE_NAME") + private String tableName; + /** + * 该列的默认值,当值在单引号内时应被解释为一个字符串(可为 null) + */ + @MappingField(value = "COLUMN_DEF") + private String columnDef; + /** + * 表的类别,它是引用属性的作用域(如果 DATA_TYPE 不是 REF,则为 null) + */ + @MappingField(value = "SCOPE_CATALOG") + private Object scopeCatalog; + /** + * 表模式 + */ + @MappingField(value = "TABLE_SCHEM") + private String tableSchem; + /** + * 列名称 + */ + @MappingField(value = "NAME") + private String columnName; + /** + * 是否允许使用 NULL。 + */ + @MappingField(value = "NULLABLE") + private String nullable; + /** + * 描述列的注释(可为 null) + */ + @MappingField(value = "REMARKS") + private String remarks; + /** + * 小数部分的位数。对于 DECIMAL_DIGITS 不适用的数据类型,则返回 Null。 + */ + @MappingField(value = "DECIMAL_DIGITS") + private String decimalDigits; + /** + * 基数(通常为 10 或 2) + */ + @MappingField(value = "NUM_PREC_RADIX") + private String numPrecRadix; + /** + * + */ + @MappingField(value = "SQL_DATETIME_SUB") + private String sqlDatetimeSub; + /** + * + */ + @MappingField(value = "IS_GENERATEDCOLUMN") + private String isGeneratedColumn; + /** + * 指示此列是否自动增加 + * YES --- 如果该列自动增加 + * NO --- 如果该列不自动增加 + */ + @MappingField(value = "IS_AUTOINCREMENT") + private String isAutoIncrement; + /** + * SQL数据类型 + */ + @MappingField(value = "SQL_DATA_TYPE") + private String sqlDataType; + /** + * 对于 char 类型,该长度是列中的最大字节数 + */ + @MappingField(value = "CHAR_OCTET_LENGTH") + private String charOctetLength; + /** + * 表中的列的索引(从 1 开始) + */ + @MappingField(value = "ORDINAL_POSITION") + private String ordinalPosition; + /** + * 表的模式,它是引用属性的作用域(如果 DATA_TYPE 不是 REF,则为 null) + */ + @MappingField(value = "SCOPE_SCHEMA") + private String scopeSchema; + /** + * 不同类型或用户生成 Ref 类型、来自 java.sql.Types 的 SQL 类型的源类型(如果 DATA_TYPE 不是 DISTINCT 或用户生成的 REF,则为 null) + */ + @MappingField(value = "SOURCE_DATA_TYPE") + private String sourceDataType; + /** + * 来自 java.sql.Types 的 SQL 类型 + */ + @MappingField(value = "DATA_TYPE") + private String dataType; + /** + * 数据源依赖的类型名称,对于 UDT,该类型名称是完全限定的 + */ + @MappingField(value = "TYPE_NAME") + private String typeName; + /** + * 列表示给定列的指定列大小。 + * 对于数值数据,这是最大精度。 + * 对于字符数据,这是字符长度。 + * 对于日期时间数据类型,这是 String 表示形式的字符长度(假定允许的最大小数秒组件的精度)。 + * 对于二进制数据,这是字节长度。 + * 对于 ROWID 数据类型,这是字节长度。对于列大小不适用的数据类型,则返回 Null。 + */ + @MappingField(value = "COLUMN_SIZE") + private String columnSize; + + /** + * 是否主键 + */ + private String primaryKey; + /** + * 列类型(带长度) + */ + @MappingField(value = "COLUMN_TYPE") + private String columnType; + + /** + * 列长度 + */ + @MappingField(value = "COLUMN_LENGTH") + private String columnLength; +} diff --git a/screw-core/src/main/java/cn/smallbun/screw/core/query/db2/model/Db2DatabaseModel.java b/screw-core/src/main/java/cn/smallbun/screw/core/query/db2/model/Db2DatabaseModel.java new file mode 100644 index 0000000..337648b --- /dev/null +++ b/screw-core/src/main/java/cn/smallbun/screw/core/query/db2/model/Db2DatabaseModel.java @@ -0,0 +1,39 @@ +/* + * screw-core - 简洁好用的数据库表结构文档生成工具 + * Copyright © 2020 SanLi (qinggang.zuo@gmail.com) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ +package cn.smallbun.screw.core.query.db2.model; + +import cn.smallbun.screw.core.mapping.MappingField; +import cn.smallbun.screw.core.metadata.Database; +import lombok.Data; + +/** + * db2数据库信息 + * + * @author hexw + * Created by 964104818@qq.com on 2021/05/31 8:44 + */ +@Data +public class Db2DatabaseModel implements Database { + + private static final long serialVersionUID = 931210775266917894L; + /** + * 数据库名称 + */ + @MappingField(value = "TABLE_CAT") + private String database; +} diff --git a/screw-core/src/main/java/cn/smallbun/screw/core/query/db2/model/Db2PrimaryKeyModel.java b/screw-core/src/main/java/cn/smallbun/screw/core/query/db2/model/Db2PrimaryKeyModel.java new file mode 100644 index 0000000..f5b5c46 --- /dev/null +++ b/screw-core/src/main/java/cn/smallbun/screw/core/query/db2/model/Db2PrimaryKeyModel.java @@ -0,0 +1,58 @@ +/* + * screw-core - 简洁好用的数据库表结构文档生成工具 + * Copyright © 2020 SanLi (qinggang.zuo@gmail.com) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ +package cn.smallbun.screw.core.query.db2.model; + +import cn.smallbun.screw.core.mapping.MappingField; +import cn.smallbun.screw.core.metadata.PrimaryKey; +import lombok.Data; + +/** + * db2 table primary + * + * @author hexw + * Created by 964104818@qq.com on 2021/05/31 8:44 + */ +@Data +public class Db2PrimaryKeyModel implements PrimaryKey { + + /** + * 表名 + */ + @MappingField(value = "TABLE_NAME") + private String tableName; + /** + * pk name + */ + @MappingField(value = "PK_NAME") + private String pkName; + /** + * 表模式 + */ + @MappingField(value = "TABLE_SCHEM") + private String tableSchem; + /** + * 列名 + */ + @MappingField(value = "COLUMN_NAME") + private String columnName; + /** + * 键序列 + */ + @MappingField(value = "KEY_SEQ") + private String keySeq; +} diff --git a/screw-core/src/main/java/cn/smallbun/screw/core/query/db2/model/Db2TableModel.java b/screw-core/src/main/java/cn/smallbun/screw/core/query/db2/model/Db2TableModel.java new file mode 100644 index 0000000..3aa4ff4 --- /dev/null +++ b/screw-core/src/main/java/cn/smallbun/screw/core/query/db2/model/Db2TableModel.java @@ -0,0 +1,57 @@ +/* + * screw-core - 简洁好用的数据库表结构文档生成工具 + * Copyright © 2020 SanLi (qinggang.zuo@gmail.com) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ +package cn.smallbun.screw.core.query.db2.model; + +import cn.smallbun.screw.core.mapping.MappingField; +import cn.smallbun.screw.core.metadata.Table; +import lombok.Data; + +/** + * db2 table + * + * @author hexw + * Created by 964104818@qq.com on 2021/05/31 8:44 + */ +@Data +public class Db2TableModel implements Table { + /** + * TABLE_CAT + */ + @MappingField(value = "TABLE_CAT") + private String tableCat; + /** + * 表名 + */ + @MappingField(value = "NAME") + private String tableName; + /** + * 表模式 + */ + @MappingField(value = "TABLE_SCHEM") + private String tableSchem; + /** + * 表类型 + */ + @MappingField(value = "TABLE_TYPE") + private String tableType; + /** + * 备注 + */ + @MappingField(value = "REMARKS") + private String remarks; +} diff --git a/screw-core/src/main/resources/properties/db2.properties b/screw-core/src/main/resources/properties/db2.properties new file mode 100644 index 0000000..6fd0ce2 --- /dev/null +++ b/screw-core/src/main/resources/properties/db2.properties @@ -0,0 +1,26 @@ +# +# screw-core - \u7B80\u6D01\u597D\u7528\u7684\u6570\u636E\u5E93\u8868\u7ED3\u6784\u6587\u6863\u751F\u6210\u5DE5\u5177 +# Copyright \u00A9 2020 SanLi (qinggang.zuo@gmail.com) +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Lesser General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public License +# along with this program. If not, see . +# + +#URL +url=jdbc:db2://xxx.xxx.xxx.xxx:50000/xxx:currentSchema=xxx; +#\u9A71\u52A8 +driver=com.ibm.db2.jcc.DB2Driver +#\u7528\u6237\u540D +username=xxx +#\u5BC6\u7801 +password=xxx \ No newline at end of file diff --git a/screw-core/src/test/java/cn/smallbun/screw/core/produce/Db2DocumentationBuilderTest.java b/screw-core/src/test/java/cn/smallbun/screw/core/produce/Db2DocumentationBuilderTest.java new file mode 100644 index 0000000..58bb83e --- /dev/null +++ b/screw-core/src/test/java/cn/smallbun/screw/core/produce/Db2DocumentationBuilderTest.java @@ -0,0 +1,86 @@ +/* + * screw-core - 简洁好用的数据库表结构文档生成工具 + * Copyright © 2020 SanLi (qinggang.zuo@gmail.com) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ +package cn.smallbun.screw.core.produce; + +import cn.smallbun.screw.core.Configuration; +import cn.smallbun.screw.core.engine.EngineConfig; +import cn.smallbun.screw.core.engine.EngineFileType; +import cn.smallbun.screw.core.engine.EngineTemplateType; +import com.zaxxer.hikari.HikariConfig; +import com.zaxxer.hikari.HikariDataSource; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.util.Properties; + +import static cn.smallbun.screw.core.common.Constants.fileOutputDir; + +/** + * SqlServer 文档生成测试 + * + * @author SanLi + * Created by qinggang.zuo@gmail.com / 2689170096@qq.com on 2020/3/30 18:33 + */ +public class Db2DocumentationBuilderTest extends AbstractDocumentationExecute { + + /** + * 构建 + */ + @Test + void build() throws IOException { + //数据源 + HikariConfig hikariConfig = new HikariConfig(); + hikariConfig.setDriverClassName(getDriver()); + hikariConfig.setJdbcUrl(getUrl()); + hikariConfig.setUsername(getUserName()); + hikariConfig.setPassword(getPassword()); + hikariConfig.setMinimumIdle(2); + hikariConfig.setMaximumPoolSize(5); + HikariDataSource dataSource = new HikariDataSource(hikariConfig); + //生成配置 + EngineConfig engineConfig = EngineConfig.builder() + //生成文件路径 + .fileOutputDir(fileOutputDir) + //文件类型 + .fileType(EngineFileType.MD) + //打开文件夹 + .openOutputDir(true) + //生成模板实现 + .produceType(EngineTemplateType.velocity).build(); + //配置 + Configuration config = Configuration.builder() + //版本 + .version("1.0.0") + //描述 + .description("数据库设计文档生成") + //数据源 + .dataSource(dataSource) + //生成配置 + .engineConfig(engineConfig).build(); + execute(config); + } + + /** + * 获取配置文件 + * @return {@link Properties} + */ + @Override + public String getConfigProperties() { + return System.getProperty("user.dir") + "/src/main/resources/properties/db2.properties"; + } +} diff --git a/screw-core/src/test/java/cn/smallbun/screw/core/querys/Db2DataBaseQueryTest.java b/screw-core/src/test/java/cn/smallbun/screw/core/querys/Db2DataBaseQueryTest.java new file mode 100644 index 0000000..de78666 --- /dev/null +++ b/screw-core/src/test/java/cn/smallbun/screw/core/querys/Db2DataBaseQueryTest.java @@ -0,0 +1,119 @@ +/* + * screw-core - 简洁好用的数据库表结构文档生成工具 + * Copyright © 2020 SanLi (qinggang.zuo@gmail.com) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ +package cn.smallbun.screw.core.querys; + +import cn.smallbun.screw.core.common.Properties; +import cn.smallbun.screw.core.exception.QueryException; +import cn.smallbun.screw.core.metadata.Column; +import cn.smallbun.screw.core.metadata.Database; +import cn.smallbun.screw.core.metadata.PrimaryKey; +import cn.smallbun.screw.core.metadata.Table; +import cn.smallbun.screw.core.query.DatabaseQuery; +import cn.smallbun.screw.core.query.DatabaseQueryFactory; +import com.alibaba.fastjson.JSON; +import com.zaxxer.hikari.HikariConfig; +import com.zaxxer.hikari.HikariDataSource; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.sql.DataSource; +import java.io.IOException; +import java.util.List; +import java.util.UUID; + +/** + * db2 database test + * + * @author SanLi + * Created by qinggang.zuo@gmail.com / 2689170096@qq.com on 2020/3/25 16:59 + */ +public class Db2DataBaseQueryTest implements Properties { + /** + * 日志 + */ + private final Logger logger = LoggerFactory.getLogger(this.getClass()); + /** + * DataBaseQuery + */ + private DatabaseQuery query; + + /** + * before + */ + @BeforeEach + void before() throws IOException { + HikariConfig config = new HikariConfig(); + config.setDriverClassName(getDriver()); + config.setJdbcUrl(getUrl()); + config.setUsername(getUserName()); + config.setPassword(getPassword()); + //config.addDataSourceProperty("remarks", "false"); + + config.setPoolName(UUID.randomUUID().toString().replace("-", "")); + DataSource dataSource = new HikariDataSource(config); + query = new DatabaseQueryFactory(dataSource).newInstance(); + } + + /** + * 查询数据库 + */ + @Test + void databases() throws QueryException { + Database dataBasesInfo = query.getDataBase(); + logger.info(JSON.toJSONString(dataBasesInfo, true)); + } + + /** + * 查询表 + */ + @Test + void tables() throws QueryException { + List tables = query.getTables(); + logger.info(JSON.toJSONString(tables, true)); + } + + /** + * 查询列 + */ + @Test + void column() throws QueryException { + List columns = query.getTableColumns(); + logger.info("{}", columns.size()); + logger.info(JSON.toJSONString(columns, true)); + } + + /** + * 查询主键 + */ + @Test + void primaryKeys() { + List primaryKeys = query.getPrimaryKeys(); + logger.info(JSON.toJSONString(primaryKeys, true)); + } + + /** + * 获取配置文件 + * @return {@link java.util.Properties} + */ + @Override + public String getConfigProperties() { + return System.getProperty("user.dir") + "/src/main/resources/properties/db2.properties"; + } +}