Add TiDB support

Signed-off-by: Xiaoguang Sun <sunxiaoguang@pingcap.com>
This commit is contained in:
Xiaoguang Sun
2021-11-29 09:25:36 +08:00
parent 98f6ea65ae
commit 1bb36ff318
20 changed files with 51 additions and 13 deletions
+1 -1
View File
@@ -60,7 +60,7 @@ public enum DbType {
oscar (1 << 40),
tidb ( 1 << 41),
ingres (0),
cloudscape (0),
@@ -1856,7 +1856,7 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
}
DbType dbType = DbType.of(this.dbTypeName);
if (dbType == DbType.mysql || dbType == DbType.ads) {
if (JdbcUtils.isMysqlDbType(dbType)) {
if (variables != null) {
ResultSet rs = null;
try {
@@ -843,10 +843,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
}
DbType dbType = DbType.of(this.dbTypeName);
if (dbType == DbType.mysql
|| dbType == DbType.mariadb
|| dbType == DbType.oceanbase
|| dbType == DbType.ads) {
if (JdbcUtils.isMysqlDbType(dbType)) {
boolean cacheServerConfigurationSet = false;
if (this.connectProperties.containsKey("cacheServerConfiguration")) {
cacheServerConfigurationSet = true;
@@ -138,7 +138,7 @@ public class DruidPooledStatement extends PoolableWrapper implements Statement {
return;
}
if (DbType.mysql != DbType.of(dataSource.dbTypeName)) {
if (!JdbcUtils.isMysqlDbType(dataSource.dbTypeName)) {
return;
}
@@ -29,6 +29,7 @@ import com.alibaba.druid.sql.dialect.oracle.visitor.OracleASTVisitorAdapter;
import com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectQueryBlock;
import com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerSelectQueryBlock;
import com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerTop;
import com.alibaba.druid.util.JdbcUtils;
import java.util.List;
@@ -157,6 +158,7 @@ public class PagerUtils {
switch (dbType) {
case mysql:
case mariadb:
case tidb:
case h2:
case ads:
return limitMySqlQueryBlock(queryBlock, dbType, offset, count, check);
@@ -534,6 +536,7 @@ public class PagerUtils {
switch (dbType) {
case mysql:
case mariadb:
case tidb:
case ads:
return new MySqlSelectQueryBlock();
case oracle:
@@ -638,7 +641,7 @@ public class PagerUtils {
public static boolean hasUnorderedLimit(String sql, DbType dbType) {
List<SQLStatement> stmtList = SQLUtils.parseStatements(sql, dbType);
if (DbType.mysql == dbType) {
if (JdbcUtils.isMysqlDbType(dbType)) {
MySqlUnorderedLimitDetectVisitor visitor = new MySqlUnorderedLimitDetectVisitor();
@@ -478,6 +478,7 @@ public class SQLUtils {
}
case mysql:
case mariadb:
case tidb:
return new MySqlOutputVisitor(out);
case postgresql:
return new PGOutputVisitor(out);
@@ -532,6 +533,7 @@ public class SQLUtils {
return new OracleSchemaStatVisitor(repository);
case mysql:
case mariadb:
case tidb:
case elastic_search:
return new MySqlSchemaStatVisitor(repository);
case postgresql:
@@ -639,7 +641,7 @@ public class SQLUtils {
dbType = DbType.mysql;
}
String formatMethod = "";
if (DbType.mysql == dbType) {
if (JdbcUtils.isMysqlDbType(dbType)) {
formatMethod = "STR_TO_DATE";
if (StringUtils.isEmpty(pattern)) pattern = "%Y-%m-%d %H:%i:%s";
} else if (DbType.oracle == dbType) {
@@ -1011,7 +1013,7 @@ public class SQLUtils {
if (OracleUtils.isKeyword(normalizeName)) {
return name;
}
} else if (DbType.mysql == dbType) {
} else if (JdbcUtils.isMysqlDbType(dbType)) {
if (MySqlUtils.isKeyword(normalizeName)) {
return name;
}
@@ -210,7 +210,9 @@ public class SQLDateExpr extends SQLExprImpl implements SQLLiteralExpr, SQLValua
, DbType.postgresql
, DbType.ads
, DbType.hive
, DbType.odps);
, DbType.odps
, DbType.mariadb
, DbType.tidb);
public static boolean isSupport(DbType dbType) {
return (dbType.mask & supportDbTypes) != 0;
@@ -149,6 +149,8 @@ public class SQLDateTimeExpr extends SQLExprImpl implements SQLLiteralExpr, SQLV
, DbType.presto
, DbType.trino
, DbType.postgresql
, DbType.mariadb
, DbType.tidb
);
public static boolean isSupport(DbType dbType) {
@@ -150,6 +150,8 @@ public class SQLTimeExpr extends SQLExprImpl implements SQLLiteralExpr, SQLValua
, DbType.presto
, DbType.trino
, DbType.postgresql
, DbType.mariadb
, DbType.tidb
);
public static boolean isSupport(DbType dbType) {
@@ -127,6 +127,8 @@ public class SQLDeleteBuilderImpl implements SQLDeleteBuilder {
case oracle:
return new OracleDeleteStatement();
case mysql:
case mariadb:
case tidb:
return new MySqlDeleteStatement();
case postgresql:
return new PGDeleteStatement();
@@ -161,6 +161,7 @@ public class SQLUpdateBuilderImpl extends SQLBuilderImpl implements SQLUpdateBui
switch (dbType) {
case mysql:
case mariadb:
case tidb:
return new MySqlUpdateStatement();
case oracle:
return new OracleUpdateStatement();
@@ -413,6 +413,7 @@ public class SchemaRepository {
switch (dbType) {
case mysql:
case mariadb:
case tidb:
case sqlite:
resolveVisitor = new SchemaResolveVisitorFactory.MySqlResolveVisitor(this, optionsValue);
break;
@@ -29,6 +29,7 @@ public class SQLRefactorVisitor extends SQLASTVisitorAdapter {
switch (dbType) {
case mysql:
case mariadb:
case tidb:
case ads:
quote = '`';
break;
@@ -43,6 +43,7 @@ public final class ExportParameterVisitorUtils {
switch (dbType) {
case mysql:
case mariadb:
case tidb:
return new MySqlExportParameterVisitor(out);
case oracle:
return new OracleExportParameterVisitor(out);
@@ -118,6 +118,7 @@ public class SQLEvalVisitorUtils {
switch (dbType) {
case mysql:
case mariadb:
case tidb:
case h2:
return new MySqlEvalVisitorImpl();
case oracle:
@@ -602,6 +602,8 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
final SQLASTVisitor orderByVisitor;
switch (dbType) {
case mysql:
case mariadb:
case tidb:
return new MySqlOrderByStatVisitor(x);
case postgresql:
return new PGOrderByStatVisitor(x);
@@ -56,6 +56,9 @@ public interface JdbcConstants {
String MARIADB = "mariadb";
String MARIADB_DRIVER = "org.mariadb.jdbc.Driver";
DbType TIDB = DbType.tidb;
String TIDB_DRIVER = "io.tidb.bigdata.jdbc.TiDBDriver";
DbType DERBY = DbType.derby;
String HBASE = "hbase";
@@ -418,6 +418,8 @@ public final class JdbcUtils implements JdbcConstants {
return LOG4JDBC_DRIVER;
} else if (rawUrl.startsWith("jdbc:mariadb:")) {
return MARIADB_DRIVER;
} else if (rawUrl.startsWith("jdbc:tidb:")) {
return TIDB_DRIVER;
} else if (rawUrl.startsWith("jdbc:oracle:") //
|| rawUrl.startsWith("JDBC:oracle:")) {
return ORACLE_DRIVER;
@@ -544,6 +546,8 @@ public final class JdbcUtils implements JdbcConstants {
return DbType.mysql;
} else if (rawUrl.startsWith("jdbc:mariadb:")) {
return DbType.mariadb;
} else if (rawUrl.startsWith("jdbc:tidb:")) {
return DbType.tidb;
} else if (rawUrl.startsWith("jdbc:oracle:") || rawUrl.startsWith("jdbc:log4jdbc:oracle:")) {
return DbType.oracle;
} else if (rawUrl.startsWith("jdbc:alibaba:oracle:")) {
@@ -886,7 +890,7 @@ public final class JdbcUtils implements JdbcConstants {
}
public static List<String> showTables(Connection conn, DbType dbType) throws SQLException {
if (DbType.mysql == dbType || DbType.oceanbase == dbType) {
if (isMysqlDbType(dbType)) {
return MySqlUtils.showTables(conn);
}
@@ -905,7 +909,7 @@ public final class JdbcUtils implements JdbcConstants {
}
public static String getCreateTableScript(Connection conn, DbType dbType, boolean sorted, boolean simplify) throws SQLException {
if (DbType.mysql == dbType || DbType.oceanbase == dbType) {
if (isMysqlDbType(dbType)) {
return MySqlUtils.getCreateTableScript(conn, sorted, simplify);
}
@@ -947,8 +951,10 @@ public final class JdbcUtils implements JdbcConstants {
switch (dbType) {
case mysql:
case oceanbase:
case ads:
case drds:
case mariadb:
case tidb:
case h2:
return true;
default:
@@ -134,6 +134,7 @@ public class WallFilter extends FilterAdapter implements WallFilterMBean {
case oceanbase:
case drds:
case mariadb:
case tidb:
case h2:
case presto:
case trino:
@@ -40,4 +40,15 @@ public class JdbcUtilsTest extends TestCase {
assertEquals("not support log4jdbc mysql, url like jdbc:log4jdbc:mysql:...", DbType.mysql, dbType);
}
public void testGetDbTypeTiDB() {
String jdbcUrl = "jdbc:tidb://localhost:4000/test";
String dbType = JdbcUtils.getDbType(jdbcUrl, null);
assertEquals("Does not support TiDB, for url like jdbc:tidb:...", DbType.tidb, dbType);
}
public void testTiDBIsMySQLDBType() {
assertTrue("TiDB suppose to be a MySQL compatible DB", JdbcUtils.isMysqlDbType(DbType.tidb));
assertTrue("TiDB suppose to be a MySQL compatible DB", JdbcUtils.isMysqlDbType("tidb"));
assertTrue("TiDB suppose to be a MySQL compatible DB", JdbcUtils.isMySqlDriver(JdbcUtils.TIDB_DRIVER));
}
}