protected IEnumerable <object> InitializeBuilderForUpdate <T>(ISqlBuilder builder, T entity) where T : IDbEntity { var metadata = entity; if (metadata.PrimaryColumns == null || metadata.PrimaryColumns.Length == 0) { throw new InvalidDataException("No primary key defined."); } var count = 0; builder.Schema = (metadata.TableOverride ?? metadata.Table).Schema; builder.Table = (metadata.TableOverride ?? metadata.Table).Name; // ReSharper disable AccessToModifiedClosure metadata.Changes.ForEach( c => builder.AddSetter(c, count++) ); metadata.PrimaryColumns.ForEach( k => builder.AddWhere(k.Name, count++) ); // ReSharper restore AccessToModifiedClosure return(entity.Changes.Select(entity.GetProperty) .Concat( entity.PrimaryColumns .Select(p => entity.GetProperty(p.Name.Trim())) // todo: this trim here is a fix to a sql column have training space in it's name. it's not optimal, but no other solution is presenting itself to me. )); }
protected static IEnumerable <object> InitializeBuilderForInsert <T>(ISqlBuilder builder, T entity) where T : IDbEntity { var count = 0; var metadata = entity; builder.Schema = (metadata.TableOverride ?? metadata.Table).Schema; builder.Table = (metadata.TableOverride ?? metadata.Table).Name; entity.Changes.ForEach(c => builder.AddSetter(c, count++)); var parameters = entity.Changes.Select( entity.GetProperty ); return(parameters); }