增加ColumnXXXNode功能
This commit is contained in:
@@ -1702,11 +1702,11 @@ public abstract class AbstractDataSqlSource extends AbstractDataSource implement
|
||||
setsql.append(", ");
|
||||
}
|
||||
String sqlColumn = info.getSQLColumn(null, col.getColumn());
|
||||
if (col.getValue() instanceof byte[]) {
|
||||
if (col.getValue2() instanceof ColumnBytesNode) {
|
||||
if (blobs == null) {
|
||||
blobs = new ArrayList<>();
|
||||
}
|
||||
blobs.add((byte[]) col.getValue());
|
||||
blobs.add(((ColumnBytesNode) col.getValue2()).getValue());
|
||||
setsql.append(sqlColumn).append("=").append(prepareParamSign(++index));
|
||||
} else {
|
||||
setsql.append(sqlColumn).append("=").append(info.formatSQLValue(sqlColumn, attr, col, sqlFormatter));
|
||||
@@ -1787,11 +1787,11 @@ public abstract class AbstractDataSqlSource extends AbstractDataSource implement
|
||||
setsql.append(", ");
|
||||
}
|
||||
String sqlColumn = info.getSQLColumn(alias, col.getColumn());
|
||||
if (col.getValue() instanceof byte[]) {
|
||||
if (col.getValue2() instanceof ColumnBytesNode) {
|
||||
if (blobs == null) {
|
||||
blobs = new ArrayList<>();
|
||||
}
|
||||
blobs.add((byte[]) col.getValue());
|
||||
blobs.add(((ColumnBytesNode) col.getValue2()).getValue());
|
||||
setsql.append(sqlColumn).append("=").append(prepareParamSign(++index));
|
||||
} else {
|
||||
setsql.append(sqlColumn).append("=").append(info.formatSQLValue(sqlColumn, attr, col, sqlFormatter));
|
||||
@@ -2456,9 +2456,9 @@ public abstract class AbstractDataSqlSource extends AbstractDataSource implement
|
||||
funcSqlColumns.append(", ");
|
||||
}
|
||||
if (funcNodes[i] instanceof ColumnFuncNode) {
|
||||
funcSqlColumns.append(info.formatSQLValue((Attribute) null, "a", (ColumnFuncNode) funcNodes[i], sqlFormatter));
|
||||
funcSqlColumns.append(info.formatColumnFuncNodeSQLValue((Attribute) null, "a", (ColumnFuncNode) funcNodes[i], sqlFormatter));
|
||||
} else {
|
||||
funcSqlColumns.append(info.formatSQLValue((Attribute) null, "a", (ColumnExpNode) funcNodes[i], sqlFormatter));
|
||||
funcSqlColumns.append(info.formatColumnExpNodeSQLValue((Attribute) null, "a", (ColumnExpNode) funcNodes[i], sqlFormatter));
|
||||
}
|
||||
}
|
||||
final Map<Class, String> joinTabalis = node == null ? null : node.getJoinTabalis();
|
||||
|
||||
46
src/main/java/org/redkale/source/ColumnBytesNode.java
Normal file
46
src/main/java/org/redkale/source/ColumnBytesNode.java
Normal file
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
*
|
||||
*/
|
||||
package org.redkale.source;
|
||||
|
||||
import org.redkale.convert.ConvertColumn;
|
||||
|
||||
/**
|
||||
* byte[]的ColumnNode
|
||||
*
|
||||
* <p>
|
||||
* 详情见: https://redkale.org
|
||||
*
|
||||
* @author zhangjx
|
||||
* @since 2.8.0
|
||||
*/
|
||||
public class ColumnBytesNode implements ColumnNode {
|
||||
|
||||
@ConvertColumn(index = 1)
|
||||
private byte[] value;
|
||||
|
||||
public ColumnBytesNode() {
|
||||
}
|
||||
|
||||
public ColumnBytesNode(byte[] value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public static ColumnBytesNode create(byte[] value) {
|
||||
return new ColumnBytesNode(value);
|
||||
}
|
||||
|
||||
public byte[] getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(byte[] value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "{\"value\":" + value + "}";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -6,6 +6,7 @@
|
||||
package org.redkale.source;
|
||||
|
||||
import java.io.Serializable;
|
||||
import org.redkale.convert.ConvertColumn;
|
||||
import static org.redkale.source.ColumnExpress.*;
|
||||
|
||||
/**
|
||||
@@ -13,9 +14,9 @@ import static org.redkale.source.ColumnExpress.*;
|
||||
* String 视为 字段名 <br>
|
||||
* Number 视为 数值 <br>
|
||||
* 例如: UPDATE Reord SET updateTime = createTime + 10 WHERE id = 1 <br>
|
||||
source.updateColumn(Record.class, 1, ColumnValue.mov("updateTime", ColumnExpNode.inc("createTime", 10))); <br>
|
||||
* source.updateColumn(Record.class, 1, ColumnValue.mov("updateTime", ColumnExpNode.inc("createTime", 10))); <br>
|
||||
* 例如: UPDATE Reord SET updateTime = createTime * 10 / createCount WHERE id = 1 <br>
|
||||
source.updateColumn(Record.class, 1, ColumnValue.mov("updateTime", ColumnExpNode.div(ColumnExpNode.mul("createTime", 10), "createCount"))); <br>
|
||||
* source.updateColumn(Record.class, 1, ColumnValue.mov("updateTime", ColumnExpNode.div(ColumnExpNode.mul("createTime", 10), "createCount"))); <br>
|
||||
*
|
||||
* <p>
|
||||
* 详情见: https://redkale.org
|
||||
@@ -25,34 +26,50 @@ import static org.redkale.source.ColumnExpress.*;
|
||||
*/
|
||||
public class ColumnExpNode implements ColumnNode {
|
||||
|
||||
protected Serializable left;//类型只能是String、Number、ColumnExpNode
|
||||
@ConvertColumn(index = 1)
|
||||
protected ColumnNode left;//类型只能是ColumnNameNode、ColumnNumberNode、ColumnExpNode
|
||||
|
||||
protected ColumnExpress express; //MOV时,left必须是String, right必须是null
|
||||
@ConvertColumn(index = 2)
|
||||
protected ColumnExpress express; //MOV时,left必须是ColumnNameNode, right必须是null
|
||||
|
||||
protected Serializable right;//类型只能是String、Number、ColumnExpNode
|
||||
@ConvertColumn(index = 3)
|
||||
protected ColumnNode right;//类型只能是ColumnNameNode、ColumnNumberNode、ColumnExpNode
|
||||
|
||||
public ColumnExpNode() {
|
||||
}
|
||||
|
||||
public ColumnExpNode(Serializable left, ColumnExpress express, Serializable right) {
|
||||
protected ColumnExpNode(Serializable left, ColumnExpress express, Serializable right) {
|
||||
if (express == null) {
|
||||
throw new IllegalArgumentException("express cannot be null");
|
||||
}
|
||||
ColumnNode leftNode = createColumnNode(left);
|
||||
ColumnNode rightNode = createColumnNode(right);
|
||||
if (express == MOV) {
|
||||
if (!(left instanceof String) || right != null) {
|
||||
throw new IllegalArgumentException("left value must be String, right value must be null on ColumnExpress.MOV");
|
||||
}
|
||||
} else {
|
||||
if (!(left instanceof String) && !(left instanceof Number) && !(left instanceof ColumnExpNode) && !(left instanceof ColumnFuncNode)) {
|
||||
throw new IllegalArgumentException("left value must be String, Number, ColumnFuncNode or ColumnExpNode");
|
||||
}
|
||||
if (!(right instanceof String) && !(right instanceof Number) && !(right instanceof ColumnExpNode) && !(right instanceof ColumnFuncNode)) {
|
||||
throw new IllegalArgumentException("right value must be String, Number, ColumnFuncNode or ColumnExpNode");
|
||||
if (!(leftNode instanceof ColumnNameNode) || right != null) {
|
||||
throw new IllegalArgumentException("left value must be ColumnNameNode, right value must be null on ColumnExpress.MOV");
|
||||
}
|
||||
}
|
||||
this.left = left;
|
||||
this.left = leftNode;
|
||||
this.express = express;
|
||||
this.right = right;
|
||||
this.right = rightNode;
|
||||
}
|
||||
|
||||
private ColumnNode createColumnNode(Serializable value) {
|
||||
if (value == null) {
|
||||
return null;
|
||||
}
|
||||
if (value instanceof String) {
|
||||
return new ColumnNameNode(value.toString());
|
||||
}
|
||||
if (value instanceof Number) {
|
||||
return new ColumnNumberNode((Number) value);
|
||||
}
|
||||
if (!(value instanceof ColumnNameNode)
|
||||
&& !(value instanceof ColumnNumberNode)
|
||||
&& !(value instanceof ColumnExpNode)) {
|
||||
throw new IllegalArgumentException("value must be ColumnNameNode、ColumnNumberNode、ColumnExpNode");
|
||||
}
|
||||
return (ColumnNode) value;
|
||||
}
|
||||
|
||||
public static ColumnExpNode create(Serializable left, ColumnExpress express, Serializable right) {
|
||||
@@ -123,15 +140,15 @@ public class ColumnExpNode implements ColumnNode {
|
||||
ColumnExpNode one = new ColumnExpNode(this.left, this.express, this.right);
|
||||
this.left = one;
|
||||
this.express = express;
|
||||
this.right = right;
|
||||
this.right = createColumnNode(right);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Serializable getLeft() {
|
||||
public ColumnNode getLeft2() {
|
||||
return left;
|
||||
}
|
||||
|
||||
public void setLeft(Serializable left) {
|
||||
public void setLeft(ColumnNode left) {
|
||||
this.left = left;
|
||||
}
|
||||
|
||||
@@ -143,16 +160,16 @@ public class ColumnExpNode implements ColumnNode {
|
||||
this.express = express;
|
||||
}
|
||||
|
||||
public Serializable getRight() {
|
||||
public ColumnNode getRight2() {
|
||||
return right;
|
||||
}
|
||||
|
||||
public void setRight(Serializable right) {
|
||||
public void setRight(ColumnNode right) {
|
||||
this.right = right;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "{\"column\":" + ((left instanceof CharSequence) ? ("\"" + left + "\"") : left) + ", \"express\":" + express + ", \"value\":" + ((right instanceof CharSequence) ? ("\"" + right + "\"") : right) + "}";
|
||||
return "{\"column\":" + left + ", \"express\":" + express + ", \"value\":" + right + "}";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,10 +6,10 @@
|
||||
package org.redkale.source;
|
||||
|
||||
import java.io.Serializable;
|
||||
import org.redkale.convert.ConvertColumn;
|
||||
|
||||
/**
|
||||
* 与ColumnExpNode 组合,用于复杂的字段表达式 。
|
||||
* String 视为 字段名
|
||||
* 与ColumnNameNode、ColumnExpNode组合,用于复杂的字段表达式 。
|
||||
*
|
||||
* <p>
|
||||
* 详情见: https://redkale.org
|
||||
@@ -19,19 +19,32 @@ import java.io.Serializable;
|
||||
*/
|
||||
public class ColumnFuncNode implements ColumnNode {
|
||||
|
||||
@ConvertColumn(index = 1)
|
||||
protected FilterFunc func;
|
||||
|
||||
protected Serializable value;//类型只能是String、ColumnExpNode
|
||||
@ConvertColumn(index = 2)
|
||||
protected ColumnNode value;//类型只能是ColumnNameNode、ColumnExpNode、ColumnFuncNode
|
||||
|
||||
public ColumnFuncNode() {
|
||||
}
|
||||
|
||||
public ColumnFuncNode(FilterFunc func, Serializable node) {
|
||||
if (!(node instanceof String) && !(node instanceof ColumnExpNode)) {
|
||||
throw new IllegalArgumentException("value must be String or ColumnExpNode");
|
||||
}
|
||||
protected ColumnFuncNode(FilterFunc func, Serializable node) {
|
||||
this.func = func;
|
||||
this.value = node;
|
||||
this.value = createColumnNode(node);
|
||||
}
|
||||
|
||||
protected ColumnNode createColumnNode(Serializable value) {
|
||||
if (value instanceof String) {
|
||||
return new ColumnNameNode(value.toString());
|
||||
} else if (value instanceof ColumnNameNode) {
|
||||
return (ColumnNode) value;
|
||||
} else if (value instanceof ColumnExpNode) {
|
||||
return (ColumnNode) value;
|
||||
} else if (value instanceof ColumnFuncNode) {
|
||||
return (ColumnNode) value;
|
||||
} else {
|
||||
throw new IllegalArgumentException("value must be ColumnNameNode or ColumnExpNode or ColumnFuncNode");
|
||||
}
|
||||
}
|
||||
|
||||
public static ColumnFuncNode create(FilterFunc func, Serializable node) {
|
||||
@@ -70,16 +83,16 @@ public class ColumnFuncNode implements ColumnNode {
|
||||
this.func = func;
|
||||
}
|
||||
|
||||
public Serializable getValue() {
|
||||
public ColumnNode getValue2() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(Serializable value) {
|
||||
public void setValue(ColumnNode value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "{\"func\":\"" + func + "\", \"value\":" + ((value instanceof CharSequence) ? ("\"" + value + "\"") : value) + "}";
|
||||
return "{\"func\":\"" + func + "\", \"value\":" + value + "}";
|
||||
}
|
||||
}
|
||||
|
||||
48
src/main/java/org/redkale/source/ColumnNameNode.java
Normal file
48
src/main/java/org/redkale/source/ColumnNameNode.java
Normal file
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
*
|
||||
*/
|
||||
package org.redkale.source;
|
||||
|
||||
import java.util.Objects;
|
||||
import org.redkale.convert.ConvertColumn;
|
||||
|
||||
/**
|
||||
* 字段名的ColumnNode
|
||||
*
|
||||
* <p>
|
||||
* 详情见: https://redkale.org
|
||||
*
|
||||
* @author zhangjx
|
||||
* @since 2.8.0
|
||||
*/
|
||||
public class ColumnNameNode implements ColumnNode {
|
||||
|
||||
@ConvertColumn(index = 1)
|
||||
private String column;
|
||||
|
||||
public ColumnNameNode() {
|
||||
}
|
||||
|
||||
public ColumnNameNode(String column) {
|
||||
Objects.requireNonNull(column, "column is null");
|
||||
this.column = column;
|
||||
}
|
||||
|
||||
public static ColumnNameNode create(String column) {
|
||||
return new ColumnNameNode(column);
|
||||
}
|
||||
|
||||
public String getColumn() {
|
||||
return column;
|
||||
}
|
||||
|
||||
public void setColumn(String column) {
|
||||
this.column = column;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "{\"column\":\"" + column + "\"}";
|
||||
}
|
||||
|
||||
}
|
||||
48
src/main/java/org/redkale/source/ColumnNumberNode.java
Normal file
48
src/main/java/org/redkale/source/ColumnNumberNode.java
Normal file
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
*
|
||||
*/
|
||||
package org.redkale.source;
|
||||
|
||||
import java.util.Objects;
|
||||
import org.redkale.convert.ConvertColumn;
|
||||
|
||||
/**
|
||||
* 数值的ColumnNode
|
||||
*
|
||||
* <p>
|
||||
* 详情见: https://redkale.org
|
||||
*
|
||||
* @author zhangjx
|
||||
* @since 2.8.0
|
||||
*/
|
||||
public class ColumnNumberNode implements ColumnNode {
|
||||
|
||||
@ConvertColumn(index = 1)
|
||||
private Number value;
|
||||
|
||||
public ColumnNumberNode() {
|
||||
}
|
||||
|
||||
public ColumnNumberNode(Number value) {
|
||||
Objects.requireNonNull(value, "number is null");
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public static ColumnNumberNode create(Number value) {
|
||||
return new ColumnNumberNode(value);
|
||||
}
|
||||
|
||||
public Number getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(Number value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "{\"value\":" + value + "}";
|
||||
}
|
||||
|
||||
}
|
||||
48
src/main/java/org/redkale/source/ColumnStringNode.java
Normal file
48
src/main/java/org/redkale/source/ColumnStringNode.java
Normal file
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
*
|
||||
*/
|
||||
package org.redkale.source;
|
||||
|
||||
import java.util.Objects;
|
||||
import org.redkale.convert.ConvertColumn;
|
||||
|
||||
/**
|
||||
* 字符串的ColumnNode
|
||||
*
|
||||
* <p>
|
||||
* 详情见: https://redkale.org
|
||||
*
|
||||
* @author zhangjx
|
||||
* @since 2.8.0
|
||||
*/
|
||||
public class ColumnStringNode implements ColumnNode {
|
||||
|
||||
@ConvertColumn(index = 1)
|
||||
private String value;
|
||||
|
||||
public ColumnStringNode() {
|
||||
}
|
||||
|
||||
public ColumnStringNode(String value) {
|
||||
Objects.requireNonNull(value, "string value is null");
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public static ColumnStringNode create(String value) {
|
||||
return new ColumnStringNode(value);
|
||||
}
|
||||
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "{\"value\":\"" + value + "\"}";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -13,7 +13,7 @@ import org.redkale.util.*;
|
||||
|
||||
/**
|
||||
* ColumnValue主要用于多个字段更新的表达式。
|
||||
* value值一般为: ColumnExpNode、ColumnFuncNode、Number、String等 <br>
|
||||
* value值一般为:ColumnExpNode、ColumnFuncNode、ColumnNameNode、ColumnNumberNode、ColumnStringNode、ColumnBytesNode <br>
|
||||
* 用于 DataSource.updateColumn 方法 <br>
|
||||
*
|
||||
* <p>
|
||||
@@ -30,33 +30,40 @@ public class ColumnValue {
|
||||
private ColumnExpress express;
|
||||
|
||||
@ConvertColumn(index = 3)
|
||||
private Serializable value;
|
||||
private ColumnNode value;
|
||||
|
||||
public ColumnValue() {
|
||||
}
|
||||
|
||||
private <T extends Serializable> ColumnValue(LambdaSupplier<T> func) {
|
||||
this(LambdaSupplier.readColumn(func), ColumnExpress.MOV, func.get());
|
||||
}
|
||||
|
||||
public <T extends Serializable> ColumnValue(LambdaSupplier<T> func, ColumnExpress express) {
|
||||
protected <T extends Serializable> ColumnValue(LambdaSupplier<T> func, ColumnExpress express) {
|
||||
this(LambdaSupplier.readColumn(func), express, func.get());
|
||||
}
|
||||
|
||||
public <T> ColumnValue(LambdaFunction<T, ?> func, ColumnExpress express, Serializable value) {
|
||||
protected <T> ColumnValue(LambdaFunction<T, ?> func, ColumnExpress express, Serializable value) {
|
||||
this(LambdaFunction.readColumn(func), express, value);
|
||||
}
|
||||
|
||||
private ColumnValue(String column, Serializable value) {
|
||||
this(column, ColumnExpress.MOV, value);
|
||||
}
|
||||
|
||||
public ColumnValue(String column, ColumnExpress express, Serializable value) {
|
||||
protected ColumnValue(String column, ColumnExpress express, Serializable value) {
|
||||
Objects.requireNonNull(column);
|
||||
Objects.requireNonNull(express);
|
||||
this.column = column;
|
||||
this.express = express;
|
||||
this.value = value;
|
||||
if (value instanceof String) {
|
||||
this.value = new ColumnStringNode(value.toString());
|
||||
} else if (value instanceof Number) {
|
||||
this.value = new ColumnNumberNode((Number) value);
|
||||
} else if (value instanceof ColumnExpNode
|
||||
|| value instanceof ColumnFuncNode
|
||||
|| value instanceof ColumnNameNode
|
||||
|| value instanceof ColumnNumberNode
|
||||
|| value instanceof ColumnStringNode
|
||||
|| value instanceof ColumnBytesNode) {
|
||||
this.value = (ColumnNode) value;
|
||||
} else if (value == null) {
|
||||
this.value = null;
|
||||
} else {
|
||||
throw new IllegalArgumentException("Not supported value: " + value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -68,7 +75,7 @@ public class ColumnValue {
|
||||
* @return ColumnValue
|
||||
*/
|
||||
public static ColumnValue create(String column, Serializable value) {
|
||||
return new ColumnValue(column, value);
|
||||
return new ColumnValue(column, MOV, value);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -205,7 +212,7 @@ public class ColumnValue {
|
||||
* @since 2.8.0
|
||||
*/
|
||||
public static <T extends Serializable> ColumnValue create(LambdaSupplier<T> func) {
|
||||
return new ColumnValue(func);
|
||||
return new ColumnValue(func, MOV);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -470,16 +477,16 @@ public class ColumnValue {
|
||||
this.express = express;
|
||||
}
|
||||
|
||||
public Serializable getValue() {
|
||||
public ColumnNode getValue2() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(Serializable value) {
|
||||
public void setValue(ColumnNode value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "{\"column\":\"" + column + "\", \"express\":" + express + ", \"value\":" + ((value instanceof CharSequence) ? ("\"" + value + "\"") : value) + "}";
|
||||
return "{\"column\":\"" + column + "\", \"express\":" + express + ", \"value\":" + value + "}";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -536,30 +536,31 @@ public final class EntityCache<T> {
|
||||
|
||||
private Number queryColumnNumber(final List<T> list, final ColumnNode funcNode) {
|
||||
if (funcNode instanceof ColumnFuncNode) {
|
||||
return queryColumnNumber(list, (ColumnFuncNode) funcNode);
|
||||
return queryColumnFuncNodeNumber(list, (ColumnFuncNode) funcNode);
|
||||
} else if (funcNode instanceof ColumnExpNode) {
|
||||
return queryColumnNumber(list, (ColumnExpNode) funcNode);
|
||||
return queryColumnExpNodeNumber(list, (ColumnExpNode) funcNode);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private Number queryColumnNumber(final List<T> list, final ColumnFuncNode funcNode) {
|
||||
if (funcNode.getValue() instanceof String) {
|
||||
final Attribute<T, Serializable> attr = info.getAttribute((String) funcNode.getValue());
|
||||
private Number queryColumnFuncNodeNumber(final List<T> list, final ColumnFuncNode funcNode) {
|
||||
if (funcNode.getValue2() instanceof ColumnNameNode) {
|
||||
final Attribute<T, Serializable> attr = info.getAttribute(((ColumnNameNode) funcNode.getValue2()).getColumn());
|
||||
final Function<T, Number> attrFunc = x -> (Number) attr.get(x);
|
||||
return getNumberResult(list, funcNode.getFunc(), null, attr.type(), attrFunc, (FilterNode) null);
|
||||
}
|
||||
Number num = null;
|
||||
if (funcNode.getValue() instanceof ColumnFuncNode) {
|
||||
num = queryColumnNumber(list, (ColumnFuncNode) funcNode.getValue());
|
||||
} else if (funcNode.getValue() instanceof ColumnExpNode) {
|
||||
num = queryColumnNumber(list, (ColumnExpNode) funcNode.getValue());
|
||||
if (funcNode.getValue2() instanceof ColumnFuncNode) {
|
||||
num = queryColumnFuncNodeNumber(list, (ColumnFuncNode) funcNode.getValue2());
|
||||
} else if (funcNode.getValue2() instanceof ColumnExpNode) {
|
||||
num = queryColumnExpNodeNumber(list, (ColumnExpNode) funcNode.getValue2());
|
||||
}
|
||||
return num;
|
||||
}
|
||||
|
||||
private Number queryColumnNumber(final List<T> list, final ColumnExpNode nodeValue) {
|
||||
private Number queryColumnExpNodeNumber(final List<T> list, final ColumnExpNode nodeValue) {
|
||||
//TODO 尚未实现
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -889,7 +890,7 @@ public final class EntityCache<T> {
|
||||
return rms;
|
||||
}
|
||||
|
||||
public <V> T updateColumn(final Serializable pk, List<Attribute<T, Serializable>> attrs, final List<ColumnValue> values) {
|
||||
public T updateColumn(final Serializable pk, List<Attribute<T, Serializable>> attrs, final List<ColumnValue> values) {
|
||||
if (pk == null || attrs == null || attrs.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
@@ -901,7 +902,7 @@ public final class EntityCache<T> {
|
||||
try {
|
||||
for (int i = 0; i < attrs.size(); i++) {
|
||||
ColumnValue cv = values.get(i);
|
||||
updateColumn(attrs.get(i), rs, cv.getExpress(), cv.getValue());
|
||||
updateColumn(attrs.get(i), rs, cv.getExpress(), cv.getValue2());
|
||||
}
|
||||
} finally {
|
||||
tableLock.unlock();
|
||||
@@ -909,7 +910,7 @@ public final class EntityCache<T> {
|
||||
return rs;
|
||||
}
|
||||
|
||||
public <V> T[] updateColumn(final FilterNode node, final Flipper flipper, List<Attribute<T, Serializable>> attrs, final List<ColumnValue> values) {
|
||||
public T[] updateColumn(final FilterNode node, final Flipper flipper, List<Attribute<T, Serializable>> attrs, final List<ColumnValue> values) {
|
||||
if (attrs == null || attrs.isEmpty() || node == null) {
|
||||
return (T[]) Creator.newArray(type, 0);
|
||||
}
|
||||
@@ -927,7 +928,7 @@ public final class EntityCache<T> {
|
||||
for (T rs : rms) {
|
||||
for (int i = 0; i < attrs.size(); i++) {
|
||||
ColumnValue cv = values.get(i);
|
||||
updateColumn(attrs.get(i), rs, cv.getExpress(), cv.getValue());
|
||||
updateColumn(attrs.get(i), rs, cv.getExpress(), cv.getValue2());
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
@@ -1003,7 +1004,7 @@ public final class EntityCache<T> {
|
||||
private <V> T updateColumn(Attribute<T, V> attr, final T entity, final ColumnExpress express, Serializable val) {
|
||||
final Class ft = attr.type();
|
||||
Number numb = null;
|
||||
Serializable newval = null;
|
||||
Serializable newVal = null;
|
||||
switch (express) {
|
||||
case INC:
|
||||
case DEC:
|
||||
@@ -1012,13 +1013,13 @@ public final class EntityCache<T> {
|
||||
case MOD:
|
||||
case AND:
|
||||
case ORR:
|
||||
numb = getValue((Number) attr.get(entity), express, val);
|
||||
numb = getNumberValue((Number) attr.get(entity), express, val instanceof ColumnNumberNode ? ((ColumnNumberNode) val).getValue() : (Number) val);
|
||||
break;
|
||||
case MOV:
|
||||
if (val instanceof ColumnExpNode) {
|
||||
val = updateColumnExpNode(attr, entity, (ColumnExpNode) val);
|
||||
}
|
||||
newval = val;
|
||||
newVal = val;
|
||||
if (val instanceof Number) {
|
||||
numb = (Number) val;
|
||||
}
|
||||
@@ -1026,85 +1027,94 @@ public final class EntityCache<T> {
|
||||
}
|
||||
if (numb != null) {
|
||||
if (ft == int.class || ft == Integer.class) {
|
||||
newval = numb.intValue();
|
||||
newVal = numb.intValue();
|
||||
} else if (ft == long.class || ft == Long.class) {
|
||||
newval = numb.longValue();
|
||||
newVal = numb.longValue();
|
||||
} else if (ft == short.class || ft == Short.class) {
|
||||
newval = numb.shortValue();
|
||||
newVal = numb.shortValue();
|
||||
} else if (ft == float.class || ft == Float.class) {
|
||||
newval = numb.floatValue();
|
||||
newVal = numb.floatValue();
|
||||
} else if (ft == double.class || ft == Double.class) {
|
||||
newval = numb.doubleValue();
|
||||
newVal = numb.doubleValue();
|
||||
} else if (ft == byte.class || ft == Byte.class) {
|
||||
newval = numb.byteValue();
|
||||
newVal = numb.byteValue();
|
||||
} else if (ft == AtomicInteger.class) {
|
||||
newval = new AtomicInteger(numb.intValue());
|
||||
newVal = new AtomicInteger(numb.intValue());
|
||||
} else if (ft == AtomicLong.class) {
|
||||
newval = new AtomicLong(numb.longValue());
|
||||
newVal = new AtomicLong(numb.longValue());
|
||||
} else if (ft == LongAdder.class) {
|
||||
LongAdder la = new LongAdder();
|
||||
la.add(numb.longValue());
|
||||
newval = la;
|
||||
newVal = la;
|
||||
}
|
||||
} else {
|
||||
if (ft == AtomicInteger.class && newval != null && newval.getClass() != AtomicInteger.class) {
|
||||
newval = new AtomicInteger(((Number) newval).intValue());
|
||||
} else if (ft == AtomicLong.class && newval != null && newval.getClass() != AtomicLong.class) {
|
||||
newval = new AtomicLong(((Number) newval).longValue());
|
||||
} else if (ft == LongAdder.class && newval != null && newval.getClass() != LongAdder.class) {
|
||||
if (ft == AtomicInteger.class && newVal != null && newVal.getClass() != AtomicInteger.class) {
|
||||
newVal = new AtomicInteger(((Number) newVal).intValue());
|
||||
} else if (ft == AtomicLong.class && newVal != null && newVal.getClass() != AtomicLong.class) {
|
||||
newVal = new AtomicLong(((Number) newVal).longValue());
|
||||
} else if (ft == LongAdder.class && newVal != null && newVal.getClass() != LongAdder.class) {
|
||||
LongAdder la = new LongAdder();
|
||||
la.add(((Number) newval).longValue());
|
||||
newval = la;
|
||||
la.add(((Number) newVal).longValue());
|
||||
newVal = la;
|
||||
}
|
||||
}
|
||||
attr.set(entity, (V) newval);
|
||||
attr.set(entity, (V) newVal);
|
||||
return entity;
|
||||
}
|
||||
|
||||
private <V> Serializable updateColumnExpNode(Attribute<T, V> attr, final T entity, ColumnExpNode node) {
|
||||
Serializable left = node.getLeft();
|
||||
if (left instanceof CharSequence) {
|
||||
left = info.getUpdateAttribute(left.toString()).get(entity);
|
||||
if (node.getExpress() == ColumnExpress.MOV) {
|
||||
return left;
|
||||
}
|
||||
} else if (left instanceof ColumnExpNode) {
|
||||
left = updateColumnExpNode(attr, entity, (ColumnExpNode) left);
|
||||
Serializable leftVal = null;
|
||||
ColumnNode leftNode = node.getLeft2();
|
||||
//类型只能是ColumnNameNode、ColumnNumberNode、ColumnExpNode
|
||||
if (leftNode instanceof ColumnNameNode) {
|
||||
leftVal = info.getUpdateAttribute(leftNode.toString()).get(entity);
|
||||
} else if (leftNode instanceof ColumnNumberNode) {
|
||||
leftVal = ((ColumnNumberNode) leftNode).getValue();
|
||||
} else if (leftNode instanceof ColumnExpNode) {
|
||||
leftVal = updateColumnExpNode(attr, entity, (ColumnExpNode) leftNode);
|
||||
}
|
||||
Serializable right = node.getRight();
|
||||
if (left instanceof CharSequence) {
|
||||
right = info.getUpdateAttribute(right.toString()).get(entity);
|
||||
} else if (left instanceof ColumnExpNode) {
|
||||
right = updateColumnExpNode(attr, entity, (ColumnExpNode) right);
|
||||
if (node.getExpress() == ColumnExpress.MOV) {
|
||||
return leftVal;
|
||||
}
|
||||
return getValue((Number) left, node.getExpress(), right);
|
||||
|
||||
Serializable rightVal = null;
|
||||
ColumnNode rightNode = node.getRight2();
|
||||
//类型只能是ColumnNameNode、ColumnNumberNode、ColumnExpNode
|
||||
if (rightNode instanceof ColumnNameNode) {
|
||||
rightVal = info.getUpdateAttribute(rightNode.toString()).get(entity);
|
||||
} else if (rightNode instanceof ColumnNumberNode) {
|
||||
rightVal = ((ColumnNumberNode) rightNode).getValue();
|
||||
} else if (rightNode instanceof ColumnExpNode) {
|
||||
rightVal = updateColumnExpNode(attr, entity, (ColumnExpNode) rightNode);
|
||||
}
|
||||
return getNumberValue((Number) leftVal, node.getExpress(), (Number) rightVal);
|
||||
}
|
||||
|
||||
private <V> Number getValue(Number numb, final ColumnExpress express, Serializable val) {
|
||||
private Number getNumberValue(Number numb, final ColumnExpress express, Number val) {
|
||||
switch (express) {
|
||||
case INC:
|
||||
if (numb == null) {
|
||||
numb = (Number) val;
|
||||
numb = val;
|
||||
} else {
|
||||
if (numb instanceof Float || ((Number) val) instanceof Float) {
|
||||
numb = numb.floatValue() + ((Number) val).floatValue();
|
||||
} else if (numb instanceof Double || ((Number) val) instanceof Double) {
|
||||
numb = numb.doubleValue() + ((Number) val).doubleValue();
|
||||
if (numb instanceof Float || val instanceof Float) {
|
||||
numb = numb.floatValue() + val.floatValue();
|
||||
} else if (numb instanceof Double || val instanceof Double) {
|
||||
numb = numb.doubleValue() + val.doubleValue();
|
||||
} else {
|
||||
numb = numb.longValue() + ((Number) val).longValue();
|
||||
numb = numb.longValue() + val.longValue();
|
||||
}
|
||||
}
|
||||
break;
|
||||
case DEC:
|
||||
if (numb == null) {
|
||||
numb = (Number) val;
|
||||
numb = val;
|
||||
} else {
|
||||
if (numb instanceof Float || ((Number) val) instanceof Float) {
|
||||
numb = numb.floatValue() - ((Number) val).floatValue();
|
||||
} else if (numb instanceof Double || ((Number) val) instanceof Double) {
|
||||
numb = numb.doubleValue() - ((Number) val).doubleValue();
|
||||
if (numb instanceof Float || val instanceof Float) {
|
||||
numb = numb.floatValue() - val.floatValue();
|
||||
} else if (numb instanceof Double || val instanceof Double) {
|
||||
numb = numb.doubleValue() - val.doubleValue();
|
||||
} else {
|
||||
numb = numb.longValue() - ((Number) val).longValue();
|
||||
numb = numb.longValue() - val.longValue();
|
||||
}
|
||||
}
|
||||
break;
|
||||
@@ -1112,35 +1122,35 @@ public final class EntityCache<T> {
|
||||
if (numb == null) {
|
||||
numb = 0;
|
||||
} else {
|
||||
numb = numb.longValue() * ((Number) val).floatValue();
|
||||
numb = numb.longValue() * val.floatValue();
|
||||
}
|
||||
break;
|
||||
case DIV:
|
||||
if (numb == null) {
|
||||
numb = 0;
|
||||
} else {
|
||||
numb = numb.longValue() / ((Number) val).floatValue();
|
||||
numb = numb.longValue() / val.floatValue();
|
||||
}
|
||||
break;
|
||||
case MOD:
|
||||
if (numb == null) {
|
||||
numb = 0;
|
||||
} else {
|
||||
numb = numb.longValue() % ((Number) val).intValue();
|
||||
numb = numb.longValue() % val.intValue();
|
||||
}
|
||||
break;
|
||||
case AND:
|
||||
if (numb == null) {
|
||||
numb = 0;
|
||||
} else {
|
||||
numb = numb.longValue() & ((Number) val).longValue();
|
||||
numb = numb.longValue() & val.longValue();
|
||||
}
|
||||
break;
|
||||
case ORR:
|
||||
if (numb == null) {
|
||||
numb = 0;
|
||||
} else {
|
||||
numb = numb.longValue() | ((Number) val).longValue();
|
||||
numb = numb.longValue() | val.longValue();
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -1511,13 +1511,22 @@ public final class EntityInfo<T> {
|
||||
if (cv == null) {
|
||||
return null;
|
||||
}
|
||||
Object val = cv.getValue();
|
||||
//ColumnExpNode、ColumnFuncNode、ColumnNameNode、ColumnNumberNode、ColumnStringNode
|
||||
ColumnNode node = cv.getValue2();
|
||||
//ColumnExpNode时 cv.getExpress() == ColumnExpress.MOV 只用于updateColumn
|
||||
if (val instanceof ColumnExpNode) {
|
||||
return formatSQLValue(attr, null, (ColumnExpNode) val, formatter);
|
||||
if (node instanceof ColumnExpNode) {
|
||||
return formatColumnExpNodeSQLValue(attr, null, (ColumnExpNode) node, formatter);
|
||||
}
|
||||
if (val instanceof ColumnFuncNode) {
|
||||
return formatSQLValue(attr, null, (ColumnFuncNode) val, formatter);
|
||||
if (node instanceof ColumnFuncNode) {
|
||||
return formatColumnFuncNodeSQLValue(attr, null, (ColumnFuncNode) node, formatter);
|
||||
}
|
||||
CharSequence val = null;
|
||||
if (node instanceof ColumnNameNode) {
|
||||
val = formatColumnNameNodeSQLValue(attr, null, (ColumnNameNode) node, formatter);
|
||||
} else if (node instanceof ColumnNumberNode) {
|
||||
val = formatColumnNumberNodeSQLValue(attr, null, (ColumnNumberNode) node, formatter);
|
||||
} else if (node instanceof ColumnStringNode) {
|
||||
val = formatColumnStringNodeSQLValue(attr, null, (ColumnStringNode) node, formatter);
|
||||
}
|
||||
switch (cv.getExpress()) {
|
||||
case INC:
|
||||
@@ -1535,62 +1544,81 @@ public final class EntityInfo<T> {
|
||||
case ORR:
|
||||
return new StringBuilder().append(sqlColumn).append(" | ").append(val);
|
||||
case MOV:
|
||||
CharSequence rs = formatter == null ? formatToString(val) : formatter.apply(this, val);
|
||||
if (rs == null && isNotNullJson(attr)) {
|
||||
rs = "";
|
||||
}
|
||||
return rs;
|
||||
return val == null && isNotNullJson(attr) ? "" : val;
|
||||
default:
|
||||
return val;
|
||||
}
|
||||
return formatter == null ? formatToString(val) : formatter.apply(this, val);
|
||||
}
|
||||
|
||||
protected CharSequence formatSQLValue(Attribute<T, Serializable> attr, String tabalis, final ColumnFuncNode node, BiFunction<EntityInfo, Object, CharSequence> formatter) {
|
||||
if (node.getValue() instanceof ColumnExpNode) {
|
||||
return node.getFunc().getColumn(formatSQLValue(attr, tabalis, (ColumnExpNode) node.getValue(), formatter).toString());
|
||||
protected CharSequence formatColumnFuncNodeSQLValue(Attribute<T, Serializable> attr, String tabalis, final ColumnFuncNode node, BiFunction<EntityInfo, Object, CharSequence> formatter) {
|
||||
if (node.getValue2() instanceof ColumnExpNode) {
|
||||
return node.getFunc().getColumn(formatColumnExpNodeSQLValue(attr, tabalis, (ColumnExpNode) node.getValue2(), formatter).toString());
|
||||
} else {
|
||||
return node.getFunc().getColumn(this.getSQLColumn(tabalis, String.valueOf(node.getValue())));
|
||||
return node.getFunc().getColumn(formatColumnNameNodeSQLValue(attr, tabalis, (ColumnNameNode) node.getValue2(), formatter).toString());
|
||||
}
|
||||
}
|
||||
|
||||
protected CharSequence formatSQLValue(Attribute<T, Serializable> attr, String tabalis, final ColumnExpNode node, BiFunction<EntityInfo, Object, CharSequence> formatter) {
|
||||
Serializable left = node.getLeft();
|
||||
if (left instanceof CharSequence) {
|
||||
left = this.getSQLColumn(tabalis, left.toString());
|
||||
if (node.getExpress() == ColumnExpress.MOV) {
|
||||
return (String) left;
|
||||
}
|
||||
} else if (left instanceof ColumnExpNode) {
|
||||
left = "(" + formatSQLValue(attr, tabalis, (ColumnExpNode) left, formatter) + ")";
|
||||
} else if (left instanceof ColumnFuncNode) {
|
||||
left = "(" + formatSQLValue(attr, tabalis, (ColumnFuncNode) left, formatter) + ")";
|
||||
protected CharSequence formatColumnExpNodeSQLValue(Attribute<T, Serializable> attr, String tabalis, final ColumnExpNode node, BiFunction<EntityInfo, Object, CharSequence> formatter) {
|
||||
CharSequence leftVal = null;
|
||||
ColumnNode leftNode = node.getLeft2();
|
||||
if (leftNode instanceof ColumnNameNode) {
|
||||
leftVal = formatColumnNameNodeSQLValue(attr, tabalis, (ColumnNameNode) leftNode, formatter);
|
||||
} else if (leftNode instanceof ColumnStringNode) {
|
||||
leftVal = formatColumnStringNodeSQLValue(attr, tabalis, (ColumnStringNode) leftNode, formatter);
|
||||
} else if (leftNode instanceof ColumnNumberNode) {
|
||||
leftVal = formatColumnNumberNodeSQLValue(attr, tabalis, (ColumnNumberNode) leftNode, formatter);
|
||||
} else if (leftNode instanceof ColumnExpNode) {
|
||||
leftVal = "(" + formatColumnExpNodeSQLValue(attr, tabalis, (ColumnExpNode) leftNode, formatter) + ")";
|
||||
} else if (leftNode instanceof ColumnFuncNode) {
|
||||
leftVal = "(" + formatColumnFuncNodeSQLValue(attr, tabalis, (ColumnFuncNode) leftNode, formatter) + ")";
|
||||
}
|
||||
Serializable right = node.getRight();
|
||||
if (right instanceof CharSequence) {
|
||||
right = this.getSQLColumn(null, right.toString());
|
||||
} else if (left instanceof ColumnExpNode) {
|
||||
right = "(" + formatSQLValue(attr, tabalis, (ColumnExpNode) right, formatter) + ")";
|
||||
} else if (left instanceof ColumnFuncNode) {
|
||||
right = "(" + formatSQLValue(attr, tabalis, (ColumnFuncNode) right, formatter) + ")";
|
||||
if (node.getExpress() == ColumnExpress.MOV) {
|
||||
return leftVal;
|
||||
}
|
||||
CharSequence rightVal = null;
|
||||
ColumnNode rightNode = node.getRight2();
|
||||
if (rightNode instanceof ColumnNameNode) {
|
||||
rightVal = formatColumnNameNodeSQLValue(attr, tabalis, (ColumnNameNode) rightNode, formatter);
|
||||
} else if (rightNode instanceof ColumnStringNode) {
|
||||
rightVal = formatColumnStringNodeSQLValue(attr, tabalis, (ColumnStringNode) rightNode, formatter);
|
||||
} else if (rightNode instanceof ColumnNumberNode) {
|
||||
rightVal = formatColumnNumberNodeSQLValue(attr, tabalis, (ColumnNumberNode) rightNode, formatter);
|
||||
} else if (rightNode instanceof ColumnExpNode) {
|
||||
rightVal = "(" + formatColumnExpNodeSQLValue(attr, tabalis, (ColumnExpNode) rightNode, formatter) + ")";
|
||||
} else if (rightNode instanceof ColumnFuncNode) {
|
||||
rightVal = "(" + formatColumnFuncNodeSQLValue(attr, tabalis, (ColumnFuncNode) rightNode, formatter) + ")";
|
||||
}
|
||||
switch (node.getExpress()) {
|
||||
case INC:
|
||||
return new StringBuilder().append(left).append(" + ").append(right);
|
||||
return new StringBuilder().append(leftVal).append(" + ").append(rightVal);
|
||||
case DEC:
|
||||
return new StringBuilder().append(left).append(" - ").append(right);
|
||||
return new StringBuilder().append(leftVal).append(" - ").append(rightVal);
|
||||
case MUL:
|
||||
return new StringBuilder().append(left).append(" * ").append(right);
|
||||
return new StringBuilder().append(leftVal).append(" * ").append(rightVal);
|
||||
case DIV:
|
||||
return new StringBuilder().append(left).append(" / ").append(right);
|
||||
return new StringBuilder().append(leftVal).append(" / ").append(rightVal);
|
||||
case MOD:
|
||||
return new StringBuilder().append(left).append(" % ").append(right);
|
||||
return new StringBuilder().append(leftVal).append(" % ").append(rightVal);
|
||||
case AND:
|
||||
return new StringBuilder().append(left).append(" & ").append(right);
|
||||
return new StringBuilder().append(leftVal).append(" & ").append(rightVal);
|
||||
case ORR:
|
||||
return new StringBuilder().append(left).append(" | ").append(right);
|
||||
return new StringBuilder().append(leftVal).append(" | ").append(rightVal);
|
||||
}
|
||||
throw new IllegalArgumentException(node + " express cannot be null or MOV");
|
||||
}
|
||||
|
||||
protected CharSequence formatColumnNameNodeSQLValue(Attribute<T, Serializable> attr, String tabalis, final ColumnNameNode node, BiFunction<EntityInfo, Object, CharSequence> formatter) {
|
||||
return this.getSQLColumn(tabalis, node.getColumn());
|
||||
}
|
||||
|
||||
protected CharSequence formatColumnStringNodeSQLValue(Attribute<T, Serializable> attr, String tabalis, final ColumnStringNode node, BiFunction<EntityInfo, Object, CharSequence> formatter) {
|
||||
return formatter == null ? formatToString(node.getValue()) : formatter.apply(this, node.getValue());
|
||||
}
|
||||
|
||||
protected CharSequence formatColumnNumberNodeSQLValue(Attribute<T, Serializable> attr, String tabalis, final ColumnNumberNode node, BiFunction<EntityInfo, Object, CharSequence> formatter) {
|
||||
return String.valueOf(node.getValue());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有数据表字段的Attribute, 不包含@Transient字段
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user