- 增加 ColumnAttribute Precision/Scale 设置;

This commit is contained in:
28810
2020-07-31 03:07:13 +08:00
parent af153295f8
commit 73c87513d2
9 changed files with 235 additions and 163 deletions

View File

@ -41,7 +41,7 @@ namespace FreeSql.Internal
field.Append(", ").Append(parent.DbField);
if (index >= 0) field.Append(_common.FieldAsAlias($"as{++index}"));
else if (index == ReadAnonymousFieldAsCsName && string.IsNullOrEmpty(parent.CsName) == false) field.Append(_common.FieldAsAlias(parent.CsName));
if (parent.CsType == null) parent.CsType = exp.Type;
if (parent.CsType == null && exp.Type.IsValueType) parent.CsType = exp.Type;
return false;
case ExpressionType.Convert: return ReadAnonymousField(_tables, field, parent, ref index, (exp as UnaryExpression)?.Operand, getSelectGroupingMapString, whereCascadeExpression, isAllDtoMap);
case ExpressionType.Constant:
@ -61,7 +61,7 @@ namespace FreeSql.Internal
field.Append(", ").Append(parent.DbField);
if (index >= 0) field.Append(_common.FieldAsAlias($"as{++index}"));
else if (index == ReadAnonymousFieldAsCsName && string.IsNullOrEmpty(parent.CsName) == false) field.Append(_common.FieldAsAlias(parent.CsName));
if (parent.CsType == null) parent.CsType = exp.Type;
if (parent.CsType == null && exp.Type.IsValueType) parent.CsType = exp.Type;
return false;
case ExpressionType.Call:
var callExp = exp as MethodCallExpression;
@ -79,7 +79,7 @@ namespace FreeSql.Internal
field.Append(", ").Append(parent.DbField);
if (index >= 0) field.Append(_common.FieldAsAlias($"as{++index}"));
else if (index == ReadAnonymousFieldAsCsName && string.IsNullOrEmpty(parent.CsName) == false) field.Append(_common.FieldAsAlias(parent.CsName));
if (parent.CsType == null) parent.CsType = exp.Type;
if (parent.CsType == null && exp.Type.IsValueType) parent.CsType = exp.Type;
return false;
case ExpressionType.Parameter:
case ExpressionType.MemberAccess:
@ -265,7 +265,7 @@ namespace FreeSql.Internal
field.Append(", ").Append(parent.DbField);
if (index >= 0) field.Append(_common.FieldAsAlias($"as{++index}"));
else if (index == ReadAnonymousFieldAsCsName && string.IsNullOrEmpty(parent.CsName) == false) field.Append(_common.FieldAsAlias(parent.CsName));
if (parent.CsType == null) parent.CsType = exp.Type;
if (parent.CsType == null && exp.Type.IsValueType) parent.CsType = exp.Type;
return false;
}
public object ReadAnonymous(ReadAnonymousTypeInfo parent, DbDataReader dr, ref int index, bool notRead, ReadAnonymousDbValueRef dbValue)

View File

@ -154,6 +154,8 @@ namespace FreeSql.Internal
if (trycol.ServerTime != DateTimeKind.Unspecified) attr.ServerTime = trycol.ServerTime;
if (trycol._StringLength != null) attr.StringLength = trycol.StringLength;
if (!string.IsNullOrEmpty(trycol.InsertValueSql)) attr.InsertValueSql = trycol.InsertValueSql;
if (trycol._Precision != null) attr.Precision = trycol.Precision;
if (trycol._Scale != null) attr.Scale = trycol.Scale;
}
var attrs = proto.GetCustomAttributes(typeof(ColumnAttribute), false);
foreach (var tryattrobj in attrs)
@ -174,7 +176,9 @@ namespace FreeSql.Internal
if (tryattr._CanUpdate != null) attr._CanUpdate = tryattr.CanUpdate;
if (tryattr.ServerTime != DateTimeKind.Unspecified) attr.ServerTime = tryattr.ServerTime;
if (tryattr._StringLength != null) attr.StringLength = tryattr.StringLength;
if (!string.IsNullOrEmpty(tryattr.InsertValueSql)) attr.InsertValueSql = tryattr.InsertValueSql;
if (!string.IsNullOrEmpty(tryattr.InsertValueSql)) attr.InsertValueSql = tryattr.InsertValueSql;
if (tryattr._Precision != null) attr.Precision = tryattr.Precision;
if (tryattr._Scale != null) attr.Scale = tryattr.Scale;
}
ColumnAttribute ret = null;
if (!string.IsNullOrEmpty(attr.Name)) ret = attr;

View File

@ -279,19 +279,19 @@ namespace FreeSql.Internal
if (colattr.MapType == typeof(byte[]) && colattr.StringLength != 0)
{
int strlen = colattr.StringLength;
var charPatten = @"(VARBINARY|BINARY|BYTEA)\s*(\([^\)]*\))?";
var bytePatten = @"(VARBINARY|BINARY|BYTEA)\s*(\([^\)]*\))?";
switch (common._orm.Ado.DataType)
{
case DataType.MySql:
case DataType.OdbcMySql:
if (strlen == -2) colattr.DbType = "LONGBLOB";
else if (strlen < 0) colattr.DbType = "BLOB";
else colattr.DbType = Regex.Replace(colattr.DbType, charPatten, $"$1({strlen})");
else colattr.DbType = Regex.Replace(colattr.DbType, bytePatten, $"$1({strlen})");
break;
case DataType.SqlServer:
case DataType.OdbcSqlServer:
if (strlen < 0) colattr.DbType = Regex.Replace(colattr.DbType, charPatten, $"$1(MAX)");
else colattr.DbType = Regex.Replace(colattr.DbType, charPatten, $"$1({strlen})");
if (strlen < 0) colattr.DbType = Regex.Replace(colattr.DbType, bytePatten, $"$1(MAX)");
else colattr.DbType = Regex.Replace(colattr.DbType, bytePatten, $"$1({strlen})");
break;
case DataType.PostgreSQL:
case DataType.OdbcPostgreSQL:
@ -314,10 +314,15 @@ namespace FreeSql.Internal
break;
case DataType.MsAccess:
if (strlen < 0) colattr.DbType = "BLOB";
else colattr.DbType = Regex.Replace(colattr.DbType, charPatten, $"$1({strlen})");
else colattr.DbType = Regex.Replace(colattr.DbType, bytePatten, $"$1({strlen})");
break;
}
}
if (colattr.MapType == typeof(decimal) && colattr.Precision > 0)
{
var decimalPatten = @"(DECIMAL|NUMERIC|NUMBER)\s*(\([^\)]*\))?";
colattr.DbType = Regex.Replace(colattr.DbType, decimalPatten, $"$1({colattr.Precision},{colattr.Scale})");
}
if (trytb.Columns.ContainsKey(colattr.Name)) throw new Exception($"ColumnAttribute.Name {colattr.Name} 重复存在,请检查(注意:不区分大小写)");
if (trytb.ColumnsByCs.ContainsKey(p.Name)) throw new Exception($"属性名 {p.Name} 重复存在,请检查(注意:不区分大小写)");