private static void CompileTableLike(ITableInfoModel tableInfoModel, IMsSqlCreator sourceCreator, string typeName, Stream to = null) { if (tableInfoModel.Exclude) { return; } var targetCsName = tableInfoModel.GetClassName(); var compiler = new ClassCompiler(sourceCreator.TargetDir, targetCsName); compiler.Type = typeName; compiler.CompileHeader = sourceCreator.GenerateCompilerHeader; compiler.Namespace = sourceCreator.Namespace; compiler.TableName = tableInfoModel.Info.TableName; if (to != null) { compiler.WriteAllways = true; } compiler.GenerateConfigMethod = sourceCreator.GenerateConfigMethod; if (tableInfoModel.WrapNullables || sourceCreator.WrapNullables) { compiler.AddAttribute(new CodeAttributeDeclaration(typeof(WrapDbNullablesAttribute).Name)); } if (tableInfoModel.CreateSelectFactory || sourceCreator.GenerateConstructor) { compiler.GenerateTypeConstructorBasedOnElements(tableInfoModel.ColumnInfos.Where(s => !s.Exclude)); } foreach (var columInfoModel in tableInfoModel.ColumnInfos) { if (columInfoModel.Exclude) { continue; } var codeMemberProperty = compiler.AddProperty(columInfoModel); if (columInfoModel.PrimaryKey) { codeMemberProperty.CustomAttributes.Add( new CodeAttributeDeclaration(typeof(PrimaryKeyAttribute).Name)); } if (columInfoModel.InsertIgnore) { codeMemberProperty.CustomAttributes.Add( new CodeAttributeDeclaration(typeof(InsertIgnoreAttribute).Name)); } if (columInfoModel.ForgeinKeyDeclarations != null) { var isRefTypeKnown = sourceCreator.Tables.FirstOrDefault(s => s.Info.TableName == columInfoModel.ForgeinKeyDeclarations.TableName); if (isRefTypeKnown == null) { codeMemberProperty.CustomAttributes.Add( new CodeAttributeDeclaration(typeof(ForeignKeyDeclarationAttribute).Name, new CodeAttributeArgument(new CodePrimitiveExpression(columInfoModel.ForgeinKeyDeclarations.TargetColumn)), new CodeAttributeArgument(new CodePrimitiveExpression(columInfoModel.ForgeinKeyDeclarations.TableName)))); } else { codeMemberProperty.CustomAttributes.Add( new CodeAttributeDeclaration(typeof(ForeignKeyDeclarationAttribute).Name, new CodeAttributeArgument(new CodePrimitiveExpression(columInfoModel.ForgeinKeyDeclarations.TargetColumn)), new CodeAttributeArgument(new CodeTypeOfExpression(isRefTypeKnown.GetClassName())))); } } if (columInfoModel.ColumnInfo.SqlType == SqlDbType.Timestamp) { codeMemberProperty.CustomAttributes.Add( new CodeAttributeDeclaration(typeof(RowVersionAttribute).Name)); } } if (tableInfoModel.CreateFallbackProperty) { compiler.AddFallbackProperty(); } Logger.WriteLine("Compile Class {0}", compiler.Name); compiler.Compile(tableInfoModel.ColumnInfos, sourceCreator.SplitByType, to); }
private static void CompileTableLike(ITableInfoModel tableInfoModel, IMsSqlCreator sourceCreator, string typeName, Stream to = null) { if (tableInfoModel.Exclude) { return; } var targetCsName = tableInfoModel.GetClassName(); var compiler = new ClassCompiler(sourceCreator.TargetDir, targetCsName); compiler.Type = typeName; compiler.CompileHeader = sourceCreator.GenerateCompilerHeader; compiler.Namespace = sourceCreator.Namespace; compiler.TableName = tableInfoModel.Info.TableName; if (to != null) { compiler.WriteAlways = true; } compiler.GenerateConfigMethod = sourceCreator.GenerateConfigMethod; foreach (var columInfoModel in tableInfoModel.ColumnInfos) { if (columInfoModel.Exclude) { continue; } var codeMemberProperty = compiler.AddProperty(columInfoModel); if (columInfoModel.PrimaryKey) { codeMemberProperty.Attributes.Add(new AttributeInfo() { Name = nameof(PrimaryKeyAttribute) }); } if (columInfoModel.InsertIgnore) { codeMemberProperty.Attributes.Add(new AttributeInfo() { Name = nameof(InsertIgnoreAttribute) }); } if (columInfoModel.ForgeinKeyDeclarations != null) { var isRefTypeKnown = sourceCreator.Tables .FirstOrDefault(s => s.Info.TableName == columInfoModel.ForgeinKeyDeclarations.TableName); if (isRefTypeKnown == null) { codeMemberProperty.Attributes.Add(new AttributeInfo() { Name = nameof(ForeignKeyDeclarationAttribute), ConstructorSetters = { { "foreignKey", "\"" + columInfoModel.ForgeinKeyDeclarations.TargetColumn + "\"" }, { "foreignTable", "\"" + columInfoModel.ForgeinKeyDeclarations.TableName + "\"" }, } }); } else { codeMemberProperty.Attributes.Add(new AttributeInfo() { Name = nameof(ForeignKeyDeclarationAttribute), ConstructorSetters = { { "foreignKey", $"\"{columInfoModel.ForgeinKeyDeclarations.TargetColumn}\"" }, { "foreignTable", $"typeof({isRefTypeKnown.GetClassName()})" }, } }); } } if (columInfoModel.ColumnInfo.SqlType == SqlDbType.Timestamp) { codeMemberProperty.Attributes.Add(new AttributeInfo() { Name = nameof(RowVersionAttribute) }); } if (tableInfoModel.CreateFallbackProperty) { compiler.AddFallbackProperty(); } } foreach (var columInfoModel in tableInfoModel.ColumnInfos) { if (columInfoModel.Exclude) { continue; } if (columInfoModel.ForgeinKeyDeclarations != null) { var isRefTypeKnown = sourceCreator.Tables .FirstOrDefault(s => s.Info.TableName == columInfoModel.ForgeinKeyDeclarations.TableName); if (isRefTypeKnown != null) { compiler.Generator.NamespaceImports.Add(typeof(EagarDataRecord).Namespace); compiler.Generator.NamespaceImports.Add(typeof(DbAccessLayerHelper).Namespace); var navPropertyName = isRefTypeKnown.GetClassName(); if (!navPropertyName.EndsWith("s")) { navPropertyName = navPropertyName.TrimEnd('s'); } var tempName = navPropertyName; var counter = 1; while (compiler.Generator.Properties.Any(f => f.Name == tempName || tempName == tableInfoModel.GetClassName())) { tempName = navPropertyName + counter++; } navPropertyName = tempName; var refProp = compiler.AddProperty(navPropertyName, null, new ClassType() { Name = isRefTypeKnown.GetClassName(), }); refProp.ForeignKey = new PropertyInfo.ForeignKeyDeclaration(columInfoModel.ForgeinKeyDeclarations.SourceColumn, columInfoModel.ForgeinKeyDeclarations.TargetColumn); refProp.Attributes.Add(new AttributeInfo() { Name = nameof(ForeignKeyAttribute), ConstructorSetters = { { "foreignKey", columInfoModel.ForgeinKeyDeclarations.SourceColumn.AsStringOfString() }, { "referenceKey", columInfoModel.ForgeinKeyDeclarations.TargetColumn.AsStringOfString() }, } }); } } } foreach (var infoModel in sourceCreator.Tables) { foreach (var columInfoModel in infoModel .ColumnInfos .Where(f => f.ForgeinKeyDeclarations?.TableName == tableInfoModel.Info.TableName)) { compiler.Generator.NamespaceImports.Add(typeof(EagarDataRecord).Namespace); compiler.Generator.NamespaceImports.Add(typeof(DbAccessLayerHelper).Namespace); var navPropertyName = infoModel.GetClassName(); if (!navPropertyName.EndsWith("s")) { navPropertyName = navPropertyName.TrimEnd('s'); } var tempName = navPropertyName; var counter = 1; while (compiler.Generator.Properties.Any(f => f.Name == tempName || tempName == tableInfoModel.GetClassName())) { tempName = navPropertyName + counter++; } navPropertyName = tempName; var refProp = compiler.AddProperty(navPropertyName, null, new ClassType() { Name = $"{nameof(DbCollection<object>)}", IsList = true, GenericTypes = { new ClassType() { Name = infoModel.GetClassName() } } }); refProp.ForeignKey = new PropertyInfo.ForeignKeyDeclaration(columInfoModel.ForgeinKeyDeclarations.SourceColumn, columInfoModel.ForgeinKeyDeclarations.TargetColumn); refProp.ForeignKey.DirectionFromParent = true; refProp.Attributes.Add(new AttributeInfo() { Name = nameof(ForeignKeyAttribute), ConstructorSetters = { { "referenceKey", columInfoModel.ForgeinKeyDeclarations.SourceColumn.AsStringOfString() }, { "foreignKey", columInfoModel.ForgeinKeyDeclarations.TargetColumn.AsStringOfString() }, } }); } } compiler.GenerateConfigMethod = true; compiler.Generator.GenerateFactory = tableInfoModel.CreateSelectFactory || sourceCreator.GenerateFactory; compiler.Generator.GenerateConstructor = sourceCreator.GenerateConstructor; Logger.WriteLine("Compile Class {0}", compiler.Name); compiler.Compile(tableInfoModel.ColumnInfos, sourceCreator.SplitByType, sourceCreator.SetNotifyProperties, to); }