From 0ff422eeb6fc3521bca72659ea1332a871edd015 Mon Sep 17 00:00:00 2001 From: 28810 <28810@YEXIANGQIN> Date: Sat, 22 Dec 2018 18:13:25 +0800 Subject: [PATCH] =?UTF-8?q?mysql/sqlserver=20CodeFirst=20=E5=AE=8C?= =?UTF-8?q?=E6=88=90=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Docs/expression.md | 26 +- FreeSql.Tests/MySql/Curd/MySqlSelectTest.cs | 77 +++ FreeSql.Tests/MySql/MySqlCodeFirstTest.cs | 18 + .../MySql/MySqlExpression/ConvertTest.cs | 114 +++++ .../SqlServer/SqlServerCodeFirstTest.cs | 24 + .../SqlServerExpression/ConvertTest.cs | 111 +++++ .../SqlServerExpression/DateTimeTest.cs | 351 -------------- .../SqlServerExpression/StringTest.cs | 440 +----------------- .../SqlServerExpression/TimeSpanTest.cs | 84 ---- FreeSql.Tests/UnitTest1.cs | 2 +- FreeSql/DataAnnotations/ColumnAttribute.cs | 5 + FreeSql/Internal/CommonExpression.cs | 33 +- .../CommonProvider/AdoProvider/AdoProvider.cs | 2 +- .../AdoProvider/AdoProviderAsync.cs | 2 +- .../Internal/CommonProvider/InsertProvider.cs | 1 - .../SelectProvider/Select0Provider.cs | 13 +- .../SelectProvider/Select1Provider.cs | 3 +- .../Internal/CommonProvider/UpdateProvider.cs | 7 +- .../Internal/Model/ReadAnonymousTypeInfo.cs | 1 + FreeSql/Internal/Model/TableInfo.cs | 1 - FreeSql/Internal/Utils.cs | 39 +- FreeSql/MySql/MySqlCodeFirst.cs | 12 +- FreeSql/MySql/MySqlExpression.cs | 125 ++--- FreeSql/PostgreSQL/PostgreSQLExpression.cs | 23 + FreeSql/SqlServer/SqlServerCodeFirst.cs | 118 +++-- FreeSql/SqlServer/SqlServerExpression.cs | 147 +++--- 26 files changed, 712 insertions(+), 1067 deletions(-) create mode 100644 FreeSql.Tests/MySql/MySqlExpression/ConvertTest.cs create mode 100644 FreeSql.Tests/SqlServer/SqlServerExpression/ConvertTest.cs diff --git a/Docs/expression.md b/Docs/expression.md index 5e68fe13..ec52ba3b 100644 --- a/Docs/expression.md +++ b/Docs/expression.md @@ -1,7 +1,8 @@ # 表达式函数 | 表达式 | MySql | SqlServer | PostgreSQL | 功能说明 | | - | - | - | - | - | -| (a ?? b) | ifnull(a, b) | isnull(a, b) | coalesce(a, b) | 当a为null时,取b值 | +| a ? b : c | case when a then b else c end | case when a then b else c end | - | a成立时取b值,否则取c值 | +| a ?? b | ifnull(a, b) | isnull(a, b) | coalesce(a, b) | 当a为null时,取b值 | | 数字 + 数字 | a + b | a + b | a + b | 数字相加 | | 数字 + 字符串 | concat(a, b) | cast(a as varchar) + cast(b as varchar) | case(a as varchar) \|\| b | 字符串相加,a或b任意一个为字符串时 | | a - b | a - b | a - b | a - b | 减 @@ -63,8 +64,8 @@ | a.Month | month(a) | datepart(month, a) | - | 月 | | a.Second | second(a) | datepart(second, a) | - | 秒 | | a.Subtract(b) | timestampdiff(microsecond, b, a) | datediff(millisecond, b, a) * 1000 | - | 将a的值和b相减 | -| a.Ticks | timestampdiff(microsecond, '1970-1-1', a) * 10 + 621355968000000000 | datediff(millisecond, '1970-1-1', a) * 10000 + 621355968000000000 | - | 刻度总数 | -| a.TimeOfDay | timestampdiff(microsecond, date_format(a, '1970-1-1 %H:%i:%s.%f'), a) + 62135596800000000 | '1970-1-1 ' + convert(varchar, a, 14) | - | 获取a的时间部分 | +| a.Ticks | timestampdiff(microsecond, '0001-1-1', a) * 10 | datediff(millisecond, '1970-1-1', a) * 10000 + 621355968000000000 | - | 刻度总数 | +| a.TimeOfDay | timestampdiff(microsecond, date_format(a, '%Y-%m-%d'), a) | '1970-1-1 ' + convert(varchar, a, 14) | - | 获取a的时间部分 | | a.Year | year(a) | datepart(year, a) | - | 年 | | a.Equals(b) | a = b | a = b | - | 比较a和b相等 | | a.CompareTo(b) | a - b | a - b | - | 比较a和b大小 | @@ -122,3 +123,22 @@ | Math.Sqrt(a) | sqrt(a) | sqrt(a) | - | - | | Math.Tan(a) | tan(a) | tan(a) | - | - | | Math.Truncate(a) | truncate(a, 0) | floor(a) | - | - | + +### 类型转换 +| 表达式 | MySql | SqlServer | PostgreSQL | 功能说明 | +| - | - | - | - | - | +| Convert.ToBoolean(a) | (a not in ('0','false)) | - | - | - | +| Convert.ToByte | cast(a as unsigned) | - | - | - | +| Convert.ToChar | substr(cast(a as char),1,1) | - | - | - | +| Convert.ToDateTime | cast(a as datetime) | - | - | - | +| Convert.ToDecimal | cast(a as decimal(36,18)) | - | - | - | +| Convert.ToDouble | cast(a as decimal(32,16)) | - | - | - | +| Convert.ToInt16 | cast(a as signed) | - | - | - | +| Convert.ToInt32 | cast(a as signed) | - | - | - | +| Convert.ToInt64 | cast(a as signed) | - | - | - | +| Convert.ToSByte | cast(a as signed) | - | - | - | +| Convert.ToSingle | cast(a as char) | - | - | - | +| Convert.ToString | cast(a as decimal(14,7)) | - | - | - | +| Convert.ToUInt16 | cast(a as unsigned) | - | - | - | +| Convert.ToUInt32 | cast(a as unsigned) | - | - | - | +| Convert.ToUInt64 | cast(a as unsigned) | - | - | - | diff --git a/FreeSql.Tests/MySql/Curd/MySqlSelectTest.cs b/FreeSql.Tests/MySql/Curd/MySqlSelectTest.cs index bbc0e659..6d996913 100644 --- a/FreeSql.Tests/MySql/Curd/MySqlSelectTest.cs +++ b/FreeSql.Tests/MySql/Curd/MySqlSelectTest.cs @@ -34,6 +34,83 @@ namespace FreeSql.Tests.MySql { [Fact] public void ToList() { + var t1 = g.mysql.Select().Where("").Where(a => a.Id > 0).Skip(100).Limit(200).ToSql(); + var t2 = g.mysql.Select().As("b").Where("").Where(a => a.Id > 0).Skip(100).Limit(200).ToSql(); + + + var sql1 = select.LeftJoin(a => a.Type.Guid == a.TestTypeInfoGuid).ToSql(); + var sql2 = select.LeftJoin((a, b) => a.TestTypeInfoGuid == b.Guid && b.Name == "111").ToSql(); + var sql3 = select.LeftJoin("TestTypeInfo b on b.Guid = a.TypeGuid").ToSql(); + + //g.mysql.Select().Join((a, b, c) => new Model.JoinResult3( + // Model.JoinType.LeftJoin, a.TypeGuid == b.Guid, + // Model.JoinType.InnerJoin, c.Id == b.ParentId && c.Name == "xxx") + //); + + //var sql4 = select.From((a, b, c) => new SelectFrom() + // .InnerJoin(a.TypeGuid == b.Guid) + // .LeftJoin(c.Id == b.ParentId) + // .Where(b.Name == "xxx")) + //.Where(a => a.Id == 1).ToSql(); + + var sql4 = select.From((s, b, c) => s + .InnerJoin(a => a.TestTypeInfoGuid == b.Guid) + .LeftJoin(a => c.Id == b.ParentId) + .Where(a => b.Name == "xxx")).ToSql(); + //.Where(a => a.Id == 1).ToSql(); + + + var list111 = select.From((s, b, c) => s + .InnerJoin(a => a.TestTypeInfoGuid == b.Guid) + .LeftJoin(a => c.Id == b.ParentId) + .Where(a => b.Name != "xxx")); + var list111sql = list111.ToSql(); + var list111data = list111.ToList((a, b, c) => new { + a.Id, + title_substring = a.Title.Substring(0, 1), + a.Type, + ccc = new { a.Id, a.Title }, + tp = a.Type, + tp2 = new { + a.Id, + tp2 = a.Type.Name + }, + tp3 = new { + a.Id, + tp33 = new { + a.Id + } + } + }); + + var ttt122 = g.mysql.Select().Where(a => a.Id > 0).ToSql(); + var sql5 = g.mysql.Select().From((s, b, c) => s).Where((a, b, c) => a.Id == b.ParentId).ToSql(); + var t11112 = g.mysql.Select().ToList(a => new { + a.Id, + a.Title, + a.Type, + ccc = new { a.Id, a.Title }, + tp = a.Type, + tp2 = new { + a.Id, + tp2 = a.Type.Name + }, + tp3 = new { + a.Id, + tp33 = new { + a.Id + } + } + + }); + + var t100 = g.mysql.Select().Where("").Where(a => a.Id > 0).Skip(100).Limit(200).Caching(50).ToList(); + var t101 = g.mysql.Select().As("b").Where("").Where(a => a.Id > 0).Skip(100).Limit(200).Caching(50).ToList(); + + + var t1111 = g.mysql.Select().ToList(a => new { a.Id, a.Title, a.Type }); + + var t2222 = g.mysql.Select().ToList(a => new { a.Id, a.Title, a.Type.Name }); } [Fact] public void ToOne() { diff --git a/FreeSql.Tests/MySql/MySqlCodeFirstTest.cs b/FreeSql.Tests/MySql/MySqlCodeFirstTest.cs index 4245a27d..6f12d47e 100644 --- a/FreeSql.Tests/MySql/MySqlCodeFirstTest.cs +++ b/FreeSql.Tests/MySql/MySqlCodeFirstTest.cs @@ -7,6 +7,24 @@ using Xunit; namespace FreeSql.Tests.MySql { public class MySqlCodeFirstTest { + + [Fact] + public void AddField() { + var sql = g.mysql.CodeFirst.GetComparisonDDLStatements(); + + var id = g.mysql.Insert().AppendData(new TopicAddField { }).ExecuteIdentity(); + } + + public class TopicAddField { + [Column(IsIdentity = true)] + public int? Id { get; set; } + + public int name { get; set; } + + [Column(DbType = "varchar(200) not null", OldName = "title")] + public string title222 { get; set; } = "10"; + } + [Fact] public void GetComparisonDDLStatements() { diff --git a/FreeSql.Tests/MySql/MySqlExpression/ConvertTest.cs b/FreeSql.Tests/MySql/MySqlExpression/ConvertTest.cs new file mode 100644 index 00000000..ee7ddd82 --- /dev/null +++ b/FreeSql.Tests/MySql/MySqlExpression/ConvertTest.cs @@ -0,0 +1,114 @@ +using FreeSql.DataAnnotations; +using System; +using System.Collections.Generic; +using System.Linq; +using Xunit; + +namespace FreeSql.Tests.MySqlExpression { + public class ConvertTest { + + ISelect select => g.mysql.Select(); + + [Table(Name = "tb_topic")] + class Topic { + [Column(IsIdentity = true, IsPrimary = true)] + public int Id { get; set; } + public int Clicks { get; set; } + public int TestTypeInfoGuid { get; set; } + public TestTypeInfo Type { get; set; } + public string Title { get; set; } + public DateTime CreateTime { get; set; } + } + class TestTypeInfo { + public int Guid { get; set; } + public int ParentId { get; set; } + public TestTypeParentInfo Parent { get; set; } + public string Name { get; set; } + } + class TestTypeParentInfo { + public int Id { get; set; } + public string Name { get; set; } + + public List Types { get; set; } + } + + [Fact] + public void ToBoolean() { + var data = new List(); + data.Add(select.Where(a => (Convert.ToBoolean(a.Clicks) ? 1 : 2) > 0).ToList()); + //SELECT a.`Id`, a.`Clicks`, a.`TestTypeInfoGuid`, a.`Title`, a.`CreateTime` + //FROM `tb_topic` a + //WHERE ((a.`Clicks` not in ('0','false'))) + } + [Fact] + public void ToByte() { + var data = new List(); + data.Add(select.Where(a => Convert.ToByte(a.Clicks) > 0).ToList()); + } + [Fact] + public void ToChar() { + var data = new List(); + data.Add(select.Where(a => Convert.ToBoolean(a.Clicks)).ToList()); + } + [Fact] + public void ToDateTime() { + var data = new List(); + data.Add(select.Where(a => Convert.ToDateTime(a.CreateTime.ToString()).Year > 0).ToList()); + } + [Fact] + public void ToDecimal() { + var data = new List(); + data.Add(select.Where(a => Convert.ToDecimal(a.Clicks) > 0).ToList()); + } + [Fact] + public void ToDouble() { + var data = new List(); + data.Add(select.Where(a => Convert.ToDouble(a.Clicks) > 0).ToList()); + } + [Fact] + public void ToInt16() { + var data = new List(); + data.Add(select.Where(a => Convert.ToInt16(a.Clicks) > 0).ToList()); + } + [Fact] + public void ToInt32() { + var data = new List(); + data.Add(select.Where(a => Convert.ToInt32(a.Clicks) > 0).ToList()); + } + [Fact] + public void ToInt64() { + var data = new List(); + data.Add(select.Where(a => Convert.ToInt64(a.Clicks) > 0).ToList()); + } + [Fact] + public void ToSByte() { + var data = new List(); + data.Add(select.Where(a => Convert.ToSByte(a.Clicks) > 0).ToList()); + } + [Fact] + public void ToSingle() { + var data = new List(); + data.Add(select.Where(a => Convert.ToSingle(a.Clicks) > 0).ToList()); + } + [Fact] + public void this_ToString() { + var data = new List(); + data.Add(select.Where(a => Convert.ToString(a.Clicks).Equals("")).ToList()); + } + [Fact] + public void ToUInt16() { + var data = new List(); + data.Add(select.Where(a => Convert.ToUInt16(a.Clicks) > 0).ToList()); + } + [Fact] + public void ToUInt32() { + var data = new List(); + data.Add(select.Where(a => Convert.ToUInt32(a.Clicks) > 0).ToList()); + } + [Fact] + public void ToUInt64() { + var data = new List(); + data.Add(select.Where(a => Convert.ToUInt64(a.Clicks) > 0).ToList()); + } + } +} diff --git a/FreeSql.Tests/SqlServer/SqlServerCodeFirstTest.cs b/FreeSql.Tests/SqlServer/SqlServerCodeFirstTest.cs index ec50497a..ea366b52 100644 --- a/FreeSql.Tests/SqlServer/SqlServerCodeFirstTest.cs +++ b/FreeSql.Tests/SqlServer/SqlServerCodeFirstTest.cs @@ -7,6 +7,30 @@ using Xunit; namespace FreeSql.Tests.SqlServer { public class SqlServerCodeFirstTest { + + [Fact] + public void AddField() { + var sql = g.sqlserver.CodeFirst.GetComparisonDDLStatements(); + + var id = g.sqlserver.Insert().AppendData(new TopicAddField { }).ExecuteIdentity(); + } + + public class TopicAddField { + [Column(IsIdentity = true)] + public int Id { get; set; } + + public int name { get; set; } = 3000; + + [Column(DbType = "varchar(200) not null", OldName = "title")] + public string title222 { get; set; } = "333"; + + [Column(DbType = "varchar(200) not null")] + public string title222333 { get; set; } = "xxx"; + + [Column(DbType = "varchar(100) not null", OldName = "title122333aaa")] + public string titleaaa { get; set; } = "fsdf"; + } + [Fact] public void GetComparisonDDLStatements() { diff --git a/FreeSql.Tests/SqlServer/SqlServerExpression/ConvertTest.cs b/FreeSql.Tests/SqlServer/SqlServerExpression/ConvertTest.cs new file mode 100644 index 00000000..e6da504f --- /dev/null +++ b/FreeSql.Tests/SqlServer/SqlServerExpression/ConvertTest.cs @@ -0,0 +1,111 @@ +using FreeSql.DataAnnotations; +using System; +using System.Collections.Generic; +using System.Linq; +using Xunit; + +namespace FreeSql.Tests.SqlServerExpression { + public class ConvertTest { + + ISelect select => g.mysql.Select(); + + [Table(Name = "tb_topic")] + class Topic { + [Column(IsIdentity = true, IsPrimary = true)] + public int Id { get; set; } + public int Clicks { get; set; } + public int TestTypeInfoGuid { get; set; } + public TestTypeInfo Type { get; set; } + public string Title { get; set; } + public DateTime CreateTime { get; set; } + } + class TestTypeInfo { + public int Guid { get; set; } + public int ParentId { get; set; } + public TestTypeParentInfo Parent { get; set; } + public string Name { get; set; } + } + class TestTypeParentInfo { + public int Id { get; set; } + public string Name { get; set; } + + public List Types { get; set; } + } + + [Fact] + public void ToBoolean() { + var data = new List(); + data.Add(select.Where(a => (Convert.ToBoolean(a.Clicks) ? 1 : 2) > 0).ToList()); + } + [Fact] + public void ToByte() { + var data = new List(); + data.Add(select.Where(a => Convert.ToByte(a.Clicks) > 0).ToList()); + } + [Fact] + public void ToChar() { + var data = new List(); + data.Add(select.Where(a => Convert.ToBoolean(a.Clicks)).ToList()); + } + [Fact] + public void ToDateTime() { + var data = new List(); + data.Add(select.Where(a => Convert.ToDateTime(a.CreateTime.ToString()).Year > 0).ToList()); + } + [Fact] + public void ToDecimal() { + var data = new List(); + data.Add(select.Where(a => Convert.ToDecimal(a.Clicks) > 0).ToList()); + } + [Fact] + public void ToDouble() { + var data = new List(); + data.Add(select.Where(a => Convert.ToDouble(a.Clicks) > 0).ToList()); + } + [Fact] + public void ToInt16() { + var data = new List(); + data.Add(select.Where(a => Convert.ToInt16(a.Clicks) > 0).ToList()); + } + [Fact] + public void ToInt32() { + var data = new List(); + data.Add(select.Where(a => Convert.ToInt32(a.Clicks) > 0).ToList()); + } + [Fact] + public void ToInt64() { + var data = new List(); + data.Add(select.Where(a => Convert.ToInt64(a.Clicks) > 0).ToList()); + } + [Fact] + public void ToSByte() { + var data = new List(); + data.Add(select.Where(a => Convert.ToSByte(a.Clicks) > 0).ToList()); + } + [Fact] + public void ToSingle() { + var data = new List(); + data.Add(select.Where(a => Convert.ToSingle(a.Clicks) > 0).ToList()); + } + [Fact] + public void this_ToString() { + var data = new List(); + data.Add(select.Where(a => Convert.ToString(a.Clicks).Equals("")).ToList()); + } + [Fact] + public void ToUInt16() { + var data = new List(); + data.Add(select.Where(a => Convert.ToUInt16(a.Clicks) > 0).ToList()); + } + [Fact] + public void ToUInt32() { + var data = new List(); + data.Add(select.Where(a => Convert.ToUInt32(a.Clicks) > 0).ToList()); + } + [Fact] + public void ToUInt64() { + var data = new List(); + data.Add(select.Where(a => Convert.ToUInt64(a.Clicks) > 0).ToList()); + } + } +} diff --git a/FreeSql.Tests/SqlServer/SqlServerExpression/DateTimeTest.cs b/FreeSql.Tests/SqlServer/SqlServerExpression/DateTimeTest.cs index caf7c28f..a4b1fa95 100644 --- a/FreeSql.Tests/SqlServer/SqlServerExpression/DateTimeTest.cs +++ b/FreeSql.Tests/SqlServer/SqlServerExpression/DateTimeTest.cs @@ -39,33 +39,21 @@ namespace FreeSql.Tests.SqlServerExpression { public void Now() { var data = new List(); data.Add(select.Where(a => a.CreateTime.Date == DateTime.Now.Date).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic111333` a - //WHERE (cast(date_format(a.`CreateTime`, '%Y-%m-%d') as datetime) = cast(date_format(now(), '%Y-%m-%d') as datetime)) } [Fact] public void UtcNow() { var data = new List(); data.Add(select.Where(a => a.CreateTime.Date == DateTime.UtcNow.Date).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic111333` a - //WHERE (cast(date_format(a.`CreateTime`, '%Y-%m-%d') as datetime) = cast(date_format(utc_timestamp(), '%Y-%m-%d') as datetime)) } [Fact] public void MinValue() { var data = new List(); data.Add(select.Where(a => a.CreateTime.Date == DateTime.MinValue.Date).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic111333` a - //WHERE (cast(date_format(a.`CreateTime`, '%Y-%m-%d') as datetime) = cast(date_format(cast('0001/1/1 0:00:00' as datetime), '%Y-%m-%d') as datetime)) } [Fact] public void MaxValue() { var data = new List(); data.Add(select.Where(a => a.CreateTime.Date == DateTime.MaxValue.Date).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic111333` a - //WHERE (cast(date_format(a.`CreateTime`, '%Y-%m-%d') as datetime) = cast(date_format(cast('9999/12/31 23:59:59' as datetime), '%Y-%m-%d') as datetime)) } [Fact] public void Date() { @@ -73,31 +61,10 @@ namespace FreeSql.Tests.SqlServerExpression { data.Add(select.Where(a => a.CreateTime.Date == DateTime.Now.Date).ToList()); data.Add(select.Where(a => a.Type.Time.Date > DateTime.Now.Date).ToList()); data.Add(select.Where(a => a.Type.Parent.Time2.Date > DateTime.Now.Date).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic111333` a - //WHERE (cast(date_format(a.`CreateTime`, '%Y-%m-%d') as datetime) = cast(date_format(now(), '%Y-%m-%d') as datetime)); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a__Type.`Time` as7, a.`Title` as8, a.`CreateTime` as9 - //FROM `tb_topic111333` a, `TestTypeInfo333` a__Type - //WHERE (cast(date_format(a__Type.`Time`, '%Y-%m-%d') as datetime) > cast(date_format(now(), '%Y-%m-%d') as datetime)); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a__Type.`Time` as7, a.`Title` as8, a.`CreateTime` as9 - //FROM `tb_topic111333` a, `TestTypeInfo333` a__Type, `TestTypeParentInfo23123` a__Type__Parent - //WHERE (cast(date_format(a__Type__Parent.`Time2`, '%Y-%m-%d') as datetime) > cast(date_format(now(), '%Y-%m-%d') as datetime)); data.Add(select.Where(a => DateTime.Now.Subtract(a.CreateTime.Date).TotalSeconds > 0).ToList()); data.Add(select.Where(a => DateTime.Now.Subtract(a.Type.Time.Date).TotalSeconds > 0).ToList()); data.Add(select.Where(a => DateTime.Now.Subtract(a.Type.Parent.Time2.Date).TotalSeconds > 0).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic111333` a - //WHERE (((timestampdiff(microsecond, cast(date_format(a.`CreateTime`, '%Y-%m-%d') as datetime), now())) / 1000000) > 0); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a__Type.`Time` as7, a.`Title` as8, a.`CreateTime` as9 - //FROM `tb_topic111333` a, `TestTypeInfo333` a__Type - //WHERE (((timestampdiff(microsecond, cast(date_format(a__Type.`Time`, '%Y-%m-%d') as datetime), now())) / 1000000) > 0); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a__Type.`Time` as7, a.`Title` as8, a.`CreateTime` as9 - //FROM `tb_topic111333` a, `TestTypeInfo333` a__Type, `TestTypeParentInfo23123` a__Type__Parent - //WHERE (((timestampdiff(microsecond, cast(date_format(a__Type__Parent.`Time2`, '%Y-%m-%d') as datetime), now())) / 1000000) > 0) } [Fact] public void TimeOfDay() { @@ -105,17 +72,6 @@ namespace FreeSql.Tests.SqlServerExpression { data.Add(select.Where(a => a.CreateTime.TimeOfDay == DateTime.Now.TimeOfDay).ToList()); data.Add(select.Where(a => a.Type.Time.TimeOfDay > DateTime.Now.TimeOfDay).ToList()); data.Add(select.Where(a => a.Type.Parent.Time2.TimeOfDay > DateTime.Now.TimeOfDay).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic111333` a - //WHERE ((timestampdiff(microsecond, date_format(a.`CreateTime`, '1970-1-1 %H:%i:%s.%f'), a.`CreateTime`) + 62135596800000000) = (timestampdiff(microsecond, date_format(now(), '1970-1-1 %H:%i:%s.%f'), now()) + 62135596800000000)); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a__Type.`Time` as7, a.`Title` as8, a.`CreateTime` as9 - //FROM `tb_topic111333` a, `TestTypeInfo333` a__Type - //WHERE ((timestampdiff(microsecond, date_format(a__Type.`Time`, '1970-1-1 %H:%i:%s.%f'), a__Type.`Time`) + 62135596800000000) > (timestampdiff(microsecond, date_format(now(), '1970-1-1 %H:%i:%s.%f'), now()) + 62135596800000000)); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a__Type.`Time` as7, a.`Title` as8, a.`CreateTime` as9 - //FROM `tb_topic111333` a, `TestTypeInfo333` a__Type, `TestTypeParentInfo23123` a__Type__Parent - //WHERE ((timestampdiff(microsecond, date_format(a__Type__Parent.`Time2`, '1970-1-1 %H:%i:%s.%f'), a__Type__Parent.`Time2`) + 62135596800000000) > (timestampdiff(microsecond, date_format(now(), '1970-1-1 %H:%i:%s.%f'), now()) + 62135596800000000)) } [Fact] public void DayOfWeek() { @@ -123,17 +79,6 @@ namespace FreeSql.Tests.SqlServerExpression { data.Add(select.Where(a => a.CreateTime.DayOfWeek > DateTime.Now.DayOfWeek).ToList()); data.Add(select.Where(a => a.Type.Time.DayOfWeek > DateTime.Now.DayOfWeek).ToList()); data.Add(select.Where(a => a.Type.Parent.Time2.DayOfWeek > DateTime.Now.DayOfWeek).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic111333` a - //WHERE ((dayofweek(a.`CreateTime`) - 1) > (dayofweek(now()) - 1)); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a__Type.`Time` as7, a.`Title` as8, a.`CreateTime` as9 - //FROM `tb_topic111333` a, `TestTypeInfo333` a__Type - //WHERE ((dayofweek(a__Type.`Time`) - 1) > (dayofweek(now()) - 1)); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a__Type.`Time` as7, a.`Title` as8, a.`CreateTime` as9 - //FROM `tb_topic111333` a, `TestTypeInfo333` a__Type, `TestTypeParentInfo23123` a__Type__Parent - //WHERE ((dayofweek(a__Type__Parent.`Time2`) - 1) > (dayofweek(now()) - 1)) } [Fact] public void Day() { @@ -141,17 +86,6 @@ namespace FreeSql.Tests.SqlServerExpression { data.Add(select.Where(a => a.CreateTime.Day > DateTime.Now.Day).ToList()); data.Add(select.Where(a => a.Type.Time.Day > DateTime.Now.Day).ToList()); data.Add(select.Where(a => a.Type.Parent.Time2.Day > DateTime.Now.Day).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic111333` a - //WHERE (dayofmonth(a.`CreateTime`) > dayofmonth(now())); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a__Type.`Time` as7, a.`Title` as8, a.`CreateTime` as9 - //FROM `tb_topic111333` a, `TestTypeInfo333` a__Type - //WHERE (dayofmonth(a__Type.`Time`) > dayofmonth(now())); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a__Type.`Time` as7, a.`Title` as8, a.`CreateTime` as9 - //FROM `tb_topic111333` a, `TestTypeInfo333` a__Type, `TestTypeParentInfo23123` a__Type__Parent - //WHERE (dayofmonth(a__Type__Parent.`Time2`) > dayofmonth(now())) } [Fact] public void DayOfYear() { @@ -159,17 +93,6 @@ namespace FreeSql.Tests.SqlServerExpression { data.Add(select.Where(a => a.CreateTime.DayOfYear > DateTime.Now.DayOfYear).ToList()); data.Add(select.Where(a => a.Type.Time.DayOfYear > DateTime.Now.DayOfYear).ToList()); data.Add(select.Where(a => a.Type.Parent.Time2.DayOfYear > DateTime.Now.DayOfYear).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic111333` a - //WHERE (dayofyear(a.`CreateTime`) > dayofyear(now())); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a__Type.`Time` as7, a.`Title` as8, a.`CreateTime` as9 - //FROM `tb_topic111333` a, `TestTypeInfo333` a__Type - //WHERE (dayofyear(a__Type.`Time`) > dayofyear(now())); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a__Type.`Time` as7, a.`Title` as8, a.`CreateTime` as9 - //FROM `tb_topic111333` a, `TestTypeInfo333` a__Type, `TestTypeParentInfo23123` a__Type__Parent - //WHERE (dayofyear(a__Type__Parent.`Time2`) > dayofyear(now())) } [Fact] public void Month() { @@ -177,17 +100,6 @@ namespace FreeSql.Tests.SqlServerExpression { data.Add(select.Where(a => a.CreateTime.Month > DateTime.Now.Month).ToList()); data.Add(select.Where(a => a.Type.Time.Month > DateTime.Now.Month).ToList()); data.Add(select.Where(a => a.Type.Parent.Time2.Month > DateTime.Now.Month).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic111333` a - //WHERE (month(a.`CreateTime`) > month(now())); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a__Type.`Time` as7, a.`Title` as8, a.`CreateTime` as9 - //FROM `tb_topic111333` a, `TestTypeInfo333` a__Type - //WHERE (month(a__Type.`Time`) > month(now())); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a__Type.`Time` as7, a.`Title` as8, a.`CreateTime` as9 - //FROM `tb_topic111333` a, `TestTypeInfo333` a__Type, `TestTypeParentInfo23123` a__Type__Parent - //WHERE (month(a__Type__Parent.`Time2`) > month(now())) } [Fact] public void Year() { @@ -195,17 +107,6 @@ namespace FreeSql.Tests.SqlServerExpression { data.Add(select.Where(a => a.CreateTime.Year > DateTime.Now.Year).ToList()); data.Add(select.Where(a => a.Type.Time.Year > DateTime.Now.Year).ToList()); data.Add(select.Where(a => a.Type.Parent.Time2.Year > DateTime.Now.Year).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic111333` a - //WHERE (year(a.`CreateTime`) > year(now())); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a__Type.`Time` as7, a.`Title` as8, a.`CreateTime` as9 - //FROM `tb_topic111333` a, `TestTypeInfo333` a__Type - //WHERE (year(a__Type.`Time`) > year(now())); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a__Type.`Time` as7, a.`Title` as8, a.`CreateTime` as9 - //FROM `tb_topic111333` a, `TestTypeInfo333` a__Type, `TestTypeParentInfo23123` a__Type__Parent - //WHERE (year(a__Type__Parent.`Time2`) > year(now())) } [Fact] public void Hour() { @@ -213,17 +114,6 @@ namespace FreeSql.Tests.SqlServerExpression { data.Add(select.Where(a => a.CreateTime.Hour > DateTime.Now.Hour).ToList()); data.Add(select.Where(a => a.Type.Time.Hour > DateTime.Now.Hour).ToList()); data.Add(select.Where(a => a.Type.Parent.Time2.Hour > DateTime.Now.Hour).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic111333` a - //WHERE (hour(a.`CreateTime`) > hour(now())); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a__Type.`Time` as7, a.`Title` as8, a.`CreateTime` as9 - //FROM `tb_topic111333` a, `TestTypeInfo333` a__Type - //WHERE (hour(a__Type.`Time`) > hour(now())); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a__Type.`Time` as7, a.`Title` as8, a.`CreateTime` as9 - //FROM `tb_topic111333` a, `TestTypeInfo333` a__Type, `TestTypeParentInfo23123` a__Type__Parent - //WHERE (hour(a__Type__Parent.`Time2`) > hour(now())) } [Fact] public void Minute() { @@ -231,17 +121,6 @@ namespace FreeSql.Tests.SqlServerExpression { data.Add(select.Where(a => a.CreateTime.Minute > DateTime.Now.Minute).ToList()); data.Add(select.Where(a => a.Type.Time.Minute > DateTime.Now.Minute).ToList()); data.Add(select.Where(a => a.Type.Parent.Time2.Minute > DateTime.Now.Minute).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic111333` a - //WHERE (minute(a.`CreateTime`) > minute(now())); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a__Type.`Time` as7, a.`Title` as8, a.`CreateTime` as9 - //FROM `tb_topic111333` a, `TestTypeInfo333` a__Type - //WHERE (minute(a__Type.`Time`) > minute(now())); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a__Type.`Time` as7, a.`Title` as8, a.`CreateTime` as9 - //FROM `tb_topic111333` a, `TestTypeInfo333` a__Type, `TestTypeParentInfo23123` a__Type__Parent - //WHERE (minute(a__Type__Parent.`Time2`) > minute(now())) } [Fact] public void Second() { @@ -249,17 +128,6 @@ namespace FreeSql.Tests.SqlServerExpression { data.Add(select.Where(a => a.CreateTime.Second > DateTime.Now.Second).ToList()); data.Add(select.Where(a => a.Type.Time.Second > DateTime.Now.Second).ToList()); data.Add(select.Where(a => a.Type.Parent.Time2.Second > DateTime.Now.Second).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic111333` a - //WHERE (second(a.`CreateTime`) > second(now())); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a__Type.`Time` as7, a.`Title` as8, a.`CreateTime` as9 - //FROM `tb_topic111333` a, `TestTypeInfo333` a__Type - //WHERE (second(a__Type.`Time`) > second(now())); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a__Type.`Time` as7, a.`Title` as8, a.`CreateTime` as9 - //FROM `tb_topic111333` a, `TestTypeInfo333` a__Type, `TestTypeParentInfo23123` a__Type__Parent - //WHERE (second(a__Type__Parent.`Time2`) > second(now())) } [Fact] public void Millisecond() { @@ -267,17 +135,6 @@ namespace FreeSql.Tests.SqlServerExpression { data.Add(select.Where(a => a.CreateTime.Millisecond > DateTime.Now.Millisecond).ToList()); data.Add(select.Where(a => a.Type.Time.Millisecond > DateTime.Now.Millisecond).ToList()); data.Add(select.Where(a => a.Type.Parent.Time2.Millisecond > DateTime.Now.Millisecond).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic111333` a - //WHERE (floor(microsecond(a.`CreateTime`) / 1000) > floor(microsecond(now()) / 1000)); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a__Type.`Time` as7, a.`Title` as8, a.`CreateTime` as9 - //FROM `tb_topic111333` a, `TestTypeInfo333` a__Type - //WHERE (floor(microsecond(a__Type.`Time`) / 1000) > floor(microsecond(now()) / 1000)); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a__Type.`Time` as7, a.`Title` as8, a.`CreateTime` as9 - //FROM `tb_topic111333` a, `TestTypeInfo333` a__Type, `TestTypeParentInfo23123` a__Type__Parent - //WHERE (floor(microsecond(a__Type__Parent.`Time2`) / 1000) > floor(microsecond(now()) / 1000)) } [Fact] public void Ticks() { @@ -285,17 +142,6 @@ namespace FreeSql.Tests.SqlServerExpression { data.Add(select.Where(a => a.CreateTime.Ticks > DateTime.Now.Ticks).ToList()); data.Add(select.Where(a => a.Type.Time.Ticks > DateTime.Now.Ticks).ToList()); data.Add(select.Where(a => a.Type.Parent.Time2.Ticks > DateTime.Now.Ticks).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic111333` a - //WHERE ((timestampdiff(microsecond, '1970-1-1', a.`CreateTime`) * 10 + 621355968000000000) > (timestampdiff(microsecond, '1970-1-1', now()) * 10 + 621355968000000000)); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a__Type.`Time` as7, a.`Title` as8, a.`CreateTime` as9 - //FROM `tb_topic111333` a, `TestTypeInfo333` a__Type - //WHERE ((timestampdiff(microsecond, '1970-1-1', a__Type.`Time`) * 10 + 621355968000000000) > (timestampdiff(microsecond, '1970-1-1', now()) * 10 + 621355968000000000)); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a__Type.`Time` as7, a.`Title` as8, a.`CreateTime` as9 - //FROM `tb_topic111333` a, `TestTypeInfo333` a__Type, `TestTypeParentInfo23123` a__Type__Parent - //WHERE ((timestampdiff(microsecond, '1970-1-1', a__Type__Parent.`Time2`) * 10 + 621355968000000000) > (timestampdiff(microsecond, '1970-1-1', now()) * 10 + 621355968000000000)) } [Fact] public void Add() { @@ -303,17 +149,6 @@ namespace FreeSql.Tests.SqlServerExpression { data.Add(select.Where(a => a.CreateTime.Add(TimeSpan.FromDays(1)) > DateTime.Now).ToList()); data.Add(select.Where(a => a.Type.Time.Add(TimeSpan.FromDays(1)) > DateTime.Now).ToList()); data.Add(select.Where(a => a.Type.Parent.Time2.Add(TimeSpan.FromDays(1)) > DateTime.Now).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic111333` a - //WHERE (date_add(a.`CreateTime`, interval ((1 * 86400000000)) microsecond) > now()); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a__Type.`Time` as7, a.`Title` as8, a.`CreateTime` as9 - //FROM `tb_topic111333` a, `TestTypeInfo333` a__Type - //WHERE (date_add(a__Type.`Time`, interval ((1 * 86400000000)) microsecond) > now()); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a__Type.`Time` as7, a.`Title` as8, a.`CreateTime` as9 - //FROM `tb_topic111333` a, `TestTypeInfo333` a__Type, `TestTypeParentInfo23123` a__Type__Parent - //WHERE (date_add(a__Type__Parent.`Time2`, interval ((1 * 86400000000)) microsecond) > now()) } [Fact] public void AddDays() { @@ -321,17 +156,6 @@ namespace FreeSql.Tests.SqlServerExpression { data.Add(select.Where(a => a.CreateTime.AddDays(1) > DateTime.Now).ToList()); data.Add(select.Where(a => a.Type.Time.AddDays(1) > DateTime.Now).ToList()); data.Add(select.Where(a => a.Type.Parent.Time2.AddDays(1) > DateTime.Now).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic111333` a - //WHERE (date_add(a.`CreateTime`, interval (1) day) > now()); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a__Type.`Time` as7, a.`Title` as8, a.`CreateTime` as9 - //FROM `tb_topic111333` a, `TestTypeInfo333` a__Type - //WHERE (date_add(a__Type.`Time`, interval (1) day) > now()); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a__Type.`Time` as7, a.`Title` as8, a.`CreateTime` as9 - //FROM `tb_topic111333` a, `TestTypeInfo333` a__Type, `TestTypeParentInfo23123` a__Type__Parent - //WHERE (date_add(a__Type__Parent.`Time2`, interval (1) day) > now()) } [Fact] public void AddHours() { @@ -339,17 +163,6 @@ namespace FreeSql.Tests.SqlServerExpression { data.Add(select.Where(a => a.CreateTime.AddHours(1) > DateTime.Now).ToList()); data.Add(select.Where(a => a.Type.Time.AddHours(1) > DateTime.Now).ToList()); data.Add(select.Where(a => a.Type.Parent.Time2.AddHours(1) > DateTime.Now).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic111333` a - //WHERE (date_add(a.`CreateTime`, interval (1) hour) > now()); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a__Type.`Time` as7, a.`Title` as8, a.`CreateTime` as9 - //FROM `tb_topic111333` a, `TestTypeInfo333` a__Type - //WHERE (date_add(a__Type.`Time`, interval (1) hour) > now()); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a__Type.`Time` as7, a.`Title` as8, a.`CreateTime` as9 - //FROM `tb_topic111333` a, `TestTypeInfo333` a__Type, `TestTypeParentInfo23123` a__Type__Parent - //WHERE (date_add(a__Type__Parent.`Time2`, interval (1) hour) > now()) } [Fact] public void AddMilliseconds() { @@ -357,17 +170,6 @@ namespace FreeSql.Tests.SqlServerExpression { data.Add(select.Where(a => a.CreateTime.AddMilliseconds(1) > DateTime.Now).ToList()); data.Add(select.Where(a => a.Type.Time.AddMilliseconds(1) > DateTime.Now).ToList()); data.Add(select.Where(a => a.Type.Parent.Time2.AddMilliseconds(1) > DateTime.Now).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic111333` a - //WHERE (date_add(a.`CreateTime`, interval (1) * 1000 microsecond) > now()); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a__Type.`Time` as7, a.`Title` as8, a.`CreateTime` as9 - //FROM `tb_topic111333` a, `TestTypeInfo333` a__Type - //WHERE (date_add(a__Type.`Time`, interval (1) * 1000 microsecond) > now()); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a__Type.`Time` as7, a.`Title` as8, a.`CreateTime` as9 - //FROM `tb_topic111333` a, `TestTypeInfo333` a__Type, `TestTypeParentInfo23123` a__Type__Parent - //WHERE (date_add(a__Type__Parent.`Time2`, interval (1) * 1000 microsecond) > now()) } [Fact] public void AddMinutes() { @@ -375,17 +177,6 @@ namespace FreeSql.Tests.SqlServerExpression { data.Add(select.Where(a => a.CreateTime.AddMinutes(1) > DateTime.Now).ToList()); data.Add(select.Where(a => a.Type.Time.AddMinutes(1) > DateTime.Now).ToList()); data.Add(select.Where(a => a.Type.Parent.Time2.AddMinutes(1) > DateTime.Now).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic111333` a - //WHERE (date_add(a.`CreateTime`, interval (1) minute) > now()); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a__Type.`Time` as7, a.`Title` as8, a.`CreateTime` as9 - //FROM `tb_topic111333` a, `TestTypeInfo333` a__Type - //WHERE (date_add(a__Type.`Time`, interval (1) minute) > now()); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a__Type.`Time` as7, a.`Title` as8, a.`CreateTime` as9 - //FROM `tb_topic111333` a, `TestTypeInfo333` a__Type, `TestTypeParentInfo23123` a__Type__Parent - //WHERE (date_add(a__Type__Parent.`Time2`, interval (1) minute) > now()) } [Fact] public void AddMonths() { @@ -393,17 +184,6 @@ namespace FreeSql.Tests.SqlServerExpression { data.Add(select.Where(a => a.CreateTime.AddMonths(1) > DateTime.Now).ToList()); data.Add(select.Where(a => a.Type.Time.AddMonths(1) > DateTime.Now).ToList()); data.Add(select.Where(a => a.Type.Parent.Time2.AddMonths(1) > DateTime.Now).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic111333` a - //WHERE (date_add(a.`CreateTime`, interval (1) month) > now()); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a__Type.`Time` as7, a.`Title` as8, a.`CreateTime` as9 - //FROM `tb_topic111333` a, `TestTypeInfo333` a__Type - //WHERE (date_add(a__Type.`Time`, interval (1) month) > now()); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a__Type.`Time` as7, a.`Title` as8, a.`CreateTime` as9 - //FROM `tb_topic111333` a, `TestTypeInfo333` a__Type, `TestTypeParentInfo23123` a__Type__Parent - //WHERE (date_add(a__Type__Parent.`Time2`, interval (1) month) > now()) } [Fact] public void AddSeconds() { @@ -411,17 +191,6 @@ namespace FreeSql.Tests.SqlServerExpression { data.Add(select.Where(a => a.CreateTime.AddSeconds(1) > DateTime.Now).ToList()); data.Add(select.Where(a => a.Type.Time.AddSeconds(1) > DateTime.Now).ToList()); data.Add(select.Where(a => a.Type.Parent.Time2.AddSeconds(1) > DateTime.Now).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic111333` a - //WHERE (date_add(a.`CreateTime`, interval (1) second) > now()); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a__Type.`Time` as7, a.`Title` as8, a.`CreateTime` as9 - //FROM `tb_topic111333` a, `TestTypeInfo333` a__Type - //WHERE (date_add(a__Type.`Time`, interval (1) second) > now()); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a__Type.`Time` as7, a.`Title` as8, a.`CreateTime` as9 - //FROM `tb_topic111333` a, `TestTypeInfo333` a__Type, `TestTypeParentInfo23123` a__Type__Parent - //WHERE (date_add(a__Type__Parent.`Time2`, interval (1) second) > now()) } [Fact] public void AddTicks() { @@ -429,17 +198,6 @@ namespace FreeSql.Tests.SqlServerExpression { data.Add(select.Where(a => a.CreateTime.AddTicks(1) > DateTime.Now).ToList()); data.Add(select.Where(a => a.Type.Time.AddTicks(1) > DateTime.Now).ToList()); data.Add(select.Where(a => a.Type.Parent.Time2.AddTicks(1) > DateTime.Now).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic111333` a - //WHERE (date_add(a.`CreateTime`, interval (1) / 10 microsecond) > now()); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a__Type.`Time` as7, a.`Title` as8, a.`CreateTime` as9 - //FROM `tb_topic111333` a, `TestTypeInfo333` a__Type - //WHERE (date_add(a__Type.`Time`, interval (1) / 10 microsecond) > now()); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a__Type.`Time` as7, a.`Title` as8, a.`CreateTime` as9 - //FROM `tb_topic111333` a, `TestTypeInfo333` a__Type, `TestTypeParentInfo23123` a__Type__Parent - //WHERE (date_add(a__Type__Parent.`Time2`, interval (1) / 10 microsecond) > now()) } [Fact] public void AddYears() { @@ -447,17 +205,6 @@ namespace FreeSql.Tests.SqlServerExpression { data.Add(select.Where(a => a.CreateTime.AddYears(1) > DateTime.Now).ToList()); data.Add(select.Where(a => a.Type.Time.AddYears(1) > DateTime.Now).ToList()); data.Add(select.Where(a => a.Type.Parent.Time2.AddYears(1) > DateTime.Now).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic111333` a - //WHERE (date_add(a.`CreateTime`, interval (1) year) > now()); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a__Type.`Time` as7, a.`Title` as8, a.`CreateTime` as9 - //FROM `tb_topic111333` a, `TestTypeInfo333` a__Type - //WHERE (date_add(a__Type.`Time`, interval (1) year) > now()); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a__Type.`Time` as7, a.`Title` as8, a.`CreateTime` as9 - //FROM `tb_topic111333` a, `TestTypeInfo333` a__Type, `TestTypeParentInfo23123` a__Type__Parent - //WHERE (date_add(a__Type__Parent.`Time2`, interval (1) year) > now()) } [Fact] public void Subtract() { @@ -465,31 +212,10 @@ namespace FreeSql.Tests.SqlServerExpression { data.Add(select.Where(a => a.CreateTime.Subtract(DateTime.Now).TotalSeconds > 0).ToList()); data.Add(select.Where(a => a.Type.Time.Subtract(DateTime.Now).TotalSeconds > 0).ToList()); data.Add(select.Where(a => a.Type.Parent.Time2.Subtract(DateTime.Now).TotalSeconds > 0).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic111333` a - //WHERE (((timestampdiff(microsecond, now(), a.`CreateTime`)) / 1000000) > 0); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a__Type.`Time` as7, a.`Title` as8, a.`CreateTime` as9 - //FROM `tb_topic111333` a, `TestTypeInfo333` a__Type - //WHERE (((timestampdiff(microsecond, now(), a__Type.`Time`)) / 1000000) > 0); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a__Type.`Time` as7, a.`Title` as8, a.`CreateTime` as9 - //FROM `tb_topic111333` a, `TestTypeInfo333` a__Type, `TestTypeParentInfo23123` a__Type__Parent - //WHERE (((timestampdiff(microsecond, now(), a__Type__Parent.`Time2`)) / 1000000) > 0); data.Add(select.Where(a => a.CreateTime.Subtract(TimeSpan.FromDays(1)) > a.CreateTime).ToList()); data.Add(select.Where(a => a.Type.Time.Subtract(TimeSpan.FromDays(1)) > a.CreateTime).ToList()); data.Add(select.Where(a => a.Type.Parent.Time2.Subtract(TimeSpan.FromDays(1)) > a.CreateTime).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic111333` a - //WHERE (date_sub(a.`CreateTime`, interval ((1 * 86400000000)) microsecond) > a.`CreateTime`); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a__Type.`Time` as7, a.`Title` as8, a.`CreateTime` as9 - //FROM `tb_topic111333` a, `TestTypeInfo333` a__Type - //WHERE (date_sub(a__Type.`Time`, interval ((1 * 86400000000)) microsecond) > a.`CreateTime`); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a__Type.`Time` as7, a.`Title` as8, a.`CreateTime` as9 - //FROM `tb_topic111333` a, `TestTypeInfo333` a__Type, `TestTypeParentInfo23123` a__Type__Parent - //WHERE (date_sub(a__Type__Parent.`Time2`, interval ((1 * 86400000000)) microsecond) > a.`CreateTime`) } [Fact] public void this_Equals() { @@ -497,17 +223,6 @@ namespace FreeSql.Tests.SqlServerExpression { data.Add(select.Where(a => a.CreateTime.AddYears(1).Equals(DateTime.Now)).ToList()); data.Add(select.Where(a => a.Type.Time.AddYears(1).Equals(DateTime.Now)).ToList()); data.Add(select.Where(a => a.Type.Parent.Time2.AddYears(1).Equals(DateTime.Now)).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic111333` a - //WHERE ((date_add(a.`CreateTime`, interval (1) year) = now())); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a__Type.`Time` as7, a.`Title` as8, a.`CreateTime` as9 - //FROM `tb_topic111333` a, `TestTypeInfo333` a__Type - //WHERE ((date_add(a__Type.`Time`, interval (1) year) = now())); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a__Type.`Time` as7, a.`Title` as8, a.`CreateTime` as9 - //FROM `tb_topic111333` a, `TestTypeInfo333` a__Type, `TestTypeParentInfo23123` a__Type__Parent - //WHERE ((date_add(a__Type__Parent.`Time2`, interval (1) year) = now())) } [Fact] public void this_ToString() { @@ -515,17 +230,6 @@ namespace FreeSql.Tests.SqlServerExpression { data.Add(select.Where(a => a.CreateTime.ToString().Equals(DateTime.Now)).ToList()); data.Add(select.Where(a => a.Type.Time.AddYears(1).ToString().Equals(DateTime.Now)).ToList()); data.Add(select.Where(a => a.Type.Parent.Time2.AddYears(1).ToString().Equals(DateTime.Now)).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic111333` a - //WHERE ((date_format(a.`CreateTime`, '%Y-%m-%d %H:%i:%s.%f') = now())); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a__Type.`Time` as7, a.`Title` as8, a.`CreateTime` as9 - //FROM `tb_topic111333` a, `TestTypeInfo333` a__Type - //WHERE ((date_format(date_add(a__Type.`Time`, interval (1) year), '%Y-%m-%d %H:%i:%s.%f') = now())); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a__Type.`Time` as7, a.`Title` as8, a.`CreateTime` as9 - //FROM `tb_topic111333` a, `TestTypeInfo333` a__Type, `TestTypeParentInfo23123` a__Type__Parent - //WHERE ((date_format(date_add(a__Type__Parent.`Time2`, interval (1) year), '%Y-%m-%d %H:%i:%s.%f') = now())) } [Fact] @@ -534,17 +238,6 @@ namespace FreeSql.Tests.SqlServerExpression { data.Add(select.Where(a => a.CreateTime.CompareTo(DateTime.Now) == 0).ToList()); data.Add(select.Where(a => a.Type.Time.AddYears(1).CompareTo(DateTime.Now) == 0).ToList()); data.Add(select.Where(a => a.Type.Parent.Time2.AddYears(1).CompareTo(DateTime.Now) == 0).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic111333` a - //WHERE (((a.`CreateTime`) - (now())) = 0); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a__Type.`Time` as7, a.`Title` as8, a.`CreateTime` as9 - //FROM `tb_topic111333` a, `TestTypeInfo333` a__Type - //WHERE (((date_add(a__Type.`Time`, interval (1) year)) - (now())) = 0); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a__Type.`Time` as7, a.`Title` as8, a.`CreateTime` as9 - //FROM `tb_topic111333` a, `TestTypeInfo333` a__Type, `TestTypeParentInfo23123` a__Type__Parent - //WHERE (((date_add(a__Type__Parent.`Time2`, interval (1) year)) - (now())) = 0) } [Fact] public void DateTime_DaysInMonth() { @@ -552,17 +245,6 @@ namespace FreeSql.Tests.SqlServerExpression { data.Add(select.Where(a => DateTime.DaysInMonth(a.CreateTime.Year, a.CreateTime.Month) > 30).ToList()); data.Add(select.Where(a => DateTime.DaysInMonth(a.Type.Time.Year, a.Type.Time.Month) > 30).ToList()); data.Add(select.Where(a => DateTime.DaysInMonth(a.Type.Parent.Time2.Year, a.Type.Parent.Time2.Month) > 30).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic111333` a - //WHERE (dayofmonth(last_day(concat(year(a.`CreateTime`), month(a.`CreateTime`), '-01'))) > 30); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a__Type.`Time` as7, a.`Title` as8, a.`CreateTime` as9 - //FROM `tb_topic111333` a, `TestTypeInfo333` a__Type - //WHERE (dayofmonth(last_day(concat(year(a__Type.`Time`), month(a__Type.`Time`), '-01'))) > 30); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a__Type.`Time` as7, a.`Title` as8, a.`CreateTime` as9 - //FROM `tb_topic111333` a, `TestTypeInfo333` a__Type, `TestTypeParentInfo23123` a__Type__Parent - //WHERE (dayofmonth(last_day(concat(year(a__Type__Parent.`Time2`), month(a__Type__Parent.`Time2`), '-01'))) > 30) } [Fact] public void DateTime_Equals() { @@ -570,17 +252,6 @@ namespace FreeSql.Tests.SqlServerExpression { data.Add(select.Where(a => DateTime.Equals(a.CreateTime.AddYears(1), DateTime.Now)).ToList()); data.Add(select.Where(a => DateTime.Equals(a.Type.Time.AddYears(1), DateTime.Now)).ToList()); data.Add(select.Where(a => DateTime.Equals(a.Type.Parent.Time2.AddYears(1), DateTime.Now)).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic111333` a - //WHERE ((date_add(a.`CreateTime`, interval (1) year) = now())); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a__Type.`Time` as7, a.`Title` as8, a.`CreateTime` as9 - //FROM `tb_topic111333` a, `TestTypeInfo333` a__Type - //WHERE ((date_add(a__Type.`Time`, interval (1) year) = now())); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a__Type.`Time` as7, a.`Title` as8, a.`CreateTime` as9 - //FROM `tb_topic111333` a, `TestTypeInfo333` a__Type, `TestTypeParentInfo23123` a__Type__Parent - //WHERE ((date_add(a__Type__Parent.`Time2`, interval (1) year) = now())) } [Fact] public void DateTime_IsLeapYear() { @@ -588,17 +259,6 @@ namespace FreeSql.Tests.SqlServerExpression { data.Add(select.Where(a => DateTime.IsLeapYear(a.CreateTime.Year)).ToList()); data.Add(select.Where(a => DateTime.IsLeapYear(a.Type.Time.AddYears(1).Year)).ToList()); data.Add(select.Where(a => DateTime.IsLeapYear(a.Type.Parent.Time2.AddYears(1).Year)).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic111333` a - //WHERE (((year(a.`CreateTime`)) % 4 = 0 AND (year(a.`CreateTime`)) % 100 <> 0 OR (year(a.`CreateTime`)) % 400 = 0)); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a__Type.`Time` as7, a.`Title` as8, a.`CreateTime` as9 - //FROM `tb_topic111333` a, `TestTypeInfo333` a__Type - //WHERE (((year(date_add(a__Type.`Time`, interval (1) year))) % 4 = 0 AND (year(date_add(a__Type.`Time`, interval (1) year))) % 100 <> 0 OR (year(date_add(a__Type.`Time`, interval (1) year))) % 400 = 0)); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a__Type.`Time` as7, a.`Title` as8, a.`CreateTime` as9 - //FROM `tb_topic111333` a, `TestTypeInfo333` a__Type, `TestTypeParentInfo23123` a__Type__Parent - //WHERE (((year(date_add(a__Type__Parent.`Time2`, interval (1) year))) % 4 = 0 AND (year(date_add(a__Type__Parent.`Time2`, interval (1) year))) % 100 <> 0 OR (year(date_add(a__Type__Parent.`Time2`, interval (1) year))) % 400 = 0)) } [Fact] public void DateTime_Parse() { @@ -606,17 +266,6 @@ namespace FreeSql.Tests.SqlServerExpression { data.Add(select.Where(a => DateTime.Parse(a.CreateTime.ToString()) > DateTime.Now).ToList()); data.Add(select.Where(a => DateTime.Parse(a.Type.Time.AddYears(1).ToString()) > DateTime.Now).ToList()); data.Add(select.Where(a => DateTime.Parse(a.Type.Parent.Time2.AddYears(1).ToString()) > DateTime.Now).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic111333` a - //WHERE (cast(date_format(a.`CreateTime`, '%Y-%m-%d %H:%i:%s.%f') as datetime) > now()); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a__Type.`Time` as7, a.`Title` as8, a.`CreateTime` as9 - //FROM `tb_topic111333` a, `TestTypeInfo333` a__Type - //WHERE (cast(date_format(date_add(a__Type.`Time`, interval (1) year), '%Y-%m-%d %H:%i:%s.%f') as datetime) > now()); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a__Type.`Time` as7, a.`Title` as8, a.`CreateTime` as9 - //FROM `tb_topic111333` a, `TestTypeInfo333` a__Type, `TestTypeParentInfo23123` a__Type__Parent - //WHERE (cast(date_format(date_add(a__Type__Parent.`Time2`, interval (1) year), '%Y-%m-%d %H:%i:%s.%f') as datetime) > now()) } } } diff --git a/FreeSql.Tests/SqlServer/SqlServerExpression/StringTest.cs b/FreeSql.Tests/SqlServer/SqlServerExpression/StringTest.cs index 1605a171..1b6324eb 100644 --- a/FreeSql.Tests/SqlServer/SqlServerExpression/StringTest.cs +++ b/FreeSql.Tests/SqlServer/SqlServerExpression/StringTest.cs @@ -36,9 +36,6 @@ namespace FreeSql.Tests.SqlServerExpression { public void Empty() { var data = new List(); data.Add(select.Where(a => (a.Title ?? "") == string.Empty).ToSql()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE (ifnull(a.`Title`, '') = '') } [Fact] @@ -48,40 +45,11 @@ namespace FreeSql.Tests.SqlServerExpression { list.Add(select.Where(a => a.Title.StartsWith(a.Title)).ToList()); list.Add(select.Where(a => a.Title.StartsWith(a.Title + 1)).ToList()); list.Add(select.Where(a => a.Title.StartsWith(a.Type.Name)).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE((a.`Title`) LIKE '%aaa') - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE((a.`Title`) LIKE concat('%', a.`Title`)) - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE((a.`Title`) LIKE concat('%', concat(a.`Title`, 1))) - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a.`Title` as7, a.`CreateTime` as8 - //FROM `tb_topic` a, `TestTypeInfo` a__Type - //WHERE((a.`Title`) LIKE concat('%', a__Type.`Name`)) list.Add(select.Where(a => (a.Title + "aaa").StartsWith("aaa")).ToList()); list.Add(select.Where(a => (a.Title + "aaa").StartsWith(a.Title)).ToList()); list.Add(select.Where(a => (a.Title + "aaa").StartsWith(a.Title + 1)).ToList()); list.Add(select.Where(a => (a.Title + "aaa").StartsWith(a.Type.Name)).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE((concat(a.`Title`, 'aaa')) LIKE '%aaa') - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE((concat(a.`Title`, 'aaa')) LIKE concat('%', a.`Title`)) - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE((concat(a.`Title`, 'aaa')) LIKE concat('%', concat(a.`Title`, 1))) - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a.`Title` as7, a.`CreateTime` as8 - //FROM `tb_topic` a, `TestTypeInfo` a__Type - //WHERE((concat(a.`Title`, 'aaa')) LIKE concat('%', a__Type.`Name`)) } [Fact] public void EndsWith() { @@ -90,40 +58,11 @@ namespace FreeSql.Tests.SqlServerExpression { list.Add(select.Where(a => a.Title.EndsWith(a.Title)).ToList()); list.Add(select.Where(a => a.Title.EndsWith(a.Title + 1)).ToList()); list.Add(select.Where(a => a.Title.EndsWith(a.Type.Name)).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE((a.`Title`) LIKE 'aaa%') - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE((a.`Title`) LIKE concat(a.`Title`, '%')) - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE((a.`Title`) LIKE concat(concat(a.`Title`, 1), '%')) - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a.`Title` as7, a.`CreateTime` as8 - //FROM `tb_topic` a, `TestTypeInfo` a__Type - //WHERE((a.`Title`) LIKE concat(a__Type.`Name`, '%')) list.Add(select.Where(a => (a.Title + "aaa").EndsWith("aaa")).ToList()); list.Add(select.Where(a => (a.Title + "aaa").EndsWith(a.Title)).ToList()); list.Add(select.Where(a => (a.Title + "aaa").EndsWith(a.Title + 1)).ToList()); list.Add(select.Where(a => (a.Title + "aaa").EndsWith(a.Type.Name)).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE((concat(a.`Title`, 'aaa')) LIKE 'aaa%') - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE((concat(a.`Title`, 'aaa')) LIKE concat(a.`Title`, '%')) - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE((concat(a.`Title`, 'aaa')) LIKE concat(concat(a.`Title`, 1), '%')) - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a.`Title` as7, a.`CreateTime` as8 - //FROM `tb_topic` a, `TestTypeInfo` a__Type - //WHERE((concat(a.`Title`, 'aaa')) LIKE concat(a__Type.`Name`, '%')) } [Fact] public void Contains() { @@ -132,40 +71,11 @@ namespace FreeSql.Tests.SqlServerExpression { list.Add(select.Where(a => a.Title.Contains(a.Title)).ToList()); list.Add(select.Where(a => a.Title.Contains(a.Title + 1)).ToList()); list.Add(select.Where(a => a.Title.Contains(a.Type.Name)).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE((a.`Title`) LIKE '%aaa%') - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE((a.`Title`) LIKE concat('%', a.`Title`, '%')) - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE((a.`Title`) LIKE concat('%', a.`Title` +1, '%')) - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a.`Title` as7, a.`CreateTime` as8 - //FROM `tb_topic` a, `TestTypeInfo` a__Type - //WHERE((a.`Title`) LIKE concat('%', a__Type.`Name`, '%')) list.Add(select.Where(a => (a.Title + "aaa").Contains("aaa")).ToList()); list.Add(select.Where(a => (a.Title + "aaa").Contains(a.Title)).ToList()); list.Add(select.Where(a => (a.Title + "aaa").Contains(a.Title + 1)).ToList()); list.Add(select.Where(a => (a.Title + "aaa").Contains(a.Type.Name)).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE((concat(a.`Title`, 'aaa')) LIKE '%aaa%') - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE((concat(a.`Title`, 'aaa')) LIKE concat('%', a.`Title`, '%')) - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE((concat(a.`Title`, 'aaa')) LIKE concat('%', concat(a.`Title`, 1), '%')) - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a.`Title` as7, a.`CreateTime` as8 - //FROM `tb_topic` a, `TestTypeInfo` a__Type - //WHERE((concat(a.`Title`, 'aaa')) LIKE concat('%', a__Type.`Name`, '%')) } [Fact] public void ToLower() { @@ -174,40 +84,11 @@ namespace FreeSql.Tests.SqlServerExpression { data.Add(select.Where(a => a.Title.ToLower() == a.Title).ToList()); data.Add(select.Where(a => a.Title.ToLower() == (a.Title + 1)).ToList()); data.Add(select.Where(a => a.Title.ToLower() == a.Type.Name).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE(lower(a.`Title`) = 'aaa'); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE(lower(a.`Title`) = a.`Title`); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE(lower(a.`Title`) = concat(a.`Title`, 1)); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a.`Title` as7, a.`CreateTime` as8 - //FROM `tb_topic` a, `TestTypeInfo` a__Type - //WHERE(lower(a.`Title`) = a__Type.`Name`); data.Add(select.Where(a => (a.Title.ToLower() + "aaa").ToLower() == "aaa").ToList()); data.Add(select.Where(a => (a.Title.ToLower() + "aaa").ToLower() == a.Title).ToList()); data.Add(select.Where(a => (a.Title.ToLower() + "aaa").ToLower() == (a.Title + 1)).ToList()); data.Add(select.Where(a => (a.Title.ToLower() + "aaa").ToLower() == a.Type.Name).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE(lower(concat(lower(a.`Title`), 'aaa')) = 'aaa'); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE(lower(concat(lower(a.`Title`), 'aaa')) = a.`Title`); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE(lower(concat(lower(a.`Title`), 'aaa')) = concat(a.`Title`, 1)); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a.`Title` as7, a.`CreateTime` as8 - //FROM `tb_topic` a, `TestTypeInfo` a__Type - //WHERE(lower(concat(lower(a.`Title`), 'aaa')) = a__Type.`Name`) } [Fact] public void ToUpper() { @@ -216,40 +97,11 @@ namespace FreeSql.Tests.SqlServerExpression { data.Add(select.Where(a => a.Title.ToUpper() == a.Title).ToList()); data.Add(select.Where(a => a.Title.ToUpper() == (a.Title + 1)).ToList()); data.Add(select.Where(a => a.Title.ToUpper() == a.Type.Name).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE (upper(a.`Title`) = 'aaa'); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE (upper(a.`Title`) = a.`Title`); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE (upper(a.`Title`) = concat(a.`Title`, 1)); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a.`Title` as7, a.`CreateTime` as8 - //FROM `tb_topic` a, `TestTypeInfo` a__Type - //WHERE (upper(a.`Title`) = a__Type.`Name`); data.Add(select.Where(a => (a.Title.ToUpper() + "aaa").ToUpper() == "aaa").ToList()); data.Add(select.Where(a => (a.Title.ToUpper() + "aaa").ToUpper() == a.Title).ToList()); data.Add(select.Where(a => (a.Title.ToUpper() + "aaa").ToUpper() == (a.Title + 1)).ToList()); data.Add(select.Where(a => (a.Title.ToUpper() + "aaa").ToUpper() == a.Type.Name).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE (upper(concat(upper(a.`Title`), 'aaa')) = 'aaa'); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE (upper(concat(upper(a.`Title`), 'aaa')) = a.`Title`); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE (upper(concat(upper(a.`Title`), 'aaa')) = concat(a.`Title`, 1)); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a.`Title` as7, a.`CreateTime` as8 - //FROM `tb_topic` a, `TestTypeInfo` a__Type - //WHERE (upper(concat(upper(a.`Title`), 'aaa')) = a__Type.`Name`) } [Fact] public void Substring() { @@ -258,40 +110,11 @@ namespace FreeSql.Tests.SqlServerExpression { data.Add(select.Where(a => a.Title.Substring(0) == a.Title).ToList()); data.Add(select.Where(a => a.Title.Substring(0) == (a.Title + 1)).ToList()); data.Add(select.Where(a => a.Title.Substring(0) == a.Type.Name).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE (substr(a.`Title`, 1) = 'aaa'); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE (substr(a.`Title`, 1) = a.`Title`); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE (substr(a.`Title`, 1) = concat(a.`Title`, 1)); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a.`Title` as7, a.`CreateTime` as8 - //FROM `tb_topic` a, `TestTypeInfo` a__Type - //WHERE (substr(a.`Title`, 1) = a__Type.`Name`); data.Add(select.Where(a => (a.Title.Substring(0) + "aaa").Substring(a.Title.Length) == "aaa").ToList()); data.Add(select.Where(a => (a.Title.Substring(0) + "aaa").Substring(0, a.Title.Length) == a.Title).ToList()); data.Add(select.Where(a => (a.Title.Substring(0) + "aaa").Substring(0, 3) == (a.Title + 1)).ToList()); data.Add(select.Where(a => (a.Title.Substring(0) + "aaa").Substring(1, 2) == a.Type.Name).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE (substr(concat(substr(a.`Title`, 1), 'aaa'), char_length(a.`Title`) + 1) = 'aaa'); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE (substr(concat(substr(a.`Title`, 1), 'aaa'), 1, char_length(a.`Title`)) = a.`Title`); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE (substr(concat(substr(a.`Title`, 1), 'aaa'), 1, 3) = concat(a.`Title`, 1)); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a.`Title` as7, a.`CreateTime` as8 - //FROM `tb_topic` a, `TestTypeInfo` a__Type - //WHERE (substr(concat(substr(a.`Title`, 1), 'aaa'), 2, 2) = a__Type.`Name`) } [Fact] public void Length() { @@ -300,40 +123,11 @@ namespace FreeSql.Tests.SqlServerExpression { data.Add(select.Where(a => a.Title.Length == 1).ToList()); data.Add(select.Where(a => a.Title.Length == a.Title.Length + 1).ToList()); data.Add(select.Where(a => a.Title.Length == a.Type.Name.Length).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE (char_length(a.`Title`) = 0); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE (char_length(a.`Title`) = 1); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE (char_length(a.`Title`) = char_length(a.`Title`) + 1); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a.`Title` as7, a.`CreateTime` as8 - //FROM `tb_topic` a, `TestTypeInfo` a__Type - //WHERE (char_length(a.`Title`) = char_length(a__Type.`Name`)); data.Add(select.Where(a => (a.Title + "aaa").Length == 0).ToList()); data.Add(select.Where(a => (a.Title + "aaa").Length == 1).ToList()); data.Add(select.Where(a => (a.Title + "aaa").Length == a.Title.Length + 1).ToList()); data.Add(select.Where(a => (a.Title + "aaa").Length == a.Type.Name.Length).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE (char_length(concat(a.`Title`, 'aaa')) = 0); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE (char_length(concat(a.`Title`, 'aaa')) = 1); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE (char_length(concat(a.`Title`, 'aaa')) = char_length(a.`Title`) + 1); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a.`Title` as7, a.`CreateTime` as8 - //FROM `tb_topic` a, `TestTypeInfo` a__Type - //WHERE (char_length(concat(a.`Title`, 'aaa')) = char_length(a__Type.`Name`)) } [Fact] public void IndexOf() { @@ -342,40 +136,11 @@ namespace FreeSql.Tests.SqlServerExpression { data.Add(select.Where(a => a.Title.IndexOf("aaa", 2) == -1).ToList()); data.Add(select.Where(a => a.Title.IndexOf("aaa", 2) == (a.Title.Length + 1)).ToList()); data.Add(select.Where(a => a.Title.IndexOf("aaa", 2) == a.Type.Name.Length + 1).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE ((locate(a.`Title`, 'aaa') - 1) = -1); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE ((locate(a.`Title`, 'aaa', 3) - 1) = -1); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE ((locate(a.`Title`, 'aaa', 3) - 1) = char_length(a.`Title`) + 1); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a.`Title` as7, a.`CreateTime` as8 - //FROM `tb_topic` a, `TestTypeInfo` a__Type - //WHERE ((locate(a.`Title`, 'aaa', 3) - 1) = char_length(a__Type.`Name`) + 1); data.Add(select.Where(a => (a.Title + "aaa").IndexOf("aaa") == -1).ToList()); data.Add(select.Where(a => (a.Title + "aaa").IndexOf("aaa", 2) == -1).ToList()); data.Add(select.Where(a => (a.Title + "aaa").IndexOf("aaa", 2) == (a.Title.Length + 1)).ToList()); data.Add(select.Where(a => (a.Title + "aaa").IndexOf("aaa", 2) == a.Type.Name.Length + 1).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE ((locate(concat(a.`Title`, 'aaa'), 'aaa') - 1) = -1); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE ((locate(concat(a.`Title`, 'aaa'), 'aaa', 3) - 1) = -1); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE ((locate(concat(a.`Title`, 'aaa'), 'aaa', 3) - 1) = char_length(a.`Title`) + 1); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a.`Title` as7, a.`CreateTime` as8 - //FROM `tb_topic` a, `TestTypeInfo` a__Type - //WHERE ((locate(concat(a.`Title`, 'aaa'), 'aaa', 3) - 1) = char_length(a__Type.`Name`) + 1) } [Fact] public void PadLeft() { @@ -384,40 +149,11 @@ namespace FreeSql.Tests.SqlServerExpression { //data.Add(select.Where(a => a.Title.PadLeft(10, 'a') == a.Title).ToList()); //data.Add(select.Where(a => a.Title.PadLeft(10, 'a') == (a.Title + 1)).ToList()); //data.Add(select.Where(a => a.Title.PadLeft(10, 'a') == a.Type.Name).ToList()); - ////SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - ////FROM `tb_topic` a - ////WHERE (lpad(a.`Title`, 10, 'a') = 'aaa'); - ////SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - ////FROM `tb_topic` a - ////WHERE (lpad(a.`Title`, 10, 'a') = a.`Title`); - - ////SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - ////FROM `tb_topic` a - ////WHERE (lpad(a.`Title`, 10, 'a') = concat(a.`Title`, 1)); - - ////SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a.`Title` as7, a.`CreateTime` as8 - ////FROM `tb_topic` a, `TestTypeInfo` a__Type - ////WHERE (lpad(a.`Title`, 10, 'a') = a__Type.`Name`); //data.Add(select.Where(a => (a.Title.PadLeft(10, 'a') + "aaa").PadLeft(20, 'b') == "aaa").ToList()); //data.Add(select.Where(a => (a.Title.PadLeft(10, 'a') + "aaa").PadLeft(20, 'b') == a.Title).ToList()); //data.Add(select.Where(a => (a.Title.PadLeft(10, 'a') + "aaa").PadLeft(20, 'b') == (a.Title + 1)).ToList()); //data.Add(select.Where(a => (a.Title.PadLeft(10, 'a') + "aaa").PadLeft(20, 'b') == a.Type.Name).ToList()); - ////SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - ////FROM `tb_topic` a - ////WHERE (lpad(concat(lpad(a.`Title`, 10, 'a'), 'aaa'), 20, 'b') = 'aaa'); - - ////SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - ////FROM `tb_topic` a - ////WHERE (lpad(concat(lpad(a.`Title`, 10, 'a'), 'aaa'), 20, 'b') = a.`Title`); - - ////SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - ////FROM `tb_topic` a - ////WHERE (lpad(concat(lpad(a.`Title`, 10, 'a'), 'aaa'), 20, 'b') = concat(a.`Title`, 1)); - - ////SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a.`Title` as7, a.`CreateTime` as8 - ////FROM `tb_topic` a, `TestTypeInfo` a__Type - ////WHERE (lpad(concat(lpad(a.`Title`, 10, 'a'), 'aaa'), 20, 'b') = a__Type.`Name`) } [Fact] public void PadRight() { @@ -426,40 +162,11 @@ namespace FreeSql.Tests.SqlServerExpression { //data.Add(select.Where(a => a.Title.PadRight(10, 'a') == a.Title).ToList()); //data.Add(select.Where(a => a.Title.PadRight(10, 'a') == (a.Title + 1)).ToList()); //data.Add(select.Where(a => a.Title.PadRight(10, 'a') == a.Type.Name).ToList()); - ////SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - ////FROM `tb_topic` a - ////WHERE (rpad(a.`Title`, 10, 'a') = 'aaa'); - ////SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - ////FROM `tb_topic` a - ////WHERE (rpad(a.`Title`, 10, 'a') = a.`Title`); - - ////SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - ////FROM `tb_topic` a - ////WHERE (rpad(a.`Title`, 10, 'a') = concat(a.`Title`, 1)); - - ////SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a.`Title` as7, a.`CreateTime` as8 - ////FROM `tb_topic` a, `TestTypeInfo` a__Type - ////WHERE (rpad(a.`Title`, 10, 'a') = a__Type.`Name`); //data.Add(select.Where(a => (a.Title.PadRight(10, 'a') + "aaa").PadRight(20, 'b') == "aaa").ToList()); //data.Add(select.Where(a => (a.Title.PadRight(10, 'a') + "aaa").PadRight(20, 'b') == a.Title).ToList()); //data.Add(select.Where(a => (a.Title.PadRight(10, 'a') + "aaa").PadRight(20, 'b') == (a.Title + 1)).ToList()); //data.Add(select.Where(a => (a.Title.PadRight(10, 'a') + "aaa").PadRight(20, 'b') == a.Type.Name).ToList()); - ////SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - ////FROM `tb_topic` a - ////WHERE (rpad(concat(rpad(a.`Title`, 10, 'a'), 'aaa'), 20, 'b') = 'aaa'); - - ////SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - ////FROM `tb_topic` a - ////WHERE (rpad(concat(rpad(a.`Title`, 10, 'a'), 'aaa'), 20, 'b') = a.`Title`); - - ////SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - ////FROM `tb_topic` a - ////WHERE (rpad(concat(rpad(a.`Title`, 10, 'a'), 'aaa'), 20, 'b') = concat(a.`Title`, 1)); - - ////SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a.`Title` as7, a.`CreateTime` as8 - ////FROM `tb_topic` a, `TestTypeInfo` a__Type - ////WHERE (rpad(concat(rpad(a.`Title`, 10, 'a'), 'aaa'), 20, 'b') = a__Type.`Name`) } [Fact] public void Trim() { @@ -468,40 +175,11 @@ namespace FreeSql.Tests.SqlServerExpression { data.Add(select.Where(a => a.Title.Trim('a') == a.Title).ToList()); data.Add(select.Where(a => a.Title.Trim('a', 'b') == (a.Title + 1)).ToList()); data.Add(select.Where(a => a.Title.Trim('a', 'b', 'c') == a.Type.Name).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE (trim(a.`Title`) = 'aaa'); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE (trim('a' from a.`Title`) = a.`Title`); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE (trim('b' from trim('a' from a.`Title`)) = concat(a.`Title`, 1)); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a.`Title` as7, a.`CreateTime` as8 - //FROM `tb_topic` a, `TestTypeInfo` a__Type - //WHERE (trim('c' from trim('b' from trim('a' from a.`Title`))) = a__Type.`Name`); data.Add(select.Where(a => (a.Title.Trim() + "aaa").Trim() == "aaa").ToList()); data.Add(select.Where(a => (a.Title.Trim('a') + "aaa").Trim('a') == a.Title).ToList()); data.Add(select.Where(a => (a.Title.Trim('a', 'b') + "aaa").Trim('a', 'b') == (a.Title + 1)).ToList()); data.Add(select.Where(a => (a.Title.Trim('a', 'b', 'c') + "aaa").Trim('a', 'b', 'c') == a.Type.Name).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE (trim(concat(trim(a.`Title`), 'aaa')) = 'aaa'); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE (trim('a' from concat(trim('a' from a.`Title`), 'aaa')) = a.`Title`); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE (trim('b' from trim('a' from concat(trim('b' from trim('a' from a.`Title`)), 'aaa'))) = concat(a.`Title`, 1)); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a.`Title` as7, a.`CreateTime` as8 - //FROM `tb_topic` a, `TestTypeInfo` a__Type - //WHERE (trim('c' from trim('b' from trim('a' from concat(trim('c' from trim('b' from trim('a' from a.`Title`))), 'aaa')))) = a__Type.`Name`) } [Fact] public void TrimStart() { @@ -510,40 +188,11 @@ namespace FreeSql.Tests.SqlServerExpression { data.Add(select.Where(a => a.Title.TrimStart('a') == a.Title).ToList()); data.Add(select.Where(a => a.Title.TrimStart('a', 'b') == (a.Title + 1)).ToList()); data.Add(select.Where(a => a.Title.TrimStart('a', 'b', 'c') == a.Type.Name).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE (ltrim(a.`Title`) = 'aaa'); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE (trim(trailing 'a' from trim(leading 'a' from a.`Title`)) = a.`Title`); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE (trim(trailing 'b' from trim(leading 'b' from trim(trailing 'a' from trim(leading 'a' from a.`Title`)))) = concat(a.`Title`, 1)); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a.`Title` as7, a.`CreateTime` as8 - //FROM `tb_topic` a, `TestTypeInfo` a__Type - //WHERE (trim(trailing 'c' from trim(leading 'c' from trim(trailing 'b' from trim(leading 'b' from trim(trailing 'a' from trim(leading 'a' from a.`Title`)))))) = a__Type.`Name`); data.Add(select.Where(a => (a.Title.TrimStart() + "aaa").TrimStart() == "aaa").ToList()); data.Add(select.Where(a => (a.Title.TrimStart('a') + "aaa").TrimStart('a') == a.Title).ToList()); data.Add(select.Where(a => (a.Title.TrimStart('a', 'b') + "aaa").TrimStart('a', 'b') == (a.Title + 1)).ToList()); data.Add(select.Where(a => (a.Title.TrimStart('a', 'b', 'c') + "aaa").TrimStart('a', 'b', 'c') == a.Type.Name).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE (ltrim(concat(ltrim(a.`Title`), 'aaa')) = 'aaa'); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE (trim(trailing 'a' from trim(leading 'a' from concat(trim(trailing 'a' from trim(leading 'a' from a.`Title`)), 'aaa'))) = a.`Title`); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE (trim(trailing 'b' from trim(leading 'b' from trim(trailing 'a' from trim(leading 'a' from concat(trim(trailing 'b' from trim(leading 'b' from trim(trailing 'a' from trim(leading 'a' from a.`Title`)))), 'aaa'))))) = concat(a.`Title`, 1)); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a.`Title` as7, a.`CreateTime` as8 - //FROM `tb_topic` a, `TestTypeInfo` a__Type - //WHERE (trim(trailing 'c' from trim(leading 'c' from trim(trailing 'b' from trim(leading 'b' from trim(trailing 'a' from trim(leading 'a' from concat(trim(trailing 'c' from trim(leading 'c' from trim(trailing 'b' from trim(leading 'b' from trim(trailing 'a' from trim(leading 'a' from a.`Title`)))))), 'aaa'))))))) = a__Type.`Name`) } [Fact] public void TrimEnd() { @@ -552,40 +201,11 @@ namespace FreeSql.Tests.SqlServerExpression { data.Add(select.Where(a => a.Title.TrimEnd('a') == a.Title).ToList()); data.Add(select.Where(a => a.Title.TrimEnd('a', 'b') == (a.Title + 1)).ToList()); data.Add(select.Where(a => a.Title.TrimEnd('a', 'b', 'c') == a.Type.Name).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE (rtrim(a.`Title`) = 'aaa'); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE (trim(trailing 'a' from a.`Title`) = a.`Title`); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE (trim(trailing 'b' from trim(trailing 'a' from a.`Title`)) = concat(a.`Title`, 1)); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a.`Title` as7, a.`CreateTime` as8 - //FROM `tb_topic` a, `TestTypeInfo` a__Type - //WHERE (trim(trailing 'c' from trim(trailing 'b' from trim(trailing 'a' from a.`Title`))) = a__Type.`Name`); data.Add(select.Where(a => (a.Title.TrimEnd() + "aaa").TrimEnd() == "aaa").ToList()); data.Add(select.Where(a => (a.Title.TrimEnd('a') + "aaa").TrimEnd('a') == a.Title).ToList()); data.Add(select.Where(a => (a.Title.TrimEnd('a', 'b') + "aaa").TrimEnd('a', 'b') == (a.Title + 1)).ToList()); data.Add(select.Where(a => (a.Title.TrimEnd('a', 'b', 'c') + "aaa").TrimEnd('a', 'b', 'c') == a.Type.Name).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE (rtrim(concat(rtrim(a.`Title`), 'aaa')) = 'aaa'); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE (trim(trailing 'a' from concat(trim(trailing 'a' from a.`Title`), 'aaa')) = a.`Title`); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE (trim(trailing 'b' from trim(trailing 'a' from concat(trim(trailing 'b' from trim(trailing 'a' from a.`Title`)), 'aaa'))) = concat(a.`Title`, 1)); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a.`Title` as7, a.`CreateTime` as8 - //FROM `tb_topic` a, `TestTypeInfo` a__Type - //WHERE (trim(trailing 'c' from trim(trailing 'b' from trim(trailing 'a' from concat(trim(trailing 'c' from trim(trailing 'b' from trim(trailing 'a' from a.`Title`))), 'aaa')))) = a__Type.`Name`) } [Fact] public void Replace() { @@ -594,40 +214,11 @@ namespace FreeSql.Tests.SqlServerExpression { data.Add(select.Where(a => a.Title.Replace("a", "b").Replace("b", "c") == a.Title).ToList()); data.Add(select.Where(a => a.Title.Replace("a", "b").Replace("b", "c").Replace("c", "a") == (a.Title + 1)).ToList()); data.Add(select.Where(a => a.Title.Replace("a", "b").Replace("b", "c").Replace(a.Type.Name, "a") == a.Type.Name).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE (replace(a.`Title`, 'a', 'b') = 'aaa'); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE (replace(replace(a.`Title`, 'a', 'b'), 'b', 'c') = a.`Title`); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE (replace(replace(replace(a.`Title`, 'a', 'b'), 'b', 'c'), 'c', 'a') = concat(a.`Title`, 1)); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a.`Title` as7, a.`CreateTime` as8 - //FROM `tb_topic` a, `TestTypeInfo` a__Type - //WHERE (replace(replace(replace(a.`Title`, 'a', 'b'), 'b', 'c'), a__Type.`Name`, 'a') = a__Type.`Name`); data.Add(select.Where(a => (a.Title.Replace("a", "b") + "aaa").TrimEnd() == "aaa").ToList()); data.Add(select.Where(a => (a.Title.Replace("a", "b").Replace("b", "c") + "aaa").TrimEnd('a') == a.Title).ToList()); data.Add(select.Where(a => (a.Title.Replace("a", "b").Replace("b", "c").Replace("c", "a") + "aaa").TrimEnd('a', 'b') == (a.Title + 1)).ToList()); data.Add(select.Where(a => (a.Title.Replace("a", "b").Replace("b", "c").Replace(a.Type.Name, "a") + "aaa").TrimEnd('a', 'b', 'c') == a.Type.Name).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE (concat(replace(a.`Title`, 'a', 'b'), 'aaa') = 'aaa'); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE (concat(replace(replace(a.`Title`, 'a', 'b'), 'b', 'c'), 'aaa') = a.`Title`); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE (concat(replace(replace(replace(a.`Title`, 'a', 'b'), 'b', 'c'), 'c', 'a'), 'aaa') = concat(a.`Title`, 1)); - - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a.`Title` as7, a.`CreateTime` as8 - //FROM `tb_topic` a, `TestTypeInfo` a__Type - //WHERE (concat(replace(replace(replace(a.`Title`, 'a', 'b'), 'b', 'c'), a__Type.`Name`, 'a'), 'aaa') = a__Type.`Name`) } [Fact] public void CompareTo() { @@ -636,40 +227,11 @@ namespace FreeSql.Tests.SqlServerExpression { //data.Add(select.Where(a => a.Title.CompareTo(a.Title) > 0).ToList()); //data.Add(select.Where(a => a.Title.CompareTo(a.Title + 1) == 0).ToList()); //data.Add(select.Where(a => a.Title.CompareTo(a.Title + a.Type.Name) == 0).ToList()); - ////SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - ////FROM `tb_topic` a - ////WHERE (strcmp(a.`Title`, a.`Title`) = 0); - - ////SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - ////FROM `tb_topic` a - ////WHERE (strcmp(a.`Title`, a.`Title`) > 0); - - ////SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - ////FROM `tb_topic` a - ////WHERE (strcmp(a.`Title`, concat(a.`Title`, 1)) = 0); - - ////SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a.`Title` as7, a.`CreateTime` as8 - ////FROM `tb_topic` a, `TestTypeInfo` a__Type - ////WHERE (strcmp(a.`Title`, concat(a.`Title`, a__Type.`Name`)) = 0); + //data.Add(select.Where(a => (a.Title + "aaa").CompareTo("aaa") == 0).ToList()); //data.Add(select.Where(a => (a.Title + "aaa").CompareTo(a.Title) > 0).ToList()); //data.Add(select.Where(a => (a.Title + "aaa").CompareTo(a.Title + 1) == 0).ToList()); //data.Add(select.Where(a => (a.Title + "aaa").CompareTo(a.Type.Name) == 0).ToList()); - ////SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - ////FROM `tb_topic` a - ////WHERE (strcmp(concat(a.`Title`, 'aaa'), 'aaa') = 0); - - ////SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - ////FROM `tb_topic` a - ////WHERE (strcmp(concat(a.`Title`, 'aaa'), a.`Title`) > 0); - - ////SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - ////FROM `tb_topic` a - ////WHERE (strcmp(concat(a.`Title`, 'aaa'), concat(a.`Title`, 1)) = 0); - - ////SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a.`Title` as7, a.`CreateTime` as8 - ////FROM `tb_topic` a, `TestTypeInfo` a__Type - ////WHERE (strcmp(concat(a.`Title`, 'aaa'), a__Type.`Name`) = 0) } } } diff --git a/FreeSql.Tests/SqlServer/SqlServerExpression/TimeSpanTest.cs b/FreeSql.Tests/SqlServer/SqlServerExpression/TimeSpanTest.cs index 6c677d67..ac99e48b 100644 --- a/FreeSql.Tests/SqlServer/SqlServerExpression/TimeSpanTest.cs +++ b/FreeSql.Tests/SqlServer/SqlServerExpression/TimeSpanTest.cs @@ -35,226 +35,142 @@ namespace FreeSql.Tests.SqlServerExpression { public void Zero() { var data = new List(); data.Add(select.Where(a => a.CreateTime.TimeOfDay > TimeSpan.Zero).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE (timestampdiff(microsecond, date_format(a.`CreateTime`, '1970-1-1 %H:%i:%s.%f'), a.`CreateTime`) + 62135596800000000) > 0) } [Fact] public void MinValue() { var data = new List(); data.Add(select.Where(a => a.CreateTime.TimeOfDay > TimeSpan.MinValue).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE (timestampdiff(microsecond, date_format(a.`CreateTime`, '1970-1-1 %H:%i:%s.%f'), a.`CreateTime`) + 62135596800000000) > -922337203685477580) } [Fact] public void MaxValue() { var data = new List(); data.Add(select.Where(a => a.CreateTime.TimeOfDay < TimeSpan.MaxValue).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE (timestampdiff(microsecond, date_format(a.`CreateTime`, '1970-1-1 %H:%i:%s.%f'), a.`CreateTime`) + 62135596800000000) < 922337203685477580) } [Fact] public void Days() { var data = new List(); data.Add(select.Where(a => a.CreateTime.TimeOfDay.Days == 0).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE (((timestampdiff(microsecond, date_format(a.`CreateTime`, '1970-1-1 %H:%i:%s.%f'), a.`CreateTime`) + 62135596800000000)) div 86400000000) = 0) } [Fact] public void Hours() { var data = new List(); data.Add(select.Where(a => a.CreateTime.TimeOfDay.Hours > 0).ToSql()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE (((timestampdiff(microsecond, date_format(a.`CreateTime`, '1970-1-1 %H:%i:%s.%f'), a.`CreateTime`) + 62135596800000000)) div 3600000000) mod 24 > 0) } [Fact] public void Milliseconds() { var data = new List(); data.Add(select.Where(a => a.CreateTime.TimeOfDay.Milliseconds > 0).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE (((timestampdiff(microsecond, date_format(a.`CreateTime`, '1970-1-1 %H:%i:%s.%f'), a.`CreateTime`) + 62135596800000000)) div 1000 mod 1000) > 0) } [Fact] public void Minutes() { var data = new List(); data.Add(select.Where(a => a.CreateTime.TimeOfDay.Minutes > 0).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE (((timestampdiff(microsecond, date_format(a.`CreateTime`, '1970-1-1 %H:%i:%s.%f'), a.`CreateTime`) + 62135596800000000)) div 60000000 mod 60) > 0) } [Fact] public void Seconds() { var data = new List(); data.Add(select.Where(a => a.CreateTime.TimeOfDay.Seconds > 0).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE (((timestampdiff(microsecond, date_format(a.`CreateTime`, '1970-1-1 %H:%i:%s.%f'), a.`CreateTime`) + 62135596800000000)) div 1000000 mod 60) > 0) } [Fact] public void Ticks() { var data = new List(); data.Add(select.Where(a => a.CreateTime.TimeOfDay.Ticks > 0).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE (((timestampdiff(microsecond, date_format(a.`CreateTime`, '1970-1-1 %H:%i:%s.%f'), a.`CreateTime`) + 62135596800000000)) * 10) > 0) } [Fact] public void TotalDays() { var data = new List(); data.Add(select.Where(a => a.CreateTime.TimeOfDay.TotalDays > 0).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE (((timestampdiff(microsecond, date_format(a.`CreateTime`, '1970-1-1 %H:%i:%s.%f'), a.`CreateTime`) + 62135596800000000)) / 86400000000) > 0) } [Fact] public void TotalHours() { var data = new List(); data.Add(select.Where(a => a.CreateTime.TimeOfDay.TotalHours > 0).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE (((timestampdiff(microsecond, date_format(a.`CreateTime`, '1970-1-1 %H:%i:%s.%f'), a.`CreateTime`) + 62135596800000000)) / 3600000000) > 0) } [Fact] public void TotalMilliseconds() { var data = new List(); data.Add(select.Where(a => a.CreateTime.TimeOfDay.TotalMilliseconds > 0).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE (((timestampdiff(microsecond, date_format(a.`CreateTime`, '1970-1-1 %H:%i:%s.%f'), a.`CreateTime`) + 62135596800000000)) / 1000) > 0) } [Fact] public void TotalMinutes() { var data = new List(); data.Add(select.Where(a => a.CreateTime.TimeOfDay.TotalMinutes > 0).ToSql()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE (((timestampdiff(microsecond, date_format(a.`CreateTime`, '1970-1-1 %H:%i:%s.%f'), a.`CreateTime`) + 62135596800000000)) / 60000000) > 0) } [Fact] public void TotalSeconds() { var data = new List(); data.Add(select.Where(a => a.CreateTime.TimeOfDay.TotalSeconds > 0).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE (((timestampdiff(microsecond, date_format(a.`CreateTime`, '1970-1-1 %H:%i:%s.%f'), a.`CreateTime`) + 62135596800000000)) / 1000000) > 0) } [Fact] public void Add() { var data = new List(); data.Add(select.Where(a => a.CreateTime.TimeOfDay.Add(TimeSpan.FromDays(1)) > TimeSpan.Zero).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE ((timestampdiff(microsecond, date_format(a.`CreateTime`, '1970-1-1 %H:%i:%s.%f'), a.`CreateTime`) + 62135596800000000) + (1 * 86400000000)) > 0) } [Fact] public void Subtract() { var data = new List(); data.Add(select.Where(a => a.CreateTime.TimeOfDay.Subtract(TimeSpan.FromDays(1)) > TimeSpan.Zero).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE ((timestampdiff(microsecond, date_format(a.`CreateTime`, '1970-1-1 %H:%i:%s.%f'), a.`CreateTime`) + 62135596800000000) - (1 * 86400000000)) > 0) } [Fact] public void CompareTo() { var data = new List(); data.Add(select.Where(a => a.CreateTime.TimeOfDay.CompareTo(TimeSpan.FromDays(1)) > 0).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE (((timestampdiff(microsecond, date_format(a.`CreateTime`, '1970-1-1 %H:%i:%s.%f'), a.`CreateTime`) + 62135596800000000)) - ((1 * 86400000000))) > 0) } [Fact] public void this_Equals() { var data = new List(); data.Add(select.Where(a => a.CreateTime.TimeOfDay.Equals(TimeSpan.FromDays(1))).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE ((timestampdiff(microsecond, date_format(a.`CreateTime`, '1970-1-1 %H:%i:%s.%f'), a.`CreateTime`) + 62135596800000000) = (1 * 86400000000))) } [Fact] public void this_ToString() { var data = new List(); data.Add(select.Where(a => a.CreateTime.TimeOfDay.ToString() == "ssss").ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE (date_format(date_add(cast('0001/1/1 0:00:00' as datetime), interval (timestampdiff(microsecond, date_format(a.`CreateTime`, '1970-1-1 %H:%i:%s.%f'), a.`CreateTime`) + 62135596800000000)) microsecond), '%Y-%m-%d %H:%i:%s.%f') = 'ssss') } [Fact] public void TimeSpan_Compare() { var data = new List(); data.Add(select.Where(a => TimeSpan.Compare(a.CreateTime.TimeOfDay, TimeSpan.FromDays(1)) > 0).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE (((timestampdiff(microsecond, date_format(a.`CreateTime`, '1970-1-1 %H:%i:%s.%f'), a.`CreateTime`) + 62135596800000000)) - ((1 * 86400000000))) > 0) } [Fact] public void TimeSpan_Equals() { var data = new List(); data.Add(select.Where(a => TimeSpan.Equals(a.CreateTime.TimeOfDay, TimeSpan.FromDays(1))).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE ((timestampdiff(microsecond, date_format(a.`CreateTime`, '1970-1-1 %H:%i:%s.%f'), a.`CreateTime`) + 62135596800000000) = (1 * 86400000000))) } [Fact] public void TimeSpan_FromDays() { var data = new List(); data.Add(select.Where(a => TimeSpan.Equals(a.CreateTime.TimeOfDay, TimeSpan.FromDays(1))).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE ((timestampdiff(microsecond, date_format(a.`CreateTime`, '1970-1-1 %H:%i:%s.%f'), a.`CreateTime`) + 62135596800000000) = (1 * 86400000000))) } [Fact] public void TimeSpan_FromHours() { var data = new List(); data.Add(select.Where(a => TimeSpan.Equals(a.CreateTime.TimeOfDay, TimeSpan.FromHours(1))).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE ((timestampdiff(microsecond, date_format(a.`CreateTime`, '1970-1-1 %H:%i:%s.%f'), a.`CreateTime`) + 62135596800000000) = (1 * 3600000000))) } [Fact] public void TimeSpan_FromMilliseconds() { var data = new List(); data.Add(select.Where(a => TimeSpan.Equals(a.CreateTime.TimeOfDay, TimeSpan.FromMilliseconds(1))).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE ((timestampdiff(microsecond, date_format(a.`CreateTime`, '1970-1-1 %H:%i:%s.%f'), a.`CreateTime`) + 62135596800000000) = (1 * 1000))) } [Fact] public void TimeSpan_FromMinutes() { var data = new List(); data.Add(select.Where(a => TimeSpan.Equals(a.CreateTime.TimeOfDay, TimeSpan.FromMinutes(1))).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE ((timestampdiff(microsecond, date_format(a.`CreateTime`, '1970-1-1 %H:%i:%s.%f'), a.`CreateTime`) + 62135596800000000) = (1 * 60000000))) } [Fact] public void TimeSpan_FromSeconds() { var data = new List(); data.Add(select.Where(a => TimeSpan.Equals(a.CreateTime.TimeOfDay, TimeSpan.FromSeconds(1))).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE ((timestampdiff(microsecond, date_format(a.`CreateTime`, '1970-1-1 %H:%i:%s.%f'), a.`CreateTime`) + 62135596800000000) = (1 * 1000000))) } [Fact] public void TimeSpan_FromTicks() { var data = new List(); data.Add(select.Where(a => TimeSpan.Equals(a.CreateTime.TimeOfDay, TimeSpan.FromTicks(1))).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE ((timestampdiff(microsecond, date_format(a.`CreateTime`, '1970-1-1 %H:%i:%s.%f'), a.`CreateTime`) + 62135596800000000) = (1 / 10))) } [Fact] public void TimeSpan_Parse() { var data = new List(); data.Add(select.Where(a => TimeSpan.Parse(a.CreateTime.TimeOfDay.ToString()) > TimeSpan.Zero).ToList()); - //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 - //FROM `tb_topic` a - //WHERE (cast(date_format(date_add(cast('0001/1/1 0:00:00' as datetime), interval (timestampdiff(microsecond, date_format(a.`CreateTime`, '1970-1-1 %H:%i:%s.%f'), a.`CreateTime`) + 62135596800000000)) microsecond), '%Y-%m-%d %H:%i:%s.%f') as signed) > 0) } } } diff --git a/FreeSql.Tests/UnitTest1.cs b/FreeSql.Tests/UnitTest1.cs index c60368d0..feba1bca 100644 --- a/FreeSql.Tests/UnitTest1.cs +++ b/FreeSql.Tests/UnitTest1.cs @@ -33,7 +33,7 @@ namespace FreeSql.Tests { var sql4 = select.From((s, b, c) => s .InnerJoin(a => a.TypeGuid == b.Guid) .LeftJoin(a => c.Id == b.ParentId) - .Where(a => b.Name == "xxx")); + .Where(a => b.Name == "xxx")).ToSql(); //.Where(a => a.Id == 1).ToSql(); diff --git a/FreeSql/DataAnnotations/ColumnAttribute.cs b/FreeSql/DataAnnotations/ColumnAttribute.cs index 0fcabff9..15750ffd 100644 --- a/FreeSql/DataAnnotations/ColumnAttribute.cs +++ b/FreeSql/DataAnnotations/ColumnAttribute.cs @@ -28,5 +28,10 @@ namespace FreeSql.DataAnnotations { /// 是否可DBNull /// public bool IsNullable { get; set; } + + /// + /// 数据库默认值 + /// + internal object DbDefautValue { get; set; } } } diff --git a/FreeSql/Internal/CommonExpression.cs b/FreeSql/Internal/CommonExpression.cs index 8a4f6a64..7dc30e12 100644 --- a/FreeSql/Internal/CommonExpression.cs +++ b/FreeSql/Internal/CommonExpression.cs @@ -17,21 +17,31 @@ namespace FreeSql.Internal { switch (exp.NodeType) { case ExpressionType.Quote: return ReadAnonymousField(_tables, field, parent, ref index, (exp as UnaryExpression)?.Operand); case ExpressionType.Lambda: return ReadAnonymousField(_tables, field, parent, ref index, (exp as LambdaExpression)?.Body); + case ExpressionType.Negate: + case ExpressionType.NegateChecked: + field.Append(", ").Append(ExpressionLambdaToSql(exp, _tables, null, SelectTableInfoType.From, true)).Append(" as").Append(++index); + return false; case ExpressionType.Convert: return ReadAnonymousField(_tables, field, parent, ref index, (exp as UnaryExpression)?.Operand); case ExpressionType.Constant: var constExp = exp as ConstantExpression; field.Append(", ").Append(constExp?.Value).Append(" as").Append(++index); return false; + case ExpressionType.Call: + field.Append(", ").Append(ExpressionLambdaToSql(exp, _tables, null, SelectTableInfoType.From, true)).Append(" as").Append(++index); + return false; case ExpressionType.MemberAccess: - var map = new List(); - ExpressionSelectColumn_MemberAccess(_tables, map, SelectTableInfoType.From, exp, true); - if (map.Count > 1) { + if (_common.GetTableByEntity(exp.Type) != null) { //加载表所有字段 + var map = new List(); + ExpressionSelectColumn_MemberAccess(_tables, map, SelectTableInfoType.From, exp, true); parent.Consturctor = map.First().Table.Table.Type.GetConstructor(new Type[0]); parent.ConsturctorType = ReadAnonymousTypeInfoConsturctorType.Properties; - } - for (var idx = 0; idx < map.Count; idx++) { - field.Append(", ").Append(map[idx].Table.Alias).Append(".").Append(_common.QuoteSqlName(map[idx].Column.Attribute.Name)).Append(" as").Append(++index); - if (map.Count > 1) parent.Childs.Add(new ReadAnonymousTypeInfo { CsName = map[idx].Column.CsName }); + for (var idx = 0; idx < map.Count; idx++) { + field.Append(", ").Append(map[idx].Table.Alias).Append(".").Append(_common.QuoteSqlName(map[idx].Column.Attribute.Name)).Append(" as").Append(++index); + parent.Childs.Add(new ReadAnonymousTypeInfo { CsName = map[idx].Column.CsName }); + } + } else { + field.Append(", ").Append(ExpressionLambdaToSql(exp, _tables, null, SelectTableInfoType.From, true)).Append(" as").Append(++index); + return false; } return false; case ExpressionType.New: @@ -39,7 +49,7 @@ namespace FreeSql.Internal { parent.Consturctor = newExp.Type.GetConstructors()[0]; parent.ConsturctorType = ReadAnonymousTypeInfoConsturctorType.Arguments; for (var a = 0; a < newExp.Members.Count; a++) { - var child = new ReadAnonymousTypeInfo { CsName = newExp.Members[a].Name }; + var child = new ReadAnonymousTypeInfo { CsName = newExp.Members[a].Name, CsType = newExp.Arguments[a].Type }; parent.Childs.Add(child); ReadAnonymousField(_tables, field, child, ref index, newExp.Arguments[a]); } @@ -53,7 +63,7 @@ namespace FreeSql.Internal { case ReadAnonymousTypeInfoConsturctorType.Arguments: var args = new object[parent.Childs.Count]; for (var a = 0; a < parent.Childs.Count; a++) { - args[a] = ReadAnonymous(parent.Childs[a], dr, ref index); + args[a] = Utils.GetDataReaderValue(parent.Childs[a].CsType, ReadAnonymous(parent.Childs[a], dr, ref index)); } return parent.Consturctor.Invoke(args); case ReadAnonymousTypeInfoConsturctorType.Properties: @@ -162,6 +172,9 @@ namespace FreeSql.Internal { case ExpressionType.Negate: case ExpressionType.NegateChecked: return "-" + ExpressionLambdaToSql((exp as UnaryExpression)?.Operand, _tables, _selectColumnMap, tbtype, isQuoteName); case ExpressionType.Constant: return _common.FormatSql("{0}", (exp as ConstantExpression)?.Value); + case ExpressionType.Conditional: + var condExp = exp as ConditionalExpression; + return $"case when {ExpressionLambdaToSql(condExp.Test, _tables, _selectColumnMap, tbtype, isQuoteName)} then {ExpressionLambdaToSql(condExp.IfTrue, _tables, _selectColumnMap, tbtype, isQuoteName)} else {ExpressionLambdaToSql(condExp.IfFalse, _tables, _selectColumnMap, tbtype, isQuoteName)} end"; case ExpressionType.Call: var exp3 = exp as MethodCallExpression; switch (exp3.Object?.Type.FullName ?? exp3.Method.DeclaringType.FullName) { @@ -169,6 +182,7 @@ namespace FreeSql.Internal { case "System.Math": return ExpressionLambdaToSqlCallMath(exp3, _tables, _selectColumnMap, tbtype, isQuoteName); case "System.DateTime": return ExpressionLambdaToSqlCallDateTime(exp3, _tables, _selectColumnMap, tbtype, isQuoteName); case "System.TimeSpan": return ExpressionLambdaToSqlCallTimeSpan(exp3, _tables, _selectColumnMap, tbtype, isQuoteName); + case "System.Convert": return ExpressionLambdaToSqlCallConvert(exp3, _tables, _selectColumnMap, tbtype, isQuoteName); } throw new Exception($"MySqlExpression 未现实函数表达式 {exp3} 解析"); case ExpressionType.MemberAccess: @@ -304,5 +318,6 @@ namespace FreeSql.Internal { internal abstract string ExpressionLambdaToSqlCallMath(MethodCallExpression exp, List _tables, List _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName); internal abstract string ExpressionLambdaToSqlCallDateTime(MethodCallExpression exp, List _tables, List _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName); internal abstract string ExpressionLambdaToSqlCallTimeSpan(MethodCallExpression exp, List _tables, List _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName); + internal abstract string ExpressionLambdaToSqlCallConvert(MethodCallExpression exp, List _tables, List _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName); } } diff --git a/FreeSql/Internal/CommonProvider/AdoProvider/AdoProvider.cs b/FreeSql/Internal/CommonProvider/AdoProvider/AdoProvider.cs index 4145badf..67ae259e 100644 --- a/FreeSql/Internal/CommonProvider/AdoProvider/AdoProvider.cs +++ b/FreeSql/Internal/CommonProvider/AdoProvider/AdoProvider.cs @@ -248,7 +248,7 @@ namespace FreeSql.Internal.CommonProvider { } var tran = TransactionCurrentThread; - if (IsTracePerformance) logtxt += $" PrepareCommand_part1: {DateTime.Now.Subtract(dt).TotalMilliseconds}ms cmdParms: {cmdParms.Length}\r\n"; + if (IsTracePerformance) logtxt += $" PrepareCommand_part1: {DateTime.Now.Subtract(dt).TotalMilliseconds}ms cmdParms: {cmd.Parameters.Count}\r\n"; if (tran != null) { if (IsTracePerformance) dt = DateTime.Now; diff --git a/FreeSql/Internal/CommonProvider/AdoProvider/AdoProviderAsync.cs b/FreeSql/Internal/CommonProvider/AdoProvider/AdoProviderAsync.cs index ed08861d..789b81c6 100644 --- a/FreeSql/Internal/CommonProvider/AdoProvider/AdoProviderAsync.cs +++ b/FreeSql/Internal/CommonProvider/AdoProvider/AdoProviderAsync.cs @@ -207,7 +207,7 @@ namespace FreeSql.Internal.CommonProvider { } } - if (IsTracePerformance) logtxt += $" PrepareCommand_tran==null: {DateTime.Now.Subtract(dt).TotalMilliseconds}ms\r\n"; + if (IsTracePerformance) logtxt += $" PrepareCommand_tran==null: {DateTime.Now.Subtract(dt).TotalMilliseconds}ms cmdParms: {cmd.Parameters.Count}\r\n"; return cmd; } diff --git a/FreeSql/Internal/CommonProvider/InsertProvider.cs b/FreeSql/Internal/CommonProvider/InsertProvider.cs index 0cab0b54..086eec0d 100644 --- a/FreeSql/Internal/CommonProvider/InsertProvider.cs +++ b/FreeSql/Internal/CommonProvider/InsertProvider.cs @@ -65,7 +65,6 @@ namespace FreeSql.Internal.CommonProvider { sb.Append(_commonUtils.QuoteSqlName(col.Attribute.Name)); ++colidx; } - if (colidx == 0) return null; sb.Append(") VALUES"); _params = new DbParameter[colidx * _source.Count]; var didx = 0; diff --git a/FreeSql/Internal/CommonProvider/SelectProvider/Select0Provider.cs b/FreeSql/Internal/CommonProvider/SelectProvider/Select0Provider.cs index 7963f4a8..fbab032e 100644 --- a/FreeSql/Internal/CommonProvider/SelectProvider/Select0Provider.cs +++ b/FreeSql/Internal/CommonProvider/SelectProvider/Select0Provider.cs @@ -178,6 +178,7 @@ namespace FreeSql.Internal.CommonProvider { var type = typeof(T1); var map = new ReadAnonymousTypeInfo { Consturctor = type.GetConstructor(new Type[0]), ConsturctorType = ReadAnonymousTypeInfoConsturctorType.Properties }; var field = new StringBuilder(); + var dicfield = new Dictionary(); var tb = _tables.First(); var index = 0; var ps = typeof(T1).GetProperties(); @@ -185,7 +186,11 @@ namespace FreeSql.Internal.CommonProvider { var child = new ReadAnonymousTypeInfo { CsName = p.Name }; if (tb.Table.ColumnsByCs.TryGetValue(p.Name, out var col)) { //普通字段 if (index > 0) field.Append(", "); - field.Append(tb.Alias).Append(".").Append(_commonUtils.QuoteSqlName(col.Attribute.Name)).Append(" as").Append(++index); + var quoteName = _commonUtils.QuoteSqlName(col.Attribute.Name); + field.Append(tb.Alias).Append(".").Append(quoteName); + ++index; + if (dicfield.ContainsKey(quoteName)) field.Append(" as").Append(index); + else dicfield.Add(quoteName, true); } else { var tb2 = _tables.Where(a => a.Table.Type == p.PropertyType && a.Alias.Contains(p.Name)).FirstOrDefault(); if (tb2 == null && ps.Where(pw => pw.PropertyType == p.PropertyType).Count() == 1) tb2 = _tables.Where(a => a.Table.Type == p.PropertyType).FirstOrDefault(); @@ -194,7 +199,11 @@ namespace FreeSql.Internal.CommonProvider { child.ConsturctorType = ReadAnonymousTypeInfoConsturctorType.Properties; foreach (var col2 in tb2.Table.Columns.Values) { if (index > 0) field.Append(", "); - field.Append(tb2.Alias).Append(".").Append(_commonUtils.QuoteSqlName(col2.Attribute.Name)).Append(" as").Append(++index); + var quoteName = _commonUtils.QuoteSqlName(col2.Attribute.Name); + field.Append(tb2.Alias).Append(".").Append(quoteName); + ++index; + if (dicfield.ContainsKey(quoteName)) field.Append(" as").Append(index); + else dicfield.Add(quoteName, true); child.Childs.Add(new ReadAnonymousTypeInfo { CsName = col2.CsName }); } } diff --git a/FreeSql/Internal/CommonProvider/SelectProvider/Select1Provider.cs b/FreeSql/Internal/CommonProvider/SelectProvider/Select1Provider.cs index acac4d7b..c6e5de93 100644 --- a/FreeSql/Internal/CommonProvider/SelectProvider/Select1Provider.cs +++ b/FreeSql/Internal/CommonProvider/SelectProvider/Select1Provider.cs @@ -47,9 +47,8 @@ namespace FreeSql.Internal.CommonProvider { default: throw new NotImplementedException($"未现实 {expCall.Method.Name}"); } } - return this; } - throw new NotImplementedException($"未现实 {exp}"); + return this; } public ISelect As(string alias) { diff --git a/FreeSql/Internal/CommonProvider/UpdateProvider.cs b/FreeSql/Internal/CommonProvider/UpdateProvider.cs index 7d6e8768..601a03d7 100644 --- a/FreeSql/Internal/CommonProvider/UpdateProvider.cs +++ b/FreeSql/Internal/CommonProvider/UpdateProvider.cs @@ -66,13 +66,10 @@ namespace FreeSql.Internal.CommonProvider { if (cols.Any() == false) return this; foreach (var col in cols) { if (col.Column.Attribute.IsNullable) { - var repltype = col.Column.CsType; - if (repltype.FullName.StartsWith("System.Nullable`1[[System.")) repltype = repltype.GenericTypeArguments[0]; - var replval = Activator.CreateInstance(repltype); + var replval = col.Column.Attribute.DbDefautValue; if (replval == null) continue; var replname = _commonUtils.QuoteSqlName(col.Column.Attribute.Name); - replval = _commonUtils.FormatSql("{0}", replval); - expt = expt.Replace(replname, _commonUtils.IsNull(replname, replval)); + expt = expt.Replace(replname, _commonUtils.IsNull(replname, _commonUtils.FormatSql("{0}", replval))); } } _set.Append(", ").Append(_commonUtils.QuoteSqlName(cols.First().Column.Attribute.Name)).Append(" = ").Append(expt); diff --git a/FreeSql/Internal/Model/ReadAnonymousTypeInfo.cs b/FreeSql/Internal/Model/ReadAnonymousTypeInfo.cs index 34972f82..53ed0894 100644 --- a/FreeSql/Internal/Model/ReadAnonymousTypeInfo.cs +++ b/FreeSql/Internal/Model/ReadAnonymousTypeInfo.cs @@ -6,6 +6,7 @@ using System.Text; namespace FreeSql.Internal.Model { class ReadAnonymousTypeInfo { public string CsName { get; set; } + public Type CsType { get; set; } public ConstructorInfo Consturctor { get; set; } public ReadAnonymousTypeInfoConsturctorType ConsturctorType { get; set; } public List Childs = new List(); diff --git a/FreeSql/Internal/Model/TableInfo.cs b/FreeSql/Internal/Model/TableInfo.cs index 4c597512..3352d77b 100644 --- a/FreeSql/Internal/Model/TableInfo.cs +++ b/FreeSql/Internal/Model/TableInfo.cs @@ -13,6 +13,5 @@ namespace FreeSql.Internal.Model { public string DbName { get; set; } public string DbOldName { get; set; } public string SelectFilter { get; set; } - public List> Uniques { get; set; } = new List>(); } } \ No newline at end of file diff --git a/FreeSql/Internal/Utils.cs b/FreeSql/Internal/Utils.cs index e8fb0fa7..c0f1d6f4 100644 --- a/FreeSql/Internal/Utils.cs +++ b/FreeSql/Internal/Utils.cs @@ -35,12 +35,22 @@ namespace FreeSql.Internal { Name = p.Name, DbType = tp.Value.dbtypeFull, IsIdentity = false, - IsNullable = tp.Value.isnullable ?? false, + IsNullable = tp.Value.isnullable ?? true, IsPrimary = false, }; + if (string.IsNullOrEmpty(colattr.DbType) == false) colattr.DbType = colattr.DbType.ToUpper(); + if (tp != null && tp.Value.isnullable == null) colattr.IsNullable = tp.Value.dbtypeFull.Contains("NOT NULL") == false; + if (string.IsNullOrEmpty(colattr.DbType) == false) colattr.IsNullable = colattr.DbType.Contains("NOT NULL") == false; if (string.IsNullOrEmpty(colattr.Name)) colattr.Name = p.Name; if (string.IsNullOrEmpty(colattr.DbType)) colattr.DbType = tp?.dbtypeFull ?? "varchar(255)"; - if (colattr.DbType.IndexOf("NOT NULL") == -1 && tp?.isnullable == false) colattr.DbType += " NOT NULL"; + if ((colattr.IsNullable == false || colattr.IsIdentity || colattr.IsPrimary) && colattr.DbType.Contains("NOT NULL") == false) colattr.DbType += " NOT NULL"; + if (colattr.IsNullable == true && colattr.DbType.Contains("NOT NULL")) colattr.DbType = colattr.DbType.Replace("NOT NULL", ""); + colattr.DbType = Regex.Replace(colattr.DbType, @"\([^\)]+\)", m => Regex.Replace(m.Groups[0].Value, @"\s", "")); + colattr.DbDefautValue = trytb.Properties[p.Name].GetValue(Activator.CreateInstance(trytb.Type)); + if (colattr.DbDefautValue == null && p.PropertyType.FullName == "System.String") colattr.DbDefautValue = string.Empty; + if (colattr.DbDefautValue == null) colattr.DbDefautValue = Activator.CreateInstance(p.PropertyType.GenericTypeArguments.FirstOrDefault() ?? p.PropertyType); + if (colattr.DbDefautValue == null) colattr.DbDefautValue = ""; + if (colattr.DbDefautValue.GetType().FullName == "System.DateTime") colattr.DbDefautValue = new DateTime(1970, 1, 1); var col = new ColumnInfo { Table = trytb, @@ -52,6 +62,7 @@ namespace FreeSql.Internal { trytb.ColumnsByCs.Add(p.Name, col); } trytb.Primarys = trytb.Columns.Values.Where(a => a.Attribute.IsPrimary).ToArray(); + if (trytb.Primarys.Any() == false) trytb.Primarys = trytb.Columns.Values.Where(a => a.Attribute.IsIdentity).ToArray(); _cacheGetTableByEntity.TryAdd(entity.FullName, trytb); return trytb; } @@ -122,22 +133,14 @@ namespace FreeSql.Internal { if (prop == null) throw new Exception(string.Concat(type.FullName, " 没有定义属性 ", members[a])); if (a < members.Length - 1) current = prop.GetValue(current); } - if (value == null || value == DBNull.Value) { - prop.SetValue(current, null, null); - return; - } - var propType = prop.PropertyType; - if (propType.FullName.StartsWith("System.Nullable`1[")) propType = propType.GenericTypeArguments.First(); - if (propType.IsEnum) { - var valueStr = string.Concat(value); - if (string.IsNullOrEmpty(valueStr) == false) prop.SetValue(current, Enum.Parse(propType, valueStr), null); - return; - } - if (propType != value.GetType()) { - prop.SetValue(current, Convert.ChangeType(value, propType), null); - return; - } - prop.SetValue(current, value, null); + prop.SetValue(current, GetDataReaderValue(prop.PropertyType, value), null); + } + internal static object GetDataReaderValue(Type type, object value) { + if (value == null || value == DBNull.Value) return null; + if (type.FullName.StartsWith("System.Nullable`1[")) type = type.GenericTypeArguments.First(); + if (type.IsEnum) return Enum.Parse(type, string.Concat(value)); + if (type != value.GetType()) return Convert.ChangeType(value, type); + return value; } internal static string GetCsName(string name) { name = Regex.Replace(name.TrimStart('@'), @"[^\w]", "_"); diff --git a/FreeSql/MySql/MySqlCodeFirst.cs b/FreeSql/MySql/MySqlCodeFirst.cs index de9bc186..8c36f480 100644 --- a/FreeSql/MySql/MySqlCodeFirst.cs +++ b/FreeSql/MySql/MySqlCodeFirst.cs @@ -106,7 +106,7 @@ namespace FreeSql.MySql { sb.Append("CREATE TABLE IF NOT EXISTS ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" ("); foreach (var tbcol in tb.Columns.Values) { sb.Append(" \r\n ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(" "); - sb.Append(tbcol.Attribute.DbType.ToUpper()); + sb.Append(tbcol.Attribute.DbType); if (tbcol.Attribute.IsIdentity && tbcol.Attribute.DbType.IndexOf("AUTO_INCREMENT", StringComparison.CurrentCultureIgnoreCase) == -1) sb.Append(" AUTO_INCREMENT"); sb.Append(","); } @@ -143,10 +143,10 @@ where a.table_schema in ({0}) and a.table_name in ({1})".FormatMySql(isRenameTab if (addcols.TryGetValue(column, out var trycol)) { if ((trycol.Attribute.DbType.IndexOf(" unsigned", StringComparison.CurrentCultureIgnoreCase) != -1) != is_unsigned || - Regex.Replace(trycol.Attribute.DbType, @"\([^\)]+\)", m => Regex.Replace(m.Groups[0].Value, @"\s", "")).StartsWith(sqlType, StringComparison.CurrentCultureIgnoreCase) == false || - (trycol.Attribute.DbType.IndexOf("NOT NULL", StringComparison.CurrentCultureIgnoreCase) == -1) != is_nullable || + trycol.Attribute.DbType.StartsWith(sqlType, StringComparison.CurrentCultureIgnoreCase) == false || + trycol.Attribute.IsNullable != is_nullable || trycol.Attribute.IsIdentity != is_identity) { - sb.Append("ALTER TABLE ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" MODIFY ").Append(_commonUtils.QuoteSqlName(column)).Append(" ").Append(trycol.Attribute.DbType.ToUpper()); + sb.Append("ALTER TABLE ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" MODIFY ").Append(_commonUtils.QuoteSqlName(column)).Append(" ").Append(trycol.Attribute.DbType); if (trycol.Attribute.IsIdentity && trycol.Attribute.DbType.IndexOf("AUTO_INCREMENT", StringComparison.CurrentCultureIgnoreCase) == -1) sb.Append(" AUTO_INCREMENT"); sb.Append(";\r\n"); } @@ -156,12 +156,12 @@ where a.table_schema in ({0}) and a.table_name in ({1})".FormatMySql(isRenameTab } foreach (var addcol in addcols.Values) { if (string.IsNullOrEmpty(addcol.Attribute.OldName) == false && surplus.ContainsKey(addcol.Attribute.OldName)) { //修改列名 - sb.Append("ALTER TABLE ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" CHANGE COLUMN ").Append(_commonUtils.QuoteSqlName(addcol.Attribute.OldName)).Append(" ").Append(_commonUtils.QuoteSqlName(addcol.Attribute.Name)).Append(" ").Append(addcol.Attribute.DbType.ToUpper()); + sb.Append("ALTER TABLE ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" CHANGE COLUMN ").Append(_commonUtils.QuoteSqlName(addcol.Attribute.OldName)).Append(" ").Append(_commonUtils.QuoteSqlName(addcol.Attribute.Name)).Append(" ").Append(addcol.Attribute.DbType); if (addcol.Attribute.IsIdentity && addcol.Attribute.DbType.IndexOf("AUTO_INCREMENT", StringComparison.CurrentCultureIgnoreCase) == -1) sb.Append(" AUTO_INCREMENT"); sb.Append(";\r\n"); } else { //添加列 - sb.Append("ALTER TABLE ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" ADD ").Append(_commonUtils.QuoteSqlName(addcol.Attribute.Name)).Append(" ").Append(addcol.Attribute.DbType.ToUpper()); + sb.Append("ALTER TABLE ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" ADD ").Append(_commonUtils.QuoteSqlName(addcol.Attribute.Name)).Append(" ").Append(addcol.Attribute.DbType); if (addcol.Attribute.IsIdentity && addcol.Attribute.DbType.IndexOf("AUTO_INCREMENT", StringComparison.CurrentCultureIgnoreCase) == -1) sb.Append(" AUTO_INCREMENT"); sb.Append(";\r\n"); } diff --git a/FreeSql/MySql/MySqlExpression.cs b/FreeSql/MySql/MySqlExpression.cs index b7598a88..4782ba18 100644 --- a/FreeSql/MySql/MySqlExpression.cs +++ b/FreeSql/MySql/MySqlExpression.cs @@ -36,9 +36,9 @@ namespace FreeSql.MySql { } var left = ExpressionLambdaToSql(exp.Expression, _tables, _selectColumnMap, tbtype, isQuoteName); switch (exp.Member.Name) { - case "Date": return $"cast(date_format({left}, '%Y-%m-%d') as datetime)"; - case "TimeOfDay": return $"(timestampdiff(microsecond, date_format({left}, '1970-1-1 %H:%i:%s.%f'), {left}) + 62135596800000000)"; - case "DayOfWeek": return $"(dayofweek({left}) - 1)"; + case "Date": return $"cast(date_format({left},'%Y-%m-%d') as datetime)"; + case "TimeOfDay": return $"timestampdiff(microsecond, date_format({left},'%Y-%m-%d'), {left})"; + case "DayOfWeek": return $"(dayofweek({left})-1)"; case "Day": return $"dayofmonth({left})"; case "DayOfYear": return $"dayofyear({left})"; case "Month": return $"month({left})"; @@ -46,8 +46,8 @@ namespace FreeSql.MySql { case "Hour": return $"hour({left})"; case "Minute": return $"minute({left})"; case "Second": return $"second({left})"; - case "Millisecond": return $"floor(microsecond({left}) / 1000)"; - case "Ticks": return $"(timestampdiff(microsecond, '1970-1-1', {left}) * 10 + 621355968000000000)"; + case "Millisecond": return $"floor(microsecond({left})/1000)"; + case "Ticks": return $"(timestampdiff(microsecond, '0001-1-1', {left})*10)"; } return null; } @@ -94,7 +94,7 @@ namespace FreeSql.MySql { case "Substring": var substrArgs1 = ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName); if (long.TryParse(substrArgs1, out var testtrylng1)) substrArgs1 = (testtrylng1 + 1).ToString(); - else substrArgs1 += " + 1"; + else substrArgs1 += "+1"; if (exp.Arguments.Count == 1) return $"substr({left}, {substrArgs1})"; return $"substr({left}, {substrArgs1}, {ExpressionLambdaToSql(exp.Arguments[1], _tables, _selectColumnMap, tbtype, isQuoteName)})"; case "IndexOf": @@ -102,10 +102,10 @@ namespace FreeSql.MySql { if (exp.Arguments.Count > 1 && exp.Arguments[1].Type.FullName == "System.Int32") { var locateArgs1 = ExpressionLambdaToSql(exp.Arguments[1], _tables, _selectColumnMap, tbtype, isQuoteName); if (long.TryParse(locateArgs1, out var testtrylng2)) locateArgs1 = (testtrylng2 + 1).ToString(); - else locateArgs1 += " + 1"; - return $"(locate({left}, {indexOfFindStr}, {locateArgs1}) - 1)"; + else locateArgs1 += "+1"; + return $"(locate({left}, {indexOfFindStr}, {locateArgs1})-1)"; } - return $"(locate({left}, {indexOfFindStr}) - 1)"; + return $"(locate({left}, {indexOfFindStr})-1)"; case "PadLeft": if (exp.Arguments.Count == 1) return $"lpad({left}, {ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})"; return $"lpad({left}, {ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)}, {ExpressionLambdaToSql(exp.Arguments[1], _tables, _selectColumnMap, tbtype, isQuoteName)})"; @@ -167,74 +167,95 @@ namespace FreeSql.MySql { internal override string ExpressionLambdaToSqlCallDateTime(MethodCallExpression exp, List _tables, List _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName) { if (exp.Object == null) { switch (exp.Method.Name) { - case "Compare": return $"(({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)}) - ({ExpressionLambdaToSql(exp.Arguments[1], _tables, _selectColumnMap, tbtype, isQuoteName)}))"; + case "Compare": return $"({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)} - ({ExpressionLambdaToSql(exp.Arguments[1], _tables, _selectColumnMap, tbtype, isQuoteName)}))"; case "DaysInMonth": return $"dayofmonth(last_day(concat({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)}, '-', {ExpressionLambdaToSql(exp.Arguments[1], _tables, _selectColumnMap, tbtype, isQuoteName)}, '-01')))"; case "Equals": return $"({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)} = {ExpressionLambdaToSql(exp.Arguments[1], _tables, _selectColumnMap, tbtype, isQuoteName)})"; case "IsLeapYear": var isLeapYearArgs1 = ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName); - return $"(({isLeapYearArgs1}) % 4 = 0 AND ({isLeapYearArgs1}) % 100 <> 0 OR ({isLeapYearArgs1}) % 400 = 0)"; + return $"(({isLeapYearArgs1})%4=0 AND ({isLeapYearArgs1})%100 <> 0 OR ({isLeapYearArgs1})%400 = 0)"; case "Parse": return $"cast({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)} as datetime)"; case "ParseExact": case "TryParse": case "TryParseExact": return $"cast({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)} as datetime)"; } - return null; - } - var left = ExpressionLambdaToSql(exp.Object, _tables, _selectColumnMap, tbtype, isQuoteName); - var args1 = exp.Arguments.Count == 0 ? null : ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName); - switch (exp.Method.Name) { - case "Add": return $"date_add({left}, interval ({args1}) microsecond)"; - case "AddDays": return $"date_add({left}, interval ({args1}) day)"; - case "AddHours": return $"date_add({left}, interval ({args1}) hour)"; - case "AddMilliseconds": return $"date_add({left}, interval ({args1}) * 1000 microsecond)"; - case "AddMinutes": return $"date_add({left}, interval ({args1}) minute)"; - case "AddMonths": return $"date_add({left}, interval ({args1}) month)"; - case "AddSeconds": return $"date_add({left}, interval ({args1}) second)"; - case "AddTicks": return $"date_add({left}, interval ({args1}) / 10 microsecond)"; - case "AddYears": return $"date_add({left}, interval ({args1}) year)"; - case "Subtract": - if (exp.Arguments[0].Type.FullName == "System.DateTime" || exp.Arguments[0].Type.GenericTypeArguments.FirstOrDefault()?.FullName == "System.DateTime") - return $"timestampdiff(microsecond, {args1}, {left})"; - if (exp.Arguments[0].Type.FullName == "System.TimeSpan" || exp.Arguments[0].Type.GenericTypeArguments.FirstOrDefault()?.FullName == "System.TimeSpan") - return $"date_sub({left}, interval ({args1}) microsecond)"; - break; - case "Equals": return $"({left} = {ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})"; - case "CompareTo": return $"(({left}) - ({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)}))"; - case "ToString": return $"date_format({left}, '%Y-%m-%d %H:%i:%s.%f')"; + } else { + var left = ExpressionLambdaToSql(exp.Object, _tables, _selectColumnMap, tbtype, isQuoteName); + var args1 = exp.Arguments.Count == 0 ? null : ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName); + switch (exp.Method.Name) { + case "Add": return $"date_add({left}, interval ({args1}) microsecond)"; + case "AddDays": return $"date_add({left}, interval ({args1}) day)"; + case "AddHours": return $"date_add({left}, interval ({args1}) hour)"; + case "AddMilliseconds": return $"date_add({left}, interval ({args1})*1000 microsecond)"; + case "AddMinutes": return $"date_add({left}, interval ({args1}) minute)"; + case "AddMonths": return $"date_add({left}, interval ({args1}) month)"; + case "AddSeconds": return $"date_add({left}, interval ({args1}) second)"; + case "AddTicks": return $"date_add({left}, interval ({args1})/10 microsecond)"; + case "AddYears": return $"date_add({left}, interval ({args1}) year)"; + case "Subtract": + if (exp.Arguments[0].Type.FullName == "System.DateTime" || exp.Arguments[0].Type.GenericTypeArguments.FirstOrDefault()?.FullName == "System.DateTime") + return $"timestampdiff(microsecond, {args1}, {left})"; + if (exp.Arguments[0].Type.FullName == "System.TimeSpan" || exp.Arguments[0].Type.GenericTypeArguments.FirstOrDefault()?.FullName == "System.TimeSpan") + return $"date_sub({left}, interval ({args1}) microsecond)"; + break; + case "Equals": return $"({left} = {ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})"; + case "CompareTo": return $"(({left}) - ({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)}))"; + case "ToString": return $"date_format({left}, '%Y-%m-%d %H:%i:%s.%f')"; + } } throw new Exception($"MySqlExpression 未现实函数表达式 {exp} 解析"); } internal override string ExpressionLambdaToSqlCallTimeSpan(MethodCallExpression exp, List _tables, List _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName) { if (exp.Object == null) { switch (exp.Method.Name) { - case "Compare": return $"(({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)}) - ({ExpressionLambdaToSql(exp.Arguments[1], _tables, _selectColumnMap, tbtype, isQuoteName)}))"; + case "Compare": return $"({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)}-({ExpressionLambdaToSql(exp.Arguments[1], _tables, _selectColumnMap, tbtype, isQuoteName)}))"; case "Equals": return $"({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)} = {ExpressionLambdaToSql(exp.Arguments[1], _tables, _selectColumnMap, tbtype, isQuoteName)})"; - case "FromDays": return $"({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)} * {(long)1000000 * 60 * 60 * 24})"; - case "FromHours": return $"({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)} * {(long)1000000 * 60 * 60})"; - case "FromMilliseconds": return $"({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)} * 1000)"; - case "FromMinutes": return $"({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)} * {(long)1000000 * 60})"; - case "FromSeconds": return $"({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)} * 1000000)"; - case "FromTicks": return $"({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)} / 10)"; + case "FromDays": return $"(({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})*{(long)1000000 * 60 * 60 * 24})"; + case "FromHours": return $"(({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})*{(long)1000000 * 60 * 60})"; + case "FromMilliseconds": return $"(({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})*1000)"; + case "FromMinutes": return $"(({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})*{(long)1000000 * 60})"; + case "FromSeconds": return $"(({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})*1000000)"; + case "FromTicks": return $"(({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})/10)"; case "Parse": return $"cast({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)} as signed)"; case "ParseExact": case "TryParse": case "TryParseExact": return $"cast({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)} as signed)"; } - return null; - } - var left = ExpressionLambdaToSql(exp.Object, _tables, _selectColumnMap, tbtype, isQuoteName); - var args1 = exp.Arguments.Count == 0 ? null : ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName); - switch (exp.Method.Name) { - case "Add": return $"({left} + {args1})"; - case "Subtract": return $"({left} - {args1})"; - case "Equals": return $"({left} = {ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})"; - case "CompareTo": return $"(({left}) - ({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)}))"; - case "ToString": return $"cast({left} as varchar)"; + } else { + var left = ExpressionLambdaToSql(exp.Object, _tables, _selectColumnMap, tbtype, isQuoteName); + var args1 = exp.Arguments.Count == 0 ? null : ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName); + switch (exp.Method.Name) { + case "Add": return $"({left}+{args1})"; + case "Subtract": return $"({left}-({args1}))"; + case "Equals": return $"({left} = {ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})"; + case "CompareTo": return $"({left}-({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)}))"; + case "ToString": return $"cast({left} as char)"; + } + } + throw new Exception($"MySqlExpression 未现实函数表达式 {exp} 解析"); + } + internal override string ExpressionLambdaToSqlCallConvert(MethodCallExpression exp, List _tables, List _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName) { + if (exp.Object == null) { + switch (exp.Method.Name) { + case "ToBoolean": return $"({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)} not in ('0','false'))"; + case "ToByte": return $"cast({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)} as unsigned)"; + case "ToChar": return $"substr(cast({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)} as char), 1, 1)"; + case "ToDateTime": return $"cast({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)} as datetime)"; + case "ToDecimal": return $"cast({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)} as decimal(36,18))"; + case "ToDouble": return $"cast({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)} as decimal(32,16))"; + case "ToInt16": + case "ToInt32": + case "ToInt64": + case "ToSByte": return $"cast({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)} as signed)"; + case "ToSingle": return $"cast({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)} as decimal(14,7))"; + case "ToString": return $"cast({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)} as char)"; + case "ToUInt16": + case "ToUInt32": + case "ToUInt64": return $"cast({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)} as unsigned)"; + } } throw new Exception($"MySqlExpression 未现实函数表达式 {exp} 解析"); - } } } diff --git a/FreeSql/PostgreSQL/PostgreSQLExpression.cs b/FreeSql/PostgreSQL/PostgreSQLExpression.cs index 64ebb02f..f303ffbb 100644 --- a/FreeSql/PostgreSQL/PostgreSQLExpression.cs +++ b/FreeSql/PostgreSQL/PostgreSQLExpression.cs @@ -176,5 +176,28 @@ namespace FreeSql.PostgreSQL { } throw new Exception($"PostgreSQLExpression 未现实函数表达式 {exp} 解析"); } + internal override string ExpressionLambdaToSqlCallConvert(MethodCallExpression exp, List _tables, List _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName) { + if (exp.Object == null) { + switch (exp.Method.Name) { + case "ToBoolean": return $"({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)} not in ('0','false'))"; + case "ToByte": return $"cast({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)} as unsigned)"; + case "ToChar": return $"substr(cast({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)} as char), 1, 1)"; + case "ToDateTime": return $"cast({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)} as datetime)"; + case "ToDecimal": return $"cast({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)} as decimal(36,18))"; + case "ToDouble": return $"cast({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)} as decimal(32,16))"; + case "ToInt16": + case "ToInt32": + case "ToInt64": + case "ToSByte": return $"cast({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)} as signed)"; + case "ToSingle": return $"cast({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)} as decimal(14,7))"; + case "ToString": return $"cast({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)} as char)"; + case "ToUInt16": + case "ToUInt32": + case "ToUInt64": return $"cast({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)} as unsigned)"; + } + throw new Exception($"MySqlExpression 未现实函数表达式 {exp} 解析"); + } + throw new Exception($"MySqlExpression 未现实函数表达式 {exp} 解析"); + } } } diff --git a/FreeSql/SqlServer/SqlServerCodeFirst.cs b/FreeSql/SqlServer/SqlServerCodeFirst.cs index b0626d51..8709537a 100644 --- a/FreeSql/SqlServer/SqlServerCodeFirst.cs +++ b/FreeSql/SqlServer/SqlServerCodeFirst.cs @@ -85,7 +85,7 @@ namespace FreeSql.SqlServer { if (_orm.Ado.ExecuteScalar(CommandType.Text, string.Format("select 1 from dbo.sysobjects where id = object_id(N'[{0}].[{1}]') and OBJECTPROPERTY(id, N'IsUserTable') = 1", tbname)) == null) { //表不存在 if (tboldname != null && _orm.Ado.ExecuteScalar(CommandType.Text, string.Format("select 1 from dbo.sysobjects where id = object_id(N'[{0}].[{1}]') and OBJECTPROPERTY(id, N'IsUserTable') = 1", tboldname)) != null) { //旧表存在 - //修改表名 + //修改表名 sb.Append(_commonUtils.FormatSql("EXEC sp_rename {0}, {1};\r\n", _commonUtils.QuoteSqlName($"{tboldname[0]}.{tboldname[1]}"), _commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}"))); isRenameTable = true; @@ -94,7 +94,7 @@ namespace FreeSql.SqlServer { sb.Append("CREATE TABLE ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" ("); foreach (var tbcol in tb.Columns.Values) { sb.Append(" \r\n ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(" "); - sb.Append(tbcol.Attribute.DbType.ToUpper()); + sb.Append(tbcol.Attribute.DbType); if (tbcol.Attribute.IsIdentity && tbcol.Attribute.DbType.IndexOf("identity", StringComparison.CurrentCultureIgnoreCase) == -1) sb.Append(" identity(1,1)"); if (tbcol.Attribute.IsPrimary) sb.Append(" primary key"); sb.Append(","); @@ -126,39 +126,95 @@ left join sys.tables d on d.object_id = a.object_id left join sys.schemas e on e.schema_id = d.schema_id where a.object_id in (object_id(N'[{0}].[{1}]'))", isRenameTable ? tboldname : tbname); var ds = _orm.Ado.ExecuteArray(CommandType.Text, sql); - foreach (var row in ds) { - string column = string.Concat(row[0]); - string sqlType = string.Concat(row[1]).ToLower(); - bool is_nullable = string.Concat(row[2]) == "1"; - bool is_identity = string.Concat(row[3]) == "1"; - - if (addcols.TryGetValue(column, out var trycol)) { - if (Regex.Replace(trycol.Attribute.DbType, @"\([^\)]+\)", m => Regex.Replace(m.Groups[0].Value, @"\s", "")).StartsWith(sqlType, StringComparison.CurrentCultureIgnoreCase) == false || - (trycol.Attribute.DbType.IndexOf("NOT NULL", StringComparison.CurrentCultureIgnoreCase) == -1) != is_nullable || - trycol.Attribute.IsIdentity != is_identity) { - sb.Append("ALTER TABLE ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" ALTER COLUMN ").Append(_commonUtils.QuoteSqlName(column)).Append(" ").Append(trycol.Attribute.DbType.ToUpper()); - if (trycol.Attribute.IsIdentity && trycol.Attribute.DbType.IndexOf("identity", StringComparison.CurrentCultureIgnoreCase) == -1) sb.Append(" identity(1,1)"); - sb.Append(";\r\n"); + var tbstruct = ds.ToDictionary(a => string.Concat(a[0]), a => new { + column = string.Concat(a[0]), + sqlType = string.Concat(a[1]), + is_nullable = string.Concat(a[2]) == "1", + is_identity = string.Concat(a[3]) == "1" + }, StringComparer.CurrentCultureIgnoreCase); + var sbalter = new StringBuilder(); + var istmpatler = false; + foreach (var tbcol in tb.Columns.Values) { + if (tbstruct.TryGetValue(tbcol.Attribute.Name, out var tbstructcol) || + string.IsNullOrEmpty(tbcol.Attribute.OldName) == false && tbstruct.TryGetValue(tbcol.Attribute.OldName, out tbstructcol)) { + if (tbcol.Attribute.DbType.StartsWith(tbstructcol.sqlType, StringComparison.CurrentCultureIgnoreCase) == false || + tbcol.Attribute.IsNullable != tbstructcol.is_nullable || + tbcol.Attribute.IsIdentity != tbstructcol.is_identity) { + istmpatler = true; + break; } - addcols.Remove(column); - } else - surplus.Add(column, true); //记录剩余字段 + if (tbstructcol.column == tbcol.Attribute.OldName) { + //修改列名 + sbalter.Append(_commonUtils.FormatSql("EXEC sp_rename {0}, {1}, 'COLUMN';\r\n", $"{tbname[0]}.{tbname[1]}.{tbcol.Attribute.OldName}", tbcol.Attribute.Name)); + } + continue; + } + //添加列 + sbalter.Append("ALTER TABLE ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" ADD ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(" ").Append(tbcol.Attribute.DbType); + if (tbcol.Attribute.IsIdentity && tbcol.Attribute.DbType.IndexOf("identity", StringComparison.CurrentCultureIgnoreCase) == -1) sbalter.Append(" identity(1,1)"); + var addcoldbdefault = tb.Properties[tbcol.CsName].GetValue(Activator.CreateInstance(tb.Type)); + if (tbcol.Attribute.IsNullable == false) addcoldbdefault = tbcol.Attribute.DbDefautValue; + if (addcoldbdefault != null) sbalter.Append(_commonUtils.FormatSql(" default({0})", addcoldbdefault)); + sbalter.Append(";\r\n"); } - foreach (var addcol in addcols.Values) { - if (string.IsNullOrEmpty(addcol.Attribute.OldName) == false && surplus.ContainsKey(addcol.Attribute.OldName)) { //修改列名 - sb.Append(_commonUtils.FormatSql("EXEC sp_rename {0}, {1}, 'COLUMN';\r\n", $"{tbname[0]}.{tbname[1]}.{addcol.Attribute.OldName}", addcol.Attribute.Name)); - sb.Append("ALTER TABLE ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" ALTER COLUMN ").Append(_commonUtils.QuoteSqlName(addcol.Attribute.Name)).Append(" ").Append(addcol.Attribute.DbType.ToUpper()); - if (addcol.Attribute.IsIdentity && addcol.Attribute.DbType.IndexOf("identity", StringComparison.CurrentCultureIgnoreCase) == -1) sb.Append(" identity(1,1)"); - if (addcol.Attribute.DbType.ToUpper().Contains("NOT NULL")) sb.Append(_commonUtils.FormatSql(" default({0})", Activator.CreateInstance(addcol.CsType.GenericTypeArguments.FirstOrDefault() ?? addcol.CsType))); - sb.Append(";\r\n"); - - } else { //添加列 - sb.Append("ALTER TABLE ").Append(_commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}")).Append(" ADD ").Append(_commonUtils.QuoteSqlName(addcol.Attribute.Name)).Append(" ").Append(addcol.Attribute.DbType.ToUpper()); - if (addcol.Attribute.IsIdentity && addcol.Attribute.DbType.IndexOf("identity", StringComparison.CurrentCultureIgnoreCase) == -1) sb.Append(" identity(1,1)"); - if (addcol.Attribute.DbType.ToUpper().Contains("NOT NULL")) sb.Append(_commonUtils.FormatSql(" default({0})", Activator.CreateInstance(addcol.CsType.GenericTypeArguments.FirstOrDefault() ?? addcol.CsType))); - sb.Append(";\r\n"); + if (istmpatler == false) { + sb.Append(sbalter); + continue; + } + //创建临时表,数据导进临时表,然后删除原表,将临时表改名为原表名 + bool idents = false; + var tablename = _commonUtils.QuoteSqlName($"{tbname[0]}.{tbname[1]}"); + var tmptablename = _commonUtils.QuoteSqlName($"{tbname[0]}.TmpFreeSqlTmp_{tbname[1]}"); + sb.Append("BEGIN TRANSACTION\r\n") + .Append("SET QUOTED_IDENTIFIER ON\r\n") + .Append("SET ARITHABORT ON\r\n") + .Append("SET NUMERIC_ROUNDABORT OFF\r\n") + .Append("SET CONCAT_NULL_YIELDS_NULL ON\r\n") + .Append("SET ANSI_NULLS ON\r\n") + .Append("SET ANSI_PADDING ON\r\n") + .Append("SET ANSI_WARNINGS ON\r\n") + .Append("COMMIT\r\n"); + sb.Append("BEGIN TRANSACTION;\r\n"); + //创建临时表 + sb.Append("CREATE TABLE ").Append(tmptablename).Append(" ("); + foreach (var tbcol in tb.Columns.Values) { + sb.Append(" \r\n ").Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(" "); + sb.Append(tbcol.Attribute.DbType); + if (tbcol.Attribute.IsIdentity && tbcol.Attribute.DbType.IndexOf("identity", StringComparison.CurrentCultureIgnoreCase) == -1) sb.Append(" identity(1,1)"); + if (tbcol.Attribute.IsPrimary) sb.Append(" primary key"); + sb.Append(","); + idents = idents || tbcol.Attribute.IsIdentity; + } + sb.Remove(sb.Length - 1, 1).Append("\r\n);\r\n"); + sb.Append("ALTER TABLE ").Append(tmptablename).Append(" SET (LOCK_ESCALATION = TABLE);\r\n"); + if (idents) sb.Append("SET IDENTITY_INSERT ").Append(tmptablename).Append(" ON;\r\n"); + sb.Append("IF EXISTS(SELECT 1 FROM ").Append(tablename).Append(")\r\n"); + sb.Append("\tEXEC('INSERT INTO ").Append(tmptablename).Append(" ("); + foreach (var tbcol in tb.Columns.Values) { + if (tbstruct.ContainsKey(tbcol.Attribute.Name) || tbstruct.ContainsKey(tbcol.Attribute.OldName)) { //导入旧表存在的字段 + sb.Append(_commonUtils.QuoteSqlName(tbcol.Attribute.Name)).Append(", "); } } + sb.Remove(sb.Length - 2, 2).Append(")\r\n\t\tSELECT "); + foreach (var tbcol in tb.Columns.Values) { + if (tbstruct.TryGetValue(tbcol.Attribute.Name, out var tbstructcol) || + string.IsNullOrEmpty(tbcol.Attribute.OldName) == false && tbstruct.TryGetValue(tbcol.Attribute.OldName, out tbstructcol)) { + var insertvalue = _commonUtils.QuoteSqlName(tbstructcol.column); + if (tbcol.Attribute.DbType.StartsWith(tbstructcol.sqlType, StringComparison.CurrentCultureIgnoreCase) == false) { + var tbcoldbtype = tbcol.Attribute.DbType.Split(' ').First(); + insertvalue = $"cast({insertvalue} as {tbcoldbtype})"; + } + if (tbcol.Attribute.IsNullable != tbstructcol.is_nullable) { + insertvalue = $"isnull({insertvalue},{_commonUtils.FormatSql("{0}", tbcol.Attribute.DbDefautValue).Replace("'", "''")})"; + } + sb.Append(insertvalue).Append(", "); + } + } + sb.Remove(sb.Length - 2, 2).Append(" FROM ").Append(tablename).Append(" WITH (HOLDLOCK TABLOCKX)');\r\n"); + if (idents) sb.Append("SET IDENTITY_INSERT ").Append(tmptablename).Append(" OFF;\r\n"); + sb.Append("DROP TABLE ").Append(tablename).Append(";\r\n"); + sb.Append("EXECUTE sp_rename N'").Append(tmptablename).Append("', N'").Append(tbname[1]).Append("', 'OBJECT' ;\r\n"); + sb.Append("COMMIT;\r\n"); } return sb.Length == 0 ? null : sb.ToString(); } diff --git a/FreeSql/SqlServer/SqlServerExpression.cs b/FreeSql/SqlServer/SqlServerExpression.cs index 1158839f..41dfa9ca 100644 --- a/FreeSql/SqlServer/SqlServerExpression.cs +++ b/FreeSql/SqlServer/SqlServerExpression.cs @@ -37,8 +37,8 @@ namespace FreeSql.SqlServer { var left = ExpressionLambdaToSql(exp.Expression, _tables, _selectColumnMap, tbtype, isQuoteName); switch (exp.Member.Name) { case "Date": return $"convert(char(10),{left},120)"; - case "TimeOfDay": return $"datediff(second, '1970-1-1 ' + convert(varchar, {left}, 14), {left})"; - case "DayOfWeek": return $"(datepart(weekday, {left}) - 1)"; + case "TimeOfDay": return $"datediff(second, convert(char(10),{left},120), {left})"; + case "DayOfWeek": return $"(datepart(weekday, {left})-1)"; case "Day": return $"datepart(day, {left})"; case "DayOfYear": return $"datepart(dayofyear, {left})"; case "Month": return $"datepart(month, {left})"; @@ -46,8 +46,8 @@ namespace FreeSql.SqlServer { case "Hour": return $"datepart(hour, {left})"; case "Minute": return $"datepart(minute, {left})"; case "Second": return $"datepart(second, {left})"; - case "Millisecond": return $"datepart(millisecond, {left})"; - case "Ticks": return $"datediff(second, '1970-1-1', {left})"; + case "Millisecond": return $"(datepart(millisecond, {left})/1000)"; + case "Ticks": return $"(cast(datediff(second, '1970-1-1', {left}) as bigint)*10000000+621355968000000000)"; } return null; } @@ -62,16 +62,16 @@ namespace FreeSql.SqlServer { } var left = ExpressionLambdaToSql(exp.Expression, _tables, _selectColumnMap, tbtype, isQuoteName); switch (exp.Member.Name) { - case "Days": return $"floor(({left}) / {60 * 60 * 24})"; - case "Hours": return $"floor(({left}) / {60 * 60} % 24)"; - case "Milliseconds": return $"(cast({left} as bigint) * 1000)"; - case "Minutes": return $"floor(({left}) / 60 % 60)"; - case "Seconds": return $"(({left}) % 60)"; - case "Ticks": return $"(cast({left} as bigint) * 10000000)"; - case "TotalDays": return $"(({left}) / {60 * 60 * 24})"; - case "TotalHours": return $"(({left}) / {60 * 60})"; - case "TotalMilliseconds": return $"(cast({left} as bigint) * 1000)"; - case "TotalMinutes": return $"(({left}) / 60)"; + case "Days": return $"floor(({left})/{60 * 60 * 24})"; + case "Hours": return $"floor(({left})/{60 * 60}%24)"; + case "Milliseconds": return $"(cast({left} as bigint)*1000)"; + case "Minutes": return $"floor(({left})/60%60)"; + case "Seconds": return $"(({left})%60)"; + case "Ticks": return $"(cast({left} as bigint)*10000000)"; + case "TotalDays": return $"(({left})/{60 * 60 * 24})"; + case "TotalHours": return $"(({left})/{60 * 60})"; + case "TotalMilliseconds": return $"(cast({left} as bigint)*1000)"; + case "TotalMinutes": return $"(({left})/60)"; case "TotalSeconds": return $"({left})"; } return null; @@ -85,22 +85,27 @@ namespace FreeSql.SqlServer { case "Contains": var args0Value = ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName); if (args0Value == "NULL") return $"({left}) IS NULL"; - if (exp.Method.Name == "StartsWith") return $"({left}) LIKE {(args0Value.EndsWith("'") ? args0Value.Insert(args0Value.Length - 1, "%") : $"(cast({args0Value} as nvarchar) + '%')")}"; - if (exp.Method.Name == "EndsWith") return $"({left}) LIKE {(args0Value.StartsWith("'") ? args0Value.Insert(1, "%") : $"('%' + cast({args0Value} as nvarchar))")}"; + if (exp.Method.Name == "StartsWith") return $"({left}) LIKE {(args0Value.EndsWith("'") ? args0Value.Insert(args0Value.Length - 1, "%") : $"(cast({args0Value} as nvarchar)+'%')")}"; + if (exp.Method.Name == "EndsWith") return $"({left}) LIKE {(args0Value.StartsWith("'") ? args0Value.Insert(1, "%") : $"('%'+cast({args0Value} as nvarchar))")}"; if (args0Value.StartsWith("'") && args0Value.EndsWith("'")) return $"({left}) LIKE {args0Value.Insert(1, "%").Insert(args0Value.Length, "%")}"; - return $"({left}) LIKE ('%' + cast({args0Value} as nvarchar) + '%')"; + return $"({left}) LIKE ('%'+cast({args0Value} as nvarchar)+'%')"; case "ToLower": return $"lower({left})"; case "ToUpper": return $"upper({left})"; case "Substring": var substrArgs1 = ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName); if (long.TryParse(substrArgs1, out var testtrylng1)) substrArgs1 = (testtrylng1 + 1).ToString(); - else substrArgs1 += " + 1"; + else substrArgs1 += "+1"; if (exp.Arguments.Count == 1) return $"left({left}, {substrArgs1})"; return $"substring({left}, {substrArgs1}, {ExpressionLambdaToSql(exp.Arguments[1], _tables, _selectColumnMap, tbtype, isQuoteName)})"; case "IndexOf": var indexOfFindStr = ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName); - if (exp.Arguments.Count > 1 && exp.Arguments[1].Type.FullName == "System.Int32") return $"(charindex({left}, {indexOfFindStr}, {ExpressionLambdaToSql(exp.Arguments[1], _tables, _selectColumnMap, tbtype, isQuoteName)} + 1) - 1)"; - return $"(charindex({left}, {indexOfFindStr}) - 1)"; + if (exp.Arguments.Count > 1 && exp.Arguments[1].Type.FullName == "System.Int32") { + var locateArgs1 = ExpressionLambdaToSql(exp.Arguments[1], _tables, _selectColumnMap, tbtype, isQuoteName); + if (long.TryParse(locateArgs1, out var testtrylng2)) locateArgs1 = (testtrylng2 + 1).ToString(); + else locateArgs1 += "+1"; + return $"(charindex({left}, {indexOfFindStr}, {locateArgs1})-1)"; + } + return $"(charindex({left}, {indexOfFindStr})-1)"; case "PadLeft": if (exp.Arguments.Count == 1) return $"lpad({left}, {ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})"; return $"lpad({left}, {ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)}, {ExpressionLambdaToSql(exp.Arguments[1], _tables, _selectColumnMap, tbtype, isQuoteName)})"; @@ -144,71 +149,93 @@ namespace FreeSql.SqlServer { internal override string ExpressionLambdaToSqlCallDateTime(MethodCallExpression exp, List _tables, List _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName) { if (exp.Object == null) { switch (exp.Method.Name) { - case "Compare": return $"(({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)}) - ({ExpressionLambdaToSql(exp.Arguments[1], _tables, _selectColumnMap, tbtype, isQuoteName)}))"; + case "Compare": return $"({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)} - ({ExpressionLambdaToSql(exp.Arguments[1], _tables, _selectColumnMap, tbtype, isQuoteName)}))"; case "DaysInMonth": return $"datepart(day, dateadd(day, -1, dateadd(month, 1, cast({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)} as varchar) + '-' + cast({ExpressionLambdaToSql(exp.Arguments[1], _tables, _selectColumnMap, tbtype, isQuoteName)} as varchar) + '-1')))"; case "Equals": return $"({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)} = {ExpressionLambdaToSql(exp.Arguments[1], _tables, _selectColumnMap, tbtype, isQuoteName)})"; case "IsLeapYear": var isLeapYearArgs1 = ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName); - return $"(({isLeapYearArgs1}) % 4 = 0 AND ({isLeapYearArgs1}) % 100 <> 0 OR ({isLeapYearArgs1}) % 400 = 0)"; + return $"(({isLeapYearArgs1})%4=0 AND ({isLeapYearArgs1})%100<>0 OR ({isLeapYearArgs1})%400=0)"; case "Parse": return $"cast({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)} as datetime)"; case "ParseExact": case "TryParse": case "TryParseExact": return $"cast({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)} as datetime)"; } - return null; - } - var left = ExpressionLambdaToSql(exp.Object, _tables, _selectColumnMap, tbtype, isQuoteName); - var args1 = exp.Arguments.Count == 0 ? null : ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName); - switch (exp.Method.Name) { - case "Add": return $"dateadd(second, {args1}, {left})"; - case "AddDays": return $"dateadd(day, {args1}, {left})"; - case "AddHours": return $"dateadd(hour, {args1}, {left})"; - case "AddMilliseconds": return $"dateadd(second, {args1} / 1000, {left})"; - case "AddMinutes": return $"dateadd(minute, {args1}, {left})"; - case "AddMonths": return $"dateadd(month, {args1}, {left})"; - case "AddSeconds": return $"dateadd(second, {args1}, {left})"; - case "AddTicks": return $"dateadd(second, {args1} / 10000000, {left})"; - case "AddYears": return $"dateadd(year, {args1}, {left})"; - case "Subtract": - if (exp.Arguments[0].Type.FullName == "System.DateTime" || exp.Arguments[0].Type.GenericTypeArguments.FirstOrDefault()?.FullName == "System.DateTime") - return $"datediff(second, {args1}, {left})"; - if (exp.Arguments[0].Type.FullName == "System.TimeSpan" || exp.Arguments[0].Type.GenericTypeArguments.FirstOrDefault()?.FullName == "System.TimeSpan") - return $"dateadd(second, {args1} * -1, {left})"; - break; - case "Equals": return $"({left} = {ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})"; - case "CompareTo": return $"(({left}) - ({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)}))"; - case "ToString": return $"convert(varchar, {left}, 121)"; + } else { + var left = ExpressionLambdaToSql(exp.Object, _tables, _selectColumnMap, tbtype, isQuoteName); + var args1 = exp.Arguments.Count == 0 ? null : ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName); + switch (exp.Method.Name) { + case "Add": return $"dateadd(second, {args1}, {left})"; + case "AddDays": return $"dateadd(day, {args1}, {left})"; + case "AddHours": return $"dateadd(hour, {args1}, {left})"; + case "AddMilliseconds": return $"dateadd(second, ({args1})/1000, {left})"; + case "AddMinutes": return $"dateadd(minute, {args1}, {left})"; + case "AddMonths": return $"dateadd(month, {args1}, {left})"; + case "AddSeconds": return $"dateadd(second, {args1}, {left})"; + case "AddTicks": return $"dateadd(second, ({args1})/10000000, {left})"; + case "AddYears": return $"dateadd(year, {args1}, {left})"; + case "Subtract": + if (exp.Arguments[0].Type.FullName == "System.DateTime" || exp.Arguments[0].Type.GenericTypeArguments.FirstOrDefault()?.FullName == "System.DateTime") + return $"datediff(second, {args1}, {left})"; + if (exp.Arguments[0].Type.FullName == "System.TimeSpan" || exp.Arguments[0].Type.GenericTypeArguments.FirstOrDefault()?.FullName == "System.TimeSpan") + return $"dateadd(second, {args1}*-1, {left})"; + break; + case "Equals": return $"({left} = {ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})"; + case "CompareTo": return $"(({left}) - ({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)}))"; + case "ToString": return $"convert(varchar, {left}, 121)"; + } } throw new Exception($"SqlServerExpression 未现实函数表达式 {exp} 解析"); } internal override string ExpressionLambdaToSqlCallTimeSpan(MethodCallExpression exp, List _tables, List _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName) { if (exp.Object == null) { switch (exp.Method.Name) { - case "Compare": return $"(({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)}) - ({ExpressionLambdaToSql(exp.Arguments[1], _tables, _selectColumnMap, tbtype, isQuoteName)}))"; + case "Compare": return $"({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)}-({ExpressionLambdaToSql(exp.Arguments[1], _tables, _selectColumnMap, tbtype, isQuoteName)}))"; case "Equals": return $"({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)} = {ExpressionLambdaToSql(exp.Arguments[1], _tables, _selectColumnMap, tbtype, isQuoteName)})"; - case "FromDays": return $"({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)} * {60 * 60 * 24})"; - case "FromHours": return $"({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)} * {60 * 60})"; - case "FromMilliseconds": return $"({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)} / 1000)"; - case "FromMinutes": return $"({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)} * 60)"; + case "FromDays": return $"(({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})*{60 * 60 * 24})"; + case "FromHours": return $"(({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})*{60 * 60})"; + case "FromMilliseconds": return $"(({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})/1000)"; + case "FromMinutes": return $"(({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})*60)"; case "FromSeconds": return $"({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})"; - case "FromTicks": return $"({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)} / 10000000)"; + case "FromTicks": return $"(({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})/10000000)"; case "Parse": return $"cast({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)} as bigint)"; case "ParseExact": case "TryParse": case "TryParseExact": return $"cast({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)} as bigint)"; } - return null; + } else { + var left = ExpressionLambdaToSql(exp.Object, _tables, _selectColumnMap, tbtype, isQuoteName); + var args1 = exp.Arguments.Count == 0 ? null : ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName); + switch (exp.Method.Name) { + case "Add": return $"({left}+{args1})"; + case "Subtract": return $"({left}-({args1}))"; + case "Equals": return $"({left} = {ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})"; + case "CompareTo": return $"({left}-({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)}))"; + case "ToString": return $"cast({left} as varchar)"; + } } - var left = ExpressionLambdaToSql(exp.Object, _tables, _selectColumnMap, tbtype, isQuoteName); - var args1 = exp.Arguments.Count == 0 ? null : ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName); - switch (exp.Method.Name) { - case "Add": return $"({left} + {args1})"; - case "Subtract": return $"({left} - {args1})"; - case "Equals": return $"({left} = {ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})"; - case "CompareTo": return $"(({left}) - ({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)}))"; - case "ToString": return $"cast({left} as varchar)"; + throw new Exception($"SqlServerExpression 未现实函数表达式 {exp} 解析"); + } + internal override string ExpressionLambdaToSqlCallConvert(MethodCallExpression exp, List _tables, List _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName) { + if (exp.Object == null) { + switch (exp.Method.Name) { + case "ToBoolean": return $"({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)} not in ('0','false'))"; + case "ToByte": return $"cast({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)} as tinyint)"; + case "ToChar": return $"substring(cast({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)} as nvarchar), 1, 1)"; + case "ToDateTime": return $"cast({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)} as datetime)"; + case "ToDecimal": return $"cast({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)} as decimal(36,18))"; + case "ToDouble": return $"cast({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)} as decimal(32,16))"; + case "ToInt16": return $"cast({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)} as smallint)"; + case "ToInt32": return $"cast({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)} as int)"; + case "ToInt64": return $"cast({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)} as bigint)"; + case "ToSByte": return $"cast({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)} as tinyint)"; + case "ToSingle": return $"cast({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)} as decimal(14,7))"; + case "ToString": return $"cast({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)} as nvarchar)"; + case "ToUInt16": return $"cast({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)} as smallint)"; + case "ToUInt32": return $"cast({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)} as int)"; + case "ToUInt64": return $"cast({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)} as bigint)"; + } } throw new Exception($"SqlServerExpression 未现实函数表达式 {exp} 解析"); }