SqlSugar/Src/Asp.Net/SqlServerTest/UnitTest/Query/JoinQuery.cs

96 lines
3.4 KiB
C#
Raw Normal View History

2017-02-26 23:56:28 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
2017-02-27 01:03:56 +08:00
using SqlSugar;
using System.Linq.Expressions;
using OrmTest.Models;
2017-03-04 15:07:58 +08:00
namespace OrmTest.UnitTest
2017-02-26 23:56:28 +08:00
{
2017-05-16 13:55:57 +08:00
public class JoinQuery : UnitTestBase
2017-02-26 23:56:28 +08:00
{
2017-03-04 15:07:58 +08:00
private JoinQuery() { }
public JoinQuery(int eachCount)
2017-02-27 01:03:56 +08:00
{
this.Count = eachCount;
}
internal void Init()
{
base.Begin();
for (int i = 0; i < base.Count; i++)
{
2017-03-05 00:29:53 +08:00
Q1();
2017-02-27 01:03:56 +08:00
Q2();
2017-04-29 21:25:31 +08:00
Q3();
2017-07-06 01:27:05 +08:00
Q4();
2017-02-27 01:03:56 +08:00
}
base.End("Method Test");
}
2017-07-06 01:27:05 +08:00
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");
}
}
2017-02-27 01:03:56 +08:00
2017-04-29 21:25:31 +08:00
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();
2017-05-28 09:33:30 +08:00
string sql = @"SELECT st.* FROM [Student] st Left JOIN School sh ON sh.id=st.schoolid WHERE st.id>@id ";
2017-04-29 21:25:31 +08:00
base.Check(sql,new List<SugarParameter>() {new SugarParameter("@id",1)}, join3.Key, join3.Value, "join 3 Error");
}
}
2017-03-05 00:29:53 +08:00
public void Q1()
{
using (var db = GetInstance())
{
2017-04-29 21:17:57 +08:00
var join1 = db.Queryable<Student, School>((st, sc) => new object[] {
2017-03-05 00:29:53 +08:00
JoinType.Left,st.SchoolId==sc.Id
2017-04-29 21:17:57 +08:00
}).Where(st => st.Id > 0).Select<Student>("*").ToSql();
2017-06-04 23:13:30 +08:00
base.Check(@"SELECT * FROM [STudent] st Left JOIN School sc ON ( [st].[SchoolId] = [sc].[Id] ) WHERE ( [st].[ID] > @Id0 ) ",
2017-04-29 21:17:57 +08:00
new List<SugarParameter>() {
new SugarParameter("@Id0",0)
}, join1.Key, join1.Value, "join 1 Error");
2017-03-05 00:29:53 +08:00
}
}
2017-02-27 01:03:56 +08:00
public void Q2()
{
2017-03-04 00:54:19 +08:00
using (var db = GetInstance())
{
2017-04-29 21:17:57 +08:00
var join2 = db.Queryable<Student, School>((st, sc) => new object[] {
2017-03-04 00:54:19 +08:00
JoinType.Left,st.SchoolId==sc.Id
2017-04-29 21:25:31 +08:00
}).Where(st => st.Id > 2).Select<Student>("*").ToSql();
2017-06-04 23:13:30 +08:00
base.Check(@"SELECT * FROM [STudent] st Left JOIN School sc ON ( [st].[SchoolId] = [sc].[Id] ) WHERE ( [st].[ID] > @Id0 ) ",
2017-04-29 21:17:57 +08:00
new List<SugarParameter>() {
new SugarParameter("@Id0",2)
2017-06-04 23:13:30 +08:00
}, join2.Key, join2.Value, "join 2 Error");
2017-03-04 00:54:19 +08:00
}
}
2017-02-27 01:03:56 +08:00
2017-03-04 00:54:19 +08:00
public SqlSugarClient GetInstance()
{
2017-05-28 10:01:58 +08:00
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() { ConnectionString = Config.ConnectionString, DbType = DbType.SqlServer });
2017-05-19 11:20:07 +08:00
db.Ado.IsEnableLogEvent = true;
db.Ado.LogEventStarting = (sql, pars) =>
2017-03-05 00:29:53 +08:00
{
2017-04-29 21:25:31 +08:00
Console.WriteLine(sql + " " + pars);
2017-03-05 00:29:53 +08:00
};
2017-03-04 00:54:19 +08:00
return db;
2017-02-27 01:03:56 +08:00
}
2017-02-26 23:56:28 +08:00
}
}