From 4b1165bd72e13658b309f9f1d70fc2820295ba81 Mon Sep 17 00:00:00 2001 From: Alexander Lavrukov Date: Thu, 5 Dec 2024 18:09:41 +0300 Subject: [PATCH] TableDescriptor in Statements (#106) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit There is only one change - I create a TableDescriptor record and pass it to the only one сщтыекгсещк ща YqlStatement, what means that all statements have to get TableDescriptor in their constructor Co-authored-by: Alexander Lavrukov --- .../ydb/merge/ByEntityYqlQueriesMerger.java | 13 +- .../ydb/statement/CountAllStatement.java | 7 +- .../ydb/statement/DeleteAllStatement.java | 5 +- .../ydb/statement/DeleteByIdStatement.java | 5 +- .../ydb/statement/FindAllYqlStatement.java | 8 +- .../ydb/statement/FindInStatement.java | 24 ++- .../ydb/statement/FindRangeStatement.java | 10 +- .../ydb/statement/FindStatement.java | 7 +- .../ydb/statement/FindYqlStatement.java | 8 +- .../ydb/statement/InsertYqlStatement.java | 5 +- .../statement/MultipleVarsYqlStatement.java | 11 +- .../ydb/statement/PredicateStatement.java | 4 +- .../ydb/statement/UpdateByIdStatement.java | 7 +- .../ydb/statement/UpdateInStatement.java | 4 +- .../ydb/statement/UpsertYqlStatement.java | 5 +- .../ydb/statement/YqlStatement.java | 11 +- .../yoj/repository/ydb/table/YdbTable.java | 58 +++++--- .../ydb/YdbRepositoryCacheTest.java | 8 +- .../ydb/YdbRepositoryIntegrationTest.java | 17 ++- .../ydb/merge/QueriesMergerTest.java | 20 ++- .../statement/DeleteByIdStatementTest.java | 6 +- .../FindInStatementIntegrationTest.java | 138 ++++++++++++------ .../ydb/statement/InsertYqlStatementTest.java | 6 +- .../ydb/statement/UpsertYqlStatementTest.java | 6 +- .../yoj/repository/db/TableDescriptor.java | 10 ++ 25 files changed, 278 insertions(+), 125 deletions(-) create mode 100644 repository/src/main/java/tech/ydb/yoj/repository/db/TableDescriptor.java diff --git a/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/merge/ByEntityYqlQueriesMerger.java b/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/merge/ByEntityYqlQueriesMerger.java index eab4279c..1c799723 100644 --- a/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/merge/ByEntityYqlQueriesMerger.java +++ b/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/merge/ByEntityYqlQueriesMerger.java @@ -7,6 +7,7 @@ import org.slf4j.LoggerFactory; import tech.ydb.yoj.repository.db.Entity; import tech.ydb.yoj.repository.db.EntitySchema; +import tech.ydb.yoj.repository.db.TableDescriptor; import tech.ydb.yoj.repository.db.cache.RepositoryCache; import tech.ydb.yoj.repository.db.exception.EntityAlreadyExistsException; import tech.ydb.yoj.repository.ydb.YdbRepository; @@ -161,12 +162,20 @@ private MergingState doTransition(MergingState state, Statement.QueryType nextQu @SuppressWarnings("unchecked") private static YdbRepository.Query convertInsertToUpsert(YdbRepository.Query query) { - return new YdbRepository.Query<>(new UpsertYqlStatement<>(EntitySchema.of(getEntityClass(query))), query.getValues().get(0)); + var type = getEntityClass(query); + var schema = EntitySchema.of(type); + var tableDescriptor = TableDescriptor.from(schema); + var statement = new UpsertYqlStatement<>(tableDescriptor, schema); + return new YdbRepository.Query<>(statement, query.getValues().get(0)); } @SuppressWarnings("unchecked") private static YdbRepository.Query convertInsertToDelete(YdbRepository.Query query) { - return new YdbRepository.Query<>(new DeleteByIdStatement<>(EntitySchema.of(getEntityClass(query))), getEntityId(query)); + var type = getEntityClass(query); + var schema = EntitySchema.of(type); + var tableDescriptor = TableDescriptor.from(schema); + var statement = new DeleteByIdStatement<>(tableDescriptor, schema); + return new YdbRepository.Query<>(statement, getEntityId(query)); } private static Entity.Id getEntityId(YdbRepository.Query query) { diff --git a/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/CountAllStatement.java b/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/CountAllStatement.java index a054e0bb..cbe36d52 100644 --- a/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/CountAllStatement.java +++ b/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/CountAllStatement.java @@ -3,6 +3,7 @@ import tech.ydb.yoj.databind.schema.ObjectSchema; import tech.ydb.yoj.repository.db.Entity; import tech.ydb.yoj.repository.db.EntitySchema; +import tech.ydb.yoj.repository.db.TableDescriptor; import tech.ydb.yoj.repository.ydb.yql.YqlPredicate; import tech.ydb.yoj.repository.ydb.yql.YqlStatementPart; @@ -15,8 +16,10 @@ public class CountAllStatement> extends PredicateStatement>, ENTITY, Count> { private final List> parts; - public CountAllStatement(EntitySchema schema, List> parts) { - super(schema, ObjectSchema.of(Count.class), parts, YqlPredicate::from); + public CountAllStatement( + TableDescriptor tableDescriptor, EntitySchema schema, List> parts + ) { + super(tableDescriptor, schema, ObjectSchema.of(Count.class), parts, YqlPredicate::from); this.parts = parts; } diff --git a/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/DeleteAllStatement.java b/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/DeleteAllStatement.java index 3552ccfc..e7a1b47a 100644 --- a/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/DeleteAllStatement.java +++ b/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/DeleteAllStatement.java @@ -2,10 +2,11 @@ import tech.ydb.yoj.repository.db.Entity; import tech.ydb.yoj.repository.db.EntitySchema; +import tech.ydb.yoj.repository.db.TableDescriptor; public class DeleteAllStatement> extends YqlStatement { - public DeleteAllStatement(EntitySchema schema) { - super(schema, schema); + public DeleteAllStatement(TableDescriptor tableDescriptor, EntitySchema schema) { + super(tableDescriptor, schema, schema); } @Override diff --git a/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/DeleteByIdStatement.java b/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/DeleteByIdStatement.java index bbec2595..f3172fcd 100644 --- a/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/DeleteByIdStatement.java +++ b/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/DeleteByIdStatement.java @@ -2,13 +2,14 @@ import tech.ydb.yoj.repository.db.Entity; import tech.ydb.yoj.repository.db.EntitySchema; +import tech.ydb.yoj.repository.db.TableDescriptor; import java.util.Map; import java.util.function.Function; public class DeleteByIdStatement> extends MultipleVarsYqlStatement.Simple { - public DeleteByIdStatement(EntitySchema schema) { - super(schema); + public DeleteByIdStatement(TableDescriptor tableDescriptor, EntitySchema schema) { + super(tableDescriptor, schema); } @Override diff --git a/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/FindAllYqlStatement.java b/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/FindAllYqlStatement.java index de530d53..e4a19a50 100644 --- a/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/FindAllYqlStatement.java +++ b/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/FindAllYqlStatement.java @@ -1,14 +1,16 @@ package tech.ydb.yoj.repository.ydb.statement; -import lombok.NonNull; import tech.ydb.yoj.databind.schema.Schema; import tech.ydb.yoj.repository.db.Entity; import tech.ydb.yoj.repository.db.EntitySchema; +import tech.ydb.yoj.repository.db.TableDescriptor; public class FindAllYqlStatement, RESULT> extends YqlStatement { - public FindAllYqlStatement(@NonNull EntitySchema schema, @NonNull Schema resultSchema) { - super(schema, resultSchema); + public FindAllYqlStatement( + TableDescriptor tableDescriptor, EntitySchema schema, Schema resultSchema + ) { + super(tableDescriptor, schema, resultSchema); } @Override diff --git a/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/FindInStatement.java b/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/FindInStatement.java index e79c5ba4..55bf091e 100644 --- a/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/FindInStatement.java +++ b/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/FindInStatement.java @@ -16,6 +16,7 @@ import tech.ydb.yoj.databind.schema.Schema.JavaField; import tech.ydb.yoj.repository.db.Entity; import tech.ydb.yoj.repository.db.EntitySchema; +import tech.ydb.yoj.repository.db.TableDescriptor; import tech.ydb.yoj.repository.ydb.yql.YqlListingQuery; import tech.ydb.yoj.repository.ydb.yql.YqlPredicate; import tech.ydb.yoj.repository.ydb.yql.YqlType; @@ -128,6 +129,7 @@ public final class FindInStatement, RESULT> extends Mult * in the {@code resultSchema} */ public static , T extends Entity, RESULT> FindInStatement, T, RESULT> from( + TableDescriptor tableDescriptor, EntitySchema schema, Schema resultSchema, Iterable ids, @@ -138,10 +140,13 @@ public static , T extends Entity, RESULT> FindInState var keySchema = schema.getIdSchema(); var keyFields = collectKeyFieldsFromIds(schema.getIdSchema(), ids); - return new FindInStatement<>(schema, resultSchema, keySchema, keyFields, null, filter, orderBy, limit); + return new FindInStatement<>( + tableDescriptor,schema, resultSchema, keySchema, keyFields, null, filter, orderBy, limit + ); } public static , RESULT> FindInStatement, T, RESULT> from( + TableDescriptor tableDescriptor, EntitySchema schema, Schema resultSchema, String indexName, @@ -153,10 +158,13 @@ public static , RESULT> FindInStatement, T, RESULT Schema keySchema = getKeySchemaFromValues(keys); Set keyFields = collectKeyFieldsFromKeys(schema, indexName, keySchema, keys); - return new FindInStatement<>(schema, resultSchema, keySchema, keyFields, indexName, filter, orderBy, limit); + return new FindInStatement<>( + tableDescriptor, schema, resultSchema, keySchema, keyFields, indexName, filter, orderBy, limit + ); } private FindInStatement( + TableDescriptor tableDescriptor, EntitySchema schema, Schema resultSchema, Schema keySchema, @@ -167,14 +175,18 @@ private FindInStatement( @Nullable Integer limit ) { - super(schema, resultSchema); + super(tableDescriptor, schema, resultSchema); this.indexName = indexName; this.orderBy = orderBy; this.limit = limit; this.keySchema = keySchema; this.keyFields = keyFields; - this.predicate = (filter == null) ? null : new PredicateClause<>(schema, YqlListingQuery.toYqlPredicate(filter)); + if (filter != null) { + this.predicate = new PredicateClause<>(tableDescriptor, schema, YqlListingQuery.toYqlPredicate(filter)); + } else { + this.predicate = null; + } validateOrderByFields(); } @@ -437,8 +449,8 @@ private boolean hasPredicate() { private static class PredicateClause> extends PredicateStatement, T, T> { private final YqlPredicate predicate; - public PredicateClause(EntitySchema schema, YqlPredicate predicate) { - super(schema, schema, Void.class, __ -> predicate); + public PredicateClause(TableDescriptor tableDescriptor, EntitySchema schema, YqlPredicate predicate) { + super(tableDescriptor, schema, schema, Void.class, __ -> predicate); this.predicate = predicate; } diff --git a/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/FindRangeStatement.java b/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/FindRangeStatement.java index a187bad1..d85775c4 100644 --- a/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/FindRangeStatement.java +++ b/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/FindRangeStatement.java @@ -7,6 +7,7 @@ import tech.ydb.yoj.repository.db.Entity; import tech.ydb.yoj.repository.db.EntitySchema; import tech.ydb.yoj.repository.db.Range; +import tech.ydb.yoj.repository.db.TableDescriptor; import tech.ydb.yoj.repository.ydb.yql.YqlType; import java.util.List; @@ -23,8 +24,13 @@ public class FindRangeStatement, ID extends Entity @Getter private final List params; - public FindRangeStatement(EntitySchema schema, Schema outSchema, Range range) { - super(schema, outSchema); + public FindRangeStatement( + TableDescriptor tableDescriptor, + EntitySchema schema, + Schema outSchema, + Range range + ) { + super(tableDescriptor, schema, outSchema); this.params = Stream.of(RangeBound.values()) .flatMap(b -> toParams(b.map(range).keySet(), b)) .collect(toList()); diff --git a/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/FindStatement.java b/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/FindStatement.java index c26ab3b3..c16388bb 100644 --- a/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/FindStatement.java +++ b/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/FindStatement.java @@ -4,6 +4,7 @@ import tech.ydb.yoj.databind.schema.Schema; import tech.ydb.yoj.repository.db.Entity; import tech.ydb.yoj.repository.db.EntitySchema; +import tech.ydb.yoj.repository.db.TableDescriptor; import tech.ydb.yoj.repository.ydb.yql.YqlOrderBy; import tech.ydb.yoj.repository.ydb.yql.YqlPredicate; import tech.ydb.yoj.repository.ydb.yql.YqlStatementPart; @@ -20,6 +21,7 @@ public class FindStatement, RESULT> extends Predic private final List> parts; public static , R> FindStatement from( + @NonNull TableDescriptor tableDescriptor, @NonNull EntitySchema schema, @NonNull Schema outSchema, @NonNull Collection> parts, @@ -32,16 +34,17 @@ public static , R> FindStatement from( } } - return new FindStatement<>(schema, outSchema, partsList, distinct); + return new FindStatement<>(tableDescriptor, schema, outSchema, partsList, distinct); } private FindStatement( + @NonNull TableDescriptor tableDescriptor, @NonNull EntitySchema schema, @NonNull Schema outSchema, @NonNull List> parts, boolean distinct ) { - super(schema, outSchema, parts, YqlPredicate::from); + super(tableDescriptor, schema, outSchema, parts, YqlPredicate::from); this.distinct = distinct; this.parts = parts; } diff --git a/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/FindYqlStatement.java b/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/FindYqlStatement.java index 09077543..c3fe402e 100644 --- a/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/FindYqlStatement.java +++ b/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/FindYqlStatement.java @@ -1,9 +1,9 @@ package tech.ydb.yoj.repository.ydb.statement; -import lombok.NonNull; import tech.ydb.yoj.databind.schema.Schema; import tech.ydb.yoj.repository.db.Entity; import tech.ydb.yoj.repository.db.EntitySchema; +import tech.ydb.yoj.repository.db.TableDescriptor; import tech.ydb.yoj.repository.db.cache.RepositoryCache; import tech.ydb.yoj.repository.ydb.yql.YqlType; @@ -13,8 +13,10 @@ import static java.util.stream.Collectors.toList; public class FindYqlStatement, RESULT> extends YqlStatement { - public FindYqlStatement(@NonNull EntitySchema schema, @NonNull Schema resultSchema) { - super(schema, resultSchema); + public FindYqlStatement( + TableDescriptor tableDescriptor, EntitySchema schema, Schema resultSchema + ) { + super(tableDescriptor, schema, resultSchema); } @Override diff --git a/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/InsertYqlStatement.java b/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/InsertYqlStatement.java index c9f25d9a..fee20394 100644 --- a/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/InsertYqlStatement.java +++ b/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/InsertYqlStatement.java @@ -2,13 +2,14 @@ import tech.ydb.yoj.repository.db.Entity; import tech.ydb.yoj.repository.db.EntitySchema; +import tech.ydb.yoj.repository.db.TableDescriptor; import java.util.Map; import java.util.function.Function; public class InsertYqlStatement> extends MultipleVarsYqlStatement.Simple { - public InsertYqlStatement(EntitySchema schema) { - super(schema); + public InsertYqlStatement(TableDescriptor tableDescriptor, EntitySchema schema) { + super(tableDescriptor, schema); } @Override diff --git a/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/MultipleVarsYqlStatement.java b/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/MultipleVarsYqlStatement.java index ae5cf373..32645b2a 100644 --- a/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/MultipleVarsYqlStatement.java +++ b/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/MultipleVarsYqlStatement.java @@ -4,6 +4,7 @@ import tech.ydb.yoj.databind.schema.Schema; import tech.ydb.yoj.repository.db.Entity; import tech.ydb.yoj.repository.db.EntitySchema; +import tech.ydb.yoj.repository.db.TableDescriptor; import tech.ydb.yoj.repository.ydb.yql.YqlType; import java.util.Collection; @@ -21,8 +22,10 @@ public abstract class MultipleVarsYqlStatement, RESULT> extends YqlStatement { public static final String listName = "$Input"; - public MultipleVarsYqlStatement(EntitySchema schema, Schema resultSchema) { - super(schema, resultSchema); + public MultipleVarsYqlStatement( + TableDescriptor tableDescriptor, EntitySchema schema, Schema resultSchema + ) { + super(tableDescriptor, schema, resultSchema); } @Override @@ -92,8 +95,8 @@ protected String toDebugParams(PARAMS params) { public abstract static class Simple> extends MultipleVarsYqlStatement { - public Simple(EntitySchema schema) { - super(schema, schema); + public Simple(TableDescriptor tableDescriptor, EntitySchema schema) { + super(tableDescriptor, schema, schema); } } } diff --git a/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/PredicateStatement.java b/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/PredicateStatement.java index b5573cb5..6041ede3 100644 --- a/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/PredicateStatement.java +++ b/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/PredicateStatement.java @@ -10,6 +10,7 @@ import tech.ydb.yoj.databind.schema.Schema.JavaField; import tech.ydb.yoj.repository.db.Entity; import tech.ydb.yoj.repository.db.EntitySchema; +import tech.ydb.yoj.repository.db.TableDescriptor; import tech.ydb.yoj.repository.ydb.yql.YqlCompositeType; import tech.ydb.yoj.repository.ydb.yql.YqlPredicate; import tech.ydb.yoj.repository.ydb.yql.YqlPredicateParam; @@ -34,12 +35,13 @@ public abstract class PredicateStatement, private final Map predParams; public PredicateStatement( + @NonNull TableDescriptor tableDescriptor, @NonNull EntitySchema schema, @NonNull Schema outSchema, @NonNull PARAMS params, @NonNull Function getPredicate ) { - super(schema, outSchema); + super(tableDescriptor, schema, outSchema); this.getPredicate = getPredicate; YqlPredicate pred = getPredicate.apply(params); diff --git a/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/UpdateByIdStatement.java b/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/UpdateByIdStatement.java index 8c1646cb..3612d581 100644 --- a/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/UpdateByIdStatement.java +++ b/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/UpdateByIdStatement.java @@ -3,6 +3,7 @@ import tech.ydb.proto.ValueProtos; import tech.ydb.yoj.repository.db.Entity; import tech.ydb.yoj.repository.db.EntitySchema; +import tech.ydb.yoj.repository.db.TableDescriptor; import tech.ydb.yoj.repository.ydb.yql.YqlType; import java.util.Collection; @@ -24,8 +25,10 @@ public final class UpdateByIdStatement, ID extends private final Set idParams; - public UpdateByIdStatement(EntitySchema schema, UpdateModel.ById model) { - super(schema, schema); + public UpdateByIdStatement( + TableDescriptor tableDescriptor, EntitySchema schema, UpdateModel.ById model + ) { + super(tableDescriptor, schema, schema); this.idParams = schema.flattenId().stream() .map(c -> YqlStatementParam.required(YqlType.of(c), c.getName())) .collect(toUnmodifiableSet()); diff --git a/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/UpdateInStatement.java b/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/UpdateInStatement.java index 15a07f58..77b6c0d3 100644 --- a/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/UpdateInStatement.java +++ b/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/UpdateInStatement.java @@ -8,6 +8,7 @@ import tech.ydb.yoj.databind.schema.Schema.JavaField; import tech.ydb.yoj.repository.db.Entity; import tech.ydb.yoj.repository.db.EntitySchema; +import tech.ydb.yoj.repository.db.TableDescriptor; import tech.ydb.yoj.repository.ydb.yql.YqlType; import java.util.Collection; @@ -33,11 +34,12 @@ public class UpdateInStatement, RESULT> private final Set keyFields; public UpdateInStatement( + TableDescriptor tableDescriptor, EntitySchema schema, Schema resultSchema, UpdateInStatementInput in ) { - super(schema, resultSchema); + super(tableDescriptor, schema, resultSchema); this.keyFields = collectKeyFields(in.ids); this.values = new HashMap<>(in.values.size()); diff --git a/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/UpsertYqlStatement.java b/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/UpsertYqlStatement.java index b2eceeb5..66dd1ca4 100644 --- a/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/UpsertYqlStatement.java +++ b/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/UpsertYqlStatement.java @@ -2,13 +2,14 @@ import tech.ydb.yoj.repository.db.Entity; import tech.ydb.yoj.repository.db.EntitySchema; +import tech.ydb.yoj.repository.db.TableDescriptor; import java.util.Map; import java.util.function.Function; public class UpsertYqlStatement> extends MultipleVarsYqlStatement.Simple { - public UpsertYqlStatement(EntitySchema schema) { - super(schema); + public UpsertYqlStatement(TableDescriptor tableDescriptor, EntitySchema schema) { + super(tableDescriptor, schema); } @Override diff --git a/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/YqlStatement.java b/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/YqlStatement.java index 361d7ed5..3f5c5115 100644 --- a/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/YqlStatement.java +++ b/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/YqlStatement.java @@ -8,6 +8,7 @@ import tech.ydb.yoj.repository.db.Entity; import tech.ydb.yoj.repository.db.EntityIdSchema; import tech.ydb.yoj.repository.db.EntitySchema; +import tech.ydb.yoj.repository.db.TableDescriptor; import tech.ydb.yoj.repository.db.cache.RepositoryCache; import tech.ydb.yoj.repository.ydb.yql.YqlOrderBy; import tech.ydb.yoj.repository.ydb.yql.YqlStatementPart; @@ -39,15 +40,13 @@ public abstract class YqlStatement, RESULT protected final ResultSetReader resultSetReader; protected final String tableName; - public YqlStatement(@NonNull EntitySchema schema, @NonNull Schema resultSchema) { - this(schema, resultSchema, schema.getName()); - } - - public YqlStatement(@NonNull EntitySchema schema, @NonNull Schema resultSchema, @NonNull String tableName) { + public YqlStatement( + TableDescriptor tableDescriptor, EntitySchema schema, Schema resultSchema + ) { this.schema = schema; this.resultSchema = resultSchema; this.resultSetReader = new ResultSetReader<>(resultSchema); - this.tableName = tableName; + this.tableName = tableDescriptor.tableName(); } @Override diff --git a/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/table/YdbTable.java b/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/table/YdbTable.java index ed8afec7..405f38f2 100644 --- a/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/table/YdbTable.java +++ b/repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/table/YdbTable.java @@ -13,6 +13,7 @@ import tech.ydb.yoj.repository.db.EntitySchema; import tech.ydb.yoj.repository.db.Range; import tech.ydb.yoj.repository.db.Table; +import tech.ydb.yoj.repository.db.TableDescriptor; import tech.ydb.yoj.repository.db.Tx; import tech.ydb.yoj.repository.db.ViewSchema; import tech.ydb.yoj.repository.db.bulk.BulkParams; @@ -68,17 +69,20 @@ public class YdbTable> implements Table { private final Class type; private final QueryExecutor executor; private final EntitySchema schema; + private final TableDescriptor tableDescriptor; public YdbTable(Class type, QueryExecutor executor) { this.type = type; this.executor = new CheckingQueryExecutor(executor); this.schema = EntitySchema.of(type); + this.tableDescriptor = TableDescriptor.from(schema); } protected YdbTable(QueryExecutor executor) { this.type = resolveEntityType(); this.executor = new CheckingQueryExecutor(executor); this.schema = EntitySchema.of(type); + this.tableDescriptor = TableDescriptor.from(schema); } @SuppressWarnings("unchecked") @@ -94,7 +98,7 @@ private static List toList(E first, E... rest) { @Override public List findAll() { - var statement = new FindAllYqlStatement<>(schema, schema); + var statement = new FindAllYqlStatement<>(tableDescriptor, schema, schema); return postLoad(executor.execute(statement, null)); } @@ -186,13 +190,13 @@ protected List find(YqlStatementPart part, YqlStatementPart... otherPa @Override public List findAll(Class viewType) { ViewSchema viewSchema = ViewSchema.of(viewType); - var statement = new FindAllYqlStatement<>(schema, viewSchema); + var statement = new FindAllYqlStatement<>(tableDescriptor, schema, viewSchema); return executor.execute(statement, null); } @Override public void deleteAll() { - executor.pendingExecute(new DeleteAllStatement<>(schema), null); + executor.pendingExecute(new DeleteAllStatement<>(tableDescriptor, schema), null); } @Override @@ -236,7 +240,7 @@ public T find(Entity.Id id) { throw new IllegalArgumentException("Cannot use partial id in find method"); } return executor.getTransactionLocal().firstLevelCache().get(id, __ -> { - var statement = new FindYqlStatement<>(schema, schema); + var statement = new FindYqlStatement<>(tableDescriptor, schema, schema); List res = postLoad(executor.execute(statement, id)); return res.isEmpty() ? null : res.get(0); }); @@ -245,21 +249,21 @@ public T find(Entity.Id id) { @Override public V find(Class viewType, Entity.Id id) { ViewSchema viewSchema = ViewSchema.of(viewType); - var statement = new FindYqlStatement<>(schema, viewSchema); + var statement = new FindYqlStatement<>(tableDescriptor, schema, viewSchema); List res = executor.execute(statement, id); return res.isEmpty() ? null : res.get(0); } @Override public > List find(Range range) { - var statement = new FindRangeStatement<>(schema, schema, range); + var statement = new FindRangeStatement<>(tableDescriptor, schema, schema, range); return postLoad(executor.execute(statement, range)); } @Override public > List find(Class viewType, Range range) { ViewSchema viewSchema = ViewSchema.of(viewType); - var statement = new FindRangeStatement<>(schema, viewSchema, range); + var statement = new FindRangeStatement<>(tableDescriptor, schema, viewSchema, range); return executor.execute(statement, range); } @@ -273,7 +277,7 @@ public final List find(YqlStatementPart part, YqlStatementPart... other } public List find(Collection> parts) { - var statement = FindStatement.from(schema, schema, parts, false); + var statement = FindStatement.from(tableDescriptor, schema, schema, parts, false); return postLoad(executor.execute(statement, parts)); } @@ -342,7 +346,7 @@ public > List findUncached(Set ids, @Nullable Fil if (ids.isEmpty()) { return List.of(); } - var statement = FindInStatement.from(schema, schema, ids, filter, orderBy, limit); + var statement = FindInStatement.from(tableDescriptor, schema, schema, ids, filter, orderBy, limit); return executor.execute(statement, ids); } @@ -352,7 +356,9 @@ public > List find(Class viewType, Set viewSchema = ViewSchema.of(viewType); - var statement = FindInStatement.from(schema, viewSchema, ids, filter, orderBy, limit); + var statement = FindInStatement.from( + tableDescriptor, schema, viewSchema, ids, filter, orderBy, limit + ); return executor.execute(statement, ids); } @@ -361,7 +367,9 @@ public List find(String indexName, Set keys, @Nullable FilterExpressio if (keys.isEmpty()) { return List.of(); } - var statement = FindInStatement.from(schema, schema, indexName, keys, filter, orderBy, limit); + var statement = FindInStatement.from( + tableDescriptor, schema, schema, indexName, keys, filter, orderBy, limit + ); return postLoad(executor.execute(statement, keys)); } @@ -371,7 +379,9 @@ public List find(Class viewType, String indexName, Set return List.of(); } ViewSchema viewSchema = ViewSchema.of(viewType); - var statement = FindInStatement.from(schema, viewSchema, indexName, keys, filter, orderBy, limit); + var statement = FindInStatement.from( + tableDescriptor, schema, viewSchema, indexName, keys, filter, orderBy, limit + ); return executor.execute(statement, keys); } @@ -403,7 +413,7 @@ public static > List... parts) { List> partsList = asList(parts); - var statement = new CountAllStatement<>(schema, partsList); + var statement = new CountAllStatement<>(tableDescriptor, schema, partsList); return executor.execute(statement, partsList).get(0).getCount(); } @@ -413,7 +423,7 @@ public List find(Class viewType, YqlStatementPart part public List find(Class viewType, Collection> parts, boolean distinct) { ViewSchema viewSchema = ViewSchema.of(viewType); - var statement = FindStatement.from(schema, viewSchema, parts, distinct); + var statement = FindStatement.from(tableDescriptor, schema, viewSchema, parts, distinct); return executor.execute(statement, parts); } @@ -423,14 +433,14 @@ public > List findIds(YqlStatementPart part, YqlS private > List findIds(Collection> parts) { EntityIdSchema idSchema = EntityIdSchema.ofEntity(type); - var statement = FindStatement.from(schema, idSchema, parts, false); + var statement = FindStatement.from(tableDescriptor, schema, idSchema, parts, false); return executor.execute(statement, parts); } @Override public > List findIds(Range range) { EntityIdSchema idSchema = EntityIdSchema.ofEntity(type); - var statement = new FindRangeStatement<>(schema, idSchema, range); + var statement = new FindRangeStatement<>(tableDescriptor, schema, idSchema, range); return executor.execute(statement, range); } @@ -441,20 +451,20 @@ public > List findIds(Set partialIds) { } OrderExpression order = defaultOrder(type); EntityIdSchema idSchema = EntityIdSchema.ofEntity(type); - var statement = FindInStatement.from(schema, idSchema, partialIds, null, order, null); + var statement = FindInStatement.from(tableDescriptor, schema, idSchema, partialIds, null, order, null); return executor.execute(statement, partialIds); } @Override public void update(Entity.Id id, Changeset changeset) { UpdateModel.ById> model = new UpdateModel.ById<>(id, changeset.toMap()); - executor.pendingExecute(new UpdateByIdStatement<>(schema, model), model); + executor.pendingExecute(new UpdateByIdStatement<>(tableDescriptor, schema, model), model); } @Override public T insert(T t) { T entityToSave = t.preSave(); - executor.pendingExecute(new InsertYqlStatement<>(schema), entityToSave); + executor.pendingExecute(new InsertYqlStatement<>(tableDescriptor, schema), entityToSave); executor.getTransactionLocal().firstLevelCache().put(entityToSave); executor.getTransactionLocal().projectionCache().save(entityToSave); return t; @@ -463,7 +473,7 @@ public T insert(T t) { @Override public T save(T t) { T entityToSave = t.preSave(); - executor.pendingExecute(new UpsertYqlStatement<>(schema), entityToSave); + executor.pendingExecute(new UpsertYqlStatement<>(tableDescriptor, schema), entityToSave); executor.getTransactionLocal().firstLevelCache().put(entityToSave); executor.getTransactionLocal().projectionCache().save(entityToSave); return t; @@ -471,7 +481,7 @@ public T save(T t) { @Override public void delete(Entity.Id id) { - executor.pendingExecute(new DeleteByIdStatement<>(schema), id); + executor.pendingExecute(new DeleteByIdStatement<>(tableDescriptor, schema), id); executor.getTransactionLocal().firstLevelCache().putEmpty(id); executor.getTransactionLocal().projectionCache().delete(id); } @@ -486,14 +496,14 @@ public void delete(Entity.Id id) { * @param entity ID type */ public > void migrate(ID id) { - var statement = new FindYqlStatement<>(schema, schema); + var statement = new FindYqlStatement<>(tableDescriptor, schema, schema); List foundRaw = executor.execute(statement, id); if (foundRaw.isEmpty()) { return; } T rawEntity = foundRaw.get(0); T entityToSave = rawEntity.postLoad().preSave(); - executor.pendingExecute(new UpsertYqlStatement<>(schema), entityToSave); + executor.pendingExecute(new UpsertYqlStatement<>(tableDescriptor, schema), entityToSave); executor.getTransactionLocal().projectionCache().save(entityToSave); } @@ -583,7 +593,7 @@ public > void updateIn(Collection ids, Changeset changeset) var params = new UpdateInStatement.UpdateInStatementInput<>(ids, changeset.toMap()); executor.pendingExecute( - new UpdateInStatement<>(schema, schema, params), + new UpdateInStatement<>(tableDescriptor, schema, schema, params), params ); } diff --git a/repository-ydb-v2/src/test/java/tech/ydb/yoj/repository/ydb/YdbRepositoryCacheTest.java b/repository-ydb-v2/src/test/java/tech/ydb/yoj/repository/ydb/YdbRepositoryCacheTest.java index 25393589..98147feb 100644 --- a/repository-ydb-v2/src/test/java/tech/ydb/yoj/repository/ydb/YdbRepositoryCacheTest.java +++ b/repository-ydb-v2/src/test/java/tech/ydb/yoj/repository/ydb/YdbRepositoryCacheTest.java @@ -16,6 +16,7 @@ import tech.ydb.table.values.StructType; import tech.ydb.yoj.repository.db.EntitySchema; import tech.ydb.yoj.repository.db.Range; +import tech.ydb.yoj.repository.db.TableDescriptor; import tech.ydb.yoj.repository.db.exception.EntityAlreadyExistsException; import tech.ydb.yoj.repository.test.sample.TestEntityOperations; import tech.ydb.yoj.repository.test.sample.model.Complex; @@ -323,8 +324,10 @@ private List createComplexesList() { @NonNull private CompletableFuture> convertEntity(List complexes) { ValueProtos.ResultSet.Builder builder = ValueProtos.ResultSet.newBuilder(); + EntitySchema schema = EntitySchema.of(Complex.class); + TableDescriptor tableDescriptor = TableDescriptor.from(schema); complexes.stream() - .map(complex -> new UpsertYqlStatement<>(EntitySchema.of(Complex.class)).toQueryParameters(complex)) + .map(complex -> new UpsertYqlStatement<>(tableDescriptor, schema).toQueryParameters(complex)) .map(map -> YdbConverter.convertToParams(map).values().get(MultipleVarsYqlStatement.listName)) .peek(value -> { if (builder.getColumnsCount() == 0) { @@ -353,7 +356,8 @@ private CompletableFuture> convertEntity(List c private Params convertId(Id id) { EntitySchema schema = EntitySchema.of(Complex.class); - var statement = new FindYqlStatement<>(schema, schema); + TableDescriptor tableDescriptor = TableDescriptor.from(schema); + var statement = new FindYqlStatement<>(tableDescriptor, schema, schema); return YdbConverter.convertToParams(statement.toQueryParameters(id)); } } diff --git a/repository-ydb-v2/src/test/java/tech/ydb/yoj/repository/ydb/YdbRepositoryIntegrationTest.java b/repository-ydb-v2/src/test/java/tech/ydb/yoj/repository/ydb/YdbRepositoryIntegrationTest.java index dd7fef28..6eb977a6 100644 --- a/repository-ydb-v2/src/test/java/tech/ydb/yoj/repository/ydb/YdbRepositoryIntegrationTest.java +++ b/repository-ydb-v2/src/test/java/tech/ydb/yoj/repository/ydb/YdbRepositoryIntegrationTest.java @@ -37,6 +37,7 @@ import tech.ydb.yoj.repository.db.IsolationLevel; import tech.ydb.yoj.repository.db.Repository; import tech.ydb.yoj.repository.db.RepositoryTransaction; +import tech.ydb.yoj.repository.db.TableDescriptor; import tech.ydb.yoj.repository.db.Tx; import tech.ydb.yoj.repository.db.bulk.BulkParams; import tech.ydb.yoj.repository.db.exception.ConversionException; @@ -83,7 +84,6 @@ import java.time.Duration; import java.util.ArrayList; import java.util.Arrays; -import java.util.Collection; import java.util.List; import java.util.Map; import java.util.Objects; @@ -213,9 +213,10 @@ public void readYqlListAndMap() { ); db.tx(() -> db.table(WithUnflattenableField.class).insert(entity)); db.tx(() -> { + EntitySchema schema = EntitySchema.of(WithUnflattenableField.class); + var tableDescriptor = TableDescriptor.from(schema); List result = ((YdbRepositoryTransaction) Tx.Current.get().getRepositoryTransaction()) - .execute(new YqlStatement<>(EntitySchema.of(WithUnflattenableField.class), - ObjectSchema.of(GroupByResult.class)) { + .execute(new YqlStatement<>(tableDescriptor, schema, ObjectSchema.of(GroupByResult.class)) { @Override public String getQuery(String tablespace) { return "PRAGMA TablePathPrefix = \"" + tablespace + "\";" + @@ -887,15 +888,17 @@ public void complexIdLtYsingYqlPredicate() { assertThat(db.tx(() -> db.supabubbles2().findLessThan(sc.getId()))).containsOnly(sa, sb); } - private void executeQuery(String expectSqlQuery, List expectRows, - Collection> query) { + private void executeQuery(String expectSqlQuery, List expectRows, List> parts) { EntitySchema schema = EntitySchema.of(IndexedEntity.class); - var statement = FindStatement.from(schema, schema, new ArrayList<>(query), false); + TableDescriptor tableDescriptor = TableDescriptor.from(schema); + var statement = FindStatement.from( + tableDescriptor, schema, schema, parts, false + ); var sqlQuery = statement.getQuery("ts/"); assertEquals(expectSqlQuery, sqlQuery); // Check we use index and query was not failed - var actual = db.tx(() -> ((YdbTable) db.indexedTable()).find(new ArrayList<>(query))); + var actual = db.tx(() -> ((YdbTable) db.indexedTable()).find(parts)); assertEquals(expectRows, actual); } diff --git a/repository-ydb-v2/src/test/java/tech/ydb/yoj/repository/ydb/merge/QueriesMergerTest.java b/repository-ydb-v2/src/test/java/tech/ydb/yoj/repository/ydb/merge/QueriesMergerTest.java index e02e9a13..465546df 100644 --- a/repository-ydb-v2/src/test/java/tech/ydb/yoj/repository/ydb/merge/QueriesMergerTest.java +++ b/repository-ydb-v2/src/test/java/tech/ydb/yoj/repository/ydb/merge/QueriesMergerTest.java @@ -4,6 +4,7 @@ import org.junit.Test; import tech.ydb.yoj.repository.db.Entity; import tech.ydb.yoj.repository.db.EntitySchema; +import tech.ydb.yoj.repository.db.TableDescriptor; import tech.ydb.yoj.repository.db.cache.RepositoryCache; import tech.ydb.yoj.repository.db.cache.RepositoryCacheImpl; import tech.ydb.yoj.repository.test.sample.model.Primitive; @@ -144,29 +145,38 @@ private QueriesMerger createMerger() { } private > YdbRepository.Query deleteAll(Class clazz) { - return new YdbRepository.Query<>(new DeleteAllStatement<>(EntitySchema.of(clazz)), null); + EntitySchema schema = EntitySchema.of(clazz); + TableDescriptor tableDescriptor = TableDescriptor.from(schema); + return new YdbRepository.Query<>(new DeleteAllStatement<>(tableDescriptor, schema), null); } @SuppressWarnings("unchecked") private > YdbRepository.Query upsert(T p) { - return new YdbRepository.Query<>(new UpsertYqlStatement<>(EntitySchema.of((Class) p.getClass())), p); + EntitySchema schema = EntitySchema.of((Class) p.getClass()); + TableDescriptor tableDescriptor = TableDescriptor.from(schema); + return new YdbRepository.Query<>(new UpsertYqlStatement<>(tableDescriptor, schema), p); } @SuppressWarnings("unchecked") private > YdbRepository.Query insert(T p) { - return new YdbRepository.Query<>(new InsertYqlStatement<>(EntitySchema.of((Class) p.getClass())), p); + EntitySchema schema = EntitySchema.of((Class) p.getClass()); + TableDescriptor tableDescriptor = TableDescriptor.from(schema); + return new YdbRepository.Query<>(new InsertYqlStatement<>(tableDescriptor, schema), p); } @SuppressWarnings("unchecked") private > YdbRepository.Query find(T p) { EntitySchema schema = EntitySchema.of((Class) p.getClass()); - var statement = new FindYqlStatement<>(schema, schema); + TableDescriptor tableDescriptor = TableDescriptor.from(schema); + var statement = new FindYqlStatement<>(tableDescriptor, schema, schema); return new YdbRepository.Query<>(statement, p.getId()); } @SuppressWarnings("unchecked") private > YdbRepository.Query delete(T p) { - return new YdbRepository.Query<>(new DeleteByIdStatement<>(EntitySchema.of((Class) p.getClass())), p.getId()); + EntitySchema schema = EntitySchema.of((Class) p.getClass()); + TableDescriptor tableDescriptor = TableDescriptor.from(schema); + return new YdbRepository.Query<>(new DeleteByIdStatement<>(tableDescriptor, schema), p.getId()); } private ArrayList getProjects() { diff --git a/repository-ydb-v2/src/test/java/tech/ydb/yoj/repository/ydb/statement/DeleteByIdStatementTest.java b/repository-ydb-v2/src/test/java/tech/ydb/yoj/repository/ydb/statement/DeleteByIdStatementTest.java index 701d7a33..5f2f953c 100644 --- a/repository-ydb-v2/src/test/java/tech/ydb/yoj/repository/ydb/statement/DeleteByIdStatementTest.java +++ b/repository-ydb-v2/src/test/java/tech/ydb/yoj/repository/ydb/statement/DeleteByIdStatementTest.java @@ -2,6 +2,7 @@ import org.junit.Test; import tech.ydb.yoj.repository.db.EntitySchema; +import tech.ydb.yoj.repository.db.TableDescriptor; import static org.assertj.core.api.Assertions.assertThat; @@ -25,7 +26,10 @@ public void testDelete() { txManager.tx(() -> { var db = getTestDb(); - var deleteStatement = new DeleteByIdStatement<>(EntitySchema.of(TestEntity.class)); + + var schema = EntitySchema.of(TestEntity.class); + var tableDescriptor = TableDescriptor.from(schema); + var deleteStatement = new DeleteByIdStatement<>(tableDescriptor, schema); db.pendingExecute(deleteStatement, ENTITY_1.getId()); db.pendingExecute(deleteStatement, ENTITY_3.getId()); diff --git a/repository-ydb-v2/src/test/java/tech/ydb/yoj/repository/ydb/statement/FindInStatementIntegrationTest.java b/repository-ydb-v2/src/test/java/tech/ydb/yoj/repository/ydb/statement/FindInStatementIntegrationTest.java index 40731ed2..588d4611 100644 --- a/repository-ydb-v2/src/test/java/tech/ydb/yoj/repository/ydb/statement/FindInStatementIntegrationTest.java +++ b/repository-ydb-v2/src/test/java/tech/ydb/yoj/repository/ydb/statement/FindInStatementIntegrationTest.java @@ -4,6 +4,7 @@ import lombok.Value; import org.junit.Test; import tech.ydb.proto.ValueProtos; +import tech.ydb.yoj.databind.expression.FilterExpression; import tech.ydb.yoj.databind.expression.OrderExpression; import tech.ydb.yoj.databind.DbType; import tech.ydb.yoj.databind.schema.Column; @@ -12,8 +13,10 @@ import tech.ydb.yoj.repository.db.Entity; import tech.ydb.yoj.repository.db.EntitySchema; import tech.ydb.yoj.repository.db.Table; +import tech.ydb.yoj.repository.db.TableDescriptor; import tech.ydb.yoj.repository.db.ViewSchema; +import javax.annotation.Nullable; import java.util.List; import java.util.Map; import java.util.Set; @@ -47,30 +50,65 @@ public class FindInStatementIntegrationTest { private static final String INDEX_NAME = "index_by_value"; private static final String NOT_EXISTENT_INDEX_NAME = "not_existent_index"; + private static , T extends Entity, RESULT> + FindInStatement, T, RESULT> buildFindInStatement( + EntitySchema schema, + Schema resultSchema, + Iterable ids, + @Nullable FilterExpression filter, + @Nullable OrderExpression orderBy, + @Nullable Integer limit + ) { + TableDescriptor tableDescriptor = TableDescriptor.from(schema); + return FindInStatement.from(tableDescriptor, schema, resultSchema, ids, filter, orderBy, limit); + } + + public static , RESULT> + FindInStatement, T, RESULT> buildFindInStatement( + EntitySchema schema, + Schema resultSchema, + String indexName, + Iterable keys, + @Nullable FilterExpression filter, + @Nullable OrderExpression orderBy, + @Nullable Integer limit + ) { + TableDescriptor tableDescriptor = TableDescriptor.from(schema); + return FindInStatement.from(tableDescriptor, schema, resultSchema, indexName, keys, filter, orderBy, limit); + } + @Test public void testGetQueryType() { - var statement = FindInStatement.from(ENTITY_SCHEMA, ENTITY_SCHEMA, IDS, null, null, null); + FindInStatement, Foo, Foo> statement = buildFindInStatement( + ENTITY_SCHEMA, ENTITY_SCHEMA, IDS, null, null, null + ); assertThat(statement.getQueryType()).isEqualTo(Statement.QueryType.SELECT); } @Test public void testToDebugString() { - var statement = FindInStatement.from(ENTITY_SCHEMA, ENTITY_SCHEMA, IDS, null, null, null); + FindInStatement, Foo, Foo> statement = buildFindInStatement( + ENTITY_SCHEMA, ENTITY_SCHEMA, IDS, null, null, null + ); assertThat(statement.toDebugString(IDS)).isNotBlank(); } @Test public void testToDebugStringWithOrder() { - var statement = FindInStatement.from(ENTITY_SCHEMA, ENTITY_SCHEMA, IDS, null, DEFAULT_ORDER, null); + FindInStatement, Foo, Foo> statement = buildFindInStatement( + ENTITY_SCHEMA, ENTITY_SCHEMA, IDS, null, DEFAULT_ORDER, null + ); assertThat(statement.toDebugString(IDS)).isNotBlank(); } @Test public void testEntityUnordered() { - var statement = FindInStatement.from(ENTITY_SCHEMA, ENTITY_SCHEMA, IDS, null, null, null); + FindInStatement, Foo, Foo> statement = buildFindInStatement( + ENTITY_SCHEMA, ENTITY_SCHEMA, IDS, null, null, null + ); String query = statement.getQuery("global/cloud/"); String expected = """ @@ -86,7 +124,9 @@ FROM AS_TABLE($Input) AS k @Test public void testEntityDefaultOrder() { - var statement = FindInStatement.from(ENTITY_SCHEMA, ENTITY_SCHEMA, IDS, null, DEFAULT_ORDER, null); + FindInStatement, Foo, Foo> statement = buildFindInStatement( + ENTITY_SCHEMA, ENTITY_SCHEMA, IDS, null, DEFAULT_ORDER, null + ); String query = statement.getQuery("global/cloud/"); String expected = """ @@ -103,12 +143,12 @@ FROM AS_TABLE($Input) AS k @Test public void testEntityCustomOrder() { - var statement = FindInStatement.from(ENTITY_SCHEMA, ENTITY_SCHEMA, IDS, null, - newOrderBuilder(Foo.class) - .orderBy("id.key1").ascending() - .orderBy("value1").descending() - .build(), - null + OrderExpression order = newOrderBuilder(Foo.class) + .orderBy("id.key1").ascending() + .orderBy("value1").descending() + .build(); + FindInStatement, Foo, Foo> statement = buildFindInStatement( + ENTITY_SCHEMA, ENTITY_SCHEMA, IDS, null, order, null ); String query = statement.getQuery("global/cloud/"); @@ -126,7 +166,9 @@ FROM AS_TABLE($Input) AS k @Test public void testViewUnordered() { - var statement = FindInStatement.from(ENTITY_SCHEMA, VIEW_SCHEMA, IDS, null, null, null); + FindInStatement, Foo, FooView> statement = buildFindInStatement( + ENTITY_SCHEMA, VIEW_SCHEMA, IDS, null, null, null + ); String query = statement.getQuery("global/cloud/"); String expected = """ @@ -142,7 +184,9 @@ FROM AS_TABLE($Input) AS k @Test public void testViewDefaultOrder() { - var statement = FindInStatement.from(ENTITY_SCHEMA, VIEW_SCHEMA, IDS, null, DEFAULT_ORDER, null); + FindInStatement, Foo, FooView> statement = buildFindInStatement( + ENTITY_SCHEMA, VIEW_SCHEMA, IDS, null, DEFAULT_ORDER, null + ); String query = statement.getQuery("global/cloud/"); String expected = """ @@ -159,12 +203,12 @@ FROM AS_TABLE($Input) AS k @Test public void testViewCustomOrder() { - var statement = FindInStatement.from(ENTITY_SCHEMA, VIEW_SCHEMA, IDS, null, - newOrderBuilder(Foo.class) - .orderBy("id.key1").ascending() - .orderBy("value1").descending() - .build(), - null + OrderExpression order = newOrderBuilder(Foo.class) + .orderBy("id.key1").ascending() + .orderBy("value1").descending() + .build(); + FindInStatement, Foo, FooView> statement = buildFindInStatement( + ENTITY_SCHEMA, VIEW_SCHEMA, IDS, null, order, null ); String query = statement.getQuery("global/cloud/"); @@ -182,11 +226,11 @@ FROM AS_TABLE($Input) AS k @Test(expected = IllegalArgumentException.class) public void testViewIncompatibleOrder() { - var statement = FindInStatement.from(ENTITY_SCHEMA, VIEW_SCHEMA, IDS, null, - newOrderBuilder(Foo.class) - .orderBy("value2").descending() - .build(), - null + OrderExpression order = newOrderBuilder(Foo.class) + .orderBy("value2").descending() + .build(); + FindInStatement, Foo, FooView> statement = buildFindInStatement( + ENTITY_SCHEMA, VIEW_SCHEMA, IDS, null, order, null ); statement.getQuery("global/cloud/"); @@ -194,11 +238,13 @@ public void testViewIncompatibleOrder() { @Test public void testFilter() { - var filter = newFilterBuilder(Foo.class) + FilterExpression filter = newFilterBuilder(Foo.class) .where("value1").neq(42L) .and("value2").in("v1", "v2") .build(); - var statement = FindInStatement.from(ENTITY_SCHEMA, VIEW_SCHEMA, IDS, filter, DEFAULT_ORDER, null); + FindInStatement, Foo, FooView> statement = buildFindInStatement( + ENTITY_SCHEMA, VIEW_SCHEMA, IDS, filter, DEFAULT_ORDER, null + ); String query = statement.getQuery("global/cloud/"); String expected = """ @@ -221,11 +267,13 @@ FROM AS_TABLE($Input) AS k @Test public void testFilterOrderLimit() { - var filter = newFilterBuilder(Foo.class) + FilterExpression filter = newFilterBuilder(Foo.class) .where("value1").neq(42L) .and("value2").in("v1", "v2") .build(); - var statement = FindInStatement.from(ENTITY_SCHEMA, VIEW_SCHEMA, IDS, filter, DEFAULT_ORDER, 42); + FindInStatement, Foo, FooView> statement = buildFindInStatement( + ENTITY_SCHEMA, VIEW_SCHEMA, IDS, filter, DEFAULT_ORDER, 42 + ); String query = statement.getQuery("global/cloud/"); String expected = """ @@ -249,7 +297,7 @@ FROM AS_TABLE($Input) AS k @Test public void testByIndex() { - var statement = FindInStatement.from( + var statement = buildFindInStatement( ENTITY_SCHEMA, VIEW_SCHEMA, INDEX_NAME, @@ -272,7 +320,7 @@ FROM AS_TABLE($Input) AS k @Test public void testNotExistentIndex() { assertThatIllegalArgumentException() - .isThrownBy(() -> FindInStatement.from( + .isThrownBy(() -> buildFindInStatement( ENTITY_SCHEMA, VIEW_SCHEMA, NOT_EXISTENT_INDEX_NAME, @@ -285,7 +333,7 @@ public void testNotExistentIndex() { @Test public void testByIndexInconsistentPrefixKeys() { assertThatIllegalArgumentException() - .isThrownBy(() -> FindInStatement.from( + .isThrownBy(() -> buildFindInStatement( ENTITY_SCHEMA, VIEW_SCHEMA, INDEX_NAME, @@ -297,7 +345,7 @@ public void testByIndexInconsistentPrefixKeys() { @Test public void testByIndexNotPrefixKeys() { assertThatIllegalArgumentException() - .isThrownBy(() -> FindInStatement.from( + .isThrownBy(() -> buildFindInStatement( ENTITY_SCHEMA, VIEW_SCHEMA, INDEX_NAME, @@ -309,7 +357,7 @@ public void testByIndexNotPrefixKeys() { @Test public void testByIndexNotIndexedKey() { assertThatIllegalArgumentException() - .isThrownBy(() -> FindInStatement.from( + .isThrownBy(() -> buildFindInStatement( ENTITY_SCHEMA, VIEW_SCHEMA, INDEX_NAME, @@ -322,7 +370,7 @@ public void testByIndexNotIndexedKey() { @Test public void testByIndexInconsistentTypeKeys() { assertThatIllegalArgumentException() - .isThrownBy(() -> FindInStatement.from( + .isThrownBy(() -> buildFindInStatement( ENTITY_SCHEMA, VIEW_SCHEMA, INDEX_NAME, @@ -334,7 +382,9 @@ public void testByIndexInconsistentTypeKeys() { @Test public void testToQueryParameters() { - var statement = FindInStatement.from(ENTITY_SCHEMA, VIEW_SCHEMA, IDS, null, DEFAULT_ORDER, null); + FindInStatement, Foo, FooView> statement = buildFindInStatement( + ENTITY_SCHEMA, VIEW_SCHEMA, IDS, null, DEFAULT_ORDER, null + ); Map queryParams = statement.toQueryParameters(IDS); @@ -343,11 +393,13 @@ public void testToQueryParameters() { @Test public void testToQueryParametersWithFilter() { - var filter = newFilterBuilder(Foo.class) + FilterExpression filter = newFilterBuilder(Foo.class) .where("value1").neq(42L) .and("value2").in("v1", "v2") .build(); - var statement = FindInStatement.from(ENTITY_SCHEMA, VIEW_SCHEMA, IDS, filter, DEFAULT_ORDER, null); + FindInStatement, Foo, FooView> statement = buildFindInStatement( + ENTITY_SCHEMA, VIEW_SCHEMA, IDS, filter, DEFAULT_ORDER, null + ); Map queryParams = statement.toQueryParameters(IDS); @@ -356,11 +408,13 @@ public void testToQueryParametersWithFilter() { @Test public void testEntityWithSimpleId() { - var entitySchema = EntitySchema.of(Bar.class); - var ids = List.of(Bar.Id.of("a"), Bar.Id.of("b")); - var filter = newFilterBuilder(Bar.class).where("value").eq(100500L).build(); - var orderBy = orderById(Bar.class, OrderExpression.SortOrder.DESCENDING); - var statement = FindInStatement.from(entitySchema, entitySchema, ids, filter, orderBy, 42); + EntitySchema entitySchema = EntitySchema.of(Bar.class); + List ids = List.of(Bar.Id.of("a"), Bar.Id.of("b")); + FilterExpression filter = newFilterBuilder(Bar.class).where("value").eq(100500L).build(); + OrderExpression orderBy = orderById(Bar.class, OrderExpression.SortOrder.DESCENDING); + FindInStatement, Bar, Bar> statement = buildFindInStatement( + entitySchema, entitySchema, ids, filter, orderBy, 42 + ); String query = statement.getQuery("global/cloud/"); String expected = """ diff --git a/repository-ydb-v2/src/test/java/tech/ydb/yoj/repository/ydb/statement/InsertYqlStatementTest.java b/repository-ydb-v2/src/test/java/tech/ydb/yoj/repository/ydb/statement/InsertYqlStatementTest.java index 15b927e1..fe26a063 100644 --- a/repository-ydb-v2/src/test/java/tech/ydb/yoj/repository/ydb/statement/InsertYqlStatementTest.java +++ b/repository-ydb-v2/src/test/java/tech/ydb/yoj/repository/ydb/statement/InsertYqlStatementTest.java @@ -2,6 +2,7 @@ import org.junit.Test; import tech.ydb.yoj.repository.db.EntitySchema; +import tech.ydb.yoj.repository.db.TableDescriptor; import static org.assertj.core.api.Assertions.assertThat; @@ -19,7 +20,10 @@ public void testInsert() { txManager.tx(() -> { var db = getTestDb(); - var deleteStatement = new InsertYqlStatement<>(EntitySchema.of(TestEntity.class)); + + var schema = EntitySchema.of(TestEntity.class); + var tableDescriptor = TableDescriptor.from(schema); + var deleteStatement = new InsertYqlStatement<>(tableDescriptor, schema); db.pendingExecute(deleteStatement, ENTITY_2); db.pendingExecute(deleteStatement, ENTITY_3); diff --git a/repository-ydb-v2/src/test/java/tech/ydb/yoj/repository/ydb/statement/UpsertYqlStatementTest.java b/repository-ydb-v2/src/test/java/tech/ydb/yoj/repository/ydb/statement/UpsertYqlStatementTest.java index db922faa..2ec9217a 100644 --- a/repository-ydb-v2/src/test/java/tech/ydb/yoj/repository/ydb/statement/UpsertYqlStatementTest.java +++ b/repository-ydb-v2/src/test/java/tech/ydb/yoj/repository/ydb/statement/UpsertYqlStatementTest.java @@ -2,6 +2,7 @@ import org.junit.Test; import tech.ydb.yoj.repository.db.EntitySchema; +import tech.ydb.yoj.repository.db.TableDescriptor; import static org.assertj.core.api.Assertions.assertThat; @@ -19,7 +20,10 @@ public void testUpsert() { txManager.tx(() -> { var db = getTestDb(); - var deleteStatement = new UpsertYqlStatement<>(EntitySchema.of(TestEntity.class)); + + var schema = EntitySchema.of(TestEntity.class); + var tableDescriptor = TableDescriptor.from(schema); + var deleteStatement = new UpsertYqlStatement<>(tableDescriptor, schema); db.pendingExecute(deleteStatement, ENTITY_1_1); db.pendingExecute(deleteStatement, ENTITY_2); diff --git a/repository/src/main/java/tech/ydb/yoj/repository/db/TableDescriptor.java b/repository/src/main/java/tech/ydb/yoj/repository/db/TableDescriptor.java new file mode 100644 index 00000000..608b65a0 --- /dev/null +++ b/repository/src/main/java/tech/ydb/yoj/repository/db/TableDescriptor.java @@ -0,0 +1,10 @@ +package tech.ydb.yoj.repository.db; + +public record TableDescriptor>( + Class entityType, + String tableName +) { + public static > TableDescriptor from(EntitySchema schema) { + return new TableDescriptor<>(schema.getType(), schema.getName()); + } +}