internal static string ExecuteStoredProcInternal(DbConnection connection, string procName, object parameter) { const string sql = @"select p.name, t.name as [type], p.max_length from sys.all_parameters p join sys.systypes t on t.xtype = p.system_type_id where p.[object_id] = OBJECT_ID(@procName, 'P') order by p.parameter_id"; var procCols = connection.Query(sql, new { procName }).Read <ProcColumn>().ToList(); Console.WriteLine($"proc has {procCols.Count} parameters"); var cols = new List <Thing>(); int i = 0; foreach (var pc in procCols) { cols.Add(ProcColToColumn(pc, i++)); } var mappings = Mapping.CreateFromSource(cols, Types.WriteablePublicThings(parameter.GetType())); var sb = new StringBuilder(100); sb.Append("EXEC ").Append(procName).Append(" "); foreach (var map in mappings.Mapped) { sb.AppendLine().Append(" @").Append(map.To.Name).Append(" = @").Append(map.From.Name).Append(","); } sb.Length -= 1; // remove last comma return(sb.ToString()); }
static Delegate CreateMapFunc(Type typeT, IReadOnlyCollection <Column> columns) { Contract.Requires(columns != null); Contract.Requires(typeT != null); Contract.Ensures(Contract.Result <Delegate>() != null); if (typeT.IsPrimitiveOrEnum() || typeT.IsNullable() || typeT == typeof(string) || typeT == typeof(Guid) || typeT == typeof(DateTime) || typeT == typeof(DateTimeOffset) || (typeT.IsClass == false && typeT.Name.Contains("Id")) && typeT.Namespace.Contains("BusterWood")) // special case for Id, IntId, LongId and GuidId structs { return(CreatePrimativeMapFunc(typeT, columns)); } MappingResult <Thing, Thing> result = Mapping.CreateFromSource(columns.Cast <Thing>().ToList(), Types.WriteablePublicThings(typeT), typeT.Name); if (result.Mapped.Count == 0) { throw new InvalidOperationException("No columns were mapped to type " + typeT); } var readerParam = Expression.Parameter(typeof(DbDataReader), "reader"); var resultParam = Expression.Parameter(typeT, "result"); var block = CreateMapBlock(typeT, result.Mapped, readerParam, resultParam); var func = typeof(Func <,>).MakeGenericType(new[] { typeof(DbDataReader), typeT }); return(Expression.Lambda(func, block, new[] { readerParam }).Compile()); }
/// <summary>Fast copying code that generates a method that does the copy from one type to another</summary> static internal Delegate CreateMapDelegate(Type inType, Type outType) { Contract.Requires(outType != null); Contract.Requires(inType != null); Contract.Ensures(Contract.Result <Delegate>() != null); var result = Mapping.CreateFromSource(inType, outType, inType.Name); if (result.Mapped.Count == 0) { throw new InvalidOperationException("No fields or properties were mapped"); } LambdaExpression lambdaExpression = CreateMappingLambda(inType, outType, result.Mapped); return(lambdaExpression.Compile()); }