Skip to content

Commit

Permalink
refactor: 优化表名和字段转义逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
zhou-hao committed Jun 27, 2024
1 parent 017df98 commit 36bdddc
Show file tree
Hide file tree
Showing 25 changed files with 291 additions and 182 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,27 @@
@Setter
public abstract class AbstractColumnMetadata implements ColumnMetadata {
protected String name;
protected String realName;
protected String alias;
protected String comment;
protected Class<?> javaType;
protected boolean updatable;
protected boolean notNull;
protected DictionaryCodec dictionaryCodec;
protected ValueCodec<?,?> valueCodec;
protected ValueCodec<?, ?> valueCodec;
protected DefaultValue defaultValue;
protected Map<String, Object> properties = new ConcurrentHashMap<>();

private Map<String, Feature> features = new ConcurrentHashMap<>();

public String getRealName() {
return realName == null ? name : realName;
}

public boolean realNameDetected() {
return realName != null;
}

@Override
public String getAlias() {
if (alias == null) {
Expand Down Expand Up @@ -86,6 +95,6 @@ public void addFeature(Feature feature) {
@Override
@SneakyThrows
public ObjectMetadata clone() {
return (ObjectMetadata)super.clone();
return (ObjectMetadata) super.clone();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
public interface ColumnMetadata extends FeatureSupportedMetadata, ObjectMetadata, Cloneable {
String getName();

String getRealName();

String getAlias();

String getComment();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public interface SchemaMetadata extends ObjectMetadata, FeatureSupportedMetadata

<T extends ObjectMetadata> Optional<T> getObject(ObjectType type, String name);

<T extends ObjectMetadata> Optional<T> getObject(ObjectType type, String name,boolean autoLoad);
<T extends ObjectMetadata> Optional<T> getObject(ObjectType type, String name, boolean autoLoad);

<T extends ObjectMetadata> Optional<T> removeObject(ObjectType type, String name);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ protected void doWrap(T instance, String column, Object value) {
if (!columnFilter.test(column)) {
return;
}
if (wrapperNestObject && column.contains(".")) {
if (wrapperNestObject && column.contains(".") && !column.startsWith(".")) {
String[] attrs = column.split("[.]", 2);
if (!columnFilter.test(attrs[0])) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public ReactiveQuery<E> createQuery() {
public ReactiveUpdate<E> createUpdate() {
return new DefaultReactiveUpdate<>(
getTable()
, operator.dml().update(getTable().getFullName())
, operator.dml().update(getTable())
, mapping
, this::applyContext
, getDefaultContextKeyValue());
Expand All @@ -135,7 +135,7 @@ public ReactiveUpdate<E> createUpdate() {
@Override
public ReactiveDelete createDelete() {
return new DefaultReactiveDelete(getTable()
, operator.dml().delete(getTable().getFullName())
, operator.dml().delete(getTable())
, this::applyContext
, getDefaultContextKeyValue()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ private Object getProperty(E data, String property) {
protected SaveResultOperator doSave(Collection<E> data) {
Collection<E> _data = tryMergeDuplicate(data);
RDBTableMetadata table = getTable();
UpsertOperator upsert = operator.dml().upsert(table.getFullName());
UpsertOperator upsert = operator.dml().upsert(table);

return EventResultOperator.create(
() -> {
Expand All @@ -188,7 +188,7 @@ protected SaveResultOperator doSave(Collection<E> data) {

protected InsertResultOperator doInsert(E data) {
RDBTableMetadata table = getTable();
InsertOperator insert = operator.dml().insert(table.getFullName());
InsertOperator insert = operator.dml().insert(table);

return EventResultOperator.create(
() -> {
Expand Down Expand Up @@ -238,7 +238,7 @@ private Object getInsertColumnValue(E data, String property) {
protected InsertResultOperator doInsert(Collection<E> batch) {
Collection<E> _data = tryMergeDuplicate(batch);
RDBTableMetadata table = getTable();
InsertOperator insert = operator.dml().insert(table.getFullName());
InsertOperator insert = operator.dml().insert(table);

return EventResultOperator.create(
() -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,12 @@ public SyncQuery<E> createQuery() {

@Override
public SyncUpdate<E> createUpdate() {
return new DefaultSyncUpdate<>(getTable(), operator.dml().update(getTable().getFullName()), mapping);
return new DefaultSyncUpdate<>(getTable(), operator.dml().update(getTable()), mapping);
}

@Override
public SyncDelete createDelete() {
return new DefaultSyncDelete(getTable(), operator.dml().delete(getTable().getFullName()));
return new DefaultSyncDelete(getTable(), operator.dml().delete(getTable()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,40 +20,48 @@ public Optional<Object> get(String key) {
@Override
public Optional<String> getString(String key) {
return get(key)
.map(String::valueOf);
.map(String::valueOf);
}

@Override
public Optional<Integer> getInteger(String key) {
return get(key)
.map(Number.class::cast)
.map(Number::intValue);
.map(Number.class::cast)
.map(Number::intValue);
}

@Override
public Optional<Boolean> getBoolean(String key) {
return get(key)
.map(val -> Boolean.TRUE.equals(val) || val.equals(1));
.map(val -> {
if (val instanceof Number) {
return ((Number) val).intValue() == 1;
}
return Boolean.TRUE.equals(val) ||
"Y".equals(val) ||
"y".equals(val) ||
"1".equals(val);
});
}

@Override
public Optional<Date> getDate(String key) {
return get(key)
.map(Date.class::cast);
.map(Date.class::cast);
}

@Override
public Optional<Record> getNest(String key) {
return get(key)
.map(val -> {
if (val instanceof Record) {
return ((Record) val);
}
if (val instanceof Map) {
return new DefaultRecord(((Map) val));
}
throw new UnsupportedOperationException("value [" + val + "] is not nest property");
});
.map(val -> {
if (val instanceof Record) {
return ((Record) val);
}
if (val instanceof Map) {
return new DefaultRecord(((Map) val));
}
throw new UnsupportedOperationException("value [" + val + "] is not nest property");
});
}

@Override
Expand All @@ -70,17 +78,17 @@ public Record putValue(String key, Object value) {
@SuppressWarnings("all")
public Optional<List<Record>> getNests(String key) {
return get(key)
.map(val -> {
if (val instanceof Record) {
return Collections.singletonList(((Record) val));
}
if (val instanceof Map) {
return Collections.singletonList(new DefaultRecord(((Map) val)));
}
if (val instanceof Collection) {
return new ArrayList<>(((Collection) val));
}
throw new UnsupportedOperationException("value [" + val + "] is not nest property");
});
.map(val -> {
if (val instanceof Record) {
return Collections.singletonList(((Record) val));
}
if (val instanceof Map) {
return Collections.singletonList(new DefaultRecord(((Map) val)));
}
if (val instanceof Collection) {
return new ArrayList<>(((Collection) val));
}
throw new UnsupportedOperationException("value [" + val + "] is not nest property");
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,16 @@ public void wrapColumn(ColumnWrapperContext<Record> context) {

if (mapping != null) {
Record record = context.getRowInstance();

String property = mapping
.getPropertyByColumnName(context.getColumnLabel())
.orElse(context.getColumnLabel());

.getPropertyByColumnName(context.getColumnLabel())
.orElse(null);
if (property == null) {
return;
}
Object value = mapping
.getColumnByProperty(property)
.map(columnMetadata -> columnMetadata.decode(context.getResult()))
.orElseGet(context::getResult);
.getColumnByProperty(property)
.map(columnMetadata -> columnMetadata.decode(context.getResult()))
.orElseGet(context::getResult);

super.doWrap(record, property, value);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ public void wrapColumn(ColumnWrapperContext<E> context) {
ForeignKeyMetadata metadata = mapping.getTable().getForeignKey(table).orElse(null);
if (null != metadata) {
Object value = metadata
.getTarget()
.getColumn(column)
.map(m -> m.decode(context.getResult()))
.orElseGet(context::getResult);
.getTarget()
.getColumn(column)
.map(m -> m.decode(context.getResult()))
.orElseGet(context::getResult);
GlobalConfig.getPropertyOperator().setProperty(nestInstance, column, value);
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,39 @@ public AbstractTableOrViewMetadata() {

@Override
public String getQuoteName() {
return quoteName == null ? quoteName = TableOrViewMetadata.super.getQuoteName() : quoteName;
if (quoteName == null) {
return quoteName = (
getSchema().getQuoteName()
+ "."
+ getDialect().quote(getRealName(), realName == null)
);
}
return quoteName;
}

@Override
public String getFullName() {
return fullName == null ? fullName = TableOrViewMetadata.super.getFullName() : fullName;
if (fullName == null) {
if (realName != null) {
return fullName =
StringUtils.concat(getSchema().getQuoteName(), ".", getDialect().quote(getRealName(), false));
}
return fullName = StringUtils.concat(getSchema().getQuoteName(), ".", name);

}
return fullName;
}

public String getRealName() {
return realName == null ? name : realName;
}

public void setRealName(String realName) {
this.realName = realName;
this.quoteName = null;
this.fullName = null;
}

public boolean isTable() {
return this instanceof RDBTableMetadata;
}
Expand Down Expand Up @@ -252,8 +273,16 @@ public String toString() {
@Override
public void merge(TableOrViewMetadata metadata) {
metadata.getForeignKeys().forEach(this::addForeignKey);
metadata.getFeatureList().forEach(this::addFeature);
for (Feature feature : metadata.getFeatureList()) {
features.putIfAbsent(feature.getId(), feature);
}
metadata.getColumns().forEach(this::addColumn);
if (metadata instanceof AbstractTableOrViewMetadata) {
String relName = ((AbstractTableOrViewMetadata) metadata).realName;
if (relName != null) {
this.setRealName(relName);
}
}

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public Dialect getDialect() {
}

public String getQuoteName() {
return quoteName == null ? quoteName = getDialect().quote(getName()) : quoteName;
return quoteName == null ? quoteName = getDialect().quote(getRealName(), realName == null) : quoteName;
}

public void setJdbcType(SQLType jdbcType, Class<?> javaType) {
Expand Down Expand Up @@ -265,7 +265,8 @@ public List<Feature> findFeatures(Predicate<Feature> predicate) {
}

public String createFullName0(String ownerName) {
return getDialect().buildColumnFullName(ownerName, getName());

return getDialect().buildColumnFullName(ownerName, getRealName(), realName == null);
}

public String getFullName(String ownerName) {
Expand All @@ -275,7 +276,7 @@ public String getFullName(String ownerName) {
if (Objects.equals(ownerName, getOwner().getName())) {
return getFullName();
}
return getDialect().buildColumnFullName(ownerName, getName());
return getDialect().buildColumnFullName(ownerName, getRealName(), realName == null);
}

public boolean ddlModifiable(RDBColumnMetadata after) {
Expand Down
Loading

0 comments on commit 36bdddc

Please sign in to comment.