Update enum

This commit is contained in:
sunkaixuna
2021-12-29 21:22:34 +08:00
parent 8fb4b46265
commit d9382a1fd9
17 changed files with 70 additions and 14 deletions

View File

@@ -605,9 +605,17 @@ namespace SqlSugar
TableId = i TableId = i
}; };
if (columnInfo.PropertyType.IsEnum()) if (columnInfo.PropertyType.IsEnum())
{
if (this.Context.CurrentConnectionConfig.MoreSettings?.TableEnumIsString == true)
{
columnInfo.Value = columnInfo.Value.ToString();
columnInfo.PropertyType = UtilConstants.StringType;
}
else
{ {
columnInfo.Value = Convert.ToInt64(columnInfo.Value); columnInfo.Value = Convert.ToInt64(columnInfo.Value);
} }
}
insertItem.Add(columnInfo); insertItem.Add(columnInfo);
} }
} }

View File

@@ -207,9 +207,16 @@ namespace SqlSugar
{ {
var value = item.PropertyInfo.GetValue(dataItem.Item, null); var value = item.PropertyInfo.GetValue(dataItem.Item, null);
if (value != null&&value.GetType().IsEnum()) if (value != null&&value.GetType().IsEnum())
{
if (this.Context.CurrentConnectionConfig.MoreSettings?.TableEnumIsString == true)
{
value = value.ToString();
}
else
{ {
value = Convert.ToInt64(value); value = Convert.ToInt64(value);
} }
}
condition.ConditionalList.Add(new KeyValuePair<WhereType, ConditionalModel>(i==0?WhereType.Or :WhereType.And, new ConditionalModel() condition.ConditionalList.Add(new KeyValuePair<WhereType, ConditionalModel>(i==0?WhereType.Or :WhereType.And, new ConditionalModel()
{ {
FieldName = item.DbColumnName, FieldName = item.DbColumnName,
@@ -228,6 +235,7 @@ namespace SqlSugar
ILambdaExpressions resolveExpress = InstanceFactory.GetLambdaExpressions(this.Context.CurrentConnectionConfig); ; ILambdaExpressions resolveExpress = InstanceFactory.GetLambdaExpressions(this.Context.CurrentConnectionConfig); ;
if (this.Context.CurrentConnectionConfig.MoreSettings != null) if (this.Context.CurrentConnectionConfig.MoreSettings != null)
{ {
resolveExpress.TableEnumIsString = this.Context.CurrentConnectionConfig.MoreSettings.TableEnumIsString;
resolveExpress.PgSqlIsAutoToLower = this.Context.CurrentConnectionConfig.MoreSettings.PgSqlIsAutoToLower; resolveExpress.PgSqlIsAutoToLower = this.Context.CurrentConnectionConfig.MoreSettings.PgSqlIsAutoToLower;
} }
else else

View File

@@ -147,6 +147,7 @@ namespace SqlSugar
this.LambdaExpressions.Clear(); this.LambdaExpressions.Clear();
if (this.Context.CurrentConnectionConfig.MoreSettings != null) if (this.Context.CurrentConnectionConfig.MoreSettings != null)
{ {
resolveExpress.TableEnumIsString = this.Context.CurrentConnectionConfig.MoreSettings.TableEnumIsString;
resolveExpress.PgSqlIsAutoToLower = this.Context.CurrentConnectionConfig.MoreSettings.PgSqlIsAutoToLower; resolveExpress.PgSqlIsAutoToLower = this.Context.CurrentConnectionConfig.MoreSettings.PgSqlIsAutoToLower;
} }
else else

View File

@@ -102,6 +102,7 @@ namespace SqlSugar
this.LambdaExpressions.Clear(); this.LambdaExpressions.Clear();
if (this.Context.CurrentConnectionConfig.MoreSettings != null) if (this.Context.CurrentConnectionConfig.MoreSettings != null)
{ {
resolveExpress.TableEnumIsString = this.Context.CurrentConnectionConfig.MoreSettings.TableEnumIsString;
resolveExpress.PgSqlIsAutoToLower = this.Context.CurrentConnectionConfig.MoreSettings.PgSqlIsAutoToLower; resolveExpress.PgSqlIsAutoToLower = this.Context.CurrentConnectionConfig.MoreSettings.PgSqlIsAutoToLower;
} }
else else
@@ -198,7 +199,7 @@ namespace SqlSugar
{ {
if (this.Context.CurrentConnectionConfig.MoreSettings?.TableEnumIsString == true) if (this.Context.CurrentConnectionConfig.MoreSettings?.TableEnumIsString == true)
{ {
return value.ToString(); return value.ToSqlValue();
} }
else else
{ {

View File

@@ -234,6 +234,7 @@ namespace SqlSugar
this.LambdaExpressions.Clear(); this.LambdaExpressions.Clear();
if (this.Context.CurrentConnectionConfig.MoreSettings != null) if (this.Context.CurrentConnectionConfig.MoreSettings != null)
{ {
resolveExpress.TableEnumIsString = this.Context.CurrentConnectionConfig.MoreSettings.TableEnumIsString;
resolveExpress.PgSqlIsAutoToLower = this.Context.CurrentConnectionConfig.MoreSettings.PgSqlIsAutoToLower; resolveExpress.PgSqlIsAutoToLower = this.Context.CurrentConnectionConfig.MoreSettings.PgSqlIsAutoToLower;
} }
else else

View File

@@ -121,6 +121,7 @@ namespace SqlSugar
if (this.Context.CurrentConnectionConfig.MoreSettings != null) if (this.Context.CurrentConnectionConfig.MoreSettings != null)
{ {
resolveExpress.PgSqlIsAutoToLower = this.Context.CurrentConnectionConfig.MoreSettings.PgSqlIsAutoToLower; resolveExpress.PgSqlIsAutoToLower = this.Context.CurrentConnectionConfig.MoreSettings.PgSqlIsAutoToLower;
resolveExpress.TableEnumIsString = this.Context.CurrentConnectionConfig.MoreSettings.TableEnumIsString;
} }
else else
{ {
@@ -306,7 +307,7 @@ namespace SqlSugar
{ {
if (this.Context.CurrentConnectionConfig.MoreSettings?.TableEnumIsString == true) if (this.Context.CurrentConnectionConfig.MoreSettings?.TableEnumIsString == true)
{ {
return value.ToString(); return value.ToSqlValue();
} }
else else
{ {

View File

@@ -550,9 +550,17 @@ namespace SqlSugar
TableId = i TableId = i
}; };
if (columnInfo.PropertyType.IsEnum()) if (columnInfo.PropertyType.IsEnum())
{
if (this.Context.CurrentConnectionConfig.MoreSettings?.TableEnumIsString == true)
{
columnInfo.PropertyType = UtilConstants.StringType;
columnInfo.Value = columnInfo.Value.ToString();
}
else
{ {
columnInfo.Value = Convert.ToInt64(columnInfo.Value); columnInfo.Value = Convert.ToInt64(columnInfo.Value);
} }
}
updateItem.Add(columnInfo); updateItem.Add(columnInfo);
} }
this.UpdateBuilder.DbColumnInfoList.AddRange(updateItem); this.UpdateBuilder.DbColumnInfoList.AddRange(updateItem);

View File

@@ -53,11 +53,21 @@ namespace SqlSugar
} }
} }
public static object GetValue(object value) public static object GetValue(object value,ExpressionContext context)
{ {
if (value == null) return value; if (value == null) return value;
var type = value.GetType(); var type = value.GetType();
if (type.IsEnum() && type != typeof(DateType) && type != typeof(JoinType) && type != typeof(OrderByType)) return Convert.ToInt64(value); if (type.IsEnum() && type != typeof(DateType) && type != typeof(JoinType) && type != typeof(OrderByType))
{
if (context.TableEnumIsString == true)
{
return value.ToString();
}
else
{
return Convert.ToInt64(value);
}
}
else else
return value; return value;
} }

View File

@@ -44,6 +44,7 @@ namespace SqlSugar
public MappingTableList MappingTables { get; set; } public MappingTableList MappingTables { get; set; }
public IgnoreColumnList IgnoreComumnList { get; set; } public IgnoreColumnList IgnoreComumnList { get; set; }
public bool PgSqlIsAutoToLower { get; set; } public bool PgSqlIsAutoToLower { get; set; }
public bool? TableEnumIsString { get; set; }
public List<SqlFuncExternal> SqlFuncServices { get; set; } public List<SqlFuncExternal> SqlFuncServices { get; set; }
public Expression RootExpression { get; set; } public Expression RootExpression { get; set; }
public bool IsSingle public bool IsSingle
@@ -131,6 +132,7 @@ namespace SqlSugar
copyContext.PgSqlIsAutoToLower = this.PgSqlIsAutoToLower; copyContext.PgSqlIsAutoToLower = this.PgSqlIsAutoToLower;
copyContext.IsSingle = this.IsSingle; copyContext.IsSingle = this.IsSingle;
copyContext.RootExpression = this.RootExpression; copyContext.RootExpression = this.RootExpression;
copyContext.TableEnumIsString = this.TableEnumIsString;
return copyContext; return copyContext;
} }
public ExpressionContext GetCopyContextWithMapping() public ExpressionContext GetCopyContextWithMapping()
@@ -145,6 +147,7 @@ namespace SqlSugar
copyContext.InitMappingInfo = this.InitMappingInfo; copyContext.InitMappingInfo = this.InitMappingInfo;
copyContext.RefreshMapping = this.RefreshMapping; copyContext.RefreshMapping = this.RefreshMapping;
copyContext.PgSqlIsAutoToLower = this.PgSqlIsAutoToLower; copyContext.PgSqlIsAutoToLower = this.PgSqlIsAutoToLower;
copyContext.TableEnumIsString = this.TableEnumIsString;
copyContext.IsSingle = this.IsSingle; copyContext.IsSingle = this.IsSingle;
copyContext.RootExpression = this.RootExpression; copyContext.RootExpression = this.RootExpression;
return copyContext; return copyContext;

View File

@@ -224,9 +224,16 @@ namespace SqlSugar
var appendValue = this.Context.SqlParameterKeyWord + ExpressionConst.Const + Context.ParameterIndex; var appendValue = this.Context.SqlParameterKeyWord + ExpressionConst.Const + Context.ParameterIndex;
Context.ParameterIndex++; Context.ParameterIndex++;
if (value != null && value.GetType().IsEnum()) if (value != null && value.GetType().IsEnum())
{
if (this.Context.TableEnumIsString == true)
{
value = value.ToString();
}
else
{ {
value = Convert.ToInt64(value); value = Convert.ToInt64(value);
} }
}
this.Context.Parameters.Add(new SugarParameter(appendValue, value)); this.Context.Parameters.Add(new SugarParameter(appendValue, value));
appendValue = string.Format(" {0} ", appendValue); appendValue = string.Format(" {0} ", appendValue);
if (isLeft == true) if (isLeft == true)

View File

@@ -12,7 +12,7 @@ namespace SqlSugar
{ {
var expression = base.Expression as ConstantExpression; var expression = base.Expression as ConstantExpression;
var isLeft = parameter.IsLeft; var isLeft = parameter.IsLeft;
object value = ExpressionTool.GetValue(expression.Value); object value = ExpressionTool.GetValue(expression.Value,this.Context);
var baseParameter = parameter.BaseParameter; var baseParameter = parameter.BaseParameter;
baseParameter.ChildExpression = expression; baseParameter.ChildExpression = expression;
var isSetTempData = baseParameter.CommonTempData.HasValue() && baseParameter.CommonTempData.Equals(CommonTempDataType.Result); var isSetTempData = baseParameter.CommonTempData.HasValue() && baseParameter.CommonTempData.Equals(CommonTempDataType.Result);

View File

@@ -28,6 +28,7 @@ namespace SqlSugar
Action RefreshMapping { get; set; } Action RefreshMapping { get; set; }
bool PgSqlIsAutoToLower { get; set; } bool PgSqlIsAutoToLower { get; set; }
Expression RootExpression { get; set; } Expression RootExpression { get; set; }
bool? TableEnumIsString { get; set; }
string GetAsString(string fieldName, string fieldValue); string GetAsString(string fieldName, string fieldValue);
void Resolve(Expression expression, ResolveExpressType resolveType); void Resolve(Expression expression, ResolveExpressType resolveType);

View File

@@ -59,7 +59,7 @@ namespace SqlSugar
{ {
if (this.Context.CurrentConnectionConfig.MoreSettings?.TableEnumIsString == true) if (this.Context.CurrentConnectionConfig.MoreSettings?.TableEnumIsString == true)
{ {
return value.ToString(); return value.ToSqlValue(); ;
} }
else else
{ {

View File

@@ -121,7 +121,7 @@ namespace SqlSugar
{ {
if (this.Context.CurrentConnectionConfig.MoreSettings?.TableEnumIsString == true) if (this.Context.CurrentConnectionConfig.MoreSettings?.TableEnumIsString == true)
{ {
return value.ToString(); return value.ToSqlValue();
} }
else else
{ {

View File

@@ -64,9 +64,16 @@ namespace SqlSugar
return bytesString; return bytesString;
} }
else if (type.IsEnum()) else if (type.IsEnum())
{
if (this.Context.CurrentConnectionConfig.MoreSettings?.TableEnumIsString == true)
{
return value.ToSqlValue();
}
else
{ {
return Convert.ToInt64(value); return Convert.ToInt64(value);
} }
}
else if (type == UtilConstants.BoolType) else if (type == UtilConstants.BoolType)
{ {
return value.ObjToBool() ? "1" : "0"; return value.ObjToBool() ? "1" : "0";

View File

@@ -101,7 +101,7 @@ namespace SqlSugar
{ {
if (this.Context.CurrentConnectionConfig.MoreSettings?.TableEnumIsString == true) if (this.Context.CurrentConnectionConfig.MoreSettings?.TableEnumIsString == true)
{ {
return value.ToString(); return value.ToSqlValue();
} }
else else
{ {

View File

@@ -63,7 +63,7 @@ namespace SqlSugar
{ {
if (this.Context.CurrentConnectionConfig.MoreSettings?.TableEnumIsString == true) if (this.Context.CurrentConnectionConfig.MoreSettings?.TableEnumIsString == true)
{ {
return value.ToString(); return value.ToSqlValue();
} }
else else
{ {