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.

188 lines
8.0 KiB

7 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;
namespace OrmTest.UnitTest
{
public class Select : UnitTestBase
{
private Select() { }
public Select(int eachCount)
{
this.Count = eachCount;
}
internal void Init()
{
base.Begin();
for (int i = 0; i < base.Count; i++)
{
7 years ago
single();
7 years ago
single2();
single3();
single4();
single5();
Multiple();
Multiple2();
singleDynamic();
MultipleDynamic();
}
base.End("Select Test");
}
private void Multiple()
{
Expression<Func<Student, School, object>> exp = (it, school) => new Student() { Name = "a", Id = it.Id, SchoolId = school.Id, TestId = it.Id + 1 };
7 years ago
OracleExpressionContext expContext = new OracleExpressionContext();
7 years ago
expContext.IsSingle = false;
expContext.Resolve(exp, ResolveExpressType.SelectMultiple);
var selectorValue = expContext.Result.GetString();
var pars = expContext.Parameters;
base.Check(
selectorValue,
pars,
7 years ago
@" :CONSTANT0 AS ""NAME"" , ""IT"".""ID"" AS ""ID"" , ""SCHOOL"".""ID"" AS ""SCHOOLID"" , ( ""IT"".""ID"" + :ID1 ) AS ""TESTID"" ",
7 years ago
new List<SugarParameter>(){
7 years ago
new SugarParameter(":constant0","a"),
new SugarParameter(":Id1",1)
7 years ago
},
"Select.Multiple Error");
}
private void Multiple2()
{
7 years ago
Expression<Func<Student, School, object>> exp = (it, school) => new ViewModelStudent3() { SchoolName = school.Name, Id = SqlFunc.GetSelfAndAutoFill(it.Id) };
7 years ago
OracleExpressionContext expContext = new OracleExpressionContext();
7 years ago
expContext.IsSingle = false;
expContext.Resolve(exp, ResolveExpressType.SelectMultiple);
var selectorValue = expContext.Result.GetString();
var pars = expContext.Parameters;
base.Check(
selectorValue,
pars,
7 years ago
@" ""SCHOOL"".""NAME"" AS ""SCHOOLNAME"" ,it.*",
7 years ago
new List<SugarParameter>()
{
7 years ago
},
"Select.Multiple Error");
}
private void MultipleDynamic()
{
Expression<Func<Student, School, object>> exp = (it, school) => new { Name = "a", Id = it.Id / 2, SchoolId = school.Id };
7 years ago
OracleExpressionContext expContext = new OracleExpressionContext();
7 years ago
expContext.IsSingle = false;
expContext.Resolve(exp, ResolveExpressType.SelectMultiple);
var selectorValue = expContext.Result.GetString();
var pars = expContext.Parameters;
base.Check(
selectorValue,
pars,
7 years ago
@" :CONSTANT0 AS ""NAME"" , ( ""IT"".""ID"" / :ID1 ) AS ""ID"" , ""SCHOOL"".""ID"" AS ""SCHOOLID"" ",
7 years ago
new List<SugarParameter>(){
7 years ago
new SugarParameter(":constant0","a"),
new SugarParameter(":Id1", 2)},
7 years ago
"Select.MultipleDynamic Error");
}
7 years ago
private void single()
7 years ago
{
int p = 1;
7 years ago
Expression<Func<Student, object>> exp = it => new Student() { Name = "a", Id = it.Id, SchoolId = p, TestId = it.Id + 11 };
7 years ago
OracleExpressionContext expContext = new OracleExpressionContext();
7 years ago
expContext.Resolve(exp, ResolveExpressType.SelectSingle);
var selectorValue = expContext.Result.GetString();
var pars = expContext.Parameters;
base.Check(
selectorValue,
pars,
7 years ago
@" :CONSTANT0 AS ""NAME"" , ""ID"" AS ""ID"" , :CONSTANT1 AS ""SCHOOLID"" , ( ""ID"" + :ID2 ) AS ""TESTID"" ",
7 years ago
new List<SugarParameter>(){
7 years ago
new SugarParameter(":constant0","a"),
new SugarParameter(":constant1",1),
new SugarParameter(":Id2",11 ) },
7 years ago
"Select.single Error");
}
7 years ago
private void single2(int p = 1)
7 years ago
{
Expression<Func<Student, object>> exp = it => new Student() { Name = "a", Id = it.Id, SchoolId = p, TestId = it.Id + 11 };
7 years ago
OracleExpressionContext expContext = new OracleExpressionContext();
7 years ago
expContext.Resolve(exp, ResolveExpressType.SelectSingle);
var selectorValue = expContext.Result.GetString();
var pars = expContext.Parameters;
base.Check(
selectorValue,
pars,
7 years ago
@" :CONSTANT0 AS ""NAME"" , ""ID"" AS ""ID"" , :CONSTANT1 AS ""SCHOOLID"" , ( ""ID"" + :ID2 ) AS ""TESTID"" ",
7 years ago
new List<SugarParameter>(){
7 years ago
new SugarParameter(":constant0","a"),
new SugarParameter(":constant1",1),
new SugarParameter(":Id2",11 ) },
7 years ago
"Select.single Error");
}
private void single3(int p = 1)
{
7 years ago
Expression<Func<Student, object>> exp = it => new DataTestInfo() { Datetime1 = DateTime.Now, String = it.Name };
7 years ago
OracleExpressionContext expContext = new OracleExpressionContext();
7 years ago
expContext.Resolve(exp, ResolveExpressType.SelectSingle);
var selectorValue = expContext.Result.GetString();
var pars = expContext.Parameters;
base.Check(
7 years ago
@" :CONSTANT0 AS ""DATETIME1"" , ""NAME"" AS ""STRING"" ", null, selectorValue, null,
7 years ago
"Select.single3 Error");
}
private void single4(int p = 1)
{
Expression<Func<Student, object>> exp = it => it.CreateTime.HasValue;
7 years ago
OracleExpressionContext expContext = new OracleExpressionContext();
7 years ago
expContext.IsSingle = false;
expContext.Resolve(exp, ResolveExpressType.WhereMultiple);
var selectorValue = expContext.Result.GetString();
var pars = expContext.Parameters;
base.Check(
7 years ago
@"( ""IT"".""CREATETIME""<>'' AND ""IT"".""CREATETIME"" IS NOT NULL )", null, selectorValue, null,
7 years ago
"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
OracleExpressionContext expContext = new OracleExpressionContext();
7 years ago
expContext.IsSingle = false;
expContext.Resolve(exp, ResolveExpressType.WhereMultiple);
var selectorValue = expContext.Result.GetString();
var pars = expContext.Parameters;
base.Check(
7 years ago
@"( :constant0<>'' AND :constant0 IS NOT NULL )", new List<SugarParameter>() {
7 years ago
new SugarParameter(":constant0",p)
7 years ago
}, selectorValue, pars,
"Select.single4 Error");
}
7 years ago
private void singleDynamic()
7 years ago
{
string a = "a";
7 years ago
Expression<Func<Student, object>> exp = it => new { x = it.Id, shoolid = 1, name = a, p = it.Id * 2 };
7 years ago
OracleExpressionContext expContext = new OracleExpressionContext();
7 years ago
expContext.Resolve(exp, ResolveExpressType.SelectSingle);
var selectorValue = expContext.Result.GetString();
var pars = expContext.Parameters;
base.Check(
selectorValue,
pars,
7 years ago
@" ""ID"" AS ""X"" , :CONSTANT0 AS ""SHOOLID"" , :CONSTANT1 AS ""NAME"" , ( ""ID"" * :ID2 ) AS ""P"" ",
7 years ago
new List<SugarParameter>(){
7 years ago
new SugarParameter(":constant0",1),
new SugarParameter(":constant1","a"),
new SugarParameter(":Id2",2)},
7 years ago
"Select.singleDynamic Error");
}
}
}