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.

141 lines
5.4 KiB

8 years ago
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
8 years ago
using SqlSugar;
using System.Linq.Expressions;
using OrmTest.Models;
8 years ago
namespace OrmTest.UnitTest
8 years ago
{
8 years ago
public class JoinQuery : UnitTestBase
8 years ago
{
8 years ago
private JoinQuery() { }
public JoinQuery(int eachCount)
8 years ago
{
this.Count = eachCount;
}
internal void Init()
{
base.Begin();
for (int i = 0; i < base.Count; i++)
{
8 years ago
Q1();
8 years ago
Q2();
8 years ago
Q3();
Q4();
7 years ago
q5();
q6();
q7();
8 years ago
}
base.End("Method Test");
}
7 years ago
private void q6()
{
using (var db = GetInstance())
{
var join6 = db.Queryable<Student, School>((st, sc) => new object[] {
JoinType.Left,st.SchoolId==sc.Id
}).Select((st, sc) => new ViewModelStudent { Name = st.Name, SchoolId = SqlFunc.AggregateMin(sc.Id) }).ToSql();
string sql = @"SELECT [st].[Name] AS [Name] , MIN([sc].[Id]) AS [SchoolId] FROM [STudent] st Left JOIN School sc ON ( [st].[SchoolId] = [sc].[Id] ) ";
base.Check(sql, null, join6.Key, null, "join 6 Error");
}
}
private void q7()
{
using (var db = GetInstance())
{
var join7 = db.Queryable<Student, School>((st, sc) => new object[] {
JoinType.Left,st.SchoolId==sc.Id
}).Select((st, sc) => new ViewModelStudent { Name = st.Name, SchoolId = SqlFunc.AggregateMin(sc.Id*1) }).ToSql();
string sql = @"SELECT [st].[Name] AS [Name] , MIN(( [sc].[Id] * @Id0 )) AS [SchoolId] FROM [STudent] st Left JOIN School sc ON ( [st].[SchoolId] = [sc].[Id] ) ";
base.Check(sql, new List<SugarParameter>() {
new SugarParameter("@Id0",1)
}, join7.Key, join7.Value, "join 7 Error");
}
}
7 years ago
private void q5()
{
using (var db = GetInstance())
{
db.MappingTables.Add("School", "SchoolTable");
var join5= db.Queryable<Student, School>((st, sc) => st.SchoolId == sc.Id).Select(st => st)
.GroupBy(st=> new{ st.Id,st.Name })
.ToSql();
string sql = @"SELECT st.* FROM [STudent] st ,[SchoolTable] sc WHERE ( [st].[SchoolId] = [sc].[Id] )GROUP BY [st].[ID],[st].[Name] ";
base.Check(sql, null, join5.Key, null, "join 5 Error");
}
}
private void Q4()
{
using (var db = GetInstance())
{
db.MappingTables.Add("School", "SchoolTable");
var join4 = db.Queryable<Student, School>((st, sc) => st.SchoolId == sc.Id).Select(st=>st).ToSql();
string sql = @"SELECT st.* FROM [STudent] st ,[SchoolTable] sc WHERE ( [st].[SchoolId] = [sc].[Id] ) ";
base.Check(sql, null, join4.Key, null, "join 4 Error");
}
}
8 years ago
8 years ago
private void Q3()
{
using (var db = GetInstance())
{
var join3 = db.Queryable("Student", "st")
.AddJoinInfo("School", "sh", "sh.id=st.schoolid")
.Where("st.id>@id")
.AddParameters(new { id = 1 })
.Select("st.*").ToSql();
8 years ago
string sql = @"SELECT st.* FROM [Student] st Left JOIN School sh ON sh.id=st.schoolid WHERE st.id>@id ";
8 years ago
base.Check(sql,new List<SugarParameter>() {new SugarParameter("@id",1)}, join3.Key, join3.Value, "join 3 Error");
}
}
8 years ago
public void Q1()
{
using (var db = GetInstance())
{
8 years ago
var join1 = db.Queryable<Student, School>((st, sc) => new object[] {
8 years ago
JoinType.Left,st.SchoolId==sc.Id
8 years ago
}).Where(st => st.Id > 0).Select<Student>("*").ToSql();
8 years ago
base.Check(@"SELECT * FROM [STudent] st Left JOIN School sc ON ( [st].[SchoolId] = [sc].[Id] ) WHERE ( [st].[ID] > @Id0 ) ",
8 years ago
new List<SugarParameter>() {
new SugarParameter("@Id0",0)
}, join1.Key, join1.Value, "join 1 Error");
8 years ago
}
}
8 years ago
public void Q2()
{
8 years ago
using (var db = GetInstance())
{
8 years ago
var join2 = db.Queryable<Student, School>((st, sc) => new object[] {
8 years ago
JoinType.Left,st.SchoolId==sc.Id
8 years ago
}).Where(st => st.Id > 2).Select<Student>("*").ToSql();
8 years ago
base.Check(@"SELECT * FROM [STudent] st Left JOIN School sc ON ( [st].[SchoolId] = [sc].[Id] ) WHERE ( [st].[ID] > @Id0 ) ",
8 years ago
new List<SugarParameter>() {
new SugarParameter("@Id0",2)
8 years ago
}, join2.Key, join2.Value, "join 2 Error");
8 years ago
}
}
8 years ago
8 years ago
7 years ago
public new SqlSugarClient GetInstance()
8 years ago
{
8 years ago
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() { ConnectionString = Config.ConnectionString, DbType = DbType.SqlServer });
8 years ago
db.Ado.IsEnableLogEvent = true;
db.Ado.LogEventStarting = (sql, pars) =>
8 years ago
{
8 years ago
Console.WriteLine(sql + " " + pars);
8 years ago
};
8 years ago
return db;
8 years ago
}
8 years ago
}
}