mirror of
https://github.com/nsnail/FreeSql.git
synced 2025-04-24 19:52:51 +08:00
706 lines
41 KiB
C#
706 lines
41 KiB
C#
using FreeSql.DataAnnotations;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using Xunit;
|
||
|
||
namespace FreeSql.Tests.Oracle {
|
||
public class OracleSelectTest {
|
||
|
||
ISelect<Topic> select => g.oracle.Select<Topic>();
|
||
|
||
[Table(Name = "tb_topic22")]
|
||
class Topic {
|
||
[Column(IsIdentity = true, IsPrimary = true)]
|
||
public int Id { get; set; }
|
||
public int Clicks { get; set; }
|
||
public int TypeGuid { get; set; }
|
||
public TestTypeInfo Type { get; set; }
|
||
public string Title { get; set; }
|
||
public DateTime CreateTime { get; set; }
|
||
}
|
||
class TestTypeInfo {
|
||
[Column(IsIdentity = true)]
|
||
public int Guid { get; set; }
|
||
public int ParentId { get; set; }
|
||
public TestTypeParentInfo Parent { get; set; }
|
||
public string Name { get; set; }
|
||
}
|
||
class TestTypeParentInfo {
|
||
public int Id { get; set; }
|
||
public string Name { get; set; }
|
||
|
||
public List<TestTypeInfo> Types { get; set; }
|
||
}
|
||
|
||
class TopicInserts {
|
||
public Guid Id { get; set; }
|
||
public int Clicks { get; set; }
|
||
public int TypeGuid { get; set; }
|
||
public TestTypeInfo Type { get; set; }
|
||
public string Title { get; set; }
|
||
public DateTime CreateTime { get; set; }
|
||
}
|
||
public partial class Song {
|
||
[Column(IsIdentity = true)]
|
||
public int Id { get; set; }
|
||
public DateTime? Create_time { get; set; }
|
||
public bool? Is_deleted { get; set; }
|
||
public string Title { get; set; }
|
||
public string Url { get; set; }
|
||
|
||
public virtual ICollection<Tag> Tags { get; set; }
|
||
}
|
||
public partial class Song_tag {
|
||
public int Song_id { get; set; }
|
||
public virtual Song Song { get; set; }
|
||
|
||
public int Tag_id { get; set; }
|
||
public virtual Tag Tag { get; set; }
|
||
}
|
||
public partial class Tag {
|
||
[Column(IsIdentity = true)]
|
||
public int Id { get; set; }
|
||
public int? Parent_id { get; set; }
|
||
public virtual Tag Parent { get; set; }
|
||
|
||
public decimal? Ddd { get; set; }
|
||
public string Name { get; set; }
|
||
|
||
public virtual ICollection<Song> Songs { get; set; }
|
||
public virtual ICollection<Tag> Tags { get; set; }
|
||
}
|
||
|
||
[Fact]
|
||
public void AsSelect() {
|
||
//OneToOne、ManyToOne
|
||
var t0 = g.oracle.Select<Tag>().Where(a => a.Parent.Parent.Name == "粤语").ToSql();
|
||
//SELECT a.`Id`, a.`Parent_id`, a__Parent.`Id` as3, a__Parent.`Parent_id` as4, a__Parent.`Ddd`, a__Parent.`Name`, a.`Ddd` as7, a.`Name` as8
|
||
//FROM `Tag` a
|
||
//LEFT JOIN `Tag` a__Parent ON a__Parent.`Id` = a.`Parent_id`
|
||
//LEFT JOIN `Tag` a__Parent__Parent ON a__Parent__Parent.`Id` = a__Parent.`Parent_id`
|
||
//WHERE (a__Parent__Parent.`Name` = '粤语')
|
||
|
||
//OneToMany
|
||
var t1 = g.oracle.Select<Tag>().Where(a => a.Tags.AsSelect().Any(t => t.Parent.Id == 10)).ToSql();
|
||
//SELECT a.`Id`, a.`Parent_id`, a.`Ddd`, a.`Name`
|
||
//FROM `Tag` a
|
||
//WHERE (exists(SELECT 1
|
||
// FROM `Tag` t
|
||
// LEFT JOIN `Tag` t__Parent ON t__Parent.`Id` = t.`Parent_id`
|
||
// WHERE (t__Parent.`Id` = 10) AND (t.`Parent_id` = a.`Id`)
|
||
// limit 0,1))
|
||
|
||
//ManyToMany
|
||
var t2 = g.oracle.Select<Song>().Where(s => s.Tags.AsSelect().Any(t => t.Name == "国语")).ToSql();
|
||
//SELECT a.`Id`, a.`Create_time`, a.`Is_deleted`, a.`Title`, a.`Url`
|
||
//FROM `Song` a
|
||
//WHERE(exists(SELECT 1
|
||
// FROM `Song_tag` Mt_Ms
|
||
// WHERE(Mt_Ms.`Song_id` = a.`Id`) AND(exists(SELECT 1
|
||
// FROM `Tag` t
|
||
// WHERE(t.`Name` = '国语') AND(t.`Id` = Mt_Ms.`Tag_id`)
|
||
// limit 0, 1))
|
||
// limit 0, 1))
|
||
}
|
||
|
||
[Fact]
|
||
public void Lazy() {
|
||
var tags = g.oracle.Select<Tag>().Where(a => a.Parent.Name == "xxx")
|
||
.LeftJoin(a => a.Parent_id == a.Parent.Id)
|
||
.ToSql();
|
||
|
||
var songs = g.oracle.Select<Song>().Limit(10).ToList();
|
||
}
|
||
|
||
[Fact]
|
||
public void ToDataTable() {
|
||
var items = new List<TopicInserts>();
|
||
for (var a = 0; a < 11; a++) items.Add(new TopicInserts { Title = $"newtitle{a}", Clicks = a * 100, CreateTime = DateTime.Now });
|
||
|
||
//Assert.Equal(1, g.oracle.Insert<TopicInserts>().AppendData(items.First()).ExecuteAffrows());
|
||
Assert.Equal(11, g.oracle.Insert<TopicInserts>().AppendData(items).ExecuteAffrows());
|
||
|
||
//items = Enumerable.Range(0, 9989).Select(a => new TopicInserts { Title = "newtitle" + a, CreateTime = DateTime.Now }).ToList();
|
||
//Assert.Equal(9989, g.oracle.Insert<TopicInserts>(items).ExecuteAffrows());
|
||
|
||
var dt1 = select.Limit(10).ToDataTable();
|
||
var dt2 = select.Limit(10).ToDataTable("id, 111222");
|
||
var dt3 = select.Limit(10).ToDataTable(a => new { a.Id, a.Type.Name, now = DateTime.Now });
|
||
}
|
||
class TestDto {
|
||
public int id { get; set; }
|
||
public string name { get; set; }
|
||
}
|
||
[Fact]
|
||
public void ToList() {
|
||
|
||
var testDto1 = select.Limit(10).ToList(a => new TestDto { id = a.Id, name = a.Title });
|
||
var testDto2 = select.Limit(10).ToList(a => new TestDto());
|
||
var testDto3 = select.Limit(10).ToList(a => new TestDto { });
|
||
var testDto4 = select.Limit(10).ToList(a => new TestDto() { });
|
||
}
|
||
[Fact]
|
||
public void ToOne() {
|
||
}
|
||
[Fact]
|
||
public void ToSql() {
|
||
}
|
||
[Fact]
|
||
public void Any() {
|
||
var count = select.Where(a => 1 == 1).Count();
|
||
Assert.False(select.Where(a => 1 == 2).Any());
|
||
Assert.Equal(count > 0, select.Where(a => 1 == 1).Any());
|
||
|
||
var sql2222 = select.Where(a =>
|
||
select.Where(b => b.Id == a.Id &&
|
||
select.Where(c => c.Id == b.Id).Where(d => d.Id == a.Id).Where(e => e.Id == b.Id)
|
||
.Offset(a.Id)
|
||
.Any()
|
||
).Any(c => c.Id == a.Id + 10)
|
||
);
|
||
var sql2222Tolist = sql2222.ToList();
|
||
|
||
var collectionSelect = select.Where(a =>
|
||
a.Type.Guid == a.TypeGuid &&
|
||
a.Type.Parent.Id == a.Type.ParentId &&
|
||
a.Type.Parent.Types.AsSelect().Where(b => b.Name == a.Title).Any(b => b.ParentId == a.Type.Parent.Id)
|
||
);
|
||
collectionSelect.ToList();
|
||
}
|
||
[Fact]
|
||
public void Count() {
|
||
var count = select.Where(a => 1 == 1).Count();
|
||
select.Where(a => 1 == 1).Count(out var count2);
|
||
Assert.Equal(count, count2);
|
||
Assert.Equal(0, select.Where(a => 1 == 2).Count());
|
||
}
|
||
[Fact]
|
||
public void Master() {
|
||
Assert.StartsWith(" SELECT", select.Master().Where(a => 1 == 1).ToSql());
|
||
}
|
||
[Fact]
|
||
public void Caching() {
|
||
var result1 = select.Where(a => 1 == 1).Caching(20, "testcaching").ToList();
|
||
var testcaching1 = g.oracle.Cache.Get("testcaching");
|
||
Assert.NotNull(testcaching1);
|
||
var result2 = select.Where(a => 1 == 1).Caching(20, "testcaching").ToList();
|
||
var testcaching2 = g.oracle.Cache.Get("testcaching");
|
||
Assert.NotNull(testcaching2);
|
||
Assert.Equal(result1.Count, result1.Count);
|
||
}
|
||
[Fact]
|
||
public void From() {
|
||
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
var query2 = select.From<TestTypeInfo, TestTypeParentInfo>((s, b, c) => s
|
||
.LeftJoin(a => a.TypeGuid == b.Guid)
|
||
.LeftJoin(a => b.ParentId == c.Id)
|
||
);
|
||
var sql = query2.ToSql().Replace("\r\n", "");
|
||
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", b.\"GUID\", b.\"PARENTID\", b.\"NAME\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a LEFT JOIN \"TESTTYPEINFO\" b ON a.\"TYPEGUID\" = b.\"GUID\" LEFT JOIN \"TESTTYPEPARENTINFO\" c ON b.\"PARENTID\" = c.\"ID\"", sql);
|
||
query2.ToList();
|
||
}
|
||
[Fact]
|
||
public void LeftJoin() {
|
||
//<2F><><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>a.Type<70><65>a.Type.Parent <20><><EFBFBD>ǵ<EFBFBD><C7B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
var query = select.LeftJoin(a => a.Type.Guid == a.TypeGuid);
|
||
var sql = query.ToSql().Replace("\r\n", "");
|
||
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a__Type.\"GUID\", a__Type.\"PARENTID\", a__Type.\"NAME\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a LEFT JOIN \"TESTTYPEINFO\" a__Type ON a__Type.\"GUID\" = a.\"TYPEGUID\"", sql);
|
||
query.ToList();
|
||
|
||
query = select.LeftJoin(a => a.Type.Guid == a.TypeGuid && a.Type.Name == "xxx");
|
||
sql = query.ToSql().Replace("\r\n", "");
|
||
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a__Type.\"GUID\", a__Type.\"PARENTID\", a__Type.\"NAME\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a LEFT JOIN \"TESTTYPEINFO\" a__Type ON a__Type.\"GUID\" = a.\"TYPEGUID\" AND a__Type.\"NAME\" = 'xxx'", sql);
|
||
query.ToList();
|
||
|
||
query = select.LeftJoin(a => a.Type.Guid == a.TypeGuid && a.Type.Name == "xxx").Where(a => a.Type.Parent.Id == 10);
|
||
sql = query.ToSql().Replace("\r\n", "");
|
||
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a__Type.\"GUID\", a__Type.\"PARENTID\", a__Type.\"NAME\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a LEFT JOIN \"TESTTYPEINFO\" a__Type ON a__Type.\"GUID\" = a.\"TYPEGUID\" AND a__Type.\"NAME\" = 'xxx' LEFT JOIN \"TESTTYPEPARENTINFO\" a__Type__Parent ON a__Type__Parent.\"ID\" = a__Type.\"PARENTID\" WHERE (a__Type__Parent.\"ID\" = 10)", sql);
|
||
query.ToList();
|
||
|
||
//<2F><><EFBFBD>û<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
query = select.LeftJoin<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid);
|
||
sql = query.ToSql().Replace("\r\n", "");
|
||
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", b.\"GUID\", b.\"PARENTID\", b.\"NAME\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a LEFT JOIN \"TESTTYPEINFO\" b ON b.\"GUID\" = a.\"TYPEGUID\"", sql);
|
||
query.ToList();
|
||
|
||
query = select.LeftJoin<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid && b.Name == "xxx");
|
||
sql = query.ToSql().Replace("\r\n", "");
|
||
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", b.\"GUID\", b.\"PARENTID\", b.\"NAME\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a LEFT JOIN \"TESTTYPEINFO\" b ON b.\"GUID\" = a.\"TYPEGUID\" AND b.\"NAME\" = 'xxx'", sql);
|
||
query.ToList();
|
||
|
||
query = select.LeftJoin<TestTypeInfo>((a, a__Type) => a__Type.Guid == a.TypeGuid && a__Type.Name == "xxx").Where(a => a.Type.Parent.Id == 10);
|
||
sql = query.ToSql().Replace("\r\n", "");
|
||
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a__Type.\"GUID\", a__Type.\"PARENTID\", a__Type.\"NAME\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a LEFT JOIN \"TESTTYPEINFO\" a__Type ON a__Type.\"GUID\" = a.\"TYPEGUID\" AND a__Type.\"NAME\" = 'xxx' LEFT JOIN \"TESTTYPEPARENTINFO\" a__Type__Parent ON a__Type__Parent.\"ID\" = a__Type.\"PARENTID\" WHERE (a__Type__Parent.\"ID\" = 10)", sql);
|
||
query.ToList();
|
||
|
||
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
query = select
|
||
.LeftJoin(a => a.Type.Guid == a.TypeGuid)
|
||
.LeftJoin(a => a.Type.Parent.Id == a.Type.ParentId);
|
||
sql = query.ToSql().Replace("\r\n", "");
|
||
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a__Type.\"GUID\", a__Type.\"PARENTID\", a__Type.\"NAME\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a LEFT JOIN \"TESTTYPEINFO\" a__Type ON a__Type.\"GUID\" = a.\"TYPEGUID\" LEFT JOIN \"TESTTYPEPARENTINFO\" a__Type__Parent ON a__Type__Parent.\"ID\" = a__Type.\"PARENTID\"", sql);
|
||
query.ToList();
|
||
|
||
query = select
|
||
.LeftJoin<TestTypeInfo>((a, a__Type) => a__Type.Guid == a.TypeGuid)
|
||
.LeftJoin<TestTypeParentInfo>((a, c) => c.Id == a.Type.ParentId);
|
||
sql = query.ToSql().Replace("\r\n", "");
|
||
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a__Type.\"GUID\", a__Type.\"PARENTID\", a__Type.\"NAME\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a LEFT JOIN \"TESTTYPEINFO\" a__Type ON a__Type.\"GUID\" = a.\"TYPEGUID\" LEFT JOIN \"TESTTYPEPARENTINFO\" c ON c.\"ID\" = a__Type.\"PARENTID\"", sql);
|
||
query.ToList();
|
||
|
||
//<2F><><EFBFBD>û<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>b<EFBFBD><62>c<EFBFBD><63><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵ
|
||
var query2 = select.From<TestTypeInfo, TestTypeParentInfo>((s, b, c) => s
|
||
.LeftJoin(a => a.TypeGuid == b.Guid)
|
||
.LeftJoin(a => b.ParentId == c.Id));
|
||
sql = query2.ToSql().Replace("\r\n", "");
|
||
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", b.\"GUID\", b.\"PARENTID\", b.\"NAME\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a LEFT JOIN \"TESTTYPEINFO\" b ON a.\"TYPEGUID\" = b.\"GUID\" LEFT JOIN \"TESTTYPEPARENTINFO\" c ON b.\"PARENTID\" = c.\"ID\"", sql);
|
||
query2.ToList();
|
||
|
||
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>϶<EFBFBD><CFB6><EFBFBD><EFBFBD>㲻<EFBFBD><E3B2BB>
|
||
query = select.LeftJoin("\"TESTTYPEINFO\" b on b.\"GUID\" = a.\"TYPEGUID\"");
|
||
sql = query.ToSql().Replace("\r\n", "");
|
||
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a LEFT JOIN \"TESTTYPEINFO\" b on b.\"GUID\" = a.\"TYPEGUID\"", sql);
|
||
query.ToList();
|
||
|
||
query = select.LeftJoin("\"TESTTYPEINFO\" b on b.\"GUID\" = a.\"TYPEGUID\" and b.\"NAME\" = :bname", new { bname = "xxx" });
|
||
sql = query.ToSql().Replace("\r\n", "");
|
||
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a LEFT JOIN \"TESTTYPEINFO\" b on b.\"GUID\" = a.\"TYPEGUID\" and b.\"NAME\" = :bname", sql);
|
||
query.ToList();
|
||
}
|
||
[Fact]
|
||
public void InnerJoin() {
|
||
//<2F><><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>a.Type<70><65>a.Type.Parent <20><><EFBFBD>ǵ<EFBFBD><C7B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
var query = select.InnerJoin(a => a.Type.Guid == a.TypeGuid);
|
||
var sql = query.ToSql().Replace("\r\n", "");
|
||
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a__Type.\"GUID\", a__Type.\"PARENTID\", a__Type.\"NAME\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a INNER JOIN \"TESTTYPEINFO\" a__Type ON a__Type.\"GUID\" = a.\"TYPEGUID\"", sql);
|
||
query.ToList();
|
||
|
||
query = select.InnerJoin(a => a.Type.Guid == a.TypeGuid && a.Type.Name == "xxx");
|
||
sql = query.ToSql().Replace("\r\n", "");
|
||
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a__Type.\"GUID\", a__Type.\"PARENTID\", a__Type.\"NAME\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a INNER JOIN \"TESTTYPEINFO\" a__Type ON a__Type.\"GUID\" = a.\"TYPEGUID\" AND a__Type.\"NAME\" = 'xxx'", sql);
|
||
query.ToList();
|
||
|
||
query = select.InnerJoin(a => a.Type.Guid == a.TypeGuid && a.Type.Name == "xxx").Where(a => a.Type.Parent.Id == 10);
|
||
sql = query.ToSql().Replace("\r\n", "");
|
||
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a__Type.\"GUID\", a__Type.\"PARENTID\", a__Type.\"NAME\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a INNER JOIN \"TESTTYPEINFO\" a__Type ON a__Type.\"GUID\" = a.\"TYPEGUID\" AND a__Type.\"NAME\" = 'xxx' LEFT JOIN \"TESTTYPEPARENTINFO\" a__Type__Parent ON a__Type__Parent.\"ID\" = a__Type.\"PARENTID\" WHERE (a__Type__Parent.\"ID\" = 10)", sql);
|
||
query.ToList();
|
||
|
||
//<2F><><EFBFBD>û<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
query = select.InnerJoin<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid);
|
||
sql = query.ToSql().Replace("\r\n", "");
|
||
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", b.\"GUID\", b.\"PARENTID\", b.\"NAME\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a INNER JOIN \"TESTTYPEINFO\" b ON b.\"GUID\" = a.\"TYPEGUID\"", sql);
|
||
query.ToList();
|
||
|
||
query = select.InnerJoin<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid && b.Name == "xxx");
|
||
sql = query.ToSql().Replace("\r\n", "");
|
||
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", b.\"GUID\", b.\"PARENTID\", b.\"NAME\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a INNER JOIN \"TESTTYPEINFO\" b ON b.\"GUID\" = a.\"TYPEGUID\" AND b.\"NAME\" = 'xxx'", sql);
|
||
query.ToList();
|
||
|
||
query = select.InnerJoin<TestTypeInfo>((a, a__Type) => a__Type.Guid == a.TypeGuid && a__Type.Name == "xxx").Where(a => a.Type.Parent.Id == 10);
|
||
sql = query.ToSql().Replace("\r\n", "");
|
||
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a__Type.\"GUID\", a__Type.\"PARENTID\", a__Type.\"NAME\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a INNER JOIN \"TESTTYPEINFO\" a__Type ON a__Type.\"GUID\" = a.\"TYPEGUID\" AND a__Type.\"NAME\" = 'xxx' LEFT JOIN \"TESTTYPEPARENTINFO\" a__Type__Parent ON a__Type__Parent.\"ID\" = a__Type.\"PARENTID\" WHERE (a__Type__Parent.\"ID\" = 10)", sql);
|
||
query.ToList();
|
||
|
||
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
query = select
|
||
.InnerJoin(a => a.Type.Guid == a.TypeGuid)
|
||
.InnerJoin(a => a.Type.Parent.Id == a.Type.ParentId);
|
||
sql = query.ToSql().Replace("\r\n", "");
|
||
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a__Type.\"GUID\", a__Type.\"PARENTID\", a__Type.\"NAME\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a INNER JOIN \"TESTTYPEINFO\" a__Type ON a__Type.\"GUID\" = a.\"TYPEGUID\" INNER JOIN \"TESTTYPEPARENTINFO\" a__Type__Parent ON a__Type__Parent.\"ID\" = a__Type.\"PARENTID\"", sql);
|
||
query.ToList();
|
||
|
||
query = select
|
||
.InnerJoin<TestTypeInfo>((a, a__Type) => a__Type.Guid == a.TypeGuid)
|
||
.InnerJoin<TestTypeParentInfo>((a, c) => c.Id == a.Type.ParentId);
|
||
sql = query.ToSql().Replace("\r\n", "");
|
||
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a__Type.\"GUID\", a__Type.\"PARENTID\", a__Type.\"NAME\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a INNER JOIN \"TESTTYPEINFO\" a__Type ON a__Type.\"GUID\" = a.\"TYPEGUID\" INNER JOIN \"TESTTYPEPARENTINFO\" c ON c.\"ID\" = a__Type.\"PARENTID\"", sql);
|
||
query.ToList();
|
||
|
||
//<2F><><EFBFBD>û<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>b<EFBFBD><62>c<EFBFBD><63><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵ
|
||
var query2 = select.From<TestTypeInfo, TestTypeParentInfo>((s, b, c) => s
|
||
.InnerJoin(a => a.TypeGuid == b.Guid)
|
||
.InnerJoin(a => b.ParentId == c.Id));
|
||
sql = query2.ToSql().Replace("\r\n", "");
|
||
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", b.\"GUID\", b.\"PARENTID\", b.\"NAME\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a INNER JOIN \"TESTTYPEINFO\" b ON a.\"TYPEGUID\" = b.\"GUID\" INNER JOIN \"TESTTYPEPARENTINFO\" c ON b.\"PARENTID\" = c.\"ID\"", sql);
|
||
query2.ToList();
|
||
|
||
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>϶<EFBFBD><CFB6><EFBFBD><EFBFBD>㲻<EFBFBD><E3B2BB>
|
||
query = select.InnerJoin("\"TESTTYPEINFO\" b on b.\"GUID\" = a.\"TYPEGUID\"");
|
||
sql = query.ToSql().Replace("\r\n", "");
|
||
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a INNER JOIN \"TESTTYPEINFO\" b on b.\"GUID\" = a.\"TYPEGUID\"", sql);
|
||
query.ToList();
|
||
|
||
query = select.InnerJoin("\"TESTTYPEINFO\" b on b.\"GUID\" = a.\"TYPEGUID\" and b.\"NAME\" = :bname", new { bname = "xxx" });
|
||
sql = query.ToSql().Replace("\r\n", "");
|
||
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a INNER JOIN \"TESTTYPEINFO\" b on b.\"GUID\" = a.\"TYPEGUID\" and b.\"NAME\" = :bname", sql);
|
||
query.ToList();
|
||
|
||
}
|
||
[Fact]
|
||
public void RightJoin() {
|
||
//<2F><><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>a.Type<70><65>a.Type.Parent <20><><EFBFBD>ǵ<EFBFBD><C7B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
var query = select.RightJoin(a => a.Type.Guid == a.TypeGuid);
|
||
var sql = query.ToSql().Replace("\r\n", "");
|
||
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a__Type.\"GUID\", a__Type.\"PARENTID\", a__Type.\"NAME\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a RIGHT JOIN \"TESTTYPEINFO\" a__Type ON a__Type.\"GUID\" = a.\"TYPEGUID\"", sql);
|
||
query.ToList();
|
||
|
||
query = select.RightJoin(a => a.Type.Guid == a.TypeGuid && a.Type.Name == "xxx");
|
||
sql = query.ToSql().Replace("\r\n", "");
|
||
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a__Type.\"GUID\", a__Type.\"PARENTID\", a__Type.\"NAME\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a RIGHT JOIN \"TESTTYPEINFO\" a__Type ON a__Type.\"GUID\" = a.\"TYPEGUID\" AND a__Type.\"NAME\" = 'xxx'", sql);
|
||
query.ToList();
|
||
|
||
query = select.RightJoin(a => a.Type.Guid == a.TypeGuid && a.Type.Name == "xxx").Where(a => a.Type.Parent.Id == 10);
|
||
sql = query.ToSql().Replace("\r\n", "");
|
||
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a__Type.\"GUID\", a__Type.\"PARENTID\", a__Type.\"NAME\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a RIGHT JOIN \"TESTTYPEINFO\" a__Type ON a__Type.\"GUID\" = a.\"TYPEGUID\" AND a__Type.\"NAME\" = 'xxx' LEFT JOIN \"TESTTYPEPARENTINFO\" a__Type__Parent ON a__Type__Parent.\"ID\" = a__Type.\"PARENTID\" WHERE (a__Type__Parent.\"ID\" = 10)", sql);
|
||
query.ToList();
|
||
|
||
//<2F><><EFBFBD>û<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
query = select.RightJoin<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid);
|
||
sql = query.ToSql().Replace("\r\n", "");
|
||
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", b.\"GUID\", b.\"PARENTID\", b.\"NAME\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a RIGHT JOIN \"TESTTYPEINFO\" b ON b.\"GUID\" = a.\"TYPEGUID\"", sql);
|
||
query.ToList();
|
||
|
||
query = select.RightJoin<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid && b.Name == "xxx");
|
||
sql = query.ToSql().Replace("\r\n", "");
|
||
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", b.\"GUID\", b.\"PARENTID\", b.\"NAME\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a RIGHT JOIN \"TESTTYPEINFO\" b ON b.\"GUID\" = a.\"TYPEGUID\" AND b.\"NAME\" = 'xxx'", sql);
|
||
query.ToList();
|
||
|
||
query = select.RightJoin<TestTypeInfo>((a, a__Type) => a__Type.Guid == a.TypeGuid && a__Type.Name == "xxx").Where(a => a.Type.Parent.Id == 10);
|
||
sql = query.ToSql().Replace("\r\n", "");
|
||
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a__Type.\"GUID\", a__Type.\"PARENTID\", a__Type.\"NAME\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a RIGHT JOIN \"TESTTYPEINFO\" a__Type ON a__Type.\"GUID\" = a.\"TYPEGUID\" AND a__Type.\"NAME\" = 'xxx' LEFT JOIN \"TESTTYPEPARENTINFO\" a__Type__Parent ON a__Type__Parent.\"ID\" = a__Type.\"PARENTID\" WHERE (a__Type__Parent.\"ID\" = 10)", sql);
|
||
query.ToList();
|
||
|
||
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
query = select
|
||
.RightJoin(a => a.Type.Guid == a.TypeGuid)
|
||
.RightJoin(a => a.Type.Parent.Id == a.Type.ParentId);
|
||
sql = query.ToSql().Replace("\r\n", "");
|
||
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a__Type.\"GUID\", a__Type.\"PARENTID\", a__Type.\"NAME\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a RIGHT JOIN \"TESTTYPEINFO\" a__Type ON a__Type.\"GUID\" = a.\"TYPEGUID\" RIGHT JOIN \"TESTTYPEPARENTINFO\" a__Type__Parent ON a__Type__Parent.\"ID\" = a__Type.\"PARENTID\"", sql);
|
||
query.ToList();
|
||
|
||
query = select
|
||
.RightJoin<TestTypeInfo>((a, a__Type) => a__Type.Guid == a.TypeGuid)
|
||
.RightJoin<TestTypeParentInfo>((a, c) => c.Id == a.Type.ParentId);
|
||
sql = query.ToSql().Replace("\r\n", "");
|
||
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a__Type.\"GUID\", a__Type.\"PARENTID\", a__Type.\"NAME\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a RIGHT JOIN \"TESTTYPEINFO\" a__Type ON a__Type.\"GUID\" = a.\"TYPEGUID\" RIGHT JOIN \"TESTTYPEPARENTINFO\" c ON c.\"ID\" = a__Type.\"PARENTID\"", sql);
|
||
query.ToList();
|
||
|
||
//<2F><><EFBFBD>û<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>b<EFBFBD><62>c<EFBFBD><63><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵ
|
||
var query2 = select.From<TestTypeInfo, TestTypeParentInfo>((s, b, c) => s
|
||
.RightJoin(a => a.TypeGuid == b.Guid)
|
||
.RightJoin(a => b.ParentId == c.Id));
|
||
sql = query2.ToSql().Replace("\r\n", "");
|
||
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", b.\"GUID\", b.\"PARENTID\", b.\"NAME\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a RIGHT JOIN \"TESTTYPEINFO\" b ON a.\"TYPEGUID\" = b.\"GUID\" RIGHT JOIN \"TESTTYPEPARENTINFO\" c ON b.\"PARENTID\" = c.\"ID\"", sql);
|
||
query2.ToList();
|
||
|
||
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>϶<EFBFBD><CFB6><EFBFBD><EFBFBD>㲻<EFBFBD><E3B2BB>
|
||
query = select.RightJoin("\"TESTTYPEINFO\" b on b.\"GUID\" = a.\"TYPEGUID\"");
|
||
sql = query.ToSql().Replace("\r\n", "");
|
||
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a RIGHT JOIN \"TESTTYPEINFO\" b on b.\"GUID\" = a.\"TYPEGUID\"", sql);
|
||
query.ToList();
|
||
|
||
query = select.RightJoin("\"TESTTYPEINFO\" b on b.\"GUID\" = a.\"TYPEGUID\" and b.\"NAME\" = :bname", new { bname = "xxx" });
|
||
sql = query.ToSql().Replace("\r\n", "");
|
||
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a RIGHT JOIN \"TESTTYPEINFO\" b on b.\"GUID\" = a.\"TYPEGUID\" and b.\"NAME\" = :bname", sql);
|
||
query.ToList();
|
||
|
||
}
|
||
[Fact]
|
||
public void Where() {
|
||
//<2F><><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>a.Type<70><65>a.Type.Parent <20><><EFBFBD>ǵ<EFBFBD><C7B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
var query = select.Where(a => a.Id == 10);
|
||
var sql = query.ToSql().Replace("\r\n", "");
|
||
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a WHERE (a.\"ID\" = 10)", sql);
|
||
query.ToList();
|
||
|
||
query = select.Where(a => a.Id == 10 && a.Id > 10 || a.Clicks > 100);
|
||
sql = query.ToSql().Replace("\r\n", "");
|
||
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a WHERE (a.\"ID\" = 10 AND a.\"ID\" > 10 OR a.\"CLICKS\" > 100)", sql);
|
||
query.ToList();
|
||
|
||
query = select.Where(a => a.Id == 10).Where(a => a.Clicks > 100);
|
||
sql = query.ToSql().Replace("\r\n", "");
|
||
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a WHERE (a.\"ID\" = 10) AND (a.\"CLICKS\" > 100)", sql);
|
||
query.ToList();
|
||
|
||
query = select.Where(a => a.Type.Name == "typeTitle");
|
||
sql = query.ToSql().Replace("\r\n", "");
|
||
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a__Type.\"GUID\", a__Type.\"PARENTID\", a__Type.\"NAME\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a LEFT JOIN \"TESTTYPEINFO\" a__Type ON a__Type.\"GUID\" = a.\"TYPEGUID\" WHERE (a__Type.\"NAME\" = 'typeTitle')", sql);
|
||
query.ToList();
|
||
|
||
query = select.Where(a => a.Type.Name == "typeTitle" && a.Type.Guid == a.TypeGuid);
|
||
sql = query.ToSql().Replace("\r\n", "");
|
||
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a__Type.\"GUID\", a__Type.\"PARENTID\", a__Type.\"NAME\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a LEFT JOIN \"TESTTYPEINFO\" a__Type ON a__Type.\"GUID\" = a.\"TYPEGUID\" WHERE (a__Type.\"NAME\" = 'typeTitle' AND a__Type.\"GUID\" = a.\"TYPEGUID\")", sql);
|
||
query.ToList();
|
||
|
||
query = select.Where(a => a.Type.Parent.Name == "tparent");
|
||
sql = query.ToSql().Replace("\r\n", "");
|
||
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a__Type.\"GUID\", a__Type.\"PARENTID\", a__Type.\"NAME\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a LEFT JOIN \"TESTTYPEINFO\" a__Type ON a__Type.\"GUID\" = a.\"TYPEGUID\" LEFT JOIN \"TESTTYPEPARENTINFO\" a__Type__Parent ON a__Type__Parent.\"ID\" = a__Type.\"PARENTID\" WHERE (a__Type__Parent.\"NAME\" = 'tparent')", sql);
|
||
query.ToList();
|
||
|
||
//<2F><><EFBFBD>û<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ԣ<EFBFBD><D4A3><EFBFBD><F2B5A5B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
query = select.Where<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid && b.Name == "typeTitle");
|
||
sql = query.ToSql().Replace("\r\n", "");
|
||
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a, \"TESTTYPEINFO\" b WHERE (b.\"GUID\" = a.\"TYPEGUID\" AND b.\"NAME\" = 'typeTitle')", sql);
|
||
query.ToList();
|
||
|
||
query = select.Where<TestTypeInfo>((a, b) => b.Name == "typeTitle" && b.Guid == a.TypeGuid);
|
||
sql = query.ToSql().Replace("\r\n", "");
|
||
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a, \"TESTTYPEINFO\" b WHERE (b.\"NAME\" = 'typeTitle' AND b.\"GUID\" = a.\"TYPEGUID\")", sql);
|
||
query.ToList();
|
||
|
||
query = select.Where<TestTypeInfo, TestTypeParentInfo>((a, b, c) => c.Name == "tparent");
|
||
sql = query.ToSql().Replace("\r\n", "");
|
||
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a, \"TESTTYPEPARENTINFO\" c WHERE (c.\"NAME\" = 'tparent')", sql);
|
||
query.ToList();
|
||
|
||
//<2F><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB> From <20><>Ķ<EFBFBD><C4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
var query2 = select.From<TestTypeInfo, TestTypeParentInfo>((s, b, c) => s
|
||
.Where(a => a.Id == 10 && c.Name == "xxx")
|
||
.Where(a => b.ParentId == 20));
|
||
sql = query2.ToSql().Replace("\r\n", "");
|
||
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a, \"TESTTYPEINFO\" b, \"TESTTYPEPARENTINFO\" c WHERE (a.\"ID\" = 10 AND c.\"NAME\" = 'xxx') AND (b.\"PARENTID\" = 20)", sql);
|
||
query2.ToList();
|
||
|
||
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>϶<EFBFBD><CFB6><EFBFBD><EFBFBD>㲻<EFBFBD><E3B2BB>
|
||
query = select.Where("a.\"CLICKS\" > 100 and a.\"ID\" = :id", new { id = 10 });
|
||
sql = query.ToSql().Replace("\r\n", "");
|
||
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a WHERE (a.\"CLICKS\" > 100 and a.\"ID\" = :id)", sql);
|
||
query.ToList();
|
||
}
|
||
[Fact]
|
||
public void WhereIf() {
|
||
//<2F><><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>a.Type<70><65>a.Type.Parent <20><><EFBFBD>ǵ<EFBFBD><C7B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
var query = select.WhereIf(true, a => a.Id == 10);
|
||
var sql = query.ToSql().Replace("\r\n", "");
|
||
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a WHERE (a.\"ID\" = 10)", sql);
|
||
query.ToList();
|
||
|
||
query = select.WhereIf(true, a => a.Id == 10 && a.Id > 10 || a.Clicks > 100);
|
||
sql = query.ToSql().Replace("\r\n", "");
|
||
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a WHERE (a.\"ID\" = 10 AND a.\"ID\" > 10 OR a.\"CLICKS\" > 100)", sql);
|
||
query.ToList();
|
||
|
||
query = select.WhereIf(true, a => a.Id == 10).WhereIf(true, a => a.Clicks > 100);
|
||
sql = query.ToSql().Replace("\r\n", "");
|
||
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a WHERE (a.\"ID\" = 10) AND (a.\"CLICKS\" > 100)", sql);
|
||
query.ToList();
|
||
|
||
query = select.WhereIf(true, a => a.Type.Name == "typeTitle");
|
||
sql = query.ToSql().Replace("\r\n", "");
|
||
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a__Type.\"GUID\", a__Type.\"PARENTID\", a__Type.\"NAME\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a LEFT JOIN \"TESTTYPEINFO\" a__Type ON a__Type.\"GUID\" = a.\"TYPEGUID\" WHERE (a__Type.\"NAME\" = 'typeTitle')", sql);
|
||
query.ToList();
|
||
|
||
query = select.WhereIf(true, a => a.Type.Name == "typeTitle" && a.Type.Guid == a.TypeGuid);
|
||
sql = query.ToSql().Replace("\r\n", "");
|
||
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a__Type.\"GUID\", a__Type.\"PARENTID\", a__Type.\"NAME\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a LEFT JOIN \"TESTTYPEINFO\" a__Type ON a__Type.\"GUID\" = a.\"TYPEGUID\" WHERE (a__Type.\"NAME\" = 'typeTitle' AND a__Type.\"GUID\" = a.\"TYPEGUID\")", sql);
|
||
query.ToList();
|
||
|
||
query = select.WhereIf(true, a => a.Type.Parent.Name == "tparent");
|
||
sql = query.ToSql().Replace("\r\n", "");
|
||
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a__Type.\"GUID\", a__Type.\"PARENTID\", a__Type.\"NAME\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a LEFT JOIN \"TESTTYPEINFO\" a__Type ON a__Type.\"GUID\" = a.\"TYPEGUID\" LEFT JOIN \"TESTTYPEPARENTINFO\" a__Type__Parent ON a__Type__Parent.\"ID\" = a__Type.\"PARENTID\" WHERE (a__Type__Parent.\"NAME\" = 'tparent')", sql);
|
||
query.ToList();
|
||
|
||
//<2F><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB> From <20><>Ķ<EFBFBD><C4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
var query2 = select.From<TestTypeInfo, TestTypeParentInfo>((s, b, c) => s
|
||
.WhereIf(true, a => a.Id == 10 && c.Name == "xxx")
|
||
.WhereIf(true, a => b.ParentId == 20));
|
||
sql = query2.ToSql().Replace("\r\n", "");
|
||
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a, \"TESTTYPEINFO\" b, \"TESTTYPEPARENTINFO\" c WHERE (a.\"ID\" = 10 AND c.\"NAME\" = 'xxx') AND (b.\"PARENTID\" = 20)", sql);
|
||
query2.ToList();
|
||
|
||
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>϶<EFBFBD><CFB6><EFBFBD><EFBFBD>㲻<EFBFBD><E3B2BB>
|
||
query = select.WhereIf(true, "a.\"CLICKS\" > 100 and a.\"ID\" = :id", new { id = 10 });
|
||
sql = query.ToSql().Replace("\r\n", "");
|
||
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a WHERE (a.\"CLICKS\" > 100 and a.\"ID\" = :id)", sql);
|
||
query.ToList();
|
||
|
||
// ==========================================WhereIf(false)
|
||
|
||
//<2F><><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>a.Type<70><65>a.Type.Parent <20><><EFBFBD>ǵ<EFBFBD><C7B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
query = select.WhereIf(false, a => a.Id == 10);
|
||
sql = query.ToSql().Replace("\r\n", "");
|
||
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a", sql);
|
||
query.ToList();
|
||
|
||
query = select.WhereIf(false, a => a.Id == 10 && a.Id > 10 || a.Clicks > 100);
|
||
sql = query.ToSql().Replace("\r\n", "");
|
||
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a", sql);
|
||
query.ToList();
|
||
|
||
query = select.WhereIf(false, a => a.Id == 10).WhereIf(false, a => a.Clicks > 100);
|
||
sql = query.ToSql().Replace("\r\n", "");
|
||
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a", sql);
|
||
query.ToList();
|
||
|
||
query = select.WhereIf(false, a => a.Type.Name == "typeTitle");
|
||
sql = query.ToSql().Replace("\r\n", "");
|
||
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a", sql);
|
||
query.ToList();
|
||
|
||
query = select.WhereIf(false, a => a.Type.Name == "typeTitle" && a.Type.Guid == a.TypeGuid);
|
||
sql = query.ToSql().Replace("\r\n", "");
|
||
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a", sql);
|
||
query.ToList();
|
||
|
||
query = select.WhereIf(false, a => a.Type.Parent.Name == "tparent");
|
||
sql = query.ToSql().Replace("\r\n", "");
|
||
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a", sql);
|
||
query.ToList();
|
||
|
||
//<2F><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB> From <20><>Ķ<EFBFBD><C4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
query2 = select.From<TestTypeInfo, TestTypeParentInfo>((s, b, c) => s
|
||
.WhereIf(false, a => a.Id == 10 && c.Name == "xxx")
|
||
.WhereIf(false, a => b.ParentId == 20));
|
||
sql = query2.ToSql().Replace("\r\n", "");
|
||
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a, \"TESTTYPEINFO\" b, \"TESTTYPEPARENTINFO\" c", sql);
|
||
query2.ToList();
|
||
|
||
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>϶<EFBFBD><CFB6><EFBFBD><EFBFBD>㲻<EFBFBD><E3B2BB>
|
||
query = select.WhereIf(false, "a.\"CLICKS\" > 100 and a.\"ID\" = :id", new { id = 10 });
|
||
sql = query.ToSql().Replace("\r\n", "");
|
||
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a", sql);
|
||
query.ToList();
|
||
}
|
||
[Fact]
|
||
public void WhereExists() {
|
||
var sql2222 = select.Where(a => select.Where(b => b.Id == a.Id).Any()).ToList();
|
||
|
||
sql2222 = select.Where(a =>
|
||
select.Where(b => b.Id == a.Id && select.Where(c => c.Id == b.Id).Where(d => d.Id == a.Id).Where(e => e.Id == b.Id)
|
||
|
||
.Offset(a.Id)
|
||
|
||
.Any()
|
||
).Any()
|
||
).ToList();
|
||
}
|
||
[Fact]
|
||
public void GroupBy() {
|
||
var groupby = select.From<TestTypeInfo, TestTypeParentInfo>((s, b, c) => s
|
||
.Where(a => a.Id == 1)
|
||
)
|
||
.GroupBy((a, b, c) => new { tt2 = a.Title.Substring(0, 2), mod4 = a.Id % 4 })
|
||
.Having(a => a.Count() > 0 && a.Avg(a.Key.mod4) > 0 && a.Max(a.Key.mod4) > 0)
|
||
.Having(a => a.Count() < 300 || a.Avg(a.Key.mod4) < 100)
|
||
.OrderBy(a => a.Key.tt2)
|
||
.OrderByDescending(a => a.Count())
|
||
.Offset(10)
|
||
.Limit(2)
|
||
.ToList(a => new {
|
||
a.Key.tt2,
|
||
cou1 = a.Count(),
|
||
arg1 = a.Avg(a.Key.mod4),
|
||
ccc2 = a.Key.tt2 ?? "now()",
|
||
//ccc = Convert.ToDateTime("now()"), partby = Convert.ToDecimal("sum(num) over(PARTITION BY server_id,os,rid,chn order by id desc)")
|
||
});
|
||
}
|
||
[Fact]
|
||
public void ToAggregate() {
|
||
var sql = select.ToAggregate(a => new { sum = a.Sum(a.Key.Id + 11.11), avg = a.Avg(a.Key.Id), count = a.Count(), max = a.Max(a.Key.Id), min = a.Min(a.Key.Id) });
|
||
}
|
||
[Fact]
|
||
public void OrderBy() {
|
||
var sql = select.Offset(10).OrderBy(a => new Random().NextDouble()).ToList();
|
||
}
|
||
[Fact]
|
||
public void Skip_Offset() {
|
||
var sql = select.Offset(10).ToList();
|
||
}
|
||
[Fact]
|
||
public void Take_Limit() {
|
||
var sql = select.Limit(10).ToList();
|
||
}
|
||
[Fact]
|
||
public void Page() {
|
||
var sql1 = select.Page(1, 10).ToList();
|
||
var sql2 = select.Page(2, 10).ToList();
|
||
var sql3 = select.Page(3, 10).ToList();
|
||
|
||
var sql11 = select.OrderBy(a => new Random().NextDouble()).Page(1, 10).ToList();
|
||
var sql22 = select.OrderBy(a => new Random().NextDouble()).Page(2, 10).ToList();
|
||
var sql33 = select.OrderBy(a => new Random().NextDouble()).Page(3, 10).ToList();
|
||
}
|
||
[Fact]
|
||
public void Sum() {
|
||
}
|
||
[Fact]
|
||
public void Min() {
|
||
}
|
||
[Fact]
|
||
public void Max() {
|
||
}
|
||
[Fact]
|
||
public void Avg() {
|
||
}
|
||
[Fact]
|
||
public void As() {
|
||
}
|
||
|
||
[Fact]
|
||
public void AsTable() {
|
||
Func<Type, string, string> tableRule = (type, oldname) => {
|
||
if (type == typeof(Topic)) return oldname + "AsTable1";
|
||
else if (type == typeof(TestTypeInfo)) return oldname + "AsTable2";
|
||
return oldname + "AsTable";
|
||
};
|
||
|
||
//<2F><><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>a.Type<70><65>a.Type.Parent <20><><EFBFBD>ǵ<EFBFBD><C7B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
var query = select.LeftJoin(a => a.Type.Guid == a.TypeGuid).AsTable(tableRule);
|
||
var sql = query.ToSql().Replace("\r\n", "");
|
||
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a__Type.\"GUID\", a__Type.\"PARENTID\", a__Type.\"NAME\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22AsTable1\" a LEFT JOIN \"TESTTYPEINFOAsTable2\" a__Type ON a__Type.\"GUID\" = a.\"TYPEGUID\"", sql);
|
||
|
||
query = select.LeftJoin(a => a.Type.Guid == a.TypeGuid && a.Type.Name == "xxx").AsTable(tableRule);
|
||
sql = query.ToSql().Replace("\r\n", "");
|
||
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a__Type.\"GUID\", a__Type.\"PARENTID\", a__Type.\"NAME\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22AsTable1\" a LEFT JOIN \"TESTTYPEINFOAsTable2\" a__Type ON a__Type.\"GUID\" = a.\"TYPEGUID\" AND a__Type.\"NAME\" = 'xxx'", sql);
|
||
|
||
query = select.LeftJoin(a => a.Type.Guid == a.TypeGuid && a.Type.Name == "xxx").Where(a => a.Type.Parent.Id == 10).AsTable(tableRule);
|
||
sql = query.ToSql().Replace("\r\n", "");
|
||
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a__Type.\"GUID\", a__Type.\"PARENTID\", a__Type.\"NAME\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22AsTable1\" a LEFT JOIN \"TESTTYPEINFOAsTable2\" a__Type ON a__Type.\"GUID\" = a.\"TYPEGUID\" AND a__Type.\"NAME\" = 'xxx' LEFT JOIN \"TESTTYPEPARENTINFOAsTable\" a__Type__Parent ON a__Type__Parent.\"ID\" = a__Type.\"PARENTID\" WHERE (a__Type__Parent.\"ID\" = 10)", sql);
|
||
|
||
//<2F><><EFBFBD>û<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
query = select.LeftJoin<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid).AsTable(tableRule);
|
||
sql = query.ToSql().Replace("\r\n", "");
|
||
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", b.\"GUID\", b.\"PARENTID\", b.\"NAME\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22AsTable1\" a LEFT JOIN \"TESTTYPEINFOAsTable2\" b ON b.\"GUID\" = a.\"TYPEGUID\"", sql);
|
||
|
||
query = select.LeftJoin<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid && b.Name == "xxx").AsTable(tableRule);
|
||
sql = query.ToSql().Replace("\r\n", "");
|
||
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", b.\"GUID\", b.\"PARENTID\", b.\"NAME\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22AsTable1\" a LEFT JOIN \"TESTTYPEINFOAsTable2\" b ON b.\"GUID\" = a.\"TYPEGUID\" AND b.\"NAME\" = 'xxx'", sql);
|
||
|
||
query = select.LeftJoin<TestTypeInfo>((a, a__Type) => a__Type.Guid == a.TypeGuid && a__Type.Name == "xxx").Where(a => a.Type.Parent.Id == 10).AsTable(tableRule);
|
||
sql = query.ToSql().Replace("\r\n", "");
|
||
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a__Type.\"GUID\", a__Type.\"PARENTID\", a__Type.\"NAME\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22AsTable1\" a LEFT JOIN \"TESTTYPEINFOAsTable2\" a__Type ON a__Type.\"GUID\" = a.\"TYPEGUID\" AND a__Type.\"NAME\" = 'xxx' LEFT JOIN \"TESTTYPEPARENTINFOAsTable\" a__Type__Parent ON a__Type__Parent.\"ID\" = a__Type.\"PARENTID\" WHERE (a__Type__Parent.\"ID\" = 10)", sql);
|
||
|
||
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
query = select
|
||
.LeftJoin(a => a.Type.Guid == a.TypeGuid)
|
||
.LeftJoin(a => a.Type.Parent.Id == a.Type.ParentId).AsTable(tableRule);
|
||
sql = query.ToSql().Replace("\r\n", "");
|
||
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a__Type.\"GUID\", a__Type.\"PARENTID\", a__Type.\"NAME\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22AsTable1\" a LEFT JOIN \"TESTTYPEINFOAsTable2\" a__Type ON a__Type.\"GUID\" = a.\"TYPEGUID\" LEFT JOIN \"TESTTYPEPARENTINFOAsTable\" a__Type__Parent ON a__Type__Parent.\"ID\" = a__Type.\"PARENTID\"", sql);
|
||
|
||
query = select
|
||
.LeftJoin<TestTypeInfo>((a, a__Type) => a__Type.Guid == a.TypeGuid)
|
||
.LeftJoin<TestTypeParentInfo>((a, c) => c.Id == a.Type.ParentId).AsTable(tableRule);
|
||
sql = query.ToSql().Replace("\r\n", "");
|
||
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a__Type.\"GUID\", a__Type.\"PARENTID\", a__Type.\"NAME\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22AsTable1\" a LEFT JOIN \"TESTTYPEINFOAsTable2\" a__Type ON a__Type.\"GUID\" = a.\"TYPEGUID\" LEFT JOIN \"TESTTYPEPARENTINFOAsTable\" c ON c.\"ID\" = a__Type.\"PARENTID\"", sql);
|
||
|
||
//<2F><><EFBFBD>û<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>b<EFBFBD><62>c<EFBFBD><63><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵ
|
||
var query2 = select.From<TestTypeInfo, TestTypeParentInfo>((s, b, c) => s
|
||
.LeftJoin(a => a.TypeGuid == b.Guid)
|
||
.LeftJoin(a => b.ParentId == c.Id)).AsTable(tableRule);
|
||
sql = query2.ToSql().Replace("\r\n", "");
|
||
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", b.\"GUID\", b.\"PARENTID\", b.\"NAME\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22AsTable1\" a LEFT JOIN \"TESTTYPEINFOAsTable2\" b ON a.\"TYPEGUID\" = b.\"GUID\" LEFT JOIN \"TESTTYPEPARENTINFOAsTable\" c ON b.\"PARENTID\" = c.\"ID\"", sql);
|
||
|
||
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>϶<EFBFBD><CFB6><EFBFBD><EFBFBD>㲻<EFBFBD><E3B2BB>
|
||
query = select.LeftJoin("\"TESTTYPEINFO\" b on b.\"GUID\" = a.\"TYPEGUID\"").AsTable(tableRule);
|
||
sql = query.ToSql().Replace("\r\n", "");
|
||
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22AsTable1\" a LEFT JOIN \"TESTTYPEINFO\" b on b.\"GUID\" = a.\"TYPEGUID\"", sql);
|
||
|
||
query = select.LeftJoin("\"TESTTYPEINFO\" b on b.\"GUID\" = a.\"TYPEGUID\" and b.\"NAME\" = :bname", new { bname = "xxx" }).AsTable(tableRule);
|
||
sql = query.ToSql().Replace("\r\n", "");
|
||
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22AsTable1\" a LEFT JOIN \"TESTTYPEINFO\" b on b.\"GUID\" = a.\"TYPEGUID\" and b.\"NAME\" = :bname", sql);
|
||
}
|
||
}
|
||
}
|