public string GetTypeNameForTableType(Definition.TableType tableType) { return($"{tableType.Name}"); }
public SourceText GetTableTypeText(Definition.Schema schema, Definition.TableType tableType) { var rootDir = _output.GetOutputRootDir(); var fileContent = File.ReadAllText(Path.Combine(rootDir.FullName, "DataContext", "TableTypes", "TableType.cs")); var tree = CSharpSyntaxTree.ParseText(fileContent); var root = tree.GetCompilationUnitRoot(); // Replace Namespace if (_configFile.Config.Project.Role.Kind == ERoleKind.Lib) { root = root.ReplaceNamespace(ns => ns.Replace("Source.DataContext", _configFile.Config.Project.Output.Namespace).Replace("Schema", schema.Name)); } else { root = root.ReplaceNamespace(ns => ns.Replace("Source", _configFile.Config.Project.Output.Namespace).Replace("Schema", schema.Name)); } // If its an extension, add usings for the lib if (_configFile.Config.Project.Role.Kind == ERoleKind.Extension) { var libModelUsingDirective = SyntaxFactory.UsingDirective(SyntaxFactory.ParseName($"{_configFile.Config.Project.Role.LibNamespace}.TableTypes")); root = root.AddUsings(libModelUsingDirective).NormalizeWhitespace(); } var nsNode = (NamespaceDeclarationSyntax)root.Members[0]; // Replace ClassName var classNode = (ClassDeclarationSyntax)nsNode.Members[0]; var classIdentifier = SyntaxFactory.ParseToken($"{classNode.Identifier.ValueText.Replace("TableType", $"{GetTypeNameForTableType(tableType)}")} "); classNode = classNode.WithIdentifier(classIdentifier); root = root.ReplaceNode(nsNode, nsNode.AddMembers(classNode)); // Create Properties if (tableType.Columns != null) { foreach (var column in tableType.Columns) { nsNode = (NamespaceDeclarationSyntax)root.Members[0]; classNode = (ClassDeclarationSyntax)nsNode.Members[1]; var propertyNode = (PropertyDeclarationSyntax)classNode.Members[0]; var propertyIdentifier = SyntaxFactory.ParseToken($" {column.Name} "); propertyNode = propertyNode .WithType(ParseTypeFromSqlDbTypeName(column.SqlTypeName, column.IsNullable ?? false)); propertyNode = propertyNode .WithIdentifier(propertyIdentifier); // Add Attribute for NVARCHAR with MaxLength if (column.SqlTypeName.Equals(SqlDbType.NVarChar.ToString(), StringComparison.InvariantCultureIgnoreCase) && column.MaxLength.HasValue) { var attributes = propertyNode.AttributeLists.Add( SyntaxFactory.AttributeList(SyntaxFactory.SingletonSeparatedList <AttributeSyntax>( SyntaxFactory.Attribute(SyntaxFactory.IdentifierName("MaxLength"), SyntaxFactory.ParseAttributeArgumentList($"({column.MaxLength})")) )).NormalizeWhitespace()); propertyNode = propertyNode.WithAttributeLists(attributes); } root = root.AddProperty(ref classNode, propertyNode); } } // Remove template Property nsNode = (NamespaceDeclarationSyntax)root.Members[0]; classNode = (ClassDeclarationSyntax)nsNode.Members[1]; root = root.ReplaceNode(classNode, classNode.WithMembers(new SyntaxList <MemberDeclarationSyntax>(classNode.Members.Cast <PropertyDeclarationSyntax>().Skip(1)))); // Remove template Class nsNode = (NamespaceDeclarationSyntax)root.Members[0]; root = root.ReplaceNode(nsNode, nsNode.WithMembers(new SyntaxList <MemberDeclarationSyntax>(nsNode.Members.Cast <ClassDeclarationSyntax>().Skip(1)))); return(root.NormalizeWhitespace().GetText()); }