mysql 表达式函数测试完成

This commit is contained in:
28810 2018-12-21 17:03:51 +08:00
parent 572aa1d8ee
commit 2a07c8bae6
14 changed files with 1454 additions and 410 deletions

124
Docs/expression.md Normal file
View File

@ -0,0 +1,124 @@
# 表达式函数
| 表达式 | MySql | SqlServer | PostgreSQL | 功能说明 |
| - | - | - | - | - |
| (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 | 减
| a * b | a * b | a * b | a * b | 乘
| a / b | a / b | a / b | a / b | 乘
| a % b | a mod b | a mod b | a mod b | 模
> 等等...
### 字符串对象
| 表达式 | MySql | SqlServer | PostgreSQL | 功能说明 |
| - | - | - | - | - |
| string.Empty | '' | '' | '' | 空字符串表示 |
| a.CompareTo(b) | a - b | - | - | 比较a和b大小 |
| a.Contains('b') | a like '%b%' | - | - | a是否包含b |
| a.EndsWith('b') | a like '%b' | - | - | a尾部是否包含b |
| a.IndexOf(b) | locate(a, b) - 1 | - | - | 查找a中出现b的位置 |
| a.Length | char_length(a) | - | - | 返回a的字符串长度 |
| a.PadLeft(b, c) | lpad(a, b, c) | - | - | 在a的左侧充字符c直到字符串长度大于b |
| a.PadRight(b, c) | rpad(a, b, c) | - | - | 在a的右侧充字符c直到字符串长度大于b |
| a.Replace(b, c) | replace(a, b, c) | - | - | 将a中字符串b替换成c |
| a.StartsWith('b') | a like 'b%' | - | - | a头部是否包含b |
| a.Substring(b, c) | substr(a, b, c) | - | - | 截取a中位置b到c的内容 |
| a.ToLower | lower(a) | - | - | 转小写 |
| a.ToUpper | upper(a) | - | - | 转大写 |
| a.Trim | trim(a) | - | - | 移除两边字符 |
| a.TrimEnd | rtrim(a) | - | - | 移除左侧指定字符 |
| a.TrimStart | ltrim(a) | - | - | 移除右侧指定字符 |
### 日期对象
| 表达式 | MySql | SqlServer | PostgreSQL | 功能说明 |
| - | - | - | - | - |
| DateTime.Now | now() | - | - | 取本地时间 |
| DateTime.UtcNow | utc_timestamp() | - | - | 取UTC时间 |
| DateTime.Today | curdate | - | - | 取本地时间,日期部分 |
| DateTime.MaxValue | cast('9999/12/31 23:59:59' as datetime) | - | - | 最大时间 |
| DateTime.MinValue | cast('0001/1/1 0:00:00' as datetime) | - | - | 最小时间 |
| DateTime.Compare(a, b) | a - b | - | - | 比较a和b的大小 |
| DateTime.DaysInMonth(a, b) | dayofmonth(last_day(concat(a, '-', b, '-1'))) | - | - | 取指定年月份的总天数 |
| DateTime.Equals(a, b) | a = b | - | - | 比较a和b相等 |
| DateTime.IsLeapYear(a) | a%4=0 and a%100<>0 or a%400=0 | - | - | 判断闰年 |
| DateTime.Parse(a) | cast(a as datetime) | - | - | 转换日期类型 |
| a.Add(b) | date_add(a, interval b microsecond) | - | - | 增加TimeSpan值 |
| a.AddDays(b) | date_add(a, interval b day) | - | - | 增加天数 |
| a.AddHours(b) | date_add(a, interval b hour) | - | - | 增加小时 |
| a.AddMilliseconds(b) | date_add(a, interval b*1000 microsecond) | - | - | 增加毫秒 |
| a.AddMinutes(b) | date_add(a, interval b minute) | - | - | 增加分钟 |
| a.AddMonths(b) | date_add(a, interval b month) | - | - | 增加月 |
| a.AddSeconds(b) | date_add(a, interval b second) | - | - | 增加秒 |
| a.AddTicks(b) | date_add(a, interval b/10 microsecond) | - | - | 增加刻度微秒的1/10 |
| a.AddYears(b) | date_add(a, interval b year) | - | - | 增加年 |
| a.Date | cast(date_format(a, '%Y-%m-%d') as datetime) | - | - | 获取a的日期部分 |
| a.Day | dayofmonth(a) | - | - | 获取a在月的第几天 |
| a.DayOfWeek | dayofweek(a) | - | - | 获取a在周的第几天 |
| a.DayOfYear | dayofyear(a) | - | - | 获取a在年的第几天 |
| a.Hour | hour(a) | - | - | 小时 |
| a.Millisecond | floor(microsecond(a) / 1000) | - | - | 毫秒 |
| a.Minute | minute(a) | - | - | 分钟 |
| a.Month | month(a) | - | - | 月 |
| a.Second | second(a) | - | - | 秒 |
| a.Subtract(b) | (time_to_sec(a) - time_to_sec(b)) * 1000000 + microsecond(a) - microsecond(b) | - | - | 将a的值和b相减 |
| a.Ticks | time_to_sec(a) * 10000000 + microsecond(a) * 10 + 62135596800000000 | - | - | 刻度总数 |
| a.TimeOfDay | time_to_sec(date_format(a, '1970-1-1 %H:%i:%s.%f')) * 1000000 + microsecond(a) + 6213559680000000 | - | - | 获取a的时间部分 |
| a.Year | year(a) | - | - | 年 |
| a.Equals(b) | a = b | - | - | 比较a和b相等 |
| a.CompareTo(b) | a - b | - | - | 比较a和b大小 |
| a.ToString() | date_format(a, '%Y-%m-%d %H:%i:%s.%f') | - | - | 转换字符串 |
### 时间对象
| 表达式 | MySql | SqlServer | PostgreSQL | 功能说明 |
| - | - | - | - | - |
| TimeSpan.Zero | 0 | - | - | 0微秒 |
| TimeSpan.MaxValue | 922337203685477580 | - | - | 最大微秒时间 |
| TimeSpan.MinValue | -922337203685477580 | - | - | 最小微秒时间 |
| TimeSpan.Compare(a, b) | a - b | - | - | 比较a和b的大小 |
| TimeSpan.Equals(a, b) | a = b | - | - | 比较a和b相等 |
| TimeSpan.FromDays(a) | a * 1000000 * 60 * 60 * 24 | - | - | a天的微秒值 |
| TimeSpan.FromHours(a) | a * 1000000 * 60 * 60 | - | - | a小时的微秒值 |
| TimeSpan.FromMilliseconds(a) | a * 1000 | - | - | a毫秒的微秒值 |
| TimeSpan.FromMinutes(a) | a * 1000000 * 60 | - | - | a分钟的微秒值 |
| TimeSpan.FromSeconds(a) | a * 1000000 | - | - | a秒钟的微秒值 |
| TimeSpan.FromTicks(a) | a / 10 | - | - | a刻度的毫秒值 |
| a.Add(b) | a + b | - | - | 增加值 |
| a.Subtract(b) | a - b | - | - | 将a的值和b相减 |
| a.CompareTo(b) | a - b | - | - | 比较a和b大小 |
| a.Days | a div (1000000 * 60 * 60 * 24) | - | - | 天数部分 |
| a.Hours | a div (1000000 * 60 * 60) mod 24 | - | - | 小时部分 |
| a.Milliseconds | a div 1000 mod 1000 | - | - | 毫秒部分 |
| a.Seconds | a div 1000000 mod 60 | - | - | 秒数部分 |
| a.Ticks | a * 10 | - | - | 刻度总数 |
| a.TotalDays | a / (1000000 * 60 * 60 * 24) | - | - | 总天数(含小数) |
| a.TotalHours | a / (1000000 * 60 * 60) | - | - | 总小时(含小数) |
| a.TotalMilliseconds | a / 1000 | - | - | 总毫秒(含小数) |
| a.TotalMinutes | a / (1000000 * 60) | - | - | 总分钟(含小数) |
| a.TotalSeconds | a / 1000000 | - | - | 总秒数(含小数) |
| a.Equals(b) | a = b | - | - | 比较a和b相等 |
| a.ToString() | | - | - | 转换字符串 |
### 数学函数
| 表达式 | MySql | SqlServer | PostgreSQL | 功能说明 |
| - | - | - | - | - |
| Math.Abs(a) | abs(a) | - | - | - |
| Math.Acos(a) | acos(a) | - | - | - |
| Math.Asin(a) | asin(a) | - | - | - |
| Math.Atan(a) | atan(a) | - | - | - |
| Math.Atan2(a, b) | atan2(a, b) | - | - | - |
| Math.Ceiling(a) | ceiling(a) | - | - | - |
| Math.Cos(a) | cos(a) | - | - | - |
| Math.Exp(a) | exp(a) | - | - | - |
| Math.Floor(a) | floor(a) | - | - | - |
| Math.Log(a) | log(a) | - | - | - |
| Math.Log10(a) | log10(a) | - | - | - |
| Math.PI(a) | 3.1415926535897931 | - | - | - |
| Math.Pow(a, b) | pow(a, b) | - | - | - |
| Math.Round(a, b) | round(a, b) | - | - | - |
| Math.Sign(a) | sign(a) | - | - | - |
| Math.Sin(a) | sin(a) | - | - | - |
| Math.Sqrt(a) | sqrt(a) | - | - | - |
| Math.Tan(a) | tan(a) | - | - | - |
| Math.Truncate(a) | truncate(a, 0) | - | - | - |

View File

@ -1,115 +0,0 @@
using FreeSql.DataAnnotations;
using System;
using System.Collections.Generic;
using System.Linq;
using Xunit;
namespace FreeSql.Tests.MySql.Expression {
public class DateTimeTest {
ISelect<Topic> select => g.mysql.Select<Topic>();
[Table(Name = "tb_topic111333")]
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; }
}
[Table(Name = "TestTypeInfo333")]
class TestTypeInfo {
public int Guid { get; set; }
public int ParentId { get; set; }
public TestTypeParentInfo Parent { get; set; }
public string Name { get; set; }
public DateTime Time { get; set; }
}
[Table(Name = "TestTypeParentInfo23123")]
class TestTypeParentInfo {
public int Id { get; set; }
public string Name { get; set; }
public List<TestTypeInfo> Types { get; set; }
public DateTime Time2 { get; set; }
}
[Fact]
public void DayOfWeek() {
var data = new List<object>();
data.Add(select.Where(a => a.CreateTime.DayOfWeek > DateTime.Now.DayOfWeek).ToSql());
data.Add(select.Where(a => a.Type.Time.DayOfWeek > DateTime.Now.DayOfWeek).ToSql());
data.Add(select.Where(a => a.Type.Parent.Time2.DayOfWeek > DateTime.Now.DayOfWeek).ToSql());
//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() {
}
[Fact]
public void DayOfYear() {
}
[Fact]
public void Month() {
}
[Fact]
public void Year() {
}
[Fact]
public void Hour() {
}
[Fact]
public void Minute() {
}
[Fact]
public void Second() {
}
[Fact]
public void Millisecond() {
}
[Fact]
public void Ticks() {
}
[Fact]
public void Add() {
}
[Fact]
public void AddDays() {
}
[Fact]
public void AddHours() {
}
[Fact]
public void AddMilliseconds() {
}
[Fact]
public void AddMinutes() {
}
[Fact]
public void AddMonths() {
}
[Fact]
public void AddSeconds() {
}
[Fact]
public void AddTicks() {
}
[Fact]
public void AddYears() {
}
[Fact]
public void Subtract() {
}
}
}

View File

@ -1,95 +0,0 @@
using FreeSql.DataAnnotations;
using System;
using System.Collections.Generic;
using System.Linq;
using Xunit;
namespace FreeSql.Tests.MySql.Expression {
public class MathTest {
ISelect<Topic> select => g.mysql.Select<Topic>();
[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<TestTypeInfo> Types { get; set; }
}
[Fact]
public void PI() {
var data = new List<object>();
data.Add(select.Where(a => Math.PI + a.Clicks > 0).ToSql());
}
[Fact]
public void Abs() {
}
[Fact]
public void Sign() {
}
[Fact]
public void Floor() {
}
[Fact]
public void Ceiling() {
}
[Fact]
public void Round() {
}
[Fact]
public void Exp() {
}
[Fact]
public void Log() {
}
[Fact]
public void Log10() {
}
[Fact]
public void Pow() {
}
[Fact]
public void Sqrt() {
}
[Fact]
public void Cos() {
}
[Fact]
public void Sin() {
}
[Fact]
public void Tan() {
}
[Fact]
public void Acos() {
}
[Fact]
public void Asin() {
}
[Fact]
public void Atan() {
}
[Fact]
public void Atan2() {
}
[Fact]
public void Truncate() {
}
}
}

View File

@ -1,75 +0,0 @@
using FreeSql.DataAnnotations;
using System;
using System.Collections.Generic;
using System.Linq;
using Xunit;
namespace FreeSql.Tests.MySql.Expression {
public class TimeSpanTest {
ISelect<Topic> select => g.mysql.Select<Topic>();
[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<TestTypeInfo> Types { get; set; }
}
[Fact]
public void Days() {
}
[Fact]
public void Hours() {
}
[Fact]
public void Milliseconds() {
}
[Fact]
public void Minutes() {
}
[Fact]
public void Seconds() {
}
[Fact]
public void Ticks() {
}
[Fact]
public void TotalDays() {
}
[Fact]
public void TotalHours() {
}
[Fact]
public void TotalMilliseconds() {
}
[Fact]
public void TotalMinutes() {
}
[Fact]
public void TotalSeconds() {
}
[Fact]
public void Add() {
}
[Fact]
public void Subtract() {
}
}
}

View File

@ -0,0 +1,622 @@
using FreeSql.DataAnnotations;
using System;
using System.Collections.Generic;
using System.Linq;
using Xunit;
namespace FreeSql.Tests.MySqlExpression {
public class DateTimeTest {
ISelect<Topic> select => g.mysql.Select<Topic>();
[Table(Name = "tb_topic111333")]
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; }
}
[Table(Name = "TestTypeInfo333")]
class TestTypeInfo {
public int Guid { get; set; }
public int ParentId { get; set; }
public TestTypeParentInfo Parent { get; set; }
public string Name { get; set; }
public DateTime Time { get; set; }
}
[Table(Name = "TestTypeParentInfo23123")]
class TestTypeParentInfo {
public int Id { get; set; }
public string Name { get; set; }
public List<TestTypeInfo> Types { get; set; }
public DateTime Time2 { get; set; }
}
[Fact]
public void Now() {
var data = new List<object>();
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<object>();
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<object>();
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<object>();
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() {
var data = new List<object>();
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 (((((time_to_sec(now()) - time_to_sec(cast(date_format(a.`CreateTime`, '%Y-%m-%d') as datetime))) * 1000000 + microsecond(now()) - microsecond(cast(date_format(a.`CreateTime`, '%Y-%m-%d') as datetime)))) / 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 (((((time_to_sec(now()) - time_to_sec(cast(date_format(a__Type.`Time`, '%Y-%m-%d') as datetime))) * 1000000 + microsecond(now()) - microsecond(cast(date_format(a__Type.`Time`, '%Y-%m-%d') as datetime)))) / 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 (((((time_to_sec(now()) - time_to_sec(cast(date_format(a__Type__Parent.`Time2`, '%Y-%m-%d') as datetime))) * 1000000 + microsecond(now()) - microsecond(cast(date_format(a__Type__Parent.`Time2`, '%Y-%m-%d') as datetime)))) / 1000000) > 0)
}
[Fact]
public void TimeOfDay() {
var data = new List<object>();
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 ((time_to_sec(a.`CreateTime`) * 1000000 + microsecond(a.`CreateTime`) + 6213559680000000) = (time_to_sec(now()) * 1000000 + microsecond(now()) + 6213559680000000));
//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 ((time_to_sec(a__Type.`Time`) * 1000000 + microsecond(a__Type.`Time`) + 6213559680000000) > (time_to_sec(now()) * 1000000 + microsecond(now()) + 6213559680000000));
//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 ((time_to_sec(a__Type__Parent.`Time2`) * 1000000 + microsecond(a__Type__Parent.`Time2`) + 6213559680000000) > (time_to_sec(now()) * 1000000 + microsecond(now()) + 6213559680000000))
}
[Fact]
public void DayOfWeek() {
var data = new List<object>();
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() {
var data = new List<object>();
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() {
var data = new List<object>();
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() {
var data = new List<object>();
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() {
var data = new List<object>();
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() {
var data = new List<object>();
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() {
var data = new List<object>();
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() {
var data = new List<object>();
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() {
var data = new List<object>();
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() {
var data = new List<object>();
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 ((time_to_sec(a.`CreateTime`) * 10000000 + microsecond(a.`CreateTime`) * 10 + 62135596800000000) > (time_to_sec(now()) * 10000000 + microsecond(now()) * 10 + 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 ((time_to_sec(a__Type.`Time`) * 10000000 + microsecond(a__Type.`Time`) * 10 + 62135596800000000) > (time_to_sec(now()) * 10000000 + microsecond(now()) * 10 + 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 ((time_to_sec(a__Type__Parent.`Time2`) * 10000000 + microsecond(a__Type__Parent.`Time2`) * 10 + 62135596800000000) > (time_to_sec(now()) * 10000000 + microsecond(now()) * 10 + 62135596800000000))
}
[Fact]
public void Add() {
var data = new List<object>();
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() {
var data = new List<object>();
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() {
var data = new List<object>();
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() {
var data = new List<object>();
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() {
var data = new List<object>();
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() {
var data = new List<object>();
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() {
var data = new List<object>();
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() {
var data = new List<object>();
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() {
var data = new List<object>();
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() {
var data = new List<object>();
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 (((((time_to_sec(a.`CreateTime`) - time_to_sec(now())) * 1000000 + microsecond(a.`CreateTime`) - microsecond(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 (((((time_to_sec(a__Type.`Time`) - time_to_sec(now())) * 1000000 + microsecond(a__Type.`Time`) - microsecond(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 (((((time_to_sec(a__Type__Parent.`Time2`) - time_to_sec(now())) * 1000000 + microsecond(a__Type__Parent.`Time2`) - microsecond(now()))) / 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() {
var data = new List<object>();
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() {
var data = new List<object>();
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]
public void DateTime_Compare() {
var data = new List<object>();
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() {
var data = new List<object>();
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() {
var data = new List<object>();
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() {
var data = new List<object>();
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() {
var data = new List<object>();
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())
}
}
}

View File

@ -0,0 +1,132 @@
using FreeSql.DataAnnotations;
using System;
using System.Collections.Generic;
using System.Linq;
using Xunit;
namespace FreeSql.Tests.MySqlExpression {
public class MathTest {
ISelect<Topic> select => g.mysql.Select<Topic>();
[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<TestTypeInfo> Types { get; set; }
}
[Fact]
public void PI() {
var data = new List<object>();
data.Add(select.Where(a => Math.PI + a.Clicks > 0).ToList());
}
[Fact]
public void Abs() {
var data = new List<object>();
data.Add(select.Where(a => Math.Abs(-a.Clicks) > 0).ToList());
}
[Fact]
public void Sign() {
var data = new List<object>();
data.Add(select.Where(a => Math.Sign(-a.Clicks) > 0).ToList());
}
[Fact]
public void Floor() {
var data = new List<object>();
data.Add(select.Where(a => Math.Floor(a.Clicks + 0.5) == a.Clicks).ToList());
}
[Fact]
public void Ceiling() {
var data = new List<object>();
data.Add(select.Where(a => Math.Ceiling(a.Clicks + 0.5) == a.Clicks + 1).ToList());
}
[Fact]
public void Round() {
var data = new List<object>();
data.Add(select.Where(a => Math.Round(a.Clicks + 0.5) == a.Clicks).ToList());
data.Add(select.Where(a => Math.Round(a.Clicks + 0.5, 1) > a.Clicks).ToList());
}
[Fact]
public void Exp() {
var data = new List<object>();
data.Add(select.Where(a => Math.Exp(1) == a.Clicks + 1).ToList());
}
[Fact]
public void Log() {
var data = new List<object>();
data.Add(select.Where(a => Math.Log(a.Clicks + 0.5) == a.Clicks + 1).ToList());
}
[Fact]
public void Log10() {
var data = new List<object>();
data.Add(select.Where(a => Math.Log10(a.Clicks + 0.5) == a.Clicks + 1).ToList());
}
[Fact]
public void Pow() {
var data = new List<object>();
data.Add(select.Where(a => Math.Pow(2, a.Clicks) == a.Clicks + 1).ToList());
}
[Fact]
public void Sqrt() {
var data = new List<object>();
data.Add(select.Where(a => Math.Sqrt(Math.Pow(2, a.Clicks)) == a.Clicks + 1).ToList());
}
[Fact]
public void Cos() {
var data = new List<object>();
data.Add(select.Where(a => Math.Cos(Math.Pow(2, a.Clicks)) == a.Clicks + 1).ToList());
}
[Fact]
public void Sin() {
var data = new List<object>();
data.Add(select.Where(a => Math.Sin(Math.Pow(2, a.Clicks)) == a.Clicks + 1).ToList());
}
[Fact]
public void Tan() {
var data = new List<object>();
data.Add(select.Where(a => Math.Tan(Math.Pow(2, a.Clicks)) == a.Clicks + 1).ToList());
}
[Fact]
public void Acos() {
var data = new List<object>();
data.Add(select.Where(a => Math.Acos(Math.Pow(2, a.Clicks)) == a.Clicks + 1).ToList());
}
[Fact]
public void Asin() {
var data = new List<object>();
data.Add(select.Where(a => Math.Asin(Math.Pow(2, a.Clicks)) == a.Clicks + 1).ToList());
}
[Fact]
public void Atan() {
var data = new List<object>();
data.Add(select.Where(a => Math.Atan(Math.Pow(2, a.Clicks)) == a.Clicks + 1).ToList());
}
[Fact]
public void Atan2() {
var data = new List<object>();
data.Add(select.Where(a => Math.Atan2(2, a.Clicks) == a.Clicks + 1).ToList());
}
[Fact]
public void Truncate() {
var data = new List<object>();
data.Add(select.Where(a => Math.Truncate(a.Clicks * 1.0 / 3) == a.Clicks + 1).ToList());
}
}
}

View File

@ -4,7 +4,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using Xunit; using Xunit;
namespace FreeSql.Tests.MySql.Expression { namespace FreeSql.Tests.MySqlExpression {
public class StringTest { public class StringTest {
ISelect<Topic> select => g.mysql.Select<Topic>(); ISelect<Topic> select => g.mysql.Select<Topic>();
@ -32,12 +32,22 @@ namespace FreeSql.Tests.MySql.Expression {
public List<TestTypeInfo> Types { get; set; } public List<TestTypeInfo> Types { get; set; }
} }
[Fact]
public void Empty() {
var data = new List<object>();
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] [Fact]
public void StartsWith() { public void StartsWith() {
var list = select.Where(a => a.Title.StartsWith("aaa")).ToList(); var list = new List<object>();
list = select.Where(a => a.Title.StartsWith(a.Title)).ToList(); list.Add(select.Where(a => a.Title.StartsWith("aaa")).ToList());
list = select.Where(a => a.Title.StartsWith(a.Title + 1)).ToList(); list.Add(select.Where(a => a.Title.StartsWith(a.Title)).ToList());
list = select.Where(a => a.Title.StartsWith(a.Type.Name)).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 //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
//FROM `tb_topic` a //FROM `tb_topic` a
//WHERE((a.`Title`) LIKE '%aaa') //WHERE((a.`Title`) LIKE '%aaa')
@ -53,10 +63,10 @@ namespace FreeSql.Tests.MySql.Expression {
//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 //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 //FROM `tb_topic` a, `TestTypeInfo` a__Type
//WHERE((a.`Title`) LIKE concat('%', a__Type.`Name`)) //WHERE((a.`Title`) LIKE concat('%', a__Type.`Name`))
list = select.Where(a => (a.Title + "aaa").StartsWith("aaa")).ToList(); list.Add(select.Where(a => (a.Title + "aaa").StartsWith("aaa")).ToList());
list = select.Where(a => (a.Title + "aaa").StartsWith(a.Title)).ToList(); list.Add(select.Where(a => (a.Title + "aaa").StartsWith(a.Title)).ToList());
list = select.Where(a => (a.Title + "aaa").StartsWith(a.Title + 1)).ToList(); list.Add(select.Where(a => (a.Title + "aaa").StartsWith(a.Title + 1)).ToList());
list = select.Where(a => (a.Title + "aaa").StartsWith(a.Type.Name)).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 //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
//FROM `tb_topic` a //FROM `tb_topic` a
//WHERE((concat(a.`Title`, 'aaa')) LIKE '%aaa') //WHERE((concat(a.`Title`, 'aaa')) LIKE '%aaa')
@ -75,10 +85,11 @@ namespace FreeSql.Tests.MySql.Expression {
} }
[Fact] [Fact]
public void EndsWith() { public void EndsWith() {
var list = select.Where(a => a.Title.EndsWith("aaa")).ToList(); var list = new List<object>();
list = select.Where(a => a.Title.EndsWith(a.Title)).ToList(); list.Add(select.Where(a => a.Title.EndsWith("aaa")).ToList());
list = select.Where(a => a.Title.EndsWith(a.Title + 1)).ToList(); list.Add(select.Where(a => a.Title.EndsWith(a.Title)).ToList());
list = select.Where(a => a.Title.EndsWith(a.Type.Name)).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 //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
//FROM `tb_topic` a //FROM `tb_topic` a
//WHERE((a.`Title`) LIKE 'aaa%') //WHERE((a.`Title`) LIKE 'aaa%')
@ -94,10 +105,10 @@ namespace FreeSql.Tests.MySql.Expression {
//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 //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 //FROM `tb_topic` a, `TestTypeInfo` a__Type
//WHERE((a.`Title`) LIKE concat(a__Type.`Name`, '%')) //WHERE((a.`Title`) LIKE concat(a__Type.`Name`, '%'))
list = select.Where(a => (a.Title + "aaa").EndsWith("aaa")).ToList(); list.Add(select.Where(a => (a.Title + "aaa").EndsWith("aaa")).ToList());
list = select.Where(a => (a.Title + "aaa").EndsWith(a.Title)).ToList(); list.Add(select.Where(a => (a.Title + "aaa").EndsWith(a.Title)).ToList());
list = select.Where(a => (a.Title + "aaa").EndsWith(a.Title + 1)).ToList(); list.Add(select.Where(a => (a.Title + "aaa").EndsWith(a.Title + 1)).ToList());
list = select.Where(a => (a.Title + "aaa").EndsWith(a.Type.Name)).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 //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
//FROM `tb_topic` a //FROM `tb_topic` a
//WHERE((concat(a.`Title`, 'aaa')) LIKE 'aaa%') //WHERE((concat(a.`Title`, 'aaa')) LIKE 'aaa%')
@ -116,10 +127,11 @@ namespace FreeSql.Tests.MySql.Expression {
} }
[Fact] [Fact]
public void Contains() { public void Contains() {
var ToList = select.Where(a => a.Title.Contains("aaa")).ToList(); var list = new List<object>();
ToList = select.Where(a => a.Title.Contains(a.Title)).ToList(); list.Add(select.Where(a => a.Title.Contains("aaa")).ToList());
ToList = select.Where(a => a.Title.Contains(a.Title + 1)).ToList(); list.Add(select.Where(a => a.Title.Contains(a.Title)).ToList());
ToList = select.Where(a => a.Title.Contains(a.Type.Name)).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 //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
//FROM `tb_topic` a //FROM `tb_topic` a
//WHERE((a.`Title`) LIKE '%aaa%') //WHERE((a.`Title`) LIKE '%aaa%')
@ -135,10 +147,10 @@ namespace FreeSql.Tests.MySql.Expression {
//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 //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 //FROM `tb_topic` a, `TestTypeInfo` a__Type
//WHERE((a.`Title`) LIKE concat('%', a__Type.`Name`, '%')) //WHERE((a.`Title`) LIKE concat('%', a__Type.`Name`, '%'))
ToList = select.Where(a => (a.Title + "aaa").Contains("aaa")).ToList(); list.Add(select.Where(a => (a.Title + "aaa").Contains("aaa")).ToList());
ToList = select.Where(a => (a.Title + "aaa").Contains(a.Title)).ToList(); list.Add(select.Where(a => (a.Title + "aaa").Contains(a.Title)).ToList());
ToList = select.Where(a => (a.Title + "aaa").Contains(a.Title + 1)).ToList(); list.Add(select.Where(a => (a.Title + "aaa").Contains(a.Title + 1)).ToList());
ToList = select.Where(a => (a.Title + "aaa").Contains(a.Type.Name)).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 //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5
//FROM `tb_topic` a //FROM `tb_topic` a
//WHERE((concat(a.`Title`, 'aaa')) LIKE '%aaa%') //WHERE((concat(a.`Title`, 'aaa')) LIKE '%aaa%')

View File

@ -0,0 +1,260 @@
using FreeSql.DataAnnotations;
using System;
using System.Collections.Generic;
using System.Linq;
using Xunit;
namespace FreeSql.Tests.MySqlExpression {
public class TimeSpanTest {
ISelect<Topic> select => g.mysql.Select<Topic>();
[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<TestTypeInfo> Types { get; set; }
}
[Fact]
public void Zero() {
var data = new List<object>();
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 ((time_to_sec(date_format(a.`CreateTime`, '1970-1-1 %H:%i:%s.%f')) * 1000000 + microsecond(a.`CreateTime`) + 6213559680000000) > 0)
}
[Fact]
public void MinValue() {
var data = new List<object>();
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 ((time_to_sec(date_format(a.`CreateTime`, '1970-1-1 %H:%i:%s.%f')) * 1000000 + microsecond(a.`CreateTime`) + 6213559680000000) > -922337203685477580)
}
[Fact]
public void MaxValue() {
var data = new List<object>();
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 ((time_to_sec(date_format(a.`CreateTime`, '1970-1-1 %H:%i:%s.%f')) * 1000000 + microsecond(a.`CreateTime`) + 6213559680000000) < 922337203685477580)
}
[Fact]
public void Days() {
var data = new List<object>();
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 ((((time_to_sec(date_format(a.`CreateTime`, '1970-1-1 %H:%i:%s.%f')) * 1000000 + microsecond(a.`CreateTime`) + 6213559680000000)) div 86400000000) = 0)
}
[Fact]
public void Hours() {
var data = new List<object>();
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 ((((time_to_sec(date_format(a.`CreateTime`, '1970-1-1 %H:%i:%s.%f')) * 1000000 + microsecond(a.`CreateTime`) + 6213559680000000)) div 3600000000) mod 24 > 0)
}
[Fact]
public void Milliseconds() {
var data = new List<object>();
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 ((((time_to_sec(date_format(a.`CreateTime`, '1970-1-1 %H:%i:%s.%f')) * 1000000 + microsecond(a.`CreateTime`) + 6213559680000000)) div 1000 mod 1000) > 0)
}
[Fact]
public void Minutes() {
var data = new List<object>();
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 ((((time_to_sec(date_format(a.`CreateTime`, '1970-1-1 %H:%i:%s.%f')) * 1000000 + microsecond(a.`CreateTime`) + 6213559680000000)) div 60000000 mod 60) > 0)
}
[Fact]
public void Seconds() {
var data = new List<object>();
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 ((((time_to_sec(date_format(a.`CreateTime`, '1970-1-1 %H:%i:%s.%f')) * 1000000 + microsecond(a.`CreateTime`) + 6213559680000000)) div 1000000 mod 60) > 0)
}
[Fact]
public void Ticks() {
var data = new List<object>();
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 ((((time_to_sec(date_format(a.`CreateTime`, '1970-1-1 %H:%i:%s.%f')) * 1000000 + microsecond(a.`CreateTime`) + 6213559680000000)) * 10) > 0)
}
[Fact]
public void TotalDays() {
var data = new List<object>();
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 ((((time_to_sec(date_format(a.`CreateTime`, '1970-1-1 %H:%i:%s.%f')) * 1000000 + microsecond(a.`CreateTime`) + 6213559680000000)) / 86400000000) > 0)
}
[Fact]
public void TotalHours() {
var data = new List<object>();
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 ((((time_to_sec(date_format(a.`CreateTime`, '1970-1-1 %H:%i:%s.%f')) * 1000000 + microsecond(a.`CreateTime`) + 6213559680000000)) / 3600000000) > 0)
}
[Fact]
public void TotalMilliseconds() {
var data = new List<object>();
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 ((((time_to_sec(date_format(a.`CreateTime`, '1970-1-1 %H:%i:%s.%f')) * 1000000 + microsecond(a.`CreateTime`) + 6213559680000000)) / 1000) > 0)
}
[Fact]
public void TotalMinutes() {
var data = new List<object>();
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 ((((time_to_sec(date_format(a.`CreateTime`, '1970-1-1 %H:%i:%s.%f')) * 1000000 + microsecond(a.`CreateTime`) + 6213559680000000)) / 60000000) > 0)
}
[Fact]
public void TotalSeconds() {
var data = new List<object>();
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 ((((time_to_sec(date_format(a.`CreateTime`, '1970-1-1 %H:%i:%s.%f')) * 1000000 + microsecond(a.`CreateTime`) + 6213559680000000)) / 1000000) > 0)
}
[Fact]
public void Add() {
var data = new List<object>();
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 (((time_to_sec(date_format(a.`CreateTime`, '1970-1-1 %H:%i:%s.%f')) * 1000000 + microsecond(a.`CreateTime`) + 6213559680000000) + (1 * 86400000000)) > 0)
}
[Fact]
public void Subtract() {
var data = new List<object>();
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 (((time_to_sec(date_format(a.`CreateTime`, '1970-1-1 %H:%i:%s.%f')) * 1000000 + microsecond(a.`CreateTime`) + 6213559680000000) - (1 * 86400000000)) > 0)
}
[Fact]
public void CompareTo() {
var data = new List<object>();
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 ((((time_to_sec(date_format(a.`CreateTime`, '1970-1-1 %H:%i:%s.%f')) * 1000000 + microsecond(a.`CreateTime`) + 6213559680000000)) - ((1 * 86400000000))) > 0)
}
[Fact]
public void this_Equals() {
var data = new List<object>();
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 (((time_to_sec(date_format(a.`CreateTime`, '1970-1-1 %H:%i:%s.%f')) * 1000000 + microsecond(a.`CreateTime`) + 6213559680000000) = (1 * 86400000000)))
}
[Fact]
public void this_ToString() {
var data = new List<object>();
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 ((time_to_sec(date_format(a.`CreateTime`, '1970-1-1 %H:%i:%s.%f')) * 1000000 + microsecond(a.`CreateTime`) + 6213559680000000)) microsecond), '%Y-%m-%d %H:%i:%s.%f') = 'ssss')
}
[Fact]
public void TimeSpan_Compare() {
var data = new List<object>();
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 ((((time_to_sec(date_format(a.`CreateTime`, '1970-1-1 %H:%i:%s.%f')) * 1000000 + microsecond(a.`CreateTime`) + 6213559680000000)) - ((1 * 86400000000))) > 0)
}
[Fact]
public void TimeSpan_Equals() {
var data = new List<object>();
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 (((time_to_sec(date_format(a.`CreateTime`, '1970-1-1 %H:%i:%s.%f')) * 1000000 + microsecond(a.`CreateTime`) + 6213559680000000) = (1 * 86400000000)))
}
[Fact]
public void TimeSpan_FromDays() {
var data = new List<object>();
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 (((time_to_sec(date_format(a.`CreateTime`, '1970-1-1 %H:%i:%s.%f')) * 1000000 + microsecond(a.`CreateTime`) + 6213559680000000) = (1 * 86400000000)))
}
[Fact]
public void TimeSpan_FromHours() {
var data = new List<object>();
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 (((time_to_sec(date_format(a.`CreateTime`, '1970-1-1 %H:%i:%s.%f')) * 1000000 + microsecond(a.`CreateTime`) + 6213559680000000) = (1 * 3600000000)))
}
[Fact]
public void TimeSpan_FromMilliseconds() {
var data = new List<object>();
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 (((time_to_sec(date_format(a.`CreateTime`, '1970-1-1 %H:%i:%s.%f')) * 1000000 + microsecond(a.`CreateTime`) + 6213559680000000) = (1 * 1000)))
}
[Fact]
public void TimeSpan_FromMinutes() {
var data = new List<object>();
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 (((time_to_sec(date_format(a.`CreateTime`, '1970-1-1 %H:%i:%s.%f')) * 1000000 + microsecond(a.`CreateTime`) + 6213559680000000) = (1 * 60000000)))
}
[Fact]
public void TimeSpan_FromSeconds() {
var data = new List<object>();
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 (((time_to_sec(date_format(a.`CreateTime`, '1970-1-1 %H:%i:%s.%f')) * 1000000 + microsecond(a.`CreateTime`) + 6213559680000000) = (1 * 1000000)))
}
[Fact]
public void TimeSpan_FromTicks() {
var data = new List<object>();
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 (((time_to_sec(date_format(a.`CreateTime`, '1970-1-1 %H:%i:%s.%f')) * 1000000 + microsecond(a.`CreateTime`) + 6213559680000000) = (1 / 10)))
}
[Fact]
public void TimeSpan_Parse() {
var data = new List<object>();
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 ((time_to_sec(date_format(a.`CreateTime`, '1970-1-1 %H:%i:%s.%f')) * 1000000 + microsecond(a.`CreateTime`) + 6213559680000000)) microsecond), '%Y-%m-%d %H:%i:%s.%f') as signed) > 0)
}
}
}

View File

@ -155,26 +155,31 @@ namespace FreeSql.Internal {
} }
internal string ExpressionLambdaToSql(Expression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName) { internal string ExpressionLambdaToSql(Expression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName) {
switch( exp.NodeType) { switch (exp.NodeType) {
case ExpressionType.Quote: return ExpressionLambdaToSql((exp as UnaryExpression)?.Operand, _tables, _selectColumnMap, tbtype, isQuoteName); case ExpressionType.Quote: return ExpressionLambdaToSql((exp as UnaryExpression)?.Operand, _tables, _selectColumnMap, tbtype, isQuoteName);
case ExpressionType.Lambda: return ExpressionLambdaToSql((exp as LambdaExpression)?.Body, _tables, _selectColumnMap, tbtype, isQuoteName); case ExpressionType.Lambda: return ExpressionLambdaToSql((exp as LambdaExpression)?.Body, _tables, _selectColumnMap, tbtype, isQuoteName);
case ExpressionType.Convert: return ExpressionLambdaToSql((exp as UnaryExpression)?.Operand, _tables, _selectColumnMap, tbtype, isQuoteName); case ExpressionType.Convert: return ExpressionLambdaToSql((exp as UnaryExpression)?.Operand, _tables, _selectColumnMap, tbtype, isQuoteName);
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.Constant: return _common.FormatSql("{0}", (exp as ConstantExpression)?.Value);
case ExpressionType.Call: case ExpressionType.Call:
var exp3 = exp as MethodCallExpression; var exp3 = exp as MethodCallExpression;
if (exp3.Object.Type.FullName == "System.String") return ExpressionLambdaToSqlCallString(exp3, _tables, _selectColumnMap, tbtype, isQuoteName); switch (exp3.Object?.Type.FullName ?? exp3.Method.DeclaringType.FullName) {
if (exp3.Object.Type.FullName == "System.Math") return ExpressionLambdaToSqlCallMath(exp3, _tables, _selectColumnMap, tbtype, isQuoteName); case "System.String": return ExpressionLambdaToSqlCallString(exp3, _tables, _selectColumnMap, tbtype, isQuoteName);
if (exp3.Object.Type.FullName == "System.DateTime" || exp3.Object.Type.GenericTypeArguments.FirstOrDefault()?.FullName == "System.DateTime") return ExpressionLambdaToSqlCallDateTime(exp3, _tables, _selectColumnMap, tbtype, isQuoteName); case "System.Math": return ExpressionLambdaToSqlCallMath(exp3, _tables, _selectColumnMap, tbtype, isQuoteName);
if (exp3.Object.Type.FullName == "System.TimeSpan" || exp3.Object.Type.GenericTypeArguments.FirstOrDefault()?.FullName == "System.TimeSpan") return ExpressionLambdaToSqlCallTimeSpan(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);
}
throw new Exception($"MySqlExpression 未现实函数表达式 {exp3} 解析"); throw new Exception($"MySqlExpression 未现实函数表达式 {exp3} 解析");
case ExpressionType.MemberAccess: case ExpressionType.MemberAccess:
var exp4 = exp as MemberExpression; var exp4 = exp as MemberExpression;
if (exp4.Expression != null && exp4.Expression.Type.FullName.StartsWith("System.Nullable`1[")) return ExpressionLambdaToSql(exp4.Expression, _tables, _selectColumnMap, tbtype, isQuoteName);
var extRet = ""; var extRet = "";
if (exp4.Expression == null && exp4.Type.FullName == "System.DateTime" && exp4.Member.Name == "Now") return ExpressionLambdaToSqlMemberAccessDateTime(exp4, _tables, _selectColumnMap, tbtype, isQuoteName); switch (exp4.Expression?.Type.FullName ?? exp4.Type.FullName) {
if (exp4.Expression.Type.FullName == "System.String") extRet = ExpressionLambdaToSqlMemberAccessString(exp4, _tables, _selectColumnMap, tbtype, isQuoteName); case "System.String": extRet = ExpressionLambdaToSqlMemberAccessString(exp4, _tables, _selectColumnMap, tbtype, isQuoteName); break;
else if (exp4.Expression.Type.FullName == "System.Math") extRet = ExpressionLambdaToSqlMemberAccessMath(exp4, _tables, _selectColumnMap, tbtype, isQuoteName); case "System.DateTime": extRet = ExpressionLambdaToSqlMemberAccessDateTime(exp4, _tables, _selectColumnMap, tbtype, isQuoteName); break;
else if (exp4.Expression.Type.FullName == "System.DateTime" || exp4.Type.GenericTypeArguments.FirstOrDefault()?.FullName == "System.DateTime") extRet = ExpressionLambdaToSqlMemberAccessDateTime(exp4, _tables, _selectColumnMap, tbtype, isQuoteName); case "System.TimeSpan": extRet = ExpressionLambdaToSqlMemberAccessTimeSpan(exp4, _tables, _selectColumnMap, tbtype, isQuoteName); break;
else if (exp4.Expression.Type.FullName == "System.TimeSpan" || exp4.Type.GenericTypeArguments.FirstOrDefault()?.FullName == "System.TimeSpan") extRet = ExpressionLambdaToSqlMemberAccessTimeSpan(exp4, _tables, _selectColumnMap, tbtype, isQuoteName); }
if (string.IsNullOrEmpty(extRet) == false) return extRet; if (string.IsNullOrEmpty(extRet) == false) return extRet;
var expStack = new Stack<Expression>(); var expStack = new Stack<Expression>();
@ -272,9 +277,14 @@ namespace FreeSql.Internal {
if (isQuoteName) name2 = _common.QuoteSqlName(name2); if (isQuoteName) name2 = _common.QuoteSqlName(name2);
return $"{alias2}.{name2}"; return $"{alias2}.{name2}";
} }
if (dicExpressionOperator.TryGetValue(exp.NodeType, out var tryoper) == false) return "";
var expBinary = exp as BinaryExpression; var expBinary = exp as BinaryExpression;
if (expBinary == null) return ""; if (expBinary == null) return "";
if (expBinary.NodeType == ExpressionType.Coalesce) {
return _common.IsNull(
ExpressionLambdaToSql(expBinary.Left, _tables, _selectColumnMap, tbtype, isQuoteName),
ExpressionLambdaToSql(expBinary.Right, _tables, _selectColumnMap, tbtype, isQuoteName));
}
if (dicExpressionOperator.TryGetValue(expBinary.NodeType, out var tryoper) == false) return "";
var left = ExpressionLambdaToSql(expBinary.Left, _tables, _selectColumnMap, tbtype, isQuoteName); var left = ExpressionLambdaToSql(expBinary.Left, _tables, _selectColumnMap, tbtype, isQuoteName);
var right = ExpressionLambdaToSql(expBinary.Right, _tables, _selectColumnMap, tbtype, isQuoteName); var right = ExpressionLambdaToSql(expBinary.Right, _tables, _selectColumnMap, tbtype, isQuoteName);
if (left == "NULL") { if (left == "NULL") {
@ -288,7 +298,6 @@ namespace FreeSql.Internal {
} }
internal abstract string ExpressionLambdaToSqlMemberAccessString(MemberExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName); internal abstract string ExpressionLambdaToSqlMemberAccessString(MemberExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName);
internal abstract string ExpressionLambdaToSqlMemberAccessMath(MemberExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName);
internal abstract string ExpressionLambdaToSqlMemberAccessDateTime(MemberExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName); internal abstract string ExpressionLambdaToSqlMemberAccessDateTime(MemberExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName);
internal abstract string ExpressionLambdaToSqlMemberAccessTimeSpan(MemberExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName); internal abstract string ExpressionLambdaToSqlMemberAccessTimeSpan(MemberExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName);
internal abstract string ExpressionLambdaToSqlCallString(MethodCallExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName); internal abstract string ExpressionLambdaToSqlCallString(MethodCallExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName);

View File

@ -38,13 +38,11 @@ namespace FreeSql.MySql {
return string.Concat("'", ((DateTime)param).ToString("yyyy-MM-dd HH:mm:ss"), "'"); return string.Concat("'", ((DateTime)param).ToString("yyyy-MM-dd HH:mm:ss"), "'");
else if (param is DateTime?) else if (param is DateTime?)
return string.Concat("'", (param as DateTime?).Value.ToString("yyyy-MM-dd HH:mm:ss"), "'"); return string.Concat("'", (param as DateTime?).Value.ToString("yyyy-MM-dd HH:mm:ss"), "'");
else if (param is TimeSpan) { else if (param is TimeSpan)
var ts = (TimeSpan)param; return ((TimeSpan)param).Ticks / 10;
return string.Concat("'", ts.Ticks > 0 ? "" : "-", ts.TotalHours, dt1970.AddTicks(Math.Abs(ts.Ticks)).ToString(":mm:ss.fff"), "'"); else if (param is TimeSpan?)
} else if (param is TimeSpan) { return (param as TimeSpan?).Value.Ticks / 10;
var ts = (param as TimeSpan?).Value; else if (param is IEnumerable) {
return string.Concat("'", ts.Ticks > 0 ? "" : "-", ts.TotalHours, dt1970.AddTicks(Math.Abs(ts.Ticks)).ToString(":mm:ss.fff"), "'");
} else if (param is IEnumerable) {
var sb = new StringBuilder(); var sb = new StringBuilder();
var ie = param as IEnumerable; var ie = param as IEnumerable;
foreach (var z in ie) sb.Append(",").Append(AddslashesProcessParam(z)); foreach (var z in ie) sb.Append(",").Append(AddslashesProcessParam(z));

View File

@ -11,25 +11,33 @@ namespace FreeSql.MySql {
public MySqlExpression(CommonUtils common) : base(common) { } public MySqlExpression(CommonUtils common) : base(common) { }
internal override string ExpressionLambdaToSqlMemberAccessString(MemberExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName) { internal override string ExpressionLambdaToSqlMemberAccessString(MemberExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName) {
if (exp.Expression == null) {
switch (exp.Member.Name) {
case "Empty": return "''";
}
return null;
}
var left = ExpressionLambdaToSql(exp.Expression, _tables, _selectColumnMap, tbtype, isQuoteName); var left = ExpressionLambdaToSql(exp.Expression, _tables, _selectColumnMap, tbtype, isQuoteName);
switch (exp.Member.Name) { switch (exp.Member.Name) {
case "Length": return $"char_length({left})"; case "Length": return $"char_length({left})";
} }
return null; return null;
} }
internal override string ExpressionLambdaToSqlMemberAccessMath(MemberExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName) {
var left = ExpressionLambdaToSql(exp.Expression, _tables, _selectColumnMap, tbtype, isQuoteName);
switch (exp.Member.Name) {
case "PI": return $"pi()";
}
return null;
}
internal override string ExpressionLambdaToSqlMemberAccessDateTime(MemberExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName) { internal override string ExpressionLambdaToSqlMemberAccessDateTime(MemberExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName) {
if (exp.Expression == null && exp.Member.Name == "Now") return "now()"; if (exp.Expression == null) {
switch (exp.Member.Name) {
case "Now": return "now()";
case "UtcNow": return "utc_timestamp()";
case "Today": return "curdate()";
case "MinValue": return "cast('0001/1/1 0:00:00' as datetime)";
case "MaxValue": return "cast('9999/12/31 23:59:59' as datetime)";
}
return null;
}
var left = ExpressionLambdaToSql(exp.Expression, _tables, _selectColumnMap, tbtype, isQuoteName); var left = ExpressionLambdaToSql(exp.Expression, _tables, _selectColumnMap, tbtype, isQuoteName);
switch (exp.Member.Name) { switch (exp.Member.Name) {
case "Date": return $"cast(date_format({left}, '%Y-%m-%d') as datetime)";
case "TimeOfDay": return $"(time_to_sec(date_format({left}, '1970-1-1 %H:%i:%s.%f')) * 1000000 + microsecond({left}) + 6213559680000000)";
case "DayOfWeek": return $"(dayofweek({left}) - 1)"; case "DayOfWeek": return $"(dayofweek({left}) - 1)";
case "Day": return $"dayofmonth({left})"; case "Day": return $"dayofmonth({left})";
case "DayOfYear": return $"dayofyear({left})"; case "DayOfYear": return $"dayofyear({left})";
@ -39,28 +47,36 @@ namespace FreeSql.MySql {
case "Minute": return $"minute({left})"; case "Minute": return $"minute({left})";
case "Second": return $"second({left})"; case "Second": return $"second({left})";
case "Millisecond": return $"floor(microsecond({left}) / 1000)"; case "Millisecond": return $"floor(microsecond({left}) / 1000)";
case "Ticks": return $"(time_to_sec({left}) * 10000000 + 621355968000000000)"; case "Ticks": return $"(time_to_sec({left}) * 10000000 + microsecond({left}) * 10 + 62135596800000000)";
}
return null;
}
internal override string ExpressionLambdaToSqlMemberAccessTimeSpan(MemberExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName) {
if (exp.Expression == null) {
switch (exp.Member.Name) {
case "Zero": return "0";
case "MinValue": return "-922337203685477580"; //微秒 Ticks / 10
case "MaxValue": return "922337203685477580";
}
return null;
}
var left = ExpressionLambdaToSql(exp.Expression, _tables, _selectColumnMap, tbtype, isQuoteName);
switch (exp.Member.Name) {
case "Days": return $"(({left}) div {(long)1000000 * 60 * 60 * 24})";
case "Hours": return $"(({left}) div {(long)1000000 * 60 * 60} mod 24)";
case "Milliseconds": return $"(({left}) div 1000 mod 1000)";
case "Minutes": return $"(({left}) div {(long)1000000 * 60} mod 60)";
case "Seconds": return $"(({left}) div 1000000 mod 60)";
case "Ticks": return $"(({left}) * 10)";
case "TotalDays": return $"(({left}) / {(long)1000000 * 60 * 60 * 24})";
case "TotalHours": return $"(({left}) / {(long)1000000 * 60 * 60})";
case "TotalMilliseconds": return $"(({left}) / 1000)";
case "TotalMinutes": return $"(({left}) / {(long)1000000 * 60})";
case "TotalSeconds": return $"(({left}) / 1000000)";
} }
return null; return null;
} }
internal override string ExpressionLambdaToSqlMemberAccessTimeSpan(MemberExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName) {
var left = ExpressionLambdaToSql(exp.Expression, _tables, _selectColumnMap, tbtype, isQuoteName);
switch (exp.Member.Name) {
case "Days": return $"floor(time_to_sec({left}) / {60 * 60 * 24})";
case "Hours": return $"extract(hour from {left})";
case "Milliseconds": return $"floor(extract(microsecond from {left}) / 1000)";
case "Minutes": return $"extract(minute from {left})";
case "Seconds": return $"extract(second from {left})";
case "Ticks": return $"(time_to_sec({left}) * 10000000 + extract(microsecond from {left}) * 10)";
case "TotalDays": return $"floor(extract(hour from {left}) / 24)";
case "TotalHours": return $"floor(time_to_sec({left}) / {60 * 60})";
case "TotalMilliseconds": return $"(time_to_sec({left}) * 1000 + floor(extract(microsecond from {left}) / 1000))";
case "TotalMinutes": return $"floor(time_to_sec({left}) / 60)";
case "TotalSeconds": return $"time_to_sec({left})";
}
return null;
}
internal override string ExpressionLambdaToSqlCallString(MethodCallExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName) { internal override string ExpressionLambdaToSqlCallString(MethodCallExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName) {
var left = ExpressionLambdaToSql(exp.Object, _tables, _selectColumnMap, tbtype, isQuoteName); var left = ExpressionLambdaToSql(exp.Object, _tables, _selectColumnMap, tbtype, isQuoteName);
switch (exp.Method.Name) { switch (exp.Method.Name) {
@ -69,8 +85,8 @@ namespace FreeSql.MySql {
case "Contains": case "Contains":
var args0Value = ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName); var args0Value = ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName);
if (args0Value == "NULL") return $"({left}) IS NULL"; if (args0Value == "NULL") return $"({left}) IS NULL";
if (exp.Method.Name == "StartsWith") return $"({left}) LIKE {(args0Value.StartsWith("'") ? args0Value.Insert(1, "%") : $"concat('%', {args0Value})")}"; if (exp.Method.Name == "StartsWith") return $"({left}) LIKE {(args0Value.EndsWith("'") ? args0Value.Insert(args0Value.Length - 1, "%") : $"concat({args0Value}, '%')")}";
if (exp.Method.Name == "EndsWith") return $"({left}) LIKE {(args0Value.EndsWith("'") ? args0Value.Insert(args0Value.Length - 1, "%") : $"concat({args0Value}, '%')")}"; if (exp.Method.Name == "EndsWith") return $"({left}) LIKE {(args0Value.StartsWith("'") ? args0Value.Insert(1, "%") : $"concat('%', {args0Value})")}";
if (args0Value.StartsWith("'") && args0Value.EndsWith("'")) return $"({left}) LIKE {args0Value.Insert(1, "%").Insert(args0Value.Length, "%")}"; if (args0Value.StartsWith("'") && args0Value.EndsWith("'")) return $"({left}) LIKE {args0Value.Insert(1, "%").Insert(args0Value.Length, "%")}";
return $"({left}) LIKE concat('%', {args0Value}, '%')"; return $"({left}) LIKE concat('%', {args0Value}, '%')";
case "ToLower": return $"lower({left})"; case "ToLower": return $"lower({left})";
@ -119,10 +135,10 @@ namespace FreeSql.MySql {
return left; return left;
case "Replace": return $"replace({left}, {ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)}, {ExpressionLambdaToSql(exp.Arguments[1], _tables, _selectColumnMap, tbtype, isQuoteName)})"; case "Replace": return $"replace({left}, {ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)}, {ExpressionLambdaToSql(exp.Arguments[1], _tables, _selectColumnMap, tbtype, isQuoteName)})";
case "CompareTo": return $"strcmp({left}, {ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})"; case "CompareTo": return $"strcmp({left}, {ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
case "Equals": return $"({left} = {ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
} }
throw new Exception($"MySqlExpression 未现实函数表达式 {exp} 解析"); throw new Exception($"MySqlExpression 未现实函数表达式 {exp} 解析");
} }
internal override string ExpressionLambdaToSqlCallMath(MethodCallExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName) { internal override string ExpressionLambdaToSqlCallMath(MethodCallExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName) {
switch (exp.Method.Name) { switch (exp.Method.Name) {
case "Abs": return $"abs({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})"; case "Abs": return $"abs({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
@ -148,37 +164,77 @@ namespace FreeSql.MySql {
} }
throw new Exception($"MySqlExpression 未现实函数表达式 {exp} 解析"); throw new Exception($"MySqlExpression 未现实函数表达式 {exp} 解析");
} }
internal override string ExpressionLambdaToSqlCallDateTime(MethodCallExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName) { internal override string ExpressionLambdaToSqlCallDateTime(MethodCallExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _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 "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)";
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 left = ExpressionLambdaToSql(exp.Object, _tables, _selectColumnMap, tbtype, isQuoteName);
var args1 = ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName); var args1 = exp.Arguments.Count == 0 ? null : ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName);
switch (exp.Method.Name) { switch (exp.Method.Name) {
case "Add": return $"date_add({left}, interval (time_to_sec({args1}) * 1000000 + extract(microsecond from {args1})) microsecond)"; case "Add": return $"date_add({left}, interval ({args1}) microsecond)";
case "AddDays": return $"date_add({left}, interval {args1} day)"; case "AddDays": return $"date_add({left}, interval ({args1}) day)";
case "AddHours": return $"date_add({left}, interval {args1} hour)"; case "AddHours": return $"date_add({left}, interval ({args1}) hour)";
case "AddMilliseconds": return $"date_add({left}, interval {args1} microsecond)"; case "AddMilliseconds": return $"date_add({left}, interval ({args1}) * 1000 microsecond)";
case "AddMinutes": return $"date_add({left}, interval {args1} minute)"; case "AddMinutes": return $"date_add({left}, interval ({args1}) minute)";
case "AddMonths": return $"date_add({left}, interval {args1} month)"; case "AddMonths": return $"date_add({left}, interval ({args1}) month)";
case "AddSeconds": return $"date_add({left}, interval {args1} second)"; case "AddSeconds": return $"date_add({left}, interval ({args1}) second)";
case "AddTicks": return $"date_add({left}, interval ({args1}) / 10 microsecond)"; case "AddTicks": return $"date_add({left}, interval ({args1}) / 10 microsecond)";
case "AddYears": return $"date_add({left}, interval {args1} year)"; case "AddYears": return $"date_add({left}, interval ({args1}) year)";
case "Subtract": case "Subtract":
if (exp.Arguments[0].Type.FullName == "System.DateTime" || exp.Arguments[0].Type.GenericTypeArguments.FirstOrDefault()?.FullName == "System.DateTime") if (exp.Arguments[0].Type.FullName == "System.DateTime" || exp.Arguments[0].Type.GenericTypeArguments.FirstOrDefault()?.FullName == "System.DateTime")
return $"({left} - {args1})"; return $"((time_to_sec({left}) - time_to_sec({args1})) * 1000000 + microsecond({left}) - microsecond({args1}))";
if (exp.Arguments[0].Type.FullName == "System.TimeSpan" || exp.Arguments[0].Type.GenericTypeArguments.FirstOrDefault()?.FullName == "System.TimeSpan") if (exp.Arguments[0].Type.FullName == "System.TimeSpan" || exp.Arguments[0].Type.GenericTypeArguments.FirstOrDefault()?.FullName == "System.TimeSpan")
return $"date_sub({left}, interval (time_to_sec({args1}) * 1000000 + extract(microsecond from {args1})) microsecond)"; return $"date_sub({left}, interval ({args1}) microsecond)";
break; 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} 解析"); throw new Exception($"MySqlExpression 未现实函数表达式 {exp} 解析");
} }
internal override string ExpressionLambdaToSqlCallTimeSpan(MethodCallExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName) { internal override string ExpressionLambdaToSqlCallTimeSpan(MethodCallExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _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 "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 "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 left = ExpressionLambdaToSql(exp.Object, _tables, _selectColumnMap, tbtype, isQuoteName);
var args1 = ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName); var args1 = exp.Arguments.Count == 0 ? null : ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName);
switch (exp.Method.Name) { switch (exp.Method.Name) {
case "Add": return $"(date_add('1970-1-1', interval (time_to_sec({left}) * 1000000 + extract(microsecond from {left}) + time_to_sec({args1}) * 1000000 + extract(microsecond from {args1})) microsecond)) microsecond) - date_add('1970-1-1', interval 0 microsecond) second))"; case "Add": return $"({left} + {args1})";
case "Subtract": return $"(date_add('1970-1-1', interval (time_to_sec({left}) * 1000000 + extract(microsecond from {left}) - time_to_sec({args1}) * 1000000 - extract(microsecond from {args1})) microsecond) - date_add('1970-1-1', interval 0 second))"; 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 $"date_format(date_add(cast('0001/1/1 0:00:00' as datetime), interval ({left}) microsecond), '%Y-%m-%d %H:%i:%s.%f')";
} }
throw new Exception($"MySqlExpression 未现实函数表达式 {exp} 解析"); throw new Exception($"MySqlExpression 未现实函数表达式 {exp} 解析");
} }
} }
} }

View File

@ -17,17 +17,14 @@ namespace FreeSql.PostgreSQL {
} }
throw new Exception($"PostgreSQLExpression 未现实函数表达式 {exp} 解析"); throw new Exception($"PostgreSQLExpression 未现实函数表达式 {exp} 解析");
} }
internal override string ExpressionLambdaToSqlMemberAccessMath(MemberExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName) {
var left = ExpressionLambdaToSql(exp.Expression, _tables, _selectColumnMap, tbtype, isQuoteName);
switch (exp.Member.Name) {
case "PI": return $"pi()";
}
throw new Exception($"PostgreSQLExpression 未现实函数表达式 {exp} 解析");
}
internal override string ExpressionLambdaToSqlMemberAccessDateTime(MemberExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName) { internal override string ExpressionLambdaToSqlMemberAccessDateTime(MemberExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName) {
if (exp.Expression == null && exp.Member.Name == "Now") return "current_timestamp"; if (exp.Expression == null) {
switch (exp.Member.Name) {
case "Now": return "current_timestamp";
case "UtcNow": return "(current_timestamp at time zone 'UTC')";
case "Today": return "current_date";
}
}
var left = ExpressionLambdaToSql(exp.Expression, _tables, _selectColumnMap, tbtype, isQuoteName); var left = ExpressionLambdaToSql(exp.Expression, _tables, _selectColumnMap, tbtype, isQuoteName);
switch (exp.Member.Name) { switch (exp.Member.Name) {
case "DayOfWeek": return $"extract(dow from ({left})::timestamp)"; case "DayOfWeek": return $"extract(dow from ({left})::timestamp)";
@ -43,7 +40,6 @@ namespace FreeSql.PostgreSQL {
} }
throw new Exception($"PostgreSQLExpression 未现实函数表达式 {exp} 解析"); throw new Exception($"PostgreSQLExpression 未现实函数表达式 {exp} 解析");
} }
internal override string ExpressionLambdaToSqlMemberAccessTimeSpan(MemberExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName) { internal override string ExpressionLambdaToSqlMemberAccessTimeSpan(MemberExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName) {
var left = ExpressionLambdaToSql(exp.Expression, _tables, _selectColumnMap, tbtype, isQuoteName); var left = ExpressionLambdaToSql(exp.Expression, _tables, _selectColumnMap, tbtype, isQuoteName);
switch (exp.Member.Name) { switch (exp.Member.Name) {
@ -61,6 +57,7 @@ namespace FreeSql.PostgreSQL {
} }
throw new Exception($"PostgreSQLExpression 未现实函数表达式 {exp} 解析"); throw new Exception($"PostgreSQLExpression 未现实函数表达式 {exp} 解析");
} }
internal override string ExpressionLambdaToSqlCallString(MethodCallExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName) { internal override string ExpressionLambdaToSqlCallString(MethodCallExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName) {
var left = ExpressionLambdaToSql(exp.Object, _tables, _selectColumnMap, tbtype, isQuoteName); var left = ExpressionLambdaToSql(exp.Object, _tables, _selectColumnMap, tbtype, isQuoteName);
switch (exp.Method.Name) { switch (exp.Method.Name) {
@ -74,8 +71,8 @@ namespace FreeSql.PostgreSQL {
if (exp.Arguments[1].Type == typeof(bool) || if (exp.Arguments[1].Type == typeof(bool) ||
exp.Arguments[1].Type == typeof(StringComparison) && ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName).Contains("IgnoreCase")) likeOpt = "ILIKE"; exp.Arguments[1].Type == typeof(StringComparison) && ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName).Contains("IgnoreCase")) likeOpt = "ILIKE";
} }
if (exp.Method.Name == "StartsWith") return $"({left}) {likeOpt} {(args0Value.StartsWith("'") ? args0Value.Insert(1, "%") : $"('%' || ({args0Value})::varchar)")}"; if (exp.Method.Name == "StartsWith") return $"({left}) {likeOpt} {(args0Value.EndsWith("'") ? args0Value.Insert(args0Value.Length - 1, "%") : $"(({args0Value})::varchar || '%')")}";
if (exp.Method.Name == "EndsWith") return $"({left}) {likeOpt} {(args0Value.EndsWith("'") ? args0Value.Insert(args0Value.Length - 1, "%") : $"(({args0Value})::varchar || '%')")}"; if (exp.Method.Name == "EndsWith") return $"({left}) {likeOpt} {(args0Value.StartsWith("'") ? args0Value.Insert(1, "%") : $"('%' || ({args0Value})::varchar)")}";
if (args0Value.StartsWith("'") && args0Value.EndsWith("'")) return $"({left}) {likeOpt} {args0Value.Insert(1, "%").Insert(args0Value.Length, "%")}"; if (args0Value.StartsWith("'") && args0Value.EndsWith("'")) return $"({left}) {likeOpt} {args0Value.Insert(1, "%").Insert(args0Value.Length, "%")}";
return $"({left}) {likeOpt} ('%' || ({args0Value})::varchar || '%')"; return $"({left}) {likeOpt} ('%' || ({args0Value})::varchar || '%')";
case "ToLower": return $"lower({left})"; case "ToLower": return $"lower({left})";
@ -123,7 +120,6 @@ namespace FreeSql.PostgreSQL {
} }
throw new Exception($"PostgreSQLExpression 未现实函数表达式 {exp} 解析"); throw new Exception($"PostgreSQLExpression 未现实函数表达式 {exp} 解析");
} }
internal override string ExpressionLambdaToSqlCallMath(MethodCallExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName) { internal override string ExpressionLambdaToSqlCallMath(MethodCallExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName) {
switch (exp.Method.Name) { switch (exp.Method.Name) {
case "Abs": return $"abs({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})"; case "Abs": return $"abs({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
@ -149,7 +145,6 @@ namespace FreeSql.PostgreSQL {
} }
throw new Exception($"PostgreSQLExpression 未现实函数表达式 {exp} 解析"); throw new Exception($"PostgreSQLExpression 未现实函数表达式 {exp} 解析");
} }
internal override string ExpressionLambdaToSqlCallDateTime(MethodCallExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName) { internal override string ExpressionLambdaToSqlCallDateTime(MethodCallExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName) {
var left = ExpressionLambdaToSql(exp.Object, _tables, _selectColumnMap, tbtype, isQuoteName); var left = ExpressionLambdaToSql(exp.Object, _tables, _selectColumnMap, tbtype, isQuoteName);
var args1 = ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName); var args1 = ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName);

View File

@ -11,23 +11,27 @@ namespace FreeSql.SqlServer {
public SqlServerExpression(CommonUtils common) : base(common) { } public SqlServerExpression(CommonUtils common) : base(common) { }
internal override string ExpressionLambdaToSqlMemberAccessString(MemberExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName) { internal override string ExpressionLambdaToSqlMemberAccessString(MemberExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName) {
if (exp.Expression == null) {
switch (exp.Member.Name) {
case "Empty": return "''";
}
return null;
}
var left = ExpressionLambdaToSql(exp.Expression, _tables, _selectColumnMap, tbtype, isQuoteName); var left = ExpressionLambdaToSql(exp.Expression, _tables, _selectColumnMap, tbtype, isQuoteName);
switch (exp.Member.Name) { switch (exp.Member.Name) {
case "Length": return $"len({left})"; case "Length": return $"len({left})";
} }
throw new Exception($"SqlServerExpression 未现实函数表达式 {exp} 解析"); throw new Exception($"SqlServerExpression 未现实函数表达式 {exp} 解析");
} }
internal override string ExpressionLambdaToSqlMemberAccessMath(MemberExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName) {
var left = ExpressionLambdaToSql(exp.Expression, _tables, _selectColumnMap, tbtype, isQuoteName);
switch (exp.Member.Name) {
case "PI": return $"pi()";
}
throw new Exception($"SqlServerExpression 未现实函数表达式 {exp} 解析");
}
internal override string ExpressionLambdaToSqlMemberAccessDateTime(MemberExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName) { internal override string ExpressionLambdaToSqlMemberAccessDateTime(MemberExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName) {
if (exp.Expression == null && exp.Member.Name == "Now") return "getdate()"; if (exp.Expression == null) {
switch (exp.Member.Name) {
case "Now": return "getdate()";
case "UtcNow": return "getutcdate()";
case "Today": return "cast(convert(char(10),getdate(),120) as date)()";
}
return null;
}
var left = ExpressionLambdaToSql(exp.Expression, _tables, _selectColumnMap, tbtype, isQuoteName); var left = ExpressionLambdaToSql(exp.Expression, _tables, _selectColumnMap, tbtype, isQuoteName);
switch (exp.Member.Name) { switch (exp.Member.Name) {
case "DayOfWeek": return $"(datepart(weekday, {left}) - 1)"; case "DayOfWeek": return $"(datepart(weekday, {left}) - 1)";
@ -43,7 +47,6 @@ namespace FreeSql.SqlServer {
} }
throw new Exception($"SqlServerExpression 未现实函数表达式 {exp} 解析"); throw new Exception($"SqlServerExpression 未现实函数表达式 {exp} 解析");
} }
internal override string ExpressionLambdaToSqlMemberAccessTimeSpan(MemberExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName) { internal override string ExpressionLambdaToSqlMemberAccessTimeSpan(MemberExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName) {
var left = ExpressionLambdaToSql(exp.Expression, _tables, _selectColumnMap, tbtype, isQuoteName); var left = ExpressionLambdaToSql(exp.Expression, _tables, _selectColumnMap, tbtype, isQuoteName);
switch (exp.Member.Name) { switch (exp.Member.Name) {
@ -61,6 +64,7 @@ namespace FreeSql.SqlServer {
} }
throw new Exception($"SqlServerExpression 未现实函数表达式 {exp} 解析"); throw new Exception($"SqlServerExpression 未现实函数表达式 {exp} 解析");
} }
internal override string ExpressionLambdaToSqlCallString(MethodCallExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName) { internal override string ExpressionLambdaToSqlCallString(MethodCallExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName) {
var left = ExpressionLambdaToSql(exp.Object, _tables, _selectColumnMap, tbtype, isQuoteName); var left = ExpressionLambdaToSql(exp.Object, _tables, _selectColumnMap, tbtype, isQuoteName);
switch (exp.Method.Name) { switch (exp.Method.Name) {
@ -69,8 +73,8 @@ namespace FreeSql.SqlServer {
case "Contains": case "Contains":
var args0Value = ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName); var args0Value = ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName);
if (args0Value == "NULL") return $"({left}) IS NULL"; if (args0Value == "NULL") return $"({left}) IS NULL";
if (exp.Method.Name == "StartsWith") 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.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, "%")}"; 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 "ToLower": return $"lower({left})";
@ -99,7 +103,6 @@ namespace FreeSql.SqlServer {
} }
throw new Exception($"SqlServerExpression 未现实函数表达式 {exp} 解析"); throw new Exception($"SqlServerExpression 未现实函数表达式 {exp} 解析");
} }
internal override string ExpressionLambdaToSqlCallMath(MethodCallExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName) { internal override string ExpressionLambdaToSqlCallMath(MethodCallExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName) {
switch (exp.Method.Name) { switch (exp.Method.Name) {
case "Abs": return $"abs({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})"; case "Abs": return $"abs({ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName)})";
@ -125,7 +128,6 @@ namespace FreeSql.SqlServer {
} }
throw new Exception($"SqlServerExpression 未现实函数表达式 {exp} 解析"); throw new Exception($"SqlServerExpression 未现实函数表达式 {exp} 解析");
} }
internal override string ExpressionLambdaToSqlCallDateTime(MethodCallExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName) { internal override string ExpressionLambdaToSqlCallDateTime(MethodCallExpression exp, List<SelectTableInfo> _tables, List<SelectColumnInfo> _selectColumnMap, SelectTableInfoType tbtype, bool isQuoteName) {
var left = ExpressionLambdaToSql(exp.Object, _tables, _selectColumnMap, tbtype, isQuoteName); var left = ExpressionLambdaToSql(exp.Object, _tables, _selectColumnMap, tbtype, isQuoteName);
var args1 = ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName); var args1 = ExpressionLambdaToSql(exp.Arguments[0], _tables, _selectColumnMap, tbtype, isQuoteName);

129
readme.md
View File

@ -8,6 +8,7 @@ https://github.com/2881099/FreeSql/blob/master/Docs/generator.md 默认提供了
* [Delete 删除数据](Docs/delete.md) * [Delete 删除数据](Docs/delete.md)
* [Select 查询数据](Docs/select.md) * [Select 查询数据](Docs/select.md)
* [Expression 表达式函数](Docs/expression.md)
* [CodeFirst 快速开发](Docs/codefirst.md) * [CodeFirst 快速开发](Docs/codefirst.md)
* [DbFirst 快速开发](Docs/dbfirst.md) * [DbFirst 快速开发](Docs/dbfirst.md)
* [DbFirst 生成器](Docs/generator.md) * [DbFirst 生成器](Docs/generator.md)
@ -118,13 +119,131 @@ sql = select.LeftJoin("TestTypeInfo b on b.Guid = a.TestTypeInfoGuid and b.Name
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 FROM `tb_topic` a LEFT JOIN TestTypeInfo b on b.Guid = a.TestTypeInfoGuid and b.Name = ?bname //SELECT a.`Id` as1, a.`Clicks` as2, a.`TestTypeInfoGuid` as3, a.`Title` as4, a.`CreateTime` as5 FROM `tb_topic` a LEFT JOIN TestTypeInfo b on b.Guid = a.TestTypeInfoGuid and b.Name = ?bname
``` ```
### 表达式函数支持 # 表达式函数
| 表达式 | MySql | SqlServer | PostgreSQL | 功能说明 |
| - | - | - | - | - |
| (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 | 减
| a * b | a * b | a * b | a * b | 乘
| a / b | a / b | a / b | a / b | 乘
| a % b | a mod b | a mod b | a mod b | 模
#### String 对象方法 > 等等...
StartsWith, EndsWith, Contains, ToLower, ToUpper, Substring, Length, IndexOf, PadLeft, PadRight, Trim, TrimStart, TrimEnd, Replace, CompareTo
### 字符串对象
| 表达式 | MySql | SqlServer | PostgreSQL | 功能说明 |
| - | - | - | - | - |
| string.Empty | '' | '' | '' | 空字符串表示 |
| a.CompareTo(b) | a - b | - | - | 比较a和b大小 |
| a.Contains('b') | a like '%b%' | - | - | a是否包含b |
| a.EndsWith('b') | a like '%b' | - | - | a尾部是否包含b |
| a.IndexOf(b) | locate(a, b) - 1 | - | - | 查找a中出现b的位置 |
| a.Length | char_length(a) | - | - | 返回a的字符串长度 |
| a.PadLeft(b, c) | lpad(a, b, c) | - | - | 在a的左侧充字符c直到字符串长度大于b |
| a.PadRight(b, c) | rpad(a, b, c) | - | - | 在a的右侧充字符c直到字符串长度大于b |
| a.Replace(b, c) | replace(a, b, c) | - | - | 将a中字符串b替换成c |
| a.StartsWith('b') | a like 'b%' | - | - | a头部是否包含b |
| a.Substring(b, c) | substr(a, b, c) | - | - | 截取a中位置b到c的内容 |
| a.ToLower | lower(a) | - | - | 转小写 |
| a.ToUpper | upper(a) | - | - | 转大写 |
| a.Trim | trim(a) | - | - | 移除两边字符 |
| a.TrimEnd | rtrim(a) | - | - | 移除左侧指定字符 |
| a.TrimStart | ltrim(a) | - | - | 移除右侧指定字符 |
### 日期对象
| 表达式 | MySql | SqlServer | PostgreSQL | 功能说明 |
| - | - | - | - | - |
| DateTime.Now | now() | - | - | 取本地时间 |
| DateTime.UtcNow | utc_timestamp() | - | - | 取UTC时间 |
| DateTime.Today | curdate | - | - | 取本地时间,日期部分 |
| DateTime.MaxValue | cast('9999/12/31 23:59:59' as datetime) | - | - | 最大时间 |
| DateTime.MinValue | cast('0001/1/1 0:00:00' as datetime) | - | - | 最小时间 |
| DateTime.Compare(a, b) | a - b | - | - | 比较a和b的大小 |
| DateTime.DaysInMonth(a, b) | dayofmonth(last_day(concat(a, '-', b, '-1'))) | - | - | 取指定年月份的总天数 |
| DateTime.Equals(a, b) | a = b | - | - | 比较a和b相等 |
| DateTime.IsLeapYear(a) | a%4=0 and a%100<>0 or a%400=0 | - | - | 判断闰年 |
| DateTime.Parse(a) | cast(a as datetime) | - | - | 转换日期类型 |
| a.Add(b) | date_add(a, interval b microsecond) | - | - | 增加TimeSpan值 |
| a.AddDays(b) | date_add(a, interval b day) | - | - | 增加天数 |
| a.AddHours(b) | date_add(a, interval b hour) | - | - | 增加小时 |
| a.AddMilliseconds(b) | date_add(a, interval b*1000 microsecond) | - | - | 增加毫秒 |
| a.AddMinutes(b) | date_add(a, interval b minute) | - | - | 增加分钟 |
| a.AddMonths(b) | date_add(a, interval b month) | - | - | 增加月 |
| a.AddSeconds(b) | date_add(a, interval b second) | - | - | 增加秒 |
| a.AddTicks(b) | date_add(a, interval b/10 microsecond) | - | - | 增加刻度微秒的1/10 |
| a.AddYears(b) | date_add(a, interval b year) | - | - | 增加年 |
| a.Date | cast(date_format(a, '%Y-%m-%d') as datetime) | - | - | 获取a的日期部分 |
| a.Day | dayofmonth(a) | - | - | 获取a在月的第几天 |
| a.DayOfWeek | dayofweek(a) | - | - | 获取a在周的第几天 |
| a.DayOfYear | dayofyear(a) | - | - | 获取a在年的第几天 |
| a.Hour | hour(a) | - | - | 小时 |
| a.Millisecond | floor(microsecond(a) / 1000) | - | - | 毫秒 |
| a.Minute | minute(a) | - | - | 分钟 |
| a.Month | month(a) | - | - | 月 |
| a.Second | second(a) | - | - | 秒 |
| a.Subtract(b) | (time_to_sec(a) - time_to_sec(b)) * 1000000 + microsecond(a) - microsecond(b) | - | - | 将a的值和b相减 |
| a.Ticks | time_to_sec(a) * 10000000 + microsecond(a) * 10 + 62135596800000000 | - | - | 刻度总数 |
| a.TimeOfDay | time_to_sec(date_format(a, '1970-1-1 %H:%i:%s.%f')) * 1000000 + microsecond(a) + 6213559680000000 | - | - | 获取a的时间部分 |
| a.Year | year(a) | - | - | 年 |
| a.Equals(b) | a = b | - | - | 比较a和b相等 |
| a.CompareTo(b) | a - b | - | - | 比较a和b大小 |
| a.ToString() | date_format(a, '%Y-%m-%d %H:%i:%s.%f') | - | - | 转换字符串 |
### 时间对象
| 表达式 | MySql | SqlServer | PostgreSQL | 功能说明 |
| - | - | - | - | - |
| TimeSpan.Zero | 0 | - | - | 0微秒 |
| TimeSpan.MaxValue | 922337203685477580 | - | - | 最大微秒时间 |
| TimeSpan.MinValue | -922337203685477580 | - | - | 最小微秒时间 |
| TimeSpan.Compare(a, b) | a - b | - | - | 比较a和b的大小 |
| TimeSpan.Equals(a, b) | a = b | - | - | 比较a和b相等 |
| TimeSpan.FromDays(a) | a * 1000000 * 60 * 60 * 24 | - | - | a天的微秒值 |
| TimeSpan.FromHours(a) | a * 1000000 * 60 * 60 | - | - | a小时的微秒值 |
| TimeSpan.FromMilliseconds(a) | a * 1000 | - | - | a毫秒的微秒值 |
| TimeSpan.FromMinutes(a) | a * 1000000 * 60 | - | - | a分钟的微秒值 |
| TimeSpan.FromSeconds(a) | a * 1000000 | - | - | a秒钟的微秒值 |
| TimeSpan.FromTicks(a) | a / 10 | - | - | a刻度的毫秒值 |
| a.Add(b) | a + b | - | - | 增加值 |
| a.Subtract(b) | a - b | - | - | 将a的值和b相减 |
| a.CompareTo(b) | a - b | - | - | 比较a和b大小 |
| a.Days | a div (1000000 * 60 * 60 * 24) | - | - | 天数部分 |
| a.Hours | a div (1000000 * 60 * 60) mod 24 | - | - | 小时部分 |
| a.Milliseconds | a div 1000 mod 1000 | - | - | 毫秒部分 |
| a.Seconds | a div 1000000 mod 60 | - | - | 秒数部分 |
| a.Ticks | a * 10 | - | - | 刻度总数 |
| a.TotalDays | a / (1000000 * 60 * 60 * 24) | - | - | 总天数(含小数) |
| a.TotalHours | a / (1000000 * 60 * 60) | - | - | 总小时(含小数) |
| a.TotalMilliseconds | a / 1000 | - | - | 总毫秒(含小数) |
| a.TotalMinutes | a / (1000000 * 60) | - | - | 总分钟(含小数) |
| a.TotalSeconds | a / 1000000 | - | - | 总秒数(含小数) |
| a.Equals(b) | a = b | - | - | 比较a和b相等 |
| a.ToString() | | - | - | 转换字符串 |
### 数学函数
| 表达式 | MySql | SqlServer | PostgreSQL | 功能说明 |
| - | - | - | - | - |
| Math.Abs(a) | abs(a) | - | - | - |
| Math.Acos(a) | acos(a) | - | - | - |
| Math.Asin(a) | asin(a) | - | - | - |
| Math.Atan(a) | atan(a) | - | - | - |
| Math.Atan2(a, b) | atan2(a, b) | - | - | - |
| Math.Ceiling(a) | ceiling(a) | - | - | - |
| Math.Cos(a) | cos(a) | - | - | - |
| Math.Exp(a) | exp(a) | - | - | - |
| Math.Floor(a) | floor(a) | - | - | - |
| Math.Log(a) | log(a) | - | - | - |
| Math.Log10(a) | log10(a) | - | - | - |
| Math.PI(a) | 3.1415926535897931 | - | - | - |
| Math.Pow(a, b) | pow(a, b) | - | - | - |
| Math.Round(a, b) | round(a, b) | - | - | - |
| Math.Sign(a) | sign(a) | - | - | - |
| Math.Sin(a) | sin(a) | - | - | - |
| Math.Sqrt(a) | sqrt(a) | - | - | - |
| Math.Tan(a) | tan(a) | - | - | - |
| Math.Truncate(a) | truncate(a, 0) | - | - | - |
#### Math 方法
...
# 更多文档整理中。。。 # 更多文档整理中。。。