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

[Dat-17482] Code refactoring #324

Merged
merged 10 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package liquibase.ext.bigquery.change;

import liquibase.change.AddColumnConfig;
import liquibase.change.ChangeMetaData;
import liquibase.change.DatabaseChange;
import liquibase.change.core.AddColumnChange;
import liquibase.change.core.DropColumnChange;
import liquibase.change.core.MergeColumnChange;
import liquibase.database.Database;
import liquibase.ext.bigquery.database.BigqueryDatabase;
import liquibase.ext.bigquery.database.BigQueryDatabase;
import liquibase.servicelocator.PrioritizedService;
import liquibase.statement.SqlStatement;
import liquibase.statement.core.RawSqlStatement;
import liquibase.structure.core.Column;
Expand All @@ -18,12 +18,12 @@

@DatabaseChange(name="mergeColumns",
description = "Concatenates the values in two columns, joins them by with string, and stores the resulting value in a new column.",
priority = ChangeMetaData.PRIORITY_DATABASE)
priority = PrioritizedService.PRIORITY_DATABASE)
public class BigQueryMergeColumnChange extends MergeColumnChange {

@Override
public boolean supports(Database database) {
return database instanceof BigqueryDatabase;
return database instanceof BigQueryDatabase;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,20 @@
import java.util.Optional;
import java.util.Properties;

import static liquibase.ext.bigquery.database.BigqueryDatabase.BIGQUERY_PRIORITY_DATABASE;
import static liquibase.ext.bigquery.database.BigQueryDatabase.BIGQUERY_PRIORITY_DATABASE;

/**
* A Bigquery specific Delegate that removes the calls to autocommit
*/

public class BigqueryConnection extends JdbcConnection {
public class BigQueryConnection extends JdbcConnection {
private static final String LOCATION = "Location";
private S42Connection con;

public BigqueryConnection() {
public BigQueryConnection() {
}

public BigqueryConnection(Connection conn) throws SQLException {
public BigQueryConnection(Connection conn) throws SQLException {
this.con = (S42Connection) conn;
String url = conn.getMetaData().getURL();
Scope.getCurrentScope().getLog(this.getClass()).fine(String.format("Setting connection to %s Location=%s", url, getUnderlyingBQConnectionLocation()));
Expand Down Expand Up @@ -85,9 +86,9 @@ public Connection getUnderlyingConnection() {

@Override
public void open(String url, Driver driverObject, Properties driverProperties) throws DatabaseException {
if (driverProperties.stringPropertyNames().contains("Location")) {
String locationValue = getUrlParamValue(url, "Location");
driverProperties.setProperty("Location", locationValue);
if (driverProperties.stringPropertyNames().contains(LOCATION)) {
String locationValue = getUrlParamValue(url, LOCATION);
driverProperties.setProperty(LOCATION, locationValue);
}

Scope.getCurrentScope().getLog(this.getClass()).fine(String.format("Opening connection to %s driverProperties=%s", url, driverProperties));
Expand Down Expand Up @@ -124,6 +125,7 @@ public boolean getAutoCommit() throws DatabaseException {

@Override
public void setAutoCommit(boolean autoCommit) throws DatabaseException {
// not supported by BigQuery
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,22 @@
import liquibase.structure.DatabaseObject;
import liquibase.structure.core.Catalog;
import liquibase.structure.core.Schema;
import liquibase.structure.core.Sequence;
import liquibase.structure.core.Table;

import java.util.HashSet;
import java.util.Set;
import java.util.regex.Pattern;

public class BigqueryDatabase extends AbstractJdbcDatabase {
public class BigQueryDatabase extends AbstractJdbcDatabase {

public static final String PRODUCT_NAME = BQDriver.DATABASE_NAME;
public static final int BIGQUERY_PRIORITY_DATABASE = 510;
private String liquibaseSchemaName;

private static final Pattern CREATE_VIEW_AS_PATTERN = Pattern.compile("^CREATE\\s+.*?VIEW\\s+.*?AS\\s+", 34);

public BigqueryDatabase() {
public BigQueryDatabase() {
this.setCurrentDateTimeFunction("CURRENT_DATETIME()");
this.unquotedObjectsAreUppercased = false;
this.addReservedWords(getDefaultReservedWords());
Expand All @@ -53,6 +54,7 @@ public int getPriority() {
return BIGQUERY_PRIORITY_DATABASE;
}

@Override
public boolean supportsDatabaseChangeLogHistory() {
return true;
}
Expand Down Expand Up @@ -93,6 +95,14 @@ public int getDatabaseMinorVersion() {
return BQDriver.DRIVER_MINOR_VERSION;
}

@Override
public boolean supports(Class<? extends DatabaseObject> object) {
if (Sequence.class.isAssignableFrom(object)) {
return false;
}
return super.supports(object);
}

@Override
public boolean supportsInitiallyDeferrableColumns() {
return false;
Expand Down Expand Up @@ -141,7 +151,7 @@ public String getDefaultDriver(String url) {

@Override
public boolean supportsSequences() {
return false;
return this.supports(Sequence.class);
}

@Override
Expand All @@ -164,7 +174,7 @@ private String getDefaultDataset() {
if (connection == null) {
return null;
}
return BigqueryConnection.getUrlParamValue(connection.getURL(), "DefaultDataset");
return BigQueryConnection.getUrlParamValue(connection.getURL(), "DefaultDataset");
}

@Override
Expand Down Expand Up @@ -222,7 +232,7 @@ public String getJdbcCatalogName(final CatalogAndSchema schema) {
@Override
public String getViewDefinition(CatalogAndSchema schema, String viewName) throws DatabaseException {
schema = schema.customize(this);
String definition = (String) ((ExecutorService) Scope.getCurrentScope().getSingleton(ExecutorService.class))
String definition = (Scope.getCurrentScope().getSingleton(ExecutorService.class))
.getExecutor("jdbc", this)
.queryForObject(new GetViewDefinitionStatement(schema.getCatalogName(), schema.getSchemaName(), viewName), String.class);
Scope.getCurrentScope().getLog(this.getClass()).info("getViewDefinition "+definition);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import liquibase.datatype.DataTypeInfo;
import liquibase.datatype.DatabaseDataType;
import liquibase.datatype.LiquibaseDataType;
import liquibase.ext.bigquery.database.BigqueryDatabase;
import liquibase.ext.bigquery.database.BigQueryDatabase;


@DataTypeInfo(
Expand All @@ -16,23 +16,29 @@
)
public class BignumericDataTypeBigQuery extends LiquibaseDataType {

private final static String BIGNUMERIC = "BIGNUMERIC";
private static final String BIGNUMERIC = "BIGNUMERIC";

public BignumericDataTypeBigQuery() {
}

@Override
public boolean supports(Database database) {
return database instanceof BigqueryDatabase;
return database instanceof BigQueryDatabase;
}

@Override
public DatabaseDataType toDatabaseDataType(Database database) {
if (database instanceof BigqueryDatabase) {
if (database instanceof BigQueryDatabase) {

DatabaseDataType type = new DatabaseDataType(BIGNUMERIC, this.getParameters());
if (this.getParameters().length > 0) {
String firstParameter = String.valueOf(this.getParameters()[0]);
int typePrecision = Integer.parseInt(firstParameter);
if (typePrecision == 77) {
try {
int typePrecision = Integer.parseInt(firstParameter);
if (typePrecision == 77) {
type.setType(BIGNUMERIC);
}
} catch (NumberFormatException e) {
type.setType(BIGNUMERIC);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import liquibase.datatype.DataTypeInfo;
import liquibase.datatype.DatabaseDataType;
import liquibase.datatype.LiquibaseDataType;
import liquibase.ext.bigquery.database.BigqueryDatabase;
import liquibase.ext.bigquery.database.BigQueryDatabase;

import static liquibase.ext.bigquery.database.BigqueryDatabase.BIGQUERY_PRIORITY_DATABASE;
import static liquibase.ext.bigquery.database.BigQueryDatabase.BIGQUERY_PRIORITY_DATABASE;


@DataTypeInfo(
Expand All @@ -20,12 +20,14 @@ public class BoolDataTypeBigQuery extends LiquibaseDataType {
public BoolDataTypeBigQuery() {
}

@Override
public boolean supports(Database database) {
return database instanceof BigqueryDatabase;
return database instanceof BigQueryDatabase;
}

@Override
public DatabaseDataType toDatabaseDataType(Database database) {
if (database instanceof BigqueryDatabase) {
if (database instanceof BigQueryDatabase) {

DatabaseDataType type = new DatabaseDataType("BOOL", this.getParameters());
type.setType("BOOL");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import liquibase.datatype.DataTypeInfo;
import liquibase.datatype.DatabaseDataType;
import liquibase.datatype.LiquibaseDataType;
import liquibase.ext.bigquery.database.BigqueryDatabase;
import liquibase.ext.bigquery.database.BigQueryDatabase;

import static liquibase.ext.bigquery.database.BigqueryDatabase.BIGQUERY_PRIORITY_DATABASE;
import static liquibase.ext.bigquery.database.BigQueryDatabase.BIGQUERY_PRIORITY_DATABASE;

@DataTypeInfo(
name = "float64",
Expand All @@ -19,12 +19,14 @@ public class Float64DataTypeBigQuery extends LiquibaseDataType {
public Float64DataTypeBigQuery() {
}

@Override
public boolean supports(Database database) {
return database instanceof BigqueryDatabase;
return database instanceof BigQueryDatabase;
}

@Override
public DatabaseDataType toDatabaseDataType(Database database) {
if (database instanceof BigqueryDatabase) {
if (database instanceof BigQueryDatabase) {

DatabaseDataType type = new DatabaseDataType("FLOAT64", this.getParameters());
type.setType("FLOAT64");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import liquibase.datatype.DataTypeInfo;
import liquibase.datatype.DatabaseDataType;
import liquibase.datatype.LiquibaseDataType;
import liquibase.ext.bigquery.database.BigqueryDatabase;
import liquibase.ext.bigquery.database.BigQueryDatabase;

import static liquibase.ext.bigquery.database.BigqueryDatabase.BIGQUERY_PRIORITY_DATABASE;
import static liquibase.ext.bigquery.database.BigQueryDatabase.BIGQUERY_PRIORITY_DATABASE;


@DataTypeInfo(
Expand All @@ -20,12 +20,14 @@ public class GeographyDataTypeBigQuery extends LiquibaseDataType {
public GeographyDataTypeBigQuery() {
}

@Override
public boolean supports(Database database) {
return database instanceof BigqueryDatabase;
return database instanceof BigQueryDatabase;
}

@Override
public DatabaseDataType toDatabaseDataType(Database database) {
if (database instanceof BigqueryDatabase) {
if (database instanceof BigQueryDatabase) {

DatabaseDataType type = new DatabaseDataType("GEOGRAPHY", this.getParameters());
type.setType("GEOGRAPHY");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
import liquibase.datatype.DataTypeInfo;
import liquibase.datatype.DatabaseDataType;
import liquibase.datatype.LiquibaseDataType;
import liquibase.ext.bigquery.database.BigqueryDatabase;

import static liquibase.ext.bigquery.database.BigqueryDatabase.BIGQUERY_PRIORITY_DATABASE;
import liquibase.ext.bigquery.database.BigQueryDatabase;


@DataTypeInfo(
Expand All @@ -20,12 +18,14 @@ public class Int64DataTypeBigQuery extends LiquibaseDataType {
public Int64DataTypeBigQuery() {
}

@Override
public boolean supports(Database database) {
return database instanceof BigqueryDatabase;
return database instanceof BigQueryDatabase;
}

@Override
public DatabaseDataType toDatabaseDataType(Database database) {
if (database instanceof BigqueryDatabase) {
if (database instanceof BigQueryDatabase) {

DatabaseDataType type = new DatabaseDataType("INT64", this.getParameters());
type.setType("INT64");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import liquibase.datatype.DataTypeInfo;
import liquibase.datatype.DatabaseDataType;
import liquibase.datatype.LiquibaseDataType;
import liquibase.ext.bigquery.database.BigqueryDatabase;
import liquibase.ext.bigquery.database.BigQueryDatabase;


@DataTypeInfo(
Expand All @@ -18,12 +18,14 @@ public class NumericDataTypeBigQuery extends LiquibaseDataType {
public NumericDataTypeBigQuery() {
}

@Override
public boolean supports(Database database) {
return database instanceof BigqueryDatabase;
return database instanceof BigQueryDatabase;
}

@Override
public DatabaseDataType toDatabaseDataType(Database database) {
if (database instanceof BigqueryDatabase) {
if (database instanceof BigQueryDatabase) {

return new DatabaseDataType("NUMERIC", this.getParameters());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import liquibase.datatype.DataTypeInfo;
import liquibase.datatype.DatabaseDataType;
import liquibase.datatype.core.VarcharType;
import liquibase.ext.bigquery.database.BigqueryDatabase;
import liquibase.ext.bigquery.database.BigQueryDatabase;

import liquibase.datatype.LiquibaseDataType;

Expand All @@ -23,20 +23,24 @@ public StringDataTypeBigQuery() {

@Override
public boolean supports(Database database) {
return database instanceof BigqueryDatabase;
return database instanceof BigQueryDatabase;
}

@Override
public DatabaseDataType toDatabaseDataType(Database database) {
if (database instanceof BigqueryDatabase) {
if (database instanceof BigQueryDatabase) {

DatabaseDataType type = new DatabaseDataType("STRING", this.getParameters());
if (this.getParameters().length == 0) {
type.setType("STRING");
} else {
String firstParameter = String.valueOf(this.getParameters()[0]);
int stringSize = Integer.parseInt(firstParameter);
if (stringSize == 65535) {
try {
int stringSize = Integer.parseInt(firstParameter);
if (stringSize == 65535) {
type.setType("STRING");
}
} catch (NumberFormatException e) {
type.setType("STRING");
}
}
Expand All @@ -57,6 +61,7 @@ public String objectToSql(Object value, Database database) {
}
}

@Override
public LoadDataChange.LOAD_DATA_TYPE getLoadTypeName() {
return LoadDataChange.LOAD_DATA_TYPE.STRING;
}
Expand Down
Loading
Loading