完成v0.3.17所有数据库的测试

This commit is contained in:
28810 2019-03-18 18:55:26 +08:00
parent 8372f96ab1
commit 8b32e5e0fc
33 changed files with 912 additions and 712 deletions

View File

@ -680,15 +680,25 @@ namespace FreeSql.Tests.MySql {
[Fact] [Fact]
public void OrderBy() { public void OrderBy() {
var sql = select.OrderBy(a => new Random().NextDouble()).ToList();
} }
[Fact] [Fact]
public void Skip_Offset() { public void Skip_Offset() {
var sql = select.Offset(10).Limit(10).ToList();
} }
[Fact] [Fact]
public void Take_Limit() { public void Take_Limit() {
var sql = select.Limit(10).ToList();
} }
[Fact] [Fact]
public void Page() { 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] [Fact]
public void Sum() { public void Sum() {

View File

@ -20,6 +20,7 @@ namespace FreeSql.Tests.Oracle {
public DateTime CreateTime { get; set; } public DateTime CreateTime { get; set; }
} }
class TestTypeInfo { class TestTypeInfo {
[Column(IsIdentity = true)]
public int Guid { get; set; } public int Guid { get; set; }
public int ParentId { get; set; } public int ParentId { get; set; }
public TestTypeParentInfo Parent { get; set; } public TestTypeParentInfo Parent { get; set; }
@ -40,6 +41,77 @@ namespace FreeSql.Tests.Oracle {
public string Title { get; set; } public string Title { get; set; }
public DateTime CreateTime { 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] [Fact]
public void ToDataTable() { public void ToDataTable() {
@ -116,7 +188,7 @@ namespace FreeSql.Tests.Oracle {
.LeftJoin(a => b.ParentId == c.Id) .LeftJoin(a => b.ParentId == c.Id)
); );
var sql = query2.ToSql().Replace("\r\n", ""); 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); 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(); query2.ToList();
} }
[Fact] [Fact]
@ -124,33 +196,33 @@ namespace FreeSql.Tests.Oracle {
//<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> //<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 query = select.LeftJoin(a => a.Type.Guid == a.TypeGuid);
var sql = query.ToSql().Replace("\r\n", ""); 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); 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.ToList();
query = select.LeftJoin(a => a.Type.Guid == a.TypeGuid && a.Type.Name == "xxx"); query = select.LeftJoin(a => a.Type.Guid == a.TypeGuid && a.Type.Name == "xxx");
sql = query.ToSql().Replace("\r\n", ""); 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); 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.ToList();
query = select.LeftJoin(a => a.Type.Guid == a.TypeGuid && a.Type.Name == "xxx").Where(a => a.Type.Parent.Id == 10); 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", ""); 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 \"TESTTYPEPARENTINFO\" a__Type__Parent ON 1 = 1 LEFT JOIN \"TESTTYPEINFO\" a__Type ON a__Type.\"GUID\" = a.\"TypeGuid\" AND a__Type.\"NAME\" = 'xxx' WHERE (a__Type__Parent.\"ID\" = 10)", sql); 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(); query.ToList();
//<2F><><EFBFBD>û<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> //<2F><><EFBFBD>û<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
query = select.LeftJoin<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid); query = select.LeftJoin<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid);
sql = query.ToSql().Replace("\r\n", ""); 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); 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.ToList();
query = select.LeftJoin<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid && b.Name == "xxx"); query = select.LeftJoin<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid && b.Name == "xxx");
sql = query.ToSql().Replace("\r\n", ""); 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); 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.ToList();
query = select.LeftJoin<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid && b.Name == "xxx").Where(a => a.Type.Parent.Id == 10); 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", ""); 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 \"TESTTYPEPARENTINFO\" b__Parent ON 1 = 1 LEFT JOIN \"TESTTYPEINFO\" b ON b.\"GUID\" = a.\"TypeGuid\" AND b.\"NAME\" = 'xxx' WHERE (b__Parent.\"ID\" = 10)", sql); 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(); query.ToList();
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
@ -158,14 +230,14 @@ namespace FreeSql.Tests.Oracle {
.LeftJoin(a => a.Type.Guid == a.TypeGuid) .LeftJoin(a => a.Type.Guid == a.TypeGuid)
.LeftJoin(a => a.Type.Parent.Id == a.Type.ParentId); .LeftJoin(a => a.Type.Parent.Id == a.Type.ParentId);
sql = query.ToSql().Replace("\r\n", ""); 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); 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.ToList();
query = select query = select
.LeftJoin<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid) .LeftJoin<TestTypeInfo>((a, a__Type) => a__Type.Guid == a.TypeGuid)
.LeftJoin<TestTypeParentInfo>((a, c) => c.Id == a.Type.ParentId); .LeftJoin<TestTypeParentInfo>((a, c) => c.Id == a.Type.ParentId);
sql = query.ToSql().Replace("\r\n", ""); 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\" LEFT JOIN \"TESTTYPEPARENTINFO\" c ON c.\"ID\" = b.\"PARENTID\"", sql); 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(); query.ToList();
//<2F><><EFBFBD>û<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>b<EFBFBD><62>c<EFBFBD><63><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵ //<2F><><EFBFBD>û<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>b<EFBFBD><62>c<EFBFBD><63><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵ
@ -173,18 +245,18 @@ namespace FreeSql.Tests.Oracle {
.LeftJoin(a => a.TypeGuid == b.Guid) .LeftJoin(a => a.TypeGuid == b.Guid)
.LeftJoin(a => b.ParentId == c.Id)); .LeftJoin(a => b.ParentId == c.Id));
sql = query2.ToSql().Replace("\r\n", ""); 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); 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(); query2.ToList();
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>϶<EFBFBD><CFB6><EFBFBD><EFBFBD><EFBFBD><E3B2BB> //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>϶<EFBFBD><CFB6><EFBFBD><EFBFBD><EFBFBD><E3B2BB>
query = select.LeftJoin("\"TESTTYPEINFO\" b on b.\"GUID\" = a.\"TypeGuid\""); query = select.LeftJoin("\"TESTTYPEINFO\" b on b.\"GUID\" = a.\"TYPEGUID\"");
sql = query.ToSql().Replace("\r\n", ""); 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); 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.ToList();
query = select.LeftJoin("\"TESTTYPEINFO\" b on b.\"GUID\" = a.\"TypeGuid\" and b.\"NAME\" = :bname", new { bname = "xxx" }); query = select.LeftJoin("\"TESTTYPEINFO\" b on b.\"GUID\" = a.\"TYPEGUID\" and b.\"NAME\" = :bname", new { bname = "xxx" });
sql = query.ToSql().Replace("\r\n", ""); 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); 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(); query.ToList();
} }
[Fact] [Fact]
@ -192,33 +264,33 @@ namespace FreeSql.Tests.Oracle {
//<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> //<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 query = select.InnerJoin(a => a.Type.Guid == a.TypeGuid);
var sql = query.ToSql().Replace("\r\n", ""); 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); 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.ToList();
query = select.InnerJoin(a => a.Type.Guid == a.TypeGuid && a.Type.Name == "xxx"); query = select.InnerJoin(a => a.Type.Guid == a.TypeGuid && a.Type.Name == "xxx");
sql = query.ToSql().Replace("\r\n", ""); 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); 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.ToList();
query = select.InnerJoin(a => a.Type.Guid == a.TypeGuid && a.Type.Name == "xxx").Where(a => a.Type.Parent.Id == 10); 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", ""); 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 \"TESTTYPEPARENTINFO\" a__Type__Parent ON 1 = 1 INNER JOIN \"TESTTYPEINFO\" a__Type ON a__Type.\"GUID\" = a.\"TypeGuid\" AND a__Type.\"NAME\" = 'xxx' WHERE (a__Type__Parent.\"ID\" = 10)", sql); 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(); query.ToList();
//<2F><><EFBFBD>û<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> //<2F><><EFBFBD>û<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
query = select.InnerJoin<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid); query = select.InnerJoin<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid);
sql = query.ToSql().Replace("\r\n", ""); 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); 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.ToList();
query = select.InnerJoin<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid && b.Name == "xxx"); query = select.InnerJoin<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid && b.Name == "xxx");
sql = query.ToSql().Replace("\r\n", ""); 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); 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.ToList();
query = select.InnerJoin<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid && b.Name == "xxx").Where(a => a.Type.Parent.Id == 10); 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", ""); 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 \"TESTTYPEPARENTINFO\" b__Parent ON 1 = 1 INNER JOIN \"TESTTYPEINFO\" b ON b.\"GUID\" = a.\"TypeGuid\" AND b.\"NAME\" = 'xxx' WHERE (b__Parent.\"ID\" = 10)", sql); 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(); query.ToList();
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
@ -226,14 +298,14 @@ namespace FreeSql.Tests.Oracle {
.InnerJoin(a => a.Type.Guid == a.TypeGuid) .InnerJoin(a => a.Type.Guid == a.TypeGuid)
.InnerJoin(a => a.Type.Parent.Id == a.Type.ParentId); .InnerJoin(a => a.Type.Parent.Id == a.Type.ParentId);
sql = query.ToSql().Replace("\r\n", ""); 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); 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.ToList();
query = select query = select
.InnerJoin<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid) .InnerJoin<TestTypeInfo>((a, a__Type) => a__Type.Guid == a.TypeGuid)
.InnerJoin<TestTypeParentInfo>((a, c) => c.Id == a.Type.ParentId); .InnerJoin<TestTypeParentInfo>((a, c) => c.Id == a.Type.ParentId);
sql = query.ToSql().Replace("\r\n", ""); 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\" INNER JOIN \"TESTTYPEPARENTINFO\" c ON c.\"ID\" = b.\"PARENTID\"", sql); 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(); query.ToList();
//<2F><><EFBFBD>û<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>b<EFBFBD><62>c<EFBFBD><63><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵ //<2F><><EFBFBD>û<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>b<EFBFBD><62>c<EFBFBD><63><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵ
@ -241,18 +313,18 @@ namespace FreeSql.Tests.Oracle {
.InnerJoin(a => a.TypeGuid == b.Guid) .InnerJoin(a => a.TypeGuid == b.Guid)
.InnerJoin(a => b.ParentId == c.Id)); .InnerJoin(a => b.ParentId == c.Id));
sql = query2.ToSql().Replace("\r\n", ""); 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); 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(); query2.ToList();
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>϶<EFBFBD><CFB6><EFBFBD><EFBFBD><EFBFBD><E3B2BB> //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>϶<EFBFBD><CFB6><EFBFBD><EFBFBD><EFBFBD><E3B2BB>
query = select.InnerJoin("\"TESTTYPEINFO\" b on b.\"GUID\" = a.\"TypeGuid\""); query = select.InnerJoin("\"TESTTYPEINFO\" b on b.\"GUID\" = a.\"TYPEGUID\"");
sql = query.ToSql().Replace("\r\n", ""); 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); 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.ToList();
query = select.InnerJoin("\"TESTTYPEINFO\" b on b.\"GUID\" = a.\"TypeGuid\" and b.\"NAME\" = :bname", new { bname = "xxx" }); query = select.InnerJoin("\"TESTTYPEINFO\" b on b.\"GUID\" = a.\"TYPEGUID\" and b.\"NAME\" = :bname", new { bname = "xxx" });
sql = query.ToSql().Replace("\r\n", ""); 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); 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(); query.ToList();
} }
@ -261,33 +333,33 @@ namespace FreeSql.Tests.Oracle {
//<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> //<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 query = select.RightJoin(a => a.Type.Guid == a.TypeGuid);
var sql = query.ToSql().Replace("\r\n", ""); 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); 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.ToList();
query = select.RightJoin(a => a.Type.Guid == a.TypeGuid && a.Type.Name == "xxx"); query = select.RightJoin(a => a.Type.Guid == a.TypeGuid && a.Type.Name == "xxx");
sql = query.ToSql().Replace("\r\n", ""); 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); 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.ToList();
query = select.RightJoin(a => a.Type.Guid == a.TypeGuid && a.Type.Name == "xxx").Where(a => a.Type.Parent.Id == 10); 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", ""); 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 \"TESTTYPEPARENTINFO\" a__Type__Parent ON 1 = 1 RIGHT JOIN \"TESTTYPEINFO\" a__Type ON a__Type.\"GUID\" = a.\"TypeGuid\" AND a__Type.\"NAME\" = 'xxx' WHERE (a__Type__Parent.\"ID\" = 10)", sql); 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(); query.ToList();
//<2F><><EFBFBD>û<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> //<2F><><EFBFBD>û<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
query = select.RightJoin<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid); query = select.RightJoin<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid);
sql = query.ToSql().Replace("\r\n", ""); 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); 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.ToList();
query = select.RightJoin<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid && b.Name == "xxx"); query = select.RightJoin<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid && b.Name == "xxx");
sql = query.ToSql().Replace("\r\n", ""); 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); 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.ToList();
query = select.RightJoin<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid && b.Name == "xxx").Where(a => a.Type.Parent.Id == 10); 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", ""); 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 \"TESTTYPEPARENTINFO\" b__Parent ON 1 = 1 RIGHT JOIN \"TESTTYPEINFO\" b ON b.\"GUID\" = a.\"TypeGuid\" AND b.\"NAME\" = 'xxx' WHERE (b__Parent.\"ID\" = 10)", sql); 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(); query.ToList();
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
@ -295,14 +367,14 @@ namespace FreeSql.Tests.Oracle {
.RightJoin(a => a.Type.Guid == a.TypeGuid) .RightJoin(a => a.Type.Guid == a.TypeGuid)
.RightJoin(a => a.Type.Parent.Id == a.Type.ParentId); .RightJoin(a => a.Type.Parent.Id == a.Type.ParentId);
sql = query.ToSql().Replace("\r\n", ""); 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); 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.ToList();
query = select query = select
.RightJoin<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid) .RightJoin<TestTypeInfo>((a, a__Type) => a__Type.Guid == a.TypeGuid)
.RightJoin<TestTypeParentInfo>((a, c) => c.Id == a.Type.ParentId); .RightJoin<TestTypeParentInfo>((a, c) => c.Id == a.Type.ParentId);
sql = query.ToSql().Replace("\r\n", ""); 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\" RIGHT JOIN \"TESTTYPEPARENTINFO\" c ON c.\"ID\" = b.\"PARENTID\"", sql); 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(); query.ToList();
//<2F><><EFBFBD>û<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>b<EFBFBD><62>c<EFBFBD><63><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵ //<2F><><EFBFBD>û<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>b<EFBFBD><62>c<EFBFBD><63><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵ
@ -310,18 +382,18 @@ namespace FreeSql.Tests.Oracle {
.RightJoin(a => a.TypeGuid == b.Guid) .RightJoin(a => a.TypeGuid == b.Guid)
.RightJoin(a => b.ParentId == c.Id)); .RightJoin(a => b.ParentId == c.Id));
sql = query2.ToSql().Replace("\r\n", ""); 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); 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(); query2.ToList();
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>϶<EFBFBD><CFB6><EFBFBD><EFBFBD><EFBFBD><E3B2BB> //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>϶<EFBFBD><CFB6><EFBFBD><EFBFBD><EFBFBD><E3B2BB>
query = select.RightJoin("\"TESTTYPEINFO\" b on b.\"GUID\" = a.\"TypeGuid\""); query = select.RightJoin("\"TESTTYPEINFO\" b on b.\"GUID\" = a.\"TYPEGUID\"");
sql = query.ToSql().Replace("\r\n", ""); 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); 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.ToList();
query = select.RightJoin("\"TESTTYPEINFO\" b on b.\"GUID\" = a.\"TypeGuid\" and b.\"NAME\" = :bname", new { bname = "xxx" }); query = select.RightJoin("\"TESTTYPEINFO\" b on b.\"GUID\" = a.\"TYPEGUID\" and b.\"NAME\" = :bname", new { bname = "xxx" });
sql = query.ToSql().Replace("\r\n", ""); 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); 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(); query.ToList();
} }
@ -330,48 +402,48 @@ namespace FreeSql.Tests.Oracle {
//<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> //<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 query = select.Where(a => a.Id == 10);
var sql = query.ToSql().Replace("\r\n", ""); 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); 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.ToList();
query = select.Where(a => a.Id == 10 && a.Id > 10 || a.Clicks > 100); query = select.Where(a => a.Id == 10 && a.Id > 10 || a.Clicks > 100);
sql = query.ToSql().Replace("\r\n", ""); 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); 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.ToList();
query = select.Where(a => a.Id == 10).Where(a => a.Clicks > 100); query = select.Where(a => a.Id == 10).Where(a => a.Clicks > 100);
sql = query.ToSql().Replace("\r\n", ""); 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); 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.ToList();
query = select.Where(a => a.Type.Name == "typeTitle"); query = select.Where(a => a.Type.Name == "typeTitle");
sql = query.ToSql().Replace("\r\n", ""); sql = query.ToSql().Replace("\r\n", "");
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TypeGuid\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a, \"TESTTYPEINFO\" a__Type WHERE (a__Type.\"NAME\" = 'typeTitle')", sql); 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.ToList();
query = select.Where(a => a.Type.Name == "typeTitle" && a.Type.Guid == a.TypeGuid); query = select.Where(a => a.Type.Name == "typeTitle" && a.Type.Guid == a.TypeGuid);
sql = query.ToSql().Replace("\r\n", ""); sql = query.ToSql().Replace("\r\n", "");
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TypeGuid\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a, \"TESTTYPEINFO\" a__Type WHERE (a__Type.\"NAME\" = 'typeTitle' AND a__Type.\"GUID\" = a.\"TypeGuid\")", sql); 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.ToList();
query = select.Where(a => a.Type.Parent.Name == "tparent"); query = select.Where(a => a.Type.Parent.Name == "tparent");
sql = query.ToSql().Replace("\r\n", ""); sql = query.ToSql().Replace("\r\n", "");
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TypeGuid\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a, \"TESTTYPEINFO\" a__Type, \"TESTTYPEPARENTINFO\" a__Type__Parent WHERE (a__Type__Parent.\"NAME\" = 'tparent')", sql); 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(); query.ToList();
//<2F><><EFBFBD>û<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ԣ<EFBFBD><D4A3>򵥶<EFBFBD><F2B5A5B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD> //<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"); query = select.Where<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid && b.Name == "typeTitle");
sql = query.ToSql().Replace("\r\n", ""); 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); 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.ToList();
query = select.Where<TestTypeInfo>((a, b) => b.Name == "typeTitle" && b.Guid == a.TypeGuid); query = select.Where<TestTypeInfo>((a, b) => b.Name == "typeTitle" && b.Guid == a.TypeGuid);
sql = query.ToSql().Replace("\r\n", ""); 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); 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.ToList();
query = select.Where<TestTypeInfo, TestTypeParentInfo>((a, b, c) => c.Name == "tparent"); query = select.Where<TestTypeInfo, TestTypeParentInfo>((a, b, c) => c.Name == "tparent");
sql = query.ToSql().Replace("\r\n", ""); 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); 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(); query.ToList();
//<2F><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB> From <20><>Ķ<EFBFBD><C4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD> //<2F><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB> From <20><>Ķ<EFBFBD><C4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
@ -379,13 +451,13 @@ namespace FreeSql.Tests.Oracle {
.Where(a => a.Id == 10 && c.Name == "xxx") .Where(a => a.Id == 10 && c.Name == "xxx")
.Where(a => b.ParentId == 20)); .Where(a => b.ParentId == 20));
sql = query2.ToSql().Replace("\r\n", ""); sql = query2.ToSql().Replace("\r\n", "");
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TypeGuid\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a, \"TESTTYPEPARENTINFO\" c, \"TESTTYPEINFO\" b WHERE (a.\"ID\" = 10 AND c.\"NAME\" = 'xxx') AND (b.\"PARENTID\" = 20)", sql); 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(); query2.ToList();
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>϶<EFBFBD><CFB6><EFBFBD><EFBFBD><EFBFBD><E3B2BB> //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>϶<EFBFBD><CFB6><EFBFBD><EFBFBD><EFBFBD><E3B2BB>
query = select.Where("a.\"CLICKS\" > 100 and a.\"ID\" = :id", new { id = 10 }); query = select.Where("a.\"CLICKS\" > 100 and a.\"ID\" = :id", new { id = 10 });
sql = query.ToSql().Replace("\r\n", ""); 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); 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(); query.ToList();
} }
[Fact] [Fact]
@ -393,32 +465,32 @@ namespace FreeSql.Tests.Oracle {
//<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> //<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 query = select.WhereIf(true, a => a.Id == 10);
var sql = query.ToSql().Replace("\r\n", ""); 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); 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.ToList();
query = select.WhereIf(true, a => a.Id == 10 && a.Id > 10 || a.Clicks > 100); query = select.WhereIf(true, a => a.Id == 10 && a.Id > 10 || a.Clicks > 100);
sql = query.ToSql().Replace("\r\n", ""); 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); 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.ToList();
query = select.WhereIf(true, a => a.Id == 10).WhereIf(true, a => a.Clicks > 100); query = select.WhereIf(true, a => a.Id == 10).WhereIf(true, a => a.Clicks > 100);
sql = query.ToSql().Replace("\r\n", ""); 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); 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.ToList();
query = select.WhereIf(true, a => a.Type.Name == "typeTitle"); query = select.WhereIf(true, a => a.Type.Name == "typeTitle");
sql = query.ToSql().Replace("\r\n", ""); sql = query.ToSql().Replace("\r\n", "");
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TypeGuid\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a, \"TESTTYPEINFO\" a__Type WHERE (a__Type.\"NAME\" = 'typeTitle')", sql); 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.ToList();
query = select.WhereIf(true, a => a.Type.Name == "typeTitle" && a.Type.Guid == a.TypeGuid); query = select.WhereIf(true, a => a.Type.Name == "typeTitle" && a.Type.Guid == a.TypeGuid);
sql = query.ToSql().Replace("\r\n", ""); sql = query.ToSql().Replace("\r\n", "");
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TypeGuid\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a, \"TESTTYPEINFO\" a__Type WHERE (a__Type.\"NAME\" = 'typeTitle' AND a__Type.\"GUID\" = a.\"TypeGuid\")", sql); 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.ToList();
query = select.WhereIf(true, a => a.Type.Parent.Name == "tparent"); query = select.WhereIf(true, a => a.Type.Parent.Name == "tparent");
sql = query.ToSql().Replace("\r\n", ""); sql = query.ToSql().Replace("\r\n", "");
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TypeGuid\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a, \"TESTTYPEINFO\" a__Type, \"TESTTYPEPARENTINFO\" a__Type__Parent WHERE (a__Type__Parent.\"NAME\" = 'tparent')", sql); 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(); query.ToList();
//<2F><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB> From <20><>Ķ<EFBFBD><C4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD> //<2F><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB> From <20><>Ķ<EFBFBD><C4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
@ -426,13 +498,13 @@ namespace FreeSql.Tests.Oracle {
.WhereIf(true, a => a.Id == 10 && c.Name == "xxx") .WhereIf(true, a => a.Id == 10 && c.Name == "xxx")
.WhereIf(true, a => b.ParentId == 20)); .WhereIf(true, a => b.ParentId == 20));
sql = query2.ToSql().Replace("\r\n", ""); sql = query2.ToSql().Replace("\r\n", "");
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TypeGuid\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a, \"TESTTYPEPARENTINFO\" c, \"TESTTYPEINFO\" b WHERE (a.\"ID\" = 10 AND c.\"NAME\" = 'xxx') AND (b.\"PARENTID\" = 20)", sql); 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(); query2.ToList();
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>϶<EFBFBD><CFB6><EFBFBD><EFBFBD><EFBFBD><E3B2BB> //<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 }); query = select.WhereIf(true, "a.\"CLICKS\" > 100 and a.\"ID\" = :id", new { id = 10 });
sql = query.ToSql().Replace("\r\n", ""); 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); 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(); query.ToList();
// ==========================================WhereIf(false) // ==========================================WhereIf(false)
@ -440,32 +512,32 @@ namespace FreeSql.Tests.Oracle {
//<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> //<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); query = select.WhereIf(false, a => a.Id == 10);
sql = query.ToSql().Replace("\r\n", ""); sql = query.ToSql().Replace("\r\n", "");
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TypeGuid\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a", sql); Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a", sql);
query.ToList(); query.ToList();
query = select.WhereIf(false, a => a.Id == 10 && a.Id > 10 || a.Clicks > 100); query = select.WhereIf(false, a => a.Id == 10 && a.Id > 10 || a.Clicks > 100);
sql = query.ToSql().Replace("\r\n", ""); sql = query.ToSql().Replace("\r\n", "");
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TypeGuid\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a", sql); Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a", sql);
query.ToList(); query.ToList();
query = select.WhereIf(false, a => a.Id == 10).WhereIf(false, a => a.Clicks > 100); query = select.WhereIf(false, a => a.Id == 10).WhereIf(false, a => a.Clicks > 100);
sql = query.ToSql().Replace("\r\n", ""); sql = query.ToSql().Replace("\r\n", "");
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TypeGuid\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a", sql); Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a", sql);
query.ToList(); query.ToList();
query = select.WhereIf(false, a => a.Type.Name == "typeTitle"); query = select.WhereIf(false, a => a.Type.Name == "typeTitle");
sql = query.ToSql().Replace("\r\n", ""); sql = query.ToSql().Replace("\r\n", "");
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TypeGuid\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a", sql); Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a", sql);
query.ToList(); query.ToList();
query = select.WhereIf(false, a => a.Type.Name == "typeTitle" && a.Type.Guid == a.TypeGuid); query = select.WhereIf(false, a => a.Type.Name == "typeTitle" && a.Type.Guid == a.TypeGuid);
sql = query.ToSql().Replace("\r\n", ""); sql = query.ToSql().Replace("\r\n", "");
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TypeGuid\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a", sql); Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a", sql);
query.ToList(); query.ToList();
query = select.WhereIf(false, a => a.Type.Parent.Name == "tparent"); query = select.WhereIf(false, a => a.Type.Parent.Name == "tparent");
sql = query.ToSql().Replace("\r\n", ""); sql = query.ToSql().Replace("\r\n", "");
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TypeGuid\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a", sql); Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a", sql);
query.ToList(); query.ToList();
//<2F><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB> From <20><>Ķ<EFBFBD><C4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD> //<2F><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB> From <20><>Ķ<EFBFBD><C4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
@ -473,13 +545,13 @@ namespace FreeSql.Tests.Oracle {
.WhereIf(false, a => a.Id == 10 && c.Name == "xxx") .WhereIf(false, a => a.Id == 10 && c.Name == "xxx")
.WhereIf(false, a => b.ParentId == 20)); .WhereIf(false, a => b.ParentId == 20));
sql = query2.ToSql().Replace("\r\n", ""); sql = query2.ToSql().Replace("\r\n", "");
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TypeGuid\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a", sql); Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a, \"TESTTYPEINFO\" b, \"TESTTYPEPARENTINFO\" c", sql);
query2.ToList(); query2.ToList();
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>϶<EFBFBD><CFB6><EFBFBD><EFBFBD><EFBFBD><E3B2BB> //<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 }); query = select.WhereIf(false, "a.\"CLICKS\" > 100 and a.\"ID\" = :id", new { id = 10 });
sql = query.ToSql().Replace("\r\n", ""); sql = query.ToSql().Replace("\r\n", "");
Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TypeGuid\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a", sql); Assert.Equal("SELECT a.\"ID\", a.\"CLICKS\", a.\"TYPEGUID\", a.\"TITLE\", a.\"CREATETIME\" FROM \"TB_TOPIC22\" a", sql);
query.ToList(); query.ToList();
} }
[Fact] [Fact]
@ -519,15 +591,25 @@ namespace FreeSql.Tests.Oracle {
} }
[Fact] [Fact]
public void OrderBy() { public void OrderBy() {
var sql = select.Offset(10).OrderBy(a => new Random().NextDouble()).ToList();
} }
[Fact] [Fact]
public void Skip_Offset() { public void Skip_Offset() {
var sql = select.Offset(10).ToList();
} }
[Fact] [Fact]
public void Take_Limit() { public void Take_Limit() {
var sql = select.Limit(10).ToList();
} }
[Fact] [Fact]
public void Page() { 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] [Fact]
public void Sum() { public void Sum() {
@ -556,57 +638,57 @@ namespace FreeSql.Tests.Oracle {
//<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> //<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 query = select.LeftJoin(a => a.Type.Guid == a.TypeGuid).AsTable(tableRule);
var sql = query.ToSql().Replace("\r\n", ""); 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); 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); query = select.LeftJoin(a => a.Type.Guid == a.TypeGuid && a.Type.Name == "xxx").AsTable(tableRule);
sql = query.ToSql().Replace("\r\n", ""); 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); 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); 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", ""); 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 \"TESTTYPEPARENTINFOAsTable\" a__Type__Parent ON 1 = 1 LEFT JOIN \"TESTTYPEINFOAsTable2\" a__Type ON a__Type.\"GUID\" = a.\"TypeGuid\" AND a__Type.\"NAME\" = 'xxx' WHERE (a__Type__Parent.\"ID\" = 10)", sql); 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> //<2F><><EFBFBD>û<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
query = select.LeftJoin<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid).AsTable(tableRule); query = select.LeftJoin<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid).AsTable(tableRule);
sql = query.ToSql().Replace("\r\n", ""); 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); 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); query = select.LeftJoin<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid && b.Name == "xxx").AsTable(tableRule);
sql = query.ToSql().Replace("\r\n", ""); 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); 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, b) => b.Guid == a.TypeGuid && b.Name == "xxx").Where(a => a.Type.Parent.Id == 10).AsTable(tableRule); 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", ""); 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 \"TESTTYPEPARENTINFOAsTable\" b__Parent ON 1 = 1 LEFT JOIN \"TESTTYPEINFOAsTable2\" b ON b.\"GUID\" = a.\"TypeGuid\" AND b.\"NAME\" = 'xxx' WHERE (b__Parent.\"ID\" = 10)", sql); 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> //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
query = select query = select
.LeftJoin(a => a.Type.Guid == a.TypeGuid) .LeftJoin(a => a.Type.Guid == a.TypeGuid)
.LeftJoin(a => a.Type.Parent.Id == a.Type.ParentId).AsTable(tableRule); .LeftJoin(a => a.Type.Parent.Id == a.Type.ParentId).AsTable(tableRule);
sql = query.ToSql().Replace("\r\n", ""); 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); 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 query = select
.LeftJoin<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid) .LeftJoin<TestTypeInfo>((a, a__Type) => a__Type.Guid == a.TypeGuid)
.LeftJoin<TestTypeParentInfo>((a, c) => c.Id == a.Type.ParentId).AsTable(tableRule); .LeftJoin<TestTypeParentInfo>((a, c) => c.Id == a.Type.ParentId).AsTable(tableRule);
sql = query.ToSql().Replace("\r\n", ""); 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\" LEFT JOIN \"TESTTYPEPARENTINFOAsTable\" c ON c.\"ID\" = b.\"PARENTID\"", sql); 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>ϵ //<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 var query2 = select.From<TestTypeInfo, TestTypeParentInfo>((s, b, c) => s
.LeftJoin(a => a.TypeGuid == b.Guid) .LeftJoin(a => a.TypeGuid == b.Guid)
.LeftJoin(a => b.ParentId == c.Id)).AsTable(tableRule); .LeftJoin(a => b.ParentId == c.Id)).AsTable(tableRule);
sql = query2.ToSql().Replace("\r\n", ""); 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); 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> //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>϶<EFBFBD><CFB6><EFBFBD><EFBFBD><EFBFBD><E3B2BB>
query = select.LeftJoin("\"TESTTYPEINFO\" b on b.\"GUID\" = a.\"TypeGuid\"").AsTable(tableRule); query = select.LeftJoin("\"TESTTYPEINFO\" b on b.\"GUID\" = a.\"TYPEGUID\"").AsTable(tableRule);
sql = query.ToSql().Replace("\r\n", ""); 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); 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); 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", ""); 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); 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);
} }
} }
} }

View File

@ -21,6 +21,7 @@ namespace FreeSql.Tests.OracleExpression {
} }
[Table(Name = "TestTypeInfo333")] [Table(Name = "TestTypeInfo333")]
class TestTypeInfo { class TestTypeInfo {
[Column(IsIdentity = true)]
public int Guid { get; set; } public int Guid { get; set; }
public int ParentId { get; set; } public int ParentId { get; set; }
public TestTypeParentInfo Parent { get; set; } public TestTypeParentInfo Parent { get; set; }

View File

@ -20,6 +20,7 @@ namespace FreeSql.Tests.OracleExpression {
public DateTime CreateTime { get; set; } public DateTime CreateTime { get; set; }
} }
class TestTypeInfo { class TestTypeInfo {
[Column(IsIdentity = true)]
public int Guid { get; set; } public int Guid { get; set; }
public int ParentId { get; set; } public int ParentId { get; set; }
public TestTypeParentInfo Parent { get; set; } public TestTypeParentInfo Parent { get; set; }

View File

@ -20,6 +20,7 @@ namespace FreeSql.Tests.PostgreSQL {
public DateTime CreateTime { get; set; } public DateTime CreateTime { get; set; }
} }
class TestTypeInfo { class TestTypeInfo {
[Column(IsIdentity = true)]
public int Guid { get; set; } public int Guid { get; set; }
public int ParentId { get; set; } public int ParentId { get; set; }
public TestTypeParentInfo Parent { get; set; } public TestTypeParentInfo Parent { get; set; }
@ -31,6 +32,77 @@ namespace FreeSql.Tests.PostgreSQL {
public List<TestTypeInfo> Types { get; set; } public List<TestTypeInfo> Types { 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.pgsql.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.pgsql.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.pgsql.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.pgsql.Select<Tag>().Where(a => a.Parent.Name == "xxx")
.LeftJoin(a => a.Parent_id == a.Parent.Id)
.ToSql();
var songs = g.pgsql.Select<Song>().Limit(10).ToList();
}
[Fact] [Fact]
public void ToDataTable() { public void ToDataTable() {
@ -184,7 +256,7 @@ namespace FreeSql.Tests.PostgreSQL {
.LeftJoin(a => b.ParentId == c.Id) .LeftJoin(a => b.ParentId == c.Id)
); );
var sql = query2.ToSql().Replace("\r\n", ""); 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_topic\" a LEFT JOIN \"testtypeinfo\" b ON a.\"TypeGuid\" = b.\"guid\" LEFT JOIN \"testtypeparentinfo\" c ON b.\"parentid\" = c.\"id\"", sql); Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"typeguid\", b.\"guid\", b.\"parentid\", b.\"name\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a LEFT JOIN \"testtypeinfo\" b ON a.\"typeguid\" = b.\"guid\" LEFT JOIN \"testtypeparentinfo\" c ON b.\"parentid\" = c.\"id\"", sql);
query2.ToList(); query2.ToList();
} }
[Fact] [Fact]
@ -192,33 +264,33 @@ namespace FreeSql.Tests.PostgreSQL {
//<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> //<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 query = select.LeftJoin(a => a.Type.Guid == a.TypeGuid);
var sql = query.ToSql().Replace("\r\n", ""); 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_topic\" a LEFT JOIN \"testtypeinfo\" a__Type ON a__Type.\"guid\" = a.\"TypeGuid\"", sql); Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"typeguid\", a__Type.\"guid\", a__Type.\"parentid\", a__Type.\"name\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a LEFT JOIN \"testtypeinfo\" a__Type ON a__Type.\"guid\" = a.\"typeguid\"", sql);
query.ToList(); query.ToList();
query = select.LeftJoin(a => a.Type.Guid == a.TypeGuid && a.Type.Name == "xxx"); query = select.LeftJoin(a => a.Type.Guid == a.TypeGuid && a.Type.Name == "xxx");
sql = query.ToSql().Replace("\r\n", ""); 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_topic\" a LEFT JOIN \"testtypeinfo\" a__Type ON a__Type.\"guid\" = a.\"TypeGuid\" AND a__Type.\"name\" = 'xxx'", sql); Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"typeguid\", a__Type.\"guid\", a__Type.\"parentid\", a__Type.\"name\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a LEFT JOIN \"testtypeinfo\" a__Type ON a__Type.\"guid\" = a.\"typeguid\" AND a__Type.\"name\" = 'xxx'", sql);
query.ToList(); query.ToList();
query = select.LeftJoin(a => a.Type.Guid == a.TypeGuid && a.Type.Name == "xxx").Where(a => a.Type.Parent.Id == 10); 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", ""); 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_topic\" a LEFT JOIN \"testtypeparentinfo\" a__Type__Parent ON 1 = 1 LEFT JOIN \"testtypeinfo\" a__Type ON a__Type.\"guid\" = a.\"TypeGuid\" AND a__Type.\"name\" = 'xxx' WHERE (a__Type__Parent.\"id\" = 10)", sql); Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"typeguid\", a__Type.\"guid\", a__Type.\"parentid\", a__Type.\"name\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" 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(); query.ToList();
//<2F><><EFBFBD>û<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> //<2F><><EFBFBD>û<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
query = select.LeftJoin<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid); query = select.LeftJoin<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid);
sql = query.ToSql().Replace("\r\n", ""); 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_topic\" a LEFT JOIN \"testtypeinfo\" b ON b.\"guid\" = a.\"TypeGuid\"", sql); Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"typeguid\", b.\"guid\", b.\"parentid\", b.\"name\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a LEFT JOIN \"testtypeinfo\" b ON b.\"guid\" = a.\"typeguid\"", sql);
query.ToList(); query.ToList();
query = select.LeftJoin<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid && b.Name == "xxx"); query = select.LeftJoin<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid && b.Name == "xxx");
sql = query.ToSql().Replace("\r\n", ""); 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_topic\" a LEFT JOIN \"testtypeinfo\" b ON b.\"guid\" = a.\"TypeGuid\" AND b.\"name\" = 'xxx'", sql); Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"typeguid\", b.\"guid\", b.\"parentid\", b.\"name\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a LEFT JOIN \"testtypeinfo\" b ON b.\"guid\" = a.\"typeguid\" AND b.\"name\" = 'xxx'", sql);
query.ToList(); query.ToList();
query = select.LeftJoin<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid && b.Name == "xxx").Where(a => a.Type.Parent.Id == 10); 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", ""); 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_topic\" a LEFT JOIN \"testtypeparentinfo\" b__Parent ON 1 = 1 LEFT JOIN \"testtypeinfo\" b ON b.\"guid\" = a.\"TypeGuid\" AND b.\"name\" = 'xxx' WHERE (b__Parent.\"id\" = 10)", sql); Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"typeguid\", a__Type.\"guid\", a__Type.\"parentid\", a__Type.\"name\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" 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(); query.ToList();
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
@ -226,14 +298,14 @@ namespace FreeSql.Tests.PostgreSQL {
.LeftJoin(a => a.Type.Guid == a.TypeGuid) .LeftJoin(a => a.Type.Guid == a.TypeGuid)
.LeftJoin(a => a.Type.Parent.Id == a.Type.ParentId); .LeftJoin(a => a.Type.Parent.Id == a.Type.ParentId);
sql = query.ToSql().Replace("\r\n", ""); 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_topic\" 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); Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"typeguid\", a__Type.\"guid\", a__Type.\"parentid\", a__Type.\"name\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" 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.ToList();
query = select query = select
.LeftJoin<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid) .LeftJoin<TestTypeInfo>((a, a__Type) => a__Type.Guid == a.TypeGuid)
.LeftJoin<TestTypeParentInfo>((a, c) => c.Id == a.Type.ParentId); .LeftJoin<TestTypeParentInfo>((a, c) => c.Id == a.Type.ParentId);
sql = query.ToSql().Replace("\r\n", ""); 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_topic\" a LEFT JOIN \"testtypeinfo\" b ON b.\"guid\" = a.\"TypeGuid\" LEFT JOIN \"testtypeparentinfo\" c ON c.\"id\" = b.\"parentid\"", sql); Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"typeguid\", a__Type.\"guid\", a__Type.\"parentid\", a__Type.\"name\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" 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(); query.ToList();
//<2F><><EFBFBD>û<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>b<EFBFBD><62>c<EFBFBD><63><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵ //<2F><><EFBFBD>û<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>b<EFBFBD><62>c<EFBFBD><63><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵ
@ -241,18 +313,18 @@ namespace FreeSql.Tests.PostgreSQL {
.LeftJoin(a => a.TypeGuid == b.Guid) .LeftJoin(a => a.TypeGuid == b.Guid)
.LeftJoin(a => b.ParentId == c.Id)); .LeftJoin(a => b.ParentId == c.Id));
sql = query2.ToSql().Replace("\r\n", ""); 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_topic\" a LEFT JOIN \"testtypeinfo\" b ON a.\"TypeGuid\" = b.\"guid\" LEFT JOIN \"testtypeparentinfo\" c ON b.\"parentid\" = c.\"id\"", sql); Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"typeguid\", b.\"guid\", b.\"parentid\", b.\"name\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a LEFT JOIN \"testtypeinfo\" b ON a.\"typeguid\" = b.\"guid\" LEFT JOIN \"testtypeparentinfo\" c ON b.\"parentid\" = c.\"id\"", sql);
query2.ToList(); query2.ToList();
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>϶<EFBFBD><CFB6><EFBFBD><EFBFBD><EFBFBD><E3B2BB> //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>϶<EFBFBD><CFB6><EFBFBD><EFBFBD><EFBFBD><E3B2BB>
query = select.LeftJoin("\"testtypeinfo\" b on b.\"guid\" = a.\"TypeGuid\""); query = select.LeftJoin("\"testtypeinfo\" b on b.\"guid\" = a.\"typeguid\"");
sql = query.ToSql().Replace("\r\n", ""); sql = query.ToSql().Replace("\r\n", "");
Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"TypeGuid\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a LEFT JOIN \"testtypeinfo\" b on b.\"guid\" = a.\"TypeGuid\"", sql); Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"typeguid\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a LEFT JOIN \"testtypeinfo\" b on b.\"guid\" = a.\"typeguid\"", sql);
query.ToList(); query.ToList();
query = select.LeftJoin("\"testtypeinfo\" b on b.\"guid\" = a.\"TypeGuid\" and b.\"name\" = @bname", new { bname = "xxx" }); query = select.LeftJoin("\"testtypeinfo\" b on b.\"guid\" = a.\"typeguid\" and b.\"name\" = @bname", new { bname = "xxx" });
sql = query.ToSql().Replace("\r\n", ""); sql = query.ToSql().Replace("\r\n", "");
Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"TypeGuid\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a LEFT JOIN \"testtypeinfo\" b on b.\"guid\" = a.\"TypeGuid\" and b.\"name\" = @bname", sql); Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"typeguid\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a LEFT JOIN \"testtypeinfo\" b on b.\"guid\" = a.\"typeguid\" and b.\"name\" = @bname", sql);
query.ToList(); query.ToList();
} }
[Fact] [Fact]
@ -260,33 +332,33 @@ namespace FreeSql.Tests.PostgreSQL {
//<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> //<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 query = select.InnerJoin(a => a.Type.Guid == a.TypeGuid);
var sql = query.ToSql().Replace("\r\n", ""); 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_topic\" a INNER JOIN \"testtypeinfo\" a__Type ON a__Type.\"guid\" = a.\"TypeGuid\"", sql); Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"typeguid\", a__Type.\"guid\", a__Type.\"parentid\", a__Type.\"name\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a INNER JOIN \"testtypeinfo\" a__Type ON a__Type.\"guid\" = a.\"typeguid\"", sql);
query.ToList(); query.ToList();
query = select.InnerJoin(a => a.Type.Guid == a.TypeGuid && a.Type.Name == "xxx"); query = select.InnerJoin(a => a.Type.Guid == a.TypeGuid && a.Type.Name == "xxx");
sql = query.ToSql().Replace("\r\n", ""); 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_topic\" a INNER JOIN \"testtypeinfo\" a__Type ON a__Type.\"guid\" = a.\"TypeGuid\" AND a__Type.\"name\" = 'xxx'", sql); Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"typeguid\", a__Type.\"guid\", a__Type.\"parentid\", a__Type.\"name\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a INNER JOIN \"testtypeinfo\" a__Type ON a__Type.\"guid\" = a.\"typeguid\" AND a__Type.\"name\" = 'xxx'", sql);
query.ToList(); query.ToList();
query = select.InnerJoin(a => a.Type.Guid == a.TypeGuid && a.Type.Name == "xxx").Where(a => a.Type.Parent.Id == 10); 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", ""); 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_topic\" a LEFT JOIN \"testtypeparentinfo\" a__Type__Parent ON 1 = 1 INNER JOIN \"testtypeinfo\" a__Type ON a__Type.\"guid\" = a.\"TypeGuid\" AND a__Type.\"name\" = 'xxx' WHERE (a__Type__Parent.\"id\" = 10)", sql); Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"typeguid\", a__Type.\"guid\", a__Type.\"parentid\", a__Type.\"name\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" 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(); query.ToList();
//<2F><><EFBFBD>û<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> //<2F><><EFBFBD>û<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
query = select.InnerJoin<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid); query = select.InnerJoin<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid);
sql = query.ToSql().Replace("\r\n", ""); 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_topic\" a INNER JOIN \"testtypeinfo\" b ON b.\"guid\" = a.\"TypeGuid\"", sql); Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"typeguid\", b.\"guid\", b.\"parentid\", b.\"name\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a INNER JOIN \"testtypeinfo\" b ON b.\"guid\" = a.\"typeguid\"", sql);
query.ToList(); query.ToList();
query = select.InnerJoin<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid && b.Name == "xxx"); query = select.InnerJoin<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid && b.Name == "xxx");
sql = query.ToSql().Replace("\r\n", ""); 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_topic\" a INNER JOIN \"testtypeinfo\" b ON b.\"guid\" = a.\"TypeGuid\" AND b.\"name\" = 'xxx'", sql); Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"typeguid\", b.\"guid\", b.\"parentid\", b.\"name\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a INNER JOIN \"testtypeinfo\" b ON b.\"guid\" = a.\"typeguid\" AND b.\"name\" = 'xxx'", sql);
query.ToList(); query.ToList();
query = select.InnerJoin<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid && b.Name == "xxx").Where(a => a.Type.Parent.Id == 10); 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", ""); 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_topic\" a LEFT JOIN \"testtypeparentinfo\" b__Parent ON 1 = 1 INNER JOIN \"testtypeinfo\" b ON b.\"guid\" = a.\"TypeGuid\" AND b.\"name\" = 'xxx' WHERE (b__Parent.\"id\" = 10)", sql); Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"typeguid\", a__Type.\"guid\", a__Type.\"parentid\", a__Type.\"name\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" 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(); query.ToList();
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
@ -294,14 +366,14 @@ namespace FreeSql.Tests.PostgreSQL {
.InnerJoin(a => a.Type.Guid == a.TypeGuid) .InnerJoin(a => a.Type.Guid == a.TypeGuid)
.InnerJoin(a => a.Type.Parent.Id == a.Type.ParentId); .InnerJoin(a => a.Type.Parent.Id == a.Type.ParentId);
sql = query.ToSql().Replace("\r\n", ""); 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_topic\" 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); Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"typeguid\", a__Type.\"guid\", a__Type.\"parentid\", a__Type.\"name\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" 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.ToList();
query = select query = select
.InnerJoin<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid) .InnerJoin<TestTypeInfo>((a, a__Type) => a__Type.Guid == a.TypeGuid)
.InnerJoin<TestTypeParentInfo>((a, c) => c.Id == a.Type.ParentId); .InnerJoin<TestTypeParentInfo>((a, c) => c.Id == a.Type.ParentId);
sql = query.ToSql().Replace("\r\n", ""); 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_topic\" a INNER JOIN \"testtypeinfo\" b ON b.\"guid\" = a.\"TypeGuid\" INNER JOIN \"testtypeparentinfo\" c ON c.\"id\" = b.\"parentid\"", sql); Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"typeguid\", a__Type.\"guid\", a__Type.\"parentid\", a__Type.\"name\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" 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(); query.ToList();
//<2F><><EFBFBD>û<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>b<EFBFBD><62>c<EFBFBD><63><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵ //<2F><><EFBFBD>û<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>b<EFBFBD><62>c<EFBFBD><63><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵ
@ -309,18 +381,18 @@ namespace FreeSql.Tests.PostgreSQL {
.InnerJoin(a => a.TypeGuid == b.Guid) .InnerJoin(a => a.TypeGuid == b.Guid)
.InnerJoin(a => b.ParentId == c.Id)); .InnerJoin(a => b.ParentId == c.Id));
sql = query2.ToSql().Replace("\r\n", ""); 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_topic\" a INNER JOIN \"testtypeinfo\" b ON a.\"TypeGuid\" = b.\"guid\" INNER JOIN \"testtypeparentinfo\" c ON b.\"parentid\" = c.\"id\"", sql); Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"typeguid\", b.\"guid\", b.\"parentid\", b.\"name\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a INNER JOIN \"testtypeinfo\" b ON a.\"typeguid\" = b.\"guid\" INNER JOIN \"testtypeparentinfo\" c ON b.\"parentid\" = c.\"id\"", sql);
query2.ToList(); query2.ToList();
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>϶<EFBFBD><CFB6><EFBFBD><EFBFBD><EFBFBD><E3B2BB> //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>϶<EFBFBD><CFB6><EFBFBD><EFBFBD><EFBFBD><E3B2BB>
query = select.InnerJoin("\"testtypeinfo\" b on b.\"guid\" = a.\"TypeGuid\""); query = select.InnerJoin("\"testtypeinfo\" b on b.\"guid\" = a.\"typeguid\"");
sql = query.ToSql().Replace("\r\n", ""); sql = query.ToSql().Replace("\r\n", "");
Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"TypeGuid\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a INNER JOIN \"testtypeinfo\" b on b.\"guid\" = a.\"TypeGuid\"", sql); Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"typeguid\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a INNER JOIN \"testtypeinfo\" b on b.\"guid\" = a.\"typeguid\"", sql);
query.ToList(); query.ToList();
query = select.InnerJoin("\"testtypeinfo\" b on b.\"guid\" = a.\"TypeGuid\" and b.\"name\" = @bname", new { bname = "xxx" }); query = select.InnerJoin("\"testtypeinfo\" b on b.\"guid\" = a.\"typeguid\" and b.\"name\" = @bname", new { bname = "xxx" });
sql = query.ToSql().Replace("\r\n", ""); sql = query.ToSql().Replace("\r\n", "");
Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"TypeGuid\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a INNER JOIN \"testtypeinfo\" b on b.\"guid\" = a.\"TypeGuid\" and b.\"name\" = @bname", sql); Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"typeguid\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a INNER JOIN \"testtypeinfo\" b on b.\"guid\" = a.\"typeguid\" and b.\"name\" = @bname", sql);
query.ToList(); query.ToList();
} }
@ -329,33 +401,33 @@ namespace FreeSql.Tests.PostgreSQL {
//<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> //<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 query = select.RightJoin(a => a.Type.Guid == a.TypeGuid);
var sql = query.ToSql().Replace("\r\n", ""); 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_topic\" a RIGHT JOIN \"testtypeinfo\" a__Type ON a__Type.\"guid\" = a.\"TypeGuid\"", sql); Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"typeguid\", a__Type.\"guid\", a__Type.\"parentid\", a__Type.\"name\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a RIGHT JOIN \"testtypeinfo\" a__Type ON a__Type.\"guid\" = a.\"typeguid\"", sql);
query.ToList(); query.ToList();
query = select.RightJoin(a => a.Type.Guid == a.TypeGuid && a.Type.Name == "xxx"); query = select.RightJoin(a => a.Type.Guid == a.TypeGuid && a.Type.Name == "xxx");
sql = query.ToSql().Replace("\r\n", ""); 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_topic\" a RIGHT JOIN \"testtypeinfo\" a__Type ON a__Type.\"guid\" = a.\"TypeGuid\" AND a__Type.\"name\" = 'xxx'", sql); Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"typeguid\", a__Type.\"guid\", a__Type.\"parentid\", a__Type.\"name\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a RIGHT JOIN \"testtypeinfo\" a__Type ON a__Type.\"guid\" = a.\"typeguid\" AND a__Type.\"name\" = 'xxx'", sql);
query.ToList(); query.ToList();
query = select.RightJoin(a => a.Type.Guid == a.TypeGuid && a.Type.Name == "xxx").Where(a => a.Type.Parent.Id == 10); 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", ""); 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_topic\" a LEFT JOIN \"testtypeparentinfo\" a__Type__Parent ON 1 = 1 RIGHT JOIN \"testtypeinfo\" a__Type ON a__Type.\"guid\" = a.\"TypeGuid\" AND a__Type.\"name\" = 'xxx' WHERE (a__Type__Parent.\"id\" = 10)", sql); Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"typeguid\", a__Type.\"guid\", a__Type.\"parentid\", a__Type.\"name\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" 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(); query.ToList();
//<2F><><EFBFBD>û<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> //<2F><><EFBFBD>û<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
query = select.RightJoin<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid); query = select.RightJoin<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid);
sql = query.ToSql().Replace("\r\n", ""); 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_topic\" a RIGHT JOIN \"testtypeinfo\" b ON b.\"guid\" = a.\"TypeGuid\"", sql); Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"typeguid\", b.\"guid\", b.\"parentid\", b.\"name\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a RIGHT JOIN \"testtypeinfo\" b ON b.\"guid\" = a.\"typeguid\"", sql);
query.ToList(); query.ToList();
query = select.RightJoin<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid && b.Name == "xxx"); query = select.RightJoin<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid && b.Name == "xxx");
sql = query.ToSql().Replace("\r\n", ""); 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_topic\" a RIGHT JOIN \"testtypeinfo\" b ON b.\"guid\" = a.\"TypeGuid\" AND b.\"name\" = 'xxx'", sql); Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"typeguid\", b.\"guid\", b.\"parentid\", b.\"name\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a RIGHT JOIN \"testtypeinfo\" b ON b.\"guid\" = a.\"typeguid\" AND b.\"name\" = 'xxx'", sql);
query.ToList(); query.ToList();
query = select.RightJoin<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid && b.Name == "xxx").Where(a => a.Type.Parent.Id == 10); 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", ""); 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_topic\" a LEFT JOIN \"testtypeparentinfo\" b__Parent ON 1 = 1 RIGHT JOIN \"testtypeinfo\" b ON b.\"guid\" = a.\"TypeGuid\" AND b.\"name\" = 'xxx' WHERE (b__Parent.\"id\" = 10)", sql); Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"typeguid\", a__Type.\"guid\", a__Type.\"parentid\", a__Type.\"name\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" 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(); query.ToList();
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
@ -363,14 +435,14 @@ namespace FreeSql.Tests.PostgreSQL {
.RightJoin(a => a.Type.Guid == a.TypeGuid) .RightJoin(a => a.Type.Guid == a.TypeGuid)
.RightJoin(a => a.Type.Parent.Id == a.Type.ParentId); .RightJoin(a => a.Type.Parent.Id == a.Type.ParentId);
sql = query.ToSql().Replace("\r\n", ""); 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_topic\" 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); Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"typeguid\", a__Type.\"guid\", a__Type.\"parentid\", a__Type.\"name\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" 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.ToList();
query = select query = select
.RightJoin<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid) .RightJoin<TestTypeInfo>((a, a__Type) => a__Type.Guid == a.TypeGuid)
.RightJoin<TestTypeParentInfo>((a, c) => c.Id == a.Type.ParentId); .RightJoin<TestTypeParentInfo>((a, c) => c.Id == a.Type.ParentId);
sql = query.ToSql().Replace("\r\n", ""); 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_topic\" a RIGHT JOIN \"testtypeinfo\" b ON b.\"guid\" = a.\"TypeGuid\" RIGHT JOIN \"testtypeparentinfo\" c ON c.\"id\" = b.\"parentid\"", sql); Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"typeguid\", a__Type.\"guid\", a__Type.\"parentid\", a__Type.\"name\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" 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(); query.ToList();
//<2F><><EFBFBD>û<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>b<EFBFBD><62>c<EFBFBD><63><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵ //<2F><><EFBFBD>û<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>b<EFBFBD><62>c<EFBFBD><63><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵ
@ -378,18 +450,18 @@ namespace FreeSql.Tests.PostgreSQL {
.RightJoin(a => a.TypeGuid == b.Guid) .RightJoin(a => a.TypeGuid == b.Guid)
.RightJoin(a => b.ParentId == c.Id)); .RightJoin(a => b.ParentId == c.Id));
sql = query2.ToSql().Replace("\r\n", ""); 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_topic\" a RIGHT JOIN \"testtypeinfo\" b ON a.\"TypeGuid\" = b.\"guid\" RIGHT JOIN \"testtypeparentinfo\" c ON b.\"parentid\" = c.\"id\"", sql); Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"typeguid\", b.\"guid\", b.\"parentid\", b.\"name\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a RIGHT JOIN \"testtypeinfo\" b ON a.\"typeguid\" = b.\"guid\" RIGHT JOIN \"testtypeparentinfo\" c ON b.\"parentid\" = c.\"id\"", sql);
query2.ToList(); query2.ToList();
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>϶<EFBFBD><CFB6><EFBFBD><EFBFBD><EFBFBD><E3B2BB> //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>϶<EFBFBD><CFB6><EFBFBD><EFBFBD><EFBFBD><E3B2BB>
query = select.RightJoin("\"testtypeinfo\" b on b.\"guid\" = a.\"TypeGuid\""); query = select.RightJoin("\"testtypeinfo\" b on b.\"guid\" = a.\"typeguid\"");
sql = query.ToSql().Replace("\r\n", ""); sql = query.ToSql().Replace("\r\n", "");
Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"TypeGuid\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a RIGHT JOIN \"testtypeinfo\" b on b.\"guid\" = a.\"TypeGuid\"", sql); Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"typeguid\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a RIGHT JOIN \"testtypeinfo\" b on b.\"guid\" = a.\"typeguid\"", sql);
query.ToList(); query.ToList();
query = select.RightJoin("\"testtypeinfo\" b on b.\"guid\" = a.\"TypeGuid\" and b.\"name\" = @bname", new { bname = "xxx" }); query = select.RightJoin("\"testtypeinfo\" b on b.\"guid\" = a.\"typeguid\" and b.\"name\" = @bname", new { bname = "xxx" });
sql = query.ToSql().Replace("\r\n", ""); sql = query.ToSql().Replace("\r\n", "");
Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"TypeGuid\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a RIGHT JOIN \"testtypeinfo\" b on b.\"guid\" = a.\"TypeGuid\" and b.\"name\" = @bname", sql); Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"typeguid\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a RIGHT JOIN \"testtypeinfo\" b on b.\"guid\" = a.\"typeguid\" and b.\"name\" = @bname", sql);
query.ToList(); query.ToList();
} }
@ -398,48 +470,48 @@ namespace FreeSql.Tests.PostgreSQL {
//<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> //<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 query = select.Where(a => a.Id == 10);
var sql = query.ToSql().Replace("\r\n", ""); var sql = query.ToSql().Replace("\r\n", "");
Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"TypeGuid\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a WHERE (a.\"id\" = 10)", sql); Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"typeguid\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a WHERE (a.\"id\" = 10)", sql);
query.ToList(); query.ToList();
query = select.Where(a => a.Id == 10 && a.Id > 10 || a.Clicks > 100); query = select.Where(a => a.Id == 10 && a.Id > 10 || a.Clicks > 100);
sql = query.ToSql().Replace("\r\n", ""); sql = query.ToSql().Replace("\r\n", "");
Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"TypeGuid\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a WHERE (a.\"id\" = 10 AND a.\"id\" > 10 OR a.\"clicks\" > 100)", sql); Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"typeguid\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a WHERE (a.\"id\" = 10 AND a.\"id\" > 10 OR a.\"clicks\" > 100)", sql);
query.ToList(); query.ToList();
query = select.Where(a => a.Id == 10).Where(a => a.Clicks > 100); query = select.Where(a => a.Id == 10).Where(a => a.Clicks > 100);
sql = query.ToSql().Replace("\r\n", ""); sql = query.ToSql().Replace("\r\n", "");
Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"TypeGuid\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a WHERE (a.\"id\" = 10) AND (a.\"clicks\" > 100)", sql); Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"typeguid\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a WHERE (a.\"id\" = 10) AND (a.\"clicks\" > 100)", sql);
query.ToList(); query.ToList();
query = select.Where(a => a.Type.Name == "typeTitle"); query = select.Where(a => a.Type.Name == "typeTitle");
sql = query.ToSql().Replace("\r\n", ""); sql = query.ToSql().Replace("\r\n", "");
Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"TypeGuid\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a, \"testtypeinfo\" a__Type WHERE (a__Type.\"name\" = 'typeTitle')", sql); Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"typeguid\", a__Type.\"guid\", a__Type.\"parentid\", a__Type.\"name\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a LEFT JOIN \"testtypeinfo\" a__Type ON a__Type.\"guid\" = a.\"typeguid\" WHERE (a__Type.\"name\" = 'typeTitle')", sql);
query.ToList(); query.ToList();
query = select.Where(a => a.Type.Name == "typeTitle" && a.Type.Guid == a.TypeGuid); query = select.Where(a => a.Type.Name == "typeTitle" && a.Type.Guid == a.TypeGuid);
sql = query.ToSql().Replace("\r\n", ""); sql = query.ToSql().Replace("\r\n", "");
Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"TypeGuid\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a, \"testtypeinfo\" a__Type WHERE (a__Type.\"name\" = 'typeTitle' AND a__Type.\"guid\" = a.\"TypeGuid\")", sql); Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"typeguid\", a__Type.\"guid\", a__Type.\"parentid\", a__Type.\"name\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" 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.ToList();
query = select.Where(a => a.Type.Parent.Name == "tparent"); query = select.Where(a => a.Type.Parent.Name == "tparent");
sql = query.ToSql().Replace("\r\n", ""); sql = query.ToSql().Replace("\r\n", "");
Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"TypeGuid\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a, \"testtypeinfo\" a__Type, \"testtypeparentinfo\" a__Type__Parent WHERE (a__Type__Parent.\"name\" = 'tparent')", sql); Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"typeguid\", a__Type.\"guid\", a__Type.\"parentid\", a__Type.\"name\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" 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(); query.ToList();
//<2F><><EFBFBD>û<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ԣ<EFBFBD><D4A3>򵥶<EFBFBD><F2B5A5B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD> //<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"); query = select.Where<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid && b.Name == "typeTitle");
sql = query.ToSql().Replace("\r\n", ""); sql = query.ToSql().Replace("\r\n", "");
Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"TypeGuid\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a, \"testtypeinfo\" b WHERE (b.\"guid\" = a.\"TypeGuid\" AND b.\"name\" = 'typeTitle')", sql); Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"typeguid\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a, \"testtypeinfo\" b WHERE (b.\"guid\" = a.\"typeguid\" AND b.\"name\" = 'typeTitle')", sql);
query.ToList(); query.ToList();
query = select.Where<TestTypeInfo>((a, b) => b.Name == "typeTitle" && b.Guid == a.TypeGuid); query = select.Where<TestTypeInfo>((a, b) => b.Name == "typeTitle" && b.Guid == a.TypeGuid);
sql = query.ToSql().Replace("\r\n", ""); sql = query.ToSql().Replace("\r\n", "");
Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"TypeGuid\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a, \"testtypeinfo\" b WHERE (b.\"name\" = 'typeTitle' AND b.\"guid\" = a.\"TypeGuid\")", sql); Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"typeguid\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a, \"testtypeinfo\" b WHERE (b.\"name\" = 'typeTitle' AND b.\"guid\" = a.\"typeguid\")", sql);
query.ToList(); query.ToList();
query = select.Where<TestTypeInfo, TestTypeParentInfo>((a, b, c) => c.Name == "tparent"); query = select.Where<TestTypeInfo, TestTypeParentInfo>((a, b, c) => c.Name == "tparent");
sql = query.ToSql().Replace("\r\n", ""); sql = query.ToSql().Replace("\r\n", "");
Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"TypeGuid\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a, \"testtypeparentinfo\" c WHERE (c.\"name\" = 'tparent')", sql); Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"typeguid\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a, \"testtypeparentinfo\" c WHERE (c.\"name\" = 'tparent')", sql);
query.ToList(); query.ToList();
//<2F><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB> From <20><>Ķ<EFBFBD><C4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD> //<2F><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB> From <20><>Ķ<EFBFBD><C4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
@ -447,13 +519,13 @@ namespace FreeSql.Tests.PostgreSQL {
.Where(a => a.Id == 10 && c.Name == "xxx") .Where(a => a.Id == 10 && c.Name == "xxx")
.Where(a => b.ParentId == 20)); .Where(a => b.ParentId == 20));
sql = query2.ToSql().Replace("\r\n", ""); sql = query2.ToSql().Replace("\r\n", "");
Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"TypeGuid\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a, \"testtypeparentinfo\" c, \"testtypeinfo\" b WHERE (a.\"id\" = 10 AND c.\"name\" = 'xxx') AND (b.\"parentid\" = 20)", sql); Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"typeguid\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a, \"testtypeinfo\" b, \"testtypeparentinfo\" c WHERE (a.\"id\" = 10 AND c.\"name\" = 'xxx') AND (b.\"parentid\" = 20)", sql);
query2.ToList(); query2.ToList();
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>϶<EFBFBD><CFB6><EFBFBD><EFBFBD><EFBFBD><E3B2BB> //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>϶<EFBFBD><CFB6><EFBFBD><EFBFBD><EFBFBD><E3B2BB>
query = select.Where("a.\"clicks\" > 100 and a.\"id\" = @id", new { id = 10 }); query = select.Where("a.\"clicks\" > 100 and a.\"id\" = @id", new { id = 10 });
sql = query.ToSql().Replace("\r\n", ""); sql = query.ToSql().Replace("\r\n", "");
Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"TypeGuid\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a WHERE (a.\"clicks\" > 100 and a.\"id\" = @id)", sql); Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"typeguid\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a WHERE (a.\"clicks\" > 100 and a.\"id\" = @id)", sql);
query.ToList(); query.ToList();
} }
[Fact] [Fact]
@ -461,32 +533,32 @@ namespace FreeSql.Tests.PostgreSQL {
//<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> //<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 query = select.WhereIf(true, a => a.Id == 10);
var sql = query.ToSql().Replace("\r\n", ""); var sql = query.ToSql().Replace("\r\n", "");
Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"TypeGuid\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a WHERE (a.\"id\" = 10)", sql); Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"typeguid\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a WHERE (a.\"id\" = 10)", sql);
query.ToList(); query.ToList();
query = select.WhereIf(true, a => a.Id == 10 && a.Id > 10 || a.Clicks > 100); query = select.WhereIf(true, a => a.Id == 10 && a.Id > 10 || a.Clicks > 100);
sql = query.ToSql().Replace("\r\n", ""); sql = query.ToSql().Replace("\r\n", "");
Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"TypeGuid\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a WHERE (a.\"id\" = 10 AND a.\"id\" > 10 OR a.\"clicks\" > 100)", sql); Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"typeguid\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a WHERE (a.\"id\" = 10 AND a.\"id\" > 10 OR a.\"clicks\" > 100)", sql);
query.ToList(); query.ToList();
query = select.WhereIf(true, a => a.Id == 10).WhereIf(true, a => a.Clicks > 100); query = select.WhereIf(true, a => a.Id == 10).WhereIf(true, a => a.Clicks > 100);
sql = query.ToSql().Replace("\r\n", ""); sql = query.ToSql().Replace("\r\n", "");
Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"TypeGuid\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a WHERE (a.\"id\" = 10) AND (a.\"clicks\" > 100)", sql); Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"typeguid\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a WHERE (a.\"id\" = 10) AND (a.\"clicks\" > 100)", sql);
query.ToList(); query.ToList();
query = select.WhereIf(true, a => a.Type.Name == "typeTitle"); query = select.WhereIf(true, a => a.Type.Name == "typeTitle");
sql = query.ToSql().Replace("\r\n", ""); sql = query.ToSql().Replace("\r\n", "");
Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"TypeGuid\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a, \"testtypeinfo\" a__Type WHERE (a__Type.\"name\" = 'typeTitle')", sql); Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"typeguid\", a__Type.\"guid\", a__Type.\"parentid\", a__Type.\"name\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a LEFT JOIN \"testtypeinfo\" a__Type ON a__Type.\"guid\" = a.\"typeguid\" WHERE (a__Type.\"name\" = 'typeTitle')", sql);
query.ToList(); query.ToList();
query = select.WhereIf(true, a => a.Type.Name == "typeTitle" && a.Type.Guid == a.TypeGuid); query = select.WhereIf(true, a => a.Type.Name == "typeTitle" && a.Type.Guid == a.TypeGuid);
sql = query.ToSql().Replace("\r\n", ""); sql = query.ToSql().Replace("\r\n", "");
Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"TypeGuid\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a, \"testtypeinfo\" a__Type WHERE (a__Type.\"name\" = 'typeTitle' AND a__Type.\"guid\" = a.\"TypeGuid\")", sql); Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"typeguid\", a__Type.\"guid\", a__Type.\"parentid\", a__Type.\"name\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" 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.ToList();
query = select.WhereIf(true, a => a.Type.Parent.Name == "tparent"); query = select.WhereIf(true, a => a.Type.Parent.Name == "tparent");
sql = query.ToSql().Replace("\r\n", ""); sql = query.ToSql().Replace("\r\n", "");
Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"TypeGuid\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a, \"testtypeinfo\" a__Type, \"testtypeparentinfo\" a__Type__Parent WHERE (a__Type__Parent.\"name\" = 'tparent')", sql); Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"typeguid\", a__Type.\"guid\", a__Type.\"parentid\", a__Type.\"name\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" 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(); query.ToList();
//<2F><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB> From <20><>Ķ<EFBFBD><C4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD> //<2F><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB> From <20><>Ķ<EFBFBD><C4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
@ -494,13 +566,13 @@ namespace FreeSql.Tests.PostgreSQL {
.WhereIf(true, a => a.Id == 10 && c.Name == "xxx") .WhereIf(true, a => a.Id == 10 && c.Name == "xxx")
.WhereIf(true, a => b.ParentId == 20)); .WhereIf(true, a => b.ParentId == 20));
sql = query2.ToSql().Replace("\r\n", ""); sql = query2.ToSql().Replace("\r\n", "");
Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"TypeGuid\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a, \"testtypeparentinfo\" c, \"testtypeinfo\" b WHERE (a.\"id\" = 10 AND c.\"name\" = 'xxx') AND (b.\"parentid\" = 20)", sql); Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"typeguid\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a, \"testtypeinfo\" b, \"testtypeparentinfo\" c WHERE (a.\"id\" = 10 AND c.\"name\" = 'xxx') AND (b.\"parentid\" = 20)", sql);
query2.ToList(); query2.ToList();
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>϶<EFBFBD><CFB6><EFBFBD><EFBFBD><EFBFBD><E3B2BB> //<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 }); query = select.WhereIf(true, "a.\"clicks\" > 100 and a.\"id\" = @id", new { id = 10 });
sql = query.ToSql().Replace("\r\n", ""); sql = query.ToSql().Replace("\r\n", "");
Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"TypeGuid\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a WHERE (a.\"clicks\" > 100 and a.\"id\" = @id)", sql); Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"typeguid\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a WHERE (a.\"clicks\" > 100 and a.\"id\" = @id)", sql);
query.ToList(); query.ToList();
// ==========================================WhereIf(false) // ==========================================WhereIf(false)
@ -508,32 +580,32 @@ namespace FreeSql.Tests.PostgreSQL {
//<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> //<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); query = select.WhereIf(false, a => a.Id == 10);
sql = query.ToSql().Replace("\r\n", ""); sql = query.ToSql().Replace("\r\n", "");
Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"TypeGuid\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a", sql); Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"typeguid\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a", sql);
query.ToList(); query.ToList();
query = select.WhereIf(false, a => a.Id == 10 && a.Id > 10 || a.Clicks > 100); query = select.WhereIf(false, a => a.Id == 10 && a.Id > 10 || a.Clicks > 100);
sql = query.ToSql().Replace("\r\n", ""); sql = query.ToSql().Replace("\r\n", "");
Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"TypeGuid\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a", sql); Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"typeguid\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a", sql);
query.ToList(); query.ToList();
query = select.WhereIf(false, a => a.Id == 10).WhereIf(false, a => a.Clicks > 100); query = select.WhereIf(false, a => a.Id == 10).WhereIf(false, a => a.Clicks > 100);
sql = query.ToSql().Replace("\r\n", ""); sql = query.ToSql().Replace("\r\n", "");
Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"TypeGuid\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a", sql); Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"typeguid\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a", sql);
query.ToList(); query.ToList();
query = select.WhereIf(false, a => a.Type.Name == "typeTitle"); query = select.WhereIf(false, a => a.Type.Name == "typeTitle");
sql = query.ToSql().Replace("\r\n", ""); sql = query.ToSql().Replace("\r\n", "");
Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"TypeGuid\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a", sql); Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"typeguid\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a", sql);
query.ToList(); query.ToList();
query = select.WhereIf(false, a => a.Type.Name == "typeTitle" && a.Type.Guid == a.TypeGuid); query = select.WhereIf(false, a => a.Type.Name == "typeTitle" && a.Type.Guid == a.TypeGuid);
sql = query.ToSql().Replace("\r\n", ""); sql = query.ToSql().Replace("\r\n", "");
Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"TypeGuid\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a", sql); Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"typeguid\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a", sql);
query.ToList(); query.ToList();
query = select.WhereIf(false, a => a.Type.Parent.Name == "tparent"); query = select.WhereIf(false, a => a.Type.Parent.Name == "tparent");
sql = query.ToSql().Replace("\r\n", ""); sql = query.ToSql().Replace("\r\n", "");
Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"TypeGuid\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a", sql); Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"typeguid\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a", sql);
query.ToList(); query.ToList();
//<2F><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB> From <20><>Ķ<EFBFBD><C4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD> //<2F><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB> From <20><>Ķ<EFBFBD><C4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
@ -541,13 +613,13 @@ namespace FreeSql.Tests.PostgreSQL {
.WhereIf(false, a => a.Id == 10 && c.Name == "xxx") .WhereIf(false, a => a.Id == 10 && c.Name == "xxx")
.WhereIf(false, a => b.ParentId == 20)); .WhereIf(false, a => b.ParentId == 20));
sql = query2.ToSql().Replace("\r\n", ""); sql = query2.ToSql().Replace("\r\n", "");
Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"TypeGuid\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a", sql); Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"typeguid\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a, \"testtypeinfo\" b, \"testtypeparentinfo\" c", sql);
query2.ToList(); query2.ToList();
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>϶<EFBFBD><CFB6><EFBFBD><EFBFBD><EFBFBD><E3B2BB> //<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 }); query = select.WhereIf(false, "a.\"clicks\" > 100 and a.\"id\" = @id", new { id = 10 });
sql = query.ToSql().Replace("\r\n", ""); sql = query.ToSql().Replace("\r\n", "");
Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"TypeGuid\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a", sql); Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"typeguid\", a.\"title\", a.\"createtime\" FROM \"tb_topic\" a", sql);
query.ToList(); query.ToList();
} }
[Fact] [Fact]
@ -587,15 +659,25 @@ namespace FreeSql.Tests.PostgreSQL {
} }
[Fact] [Fact]
public void OrderBy() { public void OrderBy() {
var sql = select.OrderBy(a => new Random().NextDouble()).ToList();
} }
[Fact] [Fact]
public void Skip_Offset() { public void Skip_Offset() {
var sql = select.Offset(10).Limit(10).ToList();
} }
[Fact] [Fact]
public void Take_Limit() { public void Take_Limit() {
var sql = select.Limit(10).ToList();
} }
[Fact] [Fact]
public void Page() { 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] [Fact]
public void Sum() { public void Sum() {
@ -624,57 +706,57 @@ namespace FreeSql.Tests.PostgreSQL {
//<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> //<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 query = select.LeftJoin(a => a.Type.Guid == a.TypeGuid).AsTable(tableRule);
var sql = query.ToSql().Replace("\r\n", ""); 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_topicAsTable1\" a LEFT JOIN \"testtypeinfoAsTable2\" a__Type ON a__Type.\"guid\" = a.\"TypeGuid\"", sql); Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"typeguid\", a__Type.\"guid\", a__Type.\"parentid\", a__Type.\"name\", a.\"title\", a.\"createtime\" FROM \"tb_topicAsTable1\" 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); query = select.LeftJoin(a => a.Type.Guid == a.TypeGuid && a.Type.Name == "xxx").AsTable(tableRule);
sql = query.ToSql().Replace("\r\n", ""); 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_topicAsTable1\" a LEFT JOIN \"testtypeinfoAsTable2\" a__Type ON a__Type.\"guid\" = a.\"TypeGuid\" AND a__Type.\"name\" = 'xxx'", sql); Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"typeguid\", a__Type.\"guid\", a__Type.\"parentid\", a__Type.\"name\", a.\"title\", a.\"createtime\" FROM \"tb_topicAsTable1\" 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); 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", ""); 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_topicAsTable1\" a LEFT JOIN \"testtypeparentinfoAsTable\" a__Type__Parent ON 1 = 1 LEFT JOIN \"testtypeinfoAsTable2\" a__Type ON a__Type.\"guid\" = a.\"TypeGuid\" AND a__Type.\"name\" = 'xxx' WHERE (a__Type__Parent.\"id\" = 10)", sql); Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"typeguid\", a__Type.\"guid\", a__Type.\"parentid\", a__Type.\"name\", a.\"title\", a.\"createtime\" FROM \"tb_topicAsTable1\" 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> //<2F><><EFBFBD>û<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
query = select.LeftJoin<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid).AsTable(tableRule); query = select.LeftJoin<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid).AsTable(tableRule);
sql = query.ToSql().Replace("\r\n", ""); 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_topicAsTable1\" a LEFT JOIN \"testtypeinfoAsTable2\" b ON b.\"guid\" = a.\"TypeGuid\"", sql); Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"typeguid\", b.\"guid\", b.\"parentid\", b.\"name\", a.\"title\", a.\"createtime\" FROM \"tb_topicAsTable1\" 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); query = select.LeftJoin<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid && b.Name == "xxx").AsTable(tableRule);
sql = query.ToSql().Replace("\r\n", ""); 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_topicAsTable1\" a LEFT JOIN \"testtypeinfoAsTable2\" b ON b.\"guid\" = a.\"TypeGuid\" AND b.\"name\" = 'xxx'", sql); Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"typeguid\", b.\"guid\", b.\"parentid\", b.\"name\", a.\"title\", a.\"createtime\" FROM \"tb_topicAsTable1\" a LEFT JOIN \"testtypeinfoAsTable2\" b ON b.\"guid\" = a.\"typeguid\" AND b.\"name\" = 'xxx'", sql);
query = select.LeftJoin<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid && b.Name == "xxx").Where(a => a.Type.Parent.Id == 10).AsTable(tableRule); 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", ""); 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_topicAsTable1\" a LEFT JOIN \"testtypeparentinfoAsTable\" b__Parent ON 1 = 1 LEFT JOIN \"testtypeinfoAsTable2\" b ON b.\"guid\" = a.\"TypeGuid\" AND b.\"name\" = 'xxx' WHERE (b__Parent.\"id\" = 10)", sql); Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"typeguid\", a__Type.\"guid\", a__Type.\"parentid\", a__Type.\"name\", a.\"title\", a.\"createtime\" FROM \"tb_topicAsTable1\" 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> //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
query = select query = select
.LeftJoin(a => a.Type.Guid == a.TypeGuid) .LeftJoin(a => a.Type.Guid == a.TypeGuid)
.LeftJoin(a => a.Type.Parent.Id == a.Type.ParentId).AsTable(tableRule); .LeftJoin(a => a.Type.Parent.Id == a.Type.ParentId).AsTable(tableRule);
sql = query.ToSql().Replace("\r\n", ""); 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_topicAsTable1\" 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); Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"typeguid\", a__Type.\"guid\", a__Type.\"parentid\", a__Type.\"name\", a.\"title\", a.\"createtime\" FROM \"tb_topicAsTable1\" 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 query = select
.LeftJoin<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid) .LeftJoin<TestTypeInfo>((a, a__Type) => a__Type.Guid == a.TypeGuid)
.LeftJoin<TestTypeParentInfo>((a, c) => c.Id == a.Type.ParentId).AsTable(tableRule); .LeftJoin<TestTypeParentInfo>((a, c) => c.Id == a.Type.ParentId).AsTable(tableRule);
sql = query.ToSql().Replace("\r\n", ""); 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_topicAsTable1\" a LEFT JOIN \"testtypeinfoAsTable2\" b ON b.\"guid\" = a.\"TypeGuid\" LEFT JOIN \"testtypeparentinfoAsTable\" c ON c.\"id\" = b.\"parentid\"", sql); Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"typeguid\", a__Type.\"guid\", a__Type.\"parentid\", a__Type.\"name\", a.\"title\", a.\"createtime\" FROM \"tb_topicAsTable1\" 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>ϵ //<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 var query2 = select.From<TestTypeInfo, TestTypeParentInfo>((s, b, c) => s
.LeftJoin(a => a.TypeGuid == b.Guid) .LeftJoin(a => a.TypeGuid == b.Guid)
.LeftJoin(a => b.ParentId == c.Id)).AsTable(tableRule); .LeftJoin(a => b.ParentId == c.Id)).AsTable(tableRule);
sql = query2.ToSql().Replace("\r\n", ""); 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_topicAsTable1\" a LEFT JOIN \"testtypeinfoAsTable2\" b ON a.\"TypeGuid\" = b.\"guid\" LEFT JOIN \"testtypeparentinfoAsTable\" c ON b.\"parentid\" = c.\"id\"", sql); Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"typeguid\", b.\"guid\", b.\"parentid\", b.\"name\", a.\"title\", a.\"createtime\" FROM \"tb_topicAsTable1\" 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> //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>϶<EFBFBD><CFB6><EFBFBD><EFBFBD><EFBFBD><E3B2BB>
query = select.LeftJoin("\"testtypeinfo\" b on b.\"guid\" = a.\"TypeGuid\"").AsTable(tableRule); query = select.LeftJoin("\"testtypeinfo\" b on b.\"guid\" = a.\"typeguid\"").AsTable(tableRule);
sql = query.ToSql().Replace("\r\n", ""); sql = query.ToSql().Replace("\r\n", "");
Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"TypeGuid\", a.\"title\", a.\"createtime\" FROM \"tb_topicAsTable1\" a LEFT JOIN \"testtypeinfo\" b on b.\"guid\" = a.\"TypeGuid\"", sql); Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"typeguid\", a.\"title\", a.\"createtime\" FROM \"tb_topicAsTable1\" 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); 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", ""); sql = query.ToSql().Replace("\r\n", "");
Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"TypeGuid\", a.\"title\", a.\"createtime\" FROM \"tb_topicAsTable1\" a LEFT JOIN \"testtypeinfo\" b on b.\"guid\" = a.\"TypeGuid\" and b.\"name\" = @bname", sql); Assert.Equal("SELECT a.\"id\", a.\"clicks\", a.\"typeguid\", a.\"title\", a.\"createtime\" FROM \"tb_topicAsTable1\" a LEFT JOIN \"testtypeinfo\" b on b.\"guid\" = a.\"typeguid\" and b.\"name\" = @bname", sql);
} }
} }
} }

View File

@ -47,8 +47,8 @@ namespace FreeSql.Tests.PostgreSQLExpression {
[Fact] [Fact]
public void ToChar() { public void ToChar() {
var data = new List<object>(); var data = new List<object>();
data.Add(select.Where(a => Convert.ToChar(a.Clicks) == '1').ToList()); //data.Add(select.Where(a => Convert.ToChar(a.Clicks) == '1').ToList());
data.Add(select.Where(a => char.Parse(a.Clicks.ToString()) == '1').ToList()); //data.Add(select.Where(a => char.Parse(a.Clicks.ToString()) == '1').ToList());
} }
[Fact] [Fact]
public void ToDateTime() { public void ToDateTime() {

View File

@ -21,6 +21,7 @@ namespace FreeSql.Tests.PostgreSQLExpression {
} }
[Table(Name = "TestTypeInfo333")] [Table(Name = "TestTypeInfo333")]
class TestTypeInfo { class TestTypeInfo {
[Column(IsIdentity = true)]
public int Guid { get; set; } public int Guid { get; set; }
public int ParentId { get; set; } public int ParentId { get; set; }
public TestTypeParentInfo Parent { get; set; } public TestTypeParentInfo Parent { get; set; }

View File

@ -20,6 +20,7 @@ namespace FreeSql.Tests.PostgreSQLExpression {
public DateTime CreateTime { get; set; } public DateTime CreateTime { get; set; }
} }
class TestTypeInfo { class TestTypeInfo {
[Column(IsIdentity = true)]
public int Guid { get; set; } public int Guid { get; set; }
public int ParentId { get; set; } public int ParentId { get; set; }
public TestTypeParentInfo Parent { get; set; } public TestTypeParentInfo Parent { get; set; }

View File

@ -29,6 +29,7 @@ namespace FreeSql.Tests.SqlServer {
public DateTime CreateTime { get; set; } public DateTime CreateTime { get; set; }
} }
class TestTypeInfo { class TestTypeInfo {
[Column(IsIdentity = true)]
public int Guid { get; set; } public int Guid { get; set; }
public int ParentId { get; set; } public int ParentId { get; set; }
public TestTypeParentInfo Parent { get; set; } public TestTypeParentInfo Parent { get; set; }
@ -40,7 +41,79 @@ namespace FreeSql.Tests.SqlServer {
public List<TestTypeInfo> Types { get; set; } public List<TestTypeInfo> Types { 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 = _sqlserverFixture.SqlServer.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 = _sqlserverFixture.SqlServer.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 = _sqlserverFixture.SqlServer.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.sqlite.Select<Tag>().Where(a => a.Parent.Name == "xxx")
.LeftJoin(a => a.Parent_id == a.Parent.Id)
.ToSql();
var songs = g.sqlite.Select<Song>().Limit(10).ToList();
}
[Fact] [Fact]
public void ToDataTable() { public void ToDataTable() {
var items = new List<Topic>(); var items = new List<Topic>();
@ -135,7 +208,7 @@ namespace FreeSql.Tests.SqlServer {
query = select.LeftJoin(a => a.Type.Guid == a.TypeGuid && a.Type.Name == "xxx").Where(a => a.Type.Parent.Id == 10); 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", ""); 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 [TestTypeParentInfo] a__Type__Parent ON 1 = 1 LEFT JOIN [TestTypeInfo] a__Type ON a__Type.[Guid] = a.[TypeGuid] AND a__Type.[Name] = 'xxx' WHERE (a__Type__Parent.[Id] = 10)", sql); 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(); query.ToList();
//<2F><><EFBFBD>û<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> //<2F><><EFBFBD>û<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
@ -151,7 +224,7 @@ namespace FreeSql.Tests.SqlServer {
query = select.LeftJoin<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid && b.Name == "xxx").Where(a => a.Type.Parent.Id == 10); query = select.LeftJoin<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid && b.Name == "xxx").Where(a => a.Type.Parent.Id == 10);
sql = query.ToSql().Replace("\r\n", ""); 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 [TestTypeParentInfo] b__Parent ON 1 = 1 LEFT JOIN [TestTypeInfo] b ON b.[Guid] = a.[TypeGuid] AND b.[Name] = 'xxx' WHERE (b__Parent.[Id] = 10)", sql); 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] b ON b.[Guid] = a.[TypeGuid] AND b.[Name] = 'xxx' 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.[Id] = 10)", sql);
query.ToList(); query.ToList();
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
@ -163,10 +236,10 @@ namespace FreeSql.Tests.SqlServer {
query.ToList(); query.ToList();
query = select query = select
.LeftJoin<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid) .LeftJoin<TestTypeInfo>((a, a__Type) => a__Type.Guid == a.TypeGuid)
.LeftJoin<TestTypeParentInfo>((a, c) => c.Id == a.Type.ParentId); .LeftJoin<TestTypeParentInfo>((a, c) => c.Id == a.Type.ParentId);
sql = query.ToSql().Replace("\r\n", ""); 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] LEFT JOIN [TestTypeParentInfo] c ON c.[Id] = b.[ParentId]", sql); 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(); query.ToList();
//<2F><><EFBFBD>û<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>b<EFBFBD><62>c<EFBFBD><63><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵ //<2F><><EFBFBD>û<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>b<EFBFBD><62>c<EFBFBD><63><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵ
@ -203,7 +276,7 @@ namespace FreeSql.Tests.SqlServer {
query = select.InnerJoin(a => a.Type.Guid == a.TypeGuid && a.Type.Name == "xxx").Where(a => a.Type.Parent.Id == 10); 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", ""); 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 [TestTypeParentInfo] a__Type__Parent ON 1 = 1 INNER JOIN [TestTypeInfo] a__Type ON a__Type.[Guid] = a.[TypeGuid] AND a__Type.[Name] = 'xxx' WHERE (a__Type__Parent.[Id] = 10)", sql); 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(); query.ToList();
//<2F><><EFBFBD>û<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> //<2F><><EFBFBD>û<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
@ -219,7 +292,7 @@ namespace FreeSql.Tests.SqlServer {
query = select.InnerJoin<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid && b.Name == "xxx").Where(a => a.Type.Parent.Id == 10); query = select.InnerJoin<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid && b.Name == "xxx").Where(a => a.Type.Parent.Id == 10);
sql = query.ToSql().Replace("\r\n", ""); 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 [TestTypeParentInfo] b__Parent ON 1 = 1 INNER JOIN [TestTypeInfo] b ON b.[Guid] = a.[TypeGuid] AND b.[Name] = 'xxx' WHERE (b__Parent.[Id] = 10)", sql); 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] b ON b.[Guid] = a.[TypeGuid] AND b.[Name] = 'xxx' 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.[Id] = 10)", sql);
query.ToList(); query.ToList();
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
@ -231,10 +304,10 @@ namespace FreeSql.Tests.SqlServer {
query.ToList(); query.ToList();
query = select query = select
.InnerJoin<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid) .InnerJoin<TestTypeInfo>((a, a__Type) => a__Type.Guid == a.TypeGuid)
.InnerJoin<TestTypeParentInfo>((a, c) => c.Id == a.Type.ParentId); .InnerJoin<TestTypeParentInfo>((a, c) => c.Id == a.Type.ParentId);
sql = query.ToSql().Replace("\r\n", ""); 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] INNER JOIN [TestTypeParentInfo] c ON c.[Id] = b.[ParentId]", sql); 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(); query.ToList();
//<2F><><EFBFBD>û<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>b<EFBFBD><62>c<EFBFBD><63><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵ //<2F><><EFBFBD>û<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>b<EFBFBD><62>c<EFBFBD><63><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵ
@ -272,7 +345,7 @@ namespace FreeSql.Tests.SqlServer {
query = select.RightJoin(a => a.Type.Guid == a.TypeGuid && a.Type.Name == "xxx").Where(a => a.Type.Parent.Id == 10); 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", ""); 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 [TestTypeParentInfo] a__Type__Parent ON 1 = 1 RIGHT JOIN [TestTypeInfo] a__Type ON a__Type.[Guid] = a.[TypeGuid] AND a__Type.[Name] = 'xxx' WHERE (a__Type__Parent.[Id] = 10)", sql); 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(); query.ToList();
//<2F><><EFBFBD>û<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> //<2F><><EFBFBD>û<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
@ -288,7 +361,7 @@ namespace FreeSql.Tests.SqlServer {
query = select.RightJoin<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid && b.Name == "xxx").Where(a => a.Type.Parent.Id == 10); query = select.RightJoin<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid && b.Name == "xxx").Where(a => a.Type.Parent.Id == 10);
sql = query.ToSql().Replace("\r\n", ""); 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 [TestTypeParentInfo] b__Parent ON 1 = 1 RIGHT JOIN [TestTypeInfo] b ON b.[Guid] = a.[TypeGuid] AND b.[Name] = 'xxx' WHERE (b__Parent.[Id] = 10)", sql); 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] b ON b.[Guid] = a.[TypeGuid] AND b.[Name] = 'xxx' 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.[Id] = 10)", sql);
query.ToList(); query.ToList();
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
@ -300,10 +373,10 @@ namespace FreeSql.Tests.SqlServer {
query.ToList(); query.ToList();
query = select query = select
.RightJoin<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid) .RightJoin<TestTypeInfo>((a, a__Type) => a__Type.Guid == a.TypeGuid)
.RightJoin<TestTypeParentInfo>((a, c) => c.Id == a.Type.ParentId); .RightJoin<TestTypeParentInfo>((a, c) => c.Id == a.Type.ParentId);
sql = query.ToSql().Replace("\r\n", ""); 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] RIGHT JOIN [TestTypeParentInfo] c ON c.[Id] = b.[ParentId]", sql); 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(); query.ToList();
//<2F><><EFBFBD>û<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>b<EFBFBD><62>c<EFBFBD><63><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵ //<2F><><EFBFBD>û<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>b<EFBFBD><62>c<EFBFBD><63><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵ
@ -346,17 +419,17 @@ namespace FreeSql.Tests.SqlServer {
query = select.Where(a => a.Type.Name == "typeTitle"); query = select.Where(a => a.Type.Name == "typeTitle");
sql = query.ToSql().Replace("\r\n", ""); sql = query.ToSql().Replace("\r\n", "");
Assert.Equal("SELECT a.[Id], a.[Clicks], a.[TypeGuid], a.[Title], a.[CreateTime] FROM [tb_topic22] a, [TestTypeInfo] a__Type WHERE (a__Type.[Name] = 'typeTitle')", sql); 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.ToList();
query = select.Where(a => a.Type.Name == "typeTitle" && a.Type.Guid == a.TypeGuid); query = select.Where(a => a.Type.Name == "typeTitle" && a.Type.Guid == a.TypeGuid);
sql = query.ToSql().Replace("\r\n", ""); sql = query.ToSql().Replace("\r\n", "");
Assert.Equal("SELECT a.[Id], a.[Clicks], a.[TypeGuid], a.[Title], a.[CreateTime] FROM [tb_topic22] a, [TestTypeInfo] a__Type WHERE (a__Type.[Name] = 'typeTitle' AND a__Type.[Guid] = a.[TypeGuid])", sql); 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.ToList();
query = select.Where(a => a.Type.Parent.Name == "tparent"); query = select.Where(a => a.Type.Parent.Name == "tparent");
sql = query.ToSql().Replace("\r\n", ""); sql = query.ToSql().Replace("\r\n", "");
Assert.Equal("SELECT a.[Id], a.[Clicks], a.[TypeGuid], a.[Title], a.[CreateTime] FROM [tb_topic22] a, [TestTypeInfo] a__Type, [TestTypeParentInfo] a__Type__Parent WHERE (a__Type__Parent.[Name] = 'tparent')", sql); 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(); query.ToList();
//<2F><><EFBFBD>û<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ԣ<EFBFBD><D4A3>򵥶<EFBFBD><F2B5A5B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD> //<2F><><EFBFBD>û<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ԣ<EFBFBD><D4A3>򵥶<EFBFBD><F2B5A5B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
@ -380,7 +453,7 @@ namespace FreeSql.Tests.SqlServer {
.Where(a => a.Id == 10 && c.Name == "xxx") .Where(a => a.Id == 10 && c.Name == "xxx")
.Where(a => b.ParentId == 20)); .Where(a => b.ParentId == 20));
sql = query2.ToSql().Replace("\r\n", ""); sql = query2.ToSql().Replace("\r\n", "");
Assert.Equal("SELECT a.[Id], a.[Clicks], a.[TypeGuid], a.[Title], a.[CreateTime] FROM [tb_topic22] a, [TestTypeParentInfo] c, [TestTypeInfo] b WHERE (a.[Id] = 10 AND c.[Name] = 'xxx') AND (b.[ParentId] = 20)", sql); 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(); query2.ToList();
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>϶<EFBFBD><CFB6><EFBFBD><EFBFBD><EFBFBD><E3B2BB> //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>϶<EFBFBD><CFB6><EFBFBD><EFBFBD><EFBFBD><E3B2BB>
@ -409,17 +482,17 @@ namespace FreeSql.Tests.SqlServer {
query = select.WhereIf(true, a => a.Type.Name == "typeTitle"); query = select.WhereIf(true, a => a.Type.Name == "typeTitle");
sql = query.ToSql().Replace("\r\n", ""); sql = query.ToSql().Replace("\r\n", "");
Assert.Equal("SELECT a.[Id], a.[Clicks], a.[TypeGuid], a.[Title], a.[CreateTime] FROM [tb_topic22] a, [TestTypeInfo] a__Type WHERE (a__Type.[Name] = 'typeTitle')", sql); 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.ToList();
query = select.WhereIf(true, a => a.Type.Name == "typeTitle" && a.Type.Guid == a.TypeGuid); query = select.WhereIf(true, a => a.Type.Name == "typeTitle" && a.Type.Guid == a.TypeGuid);
sql = query.ToSql().Replace("\r\n", ""); sql = query.ToSql().Replace("\r\n", "");
Assert.Equal("SELECT a.[Id], a.[Clicks], a.[TypeGuid], a.[Title], a.[CreateTime] FROM [tb_topic22] a, [TestTypeInfo] a__Type WHERE (a__Type.[Name] = 'typeTitle' AND a__Type.[Guid] = a.[TypeGuid])", sql); 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.ToList();
query = select.WhereIf(true, a => a.Type.Parent.Name == "tparent"); query = select.WhereIf(true, a => a.Type.Parent.Name == "tparent");
sql = query.ToSql().Replace("\r\n", ""); sql = query.ToSql().Replace("\r\n", "");
Assert.Equal("SELECT a.[Id], a.[Clicks], a.[TypeGuid], a.[Title], a.[CreateTime] FROM [tb_topic22] a, [TestTypeInfo] a__Type, [TestTypeParentInfo] a__Type__Parent WHERE (a__Type__Parent.[Name] = 'tparent')", sql); 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(); query.ToList();
//<2F><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB> From <20><>Ķ<EFBFBD><C4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD> //<2F><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB> From <20><>Ķ<EFBFBD><C4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
@ -427,7 +500,7 @@ namespace FreeSql.Tests.SqlServer {
.WhereIf(true, a => a.Id == 10 && c.Name == "xxx") .WhereIf(true, a => a.Id == 10 && c.Name == "xxx")
.WhereIf(true, a => b.ParentId == 20)); .WhereIf(true, a => b.ParentId == 20));
sql = query2.ToSql().Replace("\r\n", ""); sql = query2.ToSql().Replace("\r\n", "");
Assert.Equal("SELECT a.[Id], a.[Clicks], a.[TypeGuid], a.[Title], a.[CreateTime] FROM [tb_topic22] a, [TestTypeParentInfo] c, [TestTypeInfo] b WHERE (a.[Id] = 10 AND c.[Name] = 'xxx') AND (b.[ParentId] = 20)", sql); 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(); query2.ToList();
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>϶<EFBFBD><CFB6><EFBFBD><EFBFBD><EFBFBD><E3B2BB> //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>϶<EFBFBD><CFB6><EFBFBD><EFBFBD><EFBFBD><E3B2BB>
@ -474,7 +547,7 @@ namespace FreeSql.Tests.SqlServer {
.WhereIf(false, a => a.Id == 10 && c.Name == "xxx") .WhereIf(false, a => a.Id == 10 && c.Name == "xxx")
.WhereIf(false, a => b.ParentId == 20)); .WhereIf(false, a => b.ParentId == 20));
sql = query2.ToSql().Replace("\r\n", ""); sql = query2.ToSql().Replace("\r\n", "");
Assert.Equal("SELECT a.[Id], a.[Clicks], a.[TypeGuid], a.[Title], a.[CreateTime] FROM [tb_topic22] a", sql); Assert.Equal("SELECT a.[Id], a.[Clicks], a.[TypeGuid], a.[Title], a.[CreateTime] FROM [tb_topic22] a, [TestTypeInfo] b, [TestTypeParentInfo] c", sql);
query2.ToList(); query2.ToList();
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>϶<EFBFBD><CFB6><EFBFBD><EFBFBD><EFBFBD><E3B2BB> //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>϶<EFBFBD><CFB6><EFBFBD><EFBFBD><EFBFBD><E3B2BB>
@ -520,15 +593,25 @@ namespace FreeSql.Tests.SqlServer {
} }
[Fact] [Fact]
public void OrderBy() { public void OrderBy() {
var sql = select.OrderBy(a => new Random().NextDouble()).ToList();
} }
[Fact] [Fact]
public void Skip_Offset() { public void Skip_Offset() {
var sql = select.Offset(10).Limit(10).ToList();
} }
[Fact] [Fact]
public void Take_Limit() { public void Take_Limit() {
var sql = select.Limit(10).ToList();
} }
[Fact] [Fact]
public void Page() { 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] [Fact]
public void Sum() { public void Sum() {
@ -565,7 +648,7 @@ namespace FreeSql.Tests.SqlServer {
query = select.LeftJoin(a => a.Type.Guid == a.TypeGuid && a.Type.Name == "xxx").Where(a => a.Type.Parent.Id == 10).AsTable(tableRule); 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", ""); 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 [TestTypeParentInfoAsTable] a__Type__Parent ON 1 = 1 LEFT JOIN [TestTypeInfoAsTable2] a__Type ON a__Type.[Guid] = a.[TypeGuid] AND a__Type.[Name] = 'xxx' WHERE (a__Type__Parent.[Id] = 10)", sql); 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> //<2F><><EFBFBD>û<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
query = select.LeftJoin<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid).AsTable(tableRule); query = select.LeftJoin<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid).AsTable(tableRule);
@ -578,7 +661,7 @@ namespace FreeSql.Tests.SqlServer {
query = select.LeftJoin<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid && b.Name == "xxx").Where(a => a.Type.Parent.Id == 10).AsTable(tableRule); query = select.LeftJoin<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid && b.Name == "xxx").Where(a => a.Type.Parent.Id == 10).AsTable(tableRule);
sql = query.ToSql().Replace("\r\n", ""); 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 [TestTypeParentInfoAsTable] b__Parent ON 1 = 1 LEFT JOIN [TestTypeInfoAsTable2] b ON b.[Guid] = a.[TypeGuid] AND b.[Name] = 'xxx' WHERE (b__Parent.[Id] = 10)", sql); 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] b ON b.[Guid] = a.[TypeGuid] AND b.[Name] = 'xxx' 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] WHERE (a__Type__Parent.[Id] = 10)", sql);
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
query = select query = select
@ -591,7 +674,7 @@ namespace FreeSql.Tests.SqlServer {
.LeftJoin<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid) .LeftJoin<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid)
.LeftJoin<TestTypeParentInfo>((a, c) => c.Id == a.Type.ParentId).AsTable(tableRule); .LeftJoin<TestTypeParentInfo>((a, c) => c.Id == a.Type.ParentId).AsTable(tableRule);
sql = query.ToSql().Replace("\r\n", ""); 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] LEFT JOIN [TestTypeParentInfoAsTable] c ON c.[Id] = b.[ParentId]", sql); 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] a__Type ON a__Type.[Guid] = a.[TypeGuid] LEFT JOIN [TestTypeInfoAsTable2] b ON b.[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>ϵ //<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 var query2 = select.From<TestTypeInfo, TestTypeParentInfo>((s, b, c) => s
@ -601,13 +684,13 @@ namespace FreeSql.Tests.SqlServer {
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); 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> //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>϶<EFBFBD><CFB6><EFBFBD><EFBFBD><EFBFBD><E3B2BB>
query = select.LeftJoin("[TestTypeInfo] b on b.[Guid] = a.[TypeGuid]").AsTable(tableRule); query = select.LeftJoin("TestTypeInfo b on b.Guid = a.TypeGuid").AsTable(tableRule);
sql = query.ToSql().Replace("\r\n", ""); 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); 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); 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", ""); 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); 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);
} }
} }
} }

View File

@ -30,6 +30,7 @@ namespace FreeSql.Tests.SqlServerExpression {
} }
[Table(Name = "TestTypeInfo333")] [Table(Name = "TestTypeInfo333")]
class TestTypeInfo { class TestTypeInfo {
[Column(IsIdentity = true)]
public int Guid { get; set; } public int Guid { get; set; }
public int ParentId { get; set; } public int ParentId { get; set; }
public TestTypeParentInfo Parent { get; set; } public TestTypeParentInfo Parent { get; set; }

View File

@ -29,6 +29,7 @@ namespace FreeSql.Tests.SqlServerExpression {
public DateTime CreateTime { get; set; } public DateTime CreateTime { get; set; }
} }
class TestTypeInfo { class TestTypeInfo {
[Column(IsIdentity = true)]
public int Guid { get; set; } public int Guid { get; set; }
public int ParentId { get; set; } public int ParentId { get; set; }
public TestTypeParentInfo Parent { get; set; } public TestTypeParentInfo Parent { get; set; }

View File

@ -20,6 +20,7 @@ namespace FreeSql.Tests.Sqlite {
public DateTime CreateTime { get; set; } public DateTime CreateTime { get; set; }
} }
class TestTypeInfo { class TestTypeInfo {
[Column(IsIdentity = true)]
public int Guid { get; set; } public int Guid { get; set; }
public int ParentId { get; set; } public int ParentId { get; set; }
public TestTypeParentInfo Parent { get; set; } public TestTypeParentInfo Parent { get; set; }
@ -62,6 +63,39 @@ namespace FreeSql.Tests.Sqlite {
public virtual ICollection<Tag> Tags { get; set; } public virtual ICollection<Tag> Tags { get; set; }
} }
[Fact]
public void AsSelect() {
//OneToOne、ManyToOne
var t0 = g.sqlite.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.sqlite.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.sqlite.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] [Fact]
public void Lazy() { public void Lazy() {
var tags = g.sqlite.Select<Tag>().Where(a => a.Parent.Name == "xxx") var tags = g.sqlite.Select<Tag>().Where(a => a.Parent.Name == "xxx")
@ -166,7 +200,7 @@ namespace FreeSql.Tests.Sqlite {
query = select.LeftJoin(a => a.Type.Guid == a.TypeGuid && a.Type.Name == "xxx").Where(a => a.Type.Parent.Id == 10); 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", ""); 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 \"TestTypeParentInfo\" a__Type__Parent ON 1 = 1 LEFT JOIN \"TestTypeInfo\" a__Type ON a__Type.\"Guid\" = a.\"TypeGuid\" AND a__Type.\"Name\" = 'xxx' WHERE (a__Type__Parent.\"Id\" = 10)", sql); 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(); query.ToList();
//<2F><><EFBFBD>û<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> //<2F><><EFBFBD>û<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
@ -180,9 +214,9 @@ namespace FreeSql.Tests.Sqlite {
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); 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.ToList();
query = select.LeftJoin<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid && b.Name == "xxx").Where(a => a.Type.Parent.Id == 10); 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", ""); 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 \"TestTypeParentInfo\" b__Parent ON 1 = 1 LEFT JOIN \"TestTypeInfo\" b ON b.\"Guid\" = a.\"TypeGuid\" AND b.\"Name\" = 'xxx' WHERE (b__Parent.\"Id\" = 10)", sql); 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(); query.ToList();
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
@ -194,10 +228,10 @@ namespace FreeSql.Tests.Sqlite {
query.ToList(); query.ToList();
query = select query = select
.LeftJoin<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid) .LeftJoin<TestTypeInfo>((a, a__Type) => a__Type.Guid == a.TypeGuid)
.LeftJoin<TestTypeParentInfo>((a, c) => c.Id == a.Type.ParentId); .LeftJoin<TestTypeParentInfo>((a, c) => c.Id == a.Type.ParentId);
sql = query.ToSql().Replace("\r\n", ""); 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\" LEFT JOIN \"TestTypeParentInfo\" c ON c.\"Id\" = b.\"ParentId\"", sql); 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(); query.ToList();
//<2F><><EFBFBD>û<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>b<EFBFBD><62>c<EFBFBD><63><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵ //<2F><><EFBFBD>û<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>b<EFBFBD><62>c<EFBFBD><63><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵ
@ -234,7 +268,7 @@ namespace FreeSql.Tests.Sqlite {
query = select.InnerJoin(a => a.Type.Guid == a.TypeGuid && a.Type.Name == "xxx").Where(a => a.Type.Parent.Id == 10); 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", ""); 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 \"TestTypeParentInfo\" a__Type__Parent ON 1 = 1 INNER JOIN \"TestTypeInfo\" a__Type ON a__Type.\"Guid\" = a.\"TypeGuid\" AND a__Type.\"Name\" = 'xxx' WHERE (a__Type__Parent.\"Id\" = 10)", sql); 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(); query.ToList();
//<2F><><EFBFBD>û<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> //<2F><><EFBFBD>û<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
@ -248,9 +282,9 @@ namespace FreeSql.Tests.Sqlite {
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); 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.ToList();
query = select.InnerJoin<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid && b.Name == "xxx").Where(a => a.Type.Parent.Id == 10); 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", ""); 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 \"TestTypeParentInfo\" b__Parent ON 1 = 1 INNER JOIN \"TestTypeInfo\" b ON b.\"Guid\" = a.\"TypeGuid\" AND b.\"Name\" = 'xxx' WHERE (b__Parent.\"Id\" = 10)", sql); 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(); query.ToList();
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
@ -262,10 +296,10 @@ namespace FreeSql.Tests.Sqlite {
query.ToList(); query.ToList();
query = select query = select
.InnerJoin<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid) .InnerJoin<TestTypeInfo>((a, a__Type) => a__Type.Guid == a.TypeGuid)
.InnerJoin<TestTypeParentInfo>((a, c) => c.Id == a.Type.ParentId); .InnerJoin<TestTypeParentInfo>((a, c) => c.Id == a.Type.ParentId);
sql = query.ToSql().Replace("\r\n", ""); 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\" INNER JOIN \"TestTypeParentInfo\" c ON c.\"Id\" = b.\"ParentId\"", sql); 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(); query.ToList();
//<2F><><EFBFBD>û<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>b<EFBFBD><62>c<EFBFBD><63><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵ //<2F><><EFBFBD>û<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>b<EFBFBD><62>c<EFBFBD><63><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϵ
@ -290,71 +324,6 @@ namespace FreeSql.Tests.Sqlite {
} }
[Fact] [Fact]
public void RightJoin() { 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 LEFT JOIN \"TestTypeParentInfo\" a__Type__Parent ON 1 = 1 RIGHT JOIN \"TestTypeInfo\" a__Type ON a__Type.\"Guid\" = a.\"TypeGuid\" AND a__Type.\"Name\" = 'xxx' 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, b) => b.Guid == a.TypeGuid && b.Name == "xxx").Where(a => a.Type.Parent.Id == 10);
//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 \"TestTypeParentInfo\" b__Parent ON 1 = 1 RIGHT JOIN \"TestTypeInfo\" b ON b.\"Guid\" = a.\"TypeGuid\" AND b.\"Name\" = 'xxx' WHERE (b__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, b) => b.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\", b.\"Guid\", b.\"ParentId\", b.\"Name\", a.\"Title\", a.\"CreateTime\" FROM \"tb_topic22\" a RIGHT JOIN \"TestTypeInfo\" b ON b.\"Guid\" = a.\"TypeGuid\" RIGHT JOIN \"TestTypeParentInfo\" c ON c.\"Id\" = b.\"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] [Fact]
@ -377,17 +346,17 @@ namespace FreeSql.Tests.Sqlite {
query = select.Where(a => a.Type.Name == "typeTitle"); query = select.Where(a => a.Type.Name == "typeTitle");
sql = query.ToSql().Replace("\r\n", ""); sql = query.ToSql().Replace("\r\n", "");
Assert.Equal("SELECT a.\"Id\", a.\"Clicks\", a.\"TypeGuid\", a.\"Title\", a.\"CreateTime\" FROM \"tb_topic22\" a, \"TestTypeInfo\" a__Type WHERE (a__Type.\"Name\" = 'typeTitle')", sql); 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.ToList();
query = select.Where(a => a.Type.Name == "typeTitle" && a.Type.Guid == a.TypeGuid); query = select.Where(a => a.Type.Name == "typeTitle" && a.Type.Guid == a.TypeGuid);
sql = query.ToSql().Replace("\r\n", ""); sql = query.ToSql().Replace("\r\n", "");
Assert.Equal("SELECT a.\"Id\", a.\"Clicks\", a.\"TypeGuid\", a.\"Title\", a.\"CreateTime\" FROM \"tb_topic22\" a, \"TestTypeInfo\" a__Type WHERE (a__Type.\"Name\" = 'typeTitle' AND a__Type.\"Guid\" = a.\"TypeGuid\")", sql); 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.ToList();
query = select.Where(a => a.Type.Parent.Name == "tparent"); query = select.Where(a => a.Type.Parent.Name == "tparent");
sql = query.ToSql().Replace("\r\n", ""); sql = query.ToSql().Replace("\r\n", "");
Assert.Equal("SELECT a.\"Id\", a.\"Clicks\", a.\"TypeGuid\", a.\"Title\", a.\"CreateTime\" FROM \"tb_topic22\" a, \"TestTypeInfo\" a__Type, \"TestTypeParentInfo\" a__Type__Parent WHERE (a__Type__Parent.\"Name\" = 'tparent')", sql); 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(); query.ToList();
//<2F><><EFBFBD>û<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ԣ<EFBFBD><D4A3>򵥶<EFBFBD><F2B5A5B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD> //<2F><><EFBFBD>û<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ԣ<EFBFBD><D4A3>򵥶<EFBFBD><F2B5A5B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
@ -411,7 +380,7 @@ namespace FreeSql.Tests.Sqlite {
.Where(a => a.Id == 10 && c.Name == "xxx") .Where(a => a.Id == 10 && c.Name == "xxx")
.Where(a => b.ParentId == 20)); .Where(a => b.ParentId == 20));
sql = query2.ToSql().Replace("\r\n", ""); sql = query2.ToSql().Replace("\r\n", "");
Assert.Equal("SELECT a.\"Id\", a.\"Clicks\", a.\"TypeGuid\", a.\"Title\", a.\"CreateTime\" FROM \"tb_topic22\" a, \"TestTypeParentInfo\" c, \"TestTypeInfo\" b WHERE (a.\"Id\" = 10 AND c.\"Name\" = 'xxx') AND (b.\"ParentId\" = 20)", sql); 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(); query2.ToList();
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>϶<EFBFBD><CFB6><EFBFBD><EFBFBD><EFBFBD><E3B2BB> //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>϶<EFBFBD><CFB6><EFBFBD><EFBFBD><EFBFBD><E3B2BB>
@ -440,17 +409,17 @@ namespace FreeSql.Tests.Sqlite {
query = select.WhereIf(true, a => a.Type.Name == "typeTitle"); query = select.WhereIf(true, a => a.Type.Name == "typeTitle");
sql = query.ToSql().Replace("\r\n", ""); sql = query.ToSql().Replace("\r\n", "");
Assert.Equal("SELECT a.\"Id\", a.\"Clicks\", a.\"TypeGuid\", a.\"Title\", a.\"CreateTime\" FROM \"tb_topic22\" a, \"TestTypeInfo\" a__Type WHERE (a__Type.\"Name\" = 'typeTitle')", sql); 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.ToList();
query = select.WhereIf(true, a => a.Type.Name == "typeTitle" && a.Type.Guid == a.TypeGuid); query = select.WhereIf(true, a => a.Type.Name == "typeTitle" && a.Type.Guid == a.TypeGuid);
sql = query.ToSql().Replace("\r\n", ""); sql = query.ToSql().Replace("\r\n", "");
Assert.Equal("SELECT a.\"Id\", a.\"Clicks\", a.\"TypeGuid\", a.\"Title\", a.\"CreateTime\" FROM \"tb_topic22\" a, \"TestTypeInfo\" a__Type WHERE (a__Type.\"Name\" = 'typeTitle' AND a__Type.\"Guid\" = a.\"TypeGuid\")", sql); 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.ToList();
query = select.WhereIf(true, a => a.Type.Parent.Name == "tparent"); query = select.WhereIf(true, a => a.Type.Parent.Name == "tparent");
sql = query.ToSql().Replace("\r\n", ""); sql = query.ToSql().Replace("\r\n", "");
Assert.Equal("SELECT a.\"Id\", a.\"Clicks\", a.\"TypeGuid\", a.\"Title\", a.\"CreateTime\" FROM \"tb_topic22\" a, \"TestTypeInfo\" a__Type, \"TestTypeParentInfo\" a__Type__Parent WHERE (a__Type__Parent.\"Name\" = 'tparent')", sql); 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(); query.ToList();
//<2F><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB> From <20><>Ķ<EFBFBD><C4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD> //<2F><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB> From <20><>Ķ<EFBFBD><C4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
@ -458,7 +427,7 @@ namespace FreeSql.Tests.Sqlite {
.WhereIf(true, a => a.Id == 10 && c.Name == "xxx") .WhereIf(true, a => a.Id == 10 && c.Name == "xxx")
.WhereIf(true, a => b.ParentId == 20)); .WhereIf(true, a => b.ParentId == 20));
sql = query2.ToSql().Replace("\r\n", ""); sql = query2.ToSql().Replace("\r\n", "");
Assert.Equal("SELECT a.\"Id\", a.\"Clicks\", a.\"TypeGuid\", a.\"Title\", a.\"CreateTime\" FROM \"tb_topic22\" a, \"TestTypeParentInfo\" c, \"TestTypeInfo\" b WHERE (a.\"Id\" = 10 AND c.\"Name\" = 'xxx') AND (b.\"ParentId\" = 20)", sql); 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(); query2.ToList();
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>϶<EFBFBD><CFB6><EFBFBD><EFBFBD><EFBFBD><E3B2BB> //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>϶<EFBFBD><CFB6><EFBFBD><EFBFBD><EFBFBD><E3B2BB>
@ -505,7 +474,7 @@ namespace FreeSql.Tests.Sqlite {
.WhereIf(false, a => a.Id == 10 && c.Name == "xxx") .WhereIf(false, a => a.Id == 10 && c.Name == "xxx")
.WhereIf(false, a => b.ParentId == 20)); .WhereIf(false, a => b.ParentId == 20));
sql = query2.ToSql().Replace("\r\n", ""); sql = query2.ToSql().Replace("\r\n", "");
Assert.Equal("SELECT a.\"Id\", a.\"Clicks\", a.\"TypeGuid\", a.\"Title\", a.\"CreateTime\" FROM \"tb_topic22\" a", sql); Assert.Equal("SELECT a.\"Id\", a.\"Clicks\", a.\"TypeGuid\", a.\"Title\", a.\"CreateTime\" FROM \"tb_topic22\" a, \"TestTypeInfo\" b, \"TestTypeParentInfo\" c", sql);
query2.ToList(); query2.ToList();
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>϶<EFBFBD><CFB6><EFBFBD><EFBFBD><EFBFBD><E3B2BB> //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>϶<EFBFBD><CFB6><EFBFBD><EFBFBD><EFBFBD><E3B2BB>
@ -551,15 +520,25 @@ namespace FreeSql.Tests.Sqlite {
} }
[Fact] [Fact]
public void OrderBy() { public void OrderBy() {
var sql = select.OrderBy(a => new Random().NextDouble()).ToList();
} }
[Fact] [Fact]
public void Skip_Offset() { public void Skip_Offset() {
var sql = select.Offset(10).Limit(10).ToList();
} }
[Fact] [Fact]
public void Take_Limit() { public void Take_Limit() {
var sql = select.Limit(10).ToList();
} }
[Fact] [Fact]
public void Page() { 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] [Fact]
public void Sum() { public void Sum() {
@ -614,7 +593,7 @@ namespace FreeSql.Tests.Sqlite {
query = select.LeftJoin(a => a.Type.Guid == a.TypeGuid && a.Type.Name == "xxx").Where(a => a.Type.Parent.Id == 10).AsTable(tableRule); 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", ""); 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 \"TestTypeParentInfoAsTable\" a__Type__Parent ON 1 = 1 LEFT JOIN \"TestTypeInfoAsTable2\" a__Type ON a__Type.\"Guid\" = a.\"TypeGuid\" AND a__Type.\"Name\" = 'xxx' WHERE (a__Type__Parent.\"Id\" = 10)", sql); 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> //<2F><><EFBFBD>û<EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
query = select.LeftJoin<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid).AsTable(tableRule); query = select.LeftJoin<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid).AsTable(tableRule);
@ -625,9 +604,9 @@ namespace FreeSql.Tests.Sqlite {
sql = query.ToSql().Replace("\r\n", ""); 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); 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, b) => b.Guid == a.TypeGuid && b.Name == "xxx").Where(a => a.Type.Parent.Id == 10).AsTable(tableRule); 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", ""); 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 \"TestTypeParentInfoAsTable\" b__Parent ON 1 = 1 LEFT JOIN \"TestTypeInfoAsTable2\" b ON b.\"Guid\" = a.\"TypeGuid\" AND b.\"Name\" = 'xxx' WHERE (b__Parent.\"Id\" = 10)", sql); 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> //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
query = select query = select
@ -637,10 +616,10 @@ namespace FreeSql.Tests.Sqlite {
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); 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 query = select
.LeftJoin<TestTypeInfo>((a, b) => b.Guid == a.TypeGuid) .LeftJoin<TestTypeInfo>((a, a__Type) => a__Type.Guid == a.TypeGuid)
.LeftJoin<TestTypeParentInfo>((a, c) => c.Id == a.Type.ParentId).AsTable(tableRule); .LeftJoin<TestTypeParentInfo>((a, c) => c.Id == a.Type.ParentId).AsTable(tableRule);
sql = query.ToSql().Replace("\r\n", ""); 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\" LEFT JOIN \"TestTypeParentInfoAsTable\" c ON c.\"Id\" = b.\"ParentId\"", sql); 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>ϵ //<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 var query2 = select.From<TestTypeInfo, TestTypeParentInfo>((s, b, c) => s

View File

@ -21,6 +21,7 @@ namespace FreeSql.Tests.SqliteExpression {
} }
[Table(Name = "TestTypeInfo333")] [Table(Name = "TestTypeInfo333")]
class TestTypeInfo { class TestTypeInfo {
[Column(IsIdentity = true)]
public int Guid { get; set; } public int Guid { get; set; }
public int ParentId { get; set; } public int ParentId { get; set; }
public TestTypeParentInfo Parent { get; set; } public TestTypeParentInfo Parent { get; set; }

View File

@ -20,6 +20,7 @@ namespace FreeSql.Tests.SqliteExpression {
public DateTime CreateTime { get; set; } public DateTime CreateTime { get; set; }
} }
class TestTypeInfo { class TestTypeInfo {
[Column(IsIdentity = true)]
public int Guid { get; set; } public int Guid { get; set; }
public int ParentId { get; set; } public int ParentId { get; set; }
public TestTypeParentInfo Parent { get; set; } public TestTypeParentInfo Parent { get; set; }

View File

@ -25,6 +25,13 @@ public class g {
.UseAutoSyncStructure(true) .UseAutoSyncStructure(true)
.UseSyncStructureToLower(true) .UseSyncStructureToLower(true)
.UseLazyLoading(true) .UseLazyLoading(true)
.UseMonitorCommand(
cmd => {
Trace.WriteLine(cmd.CommandText);
}, //监听SQL命令对象在执行前
(cmd, traceLog) => {
Console.WriteLine(traceLog);
}) //监听SQL命令对象在执行后
.Build()); .Build());
public static IFreeSql pgsql => pgsqlLazy.Value; public static IFreeSql pgsql => pgsqlLazy.Value;
@ -33,6 +40,15 @@ public class g {
.UseAutoSyncStructure(true) .UseAutoSyncStructure(true)
.UseLazyLoading(true) .UseLazyLoading(true)
.UseSyncStructureToUpper(true) .UseSyncStructureToUpper(true)
//.UseNoneCommandParameter(true)
.UseMonitorCommand(
cmd => {
Trace.WriteLine(cmd.CommandText);
}, //监听SQL命令对象在执行前
(cmd, traceLog) => {
Console.WriteLine(traceLog);
}) //监听SQL命令对象在执行后
.Build()); .Build());
public static IFreeSql oracle = oracleLazy.Value; public static IFreeSql oracle = oracleLazy.Value;
@ -40,6 +56,13 @@ public class g {
.UseConnectionString(FreeSql.DataType.Sqlite, @"Data Source=|DataDirectory|\document.db;Attachs=xxxtb.db;Pooling=true;Max Pool Size=10") .UseConnectionString(FreeSql.DataType.Sqlite, @"Data Source=|DataDirectory|\document.db;Attachs=xxxtb.db;Pooling=true;Max Pool Size=10")
.UseAutoSyncStructure(true) .UseAutoSyncStructure(true)
.UseLazyLoading(true) .UseLazyLoading(true)
.UseMonitorCommand(
cmd => {
Trace.WriteLine(cmd.CommandText);
}, //监听SQL命令对象在执行前
(cmd, traceLog) => {
Console.WriteLine(traceLog);
}) //监听SQL命令对象在执行后
.Build()); .Build());
public static IFreeSql sqlite = sqliteLazy.Value; public static IFreeSql sqlite = sqliteLazy.Value;
} }

View File

@ -216,8 +216,8 @@ namespace FreeSql.Internal {
case ExpressionType.Quote: return ExpressionLambdaToSql((exp as UnaryExpression)?.Operand, _tables, _selectColumnMap, getSelectGroupingMapString, tbtype, isQuoteName); case ExpressionType.Quote: return ExpressionLambdaToSql((exp as UnaryExpression)?.Operand, _tables, _selectColumnMap, getSelectGroupingMapString, tbtype, isQuoteName);
case ExpressionType.Lambda: return ExpressionLambdaToSql((exp as LambdaExpression)?.Body, _tables, _selectColumnMap, getSelectGroupingMapString, tbtype, isQuoteName); case ExpressionType.Lambda: return ExpressionLambdaToSql((exp as LambdaExpression)?.Body, _tables, _selectColumnMap, getSelectGroupingMapString, tbtype, isQuoteName);
case ExpressionType.Convert: case ExpressionType.Convert:
var othercExp = ExpressionLambdaToSqlOther(exp, _tables, _selectColumnMap, getSelectGroupingMapString, tbtype, isQuoteName); //var othercExp = ExpressionLambdaToSqlOther(exp, _tables, _selectColumnMap, getSelectGroupingMapString, tbtype, isQuoteName);
if (string.IsNullOrEmpty(othercExp) == false) return othercExp; //if (string.IsNullOrEmpty(othercExp) == false) return othercExp;
return ExpressionLambdaToSql((exp as UnaryExpression)?.Operand, _tables, _selectColumnMap, getSelectGroupingMapString, tbtype, isQuoteName); return ExpressionLambdaToSql((exp as UnaryExpression)?.Operand, _tables, _selectColumnMap, getSelectGroupingMapString, tbtype, isQuoteName);
case ExpressionType.Negate: case ExpressionType.Negate:
case ExpressionType.NegateChecked: return "-" + ExpressionLambdaToSql((exp as UnaryExpression)?.Operand, _tables, _selectColumnMap, getSelectGroupingMapString, tbtype, isQuoteName); case ExpressionType.NegateChecked: return "-" + ExpressionLambdaToSql((exp as UnaryExpression)?.Operand, _tables, _selectColumnMap, getSelectGroupingMapString, tbtype, isQuoteName);

View File

@ -28,7 +28,7 @@ namespace FreeSql.MySql {
if (param == null) return "NULL"; if (param == null) return "NULL";
if (param is bool || param is bool?) if (param is bool || param is bool?)
return (bool)param ? 1 : 0; return (bool)param ? 1 : 0;
else if (param is string) else if (param is string || param is char)
return string.Concat("'", param.ToString().Replace("'", "''"), "'"); return string.Concat("'", param.ToString().Replace("'", "''"), "'");
else if (param is Enum) else if (param is Enum)
return string.Concat("'", param.ToString().Replace("'", "''"), "'"); //((Enum)param).ToInt64(); return string.Concat("'", param.ToString().Replace("'", "''"), "'"); //((Enum)param).ToInt64();

View File

@ -17,6 +17,8 @@ namespace FreeSql.MySql {
switch (exp.NodeType) { switch (exp.NodeType) {
case ExpressionType.Convert: case ExpressionType.Convert:
var operandExp = (exp as UnaryExpression)?.Operand; var operandExp = (exp as UnaryExpression)?.Operand;
var gentype = exp.Type.NullableTypeOrThis();
if (gentype != exp.Type.NullableTypeOrThis()) {
switch (exp.Type.NullableTypeOrThis().ToString()) { switch (exp.Type.NullableTypeOrThis().ToString()) {
case "System.Boolean": return $"({getExp(operandExp)} not in ('0','false'))"; case "System.Boolean": return $"({getExp(operandExp)} not in ('0','false'))";
case "System.Byte": return $"cast({getExp(operandExp)} as unsigned)"; case "System.Byte": return $"cast({getExp(operandExp)} as unsigned)";
@ -35,6 +37,7 @@ namespace FreeSql.MySql {
case "System.UInt64": return $"cast({getExp(operandExp)} as unsigned)"; case "System.UInt64": return $"cast({getExp(operandExp)} as unsigned)";
case "System.Guid": return $"substr(cast({getExp(operandExp)} as char), 1, 36)"; case "System.Guid": return $"substr(cast({getExp(operandExp)} as char), 1, 36)";
} }
}
break; break;
case ExpressionType.Call: case ExpressionType.Call:
var callExp = exp as MethodCallExpression; var callExp = exp as MethodCallExpression;

View File

@ -15,9 +15,10 @@ namespace FreeSql.Oracle.Curd {
_orm.CodeFirst.SyncStructure(_tables.Select(a => a.Table.Type).ToArray()); _orm.CodeFirst.SyncStructure(_tables.Select(a => a.Table.Type).ToArray());
var sb = new StringBuilder(); var sb = new StringBuilder();
var sbnav = new StringBuilder();
sb.Append(_select); sb.Append(_select);
sb.Append(field); sb.Append(field);
if (string.IsNullOrEmpty(_orderby) == false && (_skip > 0 || _limit > 0)) sb.Append(", ROWNUM AS __rownum__"); if (string.IsNullOrEmpty(_orderby) && _skip > 0) sb.Append(", ROWNUM AS \"__rownum__\"");
sb.Append(" \r\nFROM "); sb.Append(" \r\nFROM ");
var tbsjoin = _tables.Where(a => a.Type != SelectTableInfoType.From).ToArray(); var tbsjoin = _tables.Where(a => a.Type != SelectTableInfoType.From).ToArray();
var tbsfrom = _tables.Where(a => a.Type == SelectTableInfoType.From).ToArray(); var tbsfrom = _tables.Where(a => a.Type == SelectTableInfoType.From).ToArray();
@ -25,9 +26,15 @@ namespace FreeSql.Oracle.Curd {
sb.Append(_commonUtils.QuoteSqlName(tableRuleInvoke(tbsfrom[a].Table.Type, tbsfrom[a].Table.DbName))).Append(" ").Append(tbsfrom[a].Alias); sb.Append(_commonUtils.QuoteSqlName(tableRuleInvoke(tbsfrom[a].Table.Type, tbsfrom[a].Table.DbName))).Append(" ").Append(tbsfrom[a].Alias);
if (tbsjoin.Length > 0) { if (tbsjoin.Length > 0) {
//如果存在 join 查询,则处理 from t1, t2 改为 from t1 inner join t2 on 1 = 1 //如果存在 join 查询,则处理 from t1, t2 改为 from t1 inner join t2 on 1 = 1
for (var b = 1; b < tbsfrom.Length; b++) for (var b = 1; b < tbsfrom.Length; b++) {
sb.Append(" \r\nLEFT JOIN ").Append(_commonUtils.QuoteSqlName(tableRuleInvoke(tbsfrom[b].Table.Type, tbsfrom[b].Table.DbName))).Append(" ").Append(tbsfrom[b].Alias).Append(" ON 1 = 1"); sb.Append(" \r\nLEFT JOIN ").Append(_commonUtils.QuoteSqlName(tableRuleInvoke(tbsfrom[b].Table.Type, tbsfrom[b].Table.DbName))).Append(" ").Append(tbsfrom[b].Alias);
if (string.IsNullOrEmpty(tbsfrom[b].NavigateCondition) && string.IsNullOrEmpty(tbsfrom[b].On)) sb.Append(" ON 1 = 1");
else sb.Append(" ON ").Append(tbsfrom[b].NavigateCondition ?? tbsfrom[b].On);
}
break; break;
} else {
if (!string.IsNullOrEmpty(tbsfrom[a].NavigateCondition)) sbnav.Append(" AND (").Append(tbsfrom[a].NavigateCondition).Append(")");
if (!string.IsNullOrEmpty(tbsfrom[a].On)) sbnav.Append(" AND (").Append(tbsfrom[a].On).Append(")");
} }
if (a < tbsfrom.Length - 1) sb.Append(", "); if (a < tbsfrom.Length - 1) sb.Append(", ");
} }
@ -44,26 +51,22 @@ namespace FreeSql.Oracle.Curd {
sb.Append(" \r\nRIGHT JOIN "); sb.Append(" \r\nRIGHT JOIN ");
break; break;
} }
sb.Append(_commonUtils.QuoteSqlName(tableRuleInvoke(tb.Table.Type, tb.Table.DbName))).Append(" ").Append(tb.Alias).Append(" ON ").Append(tb.On); sb.Append(_commonUtils.QuoteSqlName(tableRuleInvoke(tb.Table.Type, tb.Table.DbName))).Append(" ").Append(tb.Alias).Append(" ON ").Append(tb.On ?? tb.NavigateCondition);
if (!string.IsNullOrEmpty(tb.On) && !string.IsNullOrEmpty(tb.NavigateCondition)) sbnav.Append(" AND (").Append(tb.NavigateCondition).Append(")");
} }
if (_join.Length > 0) sb.Append(_join); if (_join.Length > 0) sb.Append(_join);
var sbqf = new StringBuilder(); sbnav.Append(_where);
foreach (var tb in _tables) { foreach (var tb in _tables) {
if (tb.Type == SelectTableInfoType.Parent) continue; if (tb.Type == SelectTableInfoType.Parent) continue;
if (string.IsNullOrEmpty(tb.Table.SelectFilter) == false) if (string.IsNullOrEmpty(tb.Table.SelectFilter) == false)
sbqf.Append(" AND (").Append(tb.Table.SelectFilter.Replace("a.", $"{tb.Alias}.")).Append(")"); sbnav.Append(" AND (").Append(tb.Table.SelectFilter.Replace("a.", $"{tb.Alias}.")).Append(")");
} }
if (_where.Length > 0) { if (string.IsNullOrEmpty(_orderby) && (_skip > 0 || _limit > 0)) {
sb.Append(" \r\nWHERE ").Append(_where.ToString().Substring(5)); sbnav.Append(" AND ROWNUM < ").Append(_skip + _limit + 1);
if (string.IsNullOrEmpty(_orderby) == false && (_skip > 0 || _limit > 0)) sb.Append(" AND ROWNUM < ").Append(_skip + _limit + 1);
if (sbqf.Length > 0) sb.Append(sbqf.ToString());
} else {
if (string.IsNullOrEmpty(_orderby) == false && (_skip > 0 || _limit > 0)) {
sb.Append(" \r\nWHERE ROWNUM < ").Append(_skip + _limit + 1);
if (sbqf.Length > 0) sb.Append(sbqf.Remove(0, 5));
} }
else if (sbqf.Length > 0) sb.Append(" \r\nWHERE ").Append(sbqf.Remove(0, 5)); if (sbnav.Length > 0) {
sb.Append(" \r\nWHERE ").Append(sbnav.Remove(0, 5));
} }
if (string.IsNullOrEmpty(_groupby) == false) { if (string.IsNullOrEmpty(_groupby) == false) {
sb.Append(_groupby); sb.Append(_groupby);
@ -71,12 +74,17 @@ namespace FreeSql.Oracle.Curd {
sb.Append(" \r\nHAVING ").Append(_having.Substring(5)); sb.Append(" \r\nHAVING ").Append(_having.Substring(5));
} }
sb.Append(_orderby); sb.Append(_orderby);
if (string.IsNullOrEmpty(_orderby) == false) {
if (_skip > 0 && _limit > 0) sb.Insert(0, "SELECT t.* FROM (SELECT ROWNUM AS rt.*, __rownum__ FROM (").Append(") rt WHERE ROWNUM < ").Append(_skip + _limit + 1).Append(") t WHERE t.__rownum__ > ").Append(_skip);
else if (_skip > 0 || _limit > 0) sb.Insert(0, "SELECT t.* FROM (").Append(") t WHERE ROWNUM < ").Append(_skip + _limit + 1);
} else if (_skip > 0)
sb.Insert(0, "SELECT t.* FROM (").Append(") t WHERE t.__rownum__ > ").Append(_skip);
if (string.IsNullOrEmpty(_orderby)) {
if (_skip > 0)
sb.Insert(0, "SELECT t.* FROM (").Append(") t WHERE t.\"__rownum__\" > ").Append(_skip);
} else {
if (_skip > 0 && _limit > 0) sb.Insert(0, "SELECT t.* FROM (SELECT rt.*, ROWNUM AS \"__rownum__\" FROM (").Append(") rt WHERE ROWNUM < ").Append(_skip + _limit + 1).Append(") t WHERE t.\"__rownum__\" > ").Append(_skip);
else if (_skip > 0) sb.Insert(0, "SELECT t.* FROM (").Append(") t WHERE ROWNUM > ").Append(_skip);
else if (_limit > 0) sb.Insert(0, "SELECT t.* FROM (").Append(") t WHERE ROWNUM < ").Append(_limit + 1);
}
sbnav.Clear();
return sb.ToString(); return sb.ToString();
} }

View File

@ -28,7 +28,7 @@ namespace FreeSql.Oracle {
if (param == null) return "NULL"; if (param == null) return "NULL";
if (param is bool || param is bool?) if (param is bool || param is bool?)
return (bool)param ? 1 : 0; return (bool)param ? 1 : 0;
else if (param is string) else if (param is string || param is char)
return string.Concat("'", param.ToString().Replace("'", "''"), "'"); return string.Concat("'", param.ToString().Replace("'", "''"), "'");
else if (param is Enum) else if (param is Enum)
return ((Enum)param).ToInt64(); return ((Enum)param).ToInt64();

View File

@ -187,7 +187,7 @@ where owner={{0}} and table_name={{1}}".FormatOracleSQL(tboldname ?? tbname);
//sbalter.Append("execute immediate 'ALTER TABLE ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" MODIFY (").Append(_commonUtils.QuoteSqlName(tbstructcol.column)).Append(" ").Append(dbtypeNoneNotNull).Append(")';\r\n"); //sbalter.Append("execute immediate 'ALTER TABLE ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" MODIFY (").Append(_commonUtils.QuoteSqlName(tbstructcol.column)).Append(" ").Append(dbtypeNoneNotNull).Append(")';\r\n");
if (tbcol.Attribute.IsNullable != tbstructcol.is_nullable) { if (tbcol.Attribute.IsNullable != tbstructcol.is_nullable) {
if (tbcol.Attribute.IsNullable == false) if (tbcol.Attribute.IsNullable == false)
sbalter.Append("execute immediate 'UPDATE ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" SET ").Append(_commonUtils.QuoteSqlName(tbstructcol.column)).Append(_commonUtils.FormatSql(" = {0}", tbcol.Attribute.DbDefautValue).Replace("'", "''")).Append(" WHERE ").Append(_commonUtils.QuoteSqlName(tbstructcol.column)).Append(" IS NULL';\r\n"); sbalter.Append("execute immediate 'UPDATE ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" SET ").Append(_commonUtils.QuoteSqlName(tbstructcol.column)).Append(" = ").Append(_commonUtils.FormatSql("{0}", tbcol.Attribute.DbDefautValue).Replace("'", "''")).Append(" WHERE ").Append(_commonUtils.QuoteSqlName(tbstructcol.column)).Append(" IS NULL';\r\n");
sbalter.Append("execute immediate 'ALTER TABLE ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" MODIFY ").Append(_commonUtils.QuoteSqlName(tbstructcol.column)).Append(" ").Append(tbcol.Attribute.IsNullable == true ? "" : "NOT").Append(" NULL';\r\n"); sbalter.Append("execute immediate 'ALTER TABLE ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" MODIFY ").Append(_commonUtils.QuoteSqlName(tbstructcol.column)).Append(" ").Append(tbcol.Attribute.IsNullable == true ? "" : "NOT").Append(" NULL';\r\n");
} }
if (tbcol.Attribute.IsIdentity != tbstructcol.is_identity) if (tbcol.Attribute.IsIdentity != tbstructcol.is_identity)
@ -200,7 +200,7 @@ where owner={{0}} and table_name={{1}}".FormatOracleSQL(tboldname ?? tbname);
//添加列 //添加列
sbalter.Append("execute immediate 'ALTER TABLE ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" ADD (").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(" ").Append(dbtypeNoneNotNull).Append(")';\r\n"); sbalter.Append("execute immediate 'ALTER TABLE ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" ADD (").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(" ").Append(dbtypeNoneNotNull).Append(")';\r\n");
if (tbcol.Attribute.IsNullable == false) { if (tbcol.Attribute.IsNullable == false) {
sbalter.Append("execute immediate 'UPDATE ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" SET ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(_commonUtils.FormatSql(" = {0}", tbcol.Attribute.DbDefautValue).Replace("'", "''")).Append("';\r\n"); sbalter.Append("execute immediate 'UPDATE ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" SET ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(" = ").Append(_commonUtils.FormatSql("{0}", tbcol.Attribute.DbDefautValue).Replace("'", "''")).Append("';\r\n");
sbalter.Append("execute immediate 'ALTER TABLE ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" MODIFY ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(" NOT NULL';\r\n"); sbalter.Append("execute immediate 'ALTER TABLE ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" MODIFY ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(" NOT NULL';\r\n");
} }
if (tbcol.Attribute.IsIdentity == true) seqcols.Add((tbcol, tbname, tbcol.Attribute.IsIdentity == true)); if (tbcol.Attribute.IsIdentity == true) seqcols.Add((tbcol, tbname, tbcol.Attribute.IsIdentity == true));
@ -262,12 +262,12 @@ where owner={{0}} and table_name={{1}}".FormatOracleSQL(tboldname ?? tbname);
var tbname2 = _commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}"); var tbname2 = _commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}");
var colname2 = _commonUtils.QuoteSqlName(seqcol.Item1.Attribute.Name); var colname2 = _commonUtils.QuoteSqlName(seqcol.Item1.Attribute.Name);
if (dicDeclare.ContainsKey(seqname) == false) { if (dicDeclare.ContainsKey(seqname) == false) {
sbDeclare.Append("\r\n").Append(seqname).Append("_exists NUMBER; \r\n"); sbDeclare.Append("\r\n").Append(seqname).Append("IS NUMBER; \r\n");
dicDeclare.Add(seqname, true); dicDeclare.Add(seqname, true);
} }
sb.Append(seqname).Append("_exists := 0; \r\n") sb.Append(seqname).Append("IS := 0; \r\n")
.Append(" select count(1) into ").Append(seqname).Append("_exists from user_sequences where sequence_name={0}; \r\n".FormatOracleSQL(seqname)) .Append(" select count(1) into ").Append(seqname).Append("IS from user_sequences where sequence_name={0}; \r\n".FormatOracleSQL(seqname))
.Append("if ").Append(seqname).Append("_exists > 0 then \r\n") .Append("if ").Append(seqname).Append("IS > 0 then \r\n")
.Append(" execute immediate 'DROP SEQUENCE ").Append(_commonUtils.QuoteSqlName(seqname)).Append("';\r\n") .Append(" execute immediate 'DROP SEQUENCE ").Append(_commonUtils.QuoteSqlName(seqname)).Append("';\r\n")
.Append("end if; \r\n"); .Append("end if; \r\n");
if (seqcol.Item3) { if (seqcol.Item3) {
@ -280,12 +280,12 @@ where owner={{0}} and table_name={{1}}".FormatOracleSQL(tboldname ?? tbname);
.Append(".nextval into :new.").Append(colname2).Append(" from dual;\r\nend;';\r\n"); .Append(".nextval into :new.").Append(colname2).Append(" from dual;\r\nend;';\r\n");
} else { } else {
if (dicDeclare.ContainsKey(tiggerName) == false) { if (dicDeclare.ContainsKey(tiggerName) == false) {
sbDeclare.Append("\r\n").Append(tiggerName).Append("_exists NUMBER; \r\n"); sbDeclare.Append("\r\n").Append(tiggerName).Append("IS NUMBER; \r\n");
dicDeclare.Add(tiggerName, true); dicDeclare.Add(tiggerName, true);
} }
sb.Append(tiggerName).Append("_exists := 0; \r\n") sb.Append(tiggerName).Append("IS := 0; \r\n")
.Append(" select count(1) into ").Append(tiggerName).Append("_exists from user_triggers where trigger_name={0}; \r\n".FormatOracleSQL(tiggerName)) .Append(" select count(1) into ").Append(tiggerName).Append("IS from user_triggers where trigger_name={0}; \r\n".FormatOracleSQL(tiggerName))
.Append("if ").Append(tiggerName).Append("_exists > 0 then \r\n") .Append("if ").Append(tiggerName).Append("IS > 0 then \r\n")
.Append(" execute immediate 'DROP TRIGGER ").Append(_commonUtils.QuoteSqlName(tiggerName)).Append("';\r\n") .Append(" execute immediate 'DROP TRIGGER ").Append(_commonUtils.QuoteSqlName(tiggerName)).Append("';\r\n")
.Append("end if; \r\n"); .Append("end if; \r\n");
} }

View File

@ -17,6 +17,8 @@ namespace FreeSql.Oracle {
switch (exp.NodeType) { switch (exp.NodeType) {
case ExpressionType.Convert: case ExpressionType.Convert:
var operandExp = (exp as UnaryExpression)?.Operand; var operandExp = (exp as UnaryExpression)?.Operand;
var gentype = exp.Type.NullableTypeOrThis();
if (gentype != exp.Type.NullableTypeOrThis()) {
switch (exp.Type.NullableTypeOrThis().ToString()) { switch (exp.Type.NullableTypeOrThis().ToString()) {
//case "System.Boolean": return $"({getExp(operandExp)} not in ('0','false'))"; //case "System.Boolean": return $"({getExp(operandExp)} not in ('0','false'))";
case "System.Byte": return $"cast({getExp(operandExp)} as number)"; case "System.Byte": return $"cast({getExp(operandExp)} as number)";
@ -35,6 +37,7 @@ namespace FreeSql.Oracle {
case "System.UInt64": return $"cast({getExp(operandExp)} as number)"; case "System.UInt64": return $"cast({getExp(operandExp)} as number)";
case "System.Guid": return $"substr(to_char({getExp(operandExp)}), 1, 36)"; case "System.Guid": return $"substr(to_char({getExp(operandExp)}), 1, 36)";
} }
}
break; break;
case ExpressionType.Call: case ExpressionType.Call:
var callExp = exp as MethodCallExpression; var callExp = exp as MethodCallExpression;

View File

@ -15,6 +15,7 @@ namespace FreeSql.PostgreSQL.Curd {
_orm.CodeFirst.SyncStructure(_tables.Select(a => a.Table.Type).ToArray()); _orm.CodeFirst.SyncStructure(_tables.Select(a => a.Table.Type).ToArray());
var sb = new StringBuilder(); var sb = new StringBuilder();
var sbnav = new StringBuilder();
sb.Append(_select).Append(field).Append(" \r\nFROM "); sb.Append(_select).Append(field).Append(" \r\nFROM ");
var tbsjoin = _tables.Where(a => a.Type != SelectTableInfoType.From).ToArray(); var tbsjoin = _tables.Where(a => a.Type != SelectTableInfoType.From).ToArray();
var tbsfrom = _tables.Where(a => a.Type == SelectTableInfoType.From).ToArray(); var tbsfrom = _tables.Where(a => a.Type == SelectTableInfoType.From).ToArray();
@ -22,9 +23,15 @@ namespace FreeSql.PostgreSQL.Curd {
sb.Append(_commonUtils.QuoteSqlName(tableRuleInvoke(tbsfrom[a].Table.Type, tbsfrom[a].Table.DbName))).Append(" ").Append(tbsfrom[a].Alias); sb.Append(_commonUtils.QuoteSqlName(tableRuleInvoke(tbsfrom[a].Table.Type, tbsfrom[a].Table.DbName))).Append(" ").Append(tbsfrom[a].Alias);
if (tbsjoin.Length > 0) { if (tbsjoin.Length > 0) {
//如果存在 join 查询,则处理 from t1, t2 改为 from t1 inner join t2 on 1 = 1 //如果存在 join 查询,则处理 from t1, t2 改为 from t1 inner join t2 on 1 = 1
for (var b = 1; b < tbsfrom.Length; b++) for (var b = 1; b < tbsfrom.Length; b++) {
sb.Append(" \r\nLEFT JOIN ").Append(_commonUtils.QuoteSqlName(tableRuleInvoke(tbsfrom[b].Table.Type, tbsfrom[b].Table.DbName))).Append(" ").Append(tbsfrom[b].Alias).Append(" ON 1 = 1"); sb.Append(" \r\nLEFT JOIN ").Append(_commonUtils.QuoteSqlName(tableRuleInvoke(tbsfrom[b].Table.Type, tbsfrom[b].Table.DbName))).Append(" ").Append(tbsfrom[b].Alias);
if (string.IsNullOrEmpty(tbsfrom[b].NavigateCondition) && string.IsNullOrEmpty(tbsfrom[b].On)) sb.Append(" ON 1 = 1");
else sb.Append(" ON ").Append(tbsfrom[b].NavigateCondition ?? tbsfrom[b].On);
}
break; break;
} else {
if (!string.IsNullOrEmpty(tbsfrom[a].NavigateCondition)) sbnav.Append(" AND (").Append(tbsfrom[a].NavigateCondition).Append(")");
if (!string.IsNullOrEmpty(tbsfrom[a].On)) sbnav.Append(" AND (").Append(tbsfrom[a].On).Append(")");
} }
if (a < tbsfrom.Length - 1) sb.Append(", "); if (a < tbsfrom.Length - 1) sb.Append(", ");
} }
@ -41,21 +48,19 @@ namespace FreeSql.PostgreSQL.Curd {
sb.Append(" \r\nRIGHT JOIN "); sb.Append(" \r\nRIGHT JOIN ");
break; break;
} }
sb.Append(_commonUtils.QuoteSqlName(tableRuleInvoke(tb.Table.Type, tb.Table.DbName))).Append(" ").Append(tb.Alias).Append(" ON ").Append(tb.On); sb.Append(_commonUtils.QuoteSqlName(tableRuleInvoke(tb.Table.Type, tb.Table.DbName))).Append(" ").Append(tb.Alias).Append(" ON ").Append(tb.On ?? tb.NavigateCondition);
if (!string.IsNullOrEmpty(tb.On) && !string.IsNullOrEmpty(tb.NavigateCondition)) sbnav.Append(" AND (").Append(tb.NavigateCondition).Append(")");
} }
if (_join.Length > 0) sb.Append(_join); if (_join.Length > 0) sb.Append(_join);
var sbqf = new StringBuilder(); sbnav.Append(_where);
foreach (var tb in _tables) { foreach (var tb in _tables) {
if (tb.Type == SelectTableInfoType.Parent) continue; if (tb.Type == SelectTableInfoType.Parent) continue;
if (string.IsNullOrEmpty(tb.Table.SelectFilter) == false) if (string.IsNullOrEmpty(tb.Table.SelectFilter) == false)
sbqf.Append(" AND (").Append(tb.Table.SelectFilter.Replace("a.", $"{tb.Alias}.")).Append(")"); sbnav.Append(" AND (").Append(tb.Table.SelectFilter.Replace("a.", $"{tb.Alias}.")).Append(")");
} }
if (_where.Length > 0) { if (sbnav.Length > 0) {
sb.Append(" \r\nWHERE ").Append(_where.ToString().Substring(5)); sb.Append(" \r\nWHERE ").Append(sbnav.Remove(0, 5));
if (sbqf.Length > 0) sb.Append(sbqf.ToString());
} else {
if (sbqf.Length > 0) sb.Append(" \r\nWHERE ").Append(sbqf.Remove(0, 5));
} }
if (string.IsNullOrEmpty(_groupby) == false) { if (string.IsNullOrEmpty(_groupby) == false) {
sb.Append(_groupby); sb.Append(_groupby);
@ -68,6 +73,7 @@ namespace FreeSql.PostgreSQL.Curd {
if (_skip > 0) if (_skip > 0)
sb.Append(" \r\noffset ").Append(_skip); sb.Append(" \r\noffset ").Append(_skip);
sbnav.Clear();
return sb.ToString(); return sb.ToString();
} }

View File

@ -32,7 +32,7 @@ namespace FreeSql.PostgreSQL {
if (param == null) return "NULL"; if (param == null) return "NULL";
if (param is bool || param is bool?) if (param is bool || param is bool?)
return (bool)param ? "'t'" : "'f'"; return (bool)param ? "'t'" : "'f'";
else if (param is string || param is Enum) else if (param is string || param is char || param is Enum)
return string.Concat("'", param.ToString().Replace("'", "''"), "'"); return string.Concat("'", param.ToString().Replace("'", "''"), "'");
else if (decimal.TryParse(string.Concat(param), out var trydec)) else if (decimal.TryParse(string.Concat(param), out var trydec))
return param; return param;

View File

@ -237,7 +237,7 @@ where ns.nspname = {0} and c.relname = {1}".FormatPostgreSQL(tboldname ?? tbname
} }
//添加列 //添加列
sbalter.Append("ALTER TABLE ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" ADD COLUMN ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(" ").Append(tbcol.Attribute.DbType.Split(' ').First()).Append(";\r\n"); sbalter.Append("ALTER TABLE ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" ADD COLUMN ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(" ").Append(tbcol.Attribute.DbType.Split(' ').First()).Append(";\r\n");
sbalter.Append("UPDATE ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" SET ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(_commonUtils.FormatSql(" = {0};\r\n", tbcol.Attribute.DbDefautValue)); sbalter.Append("UPDATE ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" SET ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(" = ").Append(_commonUtils.FormatSql("{0};\r\n", tbcol.Attribute.DbDefautValue));
if (tbcol.Attribute.IsNullable == false) sbalter.Append("ALTER TABLE ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" ALTER COLUMN ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(" SET NOT NULL;\r\n"); if (tbcol.Attribute.IsNullable == false) sbalter.Append("ALTER TABLE ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" ALTER COLUMN ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(" SET NOT NULL;\r\n");
if (tbcol.Attribute.IsIdentity == true) seqcols.Add((tbcol, tbname, tbcol.Attribute.IsIdentity == true)); if (tbcol.Attribute.IsIdentity == true) seqcols.Add((tbcol, tbname, tbcol.Attribute.IsIdentity == true));
} }

View File

@ -18,6 +18,8 @@ namespace FreeSql.PostgreSQL {
switch (exp.NodeType) { switch (exp.NodeType) {
case ExpressionType.Convert: case ExpressionType.Convert:
var operandExp = (exp as UnaryExpression)?.Operand; var operandExp = (exp as UnaryExpression)?.Operand;
var gentype = exp.Type.NullableTypeOrThis();
if (gentype != exp.Type.NullableTypeOrThis()) {
switch (exp.Type.NullableTypeOrThis().ToString()) { switch (exp.Type.NullableTypeOrThis().ToString()) {
case "System.Boolean": return $"(({getExp(operandExp)})::varchar not in ('0','false','f','no'))"; case "System.Boolean": return $"(({getExp(operandExp)})::varchar not in ('0','false','f','no'))";
case "System.Byte": return $"({getExp(operandExp)})::int2"; case "System.Byte": return $"({getExp(operandExp)})::int2";
@ -36,6 +38,7 @@ namespace FreeSql.PostgreSQL {
case "System.UInt64": return $"({getExp(operandExp)})::int8"; case "System.UInt64": return $"({getExp(operandExp)})::int8";
case "System.Guid": return $"({getExp(operandExp)})::uuid"; case "System.Guid": return $"({getExp(operandExp)})::uuid";
} }
}
break; break;
case ExpressionType.ArrayLength: case ExpressionType.ArrayLength:
var arrOperExp = getExp((exp as UnaryExpression).Operand); var arrOperExp = getExp((exp as UnaryExpression).Operand);
@ -94,33 +97,6 @@ namespace FreeSql.PostgreSQL {
if (objType == null) objType = callExp.Method.DeclaringType; if (objType == null) objType = callExp.Method.DeclaringType;
if (objType != null) { if (objType != null) {
var left = objExp == null ? null : getExp(objExp); var left = objExp == null ? null : getExp(objExp);
if (objType.IsArray || typeof(IList).IsAssignableFrom(callExp.Method.DeclaringType)) {
switch (callExp.Method.Name) {
case "Any":
if (left.StartsWith("(") || left.EndsWith(")")) left = $"array[{left.TrimStart('(').TrimEnd(')')}]";
return $"(case when {left} is null then 0 else array_length({left},1) end > 0)";
case "Contains":
//判断 in 或 array @> array
var right1 = getExp(callExp.Arguments[argIndex]);
if (left.StartsWith("array[") || left.EndsWith("]"))
return $"{right1} in ({left.Substring(6, left.Length - 7)})";
if (left.StartsWith("(") || left.EndsWith(")"))
return $"{right1} in {left}";
if (right1.StartsWith("(") || right1.EndsWith(")")) right1 = $"array[{right1.TrimStart('(').TrimEnd(')')}]";
return $"({left} @> array[{right1}])";
case "Concat":
if (left.StartsWith("(") || left.EndsWith(")")) left = $"array[{left.TrimStart('(').TrimEnd(')')}]";
var right2 = getExp(callExp.Arguments[argIndex]);
if (right2.StartsWith("(") || right2.EndsWith(")")) right2 = $"array[{right2.TrimStart('(').TrimEnd(')')}]";
return $"({left} || {right2})";
case "GetLength":
case "GetLongLength":
case "Length":
case "Count":
if (left.StartsWith("(") || left.EndsWith(")")) left = $"array[{left.TrimStart('(').TrimEnd(')')}]";
return $"case when {left} is null then 0 else array_length({left},1) end";
}
}
switch (objType.FullName) { switch (objType.FullName) {
case "Newtonsoft.Json.Linq.JToken": case "Newtonsoft.Json.Linq.JToken":
case "Newtonsoft.Json.Linq.JObject": case "Newtonsoft.Json.Linq.JObject":
@ -158,6 +134,33 @@ namespace FreeSql.PostgreSQL {
case "Values": return $"avals({left})"; case "Values": return $"avals({left})";
} }
} }
if (objType.IsArray || typeof(IList).IsAssignableFrom(callExp.Method.DeclaringType)) {
switch (callExp.Method.Name) {
case "Any":
if (left.StartsWith("(") || left.EndsWith(")")) left = $"array[{left.TrimStart('(').TrimEnd(')')}]";
return $"(case when {left} is null then 0 else array_length({left},1) end > 0)";
case "Contains":
//判断 in 或 array @> array
var right1 = getExp(callExp.Arguments[argIndex]);
if (left.StartsWith("array[") || left.EndsWith("]"))
return $"{right1} in ({left.Substring(6, left.Length - 7)})";
if (left.StartsWith("(") || left.EndsWith(")"))
return $"{right1} in {left}";
if (right1.StartsWith("(") || right1.EndsWith(")")) right1 = $"array[{right1.TrimStart('(').TrimEnd(')')}]";
return $"({left} @> array[{right1}])";
case "Concat":
if (left.StartsWith("(") || left.EndsWith(")")) left = $"array[{left.TrimStart('(').TrimEnd(')')}]";
var right2 = getExp(callExp.Arguments[argIndex]);
if (right2.StartsWith("(") || right2.EndsWith(")")) right2 = $"array[{right2.TrimStart('(').TrimEnd(')')}]";
return $"({left} || {right2})";
case "GetLength":
case "GetLongLength":
case "Length":
case "Count":
if (left.StartsWith("(") || left.EndsWith(")")) left = $"array[{left.TrimStart('(').TrimEnd(')')}]";
return $"case when {left} is null then 0 else array_length({left},1) end";
}
}
} }
break; break;
case ExpressionType.MemberAccess: case ExpressionType.MemberAccess:

View File

@ -11,7 +11,7 @@ namespace FreeSql.SqlServer.Curd {
class SqlServerSelect<T1> : FreeSql.Internal.CommonProvider.Select1Provider<T1> where T1 : class { class SqlServerSelect<T1> : FreeSql.Internal.CommonProvider.Select1Provider<T1> where T1 : class {
internal static string ToSqlStatic(CommonUtils _commonUtils, string _select, string field, StringBuilder _join, StringBuilder _where, string _groupby, string _having, string _orderby, int _skip, int _limit, List<SelectTableInfo> _tables, Func<Type, string, string> tableRuleInvoke, IFreeSql _orm) internal static string ToSqlStatic(CommonUtils _commonUtils, string _select, string field, StringBuilder _join, StringBuilder _where, string _groupby, string _having, string _orderby, int _skip, int _limit, List<SelectTableInfo> _tables, Func<Type, string, string> tableRuleInvoke, IFreeSql _orm)
=> (_commonUtils as SqlServerUtils).IsSelectRowNumber ? => !(_commonUtils as SqlServerUtils).IsSelectRowNumber ?
ToSqlStaticRowNumber(_commonUtils, _select, field, _join, _where, _groupby, _having, _orderby, _skip, _limit, _tables, tableRuleInvoke, _orm) : ToSqlStaticRowNumber(_commonUtils, _select, field, _join, _where, _groupby, _having, _orderby, _skip, _limit, _tables, tableRuleInvoke, _orm) :
ToSqlStaticOffsetFetchNext(_commonUtils, _select, field, _join, _where, _groupby, _having, _orderby, _skip, _limit, _tables, tableRuleInvoke, _orm); ToSqlStaticOffsetFetchNext(_commonUtils, _select, field, _join, _where, _groupby, _having, _orderby, _skip, _limit, _tables, tableRuleInvoke, _orm);
@ -21,6 +21,7 @@ namespace FreeSql.SqlServer.Curd {
_orm.CodeFirst.SyncStructure(_tables.Select(a => a.Table.Type).ToArray()); _orm.CodeFirst.SyncStructure(_tables.Select(a => a.Table.Type).ToArray());
var sb = new StringBuilder(); var sb = new StringBuilder();
var sbnav = new StringBuilder();
sb.Append(_select); sb.Append(_select);
if (_limit > 0) sb.Append("TOP ").Append(_skip + _limit).Append(" "); if (_limit > 0) sb.Append("TOP ").Append(_skip + _limit).Append(" ");
sb.Append(field); sb.Append(field);
@ -39,9 +40,15 @@ namespace FreeSql.SqlServer.Curd {
sb.Append(_commonUtils.QuoteSqlName(tableRuleInvoke(tbsfrom[a].Table.Type, tbsfrom[a].Table.DbName))).Append(" ").Append(tbsfrom[a].Alias); sb.Append(_commonUtils.QuoteSqlName(tableRuleInvoke(tbsfrom[a].Table.Type, tbsfrom[a].Table.DbName))).Append(" ").Append(tbsfrom[a].Alias);
if (tbsjoin.Length > 0) { if (tbsjoin.Length > 0) {
//如果存在 join 查询,则处理 from t1, t2 改为 from t1 inner join t2 on 1 = 1 //如果存在 join 查询,则处理 from t1, t2 改为 from t1 inner join t2 on 1 = 1
for (var b = 1; b < tbsfrom.Length; b++) for (var b = 1; b < tbsfrom.Length; b++) {
sb.Append(" \r\nLEFT JOIN ").Append(_commonUtils.QuoteSqlName(tableRuleInvoke(tbsfrom[b].Table.Type, tbsfrom[b].Table.DbName))).Append(" ").Append(tbsfrom[b].Alias).Append(" ON 1 = 1"); sb.Append(" \r\nLEFT JOIN ").Append(_commonUtils.QuoteSqlName(tableRuleInvoke(tbsfrom[b].Table.Type, tbsfrom[b].Table.DbName))).Append(" ").Append(tbsfrom[b].Alias);
if (string.IsNullOrEmpty(tbsfrom[b].NavigateCondition) && string.IsNullOrEmpty(tbsfrom[b].On)) sb.Append(" ON 1 = 1");
else sb.Append(" ON ").Append(tbsfrom[b].NavigateCondition ?? tbsfrom[b].On);
}
break; break;
} else {
if (!string.IsNullOrEmpty(tbsfrom[a].NavigateCondition)) sbnav.Append(" AND (").Append(tbsfrom[a].NavigateCondition).Append(")");
if (!string.IsNullOrEmpty(tbsfrom[a].On)) sbnav.Append(" AND (").Append(tbsfrom[a].On).Append(")");
} }
if (a < tbsfrom.Length - 1) sb.Append(", "); if (a < tbsfrom.Length - 1) sb.Append(", ");
} }
@ -58,21 +65,19 @@ namespace FreeSql.SqlServer.Curd {
sb.Append(" \r\nRIGHT JOIN "); sb.Append(" \r\nRIGHT JOIN ");
break; break;
} }
sb.Append(_commonUtils.QuoteSqlName(tableRuleInvoke(tb.Table.Type, tb.Table.DbName))).Append(" ").Append(tb.Alias).Append(" ON ").Append(tb.On); sb.Append(_commonUtils.QuoteSqlName(tableRuleInvoke(tb.Table.Type, tb.Table.DbName))).Append(" ").Append(tb.Alias).Append(" ON ").Append(tb.On ?? tb.NavigateCondition);
if (!string.IsNullOrEmpty(tb.On) && !string.IsNullOrEmpty(tb.NavigateCondition)) sbnav.Append(" AND (").Append(tb.NavigateCondition).Append(")");
} }
if (_join.Length > 0) sb.Append(_join); if (_join.Length > 0) sb.Append(_join);
var sbqf = new StringBuilder(); sbnav.Append(_where);
foreach (var tb in _tables) { foreach (var tb in _tables) {
if (tb.Type == SelectTableInfoType.Parent) continue; if (tb.Type == SelectTableInfoType.Parent) continue;
if (string.IsNullOrEmpty(tb.Table.SelectFilter) == false) if (string.IsNullOrEmpty(tb.Table.SelectFilter) == false)
sbqf.Append(" AND (").Append(tb.Table.SelectFilter.Replace("a.", $"{tb.Alias}.")).Append(")"); sbnav.Append(" AND (").Append(tb.Table.SelectFilter.Replace("a.", $"{tb.Alias}.")).Append(")");
} }
if (_where.Length > 0) { if (sbnav.Length > 0) {
sb.Append(" \r\nWHERE ").Append(_where.ToString().Substring(5)); sb.Append(" \r\nWHERE ").Append(sbnav.Remove(0, 5));
if (sbqf.Length > 0) sb.Append(sbqf.ToString());
} else {
if (sbqf.Length > 0) sb.Append(" \r\nWHERE ").Append(sbqf.Remove(0, 5));
} }
if (string.IsNullOrEmpty(_groupby) == false) { if (string.IsNullOrEmpty(_groupby) == false) {
sb.Append(_groupby); sb.Append(_groupby);
@ -84,6 +89,7 @@ namespace FreeSql.SqlServer.Curd {
else else
sb.Insert(0, "WITH t AS ( ").Append(" ) SELECT t.* FROM t where __rownum__ > ").Append(_skip); sb.Insert(0, "WITH t AS ( ").Append(" ) SELECT t.* FROM t where __rownum__ > ").Append(_skip);
sbnav.Clear();
return sb.ToString(); return sb.ToString();
} }
#endregion #endregion
@ -94,6 +100,7 @@ namespace FreeSql.SqlServer.Curd {
_orm.CodeFirst.SyncStructure(_tables.Select(a => a.Table.Type).ToArray()); _orm.CodeFirst.SyncStructure(_tables.Select(a => a.Table.Type).ToArray());
var sb = new StringBuilder(); var sb = new StringBuilder();
var sbnav = new StringBuilder();
sb.Append(_select); sb.Append(_select);
if (_skip <= 0 && _limit > 0) sb.Append("TOP ").Append(_limit).Append(" "); if (_skip <= 0 && _limit > 0) sb.Append("TOP ").Append(_limit).Append(" ");
sb.Append(field); sb.Append(field);
@ -104,9 +111,15 @@ namespace FreeSql.SqlServer.Curd {
sb.Append(_commonUtils.QuoteSqlName(tableRuleInvoke(tbsfrom[a].Table.Type, tbsfrom[a].Table.DbName))).Append(" ").Append(tbsfrom[a].Alias); sb.Append(_commonUtils.QuoteSqlName(tableRuleInvoke(tbsfrom[a].Table.Type, tbsfrom[a].Table.DbName))).Append(" ").Append(tbsfrom[a].Alias);
if (tbsjoin.Length > 0) { if (tbsjoin.Length > 0) {
//如果存在 join 查询,则处理 from t1, t2 改为 from t1 inner join t2 on 1 = 1 //如果存在 join 查询,则处理 from t1, t2 改为 from t1 inner join t2 on 1 = 1
for (var b = 1; b < tbsfrom.Length; b++) for (var b = 1; b < tbsfrom.Length; b++) {
sb.Append(" \r\nLEFT JOIN ").Append(_commonUtils.QuoteSqlName(tableRuleInvoke(tbsfrom[b].Table.Type, tbsfrom[b].Table.DbName))).Append(" ").Append(tbsfrom[b].Alias).Append(" ON 1 = 1"); sb.Append(" \r\nLEFT JOIN ").Append(_commonUtils.QuoteSqlName(tableRuleInvoke(tbsfrom[b].Table.Type, tbsfrom[b].Table.DbName))).Append(" ").Append(tbsfrom[b].Alias);
if (string.IsNullOrEmpty(tbsfrom[b].NavigateCondition) && string.IsNullOrEmpty(tbsfrom[b].On)) sb.Append(" ON 1 = 1");
else sb.Append(" ON ").Append(tbsfrom[b].NavigateCondition ?? tbsfrom[b].On);
}
break; break;
} else {
if (!string.IsNullOrEmpty(tbsfrom[a].NavigateCondition)) sbnav.Append(" AND (").Append(tbsfrom[a].NavigateCondition).Append(")");
if (!string.IsNullOrEmpty(tbsfrom[a].On)) sbnav.Append(" AND (").Append(tbsfrom[a].On).Append(")");
} }
if (a < tbsfrom.Length - 1) sb.Append(", "); if (a < tbsfrom.Length - 1) sb.Append(", ");
} }
@ -123,21 +136,19 @@ namespace FreeSql.SqlServer.Curd {
sb.Append(" \r\nRIGHT JOIN "); sb.Append(" \r\nRIGHT JOIN ");
break; break;
} }
sb.Append(_commonUtils.QuoteSqlName(tableRuleInvoke(tb.Table.Type, tb.Table.DbName))).Append(" ").Append(tb.Alias).Append(" ON ").Append(tb.On); sb.Append(_commonUtils.QuoteSqlName(tableRuleInvoke(tb.Table.Type, tb.Table.DbName))).Append(" ").Append(tb.Alias).Append(" ON ").Append(tb.On ?? tb.NavigateCondition);
if (!string.IsNullOrEmpty(tb.On) && !string.IsNullOrEmpty(tb.NavigateCondition)) sbnav.Append(" AND (").Append(tb.NavigateCondition).Append(")");
} }
if (_join.Length > 0) sb.Append(_join); if (_join.Length > 0) sb.Append(_join);
var sbqf = new StringBuilder(); sbnav.Append(_where);
foreach (var tb in _tables) { foreach (var tb in _tables) {
if (tb.Type == SelectTableInfoType.Parent) continue; if (tb.Type == SelectTableInfoType.Parent) continue;
if (string.IsNullOrEmpty(tb.Table.SelectFilter) == false) if (string.IsNullOrEmpty(tb.Table.SelectFilter) == false)
sbqf.Append(" AND (").Append(tb.Table.SelectFilter.Replace("a.", $"{tb.Alias}.")).Append(")"); sbnav.Append(" AND (").Append(tb.Table.SelectFilter.Replace("a.", $"{tb.Alias}.")).Append(")");
} }
if (_where.Length > 0) { if (sbnav.Length > 0) {
sb.Append(" \r\nWHERE ").Append(_where.ToString().Substring(5)); sb.Append(" \r\nWHERE ").Append(sbnav.Remove(0, 5));
if (sbqf.Length > 0) sb.Append(sbqf.ToString());
} else {
if (sbqf.Length > 0) sb.Append(" \r\nWHERE ").Append(sbqf.Remove(0, 5));
} }
if (string.IsNullOrEmpty(_groupby) == false) { if (string.IsNullOrEmpty(_groupby) == false) {
sb.Append(_groupby); sb.Append(_groupby);
@ -156,6 +167,7 @@ namespace FreeSql.SqlServer.Curd {
sb.Append(_orderby); sb.Append(_orderby);
} }
sbnav.Clear();
return sb.ToString(); return sb.ToString();
} }
#endregion #endregion

View File

@ -28,7 +28,7 @@ namespace FreeSql.SqlServer {
if (param == null) return "NULL"; if (param == null) return "NULL";
if (param is bool || param is bool?) if (param is bool || param is bool?)
return (bool)param ? 1 : 0; return (bool)param ? 1 : 0;
else if (param is string) else if (param is string || param is char)
return string.Concat("'", param.ToString().Replace("'", "''"), "'"); return string.Concat("'", param.ToString().Replace("'", "''"), "'");
else if (param is Enum) else if (param is Enum)
return ((Enum)param).ToInt64(); return ((Enum)param).ToInt64();

View File

@ -17,7 +17,9 @@ namespace FreeSql.SqlServer {
switch (exp.NodeType) { switch (exp.NodeType) {
case ExpressionType.Convert: case ExpressionType.Convert:
var operandExp = (exp as UnaryExpression)?.Operand; var operandExp = (exp as UnaryExpression)?.Operand;
switch (exp.Type.NullableTypeOrThis().ToString()) { var gentype = exp.Type.NullableTypeOrThis();
if (gentype != exp.Type.NullableTypeOrThis()) {
switch (gentype.ToString()) {
case "System.Boolean": return $"(cast({getExp(operandExp)} as varchar) not in ('0','false'))"; case "System.Boolean": return $"(cast({getExp(operandExp)} as varchar) not in ('0','false'))";
case "System.Byte": return $"cast({getExp(operandExp)} as tinyint)"; case "System.Byte": return $"cast({getExp(operandExp)} as tinyint)";
case "System.Char": return $"substring(cast({getExp(operandExp)} as nvarchar),1,1)"; case "System.Char": return $"substring(cast({getExp(operandExp)} as nvarchar),1,1)";
@ -35,6 +37,7 @@ namespace FreeSql.SqlServer {
case "System.UInt64": return $"cast({getExp(operandExp)} as bigint)"; case "System.UInt64": return $"cast({getExp(operandExp)} as bigint)";
case "System.Guid": return $"cast({getExp(operandExp)} as uniqueidentifier)"; case "System.Guid": return $"cast({getExp(operandExp)} as uniqueidentifier)";
} }
}
break; break;
case ExpressionType.Call: case ExpressionType.Call:
var callExp = exp as MethodCallExpression; var callExp = exp as MethodCallExpression;

View File

@ -15,6 +15,7 @@ namespace FreeSql.Sqlite.Curd {
_orm.CodeFirst.SyncStructure(_tables.Select(a => a.Table.Type).ToArray()); _orm.CodeFirst.SyncStructure(_tables.Select(a => a.Table.Type).ToArray());
var sb = new StringBuilder(); var sb = new StringBuilder();
var sbnav = new StringBuilder();
sb.Append(_select).Append(field).Append(" \r\nFROM "); sb.Append(_select).Append(field).Append(" \r\nFROM ");
var tbsjoin = _tables.Where(a => a.Type != SelectTableInfoType.From).ToArray(); var tbsjoin = _tables.Where(a => a.Type != SelectTableInfoType.From).ToArray();
var tbsfrom = _tables.Where(a => a.Type == SelectTableInfoType.From).ToArray(); var tbsfrom = _tables.Where(a => a.Type == SelectTableInfoType.From).ToArray();
@ -22,9 +23,15 @@ namespace FreeSql.Sqlite.Curd {
sb.Append(_commonUtils.QuoteSqlName(tableRuleInvoke(tbsfrom[a].Table.Type, tbsfrom[a].Table.DbName))).Append(" ").Append(tbsfrom[a].Alias); sb.Append(_commonUtils.QuoteSqlName(tableRuleInvoke(tbsfrom[a].Table.Type, tbsfrom[a].Table.DbName))).Append(" ").Append(tbsfrom[a].Alias);
if (tbsjoin.Length > 0) { if (tbsjoin.Length > 0) {
//如果存在 join 查询,则处理 from t1, t2 改为 from t1 inner join t2 on 1 = 1 //如果存在 join 查询,则处理 from t1, t2 改为 from t1 inner join t2 on 1 = 1
for (var b = 1; b < tbsfrom.Length; b++) for (var b = 1; b < tbsfrom.Length; b++) {
sb.Append(" \r\nLEFT JOIN ").Append(_commonUtils.QuoteSqlName(tableRuleInvoke(tbsfrom[b].Table.Type, tbsfrom[b].Table.DbName))).Append(" ").Append(tbsfrom[b].Alias).Append(" ON 1 = 1"); sb.Append(" \r\nLEFT JOIN ").Append(_commonUtils.QuoteSqlName(tableRuleInvoke(tbsfrom[b].Table.Type, tbsfrom[b].Table.DbName))).Append(" ").Append(tbsfrom[b].Alias);
if (string.IsNullOrEmpty(tbsfrom[b].NavigateCondition) && string.IsNullOrEmpty(tbsfrom[b].On)) sb.Append(" ON 1 = 1");
else sb.Append(" ON ").Append(tbsfrom[b].NavigateCondition ?? tbsfrom[b].On);
}
break; break;
} else {
if (!string.IsNullOrEmpty(tbsfrom[a].NavigateCondition)) sbnav.Append(" AND (").Append(tbsfrom[a].NavigateCondition).Append(")");
if (!string.IsNullOrEmpty(tbsfrom[a].On)) sbnav.Append(" AND (").Append(tbsfrom[a].On).Append(")");
} }
if (a < tbsfrom.Length - 1) sb.Append(", "); if (a < tbsfrom.Length - 1) sb.Append(", ");
} }
@ -41,21 +48,19 @@ namespace FreeSql.Sqlite.Curd {
sb.Append(" \r\nRIGHT JOIN "); sb.Append(" \r\nRIGHT JOIN ");
break; break;
} }
sb.Append(_commonUtils.QuoteSqlName(tableRuleInvoke(tb.Table.Type, tb.Table.DbName))).Append(" ").Append(tb.Alias).Append(" ON ").Append(tb.On); sb.Append(_commonUtils.QuoteSqlName(tableRuleInvoke(tb.Table.Type, tb.Table.DbName))).Append(" ").Append(tb.Alias).Append(" ON ").Append(tb.On ?? tb.NavigateCondition);
if (!string.IsNullOrEmpty(tb.On) && !string.IsNullOrEmpty(tb.NavigateCondition)) sbnav.Append(" AND (").Append(tb.NavigateCondition).Append(")");
} }
if (_join.Length > 0) sb.Append(_join); if (_join.Length > 0) sb.Append(_join);
var sbqf = new StringBuilder(); sbnav.Append(_where);
foreach (var tb in _tables) { foreach (var tb in _tables) {
if (tb.Type == SelectTableInfoType.Parent) continue; if (tb.Type == SelectTableInfoType.Parent) continue;
if (string.IsNullOrEmpty(tb.Table.SelectFilter) == false) if (string.IsNullOrEmpty(tb.Table.SelectFilter) == false)
sbqf.Append(" AND (").Append(tb.Table.SelectFilter.Replace("a.", $"{tb.Alias}.")).Append(")"); sbnav.Append(" AND (").Append(tb.Table.SelectFilter.Replace("a.", $"{tb.Alias}.")).Append(")");
} }
if (_where.Length > 0) { if (sbnav.Length > 0) {
sb.Append(" \r\nWHERE ").Append(_where.ToString().Substring(5)); sb.Append(" \r\nWHERE ").Append(sbnav.Remove(0, 5));
if (sbqf.Length > 0) sb.Append(sbqf.ToString());
} else {
if (sbqf.Length > 0) sb.Append(" \r\nWHERE ").Append(sbqf.Remove(0, 5));
} }
if (string.IsNullOrEmpty(_groupby) == false) { if (string.IsNullOrEmpty(_groupby) == false) {
sb.Append(_groupby); sb.Append(_groupby);
@ -66,6 +71,7 @@ namespace FreeSql.Sqlite.Curd {
if (_skip > 0 || _limit > 0) if (_skip > 0 || _limit > 0)
sb.Append(" \r\nlimit ").Append(Math.Max(0, _skip)).Append(",").Append(_limit > 0 ? _limit : -1); sb.Append(" \r\nlimit ").Append(Math.Max(0, _skip)).Append(",").Append(_limit > 0 ? _limit : -1);
sbnav.Clear();
return sb.ToString(); return sb.ToString();
} }

View File

@ -28,7 +28,7 @@ namespace FreeSql.Sqlite {
if (param == null) return "NULL"; if (param == null) return "NULL";
if (param is bool || param is bool?) if (param is bool || param is bool?)
return (bool)param ? 1 : 0; return (bool)param ? 1 : 0;
else if (param is string) else if (param is string || param is char)
return string.Concat("'", param.ToString().Replace("'", "''"), "'"); return string.Concat("'", param.ToString().Replace("'", "''"), "'");
else if (param is Enum) else if (param is Enum)
return ((Enum)param).ToInt64(); return ((Enum)param).ToInt64();

View File

@ -17,6 +17,8 @@ namespace FreeSql.Sqlite {
switch (exp.NodeType) { switch (exp.NodeType) {
case ExpressionType.Convert: case ExpressionType.Convert:
var operandExp = (exp as UnaryExpression)?.Operand; var operandExp = (exp as UnaryExpression)?.Operand;
var gentype = exp.Type.NullableTypeOrThis();
if (gentype != exp.Type.NullableTypeOrThis()) {
switch (exp.Type.NullableTypeOrThis().ToString()) { switch (exp.Type.NullableTypeOrThis().ToString()) {
case "System.Boolean": return $"({getExp(operandExp)} not in ('0','false'))"; case "System.Boolean": return $"({getExp(operandExp)} not in ('0','false'))";
case "System.Byte": return $"cast({getExp(operandExp)} as int2)"; case "System.Byte": return $"cast({getExp(operandExp)} as int2)";
@ -35,6 +37,7 @@ namespace FreeSql.Sqlite {
case "System.UInt64": return $"cast({getExp(operandExp)} as decimal(21,0))"; case "System.UInt64": return $"cast({getExp(operandExp)} as decimal(21,0))";
case "System.Guid": return $"substr(cast({getExp(operandExp)} as character), 1, 36)"; case "System.Guid": return $"substr(cast({getExp(operandExp)} as character), 1, 36)";
} }
}
break; break;
case ExpressionType.Call: case ExpressionType.Call:
var callExp = exp as MethodCallExpression; var callExp = exp as MethodCallExpression;

330
readme.md
View File

@ -5,7 +5,7 @@ FreeSql 是一个功能强大的 .NETStandard 库,用于对象关系映射程
| FreeSql | [![nuget](https://img.shields.io/nuget/v/FreeSql.svg?style=flat-square)](https://www.nuget.org/packages/FreeSql) | [![stats](https://img.shields.io/nuget/dt/FreeSql.svg?style=flat-square)](https://www.nuget.org/stats/packages/FreeSql?groupby=Version) | | FreeSql | [![nuget](https://img.shields.io/nuget/v/FreeSql.svg?style=flat-square)](https://www.nuget.org/packages/FreeSql) | [![stats](https://img.shields.io/nuget/dt/FreeSql.svg?style=flat-square)](https://www.nuget.org/stats/packages/FreeSql?groupby=Version) |
| [FreeSql.Repository](https://github.com/2881099/FreeSql/wiki/Repository) | [![nuget](https://img.shields.io/nuget/v/FreeSql.Repository.svg?style=flat-square)](https://www.nuget.org/packages/FreeSql.Repository) | [![stats](https://img.shields.io/nuget/dt/FreeSql.Repository.svg?style=flat-square)](https://www.nuget.org/stats/packages/FreeSql.Repository?groupby=Version) | | [FreeSql.Repository](https://github.com/2881099/FreeSql/wiki/Repository) | [![nuget](https://img.shields.io/nuget/v/FreeSql.Repository.svg?style=flat-square)](https://www.nuget.org/packages/FreeSql.Repository) | [![stats](https://img.shields.io/nuget/dt/FreeSql.Repository.svg?style=flat-square)](https://www.nuget.org/stats/packages/FreeSql.Repository?groupby=Version) |
## 特性 # 特性
- [x] 支持 CodeFirst 迁移; - [x] 支持 CodeFirst 迁移;
- [x] 支持 DbFirst 从数据库导入实体类,支持三种模板生成器; - [x] 支持 DbFirst 从数据库导入实体类,支持三种模板生成器;
@ -17,11 +17,14 @@ FreeSql 是一个功能强大的 .NETStandard 库,用于对象关系映射程
- [x] 支持读写分离、分表分库,租户设计; - [x] 支持读写分离、分表分库,租户设计;
- [x] 支持多种数据库MySql/SqlServer/PostgreSQL/Oracle/Sqlite - [x] 支持多种数据库MySql/SqlServer/PostgreSQL/Oracle/Sqlite
[《Select查询数据文档》](https://github.com/2881099/FreeSql/wiki/%e6%9f%a5%e8%af%a2) | [《Update更新数据文档》](https://github.com/2881099/FreeSql/wiki/%e4%bf%ae%e6%94%b9) | [《Insert插入数据文档》](https://github.com/2881099/FreeSql/wiki/%e6%b7%bb%e5%8a%a0) | [《Delete删除数据文档》](https://github.com/2881099/FreeSql/wiki/%e5%88%a0%e9%99%a4) | | |
| - | - |
| 入门 | [《Select》](https://github.com/2881099/FreeSql/wiki/%e6%9f%a5%e8%af%a2) \| [《Update》](https://github.com/2881099/FreeSql/wiki/%e4%bf%ae%e6%94%b9) \| [《Insert》](https://github.com/2881099/FreeSql/wiki/%e6%b7%bb%e5%8a%a0) \| [《Delete》](https://github.com/2881099/FreeSql/wiki/%e5%88%a0%e9%99%a4) |
| 新手 | [《表达式函数》](https://github.com/2881099/FreeSql/wiki/%e8%a1%a8%e8%be%be%e5%bc%8f%e5%87%bd%e6%95%b0) \| [《CodeFirst》](https://github.com/2881099/FreeSql/wiki/CodeFirst) \| [《DbFirst》](https://github.com/2881099/FreeSql/wiki/DbFirst) |
| 高手 | [《Repository》](https://github.com/2881099/FreeSql/wiki/Repository) \| [《UnitOfWork》](https://github.com/2881099/FreeSql/wiki/%e5%b7%a5%e4%bd%9c%e5%8d%95%e5%85%83) \| [《过滤器》](https://github.com/2881099/FreeSql/wiki/%e8%bf%87%e6%bb%a4%e5%99%a8) |
| 不朽 | [《读写分离》](https://github.com/2881099/FreeSql/wiki/%e8%af%bb%e5%86%99%e5%88%86%e7%a6%bb) \| [《分区分表》](https://github.com/2881099/FreeSql/wiki/%e5%88%86%e5%8c%ba%e5%88%86%e8%a1%a8) \| [《租户》](https://github.com/2881099/FreeSql/wiki/%e7%a7%9f%e6%88%b7) \| [更新日志](https://github.com/2881099/FreeSql/wiki/%e6%9b%b4%e6%96%b0%e6%97%a5%e5%bf%97) |
[《Expression 表达式函数文档》](https://github.com/2881099/FreeSql/wiki/%e8%a1%a8%e8%be%be%e5%bc%8f%e5%87%bd%e6%95%b0) | [《CodeFirst 快速开发文档》](https://github.com/2881099/FreeSql/wiki/CodeFirst) | [《DbFirst 快速开发文档》](https://github.com/2881099/FreeSql/wiki/DbFirst) | [更新日志](https://github.com/2881099/FreeSql/wiki/%e6%9b%b4%e6%96%b0%e6%97%a5%e5%bf%97) # 快速开始
## 快速开始
```csharp ```csharp
var connstr = "Data Source=127.0.0.1;User ID=root;Password=root;" + var connstr = "Data Source=127.0.0.1;User ID=root;Password=root;" +
"Initial Catalog=cccddd;Charset=utf8;SslMode=none;Max pool size=10"; "Initial Catalog=cccddd;Charset=utf8;SslMode=none;Max pool size=10";
@ -67,46 +70,114 @@ FreeSql 使用模型执行数据访问,模型由实体类表示数据库表或
或者手动创建模型,基于模型创建或修改数据库,提供 ICodeFirst 同步结构的 API甚至可以做到开发阶段自动同步 或者手动创建模型,基于模型创建或修改数据库,提供 ICodeFirst 同步结构的 API甚至可以做到开发阶段自动同步
```csharp ```csharp
[Table(Name = "tb_topic")] class Song {
class Topic {
[Column(IsIdentity = true)] [Column(IsIdentity = true)]
public int Id { get; set; } public int Id { get; set; }
public int Clicks { get; set; } public DateTime? Create_time { get; set; }
public bool? Is_deleted { get; set; }
public string Title { get; set; } public string Title { get; set; }
public DateTime CreateTime { get; set; } public string Url { get; set; }
public int TypeId { get; set; } public virtual ICollection<Tag> Tags { get; set; }
public TopicType Type { get; set; } //导航属性
} }
class TopicType { 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; }
}
class Tag {
[Column(IsIdentity = true)] [Column(IsIdentity = true)]
public int Id { get; set; } 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 string Name { get; set; }
public int ClassId { get; set; } public virtual ICollection<Song> Songs { get; set; }
public TopicTypeClass Class { get; set; } //导航属性 public virtual ICollection<Tag> Tags { get; set; }
}
class TopicTypeClass {
public int Id { get; set; }
public string Name { get; set; }
} }
``` ```
# Part1 查询 # 查询
```csharp ```csharp
List<Topic> t1 = fsql.Select<Topic>().Where(a => a.Id > 0).ToList(); //OneToOne、ManyToOne
var t0 = fsql.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 = fsql.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 = fsql.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))
```
更多前往wiki[《Select查询数据文档》](https://github.com/2881099/FreeSql/wiki/%e6%9f%a5%e8%af%a2)
# 表达式函数
```csharp
var t1 = select.Where(a => new[] { 1, 2, 3 }.Contains(a.testFieldInt)).ToSql();
//SELECT a.`Id`, a.`Clicks`, a.`TestTypeInfoGuid`, a.`Title`, a.`CreateTime`
//FROM `Song` a
//WHERE (a.`Id` in (1,2,3))
```
查找今天创建的数据
```csharp
var t2 = select.Where(a => a.CreateTime.Date == DateTime.Now.Date).ToSql();
```
SqlServer 下随机获取记录
```csharp
var t3 = select.OrderBy(a => Guid.NewGuid()).Limit(1).ToSql();
//SELECT top 1 ...
//FROM [Song] a
//ORDER BY newid()
```
更多前往wiki[《Expression 表达式函数文档》](https://github.com/2881099/FreeSql/wiki/%e8%a1%a8%e8%be%be%e5%bc%8f%e5%87%bd%e6%95%b0)
# 返回数据
```csharp
List<Song> t1 = fsql.Select<Song>().Where(a => a.Id > 0).ToList();
//返回普通字段 + 导航对象 Type 的数据 //返回普通字段 + 导航对象 Type 的数据
List<Topic> t2 = fsql.Select<Topic>().LeftJoin(a => a.Type.Id == a.TypeId).ToList(); List<Song> t2 = fsql.Select<Song>().LeftJoin(a => a.Type.Id == a.TypeId).ToList();
//返回一个字段 //返回一个字段
List<int> t3 = fsql.Select<Topic>().Where(a => a.Id > 0).ToList(a => a.Id); List<int> t3 = fsql.Select<Song>().Where(a => a.Id > 0).ToList(a => a.Id);
//返回匿名类型 //返回匿名类型
List<匿名类型> t4 = fsql.Select<Topic>().Where(a => a.Id > 0).ToList(a => new { a.Id, a.Title }); List<匿名类型> t4 = fsql.Select<Song>().Where(a => a.Id > 0).ToList(a => new { a.Id, a.Title });
//返回元组 //返回元组
List<(int, string)> t5 = fsql.Select<Topic>().Where(a => a.Id > 0).ToList<(int, string)>("id, title"); List<(int, string)> t5 = fsql.Select<Song>().Where(a => a.Id > 0).ToList<(int, string)>("id, title");
//返回SQL字段 //返回SQL字段
List<匿名类> t4 = select.Where(a => a.Id > 0).Skip(100).Limit(200) List<匿名类> t4 = select.Where(a => a.Id > 0).Skip(100).Limit(200)
@ -117,75 +188,18 @@ List<匿名类> t4 = select.Where(a => a.Id > 0).Skip(100).Limit(200)
//奇思妙想:怎么查询开窗函数的结果 //奇思妙想:怎么查询开窗函数的结果
}); });
``` ```
### 联表之一:使用导航属性 执行SQL返回数据
```csharp ```csharp
sql = fsql.Select<Topic>() List<Song> t6 = fsql.Ado.Query<Song>("select * from song");
.LeftJoin(a => a.Type.Id == a.TypeId) List<(int, string ,string)> t7 = fsql.Ado.Query<(int, string, string)>("select id,title,url from song");
.ToSql();
sql = fsql.Select<Topic>()
.LeftJoin(a => a.Type.Id == a.TypeId)
.LeftJoin(a => a.Type.Class.Id == a.Type.ClassId)
.ToSql();
```
### 联表之二:无导航属性
```csharp
sql = fsql.Select<Topic>()
.LeftJoin<TopicType>((a, b) => b.Id == a.TypeId)
.ToSql();
sql = fsql.Select<Topic>()
.LeftJoin<TopicType>((a, b) => b.Id == a.TypeId)
.LeftJoin<TopicTypeClass>((a, c) => c.Id == a.Type.ClassId)
.ToSql();
```
### 联表之三b, c 条件怎么设?试试这种!
```csharp
sql = fsql.Select<Topic>()
.From<TopicType, TopicTypeClass>((s, b, c) => s
.LeftJoin(a => a.TypeId == b.Id)
.LeftJoin(a => b.ClassId == c.Id))
.ToSql();
```
### 联表之四原生SQL联表
```csharp
sql = fsql.Select<Topic>()
.LeftJoin("TopicType b on b.Id = a.TypeId and b.Name = ?bname", new { bname = "xxx" })
.ToSql();
```
### 分组聚合
```csharp
var groupby = fsql.Select<Topic>()
.GroupBy(a => 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())
.ToList(a => new { a.Key.tt2, cou1 = a.Count(), arg1 = a.Avg(a.Key.mod4) });
//SELECT substr(a.`Title`, 1, 2) as1, count(1) as2, avg((a.`Id` % 4)) as3
//FROM `xxx` a
//GROUP BY substr(a.`Title`, 1, 2), (a.`Id` % 4)
//HAVING (count(1) > 0 AND avg((a.`Id` % 4)) > 0 AND max((a.`Id` % 4)) > 0) AND (count(1) < 300 OR avg((a.`Id` % 4)) < 100)
//ORDER BY substr(a.`Title`, 1, 2), count(1) DESC
```
### 执行SQL返回数据
```csharp
class xxx {
public int Id { get; set; }
public string Path { get; set; }
public string Title2 { get; set; }
}
List<xxx> t6 = fsql.Ado.Query<xxx>("select * from song");
List<(int, string ,string)> t7 = fsql.Ado.Query<(int, string, string)>("select * from song");
List<dynamic> t8 = fsql.Ado.Query<dynamic>("select * from song"); List<dynamic> t8 = fsql.Ado.Query<dynamic>("select * from song");
``` ```
> 更多资料[《Select查询数据》](https://github.com/2881099/FreeSql/wiki/%e6%9f%a5%e8%af%a2) 更多前往wiki[《Select查询数据》](https://github.com/2881099/FreeSql/wiki/%e6%9f%a5%e8%af%a2)
## 性能测试 # 性能测试
### FreeSql Query & Dapper Query
FreeSql Query & Dapper Query
```shell
Elapsed: 00:00:00.6733199; Query Entity Counts: 131072; ORM: Dapper Elapsed: 00:00:00.6733199; Query Entity Counts: 131072; ORM: Dapper
Elapsed: 00:00:00.4554230; Query Tuple Counts: 131072; ORM: Dapper Elapsed: 00:00:00.4554230; Query Tuple Counts: 131072; ORM: Dapper
@ -199,148 +213,20 @@ Elapsed: 00:00:00.6060042; Query Tuple Counts: 131072; ORM: FreeSql*
Elapsed: 00:00:00.4211323; Query ToList<Tuple> Counts: 131072; ORM: FreeSql* Elapsed: 00:00:00.4211323; Query ToList<Tuple> Counts: 131072; ORM: FreeSql*
Elapsed: 00:00:01.0236285; Query Dynamic Counts: 131072; ORM: FreeSql* Elapsed: 00:00:01.0236285; Query Dynamic Counts: 131072; ORM: FreeSql*
```
### FreeSql ToList & Dapper Query FreeSql ToList & Dapper Query
```shell
Elapsed: 00:00:00.6707125; ToList Entity Counts: 131072; ORM: FreeSql* Elapsed: 00:00:00.6707125; ToList Entity Counts: 131072; ORM: FreeSql*
Elapsed: 00:00:00.6495301; Query Entity Counts: 131072; ORM: Dapper Elapsed: 00:00:00.6495301; Query Entity Counts: 131072; ORM: Dapper
```
[查看测试代码](FreeSql.Tests.PerformanceTests/MySqlAdoTest.cs) 测试方法:运行两次,以第二次性能报告,避免了首个运行慢不公平的情况。[查看测试代码](FreeSql.Tests.PerformanceTests/MySqlAdoTest.cs)
> 测试方法:运行两次,以第二次性能报告,避免了首个运行慢不公平的情况。
FreeSql 目前使用的ExpressionTree+缓存,因为支持更为复杂的数据类型,所以比 Dapper Emit 慢少许。 FreeSql 目前使用的ExpressionTree+缓存,因为支持更为复杂的数据类型,所以比 Dapper Emit 慢少许。
# Part2 添加 # 贡献者名单
```csharp
var items = new List<Topic>();
for (var a = 0; a < 10; a++)
items.Add(new Topic { Title = $"newtitle{a}", Clicks = a * 100 });
var t1 = fsql.Insert<Topic>().AppendData(items.First()).ToSql();
//批量插入
var t2 = fsql.Insert<Topic>().AppendData(items).ToSql();
//插入指定的列
var t3 = fsql.Insert<Topic>().AppendData(items)
.InsertColumns(a => a.Title).ToSql();
var t4 = fsql.Insert<Topic>().AppendData(items)
.InsertColumns(a => new { a.Title, a.Clicks }).ToSql();
//忽略列
var t5 = fsql.Insert<Topic>().AppendData(items)
.IgnoreColumns(a => a.CreateTime).ToSql();
var t6 = fsql.Insert<Topic>().AppendData(items)
.IgnoreColumns(a => new { a.Title, a.CreateTime }).ToSql();
```
### 执行命令
| 方法 | 返回值 | 描述 |
| - | - | - |
| ExecuteAffrows | long | 执行SQL语句返回影响的行数 |
| ExecuteIdentity | long | 执行SQL语句返回自增值 |
| ExecuteInserted | List\<Topic\> | 执行SQL语句返回插入后的记录 |
> 更多资料:[《Insert添加数据》](https://github.com/2881099/FreeSql/wiki/%e6%b7%bb%e5%8a%a0)
# Part3 修改
```csharp
var t1 = fsql.Update<Topic>(1).Set(a => a.CreateTime, DateTime.Now).ToSql();
//UPDATE `tb_topic` SET `CreateTime` = '2018-12-08 00:04:59' WHERE (`Id` = 1)
//更新指定列,累加
var t2 = fsql.Update<Topic>(1).Set(a => a.Clicks + 1).ToSql();
//UPDATE `tb_topic` SET `Clicks` = ifnull(`Clicks`,0) + 1 WHERE (`Id` = 1)
//保存实体
var item = new Topic { Id = 1, Title = "newtitle" };
var t3 = fsql.Update<Topic>().SetSource(item).ToSql();
//UPDATE `tb_topic` SET `Clicks` = ?p_0, `Title` = ?p_1, `CreateTime` = ?p_2 WHERE (`Id` = 1)
//忽略列
var t4 = fsql.Update<Topic>().SetSource(item)
.IgnoreColumns(a => a.Clicks).ToSql();
//UPDATE `tb_topic` SET `Title` = ?p_0, `CreateTime` = ?p_1 WHERE (`Id` = 1)
var t5 = fsql.Update<Topic>().SetSource(item)
.IgnoreColumns(a => new { a.Clicks, a.CreateTime }).ToSql();
//UPDATE `tb_topic` SET `Title` = ?p_0 WHERE (`Id` = 1)
//批量保存
```csharp
var items = new List<Topic>();
for (var a = 0; a < 10; a++)
items.Add(new Topic { Id = a + 1, Title = $"newtitle{a}", Clicks = a * 100 });
var t6 = fsql.Update<Topic>().SetSource(items).ToSql();
//UPDATE `tb_topic` SET `Clicks` = CASE `Id` WHEN 1 THEN ?p_0 WHEN 2 THEN ?p_1
//WHEN 3 THEN ?p_2 WHEN 4 THEN ?p_3 WHEN 5 THEN ?p_4 WHEN 6 THEN ?p_5 WHEN 7 THEN ?p_6
//WHEN 8 THEN ?p_7 WHEN 9 THEN ?p_8 WHEN 10 THEN ?p_9 END, `Title` = CASE `Id`
//WHEN 1 THEN ?p_10 WHEN 2 THEN ?p_11 WHEN 3 THEN ?p_12 WHEN 4 THEN ?p_13 WHEN 5 THEN ?p_14
//WHEN 6 THEN ?p_15 WHEN 7 THEN ?p_16 WHEN 8 THEN ?p_17 WHEN 9 THEN ?p_18 WHEN 10 THEN ?p_19 END,
//`CreateTime` = CASE `Id` WHEN 1 THEN ?p_20 WHEN 2 THEN ?p_21 WHEN 3 THEN ?p_22 WHEN 4 THEN ?p_23
//WHEN 5 THEN ?p_24 WHEN 6 THEN ?p_25 WHEN 7 THEN ?p_26 WHEN 8 THEN ?p_27 WHEN 9 THEN ?p_28
//WHEN 10 THEN ?p_29 END WHERE (`Id` IN (1,2,3,4,5,6,7,8,9,10))
//批量保存的时候,也可以忽略一些列
var t7 = fsql.Update<Topic>().SetSource(items)
.IgnoreColumns(a => new { a.Clicks, a.CreateTime }).ToSql();
//UPDATE `tb_topic` SET `Title` = CASE `Id` WHEN 1 THEN ?p_0 WHEN 2 THEN ?p_1 WHEN 3 THEN ?p_2
//WHEN 4 THEN ?p_3 WHEN 5 THEN ?p_4 WHEN 6 THEN ?p_5 WHEN 7 THEN ?p_6 WHEN 8 THEN ?p_7 WHEN 9
//THEN ?p_8 WHEN 10 THEN ?p_9 END WHERE (`Id` IN (1,2,3,4,5,6,7,8,9,10))
//批量更新指定列
var t8 = fsql.Update<Topic>().SetSource(items).Set(a => a.CreateTime, DateTime.Now).ToSql();
//UPDATE `tb_topic` SET `CreateTime` = ?p_0 WHERE (`Id` IN (1,2,3,4,5,6,7,8,9,10))
```
### 更新条件
```csharp
fsql.Update<Topic>(object dywhere)
```
dywhere 支持
* 主键值
* new[] { 主键值1, 主键值2 }
* Topic对象
* new[] { Topic对象1, Topic对象2 }
* new { id = 1 }
```csharp
var t9 = fsql.Update<Topic>().Set(a => a.Title, "新标题").Where(a => a.Id == 1).ToSql();
//UPDATE `tb_topic` SET `Title` = '新标题' WHERE (Id = 1)
```
### 自定义SQL
```csharp
var t10 = fsql.Update<Topic>().SetRaw("Title = {0}", "新标题").Where("Id = {0}", 1).ToSql();
//UPDATE `tb_topic` SET Title = '新标题' WHERE (Id = 1)
//sql语法条件参数使用 {0},与 string.Format 保持一致,无须加单引号,错误的用法:'{0}'
```
### 执行命令
| 方法 | 返回值 | 参数 | 描述 |
| - | - | - | - |
| ExecuteAffrows | long | | 执行SQL语句返回影响的行数 |
| ExecuteUpdated | List\<T1\> | | 执行SQL语句返回更新后的记录 |
> 更多资料:[《Update更新数据》](https://github.com/2881099/FreeSql/wiki/%e4%bf%ae%e6%94%b9)
# Part4 删除
详情查看:[《Delete 删除数据》](https://github.com/2881099/FreeSql/wiki/%e5%88%a0%e9%99%a4)
# Part5 表达式函数
详情查看:[《Expression 表达式函数》](https://github.com/2881099/FreeSql/wiki/%e8%a1%a8%e8%be%be%e5%bc%8f%e5%87%bd%e6%95%b0)
# Part6 事务
```csharp
fsql.Transaction(() => {
//code
});
```
没异常就提交,有异常会回滚。
代码体内只可以使用同步方法,因为事务对象挂靠在线程关联上,使用异步方法会切换线程。
## 贡献者名单
[systemhejiyong](https://github.com/systemhejiyong) [systemhejiyong](https://github.com/systemhejiyong)
[LambertW](https://github.com/LambertW) [LambertW](https://github.com/LambertW)