Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for constructing a BQConnection from Bigquery object #194

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 47 additions & 32 deletions src/main/java/net/starschema/clouddb/jdbc/BQConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public class BQConnection implements Connection {

private Long maxBillingBytes;

private Integer timeoutMs;
private final Integer timeoutMs;

private final Map<String, String> labels;

Expand All @@ -78,7 +78,7 @@ public class BQConnection implements Connection {
* href="https://github.com/googleapis/java-bigquery/blob/v2.34.0/google-cloud-bigquery/src/main/java/com/google/cloud/bigquery/QueryJobConfiguration.java#L98-L111">google-cloud-bigquery
* 2.34.0</a>
*/
public static enum JobCreationMode {
public enum JobCreationMode {
/** If unspecified JOB_CREATION_REQUIRED is the default. */
JOB_CREATION_MODE_UNSPECIFIED,
/** Default. Job creation is always required. */
Expand All @@ -94,19 +94,19 @@ public static enum JobCreationMode {
*/
JOB_CREATION_OPTIONAL;

private JobCreationMode() {}
JobCreationMode() {}
}

/** The job creation mode - */
private JobCreationMode jobCreationMode = JobCreationMode.JOB_CREATION_MODE_UNSPECIFIED;
private final JobCreationMode jobCreationMode;

/** getter for useLegacySql */
public boolean getUseLegacySql() {
return useLegacySql;
}

/** List to contain sql warnings in */
private List<SQLWarning> SQLWarningList = new ArrayList<SQLWarning>();
private final List<SQLWarning> SQLWarningList = new ArrayList<>();

/** String to contain the url except the url prefix */
private String URLPART = null;
Expand All @@ -124,6 +124,17 @@ public BQConnection(String url, Properties loginProp) throws SQLException {
this(url, loginProp, Oauth2Bigquery.HTTP_TRANSPORT);
}

static Properties toCaseInsensitive(Properties props) {
final Properties ret = new Properties();
if (props != null) {
for (Object o : props.keySet()) {
String prop = (String) o;
ret.setProperty(prop.toLowerCase(), props.getProperty(prop));
}
}
return ret;
}

/**
* Like {@link BQConnection(String,Properties)} but allows setting the {@link HttpTransport} for
* testing.
Expand Down Expand Up @@ -160,21 +171,10 @@ public BQConnection(String url, Properties loginProp, HttpTransport httpTranspor
throw new BQSQLException(e1);
}

Properties caseInsensitiveLoginProps = new Properties();

if (loginProp != null) {
Iterator props = loginProp.keySet().iterator();
while (props.hasNext()) {
String prop = (String) props.next();
caseInsensitiveLoginProps.setProperty(prop.toLowerCase(), loginProp.getProperty(prop));
}
}

Properties caseInsensitiveProps;

final Properties caseInsensitiveProps;
try {
// parse the connection string and override anything passed via loginProps.
caseInsensitiveProps = BQSupportFuncts.getUrlQueryComponents(url, caseInsensitiveLoginProps);
caseInsensitiveProps = BQSupportFuncts.getUrlQueryComponents(url, toCaseInsensitive(loginProp));
} catch (UnsupportedEncodingException e2) {
throw new BQSQLException(e2);
}
Expand Down Expand Up @@ -238,17 +238,7 @@ public BQConnection(String url, Properties loginProp, HttpTransport httpTranspor
this.useQueryCache =
parseBooleanQueryParam(caseInsensitiveProps.getProperty("querycache"), true);

final String jobCreationModeString = caseInsensitiveProps.getProperty("jobcreationmode");
if (jobCreationModeString == null) {
jobCreationMode = null;
} else {
try {
jobCreationMode = JobCreationMode.valueOf(jobCreationModeString);
} catch (IllegalArgumentException e) {
throw new BQSQLException(
"could not parse " + jobCreationModeString + " as job creation mode", e);
}
}
this.jobCreationMode = determineJobCreationMode(caseInsensitiveProps);

// Create Connection to BigQuery
if (serviceAccount) {
Expand All @@ -272,9 +262,7 @@ public BQConnection(String url, Properties loginProp, HttpTransport httpTranspor
targetServiceAccounts,
this.getProjectId());
this.logger.info("Authorized with service account");
} catch (GeneralSecurityException e) {
throw new BQSQLException(e);
} catch (IOException e) {
} catch (GeneralSecurityException | IOException e) {
throw new BQSQLException(e);
}
} else if (oAuthAccessToken != null) {
Expand Down Expand Up @@ -313,6 +301,33 @@ public BQConnection(String url, Properties loginProp, HttpTransport httpTranspor
logger.debug("The project id for this connections is: " + projectId);
}

public BQConnection(Bigquery bigquery, Properties properties) throws SQLException {
final Properties caseInsensitiveProps = toCaseInsensitive(properties);

// required final fields
this.timeoutMs = parseIntQueryParam("timeoutMs", caseInsensitiveProps.getProperty("timeoutms"));
this.labels = tryParseLabels(caseInsensitiveProps.getProperty("labels"));
this.useQueryCache =
parseBooleanQueryParam(caseInsensitiveProps.getProperty("querycache"), true);
this.useLegacySql =
parseBooleanQueryParam(caseInsensitiveProps.getProperty("uselegacysql"), false);
this.jobCreationMode = determineJobCreationMode(caseInsensitiveProps);
}

static JobCreationMode determineJobCreationMode(Properties caseInsensitiveProps)
throws SQLException {
final String jobCreationModeString = caseInsensitiveProps.getProperty("jobcreationmode");
if (jobCreationModeString == null) {
return JobCreationMode.JOB_CREATION_MODE_UNSPECIFIED;
}
try {
return JobCreationMode.valueOf(jobCreationModeString);
} catch (IllegalArgumentException e) {
throw new BQSQLException(
"could not parse " + jobCreationModeString + " as job creation mode", e);
}
}

private static Map<String, String> tryParseLabels(@Nullable String labels) {
if (labels == null) {
return Collections.emptyMap();
Expand Down