|
1 | | -using Ivy.Data.BigQuery; |
| 1 | +using Ivy.Data.BigQuery; |
2 | 2 | using Ivy.EntityFrameworkCore.BigQuery.Storage.ValueConversion.Internal; |
3 | 3 | using Microsoft.EntityFrameworkCore.Storage; |
4 | | -using System; |
5 | | -using System.Collections.Generic; |
| 4 | +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; |
6 | 5 | using System.Data.Common; |
7 | 6 | using System.Globalization; |
8 | | -using System.Linq; |
9 | | -using System.Text; |
10 | | -using System.Threading.Tasks; |
11 | 7 |
|
12 | | -namespace Ivy.EntityFrameworkCore.BigQuery.Storage.Internal.Mapping |
13 | | -{ |
14 | | - public class BigQueryDecimalTypeMapping : RelationalTypeMapping |
15 | | - { |
| 8 | +namespace Ivy.EntityFrameworkCore.BigQuery.Storage.Internal.Mapping; |
16 | 9 |
|
17 | | - private static readonly Type _clrType = typeof(decimal); |
18 | | - |
19 | | - public BigQueryDecimalTypeMapping(string storeType = "BIGNUMERIC(57, 28)") |
| 10 | +public class BigQueryDecimalTypeMapping : RelationalTypeMapping |
| 11 | +{ |
| 12 | + public BigQueryDecimalTypeMapping(string storeType = "BIGNUMERIC(57, 28)") |
20 | 13 | : base( |
21 | 14 | new RelationalTypeMappingParameters( |
22 | 15 | new CoreTypeMappingParameters( |
23 | 16 | typeof(decimal), |
24 | | - new DecimalToBigQueryNumericConverter(), |
| 17 | + GetConverterForStoreType(storeType), |
25 | 18 | jsonValueReaderWriter: Microsoft.EntityFrameworkCore.Storage.Json.JsonDecimalReaderWriter.Instance |
26 | | - ), |
| 19 | + ), |
27 | 20 | storeType, |
28 | 21 | StoreTypePostfix.PrecisionAndScale, |
29 | 22 | System.Data.DbType.Object |
30 | 23 | )) |
31 | | - { |
32 | | - } |
33 | | - |
| 24 | + { |
| 25 | + } |
34 | 26 |
|
35 | | - protected BigQueryDecimalTypeMapping(RelationalTypeMappingParameters parameters) |
| 27 | + protected BigQueryDecimalTypeMapping(RelationalTypeMappingParameters parameters) |
36 | 28 | : base(parameters) |
37 | | - { |
38 | | - } |
39 | | - |
| 29 | + { |
| 30 | + } |
40 | 31 |
|
41 | | - protected override RelationalTypeMapping Clone(RelationalTypeMappingParameters parameters) |
42 | | - => new BigQueryDecimalTypeMapping(parameters); |
| 32 | + private static ValueConverter GetConverterForStoreType(string storeType) |
| 33 | + { |
| 34 | + return storeType.StartsWith("BIG", StringComparison.OrdinalIgnoreCase) |
| 35 | + ? new DecimalToBigQueryBigNumericConverter() |
| 36 | + : new DecimalToBigQueryNumericConverter(); |
| 37 | + } |
43 | 38 |
|
| 39 | + protected override RelationalTypeMapping Clone(RelationalTypeMappingParameters parameters) |
| 40 | + { |
| 41 | + var isBigNumeric = parameters.StoreType.StartsWith("BIG", StringComparison.OrdinalIgnoreCase); |
| 42 | + var currentIsBigNumeric = Parameters.StoreType.StartsWith("BIG", StringComparison.OrdinalIgnoreCase); |
44 | 43 |
|
45 | | - protected override string GenerateNonNullSqlLiteral(object value) |
| 44 | + if (isBigNumeric != currentIsBigNumeric) |
46 | 45 | { |
47 | | - string stringValue; |
48 | | - if (value is decimal decimalValue) |
49 | | - { |
50 | | - stringValue = decimalValue.ToString(CultureInfo.InvariantCulture); |
51 | | - } |
52 | | - else if (value is Google.Cloud.BigQuery.V2.BigQueryBigNumeric bigQueryBigNumeric) |
53 | | - { |
54 | | - stringValue = bigQueryBigNumeric.ToString(); |
55 | | - } |
56 | | - else if (value is Google.Cloud.BigQuery.V2.BigQueryNumeric bigQueryNumeric) |
57 | | - { |
58 | | - stringValue = bigQueryNumeric.ToString(); |
59 | | - } |
60 | | - else |
61 | | - { |
62 | | - stringValue = value.ToString() ?? "0"; |
63 | | - } |
| 46 | + return new BigQueryDecimalTypeMapping(parameters.StoreType); |
| 47 | + } |
64 | 48 |
|
65 | | - string typePrefix = Parameters.StoreType.StartsWith("BIG", StringComparison.OrdinalIgnoreCase) |
66 | | - ? "BIGNUMERIC" |
67 | | - : "NUMERIC"; |
| 49 | + return new BigQueryDecimalTypeMapping(parameters); |
| 50 | + } |
68 | 51 |
|
69 | | - return $"{typePrefix} '{stringValue}'"; |
| 52 | + protected override string GenerateNonNullSqlLiteral(object value) |
| 53 | + { |
| 54 | + string stringValue; |
| 55 | + if (value is decimal decimalValue) |
| 56 | + { |
| 57 | + stringValue = decimalValue.ToString(CultureInfo.InvariantCulture); |
70 | 58 | } |
71 | | - |
72 | | - protected override void ConfigureParameter(DbParameter parameter) |
| 59 | + else if (value is Google.Cloud.BigQuery.V2.BigQueryBigNumeric bigQueryBigNumeric) |
| 60 | + { |
| 61 | + stringValue = bigQueryBigNumeric.ToString(); |
| 62 | + } |
| 63 | + else if (value is Google.Cloud.BigQuery.V2.BigQueryNumeric bigQueryNumeric) |
| 64 | + { |
| 65 | + stringValue = bigQueryNumeric.ToString(); |
| 66 | + } |
| 67 | + else |
73 | 68 | { |
74 | | - base.ConfigureParameter(parameter); |
| 69 | + stringValue = value.ToString() ?? "0"; |
| 70 | + } |
| 71 | + |
| 72 | + string typePrefix = Parameters.StoreType.StartsWith("BIG", StringComparison.OrdinalIgnoreCase) |
| 73 | + ? "BIGNUMERIC" |
| 74 | + : "NUMERIC"; |
| 75 | + |
| 76 | + return $"{typePrefix} '{stringValue}'"; |
| 77 | + } |
75 | 78 |
|
76 | | - if (parameter is BigQueryParameter bigQueryParameter) |
77 | | - { |
78 | | - // Use BigNumeric since DecimalToBigQueryNumericConverter produces BigQueryBigNumeric objects |
79 | | - bigQueryParameter.BigQueryDbType = Google.Cloud.BigQuery.V2.BigQueryDbType.BigNumeric; |
80 | | - } |
| 79 | + protected override void ConfigureParameter(DbParameter parameter) |
| 80 | + { |
| 81 | + base.ConfigureParameter(parameter); |
| 82 | + |
| 83 | + if (parameter is BigQueryParameter bigQueryParameter) |
| 84 | + { |
| 85 | + bigQueryParameter.BigQueryDbType = Parameters.StoreType.StartsWith("BIG", StringComparison.OrdinalIgnoreCase) |
| 86 | + ? Google.Cloud.BigQuery.V2.BigQueryDbType.BigNumeric |
| 87 | + : Google.Cloud.BigQuery.V2.BigQueryDbType.Numeric; |
81 | 88 | } |
82 | 89 | } |
83 | 90 | } |
0 commit comments