Skip to content

Commit

Permalink
- 优化 表达式解析变量或常量 ToString;
Browse files Browse the repository at this point in the history
  • Loading branch information
2881099 committed Dec 1, 2023
1 parent c038097 commit 5f01fbd
Show file tree
Hide file tree
Showing 28 changed files with 124 additions and 56 deletions.
19 changes: 13 additions & 6 deletions Examples/base_entity/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,6 @@ static void Main(string[] args)
.UseNameConvert(NameConvertType.ToLower)
//.UseMappingPriority(MappingPriorityType.Attribute, MappingPriorityType.FluentApi, MappingPriorityType.Aop)
.UseAdoConnectionPool(true)
.UseConnectionFactory(DataType.Xugu, () => null)

//.UseConnectionString(FreeSql.DataType.Sqlite, "data source=123.db")
//.UseConnectionString(DataType.Sqlite, "data source=db1.db;attachs=db2.db")
Expand All @@ -560,7 +559,7 @@ static void Main(string[] args)
//.UseConnectionString(FreeSql.DataType.Firebird, @"database=localhost:D:\fbdata\EXAMPLES.fdb;user=sysdba;password=123456;max pool size=5")
//.UseQuoteSqlName(false)

//.UseConnectionString(FreeSql.DataType.MySql, "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd;Charset=utf8;SslMode=none;min pool size=1;Max pool size=3;AllowLoadLocalInfile=true")
.UseConnectionString(FreeSql.DataType.MySql, "Data Source=127.0.0.1;Port=3306;User ID=root;Password=root;Initial Catalog=cccddd;Charset=utf8;SslMode=none;min pool size=1;Max pool size=3;AllowLoadLocalInfile=true")

//.UseConnectionString(FreeSql.DataType.SqlServer, "Data Source=.;Integrated Security=True;Initial Catalog=freesqlTest;Pooling=true;Max Pool Size=3;TrustServerCertificate=true")
//.UseAdoConnectionPool(false)
Expand Down Expand Up @@ -599,13 +598,21 @@ static void Main(string[] args)
.UseGenerateCommandParameterWithLambda(true)
.Build();
BaseEntity.Initialization(fsql, () => _asyncUow.Value);
#endregion
#endregion

var v1 = 123123123;
var mysql0111 = fsql.Select<User1>().Where(a => a.Nickname.Contains(v1.ToString())).ToSql();
var mysql0112 = fsql.Select<User1>().Where(a => a.Nickname.Contains(123123123.ToString())).ToSql();
var v2 = "123123123";
var mysql0113 = fsql.Select<User1>().Where(a => a.Nickname.Contains(v2)).ToSql();
var mysql0114 = fsql.Select<User1>().Where(a => a.Nickname.Contains(v2.ToString())).ToSql();
var mysql0115 = fsql.Select<User1>().Where(a => a.Nickname.Contains("123123123")).ToSql();

//fsql.Select<AsTableLog>().Where(a => a.createtime > DateTime.Now && a.createtime < DateTime.Now.AddMonths(1)).ToList();
//var table = fsql.CodeFirst.GetTableByEntity(typeof(AsTableLog));
//table.SetAsTable(new ModAsTableImpl(fsql), table.ColumnsByCs[nameof(AsTableLog.click)]);
//var table = fsql.CodeFirst.GetTableByEntity(typeof(AsTableLog));
//table.SetAsTable(new ModAsTableImpl(fsql), table.ColumnsByCs[nameof(AsTableLog.click)]);

fsql.CodeFirst.GetTableByEntity(typeof(AsTableLog)).AsTableImpl
fsql.CodeFirst.GetTableByEntity(typeof(AsTableLog)).AsTableImpl
.SetTableName(0, "AsTableLog1")
.SetTableName(1, "AsTableLog2");

Expand Down
9 changes: 9 additions & 0 deletions FreeSql.DbContext/FreeSql.DbContext.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Providers/FreeSql.Provider.ClickHouse/ClickHouseExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ public override string ExpressionLambdaToSqlOther(Expression exp, ExpTSC tsc)
tsc.SetMapColumnTmp(null).SetMapTypeReturnOld(oldMapType);
return enumStr;
}
var value = ExpressionGetValue(callExp.Object, out var success);
if (success) return formatSql(value, typeof(string), null, null);
return callExp.Arguments.Count == 0 ? $"cast({getExp(callExp.Object)} as String)" : null;
}
return null;
Expand Down
6 changes: 4 additions & 2 deletions Providers/FreeSql.Provider.Custom/CustomExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,10 @@ public override string ExpressionLambdaToSqlOther(Expression exp, ExpTSC tsc)
var enumStr = ExpressionLambdaToSql(callExp.Object, tsc);
tsc.SetMapColumnTmp(null).SetMapTypeReturnOld(oldMapType);
return enumStr;
}
return callExp.Arguments.Count == 0 ? _utils.Adapter.LambdaConvert_ToString(callExp.Object.Type, getExp(callExp.Object)) : null;
}
var value = ExpressionGetValue(callExp.Object, out var success);
if (success) return formatSql(value, typeof(string), null, null);
return callExp.Arguments.Count == 0 ? _utils.Adapter.LambdaConvert_ToString(callExp.Object.Type, getExp(callExp.Object)) : null;
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,10 @@ public override string ExpressionLambdaToSqlOther(Expression exp, ExpTSC tsc)
var enumStr = ExpressionLambdaToSql(callExp.Object, tsc);
tsc.SetMapColumnTmp(null).SetMapTypeReturnOld(oldMapType);
return enumStr;
}
return callExp.Arguments.Count == 0 ? $"cast({getExp(callExp.Object)} as char)" : null;
}
var value = ExpressionGetValue(callExp.Object, out var success);
if (success) return formatSql(value, typeof(string), null, null);
return callExp.Arguments.Count == 0 ? $"cast({getExp(callExp.Object)} as char)" : null;
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,10 @@ public override string ExpressionLambdaToSqlOther(Expression exp, ExpTSC tsc)
var enumStr = ExpressionLambdaToSql(callExp.Object, tsc);
tsc.SetMapColumnTmp(null).SetMapTypeReturnOld(oldMapType);
return enumStr;
}
return callExp.Arguments.Count == 0 ? $"to_char({getExp(callExp.Object)})" : null;
}
var value = ExpressionGetValue(callExp.Object, out var success);
if (success) return formatSql(value, typeof(string), null, null);
return callExp.Arguments.Count == 0 ? $"to_char({getExp(callExp.Object)})" : null;
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,10 @@ public override string ExpressionLambdaToSqlOther(Expression exp, ExpTSC tsc)
var enumStr = ExpressionLambdaToSql(callExp.Object, tsc);
tsc.SetMapColumnTmp(null).SetMapTypeReturnOld(oldMapType);
return enumStr;
}
return callExp.Arguments.Count == 0 ? $"({getExp(callExp.Object)})::text" : null;
}
var value = ExpressionGetValue(callExp.Object, out var success);
if (success) return formatSql(value, typeof(string), null, null);
return callExp.Arguments.Count == 0 ? $"({getExp(callExp.Object)})::text" : null;
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,10 @@ public override string ExpressionLambdaToSqlOther(Expression exp, ExpTSC tsc)
var enumStr = ExpressionLambdaToSql(callExp.Object, tsc);
tsc.SetMapColumnTmp(null).SetMapTypeReturnOld(oldMapType);
return enumStr;
}
return callExp.Arguments.Count == 0 ? (gentype2 == typeof(Guid) ?
}
var value = ExpressionGetValue(callExp.Object, out var success);
if (success) return formatSql(value, typeof(string), null, null);
return callExp.Arguments.Count == 0 ? (gentype2 == typeof(Guid) ?
$"cast({getExp(callExp.Object)} as varchar(36))" :
$"cast({getExp(callExp.Object)} as nvarchar{(gentype2.IsNumberType() || gentype2.IsEnum ? "(100)" : "(max)")})") : null;
}
Expand Down
6 changes: 4 additions & 2 deletions Providers/FreeSql.Provider.Dameng/DamengExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,10 @@ public override string ExpressionLambdaToSqlOther(Expression exp, ExpTSC tsc)
var enumStr = ExpressionLambdaToSql(callExp.Object, tsc);
tsc.SetMapColumnTmp(null).SetMapTypeReturnOld(oldMapType);
return enumStr;
}
return callExp.Arguments.Count == 0 ? $"to_char({getExp(callExp.Object)})" : null;
}
var value = ExpressionGetValue(callExp.Object, out var success);
if (success) return formatSql(value, typeof(string), null, null);
return callExp.Arguments.Count == 0 ? $"to_char({getExp(callExp.Object)})" : null;
}
return null;
}
Expand Down
6 changes: 4 additions & 2 deletions Providers/FreeSql.Provider.Firebird/FirebirdExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,10 @@ public override string ExpressionLambdaToSqlOther(Expression exp, ExpTSC tsc)
var enumStr = ExpressionLambdaToSql(callExp.Object, tsc);
tsc.SetMapColumnTmp(null).SetMapTypeReturnOld(oldMapType);
return enumStr;
}
return callExp.Arguments.Count == 0 ? $"cast({getExp(callExp.Object)} as blob sub_type 1)" : null;
}
var value = ExpressionGetValue(callExp.Object, out var success);
if (success) return formatSql(value, typeof(string), null, null);
return callExp.Arguments.Count == 0 ? $"cast({getExp(callExp.Object)} as blob sub_type 1)" : null;
}
return null;
}
Expand Down
6 changes: 4 additions & 2 deletions Providers/FreeSql.Provider.GBase/GBaseExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,10 @@ public override string ExpressionLambdaToSqlOther(Expression exp, ExpTSC tsc)
var enumStr = ExpressionLambdaToSql(callExp.Object, tsc);
tsc.SetMapColumnTmp(null).SetMapTypeReturnOld(oldMapType);
return enumStr;
}
return callExp.Arguments.Count == 0 ? $"cast({getExp(callExp.Object)} as varchar(8000))" : null;
}
var value = ExpressionGetValue(callExp.Object, out var success);
if (success) return formatSql(value, typeof(string), null, null);
return callExp.Arguments.Count == 0 ? $"cast({getExp(callExp.Object)} as varchar(8000))" : null;
}
return null;
}
Expand Down
6 changes: 4 additions & 2 deletions Providers/FreeSql.Provider.KingbaseES/KingbaseESExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,10 @@ public override string ExpressionLambdaToSqlOther(Expression exp, ExpTSC tsc)
var enumStr = ExpressionLambdaToSql(callExp.Object, tsc);
tsc.SetMapColumnTmp(null).SetMapTypeReturnOld(oldMapType);
return enumStr;
}
return callExp.Arguments.Count == 0 ? $"({getExp(callExp.Object)})::text" : null;
}
var value = ExpressionGetValue(callExp.Object, out var success);
if (success) return formatSql(value, typeof(string), null, null);
return callExp.Arguments.Count == 0 ? $"({getExp(callExp.Object)})::text" : null;
}
return null;
}
Expand Down
6 changes: 4 additions & 2 deletions Providers/FreeSql.Provider.MsAccess/MsAccessExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,10 @@ public override string ExpressionLambdaToSqlOther(Expression exp, ExpTSC tsc)
var enumStr = ExpressionLambdaToSql(callExp.Object, tsc);
tsc.SetMapColumnTmp(null).SetMapTypeReturnOld(oldMapType);
return enumStr;
}
return callExp.Arguments.Count == 0 ? MsAccessUtils.GetCastSql(getExp(callExp.Object), typeof(string)) : null;
}
var value = ExpressionGetValue(callExp.Object, out var success);
if (success) return formatSql(value, typeof(string), null, null);
return callExp.Arguments.Count == 0 ? MsAccessUtils.GetCastSql(getExp(callExp.Object), typeof(string)) : null;
}
return null;
}
Expand Down
6 changes: 4 additions & 2 deletions Providers/FreeSql.Provider.MySql/MySqlExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,10 @@ public override string ExpressionLambdaToSqlOther(Expression exp, ExpTSC tsc)
var enumStr = ExpressionLambdaToSql(callExp.Object, tsc);
tsc.SetMapColumnTmp(null).SetMapTypeReturnOld(oldMapType);
return enumStr;
}
return callExp.Arguments.Count == 0 ? $"cast({getExp(callExp.Object)} as char)" : null;
}
var value = ExpressionGetValue(callExp.Object, out var success);
if (success) return formatSql(value, typeof(string), null, null);
return callExp.Arguments.Count == 0 ? $"cast({getExp(callExp.Object)} as char)" : null;
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,10 @@ public override string ExpressionLambdaToSqlOther(Expression exp, ExpTSC tsc)
var enumStr = ExpressionLambdaToSql(callExp.Object, tsc);
tsc.SetMapColumnTmp(null).SetMapTypeReturnOld(oldMapType);
return enumStr;
}
return callExp.Arguments.Count == 0 ? $"to_char({getExp(callExp.Object)})" : null;
}
var value = ExpressionGetValue(callExp.Object, out var success);
if (success) return formatSql(value, typeof(string), null, null);
return callExp.Arguments.Count == 0 ? $"to_char({getExp(callExp.Object)})" : null;
}
return null;
}
Expand Down
6 changes: 4 additions & 2 deletions Providers/FreeSql.Provider.Odbc/Default/OdbcExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,10 @@ public override string ExpressionLambdaToSqlOther(Expression exp, ExpTSC tsc)
var enumStr = ExpressionLambdaToSql(callExp.Object, tsc);
tsc.SetMapColumnTmp(null).SetMapTypeReturnOld(oldMapType);
return enumStr;
}
return callExp.Arguments.Count == 0 ? _utils.Adapter.LambdaConvert_ToString(callExp.Object.Type, getExp(callExp.Object)) : null;
}
var value = ExpressionGetValue(callExp.Object, out var success);
if (success) return formatSql(value, typeof(string), null, null);
return callExp.Arguments.Count == 0 ? _utils.Adapter.LambdaConvert_ToString(callExp.Object.Type, getExp(callExp.Object)) : null;
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,10 @@ public override string ExpressionLambdaToSqlOther(Expression exp, ExpTSC tsc)
var enumStr = ExpressionLambdaToSql(callExp.Object, tsc);
tsc.SetMapColumnTmp(null).SetMapTypeReturnOld(oldMapType);
return enumStr;
}
return callExp.Arguments.Count == 0 ? $"({getExp(callExp.Object)})::text" : null;
}
var value = ExpressionGetValue(callExp.Object, out var success);
if (success) return formatSql(value, typeof(string), null, null);
return callExp.Arguments.Count == 0 ? $"({getExp(callExp.Object)})::text" : null;
}
return null;
}
Expand Down
6 changes: 4 additions & 2 deletions Providers/FreeSql.Provider.Odbc/MySql/OdbcMySqlExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,10 @@ public override string ExpressionLambdaToSqlOther(Expression exp, ExpTSC tsc)
var enumStr = ExpressionLambdaToSql(callExp.Object, tsc);
tsc.SetMapColumnTmp(null).SetMapTypeReturnOld(oldMapType);
return enumStr;
}
return callExp.Arguments.Count == 0 ? $"cast({getExp(callExp.Object)} as char)" : null;
}
var value = ExpressionGetValue(callExp.Object, out var success);
if (success) return formatSql(value, typeof(string), null, null);
return callExp.Arguments.Count == 0 ? $"cast({getExp(callExp.Object)} as char)" : null;
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,10 @@ public override string ExpressionLambdaToSqlOther(Expression exp, ExpTSC tsc)
var enumStr = ExpressionLambdaToSql(callExp.Object, tsc);
tsc.SetMapColumnTmp(null).SetMapTypeReturnOld(oldMapType);
return enumStr;
}
return callExp.Arguments.Count == 0 ? $"to_char({getExp(callExp.Object)})" : null;
}
var value = ExpressionGetValue(callExp.Object, out var success);
if (success) return formatSql(value, typeof(string), null, null);
return callExp.Arguments.Count == 0 ? $"to_char({getExp(callExp.Object)})" : null;
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,10 @@ public override string ExpressionLambdaToSqlOther(Expression exp, ExpTSC tsc)
var enumStr = ExpressionLambdaToSql(callExp.Object, tsc);
tsc.SetMapColumnTmp(null).SetMapTypeReturnOld(oldMapType);
return enumStr;
}
return callExp.Arguments.Count == 0 ? $"({getExp(callExp.Object)})::text" : null;
}
var value = ExpressionGetValue(callExp.Object, out var success);
if (success) return formatSql(value, typeof(string), null, null);
return callExp.Arguments.Count == 0 ? $"({getExp(callExp.Object)})::text" : null;
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,10 @@ public override string ExpressionLambdaToSqlOther(Expression exp, ExpTSC tsc)
var enumStr = ExpressionLambdaToSql(callExp.Object, tsc);
tsc.SetMapColumnTmp(null).SetMapTypeReturnOld(oldMapType);
return enumStr;
}
return callExp.Arguments.Count == 0 ? (gentype2 == typeof(Guid) ?
}
var value = ExpressionGetValue(callExp.Object, out var success);
if (success) return formatSql(value, typeof(string), null, null);
return callExp.Arguments.Count == 0 ? (gentype2 == typeof(Guid) ?
$"cast({getExp(callExp.Object)} as varchar(36))" :
$"cast({getExp(callExp.Object)} as nvarchar{(gentype2.IsNumberType() || gentype2.IsEnum ? "(100)" : "(max)")})") : null;
}
Expand Down
Loading

0 comments on commit 5f01fbd

Please sign in to comment.