Skip to content

Commit 070d72a

Browse files
baslo2AXEPOH
authored andcommitted
[DBTOOLS-1778] Added interface IJdbcConnector
1 parent 8341b36 commit 070d72a

23 files changed

+148
-112
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
99

1010
### Added
1111

12-
- Added URL validation.
12+
- Added connection string validation.
13+
- Added a common interface for JDBC connections.
1314

1415
### Changed
1516

1617
- Improved parser rules for ClickHouse.
1718
- Improved migration script generation for tables and indexes with options in MS SQL.
1819
- Added the IS JSON parser rule for PostgreSQL.
20+
- Improved validation of arrays obtained from metadata queries.
1921

2022
### Fixed
2123

CHANGELOG.ru.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@
99

1010
### Добавлено
1111

12-
- Добавлена валидация URL.
12+
- Добавлена валидация строки подключения.
13+
- Добавлен общий интерфейс для JDBC-подключений.
1314

1415
### Изменено
1516

1617
- Улучшены правила парсера для ClickHouse.
1718
- Улучшена генерация скрипта миграции для таблиц и индексов с опциями в MS SQL.
1819
- Добавлено правило парсера IS JSON для PostgreSQL.
20+
- Улучшена проверка массивов, получаемых из запросов метаданных.
1921

2022
### Исправлено
2123

src/main/java/org/pgcodekeeper/core/DatabaseType.java

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,26 +29,20 @@
2929
*/
3030
public enum DatabaseType {
3131

32-
PG("PostgreSQL", "5432"),
33-
MS("MS SQL", "1433"),
34-
CH("ClickHouse", "8123");
32+
PG("PostgreSQL"),
33+
MS("MS SQL"),
34+
CH("ClickHouse");
3535

3636
private final String dbTypeName;
37-
private final String defaultPort;
3837

39-
DatabaseType(String dbTypeName, String defaultPort) {
38+
DatabaseType(String dbTypeName) {
4039
this.dbTypeName = dbTypeName;
41-
this.defaultPort = defaultPort;
4240
}
4341

4442
public final String getDbTypeName() {
4543
return dbTypeName;
4644
}
4745

48-
public String getDefaultPort() {
49-
return defaultPort;
50-
}
51-
5246
/**
5347
* Enums valuesOf method version with custom exception.
5448
*

src/main/java/org/pgcodekeeper/core/Utils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*******************************************************************************/
1616
package org.pgcodekeeper.core;
1717

18-
import org.pgcodekeeper.core.database.base.jdbc.AbstractJdbcConnector;
18+
import org.pgcodekeeper.core.database.base.jdbc.IJdbcConnector;
1919
import org.pgcodekeeper.core.database.ch.jdbc.ChJdbcConnector;
2020
import org.pgcodekeeper.core.database.ms.jdbc.MsJdbcConnector;
2121
import org.pgcodekeeper.core.database.pg.jdbc.PgJdbcConnector;
@@ -194,7 +194,7 @@ public static UnaryOperator<String> getQuoter(DatabaseType dbType) {
194194
* @param url full jdbc connection string
195195
* @return JdbcConnector for the given database type.
196196
*/
197-
public static AbstractJdbcConnector getJdbcConnectorByType(DatabaseType dbType, String url) {
197+
public static IJdbcConnector getJdbcConnectorByType(DatabaseType dbType, String url) {
198198
return switch (dbType) {
199199
case PG -> new PgJdbcConnector(url);
200200
case MS -> new MsJdbcConnector(url);

src/main/java/org/pgcodekeeper/core/database/base/IDatabaseProvider.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
package org.pgcodekeeper.core.database.base;
1717

1818
import org.antlr.v4.runtime.*;
19-
import org.pgcodekeeper.core.database.base.jdbc.AbstractJdbcConnector;
19+
import org.pgcodekeeper.core.database.base.jdbc.IJdbcConnector;
2020

2121
/**
2222
* Interface for DBMS
@@ -28,11 +28,6 @@ public interface IDatabaseProvider {
2828
*/
2929
String getDatabaseType();
3030

31-
/**
32-
* @return default port for DBMS
33-
*/
34-
String getDefaultPort();
35-
3631
/**
3732
* @param stream - char stream
3833
* @return antlr lexer object for DBMS
@@ -62,5 +57,5 @@ public interface IDatabaseProvider {
6257
* @param url full jdbc url
6358
* @return jdbc connector for DBMS
6459
*/
65-
AbstractJdbcConnector getJdbcConnector(String url);
60+
IJdbcConnector getJdbcConnector(String url);
6661
}

src/main/java/org/pgcodekeeper/core/database/base/jdbc/AbstractJdbcConnector.java

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,18 @@
2828
* Abstract base class for JDBC database connectors.
2929
* Provides common functionality for establishing database connections.
3030
*/
31-
public abstract class AbstractJdbcConnector {
31+
public abstract class AbstractJdbcConnector implements IJdbcConnector {
32+
33+
private final String url;
3234

3335
/**
34-
* Creates a new database connection using the parameters specified in the constructor.
35-
* The caller is responsible for closing the connection.
36-
*
37-
* @return new database connection
38-
* @throws IOException if the driver is not found or a database access error occurs
36+
* @param url jdbc connection string
3937
*/
38+
protected AbstractJdbcConnector(String url) {
39+
this.url = url;
40+
}
41+
42+
@Override
4043
public Connection getConnection() throws IOException {
4144
try {
4245
loadDriver();
@@ -55,10 +58,10 @@ protected void loadDriver() throws ClassNotFoundException {
5558
Class.forName(getDriverName());
5659
}
5760

58-
/**
59-
* @return full connection string
60-
*/
61-
protected abstract String getUrl();
61+
@Override
62+
public String getUrl() {
63+
return url;
64+
}
6265

6366
/**
6467
* @return connection properties
@@ -75,16 +78,17 @@ protected Properties makeProperties() {
7578
protected abstract String getDriverName();
7679

7780
/**
78-
* Returns batch delimiter. If the value is null, each statement will be executed separately in autocommit mode.
79-
*
80-
* @return batch delimiter
81+
* @return default port
8182
*/
82-
public String getBatchDelimiter() {
83-
return null;
84-
}
85-
86-
protected abstract String getDefaultPort();
83+
protected abstract int getDefaultPort();
8784

85+
/**
86+
* Validates connection string
87+
*
88+
* @param url connection string
89+
* @param allowedPrefixes allowed prefixes
90+
* @throws IllegalArgumentException if the string does not start with the allowed prefixes
91+
*/
8892
protected void validateUrl(String url, String... allowedPrefixes) {
8993
for (var prefix : allowedPrefixes) {
9094
if (url.startsWith(prefix)) {
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*******************************************************************************
2+
* Copyright 2017-2025 TAXTELECOM, LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*******************************************************************************/
16+
package org.pgcodekeeper.core.database.base.jdbc;
17+
18+
import java.io.IOException;
19+
import java.sql.Connection;
20+
21+
public interface IJdbcConnector {
22+
23+
/**
24+
* Creates a new database connection using the parameters specified in the constructor.
25+
* The caller is responsible for closing the connection.
26+
*
27+
* @return new database connection
28+
* @throws IOException if the driver is not found or a database access error occurs
29+
*/
30+
Connection getConnection() throws IOException;
31+
32+
/**
33+
* Returns batch delimiter. If the value is null, each statement will be executed separately in autocommit mode.
34+
*
35+
* @return batch delimiter
36+
*/
37+
String getBatchDelimiter();
38+
39+
/**
40+
* @return connection string
41+
*/
42+
String getUrl();
43+
}

src/main/java/org/pgcodekeeper/core/database/ch/ChDatabaseProvider.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import org.antlr.v4.runtime.*;
1919
import org.pgcodekeeper.core.Consts;
2020
import org.pgcodekeeper.core.database.base.IDatabaseProvider;
21-
import org.pgcodekeeper.core.database.base.jdbc.AbstractJdbcConnector;
21+
import org.pgcodekeeper.core.database.base.jdbc.IJdbcConnector;
2222
import org.pgcodekeeper.core.database.ch.jdbc.ChJdbcConnector;
2323
import org.pgcodekeeper.core.parsers.antlr.ch.CustomChSQLAntlrErrorStrategy;
2424
import org.pgcodekeeper.core.parsers.antlr.ch.generated.CHLexer;
@@ -31,11 +31,6 @@ public String getDatabaseType() {
3131
return "ClickHouse";
3232
}
3333

34-
@Override
35-
public String getDefaultPort() {
36-
return "8123";
37-
}
38-
3934
@Override
4035
public Lexer getLexer(CharStream stream) {
4136
return new CHLexer(stream);
@@ -58,7 +53,7 @@ public boolean isSystemSchema(String schema) {
5853
}
5954

6055
@Override
61-
public AbstractJdbcConnector getJdbcConnector(String url) {
56+
public IJdbcConnector getJdbcConnector(String url) {
6257
return new ChJdbcConnector(url);
6358
}
6459
}

src/main/java/org/pgcodekeeper/core/database/ch/jdbc/ChJdbcConnector.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,21 @@ public class ChJdbcConnector extends AbstractJdbcConnector {
2424

2525
private static final String DRIVER_NAME = "com.clickhouse.jdbc.ClickHouseDriver";
2626

27-
protected static final String URL_START_CH = "jdbc:clickhouse:";
27+
private static final String URL_START_CH = "jdbc:clickhouse:";
2828
private static final String URL_START_CH_SHORT = "jdbc:ch:";
2929

30-
private final String url;
30+
private static final int DEFAULT_PORT = 8123;
3131

32+
/**
33+
* @param url full jdbc connection string
34+
*/
3235
public ChJdbcConnector(String url) {
36+
super(url);
3337
validateUrl(url, URL_START_CH, URL_START_CH_SHORT);
34-
this.url = url;
3538
}
3639

37-
@Override
38-
protected String getUrl() {
39-
return url;
40+
public ChJdbcConnector(String host, int port, String dbName) {
41+
super(URL_START_CH + "//" + host + ':' + (port > 0 ? port : DEFAULT_PORT) + (dbName == null ? "" : "/" + dbName));
4042
}
4143

4244
@Override
@@ -45,7 +47,12 @@ protected String getDriverName() {
4547
}
4648

4749
@Override
48-
protected String getDefaultPort() {
49-
return "8123";
50+
protected int getDefaultPort() {
51+
return DEFAULT_PORT;
52+
}
53+
54+
@Override
55+
public String getBatchDelimiter() {
56+
return null;
5057
}
5158
}

src/main/java/org/pgcodekeeper/core/database/ms/MsDatabaseProvider.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import org.antlr.v4.runtime.*;
1919
import org.pgcodekeeper.core.Consts;
2020
import org.pgcodekeeper.core.database.base.IDatabaseProvider;
21-
import org.pgcodekeeper.core.database.base.jdbc.AbstractJdbcConnector;
21+
import org.pgcodekeeper.core.database.base.jdbc.IJdbcConnector;
2222
import org.pgcodekeeper.core.database.ms.jdbc.MsJdbcConnector;
2323
import org.pgcodekeeper.core.parsers.antlr.ms.CustomTSQLAntlrErrorStrategy;
2424
import org.pgcodekeeper.core.parsers.antlr.ms.generated.TSQLLexer;
@@ -31,11 +31,6 @@ public String getDatabaseType() {
3131
return "MS SQL";
3232
}
3333

34-
@Override
35-
public String getDefaultPort() {
36-
return "1433";
37-
}
38-
3934
@Override
4035
public Lexer getLexer(CharStream stream) {
4136
return new TSQLLexer(stream);
@@ -57,7 +52,7 @@ public boolean isSystemSchema(String schema) {
5752
}
5853

5954
@Override
60-
public AbstractJdbcConnector getJdbcConnector(String url) {
55+
public IJdbcConnector getJdbcConnector(String url) {
6156
return new MsJdbcConnector(url);
6257
}
6358
}

0 commit comments

Comments
 (0)