Skip to content

Commit 5a4cba0

Browse files
authored
Support connecting to Firebird via jdbcUrl containing the absolute path to fdb (#34335)
1 parent e5af00a commit 5a4cba0

5 files changed

Lines changed: 50 additions & 9 deletions

File tree

RELEASE-NOTES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
1. Agent: Simplify the use of Agent's Docker Image - [#33356](https://github.com/apache/shardingsphere/pull/33356)
5656
1. Mode: Support modifying Hikari-CP configurations via props in standalone mode [#34185](https://github.com/apache/shardingsphere/pull/34185)
5757
1. Encrypt: Support insert statement rewrite use quote [#34259](https://github.com/apache/shardingsphere/pull/34259)
58+
1. Infra: Support connecting to Firebird via jdbcUrl containing the absolute path to fdb - [#34335](https://github.com/apache/shardingsphere/pull/34335)
5859

5960
### Bug Fixes
6061

infra/database/type/firebird/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@
3333
<version>${project.version}</version>
3434
</dependency>
3535

36+
<dependency>
37+
<groupId>org.firebirdsql.jdbc</groupId>
38+
<artifactId>jaybird</artifactId>
39+
<scope>provided</scope>
40+
<optional>true</optional>
41+
</dependency>
42+
3643
<dependency>
3744
<groupId>org.apache.shardingsphere</groupId>
3845
<artifactId>shardingsphere-test-util</artifactId>

infra/database/type/firebird/src/main/java/org/apache/shardingsphere/infra/database/firebird/connector/FirebirdConnectionPropertiesParser.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,14 @@
1717

1818
package org.apache.shardingsphere.infra.database.firebird.connector;
1919

20+
import lombok.SneakyThrows;
2021
import org.apache.shardingsphere.infra.database.core.connector.ConnectionProperties;
2122
import org.apache.shardingsphere.infra.database.core.connector.ConnectionPropertiesParser;
2223
import org.apache.shardingsphere.infra.database.core.connector.StandardConnectionProperties;
23-
import org.apache.shardingsphere.infra.database.core.connector.url.JdbcUrl;
24-
import org.apache.shardingsphere.infra.database.core.connector.url.StandardJdbcUrlParser;
24+
import org.firebirdsql.gds.impl.DbAttachInfo;
25+
import org.firebirdsql.gds.impl.GDSFactory;
26+
import org.firebirdsql.gds.impl.GDSType;
27+
import org.firebirdsql.jdbc.FBDriver;
2528

2629
import java.util.Properties;
2730

@@ -30,12 +33,18 @@
3033
*/
3134
public final class FirebirdConnectionPropertiesParser implements ConnectionPropertiesParser {
3235

33-
private static final int DEFAULT_PORT = 3050;
34-
36+
@SneakyThrows(Exception.class)
3537
@Override
3638
public ConnectionProperties parse(final String url, final String username, final String catalog) {
37-
JdbcUrl jdbcUrl = new StandardJdbcUrlParser().parse(url);
38-
return new StandardConnectionProperties(jdbcUrl.getHostname(), jdbcUrl.getPort(DEFAULT_PORT), jdbcUrl.getDatabase(), null, jdbcUrl.getQueryProperties(), new Properties());
39+
GDSType type = GDSFactory.getTypeForProtocol(url);
40+
String databaseURL = GDSFactory.getDatabasePath(type, url);
41+
DbAttachInfo dbAttachInfo = DbAttachInfo.parseConnectString(databaseURL);
42+
String attachObjectName = dbAttachInfo.getAttachObjectName();
43+
String databaseName = attachObjectName.contains("?") ? attachObjectName.split("\\?")[0] : attachObjectName;
44+
Properties queryProperties = new Properties();
45+
queryProperties.putAll(FBDriver.normalizeProperties(url, new Properties()));
46+
return new StandardConnectionProperties(dbAttachInfo.getServerName(), dbAttachInfo.getPortNumber(),
47+
databaseName, null, queryProperties, new Properties());
3948
}
4049

4150
@Override

infra/database/type/firebird/src/test/java/org/apache/shardingsphere/infra/database/firebird/connector/FirebirdConnectionPropertiesParserTest.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,24 @@
1919

2020
import org.apache.shardingsphere.infra.database.core.connector.ConnectionProperties;
2121
import org.apache.shardingsphere.infra.database.core.connector.ConnectionPropertiesParser;
22-
import org.apache.shardingsphere.infra.database.core.exception.UnrecognizedDatabaseURLException;
2322
import org.apache.shardingsphere.infra.database.core.spi.DatabaseTypedSPILoader;
2423
import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
2524
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
25+
import org.apache.shardingsphere.test.util.PropertiesBuilder;
2626
import org.junit.jupiter.api.Test;
2727
import org.junit.jupiter.api.extension.ExtensionContext;
2828
import org.junit.jupiter.params.ParameterizedTest;
2929
import org.junit.jupiter.params.provider.Arguments;
3030
import org.junit.jupiter.params.provider.ArgumentsProvider;
3131
import org.junit.jupiter.params.provider.ArgumentsSource;
3232

33+
import java.sql.SQLNonTransientConnectionException;
3334
import java.util.Properties;
3435
import java.util.stream.Stream;
3536

3637
import static org.hamcrest.CoreMatchers.is;
3738
import static org.hamcrest.MatcherAssert.assertThat;
39+
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
3840
import static org.junit.jupiter.api.Assertions.assertThrows;
3941

4042
class FirebirdConnectionPropertiesParserTest {
@@ -54,14 +56,29 @@ void assertNewConstructor(final String name, final String url, final String host
5456

5557
@Test
5658
void assertNewConstructorFailure() {
57-
assertThrows(UnrecognizedDatabaseURLException.class, () -> parser.parse("jdbc:firebirdsql:xxxxxxxx", null, null));
59+
assertDoesNotThrow(() -> parser.parse("jdbc:firebirdsql:xxxxxxxx", null, null));
60+
assertThrows(SQLNonTransientConnectionException.class, () -> parser.parse("jdbc:firebirdsql://localhost:c:/data/db/test.fdb", null, null));
5861
}
5962

6063
private static class NewConstructorTestCaseArgumentsProvider implements ArgumentsProvider {
6164

6265
@Override
6366
public Stream<? extends Arguments> provideArguments(final ExtensionContext extensionContext) {
64-
return Stream.of(Arguments.of("simple", "jdbc:firebirdsql://127.0.0.1/foo_ds", "127.0.0.1", 3050, "foo_ds", null, new Properties()));
67+
return Stream.of(
68+
Arguments.of("simple_first", "jdbc:firebirdsql://127.0.0.1/foo_ds", "127.0.0.1", 3050, "foo_ds", null, new Properties()),
69+
Arguments.of("simple_second", "jdbc:firebird://localhost:32783//var/lib/firebird/data/demo_ds_2.fdb",
70+
"localhost", 32783, "/var/lib/firebird/data/demo_ds_2.fdb", null, new Properties()),
71+
Arguments.of("simple_third", "jdbc:firebirdsql://localhost/database?socket_buffer_size=32767", "localhost", 3050, "database", null, PropertiesBuilder.build(
72+
new PropertiesBuilder.Property("socketBufferSize", "32767"))),
73+
Arguments.of("complex",
74+
"jdbc:firebirdsql://localhost/database?socket_buffer_size=32767"
75+
+ "&TRANSACTION_REPEATABLE_READ=concurrency,write,no_wait&columnLabelForName&soTimeout=1000&nonStandard2=value2",
76+
"localhost", 3050, "database", null, PropertiesBuilder.build(
77+
new PropertiesBuilder.Property("socketBufferSize", "32767"),
78+
new PropertiesBuilder.Property("TRANSACTION_REPEATABLE_READ", "concurrency,write,no_wait"),
79+
new PropertiesBuilder.Property("columnLabelForName", ""),
80+
new PropertiesBuilder.Property("soTimeout", "1000"),
81+
new PropertiesBuilder.Property("nonStandard2", "value2"))));
6582
}
6683
}
6784
}

pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@
132132
<hive-server2-jdbc-driver-thin.version>1.6.0</hive-server2-jdbc-driver-thin.version>
133133
<hadoop.version>3.3.6</hadoop.version>
134134
<presto.version>0.288.1</presto.version>
135+
<jaybird.version>5.0.6.java8</jaybird.version>
135136

136137
<hikari-cp.version>4.0.3</hikari-cp.version>
137138

@@ -489,6 +490,12 @@
489490
<version>${presto.version}</version>
490491
<scope>test</scope>
491492
</dependency>
493+
<dependency>
494+
<groupId>org.firebirdsql.jdbc</groupId>
495+
<artifactId>jaybird</artifactId>
496+
<version>${jaybird.version}</version>
497+
<scope>test</scope>
498+
</dependency>
492499

493500
<dependency>
494501
<groupId>com.zaxxer</groupId>

0 commit comments

Comments
 (0)