- 优化 MySqlConnector MySqlDataTime 读取;

This commit is contained in:
2881099 2022-12-20 22:01:51 +08:00
parent 637bff767a
commit 40a6e11457
5 changed files with 41 additions and 15 deletions

View File

@ -1,4 +1,5 @@
using FreeSql.DataAnnotations;
using FreeSql.DataAnnotations;
using MySqlConnector;
using System;
using System.Numerics;
using Xunit;
@ -19,11 +20,17 @@ namespace FreeSql.Tests.MySqlConnectorMapType
[Fact]
public void DateTimeToDateTimeOffSet()
{
//MySqlDateTime dt1 = new MySqlDateTime(DateTime.Now);
//System.Convert.ChangeType(dt1, typeof(DateTimeOffset)); // System.Exception : Specified cast is not valid.
//insert
var orm = g.mysql;
orm.Delete<DateTimeOffSetTestMap>().Where(a => true).ExecuteAffrows();
var item = new DateTimeOffSetTestMap { dtos_to_dt = DateTimeOffset.Now, dtosnullable_to_dt = DateTimeOffset.Now };
Assert.Equal(1, orm.Insert<DateTimeOffSetTestMap>().AppendData(item).ExecuteAffrows());
var find = orm.Select<DateTimeOffSetTestMap>().Where(a => a.id == item.id).First();
var find = orm.Select<DateTimeOffSetTestMap>().Where(a => a.id == item.id).First(); // System.Exception : Specified cast is not valid.
Assert.NotNull(find);
Assert.Equal(item.id, find.id);
Assert.Equal(item.dtos_to_dt.ToString("g"), find.dtos_to_dt.ToString("g"));

View File

@ -15,14 +15,13 @@ namespace FreeSql.Tests.MySql
public void InsertOrUpdate_OnePrimary()
{
fsql.Delete<tbiou02>().Where("1=1").ExecuteAffrows();
var iou = fsql.InsertOrUpdate<tbiou02>().SetSource(fsql.Select<tbiou022>().ToSql(a => new { id = a.id + 1, name = "xxx" }, FieldAliasOptions.AsProperty));
var iou = fsql.InsertOrUpdate<tbiou02>().SetSource(fsql.Select<tbiou022>().ToSql(a => new { id = a.id + 1, name = "'xxx'" }, FieldAliasOptions.AsProperty));
var sql = iou.ToSql();
Assert.Equal(@"INSERT INTO `tbiou02`(`id`, `name`)
SELECT (a.`id` + 1) `id`, xxx `name`
SELECT (a.`id` + 1) `id`, 'xxx' `name`
FROM `tbiou022` a
ON DUPLICATE KEY UPDATE
`name` = VALUES(`name`)", sql);
Assert.Equal(0, iou.ExecuteAffrows());
iou = fsql.InsertOrUpdate<tbiou02>().SetSource(new tbiou02 { id = 1, name = "01" });
sql = iou.ToSql();

View File

@ -11,7 +11,7 @@ namespace FreeSql.Tests.PostgreSQL
IInsert<Topic> insert => g.pgsql.Insert<Topic>();
[Table(Name = "tb_topic_insert")]
[Table(Name = "tb_topic_insert2")]
class Topic
{
[Column(IsIdentity = true, IsPrimary = true)]
@ -137,21 +137,33 @@ namespace FreeSql.Tests.PostgreSQL
insert.AppendData(items.First()).ExecuteInserted();
}
[Table(Name = "tb_topic_insert_pgcopy")]
class TopicPgCopy
{
[Column(IsIdentity = true, IsPrimary = true)]
public int Id { get; set; }
public int Clicks { get; set; }
public TestTypeInfo Type { get; set; }
public string Title { get; set; }
public DateTime CreateTime { get; set; }
}
[Fact]
public void ExecutePgCopy()
{
var maxId = g.pgsql.Select<Topic>().Max(a => a.Id);
var items = new List<Topic>();
for (var a = 0; a < 10; a++) items.Add(new Topic { Id = maxId + a + 1, Title = $"newtitle{a}", Clicks = a * 100, CreateTime = DateTime.Now });
var maxId = g.pgsql.Select<TopicPgCopy>().Max(a => a.Id);
var items = new List<TopicPgCopy>();
for (var a = 0; a < 10; a++) items.Add(new TopicPgCopy { Id = maxId + a + 1, Title = $"newtitle{a}", Clicks = a * 100, CreateTime = DateTime.Now });
insert.AppendData(items).InsertIdentity().ExecutePgCopy();
g.pgsql.Insert(items).InsertIdentity().ExecutePgCopy();
items = g.pgsql.Select<Topic>().OrderByDescending(a => a.Id).Limit(1000).ToList();
items = g.pgsql.Select<TopicPgCopy>().OrderByDescending(a => a.Id).Limit(1000).ToList();
var sql = g.pgsql.Insert(items).InsertIdentity().NoneParameter().ToSql();
g.pgsql.Update<Topic>().SetSource(items).ExecutePgCopy();
g.pgsql.Update<Topic>().SetSource(items, a => new { a.Id, a.Clicks }).ExecutePgCopy();
g.pgsql.Update<Topic>().SetSource(items).UpdateColumns(a => new { a.Title }).ExecutePgCopy();
g.pgsql.Update<Topic>().SetSource(items, a => new { a.Id, a.Clicks }).UpdateColumns(a => new { a.Title }).ExecutePgCopy();
g.pgsql.Update<TopicPgCopy>().SetSource(items).ExecutePgCopy();
g.pgsql.Update<TopicPgCopy>().SetSource(items, a => new { a.Id, a.Clicks }).ExecutePgCopy();
g.pgsql.Update<TopicPgCopy>().SetSource(items).UpdateColumns(a => new { a.Title }).ExecutePgCopy();
g.pgsql.Update<TopicPgCopy>().SetSource(items, a => new { a.Id, a.Clicks }).UpdateColumns(a => new { a.Title }).ExecutePgCopy();
}
[Fact]

View File

@ -1546,6 +1546,13 @@ namespace FreeSql.Internal
case DataType.GBase:
if (dr.IsDBNull(index)) return null;
break;
case DataType.MySql:
if (dr.GetFieldType(index).FullName == "MySqlConnector.MySqlDateTime")
{
if (dr.IsDBNull(index)) return null;
return dr.GetDateTime(index);
}
break;
}
return dr.GetValue(index);
}

View File

@ -36,6 +36,7 @@ namespace FreeSql.MySql
});
Select0Provider._dicMethodDataReaderGetValue[typeof(Guid)] = typeof(DbDataReader).GetMethod("GetGuid", new Type[] { typeof(int) });
Select0Provider._dicMethodDataReaderGetValue[typeof(DateTimeOffset)] = typeof(DbDataReader).GetMethod("GetDateTime", new Type[] { typeof(int) });
}
public override ISelect<T1> CreateSelectProvider<T1>(object dywhere) => new MySqlSelect<T1>(this, this.InternalCommonUtils, this.InternalCommonExpression, dywhere);