feat(ignite3): add Apache Ignite 3 support

This commit is contained in:
SCSTC authored and GitHub committed 2026-08-20 02:21:55 +08:00
1 parent 34c8afeb58
commit 634bb9a7d0
32 files changed
+555 -6

No files matched your search

@@ -0,0 +1,189 @@
package com.dbx.agent.ignite3;
import com.dbx.agent.AbstractJdbcAgent;
import com.dbx.agent.ColumnInfo;
import com.dbx.agent.ConnectParams;
import com.dbx.agent.DatabaseInfo;
import com.dbx.agent.ForeignKeyInfo;
import com.dbx.agent.IndexInfo;
import com.dbx.agent.JdbcAgentProfile;
import com.dbx.agent.MultiSessionJsonRpcServer;
import com.dbx.agent.TableInfo;
import com.dbx.agent.TriggerInfo;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Properties;
import java.util.Set;
public final class Ignite3Agent extends AbstractJdbcAgent {
private static final String JDBC_USERNAME_PROPERTY = "ignite.jdbc.username";
private static final String JDBC_PASSWORD_PROPERTY = "ignite.jdbc.password";
private String databaseName = "";
@Override
protected String driverClass() {
return "org.apache.ignite.jdbc.IgniteJdbcDriver";
}
@Override
protected String buildJdbcUrl(ConnectParams params) {
return buildUrl(params);
}
@Override
protected Properties buildConnectionProperties(ConnectParams params) {
Properties properties = new Properties();
String username = params.getUsername();
String password = params.getPassword();
if ((username != null && !username.isEmpty()) || (password != null && !password.isEmpty())) {
properties.setProperty(JDBC_USERNAME_PROPERTY, username == null ? "" : username);
properties.setProperty(JDBC_PASSWORD_PROPERTY, password == null ? "" : password);
}
return properties;
}
@Override
protected void afterConnect(ConnectParams params, Connection connection) {
// Ignite has no catalogs; name the virtual database after the connection's
// real default schema (e.g. PUBLIC) so any database-as-schema context that
// leaks into setSchema targets a schema that actually exists.
try {
String schema = connection.getSchema();
databaseName = schema == null || schema.isBlank() ? params.getDatabase() : schema;
} catch (Exception e) {
databaseName = params.getDatabase();
}
}
@Override
public List<DatabaseInfo> listDatabases() {
// Ignite has no catalogs; expose the connected schema group as a single
// virtual database so the object browser stays schema-oriented like H2.
return List.of(new DatabaseInfo(databaseName.isBlank() ? "default" : databaseName));
}
@Override
public List<String> listSchemas() {
return unchecked(() -> {
List<String> result = new ArrayList<>();
try (ResultSet rs = requireConnected().getMetaData().getSchemas()) {
while (rs.next()) {
result.add(rs.getString("TABLE_SCHEM"));
}
}
result.sort(Comparator.naturalOrder());
return result;
});
}
@Override
public List<TableInfo> listTables(String schema) {
return unchecked(() -> {
List<TableInfo> result = new ArrayList<>();
try (ResultSet rs = requireConnected().getMetaData().getTables(null, schema, null, null)) {
while (rs.next()) {
result.add(new TableInfo(
rs.getString("TABLE_NAME"),
normalizeTableType(rs.getString("TABLE_TYPE")),
null
));
}
}
result.sort(Comparator.comparing(TableInfo::getName));
return result;
});
}
@Override
public List<ColumnInfo> getColumns(String schema, String table) {
return unchecked(() -> {
Set<String> primaryKeys = new LinkedHashSet<>();
try (ResultSet rs = requireConnected().getMetaData().getPrimaryKeys(null, schema, table)) {
while (rs.next()) {
primaryKeys.add(rs.getString("COLUMN_NAME"));
}
}
List<ColumnInfo> result = new ArrayList<>();
try (ResultSet rs = requireConnected().getMetaData().getColumns(null, schema, table, null)) {
while (rs.next()) {
String colName = rs.getString("COLUMN_NAME");
result.add(new ColumnInfo(
colName,
rs.getString("TYPE_NAME"),
"YES".equals(rs.getString("IS_NULLABLE")),
rs.getString("COLUMN_DEF"),
primaryKeys.contains(colName),
null,
emptyToNull(rs.getString("REMARKS")),
intOrNull(rs, "COLUMN_SIZE"),
intOrNull(rs, "DECIMAL_DIGITS"),
null
));
}
}
return result;
});
}
@Override
public List<IndexInfo> listIndexes(String schema, String table) {
return Collections.emptyList();
}
@Override
public List<ForeignKeyInfo> listForeignKeys(String schema, String table) {
return Collections.emptyList();
}
@Override
public List<TriggerInfo> listTriggers(String schema, String table) {
return Collections.emptyList();
}
@Override
public String setSchemaSQL(String schema) {
// JdbcConnection implements Connection.setSchema, so no SQL switch is needed.
return "";
}
static String buildUrl(ConnectParams params) {
String connectionString = params.getConnection_string();
if (connectionString != null && !connectionString.trim().isEmpty()) {
return connectionString.trim();
}
String base = "jdbc:ignite:thin://" + params.getHost() + ":" + params.getPort();
String database = params.getDatabase();
String url = database == null || database.isBlank() ? base : base + "/" + database;
// Reuse the shared JDBC URL parameter mapping so advanced options
// (timeouts, SSL, authentication) configured in the UI reach the driver.
return JdbcAgentProfile.appendUrlParams(url, params.getUrl_params());
}
private static String normalizeTableType(String type) {
if ("BASE TABLE".equals(type)) {
return "TABLE";
}
return type;
}
private static Integer intOrNull(ResultSet rs, String column) throws Exception {
Object value = rs.getObject(column);
return value instanceof Number ? ((Number) value).intValue() : null;
}
private static String emptyToNull(String value) {
return value == null || value.isEmpty() ? null : value;
}
public static void main(String[] args) {
new MultiSessionJsonRpcServer(Ignite3Agent::new).run();
}
}
@@ -0,0 +1,16 @@
package com.dbx.agent.ignite3;
import com.dbx.agent.DatabaseAgent;
import com.dbx.agent.test.JdbcFakeExecutionBehaviorTest;
class Ignite3AgentTest extends JdbcFakeExecutionBehaviorTest {
@Override
protected DatabaseAgent createAgent() {
return new Ignite3Agent();
}
@Override
protected String resultSetSql() {
return "SELECT 1";
}
}
@@ -0,0 +1,245 @@
package com.dbx.agent.ignite3;
import com.dbx.agent.ConnectParams;
import java.lang.reflect.Proxy;
import java.nio.file.Path;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.DriverPropertyInfo;
import java.sql.SQLException;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Logger;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
class Ignite3AgentUrlTest {
@Test
void buildUrlUsesExplicitConnectionString() {
ConnectParams params = new ConnectParams(
"127.0.0.1", 10800, "PUBLIC", "", "", "",
"jdbc:ignite:thin://10.0.0.1:10800/PUBLIC?connectionTimeout=5000", false
);
Assertions.assertEquals(
"jdbc:ignite:thin://10.0.0.1:10800/PUBLIC?connectionTimeout=5000",
Ignite3Agent.buildUrl(params)
);
}
@Test
void buildUrlWithoutDatabaseOmitsSchemaSegment() {
ConnectParams params = new ConnectParams("127.0.0.1", 10800, "", "", "", "", "", false);
Assertions.assertEquals("jdbc:ignite:thin://127.0.0.1:10800", Ignite3Agent.buildUrl(params));
}
@Test
void buildUrlWithoutUrlParamsStaysUnchanged() {
ConnectParams params = new ConnectParams("127.0.0.1", 10800, "PUBLIC", "", "", null, "", false);
Assertions.assertEquals("jdbc:ignite:thin://127.0.0.1:10800/PUBLIC", Ignite3Agent.buildUrl(params));
}
@Test
void buildUrlAppendsAdvancedUrlParamsAfterSchema() {
ConnectParams params = new ConnectParams(
"127.0.0.1", 10800, "PUBLIC", "", "",
"connectionTimeout=5000&queryTimeout=30", "", false
);
Assertions.assertEquals(
"jdbc:ignite:thin://127.0.0.1:10800/PUBLIC?connectionTimeout=5000&queryTimeout=30",
Ignite3Agent.buildUrl(params)
);
}
@Test
void buildUrlPassesThroughUserParamStringVerbatim() {
ConnectParams params = new ConnectParams(
"127.0.0.1", 10800, "PUBLIC", "", "",
"sslEnabled=true;trustStorePath=/etc/ignite/trust.jks", "", false
);
Assertions.assertEquals(
"jdbc:ignite:thin://127.0.0.1:10800/PUBLIC?sslEnabled=true;trustStorePath=/etc/ignite/trust.jks",
Ignite3Agent.buildUrl(params)
);
}
@Test
void buildUrlStripsLeadingParamSeparators() {
ConnectParams params = new ConnectParams("127.0.0.1", 10800, "", "", "", "?sslEnabled=true", "", false);
Assertions.assertEquals("jdbc:ignite:thin://127.0.0.1:10800?sslEnabled=true", Ignite3Agent.buildUrl(params));
}
@Test
void usesThePinnedIgniteJdbcDependency() throws Exception {
Path driverJar = Path.of(
org.apache.ignite.jdbc.IgniteJdbcDriver.class.getProtectionDomain().getCodeSource().getLocation().toURI()
);
Assertions.assertEquals("ignite-jdbc-3.1.0.jar", driverJar.getFileName().toString());
}
@Test
void separateCredentialsRoundTripThroughJdbcPropertiesAndIgniteParser() throws Exception {
String username = "dbx-user";
String password = "dbx-secret";
String urlParams = "sslEnabled=true&connectionTimeout=5000&queryTimeout=30&connectionTimeZone=UTC";
CapturingDriver capturingDriver = new CapturingDriver();
DriverManager.registerDriver(capturingDriver);
try {
ConnectParams captureParams = new ConnectParams(
"127.0.0.1", 10800, "PUBLIC", username, password, urlParams,
CapturingDriver.URL, false
);
Properties connectionProperties = new Ignite3Agent().buildConnectionProperties(captureParams);
try (Connection ignored = DriverManager.getConnection(CapturingDriver.URL, connectionProperties)) {
Assertions.assertEquals(CapturingDriver.URL, capturingDriver.url);
}
Assertions.assertEquals(username, capturingDriver.properties.getProperty("ignite.jdbc.username"));
Assertions.assertEquals(password, capturingDriver.properties.getProperty("ignite.jdbc.password"));
Assertions.assertNull(capturingDriver.properties.getProperty("user"));
Assertions.assertNull(capturingDriver.properties.getProperty("password"));
ConnectParams igniteParams = new ConnectParams(
"127.0.0.1", 10800, "PUBLIC", username, password, urlParams, "", false
);
String igniteUrl = Ignite3Agent.buildUrl(igniteParams);
Assertions.assertFalse(igniteUrl.contains(username));
Assertions.assertFalse(igniteUrl.contains(password));
Map<String, String> parsed = parsedProperties(
new org.apache.ignite.jdbc.IgniteJdbcDriver(),
igniteUrl,
capturingDriver.properties
);
Assertions.assertEquals(username, parsed.get("ignite.jdbc.username"));
Assertions.assertEquals(password, parsed.get("ignite.jdbc.password"));
Assertions.assertEquals("true", parsed.get("ignite.jdbc.sslEnabled"));
Assertions.assertEquals("5000", parsed.get("ignite.jdbc.connectionTimeout"));
Assertions.assertEquals("30", parsed.get("ignite.jdbc.queryTimeout"));
Assertions.assertEquals("UTC", parsed.get("ignite.jdbc.connectionTimeZone"));
} finally {
DriverManager.deregisterDriver(capturingDriver);
}
}
@Test
void blankCredentialsDoNotEnableIgniteAuthentication() throws Exception {
CapturingDriver capturingDriver = new CapturingDriver();
DriverManager.registerDriver(capturingDriver);
try {
ConnectParams params = new ConnectParams(
"127.0.0.1", 10800, "PUBLIC", "", "", "", CapturingDriver.URL, false
);
Properties connectionProperties = new Ignite3Agent().buildConnectionProperties(params);
try (Connection ignored = DriverManager.getConnection(CapturingDriver.URL, connectionProperties)) {
Assertions.assertTrue(capturingDriver.properties.isEmpty());
}
} finally {
DriverManager.deregisterDriver(capturingDriver);
}
}
@Test
void semicolonAdvancedParamsStayVerbatimAndAreNotSplitInQueryMode() throws Exception {
String url = Ignite3Agent.buildUrl(new ConnectParams(
"127.0.0.1", 10800, "PUBLIC", "", "",
"sslEnabled=true;trustStorePath=/etc/ignite/trust.jks", "", false
));
Assertions.assertEquals(
"jdbc:ignite:thin://127.0.0.1:10800/PUBLIC?sslEnabled=true;trustStorePath=/etc/ignite/trust.jks",
url
);
Map<String, String> parsed = parsedProperties(
new org.apache.ignite.jdbc.IgniteJdbcDriver(),
url,
new Properties()
);
Assertions.assertEquals("false", parsed.get("ignite.jdbc.sslEnabled"));
Assertions.assertNull(parsed.get("ignite.jdbc.trustStorePath"));
}
private static Map<String, String> parsedProperties(
Driver driver,
String url,
Properties properties
) throws SQLException {
Map<String, String> parsed = new LinkedHashMap<>();
for (DriverPropertyInfo property : driver.getPropertyInfo(url, properties)) {
parsed.put(property.name, property.value);
}
return parsed;
}
private static final class CapturingDriver implements Driver {
private static final String URL = "jdbc:dbx-ignite3-test:";
private String url;
private Properties properties;
@Override
public Connection connect(String url, Properties info) {
if (!acceptsURL(url)) {
return null;
}
this.url = url;
properties = new Properties();
properties.putAll(info);
return (Connection) Proxy.newProxyInstance(
Connection.class.getClassLoader(),
new Class<?>[]{Connection.class},
(proxy, method, args) -> {
if ("close".equals(method.getName())) {
return null;
}
if ("isClosed".equals(method.getName())) {
return false;
}
throw new UnsupportedOperationException(method.getName());
}
);
}
@Override
public boolean acceptsURL(String url) {
return URL.equals(url);
}
@Override
public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) {
return new DriverPropertyInfo[0];
}
@Override
public int getMajorVersion() {
return 1;
}
@Override
public int getMinorVersion() {
return 0;
}
@Override
public boolean jdbcCompliant() {
return false;
}
@Override
public Logger getParentLogger() {
return Logger.getGlobal();
}
}
}