Skip to content

Commit

Permalink
Merge pull request #370 from bgrainger/mysqldbtype
Browse files Browse the repository at this point in the history
Expose and use MySqlDbType. Fixes #362
  • Loading branch information
bgrainger committed Oct 22, 2017
2 parents ccf3f4c + 6c5f42f commit dbc1f97
Show file tree
Hide file tree
Showing 17 changed files with 529 additions and 508 deletions.
4 changes: 2 additions & 2 deletions src/MySqlConnector/MySqlClient/Caches/CachedParameter.cs
@@ -1,4 +1,4 @@
using System.Data;
using System.Data;
using System.Linq;
using MySql.Data.MySqlClient.Types;

Expand Down Expand Up @@ -29,7 +29,7 @@ public CachedParameter(int ordinalPosition, string mode, string name, string dat
}
}
Name = name;
DbType = TypeMapper.Mapper.GetDbTypeMapping(dataType, unsigned).DbTypes?.First() ?? DbType.Object;
DbType = TypeMapper.Instance.GetDbTypeMapping(dataType, unsigned).DbTypes?.First() ?? DbType.Object;
}

internal readonly int Position;
Expand Down
8 changes: 3 additions & 5 deletions src/MySqlConnector/MySqlClient/Caches/CachedProcedure.cs
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
Expand All @@ -13,7 +13,7 @@ internal class CachedProcedure
{
internal static async Task<CachedProcedure> FillAsync(IOBehavior ioBehavior, MySqlConnection connection, string schema, string component, CancellationToken cancellationToken)
{
var cmd = (MySqlCommand) connection.CreateCommand();
var cmd = connection.CreateCommand();

cmd.CommandText = @"SELECT ORDINAL_POSITION, PARAMETER_MODE, PARAMETER_NAME, DATA_TYPE, DTD_IDENTIFIER
FROM information_schema.parameters
Expand All @@ -22,13 +22,11 @@ FROM information_schema.parameters
cmd.Parameters.Add(new MySqlParameter
{
ParameterName = "@schema",
DbType = DbType.String,
Value = schema
});
cmd.Parameters.Add(new MySqlParameter
{
ParameterName = "@component",
DbType = DbType.String,
Value = component
});

Expand Down Expand Up @@ -77,7 +75,7 @@ internal MySqlParameterCollection AlignParamsWithDb(MySqlParameterCollection par

if (!alignParam.HasSetDirection)
alignParam.Direction = cachedParam.Direction;
if (alignParam.DbType == default(DbType))
if (!alignParam.HasSetDbType)
alignParam.DbType = cachedParam.DbType;

// cached parameters are oredered by ordinal position
Expand Down
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
Expand Down Expand Up @@ -102,9 +102,9 @@ internal void SetParams()
for (var i = 0; i < m_outParams.Count; i++)
{
var param = m_outParams[i];
if (param.DbType != default(DbType))
if (param.HasSetDbType)
{
var dbTypeMapping = TypeMapper.Mapper.GetDbTypeMapping(param.DbType);
var dbTypeMapping = TypeMapper.Instance.GetDbTypeMapping(param.DbType);
if (dbTypeMapping != null)
{
param.Value = dbTypeMapping.DoConversion(reader.GetValue(i));
Expand Down
8 changes: 4 additions & 4 deletions src/MySqlConnector/MySqlClient/MySqlConnection.cs
Expand Up @@ -250,14 +250,14 @@ public override System.Data.DataTable GetSchema(string collectionName, string[]
});

// Column type mappings:
var colTypes = Types.TypeMapper.Mapper.GetColumnMappings();
var colTypes = Types.TypeMapper.Instance.GetColumnMappings();
foreach (var map in colTypes)
{
var dbTypeMap = map.DbTypeMapping;
var dbType = dbTypeMap.DbTypes.FirstOrDefault();
dt.Rows.Add(new object[] {
dbTypeMap.ClrType.FullName,
map.ColumnTypeName,
map.DataTypeName,
(int)dbType,
map.Unsigned
});
Expand All @@ -267,8 +267,8 @@ public override System.Data.DataTable GetSchema(string collectionName, string[]
foreach (MySqlDbType mapItem in Enum.GetValues(typeof(MySqlDbType)))
{
var typeName = Enum.GetName(typeof(MySqlDbType), mapItem);
var dbType = Types.TypeMapper.ConvertFromMySqlDbType(mapItem);
var map = Types.TypeMapper.Mapper.GetDbTypeMapping(dbType);
var dbType = Types.TypeMapper.Instance.GetDbTypeForMySqlDbType(mapItem);
var map = Types.TypeMapper.Instance.GetDbTypeMapping(dbType);
if (map != null) // MySqlDbType.Set is not supported by the mapper.
{
dt.Rows.Add(new object[] {
Expand Down
2 changes: 1 addition & 1 deletion src/MySqlConnector/MySqlClient/MySqlDataReader.cs
Expand Up @@ -241,7 +241,7 @@ public override void Close()
public ReadOnlyCollection<DbColumn> GetColumnSchema()
{
return GetResultSet().ColumnDefinitions
.Select((c, n) => (DbColumn) new MySqlDbColumn(n, c, GetFieldType(n), GetDataTypeName(n)))
.Select((c, n) => (DbColumn) new MySqlDbColumn(n, c, GetResultSet().ColumnTypes[n]))
.ToList().AsReadOnly();
}

Expand Down
15 changes: 11 additions & 4 deletions src/MySqlConnector/MySqlClient/MySqlDbColumn.cs
@@ -1,4 +1,6 @@
using System;
using System.Globalization;
using MySql.Data.MySqlClient.Types;
using MySql.Data.Serialization;

#if !NETSTANDARD1_3 && !NETSTANDARD2_0
Expand Down Expand Up @@ -38,8 +40,11 @@ namespace MySql.Data.MySqlClient
{
public sealed class MySqlDbColumn : System.Data.Common.DbColumn
{
internal MySqlDbColumn(int ordinal, ColumnDefinitionPayload column, Type type, string dataTypeName)
internal MySqlDbColumn(int ordinal, ColumnDefinitionPayload column, MySqlDbType mySqlDbType)
{
var columnTypeMetadata = TypeMapper.Instance.GetColumnTypeMetadata(mySqlDbType);

var type = columnTypeMetadata.DbTypeMapping.ClrType;
var columnSize = type == typeof(string) || type == typeof(Guid) ?
column.ColumnLength / SerializationUtility.GetBytesPerCharacter(column.CharacterSet) :
column.ColumnLength;
Expand All @@ -53,7 +58,9 @@ internal MySqlDbColumn(int ordinal, ColumnDefinitionPayload column, Type type, s
ColumnOrdinal = ordinal;
ColumnSize = columnSize > int.MaxValue ? int.MaxValue : unchecked((int) columnSize);
DataType = type;
DataTypeName = dataTypeName;
DataTypeName = columnTypeMetadata.SimpleDataTypeName;
if (mySqlDbType == MySqlDbType.String)
DataTypeName += string.Format(CultureInfo.InvariantCulture, "({0})", columnSize);
IsAliased = column.PhysicalName != column.Name;
IsAutoIncrement = (column.ColumnFlags & ColumnFlags.AutoIncrement) != 0;
IsExpression = false;
Expand All @@ -72,9 +79,9 @@ internal MySqlDbColumn(int ordinal, ColumnDefinitionPayload column, Type type, s
NumericPrecision--;
}
NumericScale = column.Decimals;
ProviderType = (int) column.ColumnType;
ProviderType = mySqlDbType;
}

public int ProviderType { get; }
public MySqlDbType ProviderType { get; }
}
}
18 changes: 10 additions & 8 deletions src/MySqlConnector/MySqlClient/MySqlDbType.cs
Expand Up @@ -4,16 +4,18 @@ namespace MySql.Data.MySqlClient
{
public enum MySqlDbType
{
Bool = -1,
Decimal,
Byte,
Int16,
Int24 = 9,
Int32 = 3,
Int64 = 8,
Float = 4,
Int32,
Float,
Double,
Timestamp = 7,
Date = 10,
Null,
Timestamp,
Int64,
Int24,
Date,
Time,
DateTime,
[Obsolete("The Datetime enum value is obsolete. Please use DateTime.")]
Expand All @@ -35,9 +37,9 @@ public enum MySqlDbType
Geometry,
UByte = 501,
UInt16,
UInt24 = 509,
UInt32 = 503,
UInt32,
UInt64 = 508,
UInt24,
Binary = 600,
VarBinary,
TinyText = 749,
Expand Down
55 changes: 37 additions & 18 deletions src/MySqlConnector/MySqlClient/MySqlParameter.cs
Expand Up @@ -2,22 +2,45 @@
using System.Data;
using System.Data.Common;
using System.IO;
using MySql.Data.MySqlClient.Types;

namespace MySql.Data.MySqlClient
{
public sealed class MySqlParameter : DbParameter
{
public MySqlParameter()
{
m_mySqlDbType = MySqlDbType.VarChar;
}

public MySqlParameter(string name, object objValue)
{
m_mySqlDbType = MySqlDbType.VarChar;
ParameterName = name;
Value = objValue;
}

public override DbType DbType { get; set; }
public override DbType DbType
{
get => m_dbType;
set
{
m_dbType = value;
m_mySqlDbType = TypeMapper.Instance.GetMySqlDbTypeForDbType(value);
HasSetDbType = true;
}
}

public MySqlDbType MySqlDbType
{
get => m_mySqlDbType;
set
{
m_dbType = TypeMapper.Instance.GetDbTypeForMySqlDbType(value);
m_mySqlDbType = value;
HasSetDbType = true;
}
}

public override ParameterDirection Direction
{
Expand Down Expand Up @@ -77,22 +100,14 @@ public override void ResetDbType()
DbType = default(DbType);
}

public MySqlDbType MySqlDbType {
get {
return Types.TypeMapper.ConverToMySqlDbType(DbType);
}
set {
DbType = Types.TypeMapper.ConvertFromMySqlDbType(MySqlDbType);
}
}


internal MySqlParameter WithParameterName(string parameterName) => new MySqlParameter(this, parameterName);

private MySqlParameter(MySqlParameter other, string parameterName)
{
DbType = other.DbType;
m_dbType = other.m_dbType;
m_mySqlDbType = other.m_mySqlDbType;
m_direction = other.m_direction;
HasSetDbType = other.HasSetDbType;
IsNullable = other.IsNullable;
Size = other.Size;
ParameterName = parameterName ?? other.ParameterName;
Expand All @@ -105,6 +120,8 @@ private MySqlParameter(MySqlParameter other, string parameterName)

internal bool HasSetDirection => m_direction.HasValue;

internal bool HasSetDbType { get; set; }

internal string NormalizedParameterName { get; private set; }

internal void AppendSqlString(BinaryWriter writer, StatementPreparerOptions options)
Expand Down Expand Up @@ -190,27 +207,27 @@ internal void AppendSqlString(BinaryWriter writer, StatementPreparerOptions opti
writer.WriteUtf8("'{0:D}'".FormatInvariant(guidValue));
}
}
else if (DbType == DbType.Int16)
else if (MySqlDbType == MySqlDbType.Int16)
{
writer.WriteUtf8("{0}".FormatInvariant((short) Value));
}
else if (DbType == DbType.UInt16)
else if (MySqlDbType == MySqlDbType.UInt16)
{
writer.WriteUtf8("{0}".FormatInvariant((ushort) Value));
}
else if (DbType == DbType.Int32)
else if (MySqlDbType == MySqlDbType.Int32)
{
writer.WriteUtf8("{0}".FormatInvariant((int) Value));
}
else if (DbType == DbType.UInt32)
else if (MySqlDbType == MySqlDbType.UInt32)
{
writer.WriteUtf8("{0}".FormatInvariant((uint) Value));
}
else if (DbType == DbType.Int64)
else if (MySqlDbType == MySqlDbType.Int64)
{
writer.WriteUtf8("{0}".FormatInvariant((long) Value));
}
else if (DbType == DbType.UInt64)
else if (MySqlDbType == MySqlDbType.UInt64)
{
writer.WriteUtf8("{0}".FormatInvariant((ulong) Value));
}
Expand Down Expand Up @@ -238,6 +255,8 @@ internal static string NormalizeParameterName(string name)
return name.StartsWith("@", StringComparison.Ordinal) || name.StartsWith("?", StringComparison.Ordinal) ? name.Substring(1) : name;
}

DbType m_dbType;
MySqlDbType m_mySqlDbType;
string m_name;
ParameterDirection? m_direction;
}
Expand Down

0 comments on commit dbc1f97

Please sign in to comment.