This commit is contained in:
@@ -93,6 +93,7 @@ public abstract class ConvertFactory<R extends Reader, W extends Writer> {
|
|||||||
this.register(String.class, StringSimpledCoder.instance);
|
this.register(String.class, StringSimpledCoder.instance);
|
||||||
this.register(StringConvertWrapper.class, StringConvertWrapperSimpledCoder.instance);
|
this.register(StringConvertWrapper.class, StringConvertWrapperSimpledCoder.instance);
|
||||||
this.register(CharSequence.class, CharSequenceSimpledCoder.instance);
|
this.register(CharSequence.class, CharSequenceSimpledCoder.instance);
|
||||||
|
this.register(StringBuilder.class, CharSequenceSimpledCoder.StringBuilderSimpledCoder.instance);
|
||||||
this.register(java.util.Date.class, DateSimpledCoder.instance);
|
this.register(java.util.Date.class, DateSimpledCoder.instance);
|
||||||
this.register(java.time.Duration.class, DurationSimpledCoder.instance);
|
this.register(java.time.Duration.class, DurationSimpledCoder.instance);
|
||||||
this.register(AtomicInteger.class, AtomicIntegerSimpledCoder.instance);
|
this.register(AtomicInteger.class, AtomicIntegerSimpledCoder.instance);
|
||||||
|
|||||||
@@ -8,9 +8,11 @@ package org.redkale.convert.ext;
|
|||||||
import org.redkale.convert.*;
|
import org.redkale.convert.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* CharSequence 的SimpledCoder实现
|
* CharSequence 的SimpledCoder实现
|
||||||
|
*
|
||||||
|
* <p>
|
||||||
|
* 详情见: https://redkale.org
|
||||||
*
|
*
|
||||||
* <p> 详情见: https://redkale.org
|
|
||||||
* @author zhangjx
|
* @author zhangjx
|
||||||
* @param <R> Reader输入的子类型
|
* @param <R> Reader输入的子类型
|
||||||
* @param <W> Writer输出的子类型
|
* @param <W> Writer输出的子类型
|
||||||
@@ -28,4 +30,20 @@ public class CharSequenceSimpledCoder<R extends Reader, W extends Writer> extend
|
|||||||
public CharSequence convertFrom(R in) {
|
public CharSequence convertFrom(R in) {
|
||||||
return in.readString();
|
return in.readString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static class StringBuilderSimpledCoder<R extends Reader, W extends Writer> extends SimpledCoder<R, W, StringBuilder> {
|
||||||
|
|
||||||
|
public static final StringBuilderSimpledCoder instance = new StringBuilderSimpledCoder();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void convertTo(W out, StringBuilder value) {
|
||||||
|
out.writeString(value == null ? null : value.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public StringBuilder convertFrom(R in) {
|
||||||
|
String rs = in.readString();
|
||||||
|
return rs == null ? null : new StringBuilder(rs);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,11 +9,13 @@ import java.io.Serializable;
|
|||||||
import static org.redkale.source.ColumnExpress.*;
|
import static org.redkale.source.ColumnExpress.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 作为ColumnValue的value字段值,用于复杂的字段表达式
|
* 作为ColumnValue的value字段值,用于复杂的字段表达式 。
|
||||||
|
* String 视为 字段名
|
||||||
|
* Number 视为 数值
|
||||||
*
|
*
|
||||||
* @author zhangjx
|
* @author zhangjx
|
||||||
*/
|
*/
|
||||||
public class ColumnNode implements Serializable {
|
public class ColumnNodeValue implements Serializable {
|
||||||
|
|
||||||
private Serializable left;//类型只能是String、Number、ColumnNode
|
private Serializable left;//类型只能是String、Number、ColumnNode
|
||||||
|
|
||||||
@@ -21,70 +23,70 @@ public class ColumnNode implements Serializable {
|
|||||||
|
|
||||||
private Serializable right;//类型只能是String、Number、ColumnNode
|
private Serializable right;//类型只能是String、Number、ColumnNode
|
||||||
|
|
||||||
public ColumnNode() {
|
public ColumnNodeValue() {
|
||||||
}
|
}
|
||||||
|
|
||||||
public ColumnNode(Serializable left, ColumnExpress express, Serializable right) {
|
public ColumnNodeValue(Serializable left, ColumnExpress express, Serializable right) {
|
||||||
if (express == null || express == ColumnExpress.MOV) throw new IllegalArgumentException("express cannot be null or MOV");
|
if (express == null || express == ColumnExpress.MOV) throw new IllegalArgumentException("express cannot be null or MOV");
|
||||||
this.left = left;
|
this.left = left;
|
||||||
this.express = express;
|
this.express = express;
|
||||||
this.right = right;
|
this.right = right;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ColumnNode create(Serializable left, ColumnExpress express, Serializable right) {
|
public static ColumnNodeValue create(Serializable left, ColumnExpress express, Serializable right) {
|
||||||
return new ColumnNode(left, express, right);
|
return new ColumnNodeValue(left, express, right);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ColumnNode inc(Serializable left, Serializable right) {
|
public static ColumnNodeValue inc(Serializable left, Serializable right) {
|
||||||
return new ColumnNode(left, INC, right);
|
return new ColumnNodeValue(left, INC, right);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ColumnNode mul(Serializable left, Serializable right) {
|
public static ColumnNodeValue mul(Serializable left, Serializable right) {
|
||||||
return new ColumnNode(left, MUL, right);
|
return new ColumnNodeValue(left, MUL, right);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ColumnNode div(Serializable left, Serializable right) {
|
public static ColumnNodeValue div(Serializable left, Serializable right) {
|
||||||
return new ColumnNode(left, DIV, right);
|
return new ColumnNodeValue(left, DIV, right);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ColumnNode mod(Serializable left, Serializable right) {
|
public static ColumnNodeValue mod(Serializable left, Serializable right) {
|
||||||
return new ColumnNode(left, MOD, right);
|
return new ColumnNodeValue(left, MOD, right);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ColumnNode and(Serializable left, Serializable right) {
|
public static ColumnNodeValue and(Serializable left, Serializable right) {
|
||||||
return new ColumnNode(left, AND, right);
|
return new ColumnNodeValue(left, AND, right);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ColumnNode orr(Serializable left, Serializable right) {
|
public static ColumnNodeValue orr(Serializable left, Serializable right) {
|
||||||
return new ColumnNode(left, ORR, right);
|
return new ColumnNodeValue(left, ORR, right);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ColumnNode inc(Serializable right) {
|
public ColumnNodeValue inc(Serializable right) {
|
||||||
return any(INC, right);
|
return any(INC, right);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ColumnNode mul(Serializable right) {
|
public ColumnNodeValue mul(Serializable right) {
|
||||||
return any(MUL, right);
|
return any(MUL, right);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ColumnNode div(Serializable right) {
|
public ColumnNodeValue div(Serializable right) {
|
||||||
return any(DIV, right);
|
return any(DIV, right);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ColumnNode mod(Serializable right) {
|
public ColumnNodeValue mod(Serializable right) {
|
||||||
return any(MOD, right);
|
return any(MOD, right);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ColumnNode and(Serializable right) {
|
public ColumnNodeValue and(Serializable right) {
|
||||||
return any(AND, right);
|
return any(AND, right);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ColumnNode orr(Serializable right) {
|
public ColumnNodeValue orr(Serializable right) {
|
||||||
return any(ORR, right);
|
return any(ORR, right);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected ColumnNode any(ColumnExpress express, Serializable right) {
|
protected ColumnNodeValue any(ColumnExpress express, Serializable right) {
|
||||||
ColumnNode one = new ColumnNode(this.left, this.express, this.right);
|
ColumnNodeValue one = new ColumnNodeValue(this.left, this.express, this.right);
|
||||||
this.left = one;
|
this.left = one;
|
||||||
this.express = express;
|
this.express = express;
|
||||||
this.right = right;
|
this.right = right;
|
||||||
@@ -602,54 +602,15 @@ public final class EntityCache<T> {
|
|||||||
Serializable newval = null;
|
Serializable newval = null;
|
||||||
switch (express) {
|
switch (express) {
|
||||||
case INC:
|
case INC:
|
||||||
numb = (Number) attr.get(entity);
|
|
||||||
if (numb == null) {
|
|
||||||
numb = (Number) val;
|
|
||||||
} else {
|
|
||||||
numb = numb.longValue() + ((Number) val).longValue();
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case MUL:
|
case MUL:
|
||||||
numb = (Number) attr.get(entity);
|
|
||||||
if (numb == null) {
|
|
||||||
numb = 0;
|
|
||||||
} else {
|
|
||||||
numb = numb.longValue() * ((Number) val).floatValue();
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case DIV:
|
case DIV:
|
||||||
numb = (Number) attr.get(entity);
|
|
||||||
if (numb == null) {
|
|
||||||
numb = 0;
|
|
||||||
} else {
|
|
||||||
numb = numb.longValue() / ((Number) val).floatValue();
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case MOD:
|
case MOD:
|
||||||
numb = (Number) attr.get(entity);
|
|
||||||
if (numb == null) {
|
|
||||||
numb = 0;
|
|
||||||
} else {
|
|
||||||
numb = numb.longValue() % ((Number) val).intValue();
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case AND:
|
case AND:
|
||||||
numb = (Number) attr.get(entity);
|
|
||||||
if (numb == null) {
|
|
||||||
numb = 0;
|
|
||||||
} else {
|
|
||||||
numb = numb.longValue() & ((Number) val).longValue();
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case ORR:
|
case ORR:
|
||||||
numb = (Number) attr.get(entity);
|
numb = getValue((Number) attr.get(entity), express, val);
|
||||||
if (numb == null) {
|
|
||||||
numb = 0;
|
|
||||||
} else {
|
|
||||||
numb = numb.longValue() | ((Number) val).longValue();
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case MOV:
|
case MOV:
|
||||||
|
if (val instanceof ColumnNodeValue) val = updateColumnNodeValue(attr, entity, (ColumnNodeValue) val);
|
||||||
newval = val;
|
newval = val;
|
||||||
if (val instanceof Number) numb = (Number) val;
|
if (val instanceof Number) numb = (Number) val;
|
||||||
break;
|
break;
|
||||||
@@ -683,6 +644,70 @@ public final class EntityCache<T> {
|
|||||||
return entity;
|
return entity;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private <V> Serializable updateColumnNodeValue(Attribute<T, V> attr, final T entity, ColumnNodeValue node) {
|
||||||
|
Serializable left = node.getLeft();
|
||||||
|
if (left instanceof CharSequence) {
|
||||||
|
left = info.getUpdateAttribute(left.toString()).get(entity);
|
||||||
|
} else if (left instanceof ColumnNodeValue) {
|
||||||
|
left = updateColumnNodeValue(attr, entity, (ColumnNodeValue) left);
|
||||||
|
}
|
||||||
|
Serializable right = node.getRight();
|
||||||
|
if (left instanceof CharSequence) {
|
||||||
|
right = info.getUpdateAttribute(right.toString()).get(entity);
|
||||||
|
} else if (left instanceof ColumnNodeValue) {
|
||||||
|
right = updateColumnNodeValue(attr, entity, (ColumnNodeValue) right);
|
||||||
|
}
|
||||||
|
return getValue((Number) left, node.getExpress(), right);
|
||||||
|
}
|
||||||
|
|
||||||
|
private <V> Number getValue(Number numb, final ColumnExpress express, Serializable val) {
|
||||||
|
switch (express) {
|
||||||
|
case INC:
|
||||||
|
if (numb == null) {
|
||||||
|
numb = (Number) val;
|
||||||
|
} else {
|
||||||
|
numb = numb.longValue() + ((Number) val).longValue();
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case MUL:
|
||||||
|
if (numb == null) {
|
||||||
|
numb = 0;
|
||||||
|
} else {
|
||||||
|
numb = numb.longValue() * ((Number) val).floatValue();
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case DIV:
|
||||||
|
if (numb == null) {
|
||||||
|
numb = 0;
|
||||||
|
} else {
|
||||||
|
numb = numb.longValue() / ((Number) val).floatValue();
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case MOD:
|
||||||
|
if (numb == null) {
|
||||||
|
numb = 0;
|
||||||
|
} else {
|
||||||
|
numb = numb.longValue() % ((Number) val).intValue();
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case AND:
|
||||||
|
if (numb == null) {
|
||||||
|
numb = 0;
|
||||||
|
} else {
|
||||||
|
numb = numb.longValue() & ((Number) val).longValue();
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case ORR:
|
||||||
|
if (numb == null) {
|
||||||
|
numb = 0;
|
||||||
|
} else {
|
||||||
|
numb = numb.longValue() | ((Number) val).longValue();
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return numb;
|
||||||
|
}
|
||||||
|
|
||||||
public Attribute<T, Serializable> getAttribute(String fieldname) {
|
public Attribute<T, Serializable> getAttribute(String fieldname) {
|
||||||
return info.getAttribute(fieldname);
|
return info.getAttribute(fieldname);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -999,7 +999,7 @@ public final class EntityInfo<T> {
|
|||||||
protected CharSequence formatSQLValue(String sqlColumn, Attribute<T, Serializable> attr, final ColumnValue cv, BiFunction<EntityInfo, Object, CharSequence> formatter) {
|
protected CharSequence formatSQLValue(String sqlColumn, Attribute<T, Serializable> attr, final ColumnValue cv, BiFunction<EntityInfo, Object, CharSequence> formatter) {
|
||||||
if (cv == null) return null;
|
if (cv == null) return null;
|
||||||
Object val = cv.getValue();
|
Object val = cv.getValue();
|
||||||
if (val instanceof ColumnNode && cv.getExpress() == ColumnExpress.MOV) return formatSQLValue(attr, (ColumnNode) val, formatter);
|
if (val instanceof ColumnNodeValue && cv.getExpress() == ColumnExpress.MOV) return formatSQLValue(attr, (ColumnNodeValue) val, formatter);
|
||||||
switch (cv.getExpress()) {
|
switch (cv.getExpress()) {
|
||||||
case INC:
|
case INC:
|
||||||
return new StringBuilder().append(sqlColumn).append(" + ").append(val);
|
return new StringBuilder().append(sqlColumn).append(" + ").append(val);
|
||||||
@@ -1023,18 +1023,18 @@ public final class EntityInfo<T> {
|
|||||||
return formatter == null ? formatToString(val) : formatter.apply(this, val);
|
return formatter == null ? formatToString(val) : formatter.apply(this, val);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected CharSequence formatSQLValue(Attribute<T, Serializable> attr, final ColumnNode node, BiFunction<EntityInfo, Object, CharSequence> formatter) {
|
protected CharSequence formatSQLValue(Attribute<T, Serializable> attr, final ColumnNodeValue node, BiFunction<EntityInfo, Object, CharSequence> formatter) {
|
||||||
Serializable left = node.getLeft();
|
Serializable left = node.getLeft();
|
||||||
if (left instanceof CharSequence) {
|
if (left instanceof CharSequence) {
|
||||||
left = this.getSQLColumn(null, left.toString());
|
left = this.getSQLColumn(null, left.toString());
|
||||||
} else if (left instanceof ColumnNode) {
|
} else if (left instanceof ColumnNodeValue) {
|
||||||
left = "(" + formatSQLValue(attr, (ColumnNode) left, formatter) + ")";
|
left = "(" + formatSQLValue(attr, (ColumnNodeValue) left, formatter) + ")";
|
||||||
}
|
}
|
||||||
Serializable right = node.getRight();
|
Serializable right = node.getRight();
|
||||||
if (right instanceof CharSequence) {
|
if (right instanceof CharSequence) {
|
||||||
right = this.getSQLColumn(null, right.toString());
|
right = this.getSQLColumn(null, right.toString());
|
||||||
} else if (left instanceof ColumnNode) {
|
} else if (left instanceof ColumnNodeValue) {
|
||||||
right = "(" + formatSQLValue(attr, (ColumnNode) right, formatter) + ")";
|
right = "(" + formatSQLValue(attr, (ColumnNodeValue) right, formatter) + ")";
|
||||||
}
|
}
|
||||||
switch (node.getExpress()) {
|
switch (node.getExpress()) {
|
||||||
case INC:
|
case INC:
|
||||||
|
|||||||
Reference in New Issue
Block a user