Skip to content

Commit

Permalink
fix(connection): check database type when test connection. (#232)
Browse files Browse the repository at this point in the history
* check database type when test connection.

* response comments.
  • Loading branch information
guowl3 authored Sep 7, 2023
1 parent 4d79274 commit e510faf
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ public ConnectionTestResult test(@NotNull @Valid TestConnectionReq req) {
req.setDefaultSchema(ConnectionSessionUtil.getUserOrSchemaString(req.getDefaultSchema(), req.getDialectType()));
ConnectionTestResult result = connectionTesting.test(req);
ErrorCode errorCode = result.getErrorCode();
if (errorCode != ErrorCodes.Unknown && errorCode != ErrorCodes.ConnectionUnsupportedConnectType) {
if (errorCode != ErrorCodes.Unknown && errorCode != ErrorCodes.ConnectionUnsupportedConnectType
&& errorCode != ErrorCodes.ConnectionDatabaseTypeMismatched) {
return result;
}
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

import org.pf4j.ExtensionPoint;

import com.oceanbase.odc.core.shared.constant.DialectType;

/**
* @author yaobin
* @date 2023-04-14
Expand All @@ -27,4 +29,6 @@
public interface InformationExtensionPoint extends ExtensionPoint {

String getDBVersion(Connection connection);

DialectType getDBType(Connection connection);
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ public static TestResult unsupportedDBVersion(@NonNull String version) {
return fail(ErrorCodes.ConnectionUnsupportedDBVersion, new String[] {version});
}

public static TestResult databaseTypeMismatched(@NonNull String dbType) {
return fail(ErrorCodes.ConnectionDatabaseTypeMismatched, new String[] {dbType});
}

public static TestResult success() {
TestResult result = new TestResult();
result.setActive(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.pf4j.Extension;

import com.oceanbase.odc.core.shared.Verify;
import com.oceanbase.odc.core.shared.constant.DialectType;
import com.oceanbase.odc.plugin.connect.api.InformationExtensionPoint;
import com.oceanbase.odc.plugin.connect.obmysql.util.JdbcOperationsUtil;

Expand Down Expand Up @@ -50,4 +51,9 @@ public String getDBVersion(Connection connection) {
Verify.notNull(dbVersion, "MySQL DB Version");
return dbVersion;
}

@Override
public DialectType getDBType(Connection connection) {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import com.oceanbase.odc.common.util.ExceptionUtils;
import com.oceanbase.odc.common.util.StringUtils;
import com.oceanbase.odc.core.datasource.ConnectionInitializer;
import com.oceanbase.odc.core.shared.constant.DialectType;
import com.oceanbase.odc.core.shared.constant.OdcConstants;
import com.oceanbase.odc.plugin.connect.api.ConnectionExtensionPoint;
import com.oceanbase.odc.plugin.connect.api.TestResult;
Expand Down Expand Up @@ -127,6 +128,10 @@ private TestResult test(String jdbcUrl, Properties properties, int queryTimeout)
return TestResult.unknownError(e);
}
try (Connection connection = DriverManager.getConnection(jdbcUrl, properties)) {
OBMySQLInformationExtension informationExtension = new OBMySQLInformationExtension();
if (informationExtension.getDBType(connection) != getDBType()) {
return TestResult.databaseTypeMismatched(getDBType().name());
}
try (Statement statement = connection.createStatement()) {
if (queryTimeout >= 0) {
statement.setQueryTimeout(queryTimeout);
Expand Down Expand Up @@ -170,4 +175,8 @@ protected HostAddress parseJdbcUrl(String jdbcUrl) throws SQLException {
urlParser.getHostAddresses().get(0).getPort());
}

public DialectType getDBType() {
return DialectType.OB_MYSQL;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@

import org.pf4j.Extension;

import com.oceanbase.odc.common.util.StringUtils;
import com.oceanbase.odc.core.shared.Verify;
import com.oceanbase.odc.core.shared.constant.DialectType;
import com.oceanbase.odc.core.sql.util.OBUtils;
import com.oceanbase.odc.plugin.connect.api.InformationExtensionPoint;
import com.oceanbase.odc.plugin.connect.obmysql.util.JdbcOperationsUtil;
Expand All @@ -35,8 +37,7 @@
@Extension
public class OBMySQLInformationExtension implements InformationExtensionPoint {

@Override
public String getDBVersion(Connection connection) {
private String getDBVersionComment(Connection connection) {
String querySql = "show variables like 'version_comment'";
String v = null;
try {
Expand All @@ -51,7 +52,30 @@ public String getDBVersion(Connection connection) {
} catch (Exception exception) {
log.warn("Failed to get ob version", exception);
}
return OBUtils.parseObVersionComment(v);
return v;
}

@Override
public String getDBVersion(Connection connection) {
String dbVersionComment = getDBVersionComment(connection);
return dbVersionComment == null ? null : OBUtils.parseObVersionComment(dbVersionComment);
}

@Override
public DialectType getDBType(Connection connection) {
try {
String dbVersionComment = getDBVersionComment(connection);
if (StringUtils.isNotEmpty(dbVersionComment) && dbVersionComment.toUpperCase().contains("OCEANBASE")) {
if (connection.getMetaData().getDatabaseProductName().equalsIgnoreCase("MYSQL")) {
return DialectType.OB_MYSQL;
}
if (connection.getMetaData().getDatabaseProductName().equalsIgnoreCase("ORACLE")) {
return DialectType.OB_ORACLE;
}
}
} catch (Exception e) {
log.warn("Failed to parse database type.", e);
}
return DialectType.UNKNOWN;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.pf4j.Extension;

import com.oceanbase.odc.core.datasource.ConnectionInitializer;
import com.oceanbase.odc.core.shared.constant.DialectType;
import com.oceanbase.odc.plugin.connect.obmysql.OBMySQLConnectionExtension;
import com.oceanbase.odc.plugin.connect.obmysql.initializer.EnableTraceInitializer;
import com.oceanbase.odc.plugin.connect.oboracle.initializer.OBOracleDBMSOutputInitializer;
Expand All @@ -41,4 +42,8 @@ public List<ConnectionInitializer> getConnectionInitializers() {
return initializers;
}

@Override
public DialectType getDBType() {
return DialectType.OB_ORACLE;
}
}

0 comments on commit e510faf

Please sign in to comment.