MySqlConnector icon indicating copy to clipboard operation
MySqlConnector copied to clipboard

Cannot create MySqlDecimal instance

Open MaceWindu opened this issue 2 years ago • 6 comments

Software versions MySqlConnector version: 2.1.0+

Describe the bug I'm working on MySqlDecimal support in linq2db and found that it's constructor is marked internal, so it is not possible to create parameter with this type (can workaround it with inserting data using literal and reading it back). I see there is a test for use of MySqlDecimal with parameter, so this should be supported scenario.

Code sample

var decimal = new MySqlDecimal("123");

Expected behavior Support for string-based constructor in public API

MaceWindu avatar Feb 18 '22 15:02 MaceWindu

I've noticed that MySql.Data also has this constructor marked internal, but still think it should be public

MaceWindu avatar Feb 18 '22 16:02 MaceWindu

Also have feature request to implement equality interfaces and equality/comparison operators. This will allow to write LINQ queries with filtering by MySqlDecimal value:

MySqlDecimal value = ...;
table.Where(r => r.DecimalNullable == value).ToList()

MaceWindu avatar Feb 18 '22 16:02 MaceWindu

The current constructor is internal because all of that supporting code (to make it a "real" type) is missing.

equality

(decimal.Parse("0.00") == decimal.Parse("0.0")) == true

Would we expect similar logic in MySqlDecimal? That would require "numeric" logic to be added (instead of just doing string equality).

comparison

Maybe if equality is implemented to treat these as numbers, this is not a large extension. (But if equality were string-based, this would be complicated to add and maybe should be dropped.)

This will allow to write LINQ queries with filtering by MySqlDecimal value:

Can we use strings?

var decimalValue = "0.12345";
table.Where(r => r.DecimalNullable?.ToString() == value).ToList()

I see there is a test for use of MySqlDecimal with parameter

The use case for this was reusing a value returned from a query. For arbitrary parameter values, MySqlParameter.Value = "123.456 /* large decimal value */" works just fine.

bgrainger avatar Feb 18 '22 16:02 bgrainger

I will agree with you here. We are not end-users, so we shouldn't ask for changes end-users probably don't need. Probably you can close it for now.

MaceWindu avatar Feb 19 '22 09:02 MaceWindu

Another question - BulkCopy support currently missing for new type - is it expected or classified as bug?

MaceWindu avatar Feb 19 '22 14:02 MaceWindu

I want to say that except BulkCopy, new type works flawlessly :+1:

Same type in MySql.Data actually doesn't work for large decimals as all reads and writes go through conversion to decimal which results in overflow and defeats type purpose :man_facepalming:

MaceWindu avatar Feb 19 '22 14:02 MaceWindu

In .NET 8.0, libraries that need to support serialization of this value could use UnsafeAccessor to create it. (Libraries targeting previous .NET versions can use reflection.)

[UnsafeAccessor(UnsafeAccessorKind.Constructor)]
private static extern MySqlDecimal CreateMySqlDecimal(string value);

(This still doesn't seem like a common enough use case to be part of the public API.)

bgrainger avatar Nov 13 '23 00:11 bgrainger