private static void FixDuplicateName(ICollection <string> tableNames, DatabaseTable table) { var netName = table.NetName; if (!tableNames.Contains(netName)) { return; } //first we try to add the schema as a prefix (eg DboCategory). var schemaOwner = NameFixer.ToPascalCase(table.SchemaOwner); var name = schemaOwner + netName; if (!tableNames.Contains(name)) { table.NetName = name; return; } //let's try suffixes- just count up to 100, and if we find a free one, use it for (var i = 0; i < 100; i++) { name = netName + "1"; if (!tableNames.Contains(name)) { table.NetName = name; return; } } }
public virtual string NameToAcronym(string parameterName) { var n = NameFixer.ToCamelCase(parameterName); //http://weblogs.asp.net/jgalloway/archive/2005/09/27/426087.aspx var friendlyName = Regex.Replace(n, "([A-Z]+|[0-9]+)", " $1", RegexOptions.Compiled).Trim(); var fields = friendlyName.Split(' '); var sb = new StringBuilder(); foreach (var f in fields) { if (CultureInfo.InvariantCulture.TextInfo.ToLower(f) == "id") { sb.Append("id"); continue; } if (Regex.IsMatch(f, "[0-9]+")) { sb.Append(f); continue; } sb.Append(f.ToLowerInvariant().Substring(0, 1)); } return(sb.ToString()); }
/// <summary> /// Translates the namedObject's Name to a code-friendly name /// </summary> /// <param name="namedObject">The named object.</param> /// <returns></returns> public virtual string Name(INamedObject namedObject) { var name = NameFixer.ToPascalCase(namedObject.Name); var column = namedObject as DatabaseColumn; if (column != null) { //if it's a foreign key (CategoryId) if (column.IsForeignKey && name.Length > 2) { if (name.EndsWith("Id", StringComparison.OrdinalIgnoreCase)) { //remove the "Id" - it's just a "Category" name = name.Substring(0, name.Length - 2); } } //member name cannot be same as class name if (name == column.Table.NetName) { name += "Property"; } } return(name); }
public void WriteWith(DatabaseTable foreignKeyChild) { var ffks = CodeWriterUtils.GetWithForeignKeys(table, foreignKeyChild).ToList(); foreach (var ffk in ffks) { var ffkTable = table.DatabaseSchema.FindTableByName(ffk.TableName); var ffkReferencedTable = ffk.ReferencedTable(table.DatabaseSchema); var ffkColumns = ffk.Columns.Select(item => ffkTable.FindColumn(item)); ffkColumns.OrderBy(item => item.Name); var ffkReferencedColumns = ffk.ReferencedColumns(table.DatabaseSchema).Select(item => ffkReferencedTable.FindColumn(item)); var withMethodSignature = CodeWriterUtils.GetWithMethodSignature( ffkReferencedTable, ffkTable, ffk, codeWriterSettings); string propertyName = null; string repositoryMethodNameForFfkTable = null; if (table.IsSharedPrimaryKey(foreignKeyChild)) { propertyName = foreignKeyChild.Name; repositoryMethodNameForFfkTable = CodeWriterUtils.GetGetMethodName(ffkColumns, codeWriterSettings, true); } else { propertyName = codeWriterSettings.Namer.ForeignKeyCollectionName(ffkReferencedTable.Name, ffkTable, ffk); repositoryMethodNameForFfkTable = CodeWriterUtils.GetGetMethodName(ffkColumns, codeWriterSettings, false); } classBuilder.BeginNest($"public virtual {withMethodSignature}"); var repositoryMethodCallParametersForFfkTable = new List <string>(); foreach (var ffkReferencedColumn in ffkReferencedColumns) { var parameter = $"{CodeWriterUtils.GetPropertyNameForDatabaseColumn(ffkReferencedColumn)}"; if (ffkReferencedColumn.Nullable && CodeWriterUtils.FindDataType(ffkReferencedColumn).EndsWith("?")) { using (classBuilder.BeginNest($"if (!{parameter}.HasValue)")) { classBuilder.AppendLine($"{propertyName} = new List<{ffkTable.NetName}>();"); classBuilder.AppendLine("return this;"); } classBuilder.AppendLine(""); parameter += ".Value"; } repositoryMethodCallParametersForFfkTable.Add(parameter); } var repositoryMethodCallParametersForFfkTablePrinted = string.Join(", ", repositoryMethodCallParametersForFfkTable); var fieldNameForFfkTableRepository = NameFixer.ToCamelCase(CodeWriterUtils.GetRepositoryImplementationName(foreignKeyChild)); classBuilder.AppendLine($"{propertyName} = _{fieldNameForFfkTableRepository}.{repositoryMethodNameForFfkTable}({repositoryMethodCallParametersForFfkTablePrinted});"); classBuilder.AppendLine("return this;"); classBuilder.EndNest(); classBuilder.AppendLine(""); } }
public string NameColumnAsMethodTitle(string name) { var namePascalCase = NameFixer.ToPascalCase(name); if (namePascalCase.EndsWith("Id", StringComparison.OrdinalIgnoreCase)) { //remove the "Id" - it's just a "Category" namePascalCase = namePascalCase.Substring(0, namePascalCase.Length - 2); } return(namePascalCase); }
private static void PrepareStoredProcedureNames(DatabaseStoredProcedure sproc) { if (string.IsNullOrEmpty(sproc.NetName)) { sproc.NetName = NameFixer.ToPascalCase(sproc.Name); } foreach (var argument in sproc.Arguments) { if (string.IsNullOrEmpty(argument.NetName)) { argument.NetName = NameFixer.ToPascalCase(argument.Name); } } }
public void WriteWith(DatabaseConstraint foreignKey) { // TODO: refactor this method to be consistent with approach taken for other overload var refTable = foreignKey.ReferencedTable(table.DatabaseSchema); var dataType = refTable.NetName; if (foreignKey.Columns.Count != foreignKey.ReferencedColumns(table.DatabaseSchema).Count()) { throw new InvalidOperationException("Number of foreign key columns does not match number of columns referenced!"); } classBuilder.BeginNest($"public virtual {CodeWriterUtils.GetWithMethodSignature(table, foreignKey, codeWriterSettings)}"); var methodCallParameters = new List <string>(); var propertyName = codeWriterSettings.Namer.ForeignKeyName(table, foreignKey); foreach (var fkc in foreignKey.Columns) { var tc = table.Columns.Single(_tc => _tc.Name == fkc); var parameter = $"{CodeWriterUtils.GetPropertyNameForDatabaseColumn(tc)}"; if (tc.Nullable && CodeWriterUtils.FindDataType(tc).EndsWith("?")) // KE: need the check for the "?" so that we correctly handle reference types like string { using (classBuilder.BeginNest($"if (!{parameter}.HasValue)")) { classBuilder.AppendLine($"{propertyName} = null;"); classBuilder.AppendLine("return this;"); } classBuilder.AppendLine(""); parameter += ".Value"; } methodCallParameters.Add(parameter); } var s = string.Join(", ", methodCallParameters); var referencedColumnNames = foreignKey.ReferencedColumns(table.DatabaseSchema).ToList(); referencedColumnNames.Sort(); var referencedColumns = referencedColumnNames.Select(c => foreignKey.ReferencedTable(table.DatabaseSchema).FindColumn(c)); var methodParameters = CodeWriterUtils.GetMethodParametersForColumns(referencedColumns, codeWriterSettings); var methodName = CodeWriterUtils.GetMethodName(methodParameters, codeWriterSettings, true, CodeWriterUtils.BaseMethodNameGet); var fieldNameForFkTableRepository = NameFixer.ToCamelCase(CodeWriterUtils.GetRepositoryImplementationName(refTable)); classBuilder.AppendLine($"{propertyName} = _{fieldNameForFkTableRepository}.{methodName}({s});"); classBuilder.AppendLine("return this;"); classBuilder.EndNest(); classBuilder.AppendLine(""); }
private static void Prepare(DatabaseColumn column) { if (!string.IsNullOrEmpty(column.NetName)) { return; } column.NetName = NameFixer.ToPascalCase(column.Name); //if it's a foreign key (CategoryId) if (column.IsForeignKey && column.NetName.EndsWith("Id", StringComparison.OrdinalIgnoreCase)) { //remove the "Id" - it's just a "Category" var netName = column.NetName; column.NetName = netName.Substring(0, netName.Length - 2); } }
public static IEnumerable <Parameter> GetTablesAsParameters(IEnumerable <DatabaseTable> tables) { var fields = new List <Parameter>(); foreach (var t in tables.Distinct().OrderBy(t => t.Name)) { var field = new Parameter { ColumnNameToQueryBy = null, DataType = CodeWriterUtils.GetRepositoryInterfaceName(t), Name = NameFixer.ToCamelCase(CodeWriterUtils.GetRepositoryImplementationName(t)) }; fields.Add(field); } return(fields); }
public static void Prepare(DatabaseSchema schema) { //no check for duplicate names //if NetNames are already set, they are not overridden foreach (var table in schema.Tables) { if (string.IsNullOrEmpty(table.NetName)) { table.NetName = NameFixer.ToPascalCase(table.Name); } foreach (var column in table.Columns) { Prepare(column); } } foreach (var view in schema.Views) { if (string.IsNullOrEmpty(view.NetName)) { view.NetName = NameFixer.ToPascalCase(view.Name); } foreach (var column in view.Columns) { Prepare(column); } } foreach (var sproc in schema.StoredProcedures) { PrepareStoredProcedureNames(sproc); } foreach (var package in schema.Packages) { if (string.IsNullOrEmpty(package.NetName)) { package.NetName = NameFixer.ToPascalCase(package.Name); } foreach (var sproc in package.StoredProcedures) { PrepareStoredProcedureNames(sproc); } } }