Skip to content

Commit

Permalink
release 1.2.3
Browse files Browse the repository at this point in the history
- refactoring schema builder
- drop index
- drop foreign key
- remove private key
  • Loading branch information
vzakharchenko committed Mar 25, 2020
1 parent 93fcf4d commit 7ae238f
Show file tree
Hide file tree
Showing 41 changed files with 1,204 additions and 514 deletions.
345 changes: 237 additions & 108 deletions README.md

Large diffs are not rendered by default.

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,22 @@ public void checkColumn(String columnName, Object value) {
public List<String> deletedColumns() {
return new ArrayList<>(removedColumns.keySet());
}

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

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 Down Expand Up @@ -125,10 +129,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 +170,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 @@ -35,35 +35,35 @@ private QDynamicTable getDynamicTable() {


@Override
public QTableBuilder buildForeignKey(RelationalPath<?> remoteQTable,
Path<?>... remotePrimaryKeys) {
return buildForeignKey(remoteQTable, Arrays.asList(remotePrimaryKeys));
public QTableBuilder addForeignKey(RelationalPath<?> remoteQTable,
Path<?>... remotePrimaryKeys) {
return addForeignKey(remoteQTable, Arrays.asList(remotePrimaryKeys));
}

public QTableBuilder buildForeignKey(RelationalPath<?> remoteQTable,
List<Path<?>> remotePrimaryKeys) {
public QTableBuilder addForeignKey(RelationalPath<?> remoteQTable,
List<Path<?>> remotePrimaryKeys) {
getDynamicTable().addForeignKey(localColumns, remoteQTable, remotePrimaryKeys);
return tableBuilder;
}

@Override
public QTableBuilder buildForeignKey(QDynamicTable remoteDynamicTable,
String... remotePrimaryKeys) {
return buildForeignKey(remoteDynamicTable,
public QTableBuilder addForeignKey(QDynamicTable remoteDynamicTable,
String... remotePrimaryKeys) {
return addForeignKey(remoteDynamicTable,
Arrays.stream(remotePrimaryKeys).map((Function<String, Path<?>>)
s -> (Path<?>) remoteDynamicTable.getColumnByName(s, Object.class))
.collect(Collectors.toList()));
}

@Override
public QTableBuilder buildForeignKey(String remoteDynamicTableName,
String... remotePrimaryKeys) {
return buildForeignKey(getDynamicTable(remoteDynamicTableName), remotePrimaryKeys);
public QTableBuilder addForeignKey(String remoteDynamicTableName,
String... remotePrimaryKeys) {
return addForeignKey(getDynamicTable(remoteDynamicTableName), remotePrimaryKeys);
}

@Override
public QTableBuilder buildForeignKey(RelationalPath<?> remoteQTable) {
return buildForeignKey(remoteQTable, PrimaryKeyHelper
public QTableBuilder addForeignKey(RelationalPath<?> remoteQTable) {
return addForeignKey(remoteQTable, PrimaryKeyHelper
.getPrimaryKeyColumns(remoteQTable).stream()
.map((Function<Path<?>, Path<?>>) path -> path).collect(Collectors.toList()));
}
Expand All @@ -79,13 +79,19 @@ private QDynamicTable getDynamicTable(String remoteDynamicTableName) {
}

@Override
public QTableBuilder buildForeignKey(String remoteDynamicTableName) {
return buildForeignKey(getDynamicTable(remoteDynamicTableName));
public QTableBuilder addForeignKey(String remoteDynamicTableName) {
return addForeignKey(getDynamicTable(remoteDynamicTableName));
}

@Override
public QTableBuilder buildForeignKey(QDynamicTable remoteDynamicTable) {
return buildForeignKey(remoteDynamicTable,
public QTableBuilder addForeignKey(QDynamicTable remoteDynamicTable) {
return addForeignKey(remoteDynamicTable,
PrimaryKeyHelper.getPrimaryKeyColumns(remoteDynamicTable).toArray(new Path[0]));
}

@Override
public QTableBuilder drop() {
dynamicTable.removeForeignKey(localColumns);
return tableBuilder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,18 @@ public QIndexBuilder clustered() {
}

@Override
public QTableBuilder buildIndex() {
public QTableBuilder addIndex() {
return buildIndex(false);
}

@Override
public QTableBuilder buildUniqueIndex() {
public QTableBuilder addUniqueIndex() {
return buildIndex(true);
}

@Override
public QTableBuilder drop() {
dynamicTable.removeIndex(localColumns);
return tableBuilder;
}
}
Loading

0 comments on commit 7ae238f

Please sign in to comment.