-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Query multiple tables using one entity (Refactoring) (#96)
- Loading branch information
Showing
12 changed files
with
292 additions
and
127 deletions.
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/CountAllStatement.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package tech.ydb.yoj.repository.ydb.statement; | ||
|
||
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.ydb.yql.YqlPredicate; | ||
import tech.ydb.yoj.repository.ydb.yql.YqlStatementPart; | ||
|
||
import java.util.Collection; | ||
import java.util.function.Function; | ||
|
||
import static java.util.Comparator.comparing; | ||
import static java.util.stream.Collectors.joining; | ||
|
||
class CountAllStatement<ENTITY extends Entity<ENTITY>> extends PredicateStatement<Collection<? extends YqlStatementPart<?>>, ENTITY, Count> { | ||
private final Collection<? extends YqlStatementPart<?>> parts; | ||
|
||
public CountAllStatement( | ||
EntitySchema<ENTITY> schema, | ||
Schema<Count> resultSchema, | ||
Collection<? extends YqlStatementPart<?>> parts, | ||
Function<Collection<? extends YqlStatementPart<?>>, YqlPredicate> predicateFrom | ||
) { | ||
super(schema, resultSchema, parts, predicateFrom); | ||
this.parts = parts; | ||
} | ||
|
||
public CountAllStatement( | ||
EntitySchema<ENTITY> schema, | ||
Schema<Count> resultSchema, | ||
Collection<? extends YqlStatementPart<?>> parts, | ||
Function<Collection<? extends YqlStatementPart<?>>, YqlPredicate> predicateFrom, | ||
String tableName | ||
) { | ||
super(schema, resultSchema, parts, predicateFrom, tableName); | ||
this.parts = parts; | ||
} | ||
|
||
@Override | ||
public String getQuery(String tablespace) { | ||
return declarations() | ||
+ "SELECT COUNT(*) AS count" | ||
+ " FROM " + table(tablespace) | ||
+ " " + mergeParts(parts.stream()) | ||
.sorted(comparing(YqlStatementPart::getPriority)) | ||
.map(sp -> sp.toFullYql(schema)) | ||
.map(this::resolveParamNames) | ||
.collect(joining(" ")); | ||
} | ||
|
||
@Override | ||
public QueryType getQueryType() { | ||
return QueryType.SELECT; | ||
} | ||
|
||
@Override | ||
public String toDebugString(Collection<? extends YqlStatementPart<?>> yqlStatementParts) { | ||
return "count(" + parts + ")"; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...sitory-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/DeleteAllStatement.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package tech.ydb.yoj.repository.ydb.statement; | ||
|
||
import lombok.NonNull; | ||
import tech.ydb.yoj.repository.db.Entity; | ||
import tech.ydb.yoj.repository.db.EntitySchema; | ||
|
||
class DeleteAllStatement<PARAMS, ENTITY extends Entity<ENTITY>> extends YqlStatement<PARAMS, ENTITY, ENTITY> { | ||
public DeleteAllStatement(@NonNull Class<ENTITY> type) { | ||
super(EntitySchema.of(type), EntitySchema.of(type)); | ||
} | ||
|
||
public DeleteAllStatement(@NonNull EntitySchema<ENTITY> schema, String tableName) { | ||
super(schema, schema, tableName); | ||
} | ||
|
||
@Override | ||
public String getQuery(String tablespace) { | ||
return "DELETE FROM " + table(tablespace); | ||
} | ||
|
||
@Override | ||
public QueryType getQueryType() { | ||
return QueryType.DELETE_ALL; | ||
} | ||
|
||
@Override | ||
public String toDebugString(PARAMS params) { | ||
return "deleteAll(" + schema.getName() + ")"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/FindStatement.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
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.ydb.yql.YqlPredicate; | ||
import tech.ydb.yoj.repository.ydb.yql.YqlStatementPart; | ||
|
||
import java.util.Collection; | ||
import java.util.function.Function; | ||
|
||
import static java.util.Comparator.comparing; | ||
import static java.util.stream.Collectors.joining; | ||
|
||
public class FindStatement<ENTITY extends Entity<ENTITY>, RESULT> extends PredicateStatement<Collection<? extends YqlStatementPart<?>>, ENTITY, RESULT> { | ||
private final boolean distinct; | ||
private final Collection<? extends YqlStatementPart<?>> parts; | ||
|
||
public FindStatement( | ||
@NonNull EntitySchema<ENTITY> schema, | ||
@NonNull Schema<RESULT> outSchema, | ||
@NonNull Collection<? extends YqlStatementPart<?>> parts, | ||
@NonNull Function<Collection<? extends YqlStatementPart<?>>, YqlPredicate> predicateFrom, | ||
boolean distinct, | ||
String tableName) { | ||
super(schema, outSchema, parts, predicateFrom, tableName); | ||
this.distinct = distinct; | ||
this.parts = parts; | ||
} | ||
|
||
public String getQuery(String tablespace) { | ||
return declarations() | ||
+ "SELECT " + (distinct ? "DISTINCT " : "") + outNames() | ||
+ " FROM " + table(tablespace) | ||
+ " " + mergeParts(parts.stream()) | ||
.sorted(comparing(YqlStatementPart::getPriority)) | ||
.map(sp -> sp.toFullYql(schema)) | ||
.map(this::resolveParamNames) | ||
.collect(joining(" ")); | ||
} | ||
|
||
@Override | ||
public Statement.QueryType getQueryType() { | ||
return Statement.QueryType.SELECT; | ||
} | ||
|
||
@Override | ||
public String toDebugString(Collection<? extends YqlStatementPart<?>> yqlStatementParts) { | ||
return "find(" + yqlStatementParts + ")"; | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
repository-ydb-v2/src/main/java/tech/ydb/yoj/repository/ydb/statement/FindYqlStatement.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
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.cache.RepositoryCache; | ||
import tech.ydb.yoj.repository.ydb.yql.YqlType; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import static java.util.stream.Collectors.toList; | ||
|
||
public class FindYqlStatement<PARAMS, ENTITY extends Entity<ENTITY>, RESULT> extends YqlStatement<PARAMS, ENTITY, RESULT> { | ||
public FindYqlStatement(@NonNull EntitySchema<ENTITY> schema, @NonNull Schema<RESULT> resultSchema) { | ||
super(schema, resultSchema); | ||
} | ||
|
||
public FindYqlStatement(@NonNull EntitySchema<ENTITY> schema, @NonNull Schema<RESULT> resultSchema, String tableName) { | ||
super(schema, resultSchema, tableName); | ||
} | ||
|
||
@Override | ||
public List<YqlStatementParam> getParams() { | ||
return schema.flattenId().stream() | ||
.map(c -> YqlStatementParam.required(YqlType.of(c), c.getName())) | ||
.collect(toList()); | ||
} | ||
|
||
@Override | ||
public String getQuery(String tablespace) { | ||
return declarations() | ||
+ "SELECT " + outNames() | ||
+ " FROM " + table(tablespace) | ||
+ " WHERE " + nameEqVars(); | ||
} | ||
|
||
@Override | ||
public List<RESULT> readFromCache(PARAMS params, RepositoryCache cache) { | ||
RepositoryCache.Key key = new RepositoryCache.Key(resultSchema.getType(), params); | ||
if (!cache.contains(key)) { | ||
return null; | ||
} | ||
|
||
//noinspection unchecked | ||
return cache.get(key) | ||
.map(o -> Collections.singletonList((RESULT) o)) | ||
.orElse(Collections.emptyList()); | ||
} | ||
|
||
@Override | ||
public void storeToCache(PARAMS params, List<RESULT> result, RepositoryCache cache) { | ||
RepositoryCache.Key key = new RepositoryCache.Key(resultSchema.getType(), params); | ||
cache.put(key, result.stream().findFirst().orElse(null)); | ||
} | ||
|
||
@Override | ||
public QueryType getQueryType() { | ||
return QueryType.SELECT; | ||
} | ||
|
||
@Override | ||
public String toDebugString(PARAMS params) { | ||
return "find(" + params + ")"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.