Skip to content
This repository was archived by the owner on Mar 23, 2026. It is now read-only.

Commit 6bab803

Browse files
committed
chore: move remaining logic to datasource
1 parent 787599c commit 6bab803

7 files changed

Lines changed: 60 additions & 119 deletions

File tree

google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryConnection.java

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
import com.google.cloud.bigquery.storage.v1.BigQueryWriteClient;
4141
import com.google.cloud.bigquery.storage.v1.BigQueryWriteSettings;
4242
import com.google.cloud.http.HttpTransportOptions;
43-
import com.google.common.base.Splitter;
4443
import java.io.IOException;
4544
import java.io.InputStream;
4645
import java.sql.CallableStatement;
@@ -53,7 +52,6 @@
5352
import java.sql.Statement;
5453
import java.time.Duration;
5554
import java.util.ArrayList;
56-
import java.util.Collections;
5755
import java.util.ConcurrentModificationException;
5856
import java.util.List;
5957
import java.util.Map;
@@ -127,7 +125,7 @@ public class BigQueryConnection extends BigQueryNoOpsConnection {
127125
int writeAPIActivationRowCount;
128126
int writeAPIAppendRowCount;
129127
int requestGoogleDriveScope;
130-
List<String> additionalProjects;
128+
String additionalProjects;
131129
boolean filterTablesOnDefaultDataset;
132130
String sslTrustStorePath;
133131
String sslTrustStorePassword;
@@ -162,9 +160,7 @@ public class BigQueryConnection extends BigQueryNoOpsConnection {
162160
this.catalog = ds.getProjectId();
163161
this.universeDomain = ds.getUniverseDomain();
164162

165-
this.overrideProperties =
166-
BigQueryJdbcUrlUtility.parseOverrideProperties(
167-
this.connectionUrl, this.connectionClassName);
163+
this.overrideProperties = ds.getOverrideProperties();
168164
if (this.universeDomain != null) {
169165
this.overrideProperties.put(
170166
BigQueryJdbcUrlUtility.UNIVERSE_DOMAIN_OVERRIDE_PROPERTY_NAME, this.universeDomain);
@@ -192,18 +188,7 @@ public class BigQueryConnection extends BigQueryNoOpsConnection {
192188
this.highThroughputMinTableSize = ds.getHighThroughputMinTableSize();
193189
this.highThroughputActivationRatio = ds.getHighThroughputActivationRatio();
194190
this.useQueryCache = ds.getUseQueryCache();
195-
Integer jobCreationMode = ds.getJobCreationMode();
196-
if (jobCreationMode == 2) {
197-
this.useStatelessQueryMode = true;
198-
} else if (jobCreationMode == 1) {
199-
this.useStatelessQueryMode = false;
200-
} else {
201-
throw new NumberFormatException(
202-
String.format(
203-
"Invalid value for %s. Use 1 for JOB_CREATION_REQUIRED and 2 for"
204-
+ " JOB_CREATION_OPTIONAL.",
205-
BigQueryJdbcUrlUtility.JOB_CREATION_MODE_PROPERTY_NAME));
206-
}
191+
this.useStatelessQueryMode = ds.getUseStatelessQueryMode();
207192

208193
this.queryDialect = ds.getQueryDialect();
209194
this.allowLargeResults = ds.getAllowLargeResults();
@@ -243,11 +228,7 @@ public class BigQueryConnection extends BigQueryNoOpsConnection {
243228
this.writeAPIActivationRowCount = ds.getSwaActivationRowCount();
244229
this.writeAPIAppendRowCount = ds.getSwaAppendRowCount();
245230

246-
String additionalProjectsStr = ds.getAdditionalProjects();
247-
this.additionalProjects =
248-
(additionalProjectsStr == null || additionalProjectsStr.isEmpty())
249-
? Collections.emptyList()
250-
: Splitter.on(',').trimResults().omitEmptyStrings().splitToList(additionalProjectsStr);
231+
this.additionalProjects = ds.getAdditionalProjects();
251232

252233
this.filterTablesOnDefaultDataset = ds.getFilterTablesOnDefaultDataset();
253234
this.requestGoogleDriveScope = ds.getRequestGoogleDriveScope();
@@ -597,7 +578,7 @@ int getHighThroughputMinTableSize() {
597578
return highThroughputMinTableSize;
598579
}
599580

600-
List<String> getAdditionalProjects() {
581+
String getAdditionalProjects() {
601582
return this.additionalProjects;
602583
}
603584

google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryDatabaseMetaData.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5208,8 +5208,13 @@ private List<String> getAccessibleCatalogNames() {
52085208
accessibleCatalogs.add(primaryCatalog);
52095209
}
52105210

5211-
List<String> additionalProjects = this.connection.getAdditionalProjects();
5212-
if (additionalProjects != null) {
5211+
String additionalProjectsStr = this.connection.getAdditionalProjects();
5212+
if (additionalProjectsStr != null && !additionalProjectsStr.trim().isEmpty()) {
5213+
List<String> additionalProjects =
5214+
com.google.common.base.Splitter.on(',')
5215+
.trimResults()
5216+
.omitEmptyStrings()
5217+
.splitToList(additionalProjectsStr);
52135218
for (String project : additionalProjects) {
52145219
if (project != null && !project.isEmpty()) {
52155220
accessibleCatalogs.add(project);

google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcProxyUtility.java

Lines changed: 1 addition & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -108,56 +108,7 @@ static Map<String, String> parseProxyProperties(DataSource ds, String callerClas
108108
}
109109

110110
static Map<String, String> parseProxyProperties(String URL, String callerClassName) {
111-
LOG.finest("++enter++\t" + callerClassName);
112-
Map<String, String> proxyProperties = new HashMap<>();
113-
String proxyHost =
114-
BigQueryJdbcUrlUtility.parseUriProperty(
115-
URL, BigQueryJdbcUrlUtility.PROXY_HOST_PROPERTY_NAME);
116-
if (proxyHost != null) {
117-
proxyProperties.put(BigQueryJdbcUrlUtility.PROXY_HOST_PROPERTY_NAME, proxyHost);
118-
}
119-
String proxyPort =
120-
BigQueryJdbcUrlUtility.parseUriProperty(
121-
URL, BigQueryJdbcUrlUtility.PROXY_PORT_PROPERTY_NAME);
122-
if (proxyPort != null) {
123-
if (!Pattern.compile(validPortRegex).matcher(proxyPort).find()) {
124-
throw new IllegalArgumentException(
125-
"Illegal port number provided %s. Please provide a valid port number.");
126-
}
127-
proxyProperties.put(BigQueryJdbcUrlUtility.PROXY_PORT_PROPERTY_NAME, proxyPort);
128-
}
129-
String proxyUid =
130-
BigQueryJdbcUrlUtility.parseUriProperty(
131-
URL, BigQueryJdbcUrlUtility.PROXY_USER_ID_PROPERTY_NAME);
132-
if (proxyUid != null) {
133-
proxyProperties.put(BigQueryJdbcUrlUtility.PROXY_USER_ID_PROPERTY_NAME, proxyUid);
134-
}
135-
String proxyPwd =
136-
BigQueryJdbcUrlUtility.parseUriProperty(
137-
URL, BigQueryJdbcUrlUtility.PROXY_PASSWORD_PROPERTY_NAME);
138-
if (proxyPwd != null) {
139-
proxyProperties.put(BigQueryJdbcUrlUtility.PROXY_PASSWORD_PROPERTY_NAME, proxyPwd);
140-
}
141-
142-
boolean isMissingProxyHostOrPortWhenProxySet =
143-
(proxyHost == null && proxyPort != null) || (proxyHost != null && proxyPort == null);
144-
if (isMissingProxyHostOrPortWhenProxySet) {
145-
throw new IllegalArgumentException(
146-
"Both ProxyHost and ProxyPort parameters need to be specified. No defaulting behavior"
147-
+ " occurs.");
148-
}
149-
boolean isMissingProxyUidOrPwdWhenAuthSet =
150-
(proxyUid == null && proxyPwd != null) || (proxyUid != null && proxyPwd == null);
151-
if (isMissingProxyUidOrPwdWhenAuthSet) {
152-
throw new IllegalArgumentException(
153-
"Both ProxyUid and ProxyPwd parameters need to be specified for authentication.");
154-
}
155-
boolean isProxyAuthSetWithoutProxySettings = proxyUid != null && proxyHost == null;
156-
if (isProxyAuthSetWithoutProxySettings) {
157-
throw new IllegalArgumentException(
158-
"Proxy authentication provided via connection string with no proxy host or port set.");
159-
}
160-
return proxyProperties;
111+
return parseProxyProperties(DataSource.fromUrl(URL), callerClassName);
161112
}
162113

163114
static HttpTransportOptions getHttpTransportOptions(

google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcUrlUtility.java

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@
2222
import com.google.common.base.Splitter;
2323
import com.google.common.collect.ImmutableList;
2424
import com.google.common.net.UrlEscapers;
25-
import java.io.UnsupportedEncodingException;
26-
import java.net.URLDecoder;
27-
import java.nio.charset.StandardCharsets;
2825
import java.util.Arrays;
2926
import java.util.Collections;
3027
import java.util.HashMap;
@@ -778,36 +775,6 @@ static String parsePartnerTokenProperty(String url, String callerClassName) {
778775
return parseUriProperty(url, PARTNER_TOKEN_PROPERTY_NAME);
779776
}
780777

781-
static Map<String, String> parseOverrideProperties(String url, String callerClassName) {
782-
LOG.finest("++enter++\t" + callerClassName);
783-
Map<String, String> overrideProps = new HashMap<>();
784-
Pattern pattern =
785-
Pattern.compile(
786-
String.format(
787-
"(?is)(%s|%s)=([^;]+)",
788-
ENDPOINT_OVERRIDES_PROPERTY_NAME, PRIVATE_SERVICE_CONNECT_PROPERTY_NAME));
789-
Matcher matcher = pattern.matcher(url);
790-
String overridePropertiesString;
791-
if (matcher.find() && matcher.groupCount() >= 1) {
792-
try {
793-
overridePropertiesString =
794-
URLDecoder.decode(matcher.group(2), StandardCharsets.UTF_8.name());
795-
} catch (UnsupportedEncodingException e) {
796-
throw new BigQueryJdbcRuntimeException(e);
797-
}
798-
} else {
799-
return overrideProps;
800-
}
801-
for (String property : OVERRIDE_PROPERTIES) {
802-
Pattern propertyPattern = Pattern.compile(String.format("(?i)%s=(.*?)(?:[,;]|$)", property));
803-
Matcher propertyMatcher = propertyPattern.matcher(overridePropertiesString);
804-
if (propertyMatcher.find() && propertyMatcher.groupCount() >= 1) {
805-
overrideProps.put(property, propertyMatcher.group(1));
806-
}
807-
}
808-
return overrideProps;
809-
}
810-
811778
public static Level parseLogLevel(String logLevelString) {
812779
int logLevel = logLevelString != null ? Integer.parseInt(logLevelString) : DEFAULT_LOG_LEVEL;
813780
switch (logLevel) {

google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/DataSource.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import java.util.Properties;
2828
import java.util.function.BiConsumer;
2929
import java.util.logging.Logger;
30+
import java.util.regex.Matcher;
31+
import java.util.regex.Pattern;
3032

3133
/**
3234
* BigQuery JDBC implementation of {@link javax.sql.DataSource}
@@ -334,6 +336,29 @@ public static DataSource fromUrl(String url) {
334336
return dataSource;
335337
}
336338

339+
public Map<String, String> getOverrideProperties() {
340+
String overridePropertiesString = null;
341+
if (endpointOverrides != null && !endpointOverrides.isEmpty()) {
342+
overridePropertiesString = endpointOverrides;
343+
} else if (privateServiceConnect != null && !privateServiceConnect.isEmpty()) {
344+
overridePropertiesString = privateServiceConnect;
345+
}
346+
347+
Map<String, String> overrideProps = new java.util.HashMap<>();
348+
if (overridePropertiesString == null || overridePropertiesString.isEmpty()) {
349+
return overrideProps;
350+
}
351+
352+
for (String property : BigQueryJdbcUrlUtility.OVERRIDE_PROPERTIES) {
353+
Pattern propertyPattern = Pattern.compile(String.format("(?i)%s=(.*?)(?:[,;]|$)", property));
354+
Matcher propertyMatcher = propertyPattern.matcher(overridePropertiesString);
355+
if (propertyMatcher.find() && propertyMatcher.groupCount() >= 1) {
356+
overrideProps.put(property, propertyMatcher.group(1));
357+
}
358+
}
359+
return overrideProps;
360+
}
361+
337362
/** An implementation of DataSource must include a public no-arg constructor. */
338363
public DataSource() {}
339364

@@ -933,6 +958,21 @@ public Integer getJobCreationMode() {
933958
: (Integer) BigQueryJdbcUrlUtility.DEFAULT_JOB_CREATION_MODE;
934959
}
935960

961+
public Boolean getUseStatelessQueryMode() {
962+
Integer jobCreationModeVal = getJobCreationMode();
963+
if (jobCreationModeVal == 2) {
964+
return true;
965+
} else if (jobCreationModeVal == 1) {
966+
return false;
967+
} else {
968+
throw new IllegalArgumentException(
969+
String.format(
970+
"Invalid value for %s. Use 1 for JOB_CREATION_REQUIRED and 2 for"
971+
+ " JOB_CREATION_OPTIONAL.",
972+
BigQueryJdbcUrlUtility.JOB_CREATION_MODE_PROPERTY_NAME));
973+
}
974+
}
975+
936976
public void setJobCreationMode(Integer jobCreationMode) {
937977
this.jobCreationMode = jobCreationMode;
938978
}

google-cloud-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/BigQueryConnectionTest.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@
2727
import java.io.IOException;
2828
import java.io.InputStream;
2929
import java.sql.SQLException;
30-
import java.util.Arrays;
31-
import java.util.Collections;
32-
import java.util.List;
3330
import java.util.Properties;
3431
import org.junit.Before;
3532
import org.junit.Test;
@@ -243,9 +240,9 @@ public void testAdditionalProjects() throws IOException, BigQueryJdbcException {
243240
+ "OAuthClientSecret=redactedToken;"
244241
+ "AdditionalProjects=projA,projB";
245242
try (BigQueryConnection conn1 = new BigQueryConnection(url1)) {
246-
List<String> additionalProjects1 = conn1.getAdditionalProjects();
243+
String additionalProjects1 = conn1.getAdditionalProjects();
247244
assertNotNull(additionalProjects1);
248-
assertEquals(Arrays.asList("projA", "projB"), additionalProjects1);
245+
assertEquals("projA,projB", additionalProjects1);
249246
} catch (SQLException | IOException e) {
250247
throw new BigQueryJdbcException(e);
251248
}
@@ -256,9 +253,9 @@ public void testAdditionalProjects() throws IOException, BigQueryJdbcException {
256253
+ "OAuthClientSecret=redactedToken;"
257254
+ "AdditionalProjects=projX";
258255
try (BigQueryConnection conn2 = new BigQueryConnection(url2)) {
259-
List<String> additionalProjects2 = conn2.getAdditionalProjects();
256+
String additionalProjects2 = conn2.getAdditionalProjects();
260257
assertNotNull(additionalProjects2);
261-
assertEquals(Collections.singletonList("projX"), additionalProjects2);
258+
assertEquals("projX", additionalProjects2);
262259
} catch (SQLException | IOException e) {
263260
throw new BigQueryJdbcException(e);
264261
}

google-cloud-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcOAuthUtilityTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public void testInvalidTokenUriForAuthType0() {
9191
Map<String, String> oauthProperties =
9292
BigQueryJdbcOAuthUtility.parseOAuthProperties(connectionString, null);
9393
Map<String, String> overrideProperties =
94-
BigQueryJdbcUrlUtility.parseOverrideProperties(connectionString, null);
94+
DataSource.fromUrl(connectionString).getOverrideProperties();
9595

9696
try {
9797
BigQueryJdbcOAuthUtility.getCredentials(oauthProperties, overrideProperties, null);
@@ -235,7 +235,7 @@ public void testGenerateUserAuthURLOverrideOauthEndpoint() {
235235
Map<String, String> authProperties =
236236
BigQueryJdbcOAuthUtility.parseOAuthProperties(connectionString, null);
237237
Map<String, String> overrideProperties =
238-
BigQueryJdbcUrlUtility.parseOverrideProperties(connectionString, null);
238+
DataSource.fromUrl(connectionString).getOverrideProperties();
239239

240240
UserAuthorizer userAuthorizer =
241241
BigQueryJdbcOAuthUtility.getUserAuthorizer(
@@ -275,7 +275,7 @@ public void testParseOverridePropsForRefreshTokenAuth() {
275275
Map<String, String> authProperties =
276276
BigQueryJdbcOAuthUtility.parseOAuthProperties(connectionString, null);
277277
Map<String, String> overrideProperties =
278-
BigQueryJdbcUrlUtility.parseOverrideProperties(connectionString, null);
278+
DataSource.fromUrl(connectionString).getOverrideProperties();
279279

280280
UserCredentials userCredentials =
281281
BigQueryJdbcOAuthUtility.getPreGeneratedRefreshTokenCredentials(

0 commit comments

Comments
 (0)