DataResultSetRow

This commit is contained in:
redkale
2024-06-12 10:43:29 +08:00
parent 7c28f2c54d
commit a676dca6b4
8 changed files with 353 additions and 271 deletions

View File

@@ -218,7 +218,7 @@ public abstract class AbstractDataSource extends AbstractService implements Data
* @param row ResultSet
* @return 对象
*/
protected <T> T getEntityValue(EntityInfo<T> info, final SelectColumn sels, final EntityInfo.DataResultSetRow row) {
protected <T> T getEntityValue(EntityInfo<T> info, final SelectColumn sels, final DataResultSetRow row) {
return info.getBuilder().getEntityValue(sels, row);
}

View File

@@ -24,7 +24,6 @@ import org.redkale.net.AsyncGroup;
import org.redkale.persistence.Table;
import org.redkale.service.Local;
import static org.redkale.source.DataSources.*;
import org.redkale.source.EntityInfo.EntityColumn;
import org.redkale.util.*;
import static org.redkale.util.Utility.isEmpty;
@@ -513,160 +512,160 @@ public abstract class AbstractDataSqlSource extends AbstractDataSource
? info.getCreator().create()
: null;
for (EntityColumn column : info.getDDLColumns()) {
if (column.primary) {
if (column.isPrimary()) {
primary = column;
}
String sqltype = "VARCHAR(" + column.length + ")";
String sqlnull = column.primary ? "NOT NULL" : "NULL";
if (column.type == boolean.class || column.type == Boolean.class) {
String sqltype = "VARCHAR(" + column.getLength() + ")";
String sqlnull = column.isPrimary() ? "NOT NULL" : "NULL";
if (column.getType() == boolean.class || column.getType() == Boolean.class) {
sqltype = "TINYINT(1)";
Boolean val = one == null
? null
: (Boolean) info.getAttribute(column.field).get(one);
: (Boolean) info.getAttribute(column.getField()).get(one);
sqlnull = "NOT NULL DEFAULT " + (val != null && val ? 1 : 0);
} else if (column.type == byte.class || column.type == Byte.class) {
} else if (column.getType() == byte.class || column.getType() == Byte.class) {
sqltype = "TINYINT";
Number val = one == null
? null
: (Number) info.getAttribute(column.field).get(one);
: (Number) info.getAttribute(column.getField()).get(one);
sqlnull = "NOT NULL DEFAULT " + (val == null ? 0 : val.byteValue());
} else if (column.type == short.class || column.type == Short.class) {
} else if (column.getType() == short.class || column.getType() == Short.class) {
sqltype = "SMALLINT";
Number val = one == null
? null
: (Number) info.getAttribute(column.field).get(one);
: (Number) info.getAttribute(column.getField()).get(one);
sqlnull = "NOT NULL DEFAULT " + (val == null ? 0 : val);
} else if (column.type == char.class || column.type == Character.class) {
} else if (column.getType() == char.class || column.getType() == Character.class) {
sqltype = "SMALLINT UNSIGNED";
Number val = one == null
? null
: (Number) info.getAttribute(column.field).get(one);
: (Number) info.getAttribute(column.getField()).get(one);
sqlnull = "NOT NULL DEFAULT " + (val == null ? 0 : val.intValue());
} else if (column.type == int.class
|| column.type == Integer.class
|| column.type == AtomicInteger.class) {
} else if (column.getType() == int.class
|| column.getType() == Integer.class
|| column.getType() == AtomicInteger.class) {
sqltype = "INT";
Number val = one == null
? null
: (Number) info.getAttribute(column.field).get(one);
: (Number) info.getAttribute(column.getField()).get(one);
sqlnull = "NOT NULL DEFAULT " + (val == null ? 0 : val);
} else if (column.type == long.class
|| column.type == Long.class
|| column.type == AtomicLong.class
|| column.type == LongAdder.class) {
} else if (column.getType() == long.class
|| column.getType() == Long.class
|| column.getType() == AtomicLong.class
|| column.getType() == LongAdder.class) {
sqltype = "BIGINT";
Number val = one == null
? null
: (Number) info.getAttribute(column.field).get(one);
: (Number) info.getAttribute(column.getField()).get(one);
sqlnull = "NOT NULL DEFAULT " + (val == null ? 0 : val);
} else if (column.type == float.class || column.type == Float.class) {
} else if (column.getType() == float.class || column.getType() == Float.class) {
sqltype = "FLOAT";
if (column.precision > 0) {
sqltype += "(" + column.precision + "," + column.scale + ")";
if (column.getPrecision() > 0) {
sqltype += "(" + column.getPrecision() + "," + column.getScale() + ")";
}
Number val = one == null
? null
: (Number) info.getAttribute(column.field).get(one);
: (Number) info.getAttribute(column.getField()).get(one);
sqlnull = "NOT NULL DEFAULT " + (val == null ? 0 : val);
} else if (column.type == double.class || column.type == Double.class) {
} else if (column.getType() == double.class || column.getType() == Double.class) {
sqltype = "DOUBLE";
if (column.precision > 0) {
sqltype += "(" + column.precision + "," + column.scale + ")";
if (column.getPrecision() > 0) {
sqltype += "(" + column.getPrecision() + "," + column.getScale() + ")";
}
Number val = one == null
? null
: (Number) info.getAttribute(column.field).get(one);
: (Number) info.getAttribute(column.getField()).get(one);
sqlnull = "NOT NULL DEFAULT " + (val == null ? 0 : val);
} else if (column.type == BigInteger.class) {
} else if (column.getType() == BigInteger.class) {
sqltype = "DECIMAL";
if (column.precision > 0) {
sqltype += "(" + column.precision + "," + column.scale + ")";
if (column.getPrecision() > 0) {
sqltype += "(" + column.getPrecision() + "," + column.getScale() + ")";
} else {
sqltype += "(19,2)";
}
Number val = one == null
? null
: (Number) info.getAttribute(column.field).get(one);
: (Number) info.getAttribute(column.getField()).get(one);
sqlnull = "NOT NULL DEFAULT " + (val == null ? 0 : val);
} else if (column.type == BigDecimal.class) {
} else if (column.getType() == BigDecimal.class) {
sqltype = "DECIMAL";
if (column.precision > 0) {
sqltype += "(" + column.precision + "," + column.scale + ")";
if (column.getPrecision() > 0) {
sqltype += "(" + column.getPrecision() + "," + column.getScale() + ")";
}
Number val = one == null
? null
: (Number) info.getAttribute(column.field).get(one);
: (Number) info.getAttribute(column.getField()).get(one);
sqlnull = "NOT NULL DEFAULT " + (val == null ? 0 : val);
} else if (column.type == String.class) {
if (column.length < 65535) {
} else if (column.getType() == String.class) {
if (column.getLength() < 65535) {
String val = one == null
? null
: (String) info.getAttribute(column.field).get(one);
: (String) info.getAttribute(column.getField()).get(one);
if (val != null) {
sqlnull = "NOT NULL DEFAULT '" + val.replace('\'', '"') + "'";
} else if (column.primary) {
} else if (column.isPrimary()) {
sqlnull = "NOT NULL DEFAULT ''";
}
} else if (column.length == 65535) {
} else if (column.getLength() == 65535) {
sqltype = "TEXT";
if (!column.nullable) {
if (!column.isNullable()) {
sqlnull = "NOT NULL";
}
} else if (column.length <= 16777215) {
} else if (column.getLength() <= 16777215) {
sqltype = "MEDIUMTEXT";
if (!column.nullable) {
if (!column.isNullable()) {
sqlnull = "NOT NULL";
}
} else {
sqltype = "LONGTEXT";
if (!column.nullable) {
if (!column.isNullable()) {
sqlnull = "NOT NULL";
}
}
} else if (column.type == byte[].class) {
if (column.length <= 65535) {
} else if (column.getType() == byte[].class) {
if (column.getLength() <= 65535) {
sqltype = "BLOB";
if (!column.nullable) {
if (!column.isNullable()) {
sqlnull = "NOT NULL";
}
} else if (column.length <= 16777215) {
} else if (column.getLength() <= 16777215) {
sqltype = "MEDIUMBLOB";
if (!column.nullable) {
if (!column.isNullable()) {
sqlnull = "NOT NULL";
}
} else {
sqltype = "LONGBLOB";
if (!column.nullable) {
if (!column.isNullable()) {
sqlnull = "NOT NULL";
}
}
} else if (column.type == java.time.LocalDate.class
|| column.type == java.util.Date.class
|| "java.sql.Date".equals(column.type.getName())) {
} else if (column.getType() == java.time.LocalDate.class
|| column.getType() == java.util.Date.class
|| "java.sql.Date".equals(column.getType().getName())) {
sqltype = "DATE";
} else if (column.type == java.time.LocalTime.class || "java.sql.Time".equals(column.type.getName())) {
} else if (column.getType() == java.time.LocalTime.class || "java.sql.Time".equals(column.getType().getName())) {
sqltype = "TIME";
} else if (column.type == java.time.LocalDateTime.class
|| "java.sql.Timestamp".equals(column.type.getName())) {
} else if (column.getType() == java.time.LocalDateTime.class
|| "java.sql.Timestamp".equals(column.getType().getName())) {
sqltype = "DATETIME";
} else { // JavaBean
sqltype = column.length >= 65535 ? "TEXT" : ("VARCHAR(" + column.length + ")");
sqlnull = !column.nullable ? "NOT NULL DEFAULT ''" : "NULL";
sqltype = column.getLength() >= 65535 ? "TEXT" : ("VARCHAR(" + column.getLength() + ")");
sqlnull = !column.isNullable() ? "NOT NULL DEFAULT ''" : "NULL";
}
sb.append(" `")
.append(column.column)
.append(column.getColumn())
.append("` ")
.append(sqltype)
.append(column.primary && info.isAutoGenerated() ? " AUTO_INCREMENT " : " ")
.append(column.primary && info.isAutoGenerated() ? "" : sqlnull);
if (column.comment != null && !column.comment.isEmpty()) {
.append(column.isPrimary() && info.isAutoGenerated() ? " AUTO_INCREMENT " : " ")
.append(column.isPrimary() && info.isAutoGenerated() ? "" : sqlnull);
if (column.getComment() != null && !column.getComment().isEmpty()) {
sb.append(" COMMENT '")
.append(column.comment.replace('\'', '"'))
.append(column.getComment().replace('\'', '"'))
.append("'");
}
sb.append(",\n");
}
sb.append(" PRIMARY KEY (`").append(primary.column).append("`)\n");
sb.append(" PRIMARY KEY (`").append(primary.getColumn()).append("`)\n");
sb.append(")ENGINE=InnoDB DEFAULT CHARSET=UTF8MB4");
if (table != null && !table.comment().isEmpty()) {
sb.append(" COMMENT '")
@@ -689,141 +688,141 @@ public abstract class AbstractDataSqlSource extends AbstractDataSource
+ table.comment().replace('\'', '"') + "'");
}
for (EntityColumn column : info.getDDLColumns()) {
if (column.primary) {
if (column.isPrimary()) {
primary = column;
}
String sqltype = "VARCHAR(" + column.length + ")";
String sqlnull = column.primary ? "NOT NULL" : "NULL";
if (column.type == boolean.class || column.type == Boolean.class) {
String sqltype = "VARCHAR(" + column.getLength() + ")";
String sqlnull = column.isPrimary() ? "NOT NULL" : "NULL";
if (column.getType() == boolean.class || column.getType() == Boolean.class) {
sqltype = "BOOL";
Boolean val = one == null
? null
: (Boolean) info.getAttribute(column.field).get(one);
: (Boolean) info.getAttribute(column.getField()).get(one);
sqlnull = "NOT NULL DEFAULT " + (val != null && val ? 1 : 0);
} else if (column.type == byte.class || column.type == Byte.class) {
} else if (column.getType() == byte.class || column.getType() == Byte.class) {
sqltype = "INT2";
Number val = one == null
? null
: (Number) info.getAttribute(column.field).get(one);
: (Number) info.getAttribute(column.getField()).get(one);
sqlnull = "NOT NULL DEFAULT " + (val == null ? 0 : val.byteValue());
} else if (column.type == short.class || column.type == Short.class) {
} else if (column.getType() == short.class || column.getType() == Short.class) {
sqltype = "INT2";
Number val = one == null
? null
: (Number) info.getAttribute(column.field).get(one);
: (Number) info.getAttribute(column.getField()).get(one);
sqlnull = "NOT NULL DEFAULT " + (val == null ? 0 : val);
} else if (column.type == char.class || column.type == Character.class) {
} else if (column.getType() == char.class || column.getType() == Character.class) {
sqltype = "INT2 UNSIGNED";
Number val = one == null
? null
: (Number) info.getAttribute(column.field).get(one);
: (Number) info.getAttribute(column.getField()).get(one);
sqlnull = "NOT NULL DEFAULT " + (val == null ? 0 : val.intValue());
} else if (column.type == int.class
|| column.type == Integer.class
|| column.type == AtomicInteger.class) {
} else if (column.getType() == int.class
|| column.getType() == Integer.class
|| column.getType() == AtomicInteger.class) {
sqltype = "INT4";
Number val = one == null
? null
: (Number) info.getAttribute(column.field).get(one);
: (Number) info.getAttribute(column.getField()).get(one);
sqlnull = "NOT NULL DEFAULT " + (val == null ? 0 : val);
} else if (column.type == long.class
|| column.type == Long.class
|| column.type == AtomicLong.class
|| column.type == LongAdder.class) {
} else if (column.getType() == long.class
|| column.getType() == Long.class
|| column.getType() == AtomicLong.class
|| column.getType() == LongAdder.class) {
sqltype = "INT8";
Number val = one == null
? null
: (Number) info.getAttribute(column.field).get(one);
: (Number) info.getAttribute(column.getField()).get(one);
sqlnull = "NOT NULL DEFAULT " + (val == null ? 0 : val);
} else if (column.type == float.class || column.type == Float.class) {
} else if (column.getType() == float.class || column.getType() == Float.class) {
sqltype = "FLOAT4";
if (column.precision > 0) {
sqltype += "(" + column.precision + "," + column.scale + ")";
if (column.getPrecision() > 0) {
sqltype += "(" + column.getPrecision() + "," + column.getScale() + ")";
}
Number val = one == null
? null
: (Number) info.getAttribute(column.field).get(one);
: (Number) info.getAttribute(column.getField()).get(one);
sqlnull = "NOT NULL DEFAULT " + (val == null ? 0 : val);
} else if (column.type == double.class || column.type == Double.class) {
} else if (column.getType() == double.class || column.getType() == Double.class) {
sqltype = "FLOAT8";
if (column.precision > 0) {
sqltype += "(" + column.precision + "," + column.scale + ")";
if (column.getPrecision() > 0) {
sqltype += "(" + column.getPrecision() + "," + column.getScale() + ")";
}
Number val = one == null
? null
: (Number) info.getAttribute(column.field).get(one);
: (Number) info.getAttribute(column.getField()).get(one);
sqlnull = "NOT NULL DEFAULT " + (val == null ? 0 : val);
} else if (column.type == BigInteger.class) {
} else if (column.getType() == BigInteger.class) {
sqltype = "NUMERIC";
if (column.precision > 0) {
sqltype += "(" + column.precision + "," + column.scale + ")";
if (column.getPrecision() > 0) {
sqltype += "(" + column.getPrecision() + "," + column.getScale() + ")";
} else {
sqltype += "(19,2)";
}
Number val = one == null
? null
: (Number) info.getAttribute(column.field).get(one);
: (Number) info.getAttribute(column.getField()).get(one);
sqlnull = "NOT NULL DEFAULT " + (val == null ? 0 : val);
} else if (column.type == BigDecimal.class) {
} else if (column.getType() == BigDecimal.class) {
sqltype = "NUMERIC";
if (column.precision > 0) {
sqltype += "(" + column.precision + "," + column.scale + ")";
if (column.getPrecision() > 0) {
sqltype += "(" + column.getPrecision() + "," + column.getScale() + ")";
}
Number val = one == null
? null
: (Number) info.getAttribute(column.field).get(one);
: (Number) info.getAttribute(column.getField()).get(one);
sqlnull = "NOT NULL DEFAULT " + (val == null ? 0 : val);
} else if (column.type == String.class) {
if (column.length < 65535) {
} else if (column.getType() == String.class) {
if (column.getLength() < 65535) {
String val = one == null
? null
: (String) info.getAttribute(column.field).get(one);
: (String) info.getAttribute(column.getField()).get(one);
if (val != null) {
sqlnull = "NOT NULL DEFAULT '" + val.replace('\'', '"') + "'";
} else if (column.primary) {
} else if (column.isPrimary()) {
sqlnull = "NOT NULL DEFAULT ''";
}
} else {
sqltype = "TEXT";
if (!column.nullable) {
if (!column.isNullable()) {
sqlnull = "NOT NULL";
}
}
} else if (column.type == byte[].class) {
} else if (column.getType() == byte[].class) {
sqltype = "BYTEA";
if (!column.nullable) {
if (!column.isNullable()) {
sqlnull = "NOT NULL";
}
} else if (column.type == java.time.LocalDate.class
|| column.type == java.util.Date.class
|| "java.sql.Date".equals(column.type.getName())) {
} else if (column.getType() == java.time.LocalDate.class
|| column.getType() == java.util.Date.class
|| "java.sql.Date".equals(column.getType().getName())) {
sqltype = "DATE";
} else if (column.type == java.time.LocalTime.class || "java.sql.Time".equals(column.type.getName())) {
} else if (column.getType() == java.time.LocalTime.class || "java.sql.Time".equals(column.getType().getName())) {
sqltype = "TIME";
} else if (column.type == java.time.LocalDateTime.class
|| "java.sql.Timestamp".equals(column.type.getName())) {
} else if (column.getType() == java.time.LocalDateTime.class
|| "java.sql.Timestamp".equals(column.getType().getName())) {
sqltype = "TIMESTAMP";
} else { // JavaBean
sqltype = column.length >= 65535 ? "TEXT" : ("VARCHAR(" + column.length + ")");
sqlnull = !column.nullable ? "NOT NULL DEFAULT ''" : "NULL";
sqltype = column.getLength() >= 65535 ? "TEXT" : ("VARCHAR(" + column.getLength() + ")");
sqlnull = !column.isNullable() ? "NOT NULL DEFAULT ''" : "NULL";
}
if (column.primary && info.isAutoGenerated()) {
if (column.isPrimary() && info.isAutoGenerated()) {
sqltype = "SERIAL";
}
sb.append(" ")
.append(column.column)
.append(column.getColumn())
.append(" ")
.append(sqltype)
.append(" ")
.append(column.primary && info.isAutoGenerated() ? "" : sqlnull);
if (column.comment != null && !column.comment.isEmpty()) {
.append(column.isPrimary() && info.isAutoGenerated() ? "" : sqlnull);
if (column.getComment() != null && !column.getComment().isEmpty()) {
// postgresql不支持DDL中直接带comment
comments.add("COMMENT ON COLUMN " + info.getOriginTable() + "." + column.column + " IS '"
+ column.comment.replace('\'', '"') + "'");
comments.add("COMMENT ON COLUMN " + info.getOriginTable() + "." + column.getColumn() + " IS '"
+ column.getComment().replace('\'', '"') + "'");
}
sb.append(",\n");
}
sb.append(" PRIMARY KEY (").append(primary.column).append(")\n");
sb.append(" PRIMARY KEY (").append(primary.getColumn()).append(")\n");
sb.append(")");
return Utility.append(Utility.ofArray(sb.toString()), comments);
}

View File

@@ -2109,7 +2109,7 @@ public class DataJdbcSource extends AbstractDataSqlSource {
final DataResultSet set = createDataResultSet(info, prestmt.executeQuery());
Serializable val = defValue;
if (set.next()) {
val = info.getBuilder().getFieldValue(attr, set, 1);
val = info.getBuilder().getFieldValue(set, attr, 1);
}
set.close();
conn.offerQueryStatement(prestmt);
@@ -2161,7 +2161,7 @@ public class DataJdbcSource extends AbstractDataSqlSource {
final DataResultSet set = createDataResultSet(info, prestmt.executeQuery());
Serializable val = defValue;
if (set.next()) {
val = info.getBuilder().getFieldValue(attr, set, 1);
val = info.getBuilder().getFieldValue(set, attr, 1);
}
set.close();
conn.offerQueryStatement(prestmt);

View File

@@ -43,85 +43,102 @@ import org.redkale.util.Attribute;
* @author zhangjx
* @since 2.5.0
*/
public interface DataResultSet extends EntityInfo.DataResultSetRow {
public interface DataResultSet extends DataResultSetRow {
public boolean next();
public void close();
public static Serializable formatColumnValue(Class t, Object o) {
return formatColumnValue(t, null, o);
/**
* 将对象转化成另一个类型对象
* @param type 类型
* @param o 数据库字段值
* @return 转换后对象
*/
public static Serializable formatColumnValue(Class type, Object o) {
return formatColumnValue(type, null, o);
}
public static Serializable formatColumnValue(Class t, Type genericType, Object o) {
if (t == byte[].class) {
/**
* 将对象转化成另一个类型对象
* @param type 类型
* @param genericType 泛型类型
* @param o 数据库字段值
* @return 转换后对象
*/
public static Serializable formatColumnValue(Class type, Type genericType, Object o) {
if (type == byte[].class) {
return (byte[]) o;
} else {
if (t.isPrimitive()) {
if (type.isPrimitive()) {
if (o != null) {
if (t == int.class) {
if (type == int.class) {
o = ((Number) o).intValue();
} else if (t == long.class) {
} else if (type == long.class) {
o = ((Number) o).longValue();
} else if (t == short.class) {
} else if (type == short.class) {
o = ((Number) o).shortValue();
} else if (t == float.class) {
} else if (type == float.class) {
o = ((Number) o).floatValue();
} else if (t == double.class) {
} else if (type == double.class) {
o = ((Number) o).doubleValue();
} else if (t == byte.class) {
} else if (type == byte.class) {
o = ((Number) o).byteValue();
} else if (t == char.class) {
} else if (type == char.class) {
o = (char) ((Number) o).intValue();
} else if (t == boolean.class) {
o = (Boolean) o;
} else if (type == boolean.class) {
if (o instanceof Number) {
o = ((Number) o).intValue() != 0;
}
}
} else if (t == int.class) {
} else if (type == int.class) {
o = 0;
} else if (t == long.class) {
} else if (type == long.class) {
o = 0L;
} else if (t == short.class) {
} else if (type == short.class) {
o = (short) 0;
} else if (t == float.class) {
} else if (type == float.class) {
o = 0.0f;
} else if (t == double.class) {
} else if (type == double.class) {
o = 0.0d;
} else if (t == byte.class) {
} else if (type == byte.class) {
o = (byte) 0;
} else if (t == boolean.class) {
} else if (type == boolean.class) {
o = false;
} else if (t == char.class) {
} else if (type == char.class) {
o = (char) 0;
}
} else if (t == Integer.class) {
} else if (type == Integer.class) {
o = ((Number) o).intValue();
} else if (t == Long.class) {
} else if (type == Long.class) {
o = ((Number) o).longValue();
} else if (t == Short.class) {
} else if (type == Short.class) {
o = ((Number) o).shortValue();
} else if (t == Float.class) {
} else if (type == Float.class) {
o = ((Number) o).floatValue();
} else if (t == Double.class) {
} else if (type == Double.class) {
o = ((Number) o).doubleValue();
} else if (t == Byte.class) {
} else if (type == Byte.class) {
o = ((Number) o).byteValue();
} else if (t == Character.class) {
} else if (type == Character.class) {
o = (char) ((Number) o).intValue();
} else if (t == Boolean.class) {
o = (Boolean) o;
} else if (t == AtomicInteger.class) {
} else if (type == Boolean.class) {
if (o instanceof Number) {
o = ((Number) o).intValue() != 0;
}
} else if (type == AtomicInteger.class) {
if (o != null) {
o = new AtomicInteger(((Number) o).intValue());
} else {
o = new AtomicInteger();
}
} else if (t == AtomicLong.class) {
} else if (type == AtomicLong.class) {
if (o != null) {
o = new AtomicLong(((Number) o).longValue());
} else {
o = new AtomicLong();
}
} else if (t == LongAdder.class) {
} else if (type == LongAdder.class) {
if (o != null) {
LongAdder v = new LongAdder();
v.add(((Number) o).longValue());
@@ -129,7 +146,7 @@ public interface DataResultSet extends EntityInfo.DataResultSetRow {
} else {
o = new LongAdder();
}
} else if (t == BigInteger.class) {
} else if (type == BigInteger.class) {
if (o != null && !(o instanceof BigInteger)) {
if (o instanceof byte[]) {
o = new BigInteger((byte[]) o);
@@ -137,7 +154,7 @@ public interface DataResultSet extends EntityInfo.DataResultSetRow {
o = new BigInteger(o.toString(), 10);
}
}
} else if (t == BigDecimal.class) {
} else if (type == BigDecimal.class) {
if (o != null && !(o instanceof BigDecimal)) {
if (o instanceof byte[]) {
o = new BigDecimal(new String((byte[]) o));
@@ -145,7 +162,7 @@ public interface DataResultSet extends EntityInfo.DataResultSetRow {
o = new BigDecimal(o.toString());
}
}
} else if (t == LocalDate.class) {
} else if (type == LocalDate.class) {
if (o != null && !(o instanceof LocalDate)) {
if (o instanceof java.sql.Date) {
o = ((java.sql.Date) o).toLocalDate();
@@ -153,7 +170,7 @@ public interface DataResultSet extends EntityInfo.DataResultSetRow {
o = ((java.sql.Timestamp) o).toLocalDateTime().toLocalDate();
}
}
} else if (t == LocalTime.class) {
} else if (type == LocalTime.class) {
if (o != null && !(o instanceof LocalTime)) {
if (o instanceof java.sql.Time) {
o = ((java.sql.Time) o).toLocalTime();
@@ -161,7 +178,7 @@ public interface DataResultSet extends EntityInfo.DataResultSetRow {
o = ((java.sql.Timestamp) o).toLocalDateTime().toLocalTime();
}
}
} else if (t == LocalDateTime.class) {
} else if (type == LocalDateTime.class) {
if (o != null && !(o instanceof LocalDateTime)) {
if (o instanceof java.sql.Date) {
o = ((java.sql.Date) o).toLocalDate().atStartOfDay();
@@ -169,7 +186,7 @@ public interface DataResultSet extends EntityInfo.DataResultSetRow {
o = ((java.sql.Timestamp) o).toLocalDateTime();
}
}
} else if (t == Instant.class) {
} else if (type == Instant.class) {
if (o != null && !(o instanceof Instant)) {
if (o instanceof java.sql.Date) {
o = ((java.sql.Date) o).toInstant();
@@ -179,7 +196,7 @@ public interface DataResultSet extends EntityInfo.DataResultSetRow {
o = ((java.sql.Timestamp) o).toInstant();
}
}
} else if (t == String.class) {
} else if (type == String.class) {
if (o == null) {
o = "";
} else if (o instanceof byte[]) {
@@ -187,17 +204,17 @@ public interface DataResultSet extends EntityInfo.DataResultSetRow {
} else {
o = o.toString();
}
} else if (o != null && !t.isAssignableFrom(o.getClass()) && o instanceof CharSequence) {
} else if (o != null && !type.isAssignableFrom(o.getClass()) && o instanceof CharSequence) {
o = ((CharSequence) o).length() == 0
? null
: JsonConvert.root().convertFrom(genericType == null ? t : genericType, o.toString());
: JsonConvert.root().convertFrom(genericType == null ? type : genericType, o.toString());
}
}
return (Serializable) o;
}
public static <T> Serializable getRowColumnValue(
final EntityInfo.DataResultSetRow row, Attribute<T, Serializable> attr, int index, String column) {
DataResultSetRow row, Attribute<T, Serializable> attr, int index, String column) {
final Class t = attr.type();
Serializable o = null;
try {

View File

@@ -0,0 +1,43 @@
/*
*/
package org.redkale.source;
import java.io.Serializable;
import java.util.List;
import org.redkale.util.Attribute;
/**
*
* @author zhangjx
*/
public interface DataResultSetRow {
// 可以为空
public EntityInfo getEntityInfo();
// index从1开始
public Object getObject(int index);
public Object getObject(String columnLabel);
// index从1开始
default <T> Serializable getObject(Attribute<T, Serializable> attr, int index, String columnLabel) {
return DataResultSet.getRowColumnValue(this, attr, index, columnLabel);
}
/**
* 判断当前行值是否为null
*
* @return boolean
*/
public boolean wasNull();
/**
* 获取字段名集合,尽量不要多次调用
*
* @return List
*/
public List<String> getColumnLabels();
}

View File

@@ -10,7 +10,6 @@ import java.util.concurrent.ConcurrentHashMap;
import org.redkale.annotation.Nullable;
import org.redkale.convert.ConvertDisabled;
import org.redkale.persistence.*;
import org.redkale.source.EntityInfo.DataResultSetRow;
import org.redkale.util.*;
/**
@@ -30,7 +29,7 @@ public class EntityBuilder<T> {
private static final ConcurrentHashMap<String, String> camelMap = new ConcurrentHashMap<>();
// 实体类名
// 实体或者JavaBean类名
private final Class<T> type;
// 是否Map类型的虚拟实体类
@@ -363,7 +362,7 @@ public class EntityBuilder<T> {
if (sqlFlag) {
attr.set(obj, getFieldValue(row, sqlCol));
} else {
attr.set(obj, getFieldValue(attr, row, 0));
attr.set(obj, getFieldValue(row, attr, 0));
}
}
}
@@ -373,11 +372,11 @@ public class EntityBuilder<T> {
Attribute<T, Serializable> attr = this.constructorAttributes[i];
String sqlCol = getSQLColumn(null, attr.field());
if (sqlColumns.contains(sqlCol)) {
cps[i] = getFieldValue(attr, row, 0);
cps[i] = getFieldValue(row, attr, 0);
} else {
sqlCol = camelCaseColumn(sqlCol);
if (sqlColumns.contains(sqlCol)) {
cps[i] = getFieldValue(attr, row, 0);
cps[i] = getFieldValue(row, attr, 0);
}
}
}
@@ -385,11 +384,11 @@ public class EntityBuilder<T> {
for (Attribute<T, Serializable> attr : this.unconstructorAttributes) {
String sqlCol = getSQLColumn(null, attr.field());
if (sqlColumns.contains(sqlCol)) {
attr.set(obj, getFieldValue(attr, row, 0));
attr.set(obj, getFieldValue(row, attr, 0));
} else {
sqlCol = camelCaseColumn(sqlCol);
if (sqlColumns.contains(sqlCol)) {
attr.set(obj, getFieldValue(attr, row, 0));
attr.set(obj, getFieldValue(row, attr, 0));
}
}
}
@@ -434,16 +433,16 @@ public class EntityBuilder<T> {
Object[] cps = new Object[this.constructorParameters.length];
for (int i = 0; i < this.constructorAttributes.length; i++) {
Attribute<T, Serializable> attr = this.constructorAttributes[i];
if (sels == null || sels.test(attr.field())) {
cps[i] = getFieldValue(attr, row, 0);
if (sels.test(attr.field())) {
cps[i] = getFieldValue(row, attr, 0);
}
}
obj = creator.create(cps);
attrs = this.unconstructorAttributes;
}
for (Attribute<T, Serializable> attr : attrs) {
if (sels == null || sels.test(attr.field())) {
attr.set(obj, getFieldValue(attr, row, 0));
if (sels.test(attr.field())) {
attr.set(obj, getFieldValue(row, attr, 0));
}
}
return obj;
@@ -500,7 +499,7 @@ public class EntityBuilder<T> {
if (attr == null) {
continue;
}
cps[i] = getFieldValue(attr, row, ++index);
cps[i] = getFieldValue(row, attr, ++index);
}
obj = creator.create(cps);
}
@@ -509,7 +508,7 @@ public class EntityBuilder<T> {
if (attr == null) {
continue;
}
attr.set(obj, getFieldValue(attr, row, ++index));
attr.set(obj, getFieldValue(row, attr, ++index));
}
}
return obj;
@@ -558,23 +557,23 @@ public class EntityBuilder<T> {
return (Serializable) row.getObject(sqlColumn);
}
protected Serializable getFieldValue(Attribute<T, Serializable> attr, final DataResultSetRow row, int index) {
protected Serializable getFieldValue(final DataResultSetRow row, Attribute<T, Serializable> attr, int index) {
return row.getObject(attr, index, index > 0 ? null : this.getSQLColumn(null, attr.field()));
}
/**
* 根据field字段名获取数据库对应的字段名
*
* @param tabalis 表别名
* @param fieldname 字段名
* @param tabAlis 表别名
* @param fieldName 字段名
* @return String
*/
public String getSQLColumn(String tabalis, String fieldname) {
public String getSQLColumn(@Nullable String tabAlis, String fieldName) {
return this.aliasMap == null
? (tabalis == null ? fieldname : (tabalis + '.' + fieldname))
: (tabalis == null
? aliasMap.getOrDefault(fieldname, fieldname)
: (tabalis + '.' + aliasMap.getOrDefault(fieldname, fieldname)));
? (tabAlis == null ? fieldName : (tabAlis + '.' + fieldName))
: (tabAlis == null
? aliasMap.getOrDefault(fieldName, fieldName)
: (tabAlis + '.' + aliasMap.getOrDefault(fieldName, fieldName)));
}
public boolean hasConstructorAttribute() {

View File

@@ -0,0 +1,98 @@
/*
*/
package org.redkale.source;
import org.redkale.annotation.Comment;
import org.redkale.convert.json.JsonConvert;
import org.redkale.persistence.Column;
/**
*
* @author zhangjx
*/
public class EntityColumn {
private final boolean primary; // 是否主键
private final String field;
private final String column;
private final Class type;
private final String comment;
private final boolean nullable;
private final boolean unique;
private final int length;
private final int precision;
private final int scale;
public EntityColumn(boolean primary, Column col, String name, Class type, Comment comment) {
this.primary = primary;
this.field = name;
this.column = col == null || col.name().isEmpty() ? name : col.name();
this.type = type;
this.comment = (col == null || col.comment().isEmpty())
&& comment != null
&& !comment.value().isEmpty()
? comment.value()
: (col == null ? "" : col.comment());
this.nullable = col != null && col.nullable();
this.unique = col != null && col.unique();
this.length = col == null ? 255 : col.length();
this.precision = col == null ? 0 : col.precision();
this.scale = col == null ? 0 : col.scale();
}
@Override
public String toString() {
return JsonConvert.root().convertTo(this);
}
public boolean isPrimary() {
return primary;
}
public String getField() {
return field;
}
public String getColumn() {
return column;
}
public Class getType() {
return type;
}
public String getComment() {
return comment;
}
public boolean isNullable() {
return nullable;
}
public boolean isUnique() {
return unique;
}
public int getLength() {
return length;
}
public int getPrecision() {
return precision;
}
public int getScale() {
return scale;
}
}

View File

@@ -520,19 +520,19 @@ public final class EntityInfo<T> {
}
this.ddlColumns = ddls.toArray(new EntityColumn[ddls.size()]);
this.attributes = attributeMap.values().toArray(new Attribute[attributeMap.size()]);
this.primaryColumn = Utility.find(this.ddlColumns, c -> c.field.equals(primary.field()));
this.primaryColumn = Utility.find(this.ddlColumns, c -> c.getField().equals(primary.field()));
this.primaryColumnOneArray = new EntityColumn[] {this.primaryColumn};
this.insertAttributes = insertAttrs.toArray(new Attribute[insertAttrs.size()]);
this.insertColumns = new EntityColumn[this.insertAttributes.length];
for (int i = 0; i < this.insertAttributes.length; i++) {
String field = this.insertAttributes[i].field();
this.insertColumns[i] = Utility.find(this.ddlColumns, c -> c.field.equals(field));
this.insertColumns[i] = Utility.find(this.ddlColumns, c -> c.getField().equals(field));
}
this.updateAttributes = updateAttrs.toArray(new Attribute[updateAttrs.size()]);
this.updateColumns = new EntityColumn[this.updateAttributes.length];
for (int i = 0; i < this.updateAttributes.length; i++) {
String field = this.updateAttributes[i].field();
this.updateColumns[i] = Utility.find(this.ddlColumns, c -> c.field.equals(field));
this.updateColumns[i] = Utility.find(this.ddlColumns, c -> c.getField().equals(field));
}
this.updateEntityAttributes = Utility.append(this.updateAttributes, this.primary);
this.updateEntityColumns = Utility.append(this.updateColumns, this.primaryColumn);
@@ -578,7 +578,7 @@ public final class EntityInfo<T> {
this.queryColumns = new EntityColumn[this.queryAttributes.length];
for (int i = 0; i < this.queryAttributes.length; i++) {
String field = this.queryAttributes[i].field();
this.queryColumns[i] = Utility.find(this.ddlColumns, c -> c.field.equals(field));
this.queryColumns[i] = Utility.find(this.ddlColumns, c -> c.getField().equals(field));
}
this.builder = new EntityBuilder<>(
type,
@@ -1775,78 +1775,4 @@ public final class EntityInfo<T> {
return getClass().getSimpleName() + "(" + type.getName() + ")@" + Objects.hashCode(this);
}
public static interface DataResultSetRow {
// 可以为空
public EntityInfo getEntityInfo();
// index从1开始
public Object getObject(int index);
public Object getObject(String columnLabel);
// index从1开始
default <T> Serializable getObject(Attribute<T, Serializable> attr, int index, String columnLabel) {
return DataResultSet.getRowColumnValue(this, attr, index, columnLabel);
}
/**
* 判断当前行值是否为null
*
* @return boolean
*/
public boolean wasNull();
/**
* 获取字段名集合,尽量不要多次调用
*
* @return List
*/
public List<String> getColumnLabels();
}
public static class EntityColumn {
public final boolean primary; // 是否主键
public final String field;
public final String column;
public final Class type;
public final String comment;
public final boolean nullable;
public final boolean unique;
public final int length;
public final int precision;
public final int scale;
protected EntityColumn(boolean primary, Column col, String name, Class type, Comment comment) {
this.primary = primary;
this.field = name;
this.column = col == null || col.name().isEmpty() ? name : col.name();
this.type = type;
this.comment = (col == null || col.comment().isEmpty())
&& comment != null
&& !comment.value().isEmpty()
? comment.value()
: (col == null ? "" : col.comment());
this.nullable = col != null && col.nullable();
this.unique = col != null && col.unique();
this.length = col == null ? 255 : col.length();
this.precision = col == null ? 0 : col.precision();
this.scale = col == null ? 0 : col.scale();
}
@Override
public String toString() {
return JsonConvert.root().convertTo(this);
}
}
}