2017-03-04 01:22:06 +08:00
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 ;
2017-03-04 15:07:58 +08:00
namespace OrmTest.UnitTest
2017-03-04 01:22:06 +08:00
{
2017-05-16 13:55:57 +08:00
public class SelectQuery : UnitTestBase
2017-03-04 01:22:06 +08:00
{
2017-03-05 00:29:53 +08:00
private SelectQuery ( ) { }
public SelectQuery ( int eachCount )
2017-03-04 01:22:06 +08:00
{
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 ( ) )
{
2017-04-09 17:03:07 +08:00
//db.Database.IsEnableLogEvent = true;
2017-05-19 11:20:07 +08:00
db . Ado . LogEventStarting = ( sql , pars ) = >
2017-03-11 09:51:58 +08:00
{
2017-03-16 10:42:31 +08:00
Console . WriteLine ( sql + " " + pars ) ;
2017-03-11 09:51:58 +08:00
} ;
2017-03-16 10:42:31 +08:00
2017-04-09 14:39:08 +08:00
#region dr ot entity
2017-05-01 14:23:13 +08:00
db . IgnoreColumns . Add ( "TestId" , "Student" ) ;
2017-04-30 16:56:41 +08:00
var s1 = db . Queryable < Student > ( ) . Select ( it = > new ViewModelStudent2 { Name = it . Name , Student = it } ) . ToList ( ) ;
var s2 = db . Queryable < Student > ( ) . Select ( it = > new { id = it . Id , w = new { x = it } } ) . ToList ( ) ;
2017-04-29 21:25:31 +08:00
var s3 = db . Queryable < Student > ( ) . Select ( it = > new { newid = it . Id } ) . ToList ( ) ;
var s4 = db . Queryable < Student > ( ) . Select ( it = > new { newid = it . Id , obj = it } ) . ToList ( ) ;
2017-04-30 16:56:41 +08:00
var s5 = db . Queryable < Student > ( ) . Select ( it = > new ViewModelStudent2 { Student = it , Name = it . Name } ) . ToList ( ) ;
2017-04-09 14:39:08 +08:00
#endregion
#region sql and parameters validate
2017-05-26 14:11:04 +08:00
var t1 = db . Queryable < Student , School > ( ( st , sc ) = > new object [ ] {
2017-04-30 16:11:51 +08:00
JoinType . Inner , st . Id = = sc . Id
2017-05-31 15:42:21 +08:00
} ) . GroupBy ( st = > st . Id ) . Having ( st = > SqlFunc . AggregateAvg ( st . Id ) = = 1 ) . Select ( st = > new { avgId = SqlFunc . AggregateAvg ( st . Id ) } ) . ToSql ( ) ;
2017-08-05 13:15:59 +08:00
base . Check ( "SELECT AVG([st].[ID]) AS [avgId] FROM [STudent] st Inner JOIN [School] sc ON ( [st].[ID] = [sc].[Id] ) GROUP BY [st].[ID] HAVING (AVG([st].[ID]) = @Const0 ) " ,
2017-04-30 16:56:41 +08:00
new List < SugarParameter > ( ) {
new SugarParameter ( "@Const0" , 1 )
}
,
2017-05-26 14:11:04 +08:00
t1 . Key , t1 . Value , " select t1 Error" ) ;
2017-04-30 16:11:51 +08:00
2017-05-26 14:13:22 +08:00
var t2 = db . Queryable < School , School > ( ( st , st2 ) = > new object [ ] {
2017-03-26 15:18:17 +08:00
JoinType . Left , st . Id = = st2 . Id
} )
2017-04-09 14:39:08 +08:00
. Where ( st = > st . Id > 0 )
2017-05-26 01:23:54 +08:00
. Select ( ( st , st2 ) = > new { stid = st . Id , scId = st2 . Id , xx = st } ) . ToSql ( ) ;
2017-04-09 07:30:15 +08:00
2017-08-05 13:15:59 +08:00
base . Check ( "SELECT [st].[Id] AS [stid] , [st2].[Id] AS [scId] , [st].[Id] AS [School.Id] , [st].[Name] AS [School.Name] FROM [School] st Left JOIN [School] st2 ON ( [st].[Id] = [st2].[Id] ) WHERE ( [st].[Id] > @Id0 ) "
2017-04-09 07:30:15 +08:00
, new List < SugarParameter > ( ) {
new SugarParameter ( "@Id0" , 0 )
2017-05-26 14:13:22 +08:00
} , t2 . Key , t2 . Value , "select t2 Error" ) ;
2017-03-08 14:01:41 +08:00
2017-03-08 16:46:34 +08:00
2017-05-26 14:13:22 +08:00
var t3 = db . Queryable < Student , School , School > ( ( st , sc , sc2 ) = > new object [ ] {
2017-03-08 16:46:34 +08:00
JoinType . Left , st . SchoolId = = sc . Id ,
JoinType . Left , sc2 . Id = = sc . Id
2017-03-11 10:16:05 +08:00
} ) . Where ( st = > st . Id > 0 )
2017-04-29 21:33:38 +08:00
. Select < School > ( ( st ) = > new School ( ) { Id = st . Id } ) . ToSql ( ) ;
2017-08-05 13:15:59 +08:00
base . Check ( "SELECT [st].[ID] AS [Id] FROM [STudent] st Left JOIN [School] sc ON ( [st].[SchoolId] = [sc].[Id] ) Left JOIN [School] sc2 ON ( [sc2].[Id] = [sc].[Id] ) WHERE ( [st].[ID] > @Id0 ) " ,
2017-04-29 21:33:38 +08:00
new List < SugarParameter > ( ) {
new SugarParameter ( "@Id0" , 0 )
2017-05-26 14:13:22 +08:00
} , t3 . Key , t3 . Value , "select t3 Error" ) ;
2017-08-09 12:59:04 +08:00
db . Ado . IsEnableLogEvent = true ;
db . Ado . LogEventStarting = ( sql , pars ) = >
{
base . Check ( " SELECT COUNT(1) FROM (SELECT [st].[ID] FROM [STudent] st Left JOIN [School] sc ON ( [st].[SchoolId] = [sc].[Id] ) Left JOIN [School] sc2 ON ( [sc2].[Id] = [sc].[Id] ) GROUP BY [st].[ID] ) CountTable " ,
null , sql , null , "select t4 Error" ) ;
} ;
var t4 = db . Queryable < Student , School , School > ( ( st , sc , sc2 ) = > new object [ ] {
JoinType . Left , st . SchoolId = = sc . Id ,
JoinType . Left , sc2 . Id = = sc . Id
2017-11-13 12:50:55 +08:00
} ) . GroupBy ( st = > st . Id ) . Select ( st = > st . Id ) . Count ( ) ;
2017-11-13 12:09:39 +08:00
DateTime ? result = DateTime . Now ;
var t5 = db . Queryable < Student > ( ) . Where ( it = > it . CreateTime > result . Value . Date ) . ToSql ( ) ;
base . Check ( "SELECT [ID],[SchoolId],[Name],[CreateTime] FROM [STudent] WHERE ( [CreateTime] > @Const0 )" ,
new List < SugarParameter > ( ) {
new SugarParameter ( "@Const0" , result . Value . Date )
} , t5 . Key , t5 . Value , "select t5 Error" ) ;
2017-11-13 12:50:55 +08:00
db . Ado . IsEnableLogEvent = false ;
var t6 = db . Queryable < DataTestInfo2 > ( ) . Where ( it = > SqlFunc . HasValue ( it . Bool2 ) = = false ) . ToSql ( ) ;
base . Check ( "SELECT [PK],[Bool1],[Bool2],[Text1] FROM [DataTestInfo2] WHERE (( CASE WHEN ( [Bool2]<>'' AND [Bool2] IS NOT NULL ) THEN 1 ELSE 0 END ) = @Const0 )" ,
new List < SugarParameter > ( ) {
new SugarParameter ( "@Const0" , false )
} , t6 . Key , t6 . Value , "select t6 Error" ) ;
2018-01-08 13:04:10 +08:00
var t7 = db . Queryable < Student > ( ) . Select ( it = > new DataTestInfo2 ( ) {
Bool1 = SqlFunc . IIF ( SqlFunc . Subqueryable < Student > ( ) . Where ( x = > x . Id
= = it . Id ) . Any ( ) , true , false )
} ) . ToSql ( ) ;
base . Check ( "SELECT ( CASE WHEN (EXISTS ( SELECT * FROM [STudent] WHERE ( [ID] = [it].[ID] ) )) THEN @MethodConst0 ELSE @MethodConst1 END ) AS [Bool1] FROM [STudent] it " ,
new List < SugarParameter > ( ) {
new SugarParameter ( "@MethodConst0" , true ) ,
new SugarParameter ( "@MethodConst1" , false )
} , t7 . Key , t7 . Value , "select t7 Error" ) ;
2017-04-09 14:39:08 +08:00
#endregion
2018-11-24 00:40:27 +08:00
try
{
var t8 = db . Queryable < Student , School , School > ( ( st , sc , sc2 ) = > new object [ ] {
JoinType . Left , st . SchoolId = = sc . Id ,
JoinType . Left , sc2 . Id = = sc . Id
} ) . Where ( st = > st . Id > 0 )
. Select < School > ( ( st1 ) = > new School ( ) { Id = st1 . Id } ) . ToList ( ) ;
}
catch ( Exception ex )
{
2018-11-24 01:00:00 +08:00
if ( ! ex . Message . Contains ( "English Message : Join st needs to be the same as Select st1" ) ) {
throw new Exception ( "selec t8 error" ) ;
}
2018-11-24 00:40:27 +08:00
Console . WriteLine ( ex . Message ) ;
}
try
{
var t8 = db . Queryable < Student , School > ( ( st , sc ) = > st . Id = = sc . Id ) . Where ( st = > st . Id > 0 )
2018-11-24 01:00:00 +08:00
. Where ( x = > x . Id = = 1 )
. Select < School > ( ( st ) = > new School ( ) { Id = st . Id } ) . ToList ( ) ;
2018-11-24 00:40:27 +08:00
}
catch ( Exception ex )
{
2017-04-09 17:03:07 +08:00
2018-11-24 01:00:00 +08:00
if ( ! ex . Message . Contains ( "English Message : Join st needs to be the same as Where x" ) ) {
throw new Exception ( "selec t8 error" ) ;
}
Console . WriteLine ( ex . Message ) ;
}
try
{
var t8 = db . Queryable < Student , School > ( ( st , sc ) = > st . Id = = sc . Id )
. Sum ( x = > x . Id ) ;
}
catch ( Exception ex )
{
if ( ! ex . Message . Contains ( "English Message : Join st needs to be the same as Sum x" ) ) {
throw new Exception ( "selec t8 error" ) ;
}
2018-11-24 00:40:27 +08:00
Console . WriteLine ( ex . Message ) ;
}
2017-03-04 01:22:06 +08:00
}
}
}
}