Skip to content

Commit

Permalink
Better
Browse files Browse the repository at this point in the history
  • Loading branch information
ptiurin committed Oct 21, 2024
1 parent 78d6f30 commit 8fad16f
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 42 deletions.
19 changes: 15 additions & 4 deletions src/main/java/com/firebolt/jdbc/connection/FireboltConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@
import com.firebolt.jdbc.statement.FireboltStatement;
import com.firebolt.jdbc.statement.preparedstatement.FireboltPreparedStatement;
import com.firebolt.jdbc.type.FireboltDataType;
import com.firebolt.jdbc.type.ParserVersion;
import com.firebolt.jdbc.type.array.FireboltArray;
import com.firebolt.jdbc.type.lob.FireboltBlob;
import com.firebolt.jdbc.type.lob.FireboltClob;
import com.firebolt.jdbc.util.PropertyUtil;
import lombok.CustomLog;
import lombok.Getter;
import lombok.NonNull;
import okhttp3.OkHttpClient;

Expand Down Expand Up @@ -83,12 +85,16 @@ public abstract class FireboltConnection extends JdbcBase implements Connection,
//Properties that are used at the beginning of the connection for authentication
protected final FireboltProperties loginProperties;
private final Collection<CacheListener> cacheListeners = Collections.newSetFromMap(new IdentityHashMap<>());
// Parameter parser is determined by the version we're running on
@Getter
public final ParserVersion parserVersion;

protected FireboltConnection(@NonNull String url,
Properties connectionSettings,
FireboltAuthenticationService fireboltAuthenticationService,
FireboltStatementService fireboltStatementService,
String protocolVersion) {
String protocolVersion,
ParserVersion parserVersion) {
this.loginProperties = extractFireboltProperties(url, connectionSettings);

this.fireboltAuthenticationService = fireboltAuthenticationService;
Expand All @@ -99,11 +105,13 @@ protected FireboltConnection(@NonNull String url,
this.connectionTimeout = loginProperties.getConnectionTimeoutMillis();
this.networkTimeout = loginProperties.getSocketTimeoutMillis();
this.protocolVersion = protocolVersion;
this.parserVersion = parserVersion;
}

// This code duplication between constructors is done because of back reference: dependent services require reference to current instance of FireboltConnection that prevents using constructor chaining or factory method.
@ExcludeFromJacocoGeneratedReport
protected FireboltConnection(@NonNull String url, Properties connectionSettings, String protocolVersion) throws SQLException {
protected FireboltConnection(@NonNull String url, Properties connectionSettings, String protocolVersion,
ParserVersion parserVersion) throws SQLException {
this.loginProperties = extractFireboltProperties(url, connectionSettings);
OkHttpClient httpClient = getHttpClient(loginProperties);

Expand All @@ -115,6 +123,7 @@ protected FireboltConnection(@NonNull String url, Properties connectionSettings,
this.connectionTimeout = loginProperties.getConnectionTimeoutMillis();
this.networkTimeout = loginProperties.getSocketTimeoutMillis();
this.protocolVersion = protocolVersion;
this.parserVersion = parserVersion;
}

protected abstract FireboltAuthenticationClient createFireboltAuthenticationClient(OkHttpClient httpClient);
Expand All @@ -125,8 +134,10 @@ public static FireboltConnection create(@NonNull String url, Properties connecti

private static FireboltConnection createConnectionInstance(@NonNull String url, Properties connectionSettings) throws SQLException {
switch(getUrlVersion(url, connectionSettings)) {
case 1: return new FireboltConnectionUserPassword(url, connectionSettings);
case 2: return new FireboltConnectionServiceSecret(url, connectionSettings);
case 1:
return new FireboltConnectionUserPassword(url, connectionSettings, ParserVersion.LEGACY);
case 2:
return new FireboltConnectionServiceSecret(url, connectionSettings, ParserVersion.CURRENT);
default: throw new IllegalArgumentException(format("Cannot distinguish version from url %s", url));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.firebolt.jdbc.service.FireboltEngineVersion2Service;
import com.firebolt.jdbc.service.FireboltGatewayUrlService;
import com.firebolt.jdbc.service.FireboltStatementService;
import com.firebolt.jdbc.type.ParserVersion;
import com.firebolt.jdbc.util.PropertyUtil;
import lombok.NonNull;
import okhttp3.OkHttpClient;
Expand All @@ -42,16 +43,19 @@ public class FireboltConnectionServiceSecret extends FireboltConnection {
FireboltGatewayUrlService fireboltGatewayUrlService,
FireboltStatementService fireboltStatementService,
FireboltEngineInformationSchemaService fireboltEngineService,
FireboltAccountIdService fireboltAccountIdService) throws SQLException {
super(url, connectionSettings, fireboltAuthenticationService, fireboltStatementService, PROTOCOL_VERSION);
FireboltAccountIdService fireboltAccountIdService,
ParserVersion parserVersion) throws SQLException {
super(url, connectionSettings, fireboltAuthenticationService, fireboltStatementService, PROTOCOL_VERSION,
parserVersion);
this.fireboltGatewayUrlService = fireboltGatewayUrlService;
this.fireboltEngineService = fireboltEngineService;
connect();
}

@ExcludeFromJacocoGeneratedReport
FireboltConnectionServiceSecret(@NonNull String url, Properties connectionSettings) throws SQLException {
super(url, connectionSettings, PROTOCOL_VERSION);
FireboltConnectionServiceSecret(@NonNull String url, Properties connectionSettings, ParserVersion parserVersion)
throws SQLException {
super(url, connectionSettings, PROTOCOL_VERSION, parserVersion);
OkHttpClient httpClient = getHttpClient(loginProperties);
this.fireboltGatewayUrlService = new FireboltGatewayUrlService(createFireboltAccountRetriever(httpClient, GatewayUrlResponse.class));
// initialization of fireboltEngineService depends on the infraVersion (the version of engine)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.firebolt.jdbc.service.FireboltEngineInformationSchemaService;
import com.firebolt.jdbc.service.FireboltEngineService;
import com.firebolt.jdbc.service.FireboltStatementService;
import com.firebolt.jdbc.type.ParserVersion;
import lombok.NonNull;
import okhttp3.OkHttpClient;

Expand All @@ -27,15 +28,18 @@ public class FireboltConnectionUserPassword extends FireboltConnection {
Properties connectionSettings,
FireboltAuthenticationService fireboltAuthenticationService,
FireboltStatementService fireboltStatementService,
FireboltEngineInformationSchemaService fireboltEngineService) throws SQLException {
super(url, connectionSettings, fireboltAuthenticationService, fireboltStatementService, PROTOCOL_VERSION);
FireboltEngineInformationSchemaService fireboltEngineService,
ParserVersion parserVersion) throws SQLException {
super(url, connectionSettings, fireboltAuthenticationService, fireboltStatementService, PROTOCOL_VERSION,
parserVersion);
this.fireboltEngineService = fireboltEngineService;
connect();
}

@ExcludeFromJacocoGeneratedReport
FireboltConnectionUserPassword(@NonNull String url, Properties connectionSettings) throws SQLException {
super(url, connectionSettings, PROTOCOL_VERSION);
FireboltConnectionUserPassword(@NonNull String url, Properties connectionSettings, ParserVersion parserVersion)
throws SQLException {
super(url, connectionSettings, PROTOCOL_VERSION, parserVersion);
OkHttpClient httpClient = getHttpClient(loginProperties);
this.fireboltEngineService = new FireboltEngineApiService(new FireboltAccountClient(httpClient, this, loginProperties.getUserDrivers(), loginProperties.getUserClients()));
connect();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import com.firebolt.jdbc.statement.rawstatement.RawStatementWrapper;
import com.firebolt.jdbc.type.ParserVersion;
import com.firebolt.jdbc.type.JavaTypeToFireboltSQLString;
import com.firebolt.jdbc.util.ConnectionParserResolver;
import com.firebolt.jdbc.util.InputStreamUtil;
import lombok.CustomLog;
import lombok.NonNull;
Expand Down Expand Up @@ -73,7 +72,7 @@ public FireboltPreparedStatement(FireboltStatementService statementService, Fire
this.rawStatement = StatementUtil.parseToRawStatementWrapper(sql);
rawStatement.getSubStatements().forEach(statement -> createValidator(statement, connection).validate(statement));
this.rows = new ArrayList<>();
this.parserVersion = ConnectionParserResolver.getParserFromConnection(connection);
this.parserVersion = connection.getParserVersion();
}

@Override
Expand Down
25 changes: 0 additions & 25 deletions src/main/java/com/firebolt/jdbc/util/ConnectionParserResolver.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.firebolt.jdbc.connection.settings.FireboltProperties;
import com.firebolt.jdbc.exception.FireboltException;
import com.firebolt.jdbc.service.FireboltGatewayUrlService;
import com.firebolt.jdbc.type.ParserVersion;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
Expand Down Expand Up @@ -100,7 +101,9 @@ void checkSystemEngineEndpoint(String gatewayUrl, String expectedHost, String ex
@SuppressWarnings("unchecked") FireboltAccountRetriever<GatewayUrlResponse> fireboltGatewayUrlClient = mock(FireboltAccountRetriever.class);
when(fireboltGatewayUrlClient.retrieve(any(), any())).thenReturn(new GatewayUrlResponse(gatewayUrl));
FireboltGatewayUrlService gatewayUrlService = new FireboltGatewayUrlService(fireboltGatewayUrlClient);
FireboltConnection connection = new FireboltConnectionServiceSecret(SYSTEM_ENGINE_URL, connectionProperties, fireboltAuthenticationService, gatewayUrlService, fireboltStatementService, fireboltEngineService, fireboltAccountIdService);
FireboltConnection connection = new FireboltConnectionServiceSecret(SYSTEM_ENGINE_URL, connectionProperties,
fireboltAuthenticationService, gatewayUrlService, fireboltStatementService, fireboltEngineService,
fireboltAccountIdService, ParserVersion.CURRENT);
FireboltProperties sessionProperties = connection.getSessionProperties();
assertEquals(expectedHost, sessionProperties.getHost());
assertEquals(expectedProps == null ? Map.of() : Arrays.stream(expectedProps.split(";")).map(kv -> kv.split("=")).collect(toMap(kv -> kv[0], kv -> kv[1])), sessionProperties.getAdditionalProperties());
Expand All @@ -113,6 +116,7 @@ void shouldNotFetchTokenNorEngineHostForLocalFirebolt() throws SQLException {
}

protected FireboltConnection createConnection(String url, Properties props) throws SQLException {
return new FireboltConnectionServiceSecret(url, props, fireboltAuthenticationService, fireboltGatewayUrlService, fireboltStatementService, fireboltEngineService, fireboltAccountIdService);
return new FireboltConnectionServiceSecret(url, props, fireboltAuthenticationService, fireboltGatewayUrlService,
fireboltStatementService, fireboltEngineService, fireboltAccountIdService, ParserVersion.CURRENT);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.firebolt.jdbc.connection;

import com.firebolt.jdbc.type.ParserVersion;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
Expand Down Expand Up @@ -73,6 +74,7 @@ void getMetadata(String engine) throws SQLException {
}

protected FireboltConnection createConnection(String url, Properties props) throws SQLException {
return new FireboltConnectionUserPassword(url, props, fireboltAuthenticationService, fireboltStatementService, fireboltEngineService);
return new FireboltConnectionUserPassword(url, props, fireboltAuthenticationService, fireboltStatementService,
fireboltEngineService, ParserVersion.LEGACY);
}
}

0 comments on commit 8fad16f

Please sign in to comment.