SqlSugar源码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

243 lines
10 KiB

8 years ago
using OrmTest.Models;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
8 years ago
namespace OrmTest.UnitTest
8 years ago
{
8 years ago
public class Select : UnitTestBase
8 years ago
{
8 years ago
private Select() { }
public Select(int eachCount)
8 years ago
{
8 years ago
this.Count = eachCount;
}
internal void Init()
{
base.Begin();
for (int i = 0; i < base.Count; i++)
8 years ago
{
single();
7 years ago
single2();
single3();
7 years ago
single4();
single5();
7 years ago
single6();
7 years ago
single7();
single8();
8 years ago
Multiple();
Multiple2();
8 years ago
singleDynamic();
8 years ago
MultipleDynamic();
8 years ago
}
8 years ago
base.End("Select Test");
8 years ago
}
7 years ago
private void single7()
{
Expression<Func<DataTestInfo2, DataTestInfo2>> exp =it => new DataTestInfo2() { Bool1=it.Bool1 , Bool2=it.Bool2 };
ExpressionContext expContext = new ExpressionContext();
expContext.IsSingle = false;
expContext.Resolve(exp, ResolveExpressType.SelectSingle);
var selectorValue = expContext.Result.GetString();
var pars = expContext.Parameters;
base.Check(
selectorValue,
pars,
@"[Bool1] AS [Bool1] , [Bool2] AS [Bool2] ",
new List<SugarParameter>()
{
},
"Select.single7 Error");
}
private void single8()
{
Expression<Func<DataTestInfo2, object>> exp = it => new { Bool1 = it.Bool1, Bool2 = it.Bool2 };
ExpressionContext expContext = new ExpressionContext();
expContext.IsSingle = false;
expContext.Resolve(exp, ResolveExpressType.SelectSingle);
var selectorValue = expContext.Result.GetString();
var pars = expContext.Parameters;
base.Check(
selectorValue,
pars,
@"[Bool1] AS [Bool1] , [Bool2] AS [Bool2] ",
new List<SugarParameter>()
{
},
"Select.single8 Error");
}
8 years ago
private void Multiple()
8 years ago
{
8 years ago
Expression<Func<Student, School, object>> exp = (it, school) => new Student() { Name = "a", Id = it.Id, SchoolId = school.Id, TestId = it.Id + 1 };
8 years ago
ExpressionContext expContext = new ExpressionContext();
expContext.IsSingle = false;
8 years ago
expContext.Resolve(exp, ResolveExpressType.SelectMultiple);
8 years ago
var selectorValue = expContext.Result.GetString();
var pars = expContext.Parameters;
8 years ago
base.Check(
8 years ago
selectorValue,
pars,
@" @constant0 AS [Name] , [it].[Id] AS [Id] , [school].[Id] AS [SchoolId] , ( [it].[Id] + @Id1 ) AS [TestId] ",
8 years ago
new List<SugarParameter>(){
new SugarParameter("@constant0","a"),
new SugarParameter("@Id1",1)
},
8 years ago
"Select.Multiple Error");
8 years ago
}
private void Multiple2()
{
Expression<Func<Student, School, object>> exp = (it, school) => new ViewModelStudent3() { SchoolName=school.Name,Id=SqlFunc.GetSelfAndAutoFill(it.Id) };
ExpressionContext expContext = new ExpressionContext();
expContext.IsSingle = false;
expContext.Resolve(exp, ResolveExpressType.SelectMultiple);
var selectorValue = expContext.Result.GetString();
var pars = expContext.Parameters;
base.Check(
selectorValue,
pars,
@" [school].[Name] AS [SchoolName] ,it.*",
new List<SugarParameter>(){
},
"Select.Multiple Error");
}
8 years ago
private void MultipleDynamic()
8 years ago
{
8 years ago
Expression<Func<Student, School, object>> exp = (it, school) => new { Name = "a", Id = it.Id / 2, SchoolId = school.Id };
8 years ago
ExpressionContext expContext = new ExpressionContext();
expContext.IsSingle = false;
8 years ago
expContext.Resolve(exp, ResolveExpressType.SelectMultiple);
8 years ago
var selectorValue = expContext.Result.GetString();
var pars = expContext.Parameters;
8 years ago
base.Check(
8 years ago
selectorValue,
pars,
@" @constant0 AS [Name] , ( [it].[Id] / @Id1 ) AS [Id] , [school].[Id] AS [SchoolId] ",
8 years ago
new List<SugarParameter>(){
new SugarParameter("@constant0","a"),
new SugarParameter("@Id1", 2)},
8 years ago
"Select.MultipleDynamic Error");
8 years ago
}
8 years ago
private void single()
8 years ago
{
8 years ago
int p = 1;
Expression<Func<Student, object>> exp = it => new Student() { Name = "a", Id = it.Id, SchoolId = p,TestId=it.Id+11 };
8 years ago
ExpressionContext expContext = new ExpressionContext();
expContext.Resolve(exp, ResolveExpressType.SelectSingle);
8 years ago
var selectorValue = expContext.Result.GetString();
8 years ago
var pars = expContext.Parameters;
8 years ago
base.Check(
8 years ago
selectorValue,
pars,
@" @constant0 AS [Name] , [Id] AS [Id] , @constant1 AS [SchoolId] , ( [Id] + @Id2 ) AS [TestId] ",
8 years ago
new List<SugarParameter>(){
new SugarParameter("@constant0","a"),
new SugarParameter("@constant1",1),
new SugarParameter("@Id2",11 ) },
7 years ago
"Select.single Error");
}
private void single2(int p=1)
{
Expression<Func<Student, object>> exp = it => new Student() { Name = "a", Id = it.Id, SchoolId = p, TestId = it.Id + 11 };
ExpressionContext expContext = new ExpressionContext();
expContext.Resolve(exp, ResolveExpressType.SelectSingle);
var selectorValue = expContext.Result.GetString();
var pars = expContext.Parameters;
base.Check(
selectorValue,
pars,
@" @constant0 AS [Name] , [Id] AS [Id] , @constant1 AS [SchoolId] , ( [Id] + @Id2 ) AS [TestId] ",
new List<SugarParameter>(){
new SugarParameter("@constant0","a"),
new SugarParameter("@constant1",1),
new SugarParameter("@Id2",11 ) },
8 years ago
"Select.single Error");
8 years ago
}
private void single3(int p = 1)
{
Expression<Func<Student, object>> exp = it => new DataTestInfo() { Datetime1=DateTime.Now, String=it.Name};
ExpressionContext expContext = new ExpressionContext();
expContext.Resolve(exp, ResolveExpressType.SelectSingle);
var selectorValue = expContext.Result.GetString();
var pars = expContext.Parameters;
base.Check(
@" @constant0 AS [Datetime1] , [Name] AS [String] ", null,selectorValue,null,
"Select.single3 Error");
}
8 years ago
7 years ago
private void single4(int p = 1)
{
Expression<Func<Student, object>> exp = it => it.CreateTime.HasValue;
SqlServerExpressionContext expContext = new SqlServerExpressionContext();
expContext.IsSingle = false;
expContext.Resolve(exp, ResolveExpressType.WhereMultiple);
var selectorValue = expContext.Result.GetString();
var pars = expContext.Parameters;
base.Check(
@"( [it].[CreateTime]<>'' AND [it].[CreateTime] IS NOT NULL )", null, selectorValue, null,
"Select.single4 Error");
}
private void single5()
7 years ago
{
var p =(DateTime?) DateTime.Now;
7 years ago
Expression<Func<Student, object>> exp = it => p.HasValue;
7 years ago
SqlServerExpressionContext expContext = new SqlServerExpressionContext();
expContext.IsSingle = false;
expContext.Resolve(exp, ResolveExpressType.WhereMultiple);
var selectorValue = expContext.Result.GetString();
var pars = expContext.Parameters;
base.Check(
@"( @constant0<>'' AND @constant0 IS NOT NULL )", new List<SugarParameter>() {
new SugarParameter("@constant0",p)
}, selectorValue, pars,
7 years ago
"Select.single5 Error");
}
private void single6()
{
var p = (DateTime?)DateTime.Now;
Expression<Func<Student, object>> exp = it => p.Value;
SqlServerExpressionContext expContext = new SqlServerExpressionContext();
expContext.IsSingle = false;
expContext.Resolve(exp, ResolveExpressType.FieldSingle);
var selectorValue = expContext.Result.GetString();
var pars = expContext.Parameters;
base.Check(
@" @Const0 ", new List<SugarParameter>() {
new SugarParameter("@Const0",p)
}, selectorValue, pars,
"Select.single6 Error");
7 years ago
}
8 years ago
private void singleDynamic()
8 years ago
{
8 years ago
string a = "a";
Expression<Func<Student, object>> exp = it => new { x = it.Id, shoolid = 1, name = a,p=it.Id*2 };
8 years ago
ExpressionContext expContext = new ExpressionContext();
expContext.Resolve(exp, ResolveExpressType.SelectSingle);
8 years ago
var selectorValue = expContext.Result.GetString();
8 years ago
var pars = expContext.Parameters;
8 years ago
base.Check(
8 years ago
selectorValue,
pars,
@" [Id] AS [x] , @constant0 AS [shoolid] , @constant1 AS [name] , ( [Id] * @Id2 ) AS [p] ",
8 years ago
new List<SugarParameter>(){
new SugarParameter("@constant0",1),
new SugarParameter("@constant1","a"),
new SugarParameter("@Id2",2)},
"Select.singleDynamic Error");
8 years ago
}
}
}