DatabaseConnectionBuilder.java
/*
* Copyright 2023-2024 Markitect
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dev.markitect.liquibase.database;
import static com.google.common.base.Preconditions.checkState;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import liquibase.Scope;
import liquibase.database.DatabaseConnection;
import liquibase.database.DatabaseFactory;
import liquibase.exception.DatabaseException;
import org.jspecify.annotations.Nullable;
public final class DatabaseConnectionBuilder {
public static DatabaseConnectionBuilder newBuilder() {
return new DatabaseConnectionBuilder();
}
private @Nullable String url;
private @Nullable String username;
private @Nullable String password;
private @Nullable String driver;
@CanIgnoreReturnValue
public DatabaseConnectionBuilder url(@Nullable String url) {
this.url = url;
return this;
}
@CanIgnoreReturnValue
public DatabaseConnectionBuilder username(@Nullable String username) {
this.username = username;
return this;
}
@CanIgnoreReturnValue
public DatabaseConnectionBuilder password(@Nullable String password) {
this.password = password;
return this;
}
@CanIgnoreReturnValue
public DatabaseConnectionBuilder driver(@Nullable String driver) {
this.driver = driver;
return this;
}
public DatabaseConnection build() {
checkState(url != null);
try {
return DatabaseFactory.getInstance()
.openConnection(
url,
username,
password,
driver,
null,
null,
null,
Scope.getCurrentScope().getResourceAccessor());
} catch (DatabaseException | RuntimeException e) {
throw new IllegalStateException(e);
}
}
}