Skip to content

Commit 40a6e11

Browse files
committed
- 优化 MySqlConnector MySqlDataTime 读取;
1 parent 637bff7 commit 40a6e11

File tree

5 files changed

+41
-15
lines changed

5 files changed

+41
-15
lines changed

FreeSql.Tests/FreeSql.Tests.Provider.MySqlConnector/MySqlConnector/MapType/DateTimeOffSetTest.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using FreeSql.DataAnnotations;
1+
using FreeSql.DataAnnotations;
2+
using MySqlConnector;
23
using System;
34
using System.Numerics;
45
using Xunit;
@@ -19,11 +20,17 @@ class DateTimeOffSetTestMap
1920
[Fact]
2021
public void DateTimeToDateTimeOffSet()
2122
{
23+
24+
//MySqlDateTime dt1 = new MySqlDateTime(DateTime.Now);
25+
//System.Convert.ChangeType(dt1, typeof(DateTimeOffset)); // System.Exception : Specified cast is not valid.
26+
27+
2228
//insert
2329
var orm = g.mysql;
30+
orm.Delete<DateTimeOffSetTestMap>().Where(a => true).ExecuteAffrows();
2431
var item = new DateTimeOffSetTestMap { dtos_to_dt = DateTimeOffset.Now, dtosnullable_to_dt = DateTimeOffset.Now };
2532
Assert.Equal(1, orm.Insert<DateTimeOffSetTestMap>().AppendData(item).ExecuteAffrows());
26-
var find = orm.Select<DateTimeOffSetTestMap>().Where(a => a.id == item.id).First();
33+
var find = orm.Select<DateTimeOffSetTestMap>().Where(a => a.id == item.id).First(); // System.Exception : Specified cast is not valid.
2734
Assert.NotNull(find);
2835
Assert.Equal(item.id, find.id);
2936
Assert.Equal(item.dtos_to_dt.ToString("g"), find.dtos_to_dt.ToString("g"));

FreeSql.Tests/FreeSql.Tests/MySql/Curd/MySqlInsertOrUpdateTest.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,13 @@ public class MySqlInsertOrUpdateTest
1515
public void InsertOrUpdate_OnePrimary()
1616
{
1717
fsql.Delete<tbiou02>().Where("1=1").ExecuteAffrows();
18-
var iou = fsql.InsertOrUpdate<tbiou02>().SetSource(fsql.Select<tbiou022>().ToSql(a => new { id = a.id + 1, name = "xxx" }, FieldAliasOptions.AsProperty));
18+
var iou = fsql.InsertOrUpdate<tbiou02>().SetSource(fsql.Select<tbiou022>().ToSql(a => new { id = a.id + 1, name = "'xxx'" }, FieldAliasOptions.AsProperty));
1919
var sql = iou.ToSql();
2020
Assert.Equal(@"INSERT INTO `tbiou02`(`id`, `name`)
21-
SELECT (a.`id` + 1) `id`, xxx `name`
21+
SELECT (a.`id` + 1) `id`, 'xxx' `name`
2222
FROM `tbiou022` a
2323
ON DUPLICATE KEY UPDATE
2424
`name` = VALUES(`name`)", sql);
25-
Assert.Equal(0, iou.ExecuteAffrows());
2625

2726
iou = fsql.InsertOrUpdate<tbiou02>().SetSource(new tbiou02 { id = 1, name = "01" });
2827
sql = iou.ToSql();

FreeSql.Tests/FreeSql.Tests/PostgreSQL/Curd/PostgreSQLInsertTest.cs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class PostgreSQLInsertTest
1111

1212
IInsert<Topic> insert => g.pgsql.Insert<Topic>();
1313

14-
[Table(Name = "tb_topic_insert")]
14+
[Table(Name = "tb_topic_insert2")]
1515
class Topic
1616
{
1717
[Column(IsIdentity = true, IsPrimary = true)]
@@ -137,21 +137,33 @@ public void ExecuteInserted()
137137
insert.AppendData(items.First()).ExecuteInserted();
138138
}
139139

140+
141+
[Table(Name = "tb_topic_insert_pgcopy")]
142+
class TopicPgCopy
143+
{
144+
[Column(IsIdentity = true, IsPrimary = true)]
145+
public int Id { get; set; }
146+
public int Clicks { get; set; }
147+
public TestTypeInfo Type { get; set; }
148+
public string Title { get; set; }
149+
public DateTime CreateTime { get; set; }
150+
}
151+
140152
[Fact]
141153
public void ExecutePgCopy()
142154
{
143-
var maxId = g.pgsql.Select<Topic>().Max(a => a.Id);
144-
var items = new List<Topic>();
145-
for (var a = 0; a < 10; a++) items.Add(new Topic { Id = maxId + a + 1, Title = $"newtitle{a}", Clicks = a * 100, CreateTime = DateTime.Now });
155+
var maxId = g.pgsql.Select<TopicPgCopy>().Max(a => a.Id);
156+
var items = new List<TopicPgCopy>();
157+
for (var a = 0; a < 10; a++) items.Add(new TopicPgCopy { Id = maxId + a + 1, Title = $"newtitle{a}", Clicks = a * 100, CreateTime = DateTime.Now });
146158

147-
insert.AppendData(items).InsertIdentity().ExecutePgCopy();
159+
g.pgsql.Insert(items).InsertIdentity().ExecutePgCopy();
148160

149-
items = g.pgsql.Select<Topic>().OrderByDescending(a => a.Id).Limit(1000).ToList();
161+
items = g.pgsql.Select<TopicPgCopy>().OrderByDescending(a => a.Id).Limit(1000).ToList();
150162
var sql = g.pgsql.Insert(items).InsertIdentity().NoneParameter().ToSql();
151-
g.pgsql.Update<Topic>().SetSource(items).ExecutePgCopy();
152-
g.pgsql.Update<Topic>().SetSource(items, a => new { a.Id, a.Clicks }).ExecutePgCopy();
153-
g.pgsql.Update<Topic>().SetSource(items).UpdateColumns(a => new { a.Title }).ExecutePgCopy();
154-
g.pgsql.Update<Topic>().SetSource(items, a => new { a.Id, a.Clicks }).UpdateColumns(a => new { a.Title }).ExecutePgCopy();
163+
g.pgsql.Update<TopicPgCopy>().SetSource(items).ExecutePgCopy();
164+
g.pgsql.Update<TopicPgCopy>().SetSource(items, a => new { a.Id, a.Clicks }).ExecutePgCopy();
165+
g.pgsql.Update<TopicPgCopy>().SetSource(items).UpdateColumns(a => new { a.Title }).ExecutePgCopy();
166+
g.pgsql.Update<TopicPgCopy>().SetSource(items, a => new { a.Id, a.Clicks }).UpdateColumns(a => new { a.Title }).ExecutePgCopy();
155167
}
156168

157169
[Fact]

FreeSql/Internal/UtilsExpressionTree.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1546,6 +1546,13 @@ internal static object InternalDataReaderGetValue(CommonUtils commonUtil, DbData
15461546
case DataType.GBase:
15471547
if (dr.IsDBNull(index)) return null;
15481548
break;
1549+
case DataType.MySql:
1550+
if (dr.GetFieldType(index).FullName == "MySqlConnector.MySqlDateTime")
1551+
{
1552+
if (dr.IsDBNull(index)) return null;
1553+
return dr.GetDateTime(index);
1554+
}
1555+
break;
15491556
}
15501557
return dr.GetValue(index);
15511558
}

Providers/FreeSql.Provider.MySql/MySqlProvider.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ static MySqlProvider()
3636
});
3737

3838
Select0Provider._dicMethodDataReaderGetValue[typeof(Guid)] = typeof(DbDataReader).GetMethod("GetGuid", new Type[] { typeof(int) });
39+
Select0Provider._dicMethodDataReaderGetValue[typeof(DateTimeOffset)] = typeof(DbDataReader).GetMethod("GetDateTime", new Type[] { typeof(int) });
3940
}
4041

4142
public override ISelect<T1> CreateSelectProvider<T1>(object dywhere) => new MySqlSelect<T1>(this, this.InternalCommonUtils, this.InternalCommonExpression, dywhere);

0 commit comments

Comments
 (0)