com.speedment.runtime.core.internal.component.sql.SqlPersistenceImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of tool-deploy Show documentation
Show all versions of tool-deploy Show documentation
A Speedment bundle that shades all dependencies into one jar. This is
useful when deploying an application on a server.
The newest version!
/*
*
* Copyright (c) 2006-2019, Speedment, Inc. All Rights Reserved.
*
* 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 com.speedment.runtime.core.internal.component.sql;
import com.speedment.common.mapstream.MapStream;
import com.speedment.runtime.config.*;
import com.speedment.runtime.config.identifier.TableIdentifier;
import com.speedment.runtime.config.trait.HasColumn;
import com.speedment.runtime.config.util.DocumentDbUtil;
import com.speedment.runtime.config.util.DocumentUtil;
import com.speedment.runtime.core.component.DbmsHandlerComponent;
import com.speedment.runtime.core.component.ManagerComponent;
import com.speedment.runtime.core.component.ProjectComponent;
import com.speedment.runtime.core.component.resultset.ResultSetMapperComponent;
import com.speedment.runtime.core.component.resultset.ResultSetMapping;
import com.speedment.runtime.core.db.DatabaseNamingConvention;
import com.speedment.runtime.core.db.DbmsColumnHandler;
import com.speedment.runtime.core.db.DbmsOperationHandler;
import com.speedment.runtime.core.db.DbmsType;
import com.speedment.runtime.core.exception.SpeedmentException;
import com.speedment.runtime.core.manager.HasLabelSet;
import com.speedment.runtime.core.manager.Manager;
import com.speedment.runtime.core.util.DatabaseUtil;
import com.speedment.runtime.field.Field;
import com.speedment.runtime.typemapper.TypeMapper;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static com.speedment.common.invariant.NullUtil.requireNonNulls;
import static com.speedment.runtime.config.util.DocumentUtil.Name.DATABASE_NAME;
import static java.util.Comparator.comparing;
import static java.util.Objects.requireNonNull;
import static java.util.function.Function.identity;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
/**
* Default implementation of the {@link SqlPersistence}-interface.
*
* @param the entity type
*
* @author Emil Forslund
* @since 3.0.1
*/
final class SqlPersistenceImpl implements SqlPersistence {
private final Supplier>> primaryKeyFields;
private final Supplier>> fields;
private final Dbms dbms;
private final Table table;
private final DbmsType dbmsType;
private final String sqlTableReference;
private final boolean hasPrimaryKeyColumns;
private final DatabaseNamingConvention naming;
private final DbmsOperationHandler operationHandler;
private final DbmsColumnHandler columnHandler;
private final Class entityClass;
private final String insertStatement;
private final String updateStatement;
private final String deleteStatement;
private final List> generatedFieldSupports;
private final List> generatedFields;
private final Map, Column> columnsByFields;
private final Predicate insertColumnFilter;
private final Predicate updateColumnFilter;
public SqlPersistenceImpl(
TableIdentifier tableId,
ProjectComponent projectComponent,
DbmsHandlerComponent dbmsHandlerComponent,
ManagerComponent managerComponent,
ResultSetMapperComponent resultSetMapperComponent) {
requireNonNulls(tableId,
projectComponent,
dbmsHandlerComponent,
managerComponent,
resultSetMapperComponent
);
final Project project = projectComponent.getProject();
this.table = DocumentDbUtil.referencedTable(project, tableId);
this.dbms = DocumentDbUtil.referencedDbms(project, tableId);
this.dbmsType = DatabaseUtil.dbmsTypeOf(dbmsHandlerComponent, dbms);
this.naming = dbmsType.getDatabaseNamingConvention();
this.operationHandler = dbmsType.getOperationHandler();
this.columnHandler = dbmsType.getColumnHandler();
@SuppressWarnings("unchecked")
final Manager manager = (Manager) managerComponent.stream()
.filter(m -> tableId.equals(m.getTableIdentifier()))
.findAny().orElseThrow(() -> new SpeedmentException(
"Could not find any manager for table '" + tableId + "'."
));
this.primaryKeyFields = manager::primaryKeyFields;
this.fields = manager::fields;
this.entityClass = manager.getEntityClass();
this.sqlTableReference = naming.fullNameOf(table);
this.hasPrimaryKeyColumns = manager.primaryKeyFields().anyMatch(m -> true);
this.insertColumnFilter = columnHandler.excludedInInsertStatement().negate();
this.insertStatement = getInsertStatement(insertColumnFilter);
this.updateColumnFilter = columnHandler.excludedInUpdateStatement().negate();
this.updateStatement = getUpdateStatement(updateColumnFilter);
this.deleteStatement = "DELETE FROM " + sqlTableReference + " WHERE " +
sqlPrimaryKeyColumnList(pk -> pk + " = ?");
this.columnsByFields = MapStream.fromKeys(fields.get(), f ->
DocumentDbUtil.referencedColumn(project, f.identifier())
).toMap();
this.generatedFieldSupports = columnsByFields.entrySet().stream().filter(e -> e.getValue().isAutoIncrement())
.map(e -> new GeneratedFieldSupport<>(
e.getKey(), e.getValue(),
resultSetMapperComponent.apply(e.getValue().findDatabaseType())
)).collect(toList());
this.generatedFields = generatedFieldSupports.stream()
.map(GeneratedFieldSupport::getField).collect(toList());
}
private String getUpdateStatement(Predicate includedInUpdate) {
return "UPDATE " + sqlTableReference + " SET " +
sqlColumnList(includedInUpdate, n -> n + " = ?") + " WHERE " +
sqlPrimaryKeyColumnList(pk -> pk + " = ?");
}
private String getInsertStatement(Predicate includedInInsert) {
return "INSERT INTO " + sqlTableReference + " (" +
sqlColumnList(includedInInsert, identity()) + ") VALUES (" +
sqlColumnList(includedInInsert, c -> "?") + ")";
}
private SqlPersistenceImpl(SqlPersistenceImpl template, HasLabelSet includedFields) {
primaryKeyFields = template.primaryKeyFields;
fields = template.fields;
dbms = template.dbms;
table = template.table;
dbmsType = template.dbmsType;
sqlTableReference = template.sqlTableReference;
hasPrimaryKeyColumns = template.hasPrimaryKeyColumns;
naming = template.naming;
operationHandler = template.operationHandler;
columnHandler = template.columnHandler;
entityClass = template.entityClass;
this.insertColumnFilter = columnHandler.excludedInInsertStatement().negate().and(c -> includedFields.test(c.getId()));
this.insertStatement = getInsertStatement(insertColumnFilter);
this.updateColumnFilter = columnHandler.excludedInUpdateStatement().negate().and(c -> includedFields.test(c.getId()));
this.updateStatement = getUpdateStatement(updateColumnFilter);
deleteStatement = template.deleteStatement;
generatedFieldSupports = template.generatedFieldSupports;
generatedFields = template.generatedFields;
columnsByFields = template.columnsByFields;
}
SqlPersistenceImpl withLimitedFields(HasLabelSet fields) {
return new SqlPersistenceImpl<>(this, fields);
}
@Override
public ENTITY persist(ENTITY entity) throws SpeedmentException {
final List