Skip to content

Commit

Permalink
added Support Composite Primary Key
Browse files Browse the repository at this point in the history
  • Loading branch information
vzakharchenko committed Mar 16, 2020
1 parent b958fc2 commit ab93164
Show file tree
Hide file tree
Showing 59 changed files with 860 additions and 368 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.github.vzakharchenko.dynamic.orm.core.query.crud.SoftDelete;
import com.querydsl.core.JoinExpression;
import com.querydsl.core.types.Expression;
import com.querydsl.core.types.Path;
import com.querydsl.sql.RelationalPath;
import com.querydsl.sql.SQLCommonQuery;
import com.querydsl.sql.SQLQuery;
Expand Down Expand Up @@ -79,4 +80,9 @@ public String showSql(SQLCommonQuery<?> sqlQuery, Expression<?>... expressions)
clone.setUseLiterals(true);
return clone.select(expressions).getSQL().getSQL();
}


protected String showListSql(SQLCommonQuery<?> sqlQuery, List<? extends Path<?>> columns) {
return showSql(sqlQuery, columns.toArray(new Expression[0]));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public interface CrudOrmQueryFactory {
*/
<MODEL extends DMLModel> CrudBuilder<MODEL> modify(RelationalPath<?> qTable,
Class<MODEL> modelClass);

/**
* Modification data for the corresponding qtable(the Metadata Model) class and data model
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,10 @@
*
*/
public abstract class CrudOrmQueryFactoryImpl implements OrmQueryFactory, AccessQueryContext {
private final DataSource dataSource;
private TransactionCacheManager transactionCacheManager;

private TransactionalEventPublisher transactionalEventPublisher;

private PlatformTransactionManager transactionManager;

private final DataSource dataSource;
private Configuration configuration;
private QueryContextImpl queryContext;
private String cacheName = "cache-orm";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ public interface OrmQueryFactoryBuilder {
OrmQueryFactory build();

OrmQueryFactoryBuilder configuration(Configuration configuration);

OrmQueryFactoryBuilder debug();

OrmQueryFactoryBuilder cacheRegion(String cacheName);

OrmQueryFactoryBuilder transactionManager(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,8 @@
*/
public class RawModelBuilderImpl implements RawModelBuilder {

private static final Logger LOGGER = LoggerFactory.getLogger(RawModelBuilderImpl.class);
public static final int SIZE = 1;

private static final Logger LOGGER = LoggerFactory.getLogger(RawModelBuilderImpl.class);
protected final SQLQuery sqlQuery;

protected final QueryContextImpl queryContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,17 @@ <MODEL extends DMLModel> List<MODEL> findAll(SQLCommonQuery<?> sqlQuery,
<MODEL extends DMLModel> MODEL findOne(SQLCommonQuery<?> sqlQuery,
RelationalPath<?> qTable,
Class<MODEL> modelClass);

/**
* The fetch a data model from a database using sqlQuery query.
* the result is mapped to the data models
*
* @param sqlQuery queryDsl query
* @param sqlQuery queryDsl query
* @param dynamicTable Dynamic Table Metadata
* @return MODEL
*/
DynamicTableModel findOne(SQLCommonQuery<?> sqlQuery,
QDynamicTable dynamicTable);
QDynamicTable dynamicTable);

/**
* The fetch a data model from a database using sqlQuery query.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public interface ShowSqlBuilder {
/**
* creates the sql query from querydsl object query.
*
* @param sqlQuery querydsl query
* @param sqlQuery querydsl query
* @param expressions column or any other expression
* @return sql query string
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.vzakharchenko.dynamic.orm.core.cache;

import com.github.vzakharchenko.dynamic.orm.core.helper.PrimaryKeyHelper;
import com.google.common.collect.ImmutableList;
import com.querydsl.core.types.Path;
import com.querydsl.sql.RelationalPath;
Expand All @@ -10,6 +11,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

/**
*
Expand Down Expand Up @@ -49,6 +51,13 @@ public <TYPE> DiffColumn<TYPE> getColumnDiff(Path<TYPE> column) {
return (DiffColumn<TYPE>) diffColumnMap.get(column);
}

public Map<Path<?>, DiffColumn<?>> getColumnDiffPrimaryKey() {
Map<Path<?>, DiffColumn<?>> primaryColumnMap = new HashMap<>();
PrimaryKeyHelper.getPrimaryKeyColumns(qTable).forEach(
(Consumer<Path<?>>) path -> primaryColumnMap.put(path, getColumnDiff(path)));
return primaryColumnMap;
}

public <TYPE> DiffColumn<TYPE> getActualColumnDiff(Path<TYPE> column) {
return (DiffColumn<TYPE>) diffColumnMap.get(column);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,25 @@ public static <TYPE> DiffColumn<TYPE> buildDiffColumn(
return new DiffColumn<>(column, oldValue, newValue);
}

private static boolean hasOldValue(Map<Path<?>, DiffColumn<?>> primaryDiff) {
return primaryDiff.entrySet().stream().anyMatch(entry -> entry.getValue()
.getOldValue() != null);
}

private static boolean hasNewValue(Map<Path<?>, DiffColumn<?>> primaryDiff) {
return primaryDiff.entrySet().stream().anyMatch(entry -> entry.getValue()
.getNewValue() != null);
}

public static <MODEL extends DMLModel> MODEL buildOldModel(
Class<MODEL> modelClass, DiffColumnModel diffColumnModel) {
RelationalPath<?> qTable = diffColumnModel.getQTable();
Path primaryKey = ModelHelper.getPrimaryKeyColumn(qTable);
DiffColumn primaryDiff = diffColumnModel.getColumnDiff(primaryKey);
if (primaryDiff.getOldValue() == null) {

Map<Path<?>, DiffColumn<?>> primaryDiff = diffColumnModel.getColumnDiffPrimaryKey();

if (!hasOldValue(primaryDiff)) {
return null;
}
RelationalPath<?> qTable = diffColumnModel.getQTable();
MODEL model = CacheHelper.newInstance(qTable, modelClass);
for (Path<?> column : qTable.getColumns()) {
DiffColumn diffColumn = diffColumnModel.getColumnDiff(column);
Expand All @@ -42,12 +53,12 @@ public static <MODEL extends DMLModel> MODEL buildOldModel(

public static <MODEL extends DMLModel> MODEL buildNewModel(
Class<MODEL> modelClass, DiffColumnModel diffColumnModel) {
RelationalPath<?> qTable = diffColumnModel.getQTable();
Path primaryKey = ModelHelper.getPrimaryKeyColumn(qTable);
DiffColumn primaryDiff = diffColumnModel.getColumnDiff(primaryKey);
if (primaryDiff.getNewValue() == null) {
Map<Path<?>, DiffColumn<?>> primaryDiff = diffColumnModel.getColumnDiffPrimaryKey();

if (!hasNewValue(primaryDiff)) {
return null;
}
RelationalPath<?> qTable = diffColumnModel.getQTable();
MODEL model = CacheHelper.newInstance(qTable, modelClass);
for (Path<?> column : qTable.getColumns()) {
DiffColumn diffColumn = diffColumnModel.getColumnDiff(column);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.github.vzakharchenko.dynamic.orm.core.cache;

import com.github.vzakharchenko.dynamic.orm.core.DMLModel;
import com.github.vzakharchenko.dynamic.orm.core.helper.CompositeKey;

import java.io.Serializable;
import java.util.List;

/**
Expand All @@ -14,7 +14,7 @@
public interface LazyList<MODEL extends DMLModel> {
List<MODEL> getModelList();

List<Serializable> getPrimaryKeyList();
List<CompositeKey> getPrimaryKeyList();

int size();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

import com.github.vzakharchenko.dynamic.orm.core.DMLModel;
import com.github.vzakharchenko.dynamic.orm.core.OrmQueryFactory;
import com.github.vzakharchenko.dynamic.orm.core.helper.CompositeKey;
import com.github.vzakharchenko.dynamic.orm.core.query.QueryContextImpl;
import com.github.vzakharchenko.dynamic.orm.core.query.cache.CacheBuilder;
import com.querydsl.sql.RelationalPath;
import org.apache.commons.collections4.CollectionUtils;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;

Expand All @@ -18,14 +18,15 @@ public class LazyListImpl<MODEL extends DMLModel> implements LazyList<MODEL> {

protected final RelationalPath<?> qTable;

private final List<Serializable> listPrimaryKey;
private final List<CompositeKey> listPrimaryKey;

private final Class<MODEL> modelClass;

private final OrmQueryFactory ormQueryFactory;

protected LazyListImpl(
RelationalPath<?> qTable, List<Serializable> listPrimaryKey,
RelationalPath<?> qTable,
List<CompositeKey> listPrimaryKey,
Class<MODEL> modelClass, QueryContextImpl queryContext) {
this.qTable = qTable;
this.modelClass = modelClass;
Expand All @@ -43,7 +44,7 @@ public List<MODEL> getModelList() {
}

@Override
public List<Serializable> getPrimaryKeyList() {
public List<CompositeKey> getPrimaryKeyList() {
return listPrimaryKey;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.vzakharchenko.dynamic.orm.core.cache;

import com.github.vzakharchenko.dynamic.orm.core.DMLModel;
import com.github.vzakharchenko.dynamic.orm.core.helper.CompositeKey;
import com.github.vzakharchenko.dynamic.orm.core.query.cache.CacheBuilder;
import com.github.vzakharchenko.dynamic.orm.core.query.cache.RawCacheBuilder;

Expand All @@ -13,10 +14,10 @@
*/
public class ModelLazyList<MODEL extends DMLModel> extends AbstractList<MODEL> {
private static final int MAX_LAZYLIST_CACHE_SIZE = 100;
private final List<Serializable> listIds;
private final List<CompositeKey> listIds;
private final CacheBuilder<MODEL> cacheBuilder;

protected ModelLazyList(List<Serializable> listIds, CacheBuilder<MODEL> cacheBuilder) {
protected ModelLazyList(List<CompositeKey> listIds, CacheBuilder<MODEL> cacheBuilder) {
super();
this.listIds = listIds;
this.cacheBuilder = cacheBuilder;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package com.github.vzakharchenko.dynamic.orm.core.cache;

import com.github.vzakharchenko.dynamic.orm.core.DMLModel;
import com.github.vzakharchenko.dynamic.orm.core.helper.CompositeKey;
import com.github.vzakharchenko.dynamic.orm.core.query.QueryContextImpl;
import com.github.vzakharchenko.dynamic.orm.core.query.cache.CacheBuilder;
import com.querydsl.sql.RelationalPath;

import java.io.Serializable;
import java.util.List;

/**
Expand All @@ -14,12 +14,12 @@
public abstract class ModelLazyListFactory {

public static <MODEL extends DMLModel> List<MODEL> buildModelLazyList(
List<Serializable> keys, CacheBuilder<MODEL> cacheBuilder) {
return new ModelLazyList<>(keys, cacheBuilder);
List<CompositeKey> keys, CacheBuilder<MODEL> cacheBuilder) {
return new ModelLazyList<MODEL>(keys, cacheBuilder);
}

public static <MODEL extends DMLModel> LazyList<MODEL> buildLazyList(
RelationalPath<?> qTable, List<Serializable> keys,
RelationalPath<?> qTable, List<CompositeKey> keys,
Class<MODEL> modelClass, QueryContextImpl queryContext) {
return new LazyListImpl<>(qTable, keys, modelClass, queryContext);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.github.vzakharchenko.dynamic.orm.core.cache;

import com.github.vzakharchenko.dynamic.orm.core.helper.CompositeKey;
import com.github.vzakharchenko.dynamic.orm.core.helper.ModelHelper;
import com.querydsl.sql.RelationalPath;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
Expand All @@ -10,20 +10,20 @@
*
*/
public class PrimaryKeyCacheKey implements Serializable {
private Serializable key;
private CompositeKey key;

private String tableName;

public PrimaryKeyCacheKey() {
super();
}

public PrimaryKeyCacheKey(Serializable key, RelationalPath<?> qTable) {
public PrimaryKeyCacheKey(CompositeKey key) {
this.key = key;
this.tableName = StringUtils.upperCase(ModelHelper.getTableName(qTable));
this.tableName = StringUtils.upperCase(ModelHelper.getTableName(key.getTable()));
}

public Serializable getKey() {
public CompositeKey getKey() {
return key;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import com.github.vzakharchenko.dynamic.orm.core.DMLModel;
import com.github.vzakharchenko.dynamic.orm.core.cache.DiffColumnModel;
import com.github.vzakharchenko.dynamic.orm.core.helper.CompositeKey;
import com.querydsl.sql.RelationalPath;

import java.io.Serializable;
import java.util.Map;

/**
Expand All @@ -24,7 +24,7 @@ public abstract class EventFactory {
public static <MODEL extends DMLModel> DiffEvent insertDiffEvent(
RelationalPath<?> qTable,
Class<MODEL> modelClass,
Map<Serializable, DiffColumnModel> diffColumnModelMap) {
Map<CompositeKey, DiffColumnModel> diffColumnModelMap) {
return new DiffEvent(qTable)
.cacheEventType(CacheEventType.INSERT)
.modelClass(modelClass)
Expand All @@ -43,7 +43,7 @@ public static <MODEL extends DMLModel> DiffEvent insertDiffEvent(
public static <MODEL extends DMLModel> DiffEvent updateDiffEvent(
RelationalPath<?> qTable,
Class<MODEL> modelClass,
Map<Serializable, DiffColumnModel> diffColumnModelMap) {
Map<CompositeKey, DiffColumnModel> diffColumnModelMap) {
// return new DiffEvent(CacheEventType.UPDATE, qTable, modelClass, diffColumnModelMap);
return new DiffEvent(qTable)
.cacheEventType(CacheEventType.UPDATE)
Expand All @@ -63,7 +63,7 @@ public static <MODEL extends DMLModel> DiffEvent updateDiffEvent(
public static <MODEL extends DMLModel> DiffEvent deleteDiffEvent(
RelationalPath<?> qTable,
Class<MODEL> modelClass,
Map<Serializable, DiffColumnModel> diffColumnModelMap) {
Map<CompositeKey, DiffColumnModel> diffColumnModelMap) {
return new DiffEvent(qTable)
.cacheEventType(CacheEventType.DELETE)
.modelClass(modelClass)
Expand All @@ -82,7 +82,7 @@ public static <MODEL extends DMLModel> DiffEvent deleteDiffEvent(
public static <MODEL extends DMLModel> DiffEvent softDeleteDiffEvent(
RelationalPath<?> qTable,
Class<MODEL> modelClass,
Map<Serializable, DiffColumnModel> diffColumnModelMap) {
Map<CompositeKey, DiffColumnModel> diffColumnModelMap) {
return new DiffEvent(qTable)
.cacheEventType(CacheEventType.SOFT_DELETE)
.modelClass(modelClass)
Expand All @@ -101,7 +101,7 @@ public static <MODEL extends DMLModel> DiffEvent softDeleteDiffEvent(
public static <MODEL extends DMLModel> CacheEvent softDeleteCacheEvent(
RelationalPath<?> qTable,
Class<MODEL> modelClass,
Map<Serializable, DiffColumnModel> diffColumnModelMap) {
Map<CompositeKey, DiffColumnModel> diffColumnModelMap) {
return new CacheEvent(qTable)
.cacheEventType(CacheEventType.SOFT_DELETE)
.modelClass(modelClass)
Expand All @@ -119,7 +119,7 @@ public static <MODEL extends DMLModel> CacheEvent softDeleteCacheEvent(
*/
public static <MODEL extends DMLModel> CacheEvent insertCacheEvent(
RelationalPath<?> qTable,
Class<MODEL> modelClass, Map<Serializable,
Class<MODEL> modelClass, Map<CompositeKey,
DiffColumnModel> diffColumnModelMap) {
return new CacheEvent(qTable)
.cacheEventType(CacheEventType.INSERT)
Expand All @@ -139,7 +139,7 @@ public static <MODEL extends DMLModel> CacheEvent insertCacheEvent(
public static <MODEL extends DMLModel> CacheEvent updateCacheEvent(
RelationalPath<?> qTable,
Class<MODEL> modelClass,
Map<Serializable, DiffColumnModel> diffColumnModelMap) {
Map<CompositeKey, DiffColumnModel> diffColumnModelMap) {
return new CacheEvent(qTable)
.cacheEventType(CacheEventType.UPDATE)
.modelClass(modelClass)
Expand All @@ -158,7 +158,7 @@ public static <MODEL extends DMLModel> CacheEvent updateCacheEvent(
public static <MODEL extends DMLModel> CacheEvent deleteCacheEvent(
RelationalPath<?> qTable,
Class<MODEL> modelClass,
Map<Serializable, DiffColumnModel> diffColumnModelMap) {
Map<CompositeKey, DiffColumnModel> diffColumnModelMap) {
return new CacheEvent(qTable)
.cacheEventType(CacheEventType.DELETE)
.modelClass(modelClass)
Expand Down
Loading

0 comments on commit ab93164

Please sign in to comment.