Skip to content

Commit

Permalink
Entity names are pascal cased and singularized
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikEJ committed Aug 1, 2024
1 parent 3f881dd commit f22fb20
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 58 deletions.
45 changes: 40 additions & 5 deletions src/Core/RevEng.Core.80/DabBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public string GetDabConfigCmdFile()
dbObject.Columns
.Select(c => c.Name).ToList());

var type = dbObject.Name.Replace(" ", string.Empty, StringComparison.OrdinalIgnoreCase);
var type = GenerateEntityName(dbObject.Name.Replace(" ", string.Empty, StringComparison.OrdinalIgnoreCase));

if (dbObject.PrimaryKey != null)
{
Expand All @@ -134,7 +134,7 @@ public string GetDabConfigCmdFile()
dbObject.Columns
.Select(c => c.Name).ToList());

var type = dbObject.Name.Replace(" ", string.Empty, StringComparison.OrdinalIgnoreCase);
var type = GenerateEntityName(dbObject.Name.Replace(" ", string.Empty, StringComparison.OrdinalIgnoreCase));

if (dbObject.PrimaryKey == null)
{
Expand Down Expand Up @@ -165,11 +165,11 @@ public string GetDabConfigCmdFile()
continue;
}

var type = dbObject.Name.Replace(" ", string.Empty, StringComparison.OrdinalIgnoreCase);
var type = GenerateEntityName(dbObject.Name.Replace(" ", string.Empty, StringComparison.OrdinalIgnoreCase));

foreach (var fk in dbObject.ForeignKeys)
{
var fkType = fk.PrincipalTable.Name.Replace(" ", string.Empty, StringComparison.OrdinalIgnoreCase);
var fkType = GenerateEntityName(fk.PrincipalTable.Name.Replace(" ", string.Empty, StringComparison.OrdinalIgnoreCase));

Check failure on line 172 in src/Core/RevEng.Core.80/DabBuilder.cs

View workflow job for this annotation

GitHub Actions / build

Code should not contain multiple whitespace characters in a row (https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1025.md)
sb.AppendLine(CultureInfo.InvariantCulture, $"dab update {type} --relationship {fkType} --target.entity {fkType} --cardinality one");
sb.AppendLine(CultureInfo.InvariantCulture, $"dab update {fkType} --relationship {type} --target.entity {type} --cardinality many");
}
Expand All @@ -183,7 +183,7 @@ public string GetDabConfigCmdFile()

foreach (var procedure in procedures.Routines)
{
var type = procedure.Name.Replace(" ", string.Empty, StringComparison.OrdinalIgnoreCase);
var type = GenerateEntityName(procedure.Name.Replace(" ", string.Empty, StringComparison.OrdinalIgnoreCase));

sb.AppendLine(CultureInfo.InvariantCulture, $"dab add \"{type}\" --source \"[{procedure.Schema}].[{procedure.Name}]\" --source.type \"stored-procedure\" --permissions \"anonymous:execute\" --rest.methods \"get\" --graphql.operation \"query\" ");
}
Expand All @@ -205,6 +205,41 @@ private static bool BreaksOn(DatabaseTable dbObject)
|| !dbObject.Columns.Any();
}

private static string GenerateEntityName(string value)
{
var candidateStringBuilder = new StringBuilder();
var previousLetterCharInWordIsLowerCase = false;
var isFirstCharacterInWord = true;

foreach (var c in value)
{
var isNotLetterOrDigit = !char.IsLetterOrDigit(c);
if (isNotLetterOrDigit
|| (previousLetterCharInWordIsLowerCase && char.IsUpper(c)))
{
isFirstCharacterInWord = true;
previousLetterCharInWordIsLowerCase = false;
if (isNotLetterOrDigit)
{
continue;
}
}

candidateStringBuilder.Append(
isFirstCharacterInWord ? char.ToUpperInvariant(c) : char.ToLowerInvariant(c));
isFirstCharacterInWord = false;
if (char.IsLower(c))
{
previousLetterCharInWordIsLowerCase = true;
}
}

var pluralizer = new HumanizerPluralizer();
var candidate = candidateStringBuilder.ToString();

return pluralizer.Singularize(candidate);
}

private void RemoveExcludedColumns(DatabaseTable dbObject)
{
var excludedColumns = options.Tables.Find(t => t.Name == $"[{dbObject.Schema}].[{dbObject.Name}]")?.ExcludedColumns;
Expand Down
7 changes: 4 additions & 3 deletions test/Ef7Playground/Ef7Playground/dab-options.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"ConnectionString": "Data Source=(local);Initial Catalog=Chinook;Integrated Security=true;",
"Dacpac": "C:\\Code\\Github\\EFCorePowerTools\\src\\Core\\NUnitTestCore\\Dacpac\\Chinook.dacpac",
"DatabaseType": 8,
"ConnectionString": null,
"ConnectionStringName": "dab-connection-string",
"Dacpac": null,
"DatabaseType": 3,
"ProjectPath": "C:\\Code\\Github\\EFCorePowerTools\\test\\Ef7Playground\\Ef7Playground",
"Tables": [
{
Expand Down
Loading

0 comments on commit f22fb20

Please sign in to comment.