Skip to content

Commit

Permalink
Add support for kingbase.
Browse files Browse the repository at this point in the history
  • Loading branch information
HanHuimin001 committed Oct 18, 2019
1 parent 91a922a commit 40b10d6
Show file tree
Hide file tree
Showing 14 changed files with 464 additions and 0 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ ext {
"postgresql" : "org.postgresql:postgresql:9.4.1212",
"oracle" : fileTree(dir: 'libs', includes: ['ojdbc-11.2.0.3-jdk16.jar']),
"dm" : fileTree(dir: 'libs', includes: ["jdbcDriver-18.jar"]),
"kingbase" : fileTree(dir: 'libs', includes: ["kingbase8-8.2.0.jar"]),
"h2" : "com.h2database:h2:1.4.197",
"mysql" : "mysql:mysql-connector-java:8.0.15",
"sqlite" : "org.xerial:sqlite-jdbc:3.27.2.1",
Expand Down
Binary file added libs/kingbase8-8.2.0.jar
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ public enum DbType {
* xugu
*/
XU_GU("xugu", "虚谷数据库", "com.baomidou.mybatisplus.extension.plugins.pagination.dialects.XuGuDialect"),
/**
* Kingbase
*/
KINGBASE_ES("kingbasees", "人大金仓数据库", "com.baomidou.mybatisplus.extension.plugins.pagination.dialects.KingbaseDialect"),
/**
* UNKONWN DB
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2011-2020, baomidou ([email protected]).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.baomidou.mybatisplus.extension.incrementer;

import com.baomidou.mybatisplus.core.incrementer.IKeyGenerator;

/**
* Kingbase Sequence
*
* @author kingbase
* @since 2019-10-17
*/
public class KingbaseKeyGenerator implements IKeyGenerator {

@Override
public String executeSql(String incrementerName) {
return "select nextval('" + incrementerName + "')";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright (c) 2011-2020, baomidou ([email protected]).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.baomidou.mybatisplus.extension.plugins.pagination.dialects;

/**
* Kingbase 数据库分页语句组装实现
*
* @author kingbase
* @since 2019-10-12
*/
public class KingbaseDialect extends PostgreDialect {
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ public static DbType getDbType(String jdbcUrl) {
return DbType.DM;
} else if (jdbcUrl.contains(":xugu:")) {
return DbType.XU_GU;
} else if (jdbcUrl.contains(":kingbase:") || jdbcUrl.contains(":kingbase8:")) {
return DbType.KINGBASE_ES;
} else {
logger.warn("The jdbcUrl is " + jdbcUrl + ", Mybatis Plus Cannot Read Database type or The Database's Not Supported!");
return DbType.OTHER;
Expand Down
1 change: 1 addition & 0 deletions mybatis-plus-generator/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ dependencies {
testCompile "${lib.postgresql}"
testCompile lib.oracle as ConfigurableFileTree
testCompile lib.dm as ConfigurableFileTree
testCompile lib.kingbase as ConfigurableFileTree
testCompile "${lib.h2}"
testCompile "${lib.mysql}"
testCompile "${lib.sqlite}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ public IDbQuery getDbQuery() {
case DM:
dbQuery = new DMQuery();
break;
case KINGBASE_ES:
dbQuery = new KingbaseESQuery();
break;
default:
// 默认 MYSQL
dbQuery = new MySqlQuery();
Expand Down Expand Up @@ -147,6 +150,8 @@ private DbType getDbType(String str) {
return DbType.MARIADB;
} else if (str.contains("h2")) {
return DbType.H2;
} else if (str.contains("kingbase") || str.contains("kingbase8")) {
return DbType.KINGBASE_ES;
} else {
return null;
}
Expand Down Expand Up @@ -176,6 +181,9 @@ public ITypeConvert getTypeConvert() {
case MARIADB:
typeConvert = new MySqlTypeConvert();
break;
case KINGBASE_ES:
typeConvert = new KingbaseESTypeConvert();
break;
default:
// 默认 MYSQL
typeConvert = new MySqlTypeConvert();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,15 @@ private List<TableInfo> getTablesInfo(StrategyConfig config) {
}
tablesSql = String.format(tablesSql, schema);
}
if (DbType.KINGBASE_ES == dbQuery.dbType()) {
String schema = dataSourceConfig.getSchemaName();
if (schema == null) {
//kingbase 默认 schema=PUBLIC
schema = "PUBLIC";
dataSourceConfig.setSchemaName(schema);
}
tablesSql = String.format(tablesSql, schema);
}
if (DbType.DB2 == dbQuery.dbType()) {
String schema = dataSourceConfig.getSchemaName();
if (schema == null) {
Expand Down Expand Up @@ -551,6 +560,8 @@ private TableInfo convertTableFields(TableInfo tableInfo, StrategyConfig config)
Set<String> h2PkColumns = new HashSet<>();
if (DbType.POSTGRE_SQL == dbType) {
tableFieldsSql = String.format(tableFieldsSql, dataSourceConfig.getSchemaName(), tableName);
} else if (DbType.KINGBASE_ES == dbType) {
tableFieldsSql = String.format(tableFieldsSql, dataSourceConfig.getSchemaName(), tableName);
} else if (DbType.DB2 == dbType) {
tableFieldsSql = String.format(tableFieldsSql, dataSourceConfig.getSchemaName(), tableName);
} else if (DbType.ORACLE == dbType) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright (c) 2011-2020, baomidou ([email protected]).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.baomidou.mybatisplus.generator.config.converts;

import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.ITypeConvert;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.config.rules.IColumnType;

/**
* KingbaseES 字段类型转换
*
* @author kingbase
* @since 2019-10-12
*/
public class KingbaseESTypeConvert implements ITypeConvert {

@Override
public IColumnType processTypeConvert(GlobalConfig globalConfig, String fieldType) {
String t = fieldType.toLowerCase();
if (t.contains("char")) {
return DbColumnType.STRING;
} else if (t.contains("bigint")) {
return DbColumnType.LONG;
} else if (t.contains("int")) {
return DbColumnType.INTEGER;
} else if (t.contains("date") || t.contains("time")) {
switch (globalConfig.getDateType()) {
case ONLY_DATE:
return DbColumnType.DATE;
case SQL_PACK:
switch (t) {
case "date":
return DbColumnType.DATE_SQL;
case "time":
return DbColumnType.TIME;
default:
return DbColumnType.TIMESTAMP;
}
case TIME_PACK:
switch (t) {
case "date":
return DbColumnType.LOCAL_DATE;
case "time":
return DbColumnType.LOCAL_TIME;
default:
return DbColumnType.LOCAL_DATE_TIME;
}
default:
return DbColumnType.DATE;
}
} else if (t.contains("text")) {
return DbColumnType.STRING;
} else if (t.contains("bit")) {
return DbColumnType.BOOLEAN;
} else if (t.contains("decimal")) {
return DbColumnType.BIG_DECIMAL;
} else if (t.contains("clob")) {
return DbColumnType.CLOB;
} else if (t.contains("blob")) {
return DbColumnType.BYTE_ARRAY;
} else if (t.contains("float")) {
return DbColumnType.FLOAT;
} else if (t.contains("double")) {
return DbColumnType.DOUBLE;
} else if (t.contains("json") || t.contains("enum")) {
return DbColumnType.STRING;
} else if (t.contains("boolean")) {
return DbColumnType.BOOLEAN;
} else if (t.contains("numeric")) {
return DbColumnType.BIG_DECIMAL;
}
return DbColumnType.STRING;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright (c) 2011-2020, baomidou ([email protected]).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.baomidou.mybatisplus.generator.config.querys;

import com.baomidou.mybatisplus.annotation.DbType;

/**
* KingbaseES 表数据查询
*
* @author kingbase
* @since 2019-10-12
*/
public class KingbaseESQuery extends AbstractDbQuery {


@Override
public DbType dbType() {
return DbType.KINGBASE_ES;
}


@Override
public String tablesSql() {
return "SELECT A.tablename, obj_description(relfilenode, 'sys_class') AS comments FROM sys_tables A, sys_class B WHERE A.schemaname='%s' AND A.tablename = B.relname";
}


@Override
public String tableFieldsSql() {
return "SELECT A.attname AS name, format_type(A.atttypid, A.atttypmod) AS type,col_description(A.attrelid, A.attnum) AS comment, (CASE C.contype WHEN 'p' THEN 'PRI' ELSE '' END) AS key " +
"FROM sys_attribute A LEFT JOIN sys_constraint C ON A.attnum = C.conkey[1] AND A.attrelid = C.conrelid " +
"WHERE A.attrelid = '%s.%s'::regclass AND A.attnum > 0 AND NOT A.attisdropped ORDER BY A.attnum";
}


@Override
public String tableName() {
return "tablename";
}


@Override
public String tableComment() {
return "comments";
}


@Override
public String fieldName() {
return "name";
}


@Override
public String fieldType() {
return "type";
}


@Override
public String fieldComment() {
return "comment";
}


@Override
public String fieldKey() {
return "key";
}

}
Loading

0 comments on commit 40b10d6

Please sign in to comment.