diff --git a/screw-core/pom.xml b/screw-core/pom.xml index 5bc4f86..8ce8199 100644 --- a/screw-core/pom.xml +++ b/screw-core/pom.xml @@ -127,6 +127,29 @@ fastjson + + + phoenix + + + org.apache.phoenix + phoenix-core + 5.0.0-HBase-2.0 + + + + mysql + mysql-connector-java + + + com.google.protobuf + protobuf-java + + + + + + diff --git a/screw-core/src/main/java/cn/smallbun/screw/core/constant/DefaultConstants.java b/screw-core/src/main/java/cn/smallbun/screw/core/constant/DefaultConstants.java index ae774b4..ea182e6 100644 --- a/screw-core/src/main/java/cn/smallbun/screw/core/constant/DefaultConstants.java +++ b/screw-core/src/main/java/cn/smallbun/screw/core/constant/DefaultConstants.java @@ -92,4 +92,16 @@ public class DefaultConstants implements Serializable { * Y */ public static final String Y = "Y"; + + /** + * phoenix 命名空间 + */ + public static final String PHOENIX_NAMESPACE_MAPPING = "phoenix.schema.isNamespaceMappingEnabled"; + + /** + * phoenix 系统命名空间 + */ + public static final String PHOENIX_SYS_NAMESPACE_MAPPING = "phoenix.schema.mapSystemTablesToNamespace"; + + } diff --git a/screw-core/src/main/java/cn/smallbun/screw/core/query/DatabaseType.java b/screw-core/src/main/java/cn/smallbun/screw/core/query/DatabaseType.java index 8a15062..fe37e9b 100644 --- a/screw-core/src/main/java/cn/smallbun/screw/core/query/DatabaseType.java +++ b/screw-core/src/main/java/cn/smallbun/screw/core/query/DatabaseType.java @@ -26,6 +26,7 @@ import cn.smallbun.screw.core.query.hsql.HsqlDataBaseQuery; import cn.smallbun.screw.core.query.mariadb.MariaDbDataBaseQuery; import cn.smallbun.screw.core.query.mysql.MySqlDataBaseQuery; import cn.smallbun.screw.core.query.oracle.OracleDataBaseQuery; +import cn.smallbun.screw.core.query.phoenix.PhoenixDataBaseQuery; import cn.smallbun.screw.core.query.postgresql.PostgreSqlDataBaseQuery; import cn.smallbun.screw.core.query.sqlite.SqliteDataBaseQuery; import cn.smallbun.screw.core.query.sqlservice.SqlServerDataBaseQuery; @@ -124,7 +125,7 @@ public enum DatabaseType implements Serializable { * Phoenix */ PHOENIX("phoenix", "Phoenix HBase数据库", - OtherDataBaseQuery.class), + PhoenixDataBaseQuery.class), /** * CacheDB diff --git a/screw-core/src/main/java/cn/smallbun/screw/core/query/phoenix/.gitkeep b/screw-core/src/main/java/cn/smallbun/screw/core/query/phoenix/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/screw-core/src/main/java/cn/smallbun/screw/core/query/phoenix/PhoenixDataBaseQuery.java b/screw-core/src/main/java/cn/smallbun/screw/core/query/phoenix/PhoenixDataBaseQuery.java new file mode 100644 index 0000000..625432a --- /dev/null +++ b/screw-core/src/main/java/cn/smallbun/screw/core/query/phoenix/PhoenixDataBaseQuery.java @@ -0,0 +1,119 @@ +package cn.smallbun.screw.core.query.phoenix; + +import cn.smallbun.screw.core.exception.QueryException; +import cn.smallbun.screw.core.mapping.Mapping; +import cn.smallbun.screw.core.metadata.Database; +import cn.smallbun.screw.core.metadata.PrimaryKey; +import cn.smallbun.screw.core.query.AbstractDatabaseQuery; +import cn.smallbun.screw.core.query.phoenix.model.PhoenixColumnModel; +import cn.smallbun.screw.core.query.phoenix.model.PhoenixPrimaryKeyModel; +import cn.smallbun.screw.core.query.phoenix.model.PhoenixSqlDatabaseModel; +import cn.smallbun.screw.core.query.phoenix.model.PhoenixlTableModel; +import cn.smallbun.screw.core.util.Assert; +import cn.smallbun.screw.core.util.ExceptionUtils; +import cn.smallbun.screw.core.util.JdbcUtils; + +import javax.sql.DataSource; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.List; + +import static cn.smallbun.screw.core.constant.DefaultConstants.PERCENT_SIGN; + +/** + * @author xielongwang + * @create 2020/8/19 4:53 下午 + * @email siaron.wang@gmail.com + * @description + */ +public class PhoenixDataBaseQuery extends AbstractDatabaseQuery { + + public PhoenixDataBaseQuery(DataSource dataSource) { + super(dataSource); + } + + @Override + public Database getDataBase() throws QueryException { + PhoenixSqlDatabaseModel model = new PhoenixSqlDatabaseModel(); + model.setDatabase(getCatalog()); + return model; + } + + @Override + public List getTables() throws QueryException { + ResultSet resultSet = null; + try { + //查询 + resultSet = getMetaData().getTables("", getSchema(), PERCENT_SIGN, + new String[]{"TABLE"}); + //映射 + return Mapping.convertList(resultSet, PhoenixlTableModel.class); + } catch (SQLException e) { + throw ExceptionUtils.mpe(e); + } finally { + JdbcUtils.close(resultSet); + } + } + + @Override + public List getTableColumns(String table) throws QueryException { + Assert.notEmpty(table, "Table name can not be empty!"); + ResultSet resultSet = null; + try { + //查询 + resultSet = getMetaData().getColumns("", getSchema(), table, PERCENT_SIGN); + //映射 + List list = Mapping.convertList(resultSet, PhoenixColumnModel.class); + + //处理columnName + list.forEach(model -> model.setColumnType(model.getTypeName())); + return list; + } catch (SQLException e) { + throw ExceptionUtils.mpe(e); + } finally { + JdbcUtils.close(resultSet); + } + } + + @Override + public List getTableColumns() throws QueryException { + return getTableColumns(PERCENT_SIGN); + } + + @Override + public List getPrimaryKeys(String table) throws QueryException { + ResultSet resultSet = null; + try { + //查询 + resultSet = getMetaData().getPrimaryKeys("", getSchema(), table); + //映射 + return Mapping.convertList(resultSet, PhoenixPrimaryKeyModel.class); + } catch (SQLException e) { + throw ExceptionUtils.mpe(e); + } finally { + JdbcUtils.close(resultSet, this.connection); + } + } + + + @Override + public List getPrimaryKeys() throws QueryException { + ResultSet resultSet = null; + try { + // 由于单条循环查询存在性能问题,所以这里通过自定义SQL查询数据库主键信息 + String sql = "SELECT '' AS TABLE_CAT, TABLE_SCHEM AS TABLE_SCHEM, TABLE_NAME, COLUMN_NAME AS COLUMN_NAME, KEY_SEQ, PK_NAME FROM SYSTEM.\"CATALOG\" WHERE PK_NAME IS NOT NULL AND KEY_SEQ IS NOT NULL GROUP BY TABLE_SCHEM, TABLE_NAME, COLUMN_NAME, KEY_SEQ, PK_NAME"; + // 拼接参数 + resultSet = prepareStatement(sql).executeQuery(); + return Mapping.convertList(resultSet, PhoenixPrimaryKeyModel.class); + } catch (SQLException e) { + throw new QueryException(e); + } finally { + JdbcUtils.close(resultSet); + } + } + + @Override + protected String getCatalog() throws QueryException { + return "phoenix"; + } +} diff --git a/screw-core/src/main/java/cn/smallbun/screw/core/query/phoenix/model/PhoenixColumnModel.java b/screw-core/src/main/java/cn/smallbun/screw/core/query/phoenix/model/PhoenixColumnModel.java new file mode 100644 index 0000000..28a8214 --- /dev/null +++ b/screw-core/src/main/java/cn/smallbun/screw/core/query/phoenix/model/PhoenixColumnModel.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.phoenix.model; + +import cn.smallbun.screw.core.mapping.MappingField; +import cn.smallbun.screw.core.metadata.Column; +import lombok.Data; + +/** + * 表字段信息 + * + * @author siaron.wang@gmail.com + */ +@Data +public class PhoenixColumnModel implements Column { + + private static final long serialVersionUID = -7231934486902707912L; + /** + * + */ + @MappingField(value = "SCOPE_TABLE") + private Object scopeTable; + /** + * + */ + @MappingField(value = "TABLE_CAT") + private String tableCat; + /** + * + */ + @MappingField(value = "BUFFER_LENGTH") + private String bufferLength; + /** + * + */ + @MappingField(value = "IS_NULLABLE") + private String isNullable; + /** + * 表名 + */ + @MappingField(value = "TABLE_NAME") + private String tableName; + /** + * 默认值 + */ + @MappingField(value = "COLUMN_DEF") + private String columnDef; + /** + * + */ + @MappingField(value = "SCOPE_CATALOG") + private Object scopeCatalog; + /** + * + */ + @MappingField(value = "TABLE_SCHEM") + private Object tableSchem; + /** + * + */ + @MappingField(value = "COLUMN_NAME") + private String columnName; + /** + * + */ + @MappingField(value = "NULLABLE") + private String nullable; + /** + * 说明 + */ + @MappingField(value = "REMARKS") + private String remarks; + /** + * 小数位 + */ + @MappingField(value = "DECIMAL_DIGITS") + private String decimalDigits; + /** + * + */ + @MappingField(value = "NUM_PREC_RADIX") + private String numPrecRadix; + /** + * + */ + @MappingField(value = "SQL_DATETIME_SUB") + private String sqlDatetimeSub; + /** + * + */ + @MappingField(value = "IS_GENERATEDCOLUMN") + private String isGeneratedColumn; + /** + * + */ + @MappingField(value = "IS_AUTOINCREMENT") + private String isAutoIncrement; + /** + * + */ + @MappingField(value = "SQL_DATA_TYPE") + private String sqlDataType; + /** + * + */ + @MappingField(value = "CHAR_OCTET_LENGTH") + private String charOctetLength; + /** + * + */ + @MappingField(value = "ORDINAL_POSITION") + private String ordinalPosition; + /** + * + */ + @MappingField(value = "SCOPE_SCHEMA") + private Object scopeSchema; + /** + * + */ + @MappingField(value = "SOURCE_DATA_TYPE") + private Object sourceDataType; + /** + * 数据类型 + */ + @MappingField(value = "DATA_TYPE") + private String dataType; + /** + * + */ + @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/phoenix/model/PhoenixPrimaryKeyModel.java b/screw-core/src/main/java/cn/smallbun/screw/core/query/phoenix/model/PhoenixPrimaryKeyModel.java new file mode 100644 index 0000000..ac98bc9 --- /dev/null +++ b/screw-core/src/main/java/cn/smallbun/screw/core/query/phoenix/model/PhoenixPrimaryKeyModel.java @@ -0,0 +1,63 @@ +/* + * 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.phoenix.model; + +import cn.smallbun.screw.core.mapping.MappingField; +import cn.smallbun.screw.core.metadata.PrimaryKey; +import lombok.Data; + +/** + * 表主键 + * + * @author siaron.wang@gmail.com + */ +@Data +public class PhoenixPrimaryKeyModel implements PrimaryKey { + + private static final long serialVersionUID = -4908250184995248600L; + /** + * 主键名称 + */ + @MappingField(value = "PK_NAME") + private String pkName; + /** + * + */ + @MappingField(value = "TABLE_SCHEM") + private String tableSchem; + /** + * + */ + @MappingField(value = "KEY_SEQ") + private String keySeq; + /** + * tableCat + */ + @MappingField(value = "TABLE_CAT") + private String tableCat; + /** + * 列名 + */ + @MappingField(value = "COLUMN_NAME") + private String columnName; + /** + * 表名 + */ + @MappingField(value = "TABLE_NAME") + private String tableName; +} diff --git a/screw-core/src/main/java/cn/smallbun/screw/core/query/phoenix/model/PhoenixSqlDatabaseModel.java b/screw-core/src/main/java/cn/smallbun/screw/core/query/phoenix/model/PhoenixSqlDatabaseModel.java new file mode 100644 index 0000000..7ad19c8 --- /dev/null +++ b/screw-core/src/main/java/cn/smallbun/screw/core/query/phoenix/model/PhoenixSqlDatabaseModel.java @@ -0,0 +1,18 @@ +package cn.smallbun.screw.core.query.phoenix.model; + +import cn.smallbun.screw.core.metadata.Database; +import lombok.Data; + +/** + * @author xielongwang + * @create 2020/8/19 4:55 下午 + * @email siaron.wang@gmail.com + * @description + */ +@Data +public class PhoenixSqlDatabaseModel implements Database { + /** + * 数据库名称 + */ + private String database; +} diff --git a/screw-core/src/main/java/cn/smallbun/screw/core/query/phoenix/model/PhoenixlTableModel.java b/screw-core/src/main/java/cn/smallbun/screw/core/query/phoenix/model/PhoenixlTableModel.java new file mode 100644 index 0000000..4e879c6 --- /dev/null +++ b/screw-core/src/main/java/cn/smallbun/screw/core/query/phoenix/model/PhoenixlTableModel.java @@ -0,0 +1,81 @@ +/* + * 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.phoenix.model; + +import cn.smallbun.screw.core.mapping.MappingField; +import cn.smallbun.screw.core.metadata.Table; +import lombok.Data; + +/** + * 表信息 + * + * @author siaron.wang@gmail.com + */ +@Data +public class PhoenixlTableModel implements Table { + /** + * refGeneration + */ + @MappingField(value = "REF_GENERATION") + private String refGeneration; + /** + * typeName + */ + @MappingField(value = "TYPE_NAME") + private String typeName; + /** + * typeSchem + */ + @MappingField(value = "TYPE_SCHEM") + private String typeSchem; + /** + * tableSchem + */ + @MappingField(value = "TABLE_SCHEM") + private String tableSchem; + /** + * typeCat + */ + @MappingField(value = "TYPE_CAT") + private String typeCat; + /** + * tableCat + */ + @MappingField(value = "TABLE_CAT") + private Object tableCat; + /** + * 表名称 + */ + @MappingField(value = "TABLE_NAME") + private String tableName; + /** + * selfReferencingColName + */ + @MappingField(value = "SELF_REFERENCING_COL_NAME") + private String selfReferencingColName; + /** + * 说明 + */ + @MappingField(value = "REMARKS") + private String remarks; + /** + * 表类型 + */ + @MappingField(value = "TABLE_TYPE") + private String tableType; +} diff --git a/screw-core/src/main/java/cn/smallbun/screw/core/query/phoenix/package-info.java b/screw-core/src/main/java/cn/smallbun/screw/core/query/phoenix/package-info.java new file mode 100644 index 0000000..b272a6c --- /dev/null +++ b/screw-core/src/main/java/cn/smallbun/screw/core/query/phoenix/package-info.java @@ -0,0 +1,25 @@ +/* + * 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 . + */ +/** + * Database query + * + * @author xielong.wang + * Created by siaron.wang@gmail.com + */ +package cn.smallbun.screw.core.query.phoenix; +//phoenix 默认无法添加注释的(不解析 COMMENT ON COLUMN 语句). 但是可以再catalog表中remark字段上手动增加注释.符合jdbc的规范.导出时可以有注释的. \ No newline at end of file diff --git a/screw-core/src/main/resources/properties/phoenix.properties b/screw-core/src/main/resources/properties/phoenix.properties new file mode 100644 index 0000000..7d68554 --- /dev/null +++ b/screw-core/src/main/resources/properties/phoenix.properties @@ -0,0 +1,4 @@ +driver=org.apache.phoenix.jdbc.PhoenixDriver +url=jdbc:phoenix:xxxx:2181 +username=username +password=password \ No newline at end of file diff --git a/screw-core/src/test/java/cn/smallbun/screw/core/produce/PhoenixDocumentationBuilderTest.java b/screw-core/src/test/java/cn/smallbun/screw/core/produce/PhoenixDocumentationBuilderTest.java new file mode 100644 index 0000000..7f0ead5 --- /dev/null +++ b/screw-core/src/test/java/cn/smallbun/screw/core/produce/PhoenixDocumentationBuilderTest.java @@ -0,0 +1,91 @@ +/* + * 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 javax.sql.DataSource; +import java.io.IOException; +import java.util.Properties; + +import static cn.smallbun.screw.core.common.Constants.fileOutputDir; + +/** + * Phoenix 文档生成测试 + * + * @author xielong.wang + * Created by siaron.wang@gmail.com 2020/3/30 18:36 + */ +public class PhoenixDocumentationBuilderTest 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); + Properties properties = new Properties(); + properties.put("phoenix.schema.isNamespaceMappingEnabled", true); + properties.put("phoenix.schema.mapSystemTablesToNamespace", true); + hikariConfig.setDataSourceProperties(properties); + DataSource dataSource = new HikariDataSource(hikariConfig); + //生成配置 + EngineConfig engineConfig = EngineConfig.builder() + //生成文件路径 + .fileOutputDir(fileOutputDir) + //文件类型 + .fileType(EngineFileType.HTML) + //生成模板实现 + .produceType(EngineTemplateType.freemarker).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/phoenix.properties"; + } +} diff --git a/screw-maven-plugin/src/main/java/cn/smallbun/screw/maven/plugin/mojo/RunDocMojo.java b/screw-maven-plugin/src/main/java/cn/smallbun/screw/maven/plugin/mojo/RunDocMojo.java index 9075893..32fb7c1 100644 --- a/screw-maven-plugin/src/main/java/cn/smallbun/screw/maven/plugin/mojo/RunDocMojo.java +++ b/screw-maven-plugin/src/main/java/cn/smallbun/screw/maven/plugin/mojo/RunDocMojo.java @@ -39,6 +39,7 @@ import org.apache.maven.plugins.annotations.Parameter; import javax.sql.DataSource; import java.util.List; +import java.util.Properties; import static cn.smallbun.screw.core.constant.DefaultConstants.*; import static cn.smallbun.screw.core.util.FileUtils.getRealFilePath; @@ -267,6 +268,11 @@ public class RunDocMojo extends AbstractMojo { || JdbcUtils.getDbType(getJdbcUrl()).equals(DatabaseType.MARIADB)) { hikariConfig.addDataSourceProperty(USE_INFORMATION_SCHEMA, "true"); } + //phoenix + if (JdbcUtils.getDbType(getJdbcUrl()).equals(DatabaseType.PHOENIX)) { + hikariConfig.addDataSourceProperty(PHOENIX_SYS_NAMESPACE_MAPPING,true); + hikariConfig.addDataSourceProperty(PHOENIX_NAMESPACE_MAPPING,true); + } //oracle if (JdbcUtils.getDbType(getJdbcUrl()).equals(DatabaseType.ORACLE)) { hikariConfig.addDataSourceProperty(ORACLE_REMARKS, "true");