Skip to content

Commit 39a3e8f

Browse files
committed
Support BigInteger as parameter. Fixes #1069
1 parent 4f59db6 commit 39a3e8f

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

src/MySqlConnector/MySqlParameter.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System.Buffers.Text;
22
using System.Diagnostics;
33
using System.Diagnostics.CodeAnalysis;
4+
using System.Globalization;
5+
using System.Numerics;
46
using System.Text;
57
using MySqlConnector.Core;
68
using MySqlConnector.Protocol.Serialization;
@@ -314,6 +316,10 @@ internal void AppendSqlString(ByteBufferWriter writer, StatementPreparerOptions
314316
// NOTE: Utf8Formatter doesn't support "R"
315317
writer.Write("{0:R}".FormatInvariant(Value));
316318
}
319+
else if (Value is BigInteger bigInteger)
320+
{
321+
writer.Write(bigInteger.ToString(CultureInfo.InvariantCulture));
322+
}
317323
else if (Value is MySqlDateTime mySqlDateTimeValue)
318324
{
319325
if (mySqlDateTimeValue.IsValidDateTime)
@@ -594,6 +600,10 @@ internal void AppendBinary(ByteBufferWriter writer, StatementPreparerOptions opt
594600
{
595601
writer.WriteLengthEncodedString("{0}".FormatInvariant(Value));
596602
}
603+
else if (Value is BigInteger bigInteger)
604+
{
605+
writer.WriteLengthEncodedString(bigInteger.ToString(CultureInfo.InvariantCulture));
606+
}
597607
else if (Value is MySqlDateTime mySqlDateTimeValue)
598608
{
599609
if (mySqlDateTimeValue.IsValidDateTime)

tests/SideBySide/InsertTests.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Numerics;
12
#if BASELINE
23
using MySql.Data.Types;
34
#endif
@@ -387,6 +388,52 @@ public void InsertStringBuilder(bool prepare)
387388
Assert.Equal(expected, reader.GetValue(0));
388389
}
389390

391+
[Theory]
392+
[InlineData(false)]
393+
[InlineData(true)]
394+
public void InsertBigInteger(bool prepare)
395+
{
396+
using var connection = new MySqlConnection(AppConfig.ConnectionString);
397+
connection.Open();
398+
connection.Execute(@"drop table if exists insert_big_integer;
399+
create table insert_big_integer(rowid integer not null primary key auto_increment, value bigint);");
400+
401+
var value = 1_000_000_000_000_000L;
402+
using var cmd = connection.CreateCommand();
403+
cmd.CommandText = @"insert into insert_big_integer(value) values(@value);";
404+
cmd.Parameters.AddWithValue("@value", new BigInteger(value));
405+
if (prepare)
406+
cmd.Prepare();
407+
cmd.ExecuteNonQuery();
408+
409+
using var reader = connection.ExecuteReader(@"select value from insert_big_integer order by rowid;");
410+
Assert.True(reader.Read());
411+
Assert.Equal(value, reader.GetValue(0));
412+
}
413+
414+
[Theory]
415+
[InlineData(false)]
416+
[InlineData(true)]
417+
public void InsertBigIntegerAsDecimal(bool prepare)
418+
{
419+
using var connection = new MySqlConnection(AppConfig.ConnectionString);
420+
connection.Open();
421+
connection.Execute(@"drop table if exists insert_big_integer;
422+
create table insert_big_integer(rowid integer not null primary key auto_increment, value decimal(40, 2));");
423+
424+
var value = long.MaxValue * 1000m;
425+
using var cmd = connection.CreateCommand();
426+
cmd.CommandText = @"insert into insert_big_integer(value) values(@value);";
427+
cmd.Parameters.AddWithValue("@value", new BigInteger(value));
428+
if (prepare)
429+
cmd.Prepare();
430+
cmd.ExecuteNonQuery();
431+
432+
using var reader = connection.ExecuteReader(@"select value from insert_big_integer order by rowid;");
433+
Assert.True(reader.Read());
434+
Assert.Equal(value, reader.GetValue(0));
435+
}
436+
390437
[Fact]
391438
public void InsertOldGuid()
392439
{

0 commit comments

Comments
 (0)