Skip to content

Commit dbc1f97

Browse files
authored
Merge pull request #370 from bgrainger/mysqldbtype
Expose and use MySqlDbType. Fixes #362
2 parents ccf3f4c + 6c5f42f commit dbc1f97

17 files changed

+529
-508
lines changed

src/MySqlConnector/MySqlClient/Caches/CachedParameter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Data;
1+
using System.Data;
22
using System.Linq;
33
using MySql.Data.MySqlClient.Types;
44

@@ -29,7 +29,7 @@ public CachedParameter(int ordinalPosition, string mode, string name, string dat
2929
}
3030
}
3131
Name = name;
32-
DbType = TypeMapper.Mapper.GetDbTypeMapping(dataType, unsigned).DbTypes?.First() ?? DbType.Object;
32+
DbType = TypeMapper.Instance.GetDbTypeMapping(dataType, unsigned).DbTypes?.First() ?? DbType.Object;
3333
}
3434

3535
internal readonly int Position;

src/MySqlConnector/MySqlClient/Caches/CachedProcedure.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.Collections.ObjectModel;
44
using System.Data;
@@ -13,7 +13,7 @@ internal class CachedProcedure
1313
{
1414
internal static async Task<CachedProcedure> FillAsync(IOBehavior ioBehavior, MySqlConnection connection, string schema, string component, CancellationToken cancellationToken)
1515
{
16-
var cmd = (MySqlCommand) connection.CreateCommand();
16+
var cmd = connection.CreateCommand();
1717

1818
cmd.CommandText = @"SELECT ORDINAL_POSITION, PARAMETER_MODE, PARAMETER_NAME, DATA_TYPE, DTD_IDENTIFIER
1919
FROM information_schema.parameters
@@ -22,13 +22,11 @@ FROM information_schema.parameters
2222
cmd.Parameters.Add(new MySqlParameter
2323
{
2424
ParameterName = "@schema",
25-
DbType = DbType.String,
2625
Value = schema
2726
});
2827
cmd.Parameters.Add(new MySqlParameter
2928
{
3029
ParameterName = "@component",
31-
DbType = DbType.String,
3230
Value = component
3331
});
3432

@@ -77,7 +75,7 @@ internal MySqlParameterCollection AlignParamsWithDb(MySqlParameterCollection par
7775

7876
if (!alignParam.HasSetDirection)
7977
alignParam.Direction = cachedParam.Direction;
80-
if (alignParam.DbType == default(DbType))
78+
if (!alignParam.HasSetDbType)
8179
alignParam.DbType = cachedParam.DbType;
8280

8381
// cached parameters are oredered by ordinal position

src/MySqlConnector/MySqlClient/CommandExecutors/StoredProcedureCommandExecutor.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.Data;
44
using System.Data.Common;
@@ -102,9 +102,9 @@ internal void SetParams()
102102
for (var i = 0; i < m_outParams.Count; i++)
103103
{
104104
var param = m_outParams[i];
105-
if (param.DbType != default(DbType))
105+
if (param.HasSetDbType)
106106
{
107-
var dbTypeMapping = TypeMapper.Mapper.GetDbTypeMapping(param.DbType);
107+
var dbTypeMapping = TypeMapper.Instance.GetDbTypeMapping(param.DbType);
108108
if (dbTypeMapping != null)
109109
{
110110
param.Value = dbTypeMapping.DoConversion(reader.GetValue(i));

src/MySqlConnector/MySqlClient/MySqlConnection.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -250,14 +250,14 @@ public override System.Data.DataTable GetSchema(string collectionName, string[]
250250
});
251251

252252
// Column type mappings:
253-
var colTypes = Types.TypeMapper.Mapper.GetColumnMappings();
253+
var colTypes = Types.TypeMapper.Instance.GetColumnMappings();
254254
foreach (var map in colTypes)
255255
{
256256
var dbTypeMap = map.DbTypeMapping;
257257
var dbType = dbTypeMap.DbTypes.FirstOrDefault();
258258
dt.Rows.Add(new object[] {
259259
dbTypeMap.ClrType.FullName,
260-
map.ColumnTypeName,
260+
map.DataTypeName,
261261
(int)dbType,
262262
map.Unsigned
263263
});
@@ -267,8 +267,8 @@ public override System.Data.DataTable GetSchema(string collectionName, string[]
267267
foreach (MySqlDbType mapItem in Enum.GetValues(typeof(MySqlDbType)))
268268
{
269269
var typeName = Enum.GetName(typeof(MySqlDbType), mapItem);
270-
var dbType = Types.TypeMapper.ConvertFromMySqlDbType(mapItem);
271-
var map = Types.TypeMapper.Mapper.GetDbTypeMapping(dbType);
270+
var dbType = Types.TypeMapper.Instance.GetDbTypeForMySqlDbType(mapItem);
271+
var map = Types.TypeMapper.Instance.GetDbTypeMapping(dbType);
272272
if (map != null) // MySqlDbType.Set is not supported by the mapper.
273273
{
274274
dt.Rows.Add(new object[] {

src/MySqlConnector/MySqlClient/MySqlDataReader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ public override void Close()
241241
public ReadOnlyCollection<DbColumn> GetColumnSchema()
242242
{
243243
return GetResultSet().ColumnDefinitions
244-
.Select((c, n) => (DbColumn) new MySqlDbColumn(n, c, GetFieldType(n), GetDataTypeName(n)))
244+
.Select((c, n) => (DbColumn) new MySqlDbColumn(n, c, GetResultSet().ColumnTypes[n]))
245245
.ToList().AsReadOnly();
246246
}
247247

src/MySqlConnector/MySqlClient/MySqlDbColumn.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System;
2+
using System.Globalization;
3+
using MySql.Data.MySqlClient.Types;
24
using MySql.Data.Serialization;
35

46
#if !NETSTANDARD1_3 && !NETSTANDARD2_0
@@ -38,8 +40,11 @@ namespace MySql.Data.MySqlClient
3840
{
3941
public sealed class MySqlDbColumn : System.Data.Common.DbColumn
4042
{
41-
internal MySqlDbColumn(int ordinal, ColumnDefinitionPayload column, Type type, string dataTypeName)
43+
internal MySqlDbColumn(int ordinal, ColumnDefinitionPayload column, MySqlDbType mySqlDbType)
4244
{
45+
var columnTypeMetadata = TypeMapper.Instance.GetColumnTypeMetadata(mySqlDbType);
46+
47+
var type = columnTypeMetadata.DbTypeMapping.ClrType;
4348
var columnSize = type == typeof(string) || type == typeof(Guid) ?
4449
column.ColumnLength / SerializationUtility.GetBytesPerCharacter(column.CharacterSet) :
4550
column.ColumnLength;
@@ -53,7 +58,9 @@ internal MySqlDbColumn(int ordinal, ColumnDefinitionPayload column, Type type, s
5358
ColumnOrdinal = ordinal;
5459
ColumnSize = columnSize > int.MaxValue ? int.MaxValue : unchecked((int) columnSize);
5560
DataType = type;
56-
DataTypeName = dataTypeName;
61+
DataTypeName = columnTypeMetadata.SimpleDataTypeName;
62+
if (mySqlDbType == MySqlDbType.String)
63+
DataTypeName += string.Format(CultureInfo.InvariantCulture, "({0})", columnSize);
5764
IsAliased = column.PhysicalName != column.Name;
5865
IsAutoIncrement = (column.ColumnFlags & ColumnFlags.AutoIncrement) != 0;
5966
IsExpression = false;
@@ -72,9 +79,9 @@ internal MySqlDbColumn(int ordinal, ColumnDefinitionPayload column, Type type, s
7279
NumericPrecision--;
7380
}
7481
NumericScale = column.Decimals;
75-
ProviderType = (int) column.ColumnType;
82+
ProviderType = mySqlDbType;
7683
}
7784

78-
public int ProviderType { get; }
85+
public MySqlDbType ProviderType { get; }
7986
}
8087
}

src/MySqlConnector/MySqlClient/MySqlDbType.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@ namespace MySql.Data.MySqlClient
44
{
55
public enum MySqlDbType
66
{
7+
Bool = -1,
78
Decimal,
89
Byte,
910
Int16,
10-
Int24 = 9,
11-
Int32 = 3,
12-
Int64 = 8,
13-
Float = 4,
11+
Int32,
12+
Float,
1413
Double,
15-
Timestamp = 7,
16-
Date = 10,
14+
Null,
15+
Timestamp,
16+
Int64,
17+
Int24,
18+
Date,
1719
Time,
1820
DateTime,
1921
[Obsolete("The Datetime enum value is obsolete. Please use DateTime.")]
@@ -35,9 +37,9 @@ public enum MySqlDbType
3537
Geometry,
3638
UByte = 501,
3739
UInt16,
38-
UInt24 = 509,
39-
UInt32 = 503,
40+
UInt32,
4041
UInt64 = 508,
42+
UInt24,
4143
Binary = 600,
4244
VarBinary,
4345
TinyText = 749,

src/MySqlConnector/MySqlClient/MySqlParameter.cs

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,45 @@
22
using System.Data;
33
using System.Data.Common;
44
using System.IO;
5+
using MySql.Data.MySqlClient.Types;
56

67
namespace MySql.Data.MySqlClient
78
{
89
public sealed class MySqlParameter : DbParameter
910
{
1011
public MySqlParameter()
1112
{
13+
m_mySqlDbType = MySqlDbType.VarChar;
1214
}
1315

1416
public MySqlParameter(string name, object objValue)
1517
{
18+
m_mySqlDbType = MySqlDbType.VarChar;
1619
ParameterName = name;
1720
Value = objValue;
1821
}
1922

20-
public override DbType DbType { get; set; }
23+
public override DbType DbType
24+
{
25+
get => m_dbType;
26+
set
27+
{
28+
m_dbType = value;
29+
m_mySqlDbType = TypeMapper.Instance.GetMySqlDbTypeForDbType(value);
30+
HasSetDbType = true;
31+
}
32+
}
33+
34+
public MySqlDbType MySqlDbType
35+
{
36+
get => m_mySqlDbType;
37+
set
38+
{
39+
m_dbType = TypeMapper.Instance.GetDbTypeForMySqlDbType(value);
40+
m_mySqlDbType = value;
41+
HasSetDbType = true;
42+
}
43+
}
2144

2245
public override ParameterDirection Direction
2346
{
@@ -77,22 +100,14 @@ public override void ResetDbType()
77100
DbType = default(DbType);
78101
}
79102

80-
public MySqlDbType MySqlDbType {
81-
get {
82-
return Types.TypeMapper.ConverToMySqlDbType(DbType);
83-
}
84-
set {
85-
DbType = Types.TypeMapper.ConvertFromMySqlDbType(MySqlDbType);
86-
}
87-
}
88-
89-
90103
internal MySqlParameter WithParameterName(string parameterName) => new MySqlParameter(this, parameterName);
91104

92105
private MySqlParameter(MySqlParameter other, string parameterName)
93106
{
94-
DbType = other.DbType;
107+
m_dbType = other.m_dbType;
108+
m_mySqlDbType = other.m_mySqlDbType;
95109
m_direction = other.m_direction;
110+
HasSetDbType = other.HasSetDbType;
96111
IsNullable = other.IsNullable;
97112
Size = other.Size;
98113
ParameterName = parameterName ?? other.ParameterName;
@@ -105,6 +120,8 @@ private MySqlParameter(MySqlParameter other, string parameterName)
105120

106121
internal bool HasSetDirection => m_direction.HasValue;
107122

123+
internal bool HasSetDbType { get; set; }
124+
108125
internal string NormalizedParameterName { get; private set; }
109126

110127
internal void AppendSqlString(BinaryWriter writer, StatementPreparerOptions options)
@@ -190,27 +207,27 @@ internal void AppendSqlString(BinaryWriter writer, StatementPreparerOptions opti
190207
writer.WriteUtf8("'{0:D}'".FormatInvariant(guidValue));
191208
}
192209
}
193-
else if (DbType == DbType.Int16)
210+
else if (MySqlDbType == MySqlDbType.Int16)
194211
{
195212
writer.WriteUtf8("{0}".FormatInvariant((short) Value));
196213
}
197-
else if (DbType == DbType.UInt16)
214+
else if (MySqlDbType == MySqlDbType.UInt16)
198215
{
199216
writer.WriteUtf8("{0}".FormatInvariant((ushort) Value));
200217
}
201-
else if (DbType == DbType.Int32)
218+
else if (MySqlDbType == MySqlDbType.Int32)
202219
{
203220
writer.WriteUtf8("{0}".FormatInvariant((int) Value));
204221
}
205-
else if (DbType == DbType.UInt32)
222+
else if (MySqlDbType == MySqlDbType.UInt32)
206223
{
207224
writer.WriteUtf8("{0}".FormatInvariant((uint) Value));
208225
}
209-
else if (DbType == DbType.Int64)
226+
else if (MySqlDbType == MySqlDbType.Int64)
210227
{
211228
writer.WriteUtf8("{0}".FormatInvariant((long) Value));
212229
}
213-
else if (DbType == DbType.UInt64)
230+
else if (MySqlDbType == MySqlDbType.UInt64)
214231
{
215232
writer.WriteUtf8("{0}".FormatInvariant((ulong) Value));
216233
}
@@ -238,6 +255,8 @@ internal static string NormalizeParameterName(string name)
238255
return name.StartsWith("@", StringComparison.Ordinal) || name.StartsWith("?", StringComparison.Ordinal) ? name.Substring(1) : name;
239256
}
240257

258+
DbType m_dbType;
259+
MySqlDbType m_mySqlDbType;
241260
string m_name;
242261
ParameterDirection? m_direction;
243262
}

0 commit comments

Comments
 (0)