Skip to content

Commit

Permalink
Merge pull request #33 from vzakharchenko/1.2.3
Browse files Browse the repository at this point in the history
release 1.2.3
  • Loading branch information
vzakharchenko authored Mar 25, 2020
2 parents 93fcf4d + 5d2a7c5 commit 74a4843
Show file tree
Hide file tree
Showing 46 changed files with 1,267 additions and 536 deletions.
355 changes: 243 additions & 112 deletions README.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public void registerQ(Map<String, QDynamicTable> qDynamicTableMap,
for (QDynamicTable qDynamicTable : qDynamicTables) {
qDynamicTableMap.put(StringUtils.upperCase(
qDynamicTable.getTableName()), qDynamicTable);
qDynamicTable.reInit();
}
updateCache(qDynamicTableMap, cacheName);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import com.querydsl.core.types.Path;

public interface ModifyColumnMetaDataInfo {
public interface ModifyColumnMetaDataInfo extends ColumnMetaDataInfo {
void setColumn(Path column);

void setSize(Integer size);

void setDecimalDigits(Integer decimalDigits);

void setPrimaryKey(Boolean primaryKey);

void setNullable(Boolean nullable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
import com.querydsl.core.types.Path;

public class ModifyColumnMetaDataInfoImpl
implements ColumnMetaDataInfo, ModifyColumnMetaDataInfo {
implements ModifyColumnMetaDataInfo {
private final ColumnMetaDataInfo columnMetaDataInfo;
private Path column;
private Integer size;
private Integer decimalDigits;
private Boolean nullable;
private Boolean primaryKey;

public ModifyColumnMetaDataInfoImpl(ColumnMetaDataInfo columnMetaDataInfo) {
this.columnMetaDataInfo = columnMetaDataInfo;
Expand Down Expand Up @@ -56,7 +57,12 @@ public Boolean isNullable() {

@Override
public Boolean isPrimaryKey() {
return columnMetaDataInfo.isPrimaryKey();
return primaryKey == null ? columnMetaDataInfo.isPrimaryKey() : primaryKey;
}

@Override
public void setPrimaryKey(Boolean primaryKey) {
this.primaryKey = primaryKey;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.github.vzakharchenko.dynamic.orm.core.dynamic;

import com.github.vzakharchenko.dynamic.orm.core.helper.ModelHelper;
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableList;
import com.querydsl.core.types.Path;
import com.querydsl.core.types.PathMetadataFactory;
import com.querydsl.sql.*;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
Expand All @@ -23,14 +25,14 @@ public abstract class QAbstractDynamicTable<DYNAMIC_TABLE extends QAbstractDynam
protected final Map<String, Path<?>> columns = new LinkedHashMap<>();
protected final Map<Path<?>, ColumnMetaDataInfo> columnMetaDataInfoMap = new HashMap<>();
private final Map<String, Path<?>> removedColumns = new LinkedHashMap<>();

private final List<ForeignKey<?>> removedForeignKeys = new ArrayList<>();

protected QAbstractDynamicTable(String tableName) {
super(Object.class, PathMetadataFactory.forVariable(tableName), "", tableName);
}

protected DYNAMIC_TABLE addPrimaryKey(
Path path) {
protected DYNAMIC_TABLE modifyPrimaryKey(
Path path, boolean remove) {
Assert.notNull(path);
RelationalPath<?> qTable = ModelHelper.getQTable(path);
Assert.isTrue(Objects.equals(this, qTable));
Expand All @@ -42,21 +44,33 @@ protected DYNAMIC_TABLE addPrimaryKey(
createPrimaryKey(path);
} else {
List<? extends Path<?>> localColumns = primaryKey.getLocalColumns();
updatePrimaryKey(localColumns.toArray(new Path[0]), path);
updatePrimaryKey(localColumns.toArray(new Path[0]), path, remove);
}
return (DYNAMIC_TABLE) this;
}

protected PrimaryKey<Object> updatePrimaryKey(Path[] columnArray,
Path column) {
return createPrimaryKey(ArrayUtils.add(columnArray, column));
Path column,
boolean remove) {
return createPrimaryKey(remove ? ArrayUtils.removeElement(columnArray, column) :
ArrayUtils.add(columnArray, column));
}


protected DYNAMIC_TABLE addPrimaryKey(String columnName) {
Assert.hasText(columnName);
Path<?> pkColumn = columns.get(StringUtils.upperCase(columnName));
return addPrimaryKey(pkColumn);
return modifyPrimaryKey(pkColumn, false);
}

protected DYNAMIC_TABLE removePrimaryKey(String columnName) {
Assert.hasText(columnName);
Path<?> pkColumn = columns.get(StringUtils.upperCase(columnName));
Assert.notNull(pkColumn, "Column " + columnName + " does not exist.");
ModifyColumnMetaDataInfo metaInfo = new ModifyColumnMetaDataInfoImpl(getMetaInfo(pkColumn));
metaInfo.setPrimaryKey(Boolean.FALSE);
columnMetaDataInfoMap.put(pkColumn, metaInfo);
return modifyPrimaryKey(pkColumn, true);
}

protected DYNAMIC_TABLE addForeignKey(
Expand All @@ -77,6 +91,7 @@ protected DYNAMIC_TABLE addForeignKey(
ModelHelper::getColumnRealName)
.collect(Collectors.toList())));
getForeignKeys().add(foreignKey);
removedForeignKeys.remove(foreignKey);
return (DYNAMIC_TABLE) this;
}

Expand All @@ -87,7 +102,7 @@ protected DYNAMIC_TABLE addColumn(
addMetadata(columnMetaDataInfo);
Path column = columnMetaDataInfo.getColumn();
if (BooleanUtils.isTrue(columnMetaDataInfo.isPrimaryKey())) {
addPrimaryKey(column);
modifyPrimaryKey(column, false);
}
columns.put(ModelHelper.getColumnRealName(column), column);
columnMetaDataInfoMap.put(column, columnMetaDataInfo);
Expand Down Expand Up @@ -119,6 +134,7 @@ private void addMetadata(
if (!columnMetaDataInfo.isNullable()) {
columnMetadata = columnMetadata.notNull();
}
removedColumns.remove(columnMetadata.getName());
addMetadata(column, columnMetadata);
}

Expand Down Expand Up @@ -146,4 +162,30 @@ public void checkColumn(String columnName, Object value) {
public List<String> deletedColumns() {
return new ArrayList<>(removedColumns.keySet());
}

public List<ForeignKey<?>> deletedForeignKeys() {
return this.removedForeignKeys;
}

protected abstract void init();

public void reInit() {
deletedColumns().clear();
deletedForeignKeys().clear();
init();
}

public void removeForeignKey(List<Path<?>> localColumns) {
List<ForeignKey<?>> foreignKeys = getForeignKeys().stream().filter(
(Predicate<ForeignKey<?>>) foreignKey ->
!CollectionUtils.isEqualCollection(foreignKey.getLocalColumns(),
localColumns)).collect(Collectors.toList());
List<ForeignKey<?>> foreignKeysToDelete = getForeignKeys().stream().filter(
(Predicate<ForeignKey<?>>) foreignKey ->
CollectionUtils.isEqualCollection(foreignKey.getLocalColumns(),
localColumns)).collect(Collectors.toList());
this.removedForeignKeys.addAll(foreignKeysToDelete);
getForeignKeys().clear();
getForeignKeys().addAll(foreignKeys);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import com.github.vzakharchenko.dynamic.orm.core.pk.PKGenerator;
import com.github.vzakharchenko.dynamic.orm.core.query.crud.SoftDelete;
import com.github.vzakharchenko.dynamic.orm.core.query.crud.SoftDeleteFactory;
import com.google.common.collect.Sets;
import com.querydsl.core.types.Path;
import com.querydsl.sql.RelationalPath;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.Assert;
Expand All @@ -15,6 +17,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import static com.github.vzakharchenko.dynamic.orm.core.RawModelBuilderImpl.SIZE;

Expand All @@ -25,6 +28,7 @@ public class QDynamicTable extends QAbstractSetColumnDynamicTable<QDynamicTable>

public static final String WRONG_COLUMN = "Wrong column ";
private final List<IndexData> indexDatas = new ArrayList<>();
private final List<IndexData> removedIndexList = new ArrayList<>();
private PKGenerator<?> pkGenerator;
private Path<?> versionColumn;
private SoftDelete<?> softDelete;
Expand All @@ -33,6 +37,11 @@ protected QDynamicTable(String tableName) {
super(StringUtils.upperCase(tableName));
}

@Override
protected void init() {
removedIndexList.clear();
}

// CHECKSTYLE:OFF
@Override
public boolean equals(Object o) { //NOPMD
Expand Down Expand Up @@ -125,10 +134,21 @@ protected QDynamicTable addIndex(List<Path<?>> columns,
throw new IllegalStateException(WRONG_COLUMN + column);
}
}
indexDatas.add(new IndexData(columns, unique, clustered));
IndexData indexData = new IndexData(columns, unique, clustered);
indexDatas.add(indexData);
resetRemovedIndices(indexData);
return this;
}


private void resetRemovedIndices(IndexData indexData) {
List<IndexData> list = removedIndexList.stream().filter(id ->
!CollectionUtils.isEqualCollection(
Sets.newHashSet(indexData.getColumns()), Sets.newHashSet(id.getColumns())))
.collect(Collectors.toList());
updateIndices(removedIndexList, list);
}

public PKGenerator<?> getPkGenerator() {
return pkGenerator;
}
Expand All @@ -155,4 +175,27 @@ public Path<?>[] all() {
public List<Path<?>> getColumns() {
return new ArrayList<>(columns.values());
}

public void removeIndex(List<Path<?>> localColumns) {
List<IndexData> list1 = indexDatas.stream().filter(indexData ->
!CollectionUtils.isEqualCollection(
Sets.newHashSet(indexData.getColumns()),
Sets.newHashSet(localColumns))).collect(Collectors.toList());

List<IndexData> list2 = indexDatas.stream().filter(indexData ->
CollectionUtils.isEqualCollection(
Sets.newHashSet(indexData.getColumns()),
Sets.newHashSet(localColumns))).collect(Collectors.toList());
updateIndices(indexDatas, list1);
removedIndexList.addAll(list2);
}

public List<IndexData> removedIndices() {
return this.removedIndexList;
}

private void updateIndices(List<IndexData> origin, List<IndexData> list) {
origin.clear();
origin.addAll(list);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ public QTableColumn columns() {
}

@Override
public QPrimaryKeyBuilder addPrimaryKey() {
public QPrimaryKeyBuilder primaryKey() {
return new QPrimaryKeyBuilderImpl(this, qDynamicTable);
}

@Override
public QForeignKeyBuilder addForeignKey(String... localColumns) {
public QForeignKeyBuilder foreignKey(String... localColumns) {
List<Path<?>> localStringColumns = Arrays.stream(localColumns)
.map(StringUtils::upperCase).map((Function<String, Path<?>>)
s -> qDynamicTable.getColumnByName(s))
Expand All @@ -66,25 +66,25 @@ public QForeignKeyBuilder addForeignKey(String... localColumns) {
}

@Override
public QForeignKeyBuilder addForeignKeyPath(Path<?>... localColumns) {
public QForeignKeyBuilder foreignKeyPath(Path<?>... localColumns) {
return addForeignKey(Arrays.asList(localColumns));
}

@Override
public QIndexBuilder addIndex(String... localColumns) {
public QIndexBuilder index(String... localColumns) {
List<Path<?>> localStringColumns = Arrays.stream(localColumns)
.map(StringUtils::upperCase).map((Function<String, Path<?>>)
s -> qDynamicTable.getColumnByName(s))
.collect(Collectors.toList());
return addIndex(localStringColumns);
return index(localStringColumns);
}

@Override
public QIndexBuilder addIndex(Path<?>... localColumns) {
return addIndex(Arrays.asList(localColumns));
public QIndexBuilder index(Path<?>... localColumns) {
return index(Arrays.asList(localColumns));
}

private QIndexBuilder addIndex(List<Path<?>> localColumns) {
private QIndexBuilder index(List<Path<?>> localColumns) {
return new QIndexBuilderImpl(this,
localColumns, qDynamicTable);
}
Expand Down Expand Up @@ -121,7 +121,7 @@ public QTableBuilder buildNextTable(String tableName) {
}

@Override
public QDynamicTableFactory finish() {
public QDynamicTableFactory endBuildTables() {
dynamicContextHolder.getContextTables().put(qDynamicTable.getTableName(), qDynamicTable);
return dynamicContextHolder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.github.vzakharchenko.dynamic.orm.core.dynamic.structure.DynamicStructureSaver;
import com.github.vzakharchenko.dynamic.orm.core.dynamic.structure.DynamicStructureUpdater;
import com.github.vzakharchenko.dynamic.orm.core.dynamic.structure.LiquibaseHolder;
import com.github.vzakharchenko.dynamic.orm.core.helper.CacheHelper;
import liquibase.database.Database;
import liquibase.database.DatabaseFactory;
import liquibase.database.jvm.JdbcConnection;
Expand Down Expand Up @@ -150,6 +151,9 @@ public void clear() {
sequenceModelMap.clear();
dynamicTableMap.clear();
viewModelMap.clear();
removedTables.clear();
removedSequences.clear();
CacheHelper.getAccessQueryContext(ormQueryFactory).clearCache();
}


Expand Down
Loading

0 comments on commit 74a4843

Please sign in to comment.