Skip to content

Commit 5d73405

Browse files
2881028810
authored andcommitted
- 优化 实体数据属性 DbDefaultValue 处理;
- 修复 Expression 表达式解析 Convert 的判断 bug;
1 parent d54b245 commit 5d73405

File tree

9 files changed

+32
-14
lines changed

9 files changed

+32
-14
lines changed

Examples/restful/Startup.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,18 @@ public Startup(IConfiguration configuration, ILoggerFactory loggerFactory) {
1717
.UseLogger(loggerFactory.CreateLogger<IFreeSql>())
1818
.UseAutoSyncStructure(true)
1919
.Build();
20+
21+
Fsql.Aop.CurdAfter = (s, e) => {
22+
if (e.ElapsedMilliseconds > 200) {
23+
//记录日志
24+
//发送短信给负责人
25+
}
26+
};
27+
28+
//Fsql.Aop.Where = (s, e) => {
29+
// if (e.Parameters[0]?.ToString() == "1")
30+
// e.IsCancel = true;
31+
//};
2032
}
2133

2234
public IConfiguration Configuration { get; }

FreeSql.Tests/UnitTest1.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ IEnumerator IEnumerable.GetEnumerator() {
126126
[Fact]
127127
public void Test1() {
128128

129-
g.sqlite.CodeFirst.SyncStructure<TestEnumable>();
129+
Assert.Throws<Exception>(() => g.sqlite.CodeFirst.SyncStructure<TestEnumable>());
130130

131131
var TestEnumable = new TestEnumable();
132132

FreeSql/FreeSql.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFramework>netstandard2.0</TargetFramework>
5-
<Version>0.5.22</Version>
5+
<Version>0.5.23</Version>
66
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
77
<Authors>YeXiangQin</Authors>
88
<Description>FreeSql is the most convenient ORM in dotnet. It supports Mysql, Postgresql, SqlServer, Oracle and Sqlite.</Description>
@@ -18,7 +18,7 @@
1818
</PropertyGroup>
1919

2020
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
21-
<DocumentationFile>C:\Users\28810\Desktop\github\FreeSql\FreeSql\FreeSql.xml</DocumentationFile>
21+
<DocumentationFile>FreeSql.xml</DocumentationFile>
2222
<WarningLevel>3</WarningLevel>
2323
</PropertyGroup>
2424

FreeSql/Internal/UtilsExpressionTree.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,6 @@ internal static TableInfo GetTableByEntity(Type entity, CommonUtils common) {
109109
if (tmpLt.Contains("BYTE")) tmpLt = tmpLt.Replace("BYTE", " BYTE");
110110
return tmpLt;
111111
});
112-
colattr.DbDefautValue = trytb.Properties[p.Name].GetValue(Activator.CreateInstance(trytb.Type));
113-
if (colattr.DbDefautValue != null && p.PropertyType != colattr.MapType) colattr.DbDefautValue = Utils.GetDataReaderValue(colattr.MapType, colattr.DbDefautValue);
114-
if (colattr.DbDefautValue == null) colattr.DbDefautValue = tp?.defaultValue;
115-
if (colattr.IsNullable == false && colattr.DbDefautValue == null)
116-
colattr.DbDefautValue = Activator.CreateInstance(colattr.MapType.IsNullableType() ? colattr.MapType.GenericTypeArguments.FirstOrDefault() : colattr.MapType);
117112
if (colattr.IsIdentity == true && colattr.MapType.IsNumberType() == false)
118113
colattr.IsIdentity = false;
119114
if (setMethod == null) colattr.IsIgnore = true;
@@ -128,6 +123,17 @@ internal static TableInfo GetTableByEntity(Type entity, CommonUtils common) {
128123
trytb.ColumnsByCsIgnore.Add(p.Name, col);
129124
continue;
130125
}
126+
colattr.DbDefautValue = trytb.Properties[p.Name].GetValue(Activator.CreateInstance(trytb.Type));
127+
if (colattr.DbDefautValue != null && p.PropertyType != colattr.MapType) colattr.DbDefautValue = Utils.GetDataReaderValue(colattr.MapType, colattr.DbDefautValue);
128+
if (colattr.DbDefautValue == null) colattr.DbDefautValue = tp?.defaultValue;
129+
if (colattr.IsNullable == false && colattr.DbDefautValue == null) {
130+
var citype = colattr.MapType.IsNullableType() ? colattr.MapType.GenericTypeArguments.FirstOrDefault() : colattr.MapType;
131+
if (citype.IsArray)
132+
colattr.DbDefautValue = Array.CreateInstance(citype, 0);
133+
else
134+
colattr.DbDefautValue = Activator.CreateInstance(citype);
135+
}
136+
131137
trytb.Columns.Add(colattr.Name, col);
132138
trytb.ColumnsByCs.Add(p.Name, col);
133139
}

FreeSql/MySql/MySqlExpression.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ internal override string ExpressionLambdaToSqlOther(Expression exp, ExpTSC tsc)
1818
case ExpressionType.Convert:
1919
var operandExp = (exp as UnaryExpression)?.Operand;
2020
var gentype = exp.Type.NullableTypeOrThis();
21-
if (gentype != exp.Type.NullableTypeOrThis()) {
22-
switch (exp.Type.NullableTypeOrThis().ToString()) {
21+
if (gentype != operandExp.Type.NullableTypeOrThis()) {
22+
switch (gentype.ToString()) {
2323
case "System.Boolean": return $"({getExp(operandExp)} not in ('0','false'))";
2424
case "System.Byte": return $"cast({getExp(operandExp)} as unsigned)";
2525
case "System.Char": return $"substr(cast({getExp(operandExp)} as char), 1, 1)";

FreeSql/Oracle/OracleExpression.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ internal override string ExpressionLambdaToSqlOther(Expression exp, ExpTSC tsc)
1818
case ExpressionType.Convert:
1919
var operandExp = (exp as UnaryExpression)?.Operand;
2020
var gentype = exp.Type.NullableTypeOrThis();
21-
if (gentype != exp.Type.NullableTypeOrThis()) {
21+
if (gentype != operandExp.Type.NullableTypeOrThis()) {
2222
switch (exp.Type.NullableTypeOrThis().ToString()) {
2323
//case "System.Boolean": return $"({getExp(operandExp)} not in ('0','false'))";
2424
case "System.Byte": return $"cast({getExp(operandExp)} as number)";

FreeSql/PostgreSQL/PostgreSQLExpression.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ internal override string ExpressionLambdaToSqlOther(Expression exp, ExpTSC tsc)
1919
case ExpressionType.Convert:
2020
var operandExp = (exp as UnaryExpression)?.Operand;
2121
var gentype = exp.Type.NullableTypeOrThis();
22-
if (gentype != exp.Type.NullableTypeOrThis()) {
22+
if (gentype != operandExp.Type.NullableTypeOrThis()) {
2323
switch (exp.Type.NullableTypeOrThis().ToString()) {
2424
case "System.Boolean": return $"(({getExp(operandExp)})::varchar not in ('0','false','f','no'))";
2525
case "System.Byte": return $"({getExp(operandExp)})::int2";

FreeSql/SqlServer/SqlServerExpression.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ internal override string ExpressionLambdaToSqlOther(Expression exp, ExpTSC tsc)
1818
case ExpressionType.Convert:
1919
var operandExp = (exp as UnaryExpression)?.Operand;
2020
var gentype = exp.Type.NullableTypeOrThis();
21-
if (gentype != exp.Type.NullableTypeOrThis()) {
21+
if (gentype != operandExp.Type.NullableTypeOrThis()) {
2222
switch (gentype.ToString()) {
2323
case "System.Boolean": return $"(cast({getExp(operandExp)} as varchar) not in ('0','false'))";
2424
case "System.Byte": return $"cast({getExp(operandExp)} as tinyint)";

FreeSql/Sqlite/SqliteExpression.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ internal override string ExpressionLambdaToSqlOther(Expression exp, ExpTSC tsc)
1818
case ExpressionType.Convert:
1919
var operandExp = (exp as UnaryExpression)?.Operand;
2020
var gentype = exp.Type.NullableTypeOrThis();
21-
if (gentype != exp.Type.NullableTypeOrThis()) {
21+
if (gentype != operandExp.Type.NullableTypeOrThis()) {
2222
switch (exp.Type.NullableTypeOrThis().ToString()) {
2323
case "System.Boolean": return $"({getExp(operandExp)} not in ('0','false'))";
2424
case "System.Byte": return $"cast({getExp(operandExp)} as int2)";

0 commit comments

Comments
 (0)