Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2017-2020 original authors
*
* 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
*
* https://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 io.micronaut.data.jdbc.operations;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import io.micronaut.core.annotation.AnnotationMetadata;
import io.micronaut.core.annotation.AnnotationValue;
import io.micronaut.core.beans.BeanProperty;
import io.micronaut.core.util.ArgumentUtils;
import io.micronaut.core.util.ArrayUtils;
import io.micronaut.core.util.CollectionUtils;
import io.micronaut.data.annotation.*;
import io.micronaut.data.exceptions.DataAccessException;
import io.micronaut.data.intercept.annotation.DataMethod;
import io.micronaut.data.model.*;
import io.micronaut.data.model.query.QueryModel;
import io.micronaut.data.model.query.QueryParameter;
import io.micronaut.data.model.query.builder.AbstractSqlLikeQueryBuilder;
import io.micronaut.data.model.query.builder.QueryResult;
import io.micronaut.data.model.query.builder.sql.Dialect;
import io.micronaut.data.model.query.builder.sql.SqlQueryBuilder;
import io.micronaut.data.model.runtime.EntityOperation;
import io.micronaut.data.model.runtime.RuntimeAssociation;
import io.micronaut.data.model.runtime.RuntimePersistentEntity;
import io.micronaut.data.model.runtime.RuntimePersistentProperty;
import io.micronaut.data.operations.RepositoryOperations;
import io.micronaut.data.runtime.config.DataSettings;
import io.micronaut.data.runtime.date.DateTimeProvider;
import io.micronaut.data.runtime.mapper.QueryStatement;
import io.micronaut.data.runtime.mapper.ResultReader;
import io.micronaut.http.MediaType;
import io.micronaut.http.codec.MediaTypeCodec;
import org.slf4j.Logger;
import java.lang.reflect.Array;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
/**
* Abstract SQL repository implementation not specifically bound to JDBC.
*
* @param The result set type
* @param The prepared statement type
* @author graemerocher
* @since 1.0.0
*/
public abstract class AbstractSqlRepositoryOperations implements RepositoryOperations {
protected static final Logger QUERY_LOG = DataSettings.QUERY_LOG;
protected static final SqlQueryBuilder DEFAULT_SQL_BUILDER = new SqlQueryBuilder();
protected static final Pattern IN_EXPRESSION_PATTERN = Pattern.compile("\\s\\?\\$IN\\((\\d+)\\)");
protected static final String NOT_TRUE_EXPRESSION = "1 = 2";
@SuppressWarnings("WeakerAccess")
protected final ResultReader columnNameResultSetReader;
@SuppressWarnings("WeakerAccess")
protected final ResultReader columnIndexResultSetReader;
@SuppressWarnings("WeakerAccess")
protected final QueryStatement preparedStatementWriter;
protected final Map dialects = new HashMap<>(10);
protected final Map queryBuilders = new HashMap<>(Dialect.values().length);
protected final MediaTypeCodec jsonCodec;
protected final DateTimeProvider dateTimeProvider;
private final Map storedInserts = new ConcurrentHashMap<>(10);
private final Map entityInserts = new ConcurrentHashMap<>(10);
private final Map entityUpdates = new ConcurrentHashMap<>(10);
private final Map associationInserts = new ConcurrentHashMap<>(10);
private final Map entities = new ConcurrentHashMap<>(10);
private final Map idReaders = new ConcurrentHashMap<>(10);
/**
* Default constructor.
*
* @param columnNameResultSetReader The column name result reader
* @param columnIndexResultSetReader The column index result reader
* @param preparedStatementWriter The prepared statement writer
* @param codecs The media type codecs
* @param dateTimeProvider The injected dateTimeProvider instance
*/
protected AbstractSqlRepositoryOperations(
ResultReader columnNameResultSetReader,
ResultReader columnIndexResultSetReader,
QueryStatement preparedStatementWriter,
List codecs,
@NonNull DateTimeProvider dateTimeProvider) {
this.columnNameResultSetReader = columnNameResultSetReader;
this.columnIndexResultSetReader = columnIndexResultSetReader;
this.preparedStatementWriter = preparedStatementWriter;
this.jsonCodec = resolveJsonCodec(codecs);
this.dateTimeProvider = dateTimeProvider;
}
private MediaTypeCodec resolveJsonCodec(List codecs) {
return CollectionUtils.isNotEmpty(codecs) ? codecs.stream().filter(c -> c.getMediaTypes().contains(MediaType.APPLICATION_JSON_TYPE)).findFirst().orElse(null) : null;
}
@NonNull
@Override
public final RuntimePersistentEntity getEntity(@NonNull Class type) {
ArgumentUtils.requireNonNull("type", type);
RuntimePersistentEntity entity = entities.get(type);
if (entity == null) {
entity = new RuntimePersistentEntity(type) {
@Override
protected RuntimePersistentEntity getEntity(Class type) {
return AbstractSqlRepositoryOperations.this.getEntity(type);
}
};
entities.put(type, entity);
}
return entity;
}
/**
* Sets the insert parameters for the given insert, entity and statement.
*
* @param insert The insert
* @param entity The entity
* @param stmt The statement
* @param The entity type
*/
protected final void setInsertParameters(@NonNull StoredInsert insert, @NonNull T entity, @NonNull PS stmt) {
Object now = null;
RuntimePersistentEntity persistentEntity = insert.getPersistentEntity();
final String[] parameterBinding = insert.getParameterBinding();
for (int i = 0; i < parameterBinding.length; i++) {
String path = parameterBinding[i];
RuntimePersistentProperty prop = persistentEntity.getPropertyByName(path);
if (prop == null) {
int j = path.indexOf('.');
if (j > -1) {
RuntimePersistentProperty embeddedProp = (RuntimePersistentProperty)
persistentEntity.getPropertyByPath(path).orElse(null);
if (embeddedProp != null) {
// embedded case
prop = persistentEntity.getPropertyByName(path.substring(0, j));
if (prop instanceof Association) {
Association assoc = (Association) prop;
if (assoc.getKind() == Relation.Kind.EMBEDDED) {
Object value = prop.getProperty().get(entity);
Object embeddedValue = value != null ? embeddedProp.getProperty().get(value) : null;
int index = i + 1;
preparedStatementWriter.setDynamic(
stmt,
index,
embeddedProp.getDataType(),
embeddedValue
);
}
}
}
}
} else {
DataType type = prop.getDataType();
BeanProperty beanProperty = (BeanProperty) prop.getProperty();
Object value = beanProperty.get(entity);
int index = i + 1;
if (prop instanceof Association) {
Association association = (Association) prop;
if (!association.isForeignKey()) {
@SuppressWarnings("unchecked")
RuntimePersistentEntity