public bool Alter(string column, AlterColumnAction alterColumnAction) { var index = GetIndex(column); if (index == -1) { return(false); } var previous = Columns[index]; var type = previous.Type; var isNullable = previous.IsNullable; var isAutoIncrementByType = previous.IsAutoIncrementByType; switch (alterColumnAction) { case DropNotNull _: isNullable = true; break; case SetNotNull _: isNullable = false; break; case AlterType a: isAutoIncrementByType = a.Type.ToUpper().Equals("SERIAL") || a.Type.ToUpper().Equals("BIGSERIAL"); type = ColumnParser.ParseType(a.Type); break; } Columns[index] = new Column(previous.Name, type, isNullable, previous.IsAutoIncrementByDefinition, isAutoIncrementByType); return(true); }
public static string GenerateClass(string ns, Table table, string entityInterface, string primaryKeyAttribute, string autoIncrementAttribute, string suffix) { var className = GetClassName(table, suffix); var entityInterfaces = ParseEntityInterfaces(entityInterface); var primaryKeyAttributeClass = ParseClassName(primaryKeyAttribute); var autoIncrementAttributeClass = ParseClassName(autoIncrementAttribute); var contentBuilder = new StringBuilder(); if (ColumnParser.RequiresSystemUsing(table.Columns)) { contentBuilder.Append("using System;\n"); } var hasPrimaryKeys = table.Columns .Any(c => table.IsPrimaryKey(c.Name)); var matchingInterfaces = entityInterfaces.Where(e => e.Properties.All(p => table.Columns.Exists(c => string.Equals(p.Item1, c.Name, StringComparison.CurrentCultureIgnoreCase)))) .ToImmutableList(); foreach (var matchingInterfaceNamespace in matchingInterfaces.Select(m => m.Namespace).Distinct()) { contentBuilder.Append($"using {matchingInterfaceNamespace};\n"); } if (primaryKeyAttributeClass != null && hasPrimaryKeys) { if (!matchingInterfaces.Exists(e => string.Equals($"{e.Namespace}", primaryKeyAttributeClass.Item1, StringComparison.CurrentCultureIgnoreCase))) { contentBuilder.Append($"using {primaryKeyAttributeClass.Item1};\n"); } var hasAutoIncrementKeys = table.Columns .Any(c => c.IsAutoIncrement); if (autoIncrementAttributeClass != null && hasAutoIncrementKeys && !matchingInterfaces.Exists(e => string.Equals($"{e.Namespace}", autoIncrementAttributeClass.Item1, StringComparison.CurrentCultureIgnoreCase)) && !autoIncrementAttributeClass.Item1.Equals(primaryKeyAttributeClass.Item1)) { contentBuilder.Append($"using {autoIncrementAttributeClass.Item1};\n"); } } contentBuilder.Append($"\nnamespace {ns}\n{{\n\n"); contentBuilder.Append($"\tpublic sealed class {className}"); if (matchingInterfaces.Count > 0) { contentBuilder.Append( $" : {string.Join(", ", matchingInterfaces.Select(e => e.GetDeclaration(table.Columns)))}"); } contentBuilder.Append("\n\t{\n\n"); var args = string.Join(", ", table.Columns.Select(c => $"{c.TypeAsString()} {c.Name}")); contentBuilder.Append($"\t\tpublic {className}({args})\n\t\t{{\n"); contentBuilder.Append(string.Join("\n", table.Columns.Select(c => $"\t\t\t{ToPascalCase(c.Name)} = {c.Name};"))); contentBuilder.Append("\n\t\t}\n\n"); foreach (var column in table.Columns) { if (primaryKeyAttributeClass != null && table.IsPrimaryKey(column.Name)) { contentBuilder.Append( $"\t\t[{primaryKeyAttributeClass.Item2}]\n"); } if (autoIncrementAttributeClass != null && column.IsAutoIncrement) { contentBuilder.Append( $"\t\t[{autoIncrementAttributeClass.Item2}]\n"); } contentBuilder.Append( $"\t\tpublic {column.TypeAsString()} {ToPascalCase(column.Name)} {{ get; }}\n\n"); } contentBuilder.Append("\t}\n\n}"); var content = contentBuilder.ToString(); return(content); }