Skip to content

Commit 2eaf1d1

Browse files
committed
Convert output TIME parameters. Fixes #680
1 parent f175c95 commit 2eaf1d1

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

src/MySqlConnector/Core/TypeMapper.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Data;
44
using System.Linq;
5+
using System.Text;
56
using MySql.Data.MySqlClient;
67
using MySqlConnector.Protocol;
78
using MySqlConnector.Protocol.Payloads;
@@ -94,7 +95,7 @@ private TypeMapper()
9495
var typeDate = AddDbTypeMapping(new DbTypeMapping(typeof(DateTime), new[] { DbType.Date }));
9596
var typeDateTime = AddDbTypeMapping(new DbTypeMapping(typeof(DateTime), new[] { DbType.DateTime, DbType.DateTime2, DbType.DateTimeOffset }));
9697
AddDbTypeMapping(new DbTypeMapping(typeof(DateTimeOffset), new[] { DbType.DateTimeOffset }));
97-
var typeTime = AddDbTypeMapping(new DbTypeMapping(typeof(TimeSpan), new[] { DbType.Time }));
98+
var typeTime = AddDbTypeMapping(new DbTypeMapping(typeof(TimeSpan), new[] { DbType.Time }, convert: o => o is string s ? Utility.ParseTimeSpan(Encoding.UTF8.GetBytes(s)) : Convert.ChangeType(o, typeof(TimeSpan))));
9899
AddColumnTypeMetadata(new ColumnTypeMetadata("DATETIME", typeDateTime, MySqlDbType.DateTime));
99100
AddColumnTypeMetadata(new ColumnTypeMetadata("DATE", typeDate, MySqlDbType.Date));
100101
AddColumnTypeMetadata(new ColumnTypeMetadata("DATE", typeDate, MySqlDbType.Newdate));

tests/SideBySide/StoredProcedureFixture.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,11 @@ create procedure number_lister (inout high int)
109109
begin
110110
select 1, 2, 3;
111111
end;");
112+
Connection.Execute(@"DROP PROCEDURE IF EXISTS `GetTime`;
113+
CREATE PROCEDURE `GetTime`(OUT OutTime TIME)
114+
BEGIN
115+
SET OutTime = CURTIME();
116+
END");
112117

113118
if (AppConfig.SupportsJson)
114119
{

tests/SideBySide/StoredProcedureTests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,22 @@ public void CallNonExistentStoredProcedure()
590590
}
591591
}
592592

593+
[Fact]
594+
public void OutputTimeParameter()
595+
{
596+
using (var command = new MySqlCommand("GetTime", m_database.Connection))
597+
{
598+
command.CommandType = CommandType.StoredProcedure;
599+
var parameter = command.CreateParameter();
600+
parameter.ParameterName = "OutTime";
601+
parameter.Direction = ParameterDirection.Output;
602+
command.Parameters.Add(parameter);
603+
604+
command.ExecuteNonQuery();
605+
Assert.IsType<TimeSpan>(parameter.Value);
606+
}
607+
}
608+
593609
private static string NormalizeSpaces(string input)
594610
{
595611
input = input.Replace('\r', ' ');

0 commit comments

Comments
 (0)