Skip to content

Commit 70266c0

Browse files
2881028810
authored andcommitted
- 增加 PostgreSQL 表达式解析 yyyyMMdd 常用 c# 日期格式化;
1 parent e20f532 commit 70266c0

File tree

9 files changed

+198
-189
lines changed

9 files changed

+198
-189
lines changed

FreeSql.Tests/FreeSql.Tests.Provider.Odbc/PostgreSQL/PostgreSQLExpression/DateTimeTest.cs

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,52 @@ class TestTypeParentInfo
4141
public List<TestTypeInfo> Types { get; set; }
4242
public DateTime Time2 { get; set; }
4343
}
44+
45+
[Fact]
46+
public void this_ToString()
47+
{
48+
var data = new List<object>();
49+
data.Add(select.Where(a => a.CreateTime.ToString().Equals(DateTime.Now)).ToList());
50+
data.Add(select.Where(a => a.Type.Time.AddYears(1).ToString().Equals(DateTime.Now)).ToList());
51+
data.Add(select.Where(a => a.Type.Parent.Time2.AddYears(1).ToString().Equals(DateTime.Now)).ToList());
52+
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TypeGuid` as3, a.`Title` as4, a.`CreateTime` as5
53+
//FROM `tb_topic111333` a
54+
//WHERE ((date_format(a.`CreateTime`, '%Y-%m-%d %H:%i:%s.%f') = now()));
55+
56+
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TypeGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a__Type.`Time` as7, a.`Title` as8, a.`CreateTime` as9
57+
//FROM `tb_topic111333` a, `TestTypeInfo333` a__Type
58+
//WHERE ((date_format(date_add(a__Type.`Time`, interval (1) year), '%Y-%m-%d %H:%i:%s.%f') = now()));
59+
60+
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TypeGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a__Type.`Time` as7, a.`Title` as8, a.`CreateTime` as9
61+
//FROM `tb_topic111333` a, `TestTypeInfo333` a__Type, `TestTypeParentInfo23123` a__Type__Parent
62+
//WHERE ((date_format(date_add(a__Type__Parent.`Time2`, interval (1) year), '%Y-%m-%d %H:%i:%s.%f') = now()))
63+
64+
g.pgsql.Insert(new Topic()).ExecuteAffrows();
65+
var dtn = DateTime.Parse("2020-1-1 0:0:0");
66+
var dts = Enumerable.Range(1, 12).Select(a => dtn.AddMonths(a))
67+
.Concat(Enumerable.Range(1, 31).Select(a => dtn.AddDays(a)))
68+
.Concat(Enumerable.Range(1, 24).Select(a => dtn.AddHours(a)))
69+
.Concat(Enumerable.Range(1, 60).Select(a => dtn.AddMinutes(a)))
70+
.Concat(Enumerable.Range(1, 60).Select(a => dtn.AddSeconds(a)));
71+
foreach (var dt in dts)
72+
{
73+
Assert.Equal(dt.ToString("yyyy-MM-dd HH:mm:ss.ffffff"), select.First(a => dt.ToString()));
74+
Assert.Equal(dt.ToString("yyyy-MM-dd HH:mm:ss"), select.First(a => dt.ToString("yyyy-MM-dd HH:mm:ss")));
75+
Assert.Equal(dt.ToString("yyyy-MM-dd HH:mm"), select.First(a => dt.ToString("yyyy-MM-dd HH:mm")));
76+
Assert.Equal(dt.ToString("yyyy-MM-dd HH"), select.First(a => dt.ToString("yyyy-MM-dd HH")));
77+
Assert.Equal(dt.ToString("yyyy-MM-dd"), select.First(a => dt.ToString("yyyy-MM-dd")));
78+
Assert.Equal(dt.ToString("yyyy-MM"), select.First(a => dt.ToString("yyyy-MM")));
79+
Assert.Equal(dt.ToString("yyyyMMddHHmmss"), select.First(a => dt.ToString("yyyyMMddHHmmss")));
80+
Assert.Equal(dt.ToString("yyyyMMddHHmm"), select.First(a => dt.ToString("yyyyMMddHHmm")));
81+
Assert.Equal(dt.ToString("yyyyMMddHH"), select.First(a => dt.ToString("yyyyMMddHH")));
82+
Assert.Equal(dt.ToString("yyyyMMdd"), select.First(a => dt.ToString("yyyyMMdd")));
83+
Assert.Equal(dt.ToString("yyyyMM"), select.First(a => dt.ToString("yyyyMM")));
84+
Assert.Equal(dt.ToString("yyyy"), select.First(a => dt.ToString("yyyy")));
85+
Assert.Equal(dt.ToString("HH:mm:ss"), select.First(a => dt.ToString("HH:mm:ss")));
86+
Assert.Equal(dt.ToString("yyyy MM dd HH mm ss yy M d H hh h"), select.First(a => dt.ToString("yyyy MM dd HH mm ss yy M d H hh h")));
87+
Assert.Equal(dt.ToString("yyyy MM dd HH mm ss yy M d H hh h m s tt t").Replace("上午", "AM").Replace("下午", "PM").Replace("上", "A").Replace("下", "P"), select.First(a => dt.ToString("yyyy MM dd HH mm ss yy M d H hh h m s tt t")));
88+
}
89+
}
4490
[Fact]
4591
public void Now()
4692
{
@@ -587,26 +633,6 @@ public void this_Equals()
587633
//FROM `tb_topic111333` a, `TestTypeInfo333` a__Type, `TestTypeParentInfo23123` a__Type__Parent
588634
//WHERE ((date_add(a__Type__Parent.`Time2`, interval (1) year) = now()))
589635
}
590-
[Fact]
591-
public void this_ToString()
592-
{
593-
var data = new List<object>();
594-
data.Add(select.Where(a => a.CreateTime.ToString().Equals(DateTime.Now)).ToList());
595-
data.Add(select.Where(a => a.Type.Time.AddYears(1).ToString().Equals(DateTime.Now)).ToList());
596-
data.Add(select.Where(a => a.Type.Parent.Time2.AddYears(1).ToString().Equals(DateTime.Now)).ToList());
597-
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TypeGuid` as3, a.`Title` as4, a.`CreateTime` as5
598-
//FROM `tb_topic111333` a
599-
//WHERE ((date_format(a.`CreateTime`, '%Y-%m-%d %H:%i:%s.%f') = now()));
600-
601-
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TypeGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a__Type.`Time` as7, a.`Title` as8, a.`CreateTime` as9
602-
//FROM `tb_topic111333` a, `TestTypeInfo333` a__Type
603-
//WHERE ((date_format(date_add(a__Type.`Time`, interval (1) year), '%Y-%m-%d %H:%i:%s.%f') = now()));
604-
605-
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TypeGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a__Type.`Time` as7, a.`Title` as8, a.`CreateTime` as9
606-
//FROM `tb_topic111333` a, `TestTypeInfo333` a__Type, `TestTypeParentInfo23123` a__Type__Parent
607-
//WHERE ((date_format(date_add(a__Type__Parent.`Time2`, interval (1) year), '%Y-%m-%d %H:%i:%s.%f') = now()))
608-
}
609-
610636
[Fact]
611637
public void DateTime_Compare()
612638
{

FreeSql.Tests/FreeSql.Tests/PostgreSQL/PostgreSQLExpression/DateTimeTest.cs

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,52 @@ class TestTypeParentInfo
4141
public List<TestTypeInfo> Types { get; set; }
4242
public DateTime Time2 { get; set; }
4343
}
44+
45+
[Fact]
46+
public void this_ToString()
47+
{
48+
var data = new List<object>();
49+
data.Add(select.Where(a => a.CreateTime.ToString().Equals(DateTime.Now)).ToList());
50+
data.Add(select.Where(a => a.Type.Time.AddYears(1).ToString().Equals(DateTime.Now)).ToList());
51+
data.Add(select.Where(a => a.Type.Parent.Time2.AddYears(1).ToString().Equals(DateTime.Now)).ToList());
52+
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TypeGuid` as3, a.`Title` as4, a.`CreateTime` as5
53+
//FROM `tb_topic111333` a
54+
//WHERE ((date_format(a.`CreateTime`, '%Y-%m-%d %H:%i:%s.%f') = now()));
55+
56+
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TypeGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a__Type.`Time` as7, a.`Title` as8, a.`CreateTime` as9
57+
//FROM `tb_topic111333` a, `TestTypeInfo333` a__Type
58+
//WHERE ((date_format(date_add(a__Type.`Time`, interval (1) year), '%Y-%m-%d %H:%i:%s.%f') = now()));
59+
60+
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TypeGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a__Type.`Time` as7, a.`Title` as8, a.`CreateTime` as9
61+
//FROM `tb_topic111333` a, `TestTypeInfo333` a__Type, `TestTypeParentInfo23123` a__Type__Parent
62+
//WHERE ((date_format(date_add(a__Type__Parent.`Time2`, interval (1) year), '%Y-%m-%d %H:%i:%s.%f') = now()))
63+
64+
g.pgsql.Insert(new Topic()).ExecuteAffrows();
65+
var dtn = DateTime.Parse("2020-1-1 0:0:0");
66+
var dts = Enumerable.Range(1, 12).Select(a => dtn.AddMonths(a))
67+
.Concat(Enumerable.Range(1, 31).Select(a => dtn.AddDays(a)))
68+
.Concat(Enumerable.Range(1, 24).Select(a => dtn.AddHours(a)))
69+
.Concat(Enumerable.Range(1, 60).Select(a => dtn.AddMinutes(a)))
70+
.Concat(Enumerable.Range(1, 60).Select(a => dtn.AddSeconds(a)));
71+
foreach (var dt in dts)
72+
{
73+
Assert.Equal(dt.ToString("yyyy-MM-dd HH:mm:ss.ffffff"), select.First(a => dt.ToString()));
74+
Assert.Equal(dt.ToString("yyyy-MM-dd HH:mm:ss"), select.First(a => dt.ToString("yyyy-MM-dd HH:mm:ss")));
75+
Assert.Equal(dt.ToString("yyyy-MM-dd HH:mm"), select.First(a => dt.ToString("yyyy-MM-dd HH:mm")));
76+
Assert.Equal(dt.ToString("yyyy-MM-dd HH"), select.First(a => dt.ToString("yyyy-MM-dd HH")));
77+
Assert.Equal(dt.ToString("yyyy-MM-dd"), select.First(a => dt.ToString("yyyy-MM-dd")));
78+
Assert.Equal(dt.ToString("yyyy-MM"), select.First(a => dt.ToString("yyyy-MM")));
79+
Assert.Equal(dt.ToString("yyyyMMddHHmmss"), select.First(a => dt.ToString("yyyyMMddHHmmss")));
80+
Assert.Equal(dt.ToString("yyyyMMddHHmm"), select.First(a => dt.ToString("yyyyMMddHHmm")));
81+
Assert.Equal(dt.ToString("yyyyMMddHH"), select.First(a => dt.ToString("yyyyMMddHH")));
82+
Assert.Equal(dt.ToString("yyyyMMdd"), select.First(a => dt.ToString("yyyyMMdd")));
83+
Assert.Equal(dt.ToString("yyyyMM"), select.First(a => dt.ToString("yyyyMM")));
84+
Assert.Equal(dt.ToString("yyyy"), select.First(a => dt.ToString("yyyy")));
85+
Assert.Equal(dt.ToString("HH:mm:ss"), select.First(a => dt.ToString("HH:mm:ss")));
86+
Assert.Equal(dt.ToString("yyyy MM dd HH mm ss yy M d H hh h"), select.First(a => dt.ToString("yyyy MM dd HH mm ss yy M d H hh h")));
87+
Assert.Equal(dt.ToString("yyyy MM dd HH mm ss yy M d H hh h m s tt t").Replace("上午", "AM").Replace("下午", "PM").Replace("上", "A").Replace("下", "P"), select.First(a => dt.ToString("yyyy MM dd HH mm ss yy M d H hh h m s tt t")));
88+
}
89+
}
4490
[Fact]
4591
public void Now()
4692
{
@@ -587,26 +633,6 @@ public void this_Equals()
587633
//FROM `tb_topic111333` a, `TestTypeInfo333` a__Type, `TestTypeParentInfo23123` a__Type__Parent
588634
//WHERE ((date_add(a__Type__Parent.`Time2`, interval (1) year) = now()))
589635
}
590-
[Fact]
591-
public void this_ToString()
592-
{
593-
var data = new List<object>();
594-
data.Add(select.Where(a => a.CreateTime.ToString().Equals(DateTime.Now)).ToList());
595-
data.Add(select.Where(a => a.Type.Time.AddYears(1).ToString().Equals(DateTime.Now)).ToList());
596-
data.Add(select.Where(a => a.Type.Parent.Time2.AddYears(1).ToString().Equals(DateTime.Now)).ToList());
597-
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TypeGuid` as3, a.`Title` as4, a.`CreateTime` as5
598-
//FROM `tb_topic111333` a
599-
//WHERE ((date_format(a.`CreateTime`, '%Y-%m-%d %H:%i:%s.%f') = now()));
600-
601-
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TypeGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a__Type.`Time` as7, a.`Title` as8, a.`CreateTime` as9
602-
//FROM `tb_topic111333` a, `TestTypeInfo333` a__Type
603-
//WHERE ((date_format(date_add(a__Type.`Time`, interval (1) year), '%Y-%m-%d %H:%i:%s.%f') = now()));
604-
605-
//SELECT a.`Id` as1, a.`Clicks` as2, a.`TypeGuid` as3, a__Type.`Guid` as4, a__Type.`ParentId` as5, a__Type.`Name` as6, a__Type.`Time` as7, a.`Title` as8, a.`CreateTime` as9
606-
//FROM `tb_topic111333` a, `TestTypeInfo333` a__Type, `TestTypeParentInfo23123` a__Type__Parent
607-
//WHERE ((date_format(date_add(a__Type__Parent.`Time2`, interval (1) year), '%Y-%m-%d %H:%i:%s.%f') = now()))
608-
}
609-
610636
[Fact]
611637
public void DateTime_Compare()
612638
{

FreeSql/FreeSql.xml

Lines changed: 0 additions & 143 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Providers/FreeSql.Provider.MySql/MySqlExpression.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,6 @@ public override string ExpressionLambdaToSqlCallDateTime(MethodCallExpression ex
410410
case "'yyyy'": return $"date_format({left},'%Y')";
411411
case "'HH:mm:ss'": return $"date_format({left},'%H:%i:%s')";
412412
}
413-
414413
args1 = Regex.Replace(args1, "(yyyy|yy|MM|M|dd|d|HH|H|hh|h|mm|ss|tt)", m =>
415414
{
416415
switch (m.Groups[1].Value)

0 commit comments

Comments
 (0)