/// <summary>Gets column database type.</summary> /// <param name="valueType">Type of the value.</param> /// <returns>The column database type.</returns> /// <summary>Gets column type definition.</summary> /// <param name="fieldType">Type of the field.</param> /// <param name="fieldName"></param> /// <param name="fieldLength"></param> /// <returns>The column type definition.</returns> public virtual string GetColumnTypeDefinition(Type fieldType, string fieldName, int?fieldLength) { string fieldDefinition; SqlMapper.ITypeHandler typeHandler = null; #pragma warning disable 618 var dbType = SqlMapper.LookupDbType(fieldType, fieldName, false, out typeHandler); #pragma warning restore 618 var typeHandlerColumnType = typeHandler as ITypeHandlerColumnType; if (typeHandlerColumnType != null) { dbType = typeHandlerColumnType.ColumnType; fieldLength = typeHandlerColumnType.Length; } if (dbType == DbType.AnsiString || dbType == DbType.AnsiStringFixedLength || dbType == DbType.String || dbType == DbType.StringFixedLength) { fieldDefinition = string.Format(StringLengthColumnDefinitionFormat, fieldLength ?? DefaultStringLength); } else { if (!DbTypeMap.ColumnDbTypeMap.TryGetValue(dbType, out fieldDefinition)) { fieldDefinition = GetUndefinedColumnDefinition(fieldType, fieldLength); } } return(fieldDefinition ?? GetUndefinedColumnDefinition(fieldType, null)); }
public void Can_convert_localtime_to_timespan(int hour, int minute, int second) { SqlMapper.ITypeHandler handler = CreateHandler(); var localTime = new LocalTime(hour, minute, second); IDbDataParameter parameter = CreateParameter(); handler.SetValue(parameter, localTime); Assert.Equal(new TimeSpan(0, hour, minute, second), parameter.Value); }
public static bool HasTypeHandler(Type type, out SqlMapper.ITypeHandler handler) { if (Handlers.ContainsKey(type)) { handler = Handlers[type]; return(true); } else { handler = null; return(false); } }
public void Can_convert_instant_to_datetime(int year, int month, int day, int hour, int minute, int second) { SqlMapper.ITypeHandler handler = CreateHandler(); var dateTime = new DateTime(year, month, day, hour, minute, second, DateTimeKind.Utc); Instant instant = Instant.FromDateTimeUtc(dateTime); IDbDataParameter parameter = CreateParameter(); handler.SetValue(parameter, instant); Assert.Equal(DbType.DateTime2, parameter.DbType); Assert.Equal(dateTime, parameter.Value); Assert.Equal(DateTimeKind.Utc, dateTime.Kind); }
public void Can_convert_datetime_to_instant(int year, int month, int day, int hour, int minute, int second) { SqlMapper.ITypeHandler handler = CreateHandler(); var dateTimeUtc = new DateTime(year, month, day, hour, minute, second, DateTimeKind.Utc); var dateTimeLocal = new DateTime(year, month, day, hour, minute, second, DateTimeKind.Local); var dateTimeUnspecified = new DateTime(year, month, day, hour, minute, second, DateTimeKind.Unspecified); var instantUtc = (Instant)handler.Parse(typeof(Instant), dateTimeUtc); var instantLocal = (Instant)handler.Parse(typeof(Instant), dateTimeLocal); var instantUnspecified = (Instant)handler.Parse(typeof(Instant), dateTimeUnspecified); Assert.Equal(Instant.FromDateTimeUtc(dateTimeUtc), instantUtc); Assert.Equal(Instant.FromDateTimeUtc(DateTime.SpecifyKind(dateTimeLocal, DateTimeKind.Utc)), instantLocal); Assert.Equal(Instant.FromDateTimeUtc(DateTime.SpecifyKind(dateTimeUnspecified, DateTimeKind.Utc)), instantUnspecified); }
public static void Register(SqlMapper.ITypeHandler typeHandler) { for (var type = typeHandler.GetType(); typeof(object) != type; type = type.BaseType) { if (type == null) { break; } if (!type.IsGenericType || type.GetGenericTypeDefinition() != typeof(SqlMapper.TypeHandler <>)) { continue; } SqlMapper.AddTypeHandler(type.GenericTypeArguments.Single(), typeHandler); return; } // logger; }
public static IServiceCollection AddApplicationQueries(this IServiceCollection services) { services.AddScoped <SqlServerDbConnectionFactory>(provider => { IConfiguration configuration = provider.GetRequiredService <IConfiguration>(); string connectionString = configuration.GetConnectionString("AppDbContext"); return(new SqlServerDbConnectionFactory(connectionString)); }); services.AddMediatR(typeof(SqlServerDbConnectionFactory).Assembly); services.AddValidatorsFromAssembly(typeof(SqlServerDbConnectionFactory).Assembly); StronglyTypedIdTypeDescriptor.AddStronglyTypedIdConverter((idType) => { Type idTypeHandler = typeof(StronglyTypedIdMapper <>).MakeGenericType(idType); SqlMapper.ITypeHandler idTypeHandlerInstance = (SqlMapper.ITypeHandler)Activator.CreateInstance(idTypeHandler); SqlMapper.AddTypeHandler(idType, idTypeHandlerInstance); }); return(services); }
=> dbType != EnumerableMultiParameter; // just in case called with non-nullable /// <summary> /// Add all the parameters needed to the command just before it executes /// </summary> /// <param name="command">The raw command prior to execution</param> /// <param name="identity">Information about the query</param> protected void AddParameters(IDbCommand command, SqlMapper.Identity identity) { var literals = SqlMapper.GetLiteralTokens(identity.sql); if (templates != null) { foreach (var template in templates) { var newIdent = identity.ForDynamicParameters(template.GetType()); Action <IDbCommand, object> appender; lock (paramReaderCache) { if (!paramReaderCache.TryGetValue(newIdent, out appender)) { appender = SqlMapper.CreateParamInfoGenerator(newIdent, true, RemoveUnused, literals); paramReaderCache[newIdent] = appender; } } appender(command, template); } // The parameters were added to the command, but not the // DynamicParameters until now. foreach (IDbDataParameter param in command.Parameters) { // If someone makes a DynamicParameters with a template, // then explicitly adds a parameter of a matching name, // it will already exist in 'parameters'. if (!parameters.ContainsKey(param.ParameterName)) { parameters.Add(param.ParameterName, new ParamInfo { AttachedParam = param, CameFromTemplate = true, DbType = param.DbType, Name = param.ParameterName, ParameterDirection = param.Direction, Size = param.Size, Value = param.Value }); } } // Now that the parameters are added to the command, let's place our output callbacks var tmp = outputCallbacks; if (tmp != null) { foreach (var generator in tmp) { generator(); } } } foreach (var param in parameters.Values) { if (param.CameFromTemplate) { continue; } var dbType = param.DbType; var val = param.Value; string name = Clean(param.Name); var isCustomQueryParameter = val is SqlMapper.ICustomQueryParameter; SqlMapper.ITypeHandler handler = null; if (dbType == null && val != null && !isCustomQueryParameter) { #pragma warning disable 618 dbType = SqlMapper.LookupDbType(val.GetType(), name, true, out handler); #pragma warning disable 618 } if (isCustomQueryParameter) { ((SqlMapper.ICustomQueryParameter)val).AddParameter(command, name); } else if (dbType == EnumerableMultiParameter) { #pragma warning disable 612, 618 SqlMapper.PackListParameters(command, name, val); #pragma warning restore 612, 618 } else { bool add = !command.Parameters.Contains(name); IDbDataParameter p; if (add) { p = command.CreateParameter(); p.ParameterName = name; } else { p = (IDbDataParameter)command.Parameters[name]; } p.Direction = param.ParameterDirection; if (handler == null) { #pragma warning disable 0618 p.Value = SqlMapper.SanitizeParameterValue(val); #pragma warning restore 0618 if (ShouldSetDbType(dbType) && p.DbType != dbType.GetValueOrDefault()) { p.DbType = dbType.GetValueOrDefault(); } var s = val as string; if (s?.Length <= DbString.DefaultLength) { p.Size = DbString.DefaultLength; } if (param.Size != null) { p.Size = param.Size.Value; } if (param.Precision != null) { p.Precision = param.Precision.Value; } if (param.Scale != null) { p.Scale = param.Scale.Value; } } else { if (ShouldSetDbType(dbType)) { p.DbType = dbType.GetValueOrDefault(); } if (param.Size != null) { p.Size = param.Size.Value; } if (param.Precision != null) { p.Precision = param.Precision.Value; } if (param.Scale != null) { p.Scale = param.Scale.Value; } handler.SetValue(p, val ?? DBNull.Value); } if (add) { command.Parameters.Add(p); } param.AttachedParam = p; } } // note: most non-privileged implementations would use: this.ReplaceLiterals(command); if (literals.Count != 0) { SqlMapper.ReplaceLiterals(this, command, literals); } }
/// <summary> /// Register dapper type handler<br/> /// 注册dapper类型处理器<br/> /// </summary> public static void Register(Type type, SqlMapper.ITypeHandler handler) { // Dapper will replace exists handler, and no need to clone typeHandlers SqlMapper.AddTypeHandlerImpl(type, handler, false); }
public static void AddTypeHandler <T>(SqlMapper.ITypeHandler handler) { Handlers[typeof(T)] = handler; SqlMapper.AddTypeHandler(typeof(T), handler); }
public static void AddTypeHandler(Type type, SqlMapper.ITypeHandler handler) { Handlers[type] = handler; SqlMapper.AddTypeHandler(type, handler); }
// Token: 0x0600003A RID: 58 RVA: 0x00002DA0 File Offset: 0x00000FA0 protected void AddParameters(IDbCommand command, SqlMapper.Identity identity) { IList <SqlMapper.LiteralToken> literalTokens = SqlMapper.GetLiteralTokens(identity.sql); if (this.templates != null) { foreach (object obj in this.templates) { SqlMapper.Identity identity2 = identity.ForDynamicParameters(obj.GetType()); Dictionary <SqlMapper.Identity, Action <IDbCommand, object> > obj2 = DynamicParameters.paramReaderCache; Action <IDbCommand, object> action; lock (obj2) { if (!DynamicParameters.paramReaderCache.TryGetValue(identity2, out action)) { action = SqlMapper.CreateParamInfoGenerator(identity2, true, this.RemoveUnused, literalTokens); DynamicParameters.paramReaderCache[identity2] = action; } } action(command, obj); } foreach (object obj3 in command.Parameters) { IDbDataParameter dbDataParameter = (IDbDataParameter)obj3; if (!this.parameters.ContainsKey(dbDataParameter.ParameterName)) { this.parameters.Add(dbDataParameter.ParameterName, new DynamicParameters.ParamInfo { AttachedParam = dbDataParameter, CameFromTemplate = true, DbType = new DbType?(dbDataParameter.DbType), Name = dbDataParameter.ParameterName, ParameterDirection = dbDataParameter.Direction, Size = new int?(dbDataParameter.Size), Value = dbDataParameter.Value }); } } List <Action> list = this.outputCallbacks; if (list != null) { foreach (Action action2 in list) { action2(); } } } foreach (DynamicParameters.ParamInfo paramInfo in this.parameters.Values) { if (!paramInfo.CameFromTemplate) { DbType?dbType = paramInfo.DbType; object value = paramInfo.Value; string text = DynamicParameters.Clean(paramInfo.Name); bool flag2 = value is SqlMapper.ICustomQueryParameter; SqlMapper.ITypeHandler typeHandler = null; if (dbType == null && value != null && !flag2) { dbType = new DbType?(SqlMapper.LookupDbType(value.GetType(), text, true, out typeHandler)); } if (flag2) { ((SqlMapper.ICustomQueryParameter)value).AddParameter(command, text); } else if (dbType == (DbType)(-1)) { SqlMapper.PackListParameters(command, text, value); } else { bool flag3 = !command.Parameters.Contains(text); IDbDataParameter dbDataParameter2; if (flag3) { dbDataParameter2 = command.CreateParameter(); dbDataParameter2.ParameterName = text; } else { dbDataParameter2 = (IDbDataParameter)command.Parameters[text]; } dbDataParameter2.Direction = paramInfo.ParameterDirection; if (typeHandler == null) { dbDataParameter2.Value = SqlMapper.SanitizeParameterValue(value); if (dbType != null && dbDataParameter2.DbType != dbType) { dbDataParameter2.DbType = dbType.Value; } string text2 = value as string; if (text2 != null && text2.Length <= 4000) { dbDataParameter2.Size = 4000; } if (paramInfo.Size != null) { dbDataParameter2.Size = paramInfo.Size.Value; } if (paramInfo.Precision != null) { dbDataParameter2.Precision = paramInfo.Precision.Value; } if (paramInfo.Scale != null) { dbDataParameter2.Scale = paramInfo.Scale.Value; } } else { if (dbType != null) { dbDataParameter2.DbType = dbType.Value; } if (paramInfo.Size != null) { dbDataParameter2.Size = paramInfo.Size.Value; } if (paramInfo.Precision != null) { dbDataParameter2.Precision = paramInfo.Precision.Value; } if (paramInfo.Scale != null) { dbDataParameter2.Scale = paramInfo.Scale.Value; } typeHandler.SetValue(dbDataParameter2, value ?? DBNull.Value); } if (flag3) { command.Parameters.Add(dbDataParameter2); } paramInfo.AttachedParam = dbDataParameter2; } } } if (literalTokens.Count != 0) { SqlMapper.ReplaceLiterals(this, command, literalTokens); } }
/// <summary> /// Configure the specified type to be processed by a custom /// <see href="https://stackexchange.github.io/Dapper/">Dapper</see> handler. /// </summary> /// <param name="contextBuilder"> /// The builder being used to configure the context. /// </param> /// <param name="type">The type to handle.</param> /// <param name="handler">The handler for the type <paramref name="type"/>.</param> /// <param name="clone">Whether to clone the current type handler map.</param> /// <returns> /// The context builder so that further configuration can be chained. /// </returns> public static DapperContextBuilder AddTypeHandlerImpl(this DapperContextBuilder contextBuilder, Type type, SqlMapper.ITypeHandler handler, bool clone) { SqlMapper.AddTypeHandlerImpl(type, handler, clone); return(contextBuilder); }
/// <summary> /// Configure the specified type to be processed by a custom /// <see href="https://stackexchange.github.io/Dapper/">Dapper</see> handler. /// </summary> /// <param name="contextBuilder"> /// The builder being used to configure the context. /// </param> /// <param name="type">The type to handle.</param> /// <param name="handler">The handler for the type <paramref name="type"/>.</param> /// <returns> /// The context builder so that further configuration can be chained. /// </returns> public static DapperContextBuilder AddTypeHandler(this DapperContextBuilder contextBuilder, Type type, SqlMapper.ITypeHandler handler) { SqlMapper.AddTypeHandler(type, handler); return(contextBuilder); }