liquibase.nosql.executor.NoSqlExecutor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of liquibase-cosmosdb Show documentation
Show all versions of liquibase-cosmosdb Show documentation
Liquibase extension for Cosmos DB via Core (SQL) API
package liquibase.nosql.executor;
/*-
* #%L
* Liquibase CosmosDB Extension
* %%
* Copyright (C) 2020 Mastercard
* %%
* 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.
* #L%
*/
import liquibase.Scope;
import liquibase.changelog.ChangeLogHistoryServiceFactory;
import liquibase.database.Database;
import liquibase.exception.DatabaseException;
import liquibase.executor.AbstractExecutor;
import liquibase.ext.cosmosdb.database.CosmosLiquibaseDatabase;
import liquibase.logging.Logger;
import liquibase.nosql.changelog.AbstractNoSqlHistoryService;
import liquibase.nosql.database.AbstractNoSqlConnection;
import liquibase.nosql.database.AbstractNoSqlDatabase;
import liquibase.nosql.statement.NoSqlExecuteStatement;
import liquibase.nosql.statement.NoSqlQueryForListStatement;
import liquibase.nosql.statement.NoSqlQueryForLongStatement;
import liquibase.nosql.statement.NoSqlQueryForObjectStatement;
import liquibase.nosql.statement.NoSqlUpdateStatement;
import liquibase.servicelocator.LiquibaseService;
import liquibase.sql.visitor.SqlVisitor;
import liquibase.statement.SqlStatement;
import liquibase.statement.core.UpdateStatement;
import lombok.NoArgsConstructor;
import java.util.List;
import java.util.Map;
import static java.util.Collections.emptyList;
import static java.util.Optional.ofNullable;
@LiquibaseService
@NoArgsConstructor
public class NoSqlExecutor extends AbstractExecutor {
public static final String EXECUTOR_NAME = "jdbc";
private final Logger log = Scope.getCurrentScope().getLog(getClass());
@Override
public void setDatabase(final Database database) {
super.setDatabase(database);
}
@SuppressWarnings("unchecked")
private T getDatabase() {
return (T) database;
}
@Override
public String getName() {
return EXECUTOR_NAME;
}
@Override
public boolean supports(Database database) {
return database instanceof CosmosLiquibaseDatabase;
}
@Override
public int getPriority() {
return PRIORITY_SPECIALIZED;
}
@SuppressWarnings("unchecked")
protected C getConnection() {
return (C) ofNullable(database)
.map(Database::getConnection).orElse(null);
}
@Override
public T queryForObject(final SqlStatement sql, final Class requiredType) throws DatabaseException {
return queryForObject(sql, requiredType, emptyList());
}
@Override
public T queryForObject(final SqlStatement sql, final Class requiredType, final List sqlVisitors) throws DatabaseException {
if (sql instanceof NoSqlQueryForObjectStatement) {
try {
return ((NoSqlQueryForObjectStatement>) sql)
.queryForObject(getDatabase(), requiredType);
} catch (final Exception e) {
throw new DatabaseException("Could not query for object", e);
}
}
throw new IllegalArgumentException();
}
@Override
public long queryForLong(final SqlStatement sql) throws DatabaseException {
return queryForLong(sql, emptyList());
}
@Override
public long queryForLong(final SqlStatement sql, final List sqlVisitors) throws DatabaseException {
if (sql instanceof NoSqlQueryForLongStatement) {
try {
return ((NoSqlQueryForLongStatement extends AbstractNoSqlDatabase>) sql).queryForLong(getDatabase());
} catch (final Exception e) {
throw new DatabaseException("Could not query for long", e);
}
}
throw new IllegalArgumentException();
}
@Override
public int queryForInt(final SqlStatement sql) {
throw new UnsupportedOperationException();
}
@Override
public int queryForInt(final SqlStatement sql, final List sqlVisitors) {
throw new UnsupportedOperationException();
}
@Override
public List