private EntityInfo CreateEntityInfo <T>(IEnumerable <PropertyInfo> props) { var t = typeof(T); var tableName = NpgsqlHelper.GetTableName(context, t); var mappingInfo = GetMappingInfo(t, tableName); var tableNameQualified = NpgsqlHelper.GetTableNameQualified(context, t); // filter by props var keyProps = mappingInfo.Where(x => x.IsKey).Select(x => x.Property); var propToInfo = mappingInfo.ToDictionary(x => x.Property); mappingInfo = props.Union(keyProps).Select(x => propToInfo[x]).ToList(); // key for partial coe builder var propNames = typeof(T).FullName + "_" + string.Join("_", props.Select(x => $"{x.DeclaringType.Name}_{x.Name}")); NpgsqlBulkCodeBuilder <T> codeBuilder; if (partialCodeBuilders.TryGetValue(propNames, out object cb)) { codeBuilder = (NpgsqlBulkCodeBuilder <T>)cb; return(CreateEntityInfo <T>(tableName, tableNameQualified, mappingInfo, codeBuilder)); } else { codeBuilder = new NpgsqlBulkCodeBuilder <T>(); var info = CreateEntityInfo <T>(tableName, tableNameQualified, mappingInfo, codeBuilder); codeBuilder.InitBuilder(info, ReadValue); partialCodeBuilders.TryAdd(propNames, codeBuilder); return(info); } }
internal EntityInfo CreateEntityInfo <T>() { var t = typeof(T); var tableName = NpgsqlHelper.GetTableName(context, t); var mappingInfo = GetMappingInfo(t, tableName); var tableNameQualified = NpgsqlHelper.GetTableNameQualified(context, t); return(CreateEntityInfo <T>(tableName, tableNameQualified, mappingInfo)); }
private EntityInfo CreateEntityInfo <T>() { var t = typeof(T); var tableName = NpgsqlHelper.GetTableName(context, t); var mappingInfo = GetMappingInfo(t, tableName); var codeBuilder = new NpgsqlBulkCodeBuilder <T>(); var info = new EntityInfo() { TableNameQualified = NpgsqlHelper.GetTableNameQualified(context, t), TableName = tableName, CodeBuilder = codeBuilder, MappingInfos = mappingInfo, TableNames = mappingInfo.Select(x => x.TableName).Distinct().ToArray(), ClientDataInfos = mappingInfo.Where(x => !x.IsDbGenerated).ToArray(), ClientDataWithKeysInfos = mappingInfo.Where(x => !x.IsDbGenerated || x.IsKey).ToArray() }; var grouppedByTables = mappingInfo.GroupBy(x => x.TableName) .Select(x => new { TableName = x.Key, x.First().TableNameQualified, KeyInfos = x.Where(y => y.IsKey).ToList(), ClientDataInfos = x.Where(y => !y.IsDbGenerated).ToList(), ReturningInfos = x.Where(y => y.IsDbGenerated).ToList() }).ToList(); info.InsertQueryParts = grouppedByTables.Select(x => { var others = grouppedByTables.Where(y => y.TableName != x.TableName) .SelectMany(y => y.ClientDataInfos) .Select(y => new { My = y, Others = x.ReturningInfos.FirstOrDefault(ri => ri.Property.Name == y.Property.Name) }) .Where(y => y.Others != null) .ToList(); return(new InsertQueryParts() { TableName = x.TableName, TableNameQualified = x.TableNameQualified, TargetColumnNamesQueryPart = string.Join(", ", x.ClientDataInfos.Select(y => NpgsqlHelper.GetQualifiedName(y.ColumnInfo.ColumnName))), SourceColumnNamesQueryPart = string.Join(", ", x.ClientDataInfos.Select(y => y.TempAliasedColumnName)), Returning = string.Join(", ", x.ReturningInfos.Select(y => y.ColumnInfo.ColumnName)), ReturningSetQueryPart = string.Join(", ", others.Select(y => $"{y.My.TempAliasedColumnName} = source.{y.Others.ColumnInfo.ColumnName}")) }); }).ToList(); info.SelectSourceForInsertQuery = "SELECT " + string.Join(", ", info.ClientDataInfos .Select(x => $"{x.QualifiedColumnName} AS {x.TempAliasedColumnName}")) + " FROM " + string.Join(", ", grouppedByTables.Select(x => x.TableNameQualified)); info.CopyColumnsForInsertQueryPart = string.Join(", ", info.ClientDataInfos .Select(x => x.TempAliasedColumnName)); info.UpdateQueryParts = grouppedByTables.Select(x => { var updateableInfos = x.ClientDataInfos; updateableInfos = updateableInfos.Where( y => y.ModifierAttributes == null || y.ModifierAttributes.All( m => m.Modification != BulkOperationModification.IgnoreForUpdate) ).ToList(); return(new UpdateQueryParts() { TableName = x.TableName, TableNameQualified = x.TableNameQualified, SetClause = string.Join(", ", updateableInfos.Select(y => { var colName = NpgsqlHelper.GetQualifiedName(y.ColumnInfo.ColumnName); return $"{colName} = source.{y.TempAliasedColumnName}"; })), WhereClause = string.Join(", ", x.KeyInfos.Select(y => { var colName = NpgsqlHelper.GetQualifiedName(y.ColumnInfo.ColumnName); return $"{colName} = source.{y.TempAliasedColumnName}"; })) }); }).ToList(); info.SelectSourceForUpdateQuery = "SELECT " + string.Join(", ", info.ClientDataWithKeysInfos .Select(x => $"{x.QualifiedColumnName} AS {x.TempAliasedColumnName}")) + " FROM " + string.Join(", ", grouppedByTables.Select(x => x.TableNameQualified)); info.CopyColumnsForUpdateQueryPart = string.Join(", ", info.ClientDataWithKeysInfos .Select(x => x.TempAliasedColumnName)); info.ClientDataColumnNames = string.Join(", ", info.ClientDataInfos.Select(x => NpgsqlHelper.GetQualifiedName(x.ColumnInfo.ColumnName))); info.KeyInfos = info.MappingInfos.Where(x => x.IsKey).ToArray(); info.KeyColumnNames = info.KeyInfos.Select(x => x.ColumnInfo.ColumnName).ToArray(); info.ClientDataWithKeysColumnNames = string.Join(", ", info.ClientDataWithKeysInfos.Select(x => NpgsqlHelper.GetQualifiedName(x.ColumnInfo.ColumnName))); info.DbGeneratedInfos = info.MappingInfos.Where(x => x.IsDbGenerated).ToArray(); info.DbGeneratedColumnNames = string.Join(", ", info.DbGeneratedInfos.Select(x => NpgsqlHelper.GetQualifiedName(x.ColumnInfo.ColumnName))); codeBuilder.InitBuilder(info.ClientDataInfos, info.ClientDataWithKeysInfos, info.DbGeneratedInfos, ReadValue); return(info); }