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.

113 lines
5.5 KiB

8 years ago
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SqlSugar;
using System.Linq.Expressions;
using OrmTest.Models;
8 years ago
namespace OrmTest.UnitTest
8 years ago
{
8 years ago
public class SingleQuery : UnitTestBase
8 years ago
{
8 years ago
private SingleQuery() { }
public SingleQuery(int eachCount)
8 years ago
{
this.Count = eachCount;
}
internal void Init()
{
base.Begin();
for (int i = 0; i < base.Count; i++)
{
Q2();
}
base.End("Method Test");
}
public void Q2()
{
using (var db = GetInstance())
{
8 years ago
var t1 = db.Queryable<Student>().ToSql();
base.Check("SELECT [ID],[SchoolId],[Name],[CreateTime] FROM [STudent]", null, t1.Key, null, "single t1 Error");
8 years ago
8 years ago
var t2 = db.Queryable<Student>().With(SqlWith.NoLock).ToSql();
base.Check("SELECT [ID],[SchoolId],[Name],[CreateTime] FROM [STudent] WITH(NOLOCK)", null, t2.Key, null, "single t2 Error");
8 years ago
8 years ago
var t3 = db.Queryable<Student>().OrderBy(it=>it.Id).ToSql();
base.Check("SELECT [ID],[SchoolId],[Name],[CreateTime] FROM [STudent] ORDER BY [ID] ASC", null, t3.Key, null, "single t3 Error");
8 years ago
8 years ago
var t4 = db.Queryable<Student>().OrderBy(it => it.Id).Take(3).ToSql();
base.Check(@"SELECT * FROM (SELECT [ID],[SchoolId],[Name],[CreateTime],ROW_NUMBER() OVER(ORDER BY [ID] ASC) AS RowIndex FROM [STudent] ) T WHERE RowIndex BETWEEN 1 AND 3", null, t4.Key, null, "single t4 Error");
8 years ago
8 years ago
var t5 = db.Queryable<Student>().OrderBy(it => it.Id).Skip(3).ToSql();
base.Check(@"SELECT * FROM (SELECT [ID],[SchoolId],[Name],[CreateTime],ROW_NUMBER() OVER(ORDER BY [ID] ASC) AS RowIndex FROM [STudent] ) T WHERE RowIndex BETWEEN 4 AND 9223372036854775807", null, t5.Key,null, "single t5 Error");
8 years ago
int pageIndex = 2;
int pageSize = 10;
8 years ago
var t6 = db.Queryable<Student>().OrderBy(it => it.Id,OrderByType.Desc).Skip((pageIndex-1)*pageSize).Take(pageSize).ToSql();
base.Check(@"SELECT * FROM (SELECT [ID],[SchoolId],[Name],[CreateTime],ROW_NUMBER() OVER(ORDER BY [ID] DESC) AS RowIndex FROM [STudent] ) T WHERE RowIndex BETWEEN 11 AND 20", null, t6.Key, null, "single t6 Error");
8 years ago
8 years ago
int studentCount=db.Ado.GetInt("select count(1) from Student");
8 years ago
var countIsSuccess=db.Queryable<Student>().Count()== studentCount;
if (!countIsSuccess) {
8 years ago
throw new Exception(" single countIsSuccess Error");
8 years ago
}
8 years ago
var t7 = db.Queryable<Student>().OrderBy(it => it.Id, OrderByType.Desc).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToPageList(pageIndex,pageSize,ref studentCount);
8 years ago
countIsSuccess = studentCount == db.Queryable<Student>().OrderBy(it => it.Id, OrderByType.Desc).Skip((pageIndex - 1) * pageSize).Take(pageSize * pageIndex).Count();
if (!countIsSuccess)
{
8 years ago
throw new Exception("single t7 Error");
8 years ago
}
8 years ago
int studentMin = db.Ado.GetInt("select min(id) from Student");
8 years ago
var minIsSuccess = db.Queryable<Student>().Min(it=>it.Id) == studentMin;
if (!minIsSuccess)
{
8 years ago
throw new Exception("single minIsSuccess Error");
8 years ago
}
8 years ago
int studentMax = db.Ado.GetInt("select max(id) from Student");
8 years ago
var maxIsSuccess = db.Queryable<Student>().Max(it => it.Id) == studentMax;
if (!maxIsSuccess)
{
8 years ago
throw new Exception("single maxIsSuccess Error");
8 years ago
}
8 years ago
int studentAvg = db.Ado.GetInt("select avg(id) from Student");
8 years ago
var avgIsSuccess = db.Queryable<Student>().Avg(it => it.Id) == studentAvg;
if (!maxIsSuccess)
{
8 years ago
throw new Exception(" single avgIsSuccess Error");
8 years ago
}
8 years ago
int studentSum = db.Ado.GetInt("select sum(id) from Student");
8 years ago
var sumIsSuccess = db.Queryable<Student>().Sum(it => it.Id) == studentSum;
if (!sumIsSuccess)
{
8 years ago
throw new Exception("single sumIsSuccess Error");
8 years ago
}
8 years ago
8 years ago
var t8 = db.Queryable<Student>()
8 years ago
.Where(it=>it.Id==1)
8 years ago
.WhereIF(true,it=> SqlFunc.Contains(it.Name,"a"))
8 years ago
.OrderBy(it => it.Id, OrderByType.Desc).Skip((pageIndex - 1) * pageSize).Take(pageSize ).With(SqlWith.NoLock).ToSql();
base.Check(@"SELECT * FROM (SELECT [ID],[SchoolId],[Name],[CreateTime],ROW_NUMBER() OVER(ORDER BY [ID] DESC) AS RowIndex FROM [STudent] WITH(NOLOCK) WHERE ( [ID] = @Id0 ) AND ([Name] like '%'+@MethodConst1+'%') ) T WHERE RowIndex BETWEEN 11 AND 20", new List<SugarParameter>() {
8 years ago
new SugarParameter("@Id0",1),new SugarParameter("@MethodConst1","a")
8 years ago
}, t8.Key, t8.Value,"single t8 Error");
8 years ago
var t9 = db.Queryable<Student>()
.In(1)
.Select(it => new { it.Id, it.Name,x=it.Id }).ToSql();
base.Check("SELECT [ID] AS [Id] , [Name] AS [Name] , [ID] AS [x] FROM [STudent] WHERE [Id] IN (@InPara0) ", new List<SugarParameter>() {
new SugarParameter("@InPara0",1) },t9.Key,t9.Value, "single t9 error");
8 years ago
}
}
}
}