Browse Source

Update exp to sql

pull/12/head
sunkaixuna 3 years ago
parent
commit
cf4ec53150
  1. 1
      Src/Asp.Net/SqlServerTest/SqlServerTest.csproj
  2. 1
      Src/Asp.Net/SqlServerTest/UnitTest/Main.cs
  3. 191
      Src/Asp.Net/SqlServerTest/UnitTest/UCustom02.cs
  4. 5
      Src/Asp.Net/SqlSugar/ExpressionsToSql/Subquery/Items/SubWhere.cs
  5. 6
      Src/Asp.Net/SqlSugar/ExpressionsToSql/Subquery/SubResolve.cs

1
Src/Asp.Net/SqlServerTest/SqlServerTest.csproj

@ -95,6 +95,7 @@
<Compile Include="Demo\Demo0_SqlSugarClient.cs" />
<Compile Include="Models\ViewOrder.cs" />
<Compile Include="UnitTest\UCustom01.cs" />
<Compile Include="UnitTest\UCustom02.cs" />
<Compile Include="UnitTest\UDelete.cs" />
<Compile Include="UnitTest\UFastest.cs" />
<Compile Include="UnitTest\UConfig.cs" />

1
Src/Asp.Net/SqlServerTest/UnitTest/Main.cs

@ -32,6 +32,7 @@ namespace OrmTest
public static void Init()
{
UCustom01.Init();
UCustom02.Init();
SubQueryTest();
UConfig();
DeleteTest();

191
Src/Asp.Net/SqlServerTest/UnitTest/UCustom02.cs

@ -0,0 +1,191 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SqlSugar;
namespace OrmTest
{
[SugarTable("UnitTest0")]
public class Test0
{
[SugarColumn(IsNullable = false, IsPrimaryKey = true)]
public long Id { get; set; }
public string Title { get; set; }
}
[SugarTable("UnitTest_1")]
public class Test1
{
[SugarColumn(IsNullable = false, IsPrimaryKey = true)]
public long Id { get; set; }
[SugarColumn(ColumnName = "test0id")]
public long Test0Id { get; set; }
public string Title1 { get; set; }
}
[SugarTable("UnitTest_2")]
public class Test2
{
[SugarColumn(IsNullable = false, IsPrimaryKey = true)]
public long Id { get; set; }
[SugarColumn(ColumnName = "test0i_d")]
public long Test0Id { get; set; }
public string Title2 { get; set; }
}
[SugarTable("UnitTest_3")]
public class Test3
{
[SugarColumn(IsNullable = false, IsPrimaryKey = true)]
public long Id { get; set; }
[SugarColumn(ColumnName = "test0_id")]
public long Test0Id { get; set; }
public string Title3 { get; set; }
}
public class UCustom02
{
public static void Init()
{
var db = NewUnitTest.Db; ;
//用来打印Sql方便你调式
db.Aop.OnLogExecuting = (sql, pars) =>
{
Console.WriteLine(sql + "\r\n" +
db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
Console.WriteLine();
};
db.Aop.OnError = (sql) =>
{
string ss = sql.Sql;
};
db.Aop.OnLogExecuting = (sql, p) =>
{
Console.WriteLine(sql);
};
db.Aop.OnLogExecuted = (sql, p) =>
{
Console.WriteLine(sql);
};
db.CodeFirst.InitTables(typeof(Test0), typeof(Test1), typeof(Test2), typeof(Test3));
db.Deleteable<Test0>().ExecuteCommand();
db.Deleteable<Test1>().ExecuteCommand();
db.Deleteable<Test2>().ExecuteCommand();
db.Deleteable<Test3>().ExecuteCommand();
db.Insertable(new Test0 { Id = 1, Title = "111111" }).ExecuteCommand();
db.Insertable(new Test1 { Id = 1, Test0Id = 1, Title1 = "111111" }).ExecuteCommand();
db.Insertable(new Test2 { Id = 1, Test0Id = 1, Title2 = "222222222" }).ExecuteCommand();
db.Insertable(new Test3 { Id = 1, Test0Id = 1, Title3 = "333333333" }).ExecuteCommand();
var cacheQueryT = db.Queryable<Test0>().Select(x => new
{
id = x.Id,
title = x.Title,
title1 =
SqlFunc.Subqueryable<Test1>().Where(w => w.Test0Id == x.Id &&
SqlFunc.Subqueryable<Test2>().Where(t => t.Test0Id == w.Test0Id &&
SqlFunc.Subqueryable<Test3>().Where(t1 => t1.Test0Id == w.Test0Id).Any()).Any()).Select(w => w.Title1)
}).ToList();
}
}
}

5
Src/Asp.Net/SqlSugar/ExpressionsToSql/Subquery/Items/SubWhere.cs

@ -40,10 +40,15 @@ namespace SqlSugar
public string GetValue(Expression expression)
{
var exp = expression as MethodCallExpression;
if (Regex.Matches( expression.ToString(), "Subqueryable").Count >= 2)
{
new SubSelect() { Context = this.Context }.SetShortName(exp, "+");
}
var argExp = exp.Arguments[0];
var result = "WHERE " + SubTools.GetMethodValue(Context, argExp, ResolveExpressType.WhereMultiple);
var regex = @"^WHERE (\@Const\d+) $";
if (this.Context is OracleExpressionContext)
{

6
Src/Asp.Net/SqlSugar/ExpressionsToSql/Subquery/SubResolve.cs

@ -148,8 +148,14 @@ namespace SqlSugar
private List<string> GetSubItems()
{
var isSubSubQuery = this.allMethods.Select(it => it.ToString()).Any(it => Regex.Matches(it, "Subquery").Count > 1);
var isubList = this.allMethods.Select(exp =>
{
if (isSubSubQuery)
{
this.context.JoinIndex = 1;
this.context.SubQueryIndex = 0;
}
var methodName = exp.Method.Name;
var items = SubTools.SubItems(this.context);
var item = items.First(s => s.Name == methodName);

Loading…
Cancel
Save