|
| 1 | +package tech.ydb.data.repository.support; |
| 2 | + |
| 3 | +import java.lang.reflect.Method; |
| 4 | +import java.util.Optional; |
| 5 | + |
| 6 | +import org.apache.commons.logging.Log; |
| 7 | +import org.apache.commons.logging.LogFactory; |
| 8 | +import org.springframework.beans.factory.BeanFactory; |
| 9 | +import org.springframework.context.ApplicationEventPublisher; |
| 10 | +import org.springframework.data.jdbc.core.convert.DataAccessStrategy; |
| 11 | +import org.springframework.data.jdbc.core.convert.JdbcConverter; |
| 12 | +import org.springframework.data.jdbc.repository.QueryMappingConfiguration; |
| 13 | +import org.springframework.data.jdbc.repository.query.JdbcQueryMethod; |
| 14 | +import org.springframework.data.jdbc.repository.query.PartTreeJdbcQuery; |
| 15 | +import org.springframework.data.jdbc.repository.query.RowMapperFactory; |
| 16 | +import org.springframework.data.jdbc.repository.query.StringBasedJdbcQuery; |
| 17 | +import org.springframework.data.jdbc.repository.support.BeanFactoryAwareRowMapperFactory; |
| 18 | +import org.springframework.data.jdbc.repository.support.JdbcQueryLookupStrategy; |
| 19 | +import org.springframework.data.jdbc.repository.support.JdbcRepositoryFactory; |
| 20 | +import org.springframework.data.mapping.callback.EntityCallbacks; |
| 21 | +import org.springframework.data.projection.ProjectionFactory; |
| 22 | +import org.springframework.data.relational.core.dialect.Dialect; |
| 23 | +import org.springframework.data.relational.core.mapping.RelationalMappingContext; |
| 24 | +import org.springframework.data.repository.core.NamedQueries; |
| 25 | +import org.springframework.data.repository.core.RepositoryMetadata; |
| 26 | +import org.springframework.data.repository.query.CachingValueExpressionDelegate; |
| 27 | +import org.springframework.data.repository.query.QueryLookupStrategy; |
| 28 | +import org.springframework.data.repository.query.RepositoryQuery; |
| 29 | +import org.springframework.data.repository.query.ValueExpressionDelegate; |
| 30 | +import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations; |
| 31 | +import org.springframework.lang.Nullable; |
| 32 | + |
| 33 | +import tech.ydb.data.core.YdbQueryMethod; |
| 34 | + |
| 35 | +/** |
| 36 | + * Custom {@link JdbcRepositoryFactory repository factory} to allow for query tuning. |
| 37 | + * |
| 38 | + * @author Mikhail Polivakha |
| 39 | + */ |
| 40 | +public class YdbRepositoryFactory extends JdbcRepositoryFactory { |
| 41 | + |
| 42 | + private final EntityCallbacks entityCallbacks; |
| 43 | + private final QueryMappingConfiguration queryMappingConfiguration; |
| 44 | + |
| 45 | + private static final Log LOG = LogFactory.getLog(JdbcQueryLookupStrategy.class); |
| 46 | + |
| 47 | + /** |
| 48 | + * Creates a new {@link JdbcRepositoryFactory} for the given {@link DataAccessStrategy}, |
| 49 | + * {@link RelationalMappingContext} and {@link ApplicationEventPublisher}. |
| 50 | + * |
| 51 | + * @param dataAccessStrategy must not be {@literal null}. |
| 52 | + * @param context must not be {@literal null}. |
| 53 | + * @param converter must not be {@literal null}. |
| 54 | + * @param dialect must not be {@literal null}. |
| 55 | + * @param publisher must not be {@literal null}. |
| 56 | + * @param operations must not be {@literal null}. |
| 57 | + */ |
| 58 | + public YdbRepositoryFactory( |
| 59 | + DataAccessStrategy dataAccessStrategy, |
| 60 | + RelationalMappingContext context, |
| 61 | + JdbcConverter converter, |
| 62 | + Dialect dialect, |
| 63 | + ApplicationEventPublisher publisher, |
| 64 | + NamedParameterJdbcOperations operations, |
| 65 | + EntityCallbacks entityCallbacks, |
| 66 | + QueryMappingConfiguration queryMappingConfiguration |
| 67 | + ) { |
| 68 | + super(dataAccessStrategy, context, converter, dialect, publisher, operations); |
| 69 | + |
| 70 | + this.entityCallbacks = entityCallbacks; |
| 71 | + this.queryMappingConfiguration = queryMappingConfiguration; |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + protected Optional<QueryLookupStrategy> getQueryLookupStrategy(QueryLookupStrategy.Key key, ValueExpressionDelegate valueExpressionDelegate) { |
| 76 | + return Optional.of( |
| 77 | + new YdbQueryLookupStrategy( |
| 78 | + publisher, |
| 79 | + entityCallbacks, |
| 80 | + context, |
| 81 | + converter, |
| 82 | + dialect, |
| 83 | + beanFactory, |
| 84 | + queryMappingConfiguration, |
| 85 | + operations, |
| 86 | + new CachingValueExpressionDelegate(valueExpressionDelegate) |
| 87 | + ) |
| 88 | + ); |
| 89 | + } |
| 90 | + |
| 91 | + static class YdbQueryLookupStrategy extends JdbcQueryLookupStrategy { |
| 92 | + |
| 93 | + private final RowMapperFactory rowMapperFactory; |
| 94 | + |
| 95 | + public YdbQueryLookupStrategy( |
| 96 | + ApplicationEventPublisher publisher, @Nullable EntityCallbacks callbacks, |
| 97 | + RelationalMappingContext context, JdbcConverter converter, Dialect dialect, BeanFactory beanFactory, |
| 98 | + QueryMappingConfiguration queryMappingConfiguration, NamedParameterJdbcOperations operations, |
| 99 | + ValueExpressionDelegate delegate |
| 100 | + ) { |
| 101 | + super(publisher, callbacks, context, converter, dialect, queryMappingConfiguration, operations, delegate); |
| 102 | + |
| 103 | + this.rowMapperFactory = new BeanFactoryAwareRowMapperFactory(context, converter, queryMappingConfiguration, callbacks, publisher, beanFactory); |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public RepositoryQuery resolveQuery( |
| 108 | + Method method, |
| 109 | + RepositoryMetadata repositoryMetadata, |
| 110 | + ProjectionFactory projectionFactory, |
| 111 | + NamedQueries namedQueries |
| 112 | + ) { |
| 113 | + JdbcQueryMethod queryMethod = new YdbQueryMethod(method, repositoryMetadata, projectionFactory, namedQueries, getMappingContext()); |
| 114 | + |
| 115 | + if (namedQueries.hasQuery(queryMethod.getNamedQueryName()) || queryMethod.hasAnnotatedQuery()) { |
| 116 | + |
| 117 | + if (queryMethod.hasAnnotatedQuery() && queryMethod.hasAnnotatedQueryName()) { |
| 118 | + LOG.warn(String.format( |
| 119 | + "Query method %s is annotated with both, a query and a query name; Using the declared query", method)); |
| 120 | + } |
| 121 | + |
| 122 | + String queryString = evaluateTableExpressions(repositoryMetadata, queryMethod.getRequiredQuery()); |
| 123 | + |
| 124 | + return new StringBasedJdbcQuery(queryString, queryMethod, getOperations(), rowMapperFactory, getConverter(), |
| 125 | + delegate); |
| 126 | + } else { |
| 127 | + return new PartTreeJdbcQuery(getMappingContext(), queryMethod, getDialect(), getConverter(), getOperations(), rowMapperFactory); |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | +} |
0 commit comments