From f22fb2039592f6e11d81db8429a7a9b5ba9c0157 Mon Sep 17 00:00:00 2001 From: Erik Ejlskov Jensen Date: Thu, 1 Aug 2024 12:27:48 +0200 Subject: [PATCH] Entity names are pascal cased and singularized --- src/Core/RevEng.Core.80/DabBuilder.cs | 45 ++++++++- .../Ef7Playground/dab-options.json | 7 +- .../ScaffoldingTester5/dab-config.cmd | 98 +++++++++---------- 3 files changed, 92 insertions(+), 58 deletions(-) diff --git a/src/Core/RevEng.Core.80/DabBuilder.cs b/src/Core/RevEng.Core.80/DabBuilder.cs index 5687204d7..02da0e54e 100644 --- a/src/Core/RevEng.Core.80/DabBuilder.cs +++ b/src/Core/RevEng.Core.80/DabBuilder.cs @@ -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) { @@ -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) { @@ -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)); 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"); } @@ -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\" "); } @@ -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; diff --git a/test/Ef7Playground/Ef7Playground/dab-options.json b/test/Ef7Playground/Ef7Playground/dab-options.json index 79f505759..08d322143 100644 --- a/test/Ef7Playground/Ef7Playground/dab-options.json +++ b/test/Ef7Playground/Ef7Playground/dab-options.json @@ -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": [ { diff --git a/test/ScaffoldingTester/ScaffoldingTester5/dab-config.cmd b/test/ScaffoldingTester/ScaffoldingTester5/dab-config.cmd index ed9f92c0b..6716d6248 100644 --- a/test/ScaffoldingTester/ScaffoldingTester5/dab-config.cmd +++ b/test/ScaffoldingTester/ScaffoldingTester5/dab-config.cmd @@ -7,79 +7,77 @@ dotnet tool install -g Microsoft.DataApiBuilder dab init -c dab-config.json --database-type mssql --connection-string "@env('dab-connection-string')" --host-mode Development @echo Adding tables -dab add "Categories" --source "[dbo].[Categories]" --fields.include "CategoryID,CategoryName,Description,Picture" --permissions "anonymous:*" +dab add "Category" --source "[dbo].[Categories]" --fields.include "CategoryID,CategoryName,Description,Picture" --permissions "anonymous:*" dab add "CustomerCustomerDemo" --source "[dbo].[CustomerCustomerDemo]" --fields.include "CustomerID,CustomerTypeID" --permissions "anonymous:*" -dab add "CustomerDemographics" --source "[dbo].[CustomerDemographics]" --fields.include "CustomerTypeID,CustomerDesc" --permissions "anonymous:*" -dab add "Customers" --source "[dbo].[Customers]" --fields.include "CustomerID,CompanyName,ContactName,ContactTitle,Address,City,Region,PostalCode,Country,Rating" --permissions "anonymous:*" -dab add "Employees" --source "[dbo].[Employees]" --fields.include "EmployeeID,LastName,FirstName,Title,TitleOfCourtesy,BirthDate,HireDate,Address,City,Region,PostalCode,Country,HomePhone,Extension,Photo,Notes,ReportsTo,PhotoPath" --permissions "anonymous:*" -dab add "EmployeeTerritories" --source "[dbo].[EmployeeTerritories]" --fields.include "EmployeeID,TerritoryID" --permissions "anonymous:*" -dab add "OrderDetails" --source "[dbo].[Order Details]" --fields.include "OrderID,ProductID,UnitPrice,Quantity,Discount" --permissions "anonymous:*" -dab add "Orders" --source "[dbo].[Orders]" --fields.include "OrderID,CustomerID,EmployeeID,OrderDate,RequiredDate,ShippedDate,ShipVia,Freight,ShipName,ShipAddress,ShipCity,ShipRegion,ShipPostalCode,ShipCountry" --permissions "anonymous:*" -dab add "Products" --source "[dbo].[Products]" --fields.include "ProductID,ProductName,SupplierID,CategoryID,QuantityPerUnit,UnitPrice,UnitsInStock,UnitsOnOrder,ReorderLevel,Discontinued" --permissions "anonymous:*" +dab add "CustomerDemographic" --source "[dbo].[CustomerDemographics]" --fields.include "CustomerTypeID,CustomerDesc" --permissions "anonymous:*" +dab add "Customer" --source "[dbo].[Customers]" --fields.include "CustomerID,CompanyName,ContactName,ContactTitle,Address,City,Region,PostalCode,Country" --permissions "anonymous:*" +dab add "Employee" --source "[dbo].[Employees]" --fields.include "EmployeeID,LastName,FirstName,Title,TitleOfCourtesy,BirthDate,HireDate,Address,City,Region,PostalCode,Country,HomePhone,Extension,Photo,Notes,ReportsTo,PhotoPath" --permissions "anonymous:*" +dab add "EmployeeTerritory" --source "[dbo].[EmployeeTerritories]" --fields.include "EmployeeID,TerritoryID" --permissions "anonymous:*" +dab add "OrderDetail" --source "[dbo].[Order Details]" --fields.include "OrderID,ProductID,UnitPrice,Quantity,Discount" --permissions "anonymous:*" +dab add "Order" --source "[dbo].[Orders]" --fields.include "OrderID,CustomerID,EmployeeID,OrderDate,RequiredDate,ShippedDate,ShipVia,Freight,ShipName,ShipAddress,ShipCity,ShipRegion,ShipPostalCode,ShipCountry" --permissions "anonymous:*" +dab add "Product" --source "[dbo].[Products]" --fields.include "ProductID,ProductName,SupplierID,CategoryID,QuantityPerUnit,UnitPrice,UnitsInStock,UnitsOnOrder,ReorderLevel,Discontinued" --permissions "anonymous:*" dab add "Region" --source "[dbo].[Region]" --fields.include "RegionID,RegionDescription" --permissions "anonymous:*" -dab add "Shippers" --source "[dbo].[Shippers]" --fields.include "ShipperID,CompanyName,Phone" --permissions "anonymous:*" -dab add "Suppliers" --source "[dbo].[Suppliers]" --fields.include "SupplierID,CompanyName,ContactName,ContactTitle,Address,City,Region,PostalCode,Country,Phone,Fax,HomePage" --permissions "anonymous:*" -dab add "Territories" --source "[dbo].[Territories]" --fields.include "TerritoryID,TerritoryDescription,RegionID" --permissions "anonymous:*" +dab add "Shipper" --source "[dbo].[Shippers]" --fields.include "ShipperID,CompanyName,Phone" --permissions "anonymous:*" +dab add "Supplier" --source "[dbo].[Suppliers]" --fields.include "SupplierID,CompanyName,ContactName,ContactTitle,Address,City,Region,PostalCode,Country,Phone,Fax,HomePage" --permissions "anonymous:*" +dab add "Territory" --source "[dbo].[Territories]" --fields.include "TerritoryID,TerritoryDescription,RegionID" --permissions "anonymous:*" @echo Adding views and tables without primary key @echo No primary key found for table/view 'Alphabetical list of products', using first Id column (ProductID) as key field -dab add "Alphabeticallistofproducts" --source "[dbo].[Alphabetical list of products]" --fields.include "ProductID,ProductName,SupplierID,CategoryID,QuantityPerUnit,UnitPrice,UnitsInStock,UnitsOnOrder,ReorderLevel,Discontinued,CategoryName" --source.type "view" --source.key-fields "ProductID" --permissions "anonymous:*" -@echo No primary key found for table/view 'Category Sales for 1997', using first column (CategoryName) as key field -dab add "CategorySalesfor1997" --source "[dbo].[Category Sales for 1997]" --fields.include "CategoryName,CategorySales" --source.type "view" --source.key-fields "CategoryName" --permissions "anonymous:*" +dab add "Alphabeticallistofproduct" --source "[dbo].[Alphabetical list of products]" --fields.include "ProductID,ProductName,SupplierID,CategoryID,QuantityPerUnit,UnitPrice,UnitsInStock,UnitsOnOrder,ReorderLevel,Discontinued,CategoryName" --source.type "view" --source.key-fields "ProductID" --permissions "anonymous:*" @echo No primary key found for table/view 'Current Product List', using first Id column (ProductID) as key field dab add "CurrentProductList" --source "[dbo].[Current Product List]" --fields.include "ProductID,ProductName" --source.type "view" --source.key-fields "ProductID" --permissions "anonymous:*" @echo No primary key found for table/view 'Customer and Suppliers by City', using first column (City) as key field -dab add "CustomerandSuppliersbyCity" --source "[dbo].[Customer and Suppliers by City]" --fields.include "City,CompanyName,ContactName,Relationship" --source.type "view" --source.key-fields "City" --permissions "anonymous:*" +dab add "CustomerandSuppliersbyCity" --source "[dbo].[Customer and Suppliers by City]" --fields.include "City,CompanyName,ContactName" --source.type "view" --source.key-fields "City" --permissions "anonymous:*" @echo No primary key found for table/view 'Invoices', using first Id column (CustomerID) as key field -dab add "Invoices" --source "[dbo].[Invoices]" --fields.include "ShipName,ShipAddress,ShipCity,ShipRegion,ShipPostalCode,ShipCountry,CustomerID,CustomerName,Address,City,Region,PostalCode,Country,Salesperson,OrderID,OrderDate,RequiredDate,ShippedDate,ShipperName,ProductID,ProductName,UnitPrice,Quantity,Discount,ExtendedPrice,Freight" --source.type "view" --source.key-fields "CustomerID" --permissions "anonymous:*" +dab add "Invoice" --source "[dbo].[Invoices]" --fields.include "ShipName,ShipAddress,ShipCity,ShipRegion,ShipPostalCode,ShipCountry,CustomerID,CustomerName,Address,City,Region,PostalCode,Country,OrderID,OrderDate,RequiredDate,ShippedDate,ShipperName,ProductID,ProductName,UnitPrice,Quantity,Discount,Freight" --source.type "view" --source.key-fields "CustomerID" --permissions "anonymous:*" @echo No primary key found for table/view 'Order Details Extended', using first Id column (OrderID) as key field -dab add "OrderDetailsExtended" --source "[dbo].[Order Details Extended]" --fields.include "OrderID,ProductID,ProductName,UnitPrice,Quantity,Discount,ExtendedPrice" --source.type "view" --source.key-fields "OrderID" --permissions "anonymous:*" +dab add "OrderDetailsExtended" --source "[dbo].[Order Details Extended]" --fields.include "OrderID,ProductID,ProductName,UnitPrice,Quantity,Discount" --source.type "view" --source.key-fields "OrderID" --permissions "anonymous:*" @echo No primary key found for table/view 'Order Subtotals', using first Id column (OrderID) as key field -dab add "OrderSubtotals" --source "[dbo].[Order Subtotals]" --fields.include "OrderID,Subtotal" --source.type "view" --source.key-fields "OrderID" --permissions "anonymous:*" +dab add "OrderSubtotal" --source "[dbo].[Order Subtotals]" --fields.include "OrderID" --source.type "view" --source.key-fields "OrderID" --permissions "anonymous:*" @echo No primary key found for table/view 'Orders Qry', using first Id column (OrderID) as key field dab add "OrdersQry" --source "[dbo].[Orders Qry]" --fields.include "OrderID,CustomerID,EmployeeID,OrderDate,RequiredDate,ShippedDate,ShipVia,Freight,ShipName,ShipAddress,ShipCity,ShipRegion,ShipPostalCode,ShipCountry,CompanyName,Address,City,Region,PostalCode,Country" --source.type "view" --source.key-fields "OrderID" --permissions "anonymous:*" @echo No primary key found for table/view 'Product Sales for 1997', using first column (CategoryName) as key field -dab add "ProductSalesfor1997" --source "[dbo].[Product Sales for 1997]" --fields.include "CategoryName,ProductName,ProductSales" --source.type "view" --source.key-fields "CategoryName" --permissions "anonymous:*" +dab add "ProductSalesfor1997" --source "[dbo].[Product Sales for 1997]" --fields.include "CategoryName,ProductName" --source.type "view" --source.key-fields "CategoryName" --permissions "anonymous:*" @echo No primary key found for table/view 'Products Above Average Price', using first column (ProductName) as key field dab add "ProductsAboveAveragePrice" --source "[dbo].[Products Above Average Price]" --fields.include "ProductName,UnitPrice" --source.type "view" --source.key-fields "ProductName" --permissions "anonymous:*" @echo No primary key found for table/view 'Products by Category', using first column (CategoryName) as key field dab add "ProductsbyCategory" --source "[dbo].[Products by Category]" --fields.include "CategoryName,ProductName,QuantityPerUnit,UnitsInStock,Discontinued" --source.type "view" --source.key-fields "CategoryName" --permissions "anonymous:*" @echo No primary key found for table/view 'Quarterly Orders', using first Id column (CustomerID) as key field -dab add "QuarterlyOrders" --source "[dbo].[Quarterly Orders]" --fields.include "CustomerID,CompanyName,City,Country" --source.type "view" --source.key-fields "CustomerID" --permissions "anonymous:*" +dab add "QuarterlyOrder" --source "[dbo].[Quarterly Orders]" --fields.include "CustomerID,CompanyName,City,Country" --source.type "view" --source.key-fields "CustomerID" --permissions "anonymous:*" @echo No primary key found for table/view 'Sales by Category', using first Id column (CategoryID) as key field -dab add "SalesbyCategory" --source "[dbo].[Sales by Category]" --fields.include "CategoryID,CategoryName,ProductName,ProductSales" --source.type "view" --source.key-fields "CategoryID" --permissions "anonymous:*" +dab add "SalesbyCategory" --source "[dbo].[Sales by Category]" --fields.include "CategoryID,CategoryName,ProductName" --source.type "view" --source.key-fields "CategoryID" --permissions "anonymous:*" @echo No primary key found for table/view 'Sales Totals by Amount', using first Id column (OrderID) as key field -dab add "SalesTotalsbyAmount" --source "[dbo].[Sales Totals by Amount]" --fields.include "SaleAmount,OrderID,CompanyName,ShippedDate" --source.type "view" --source.key-fields "OrderID" --permissions "anonymous:*" +dab add "SalesTotalsbyAmount" --source "[dbo].[Sales Totals by Amount]" --fields.include "OrderID,CompanyName,ShippedDate" --source.type "view" --source.key-fields "OrderID" --permissions "anonymous:*" @echo No primary key found for table/view 'Summary of Sales by Quarter', using first Id column (OrderID) as key field -dab add "SummaryofSalesbyQuarter" --source "[dbo].[Summary of Sales by Quarter]" --fields.include "ShippedDate,OrderID,Subtotal" --source.type "view" --source.key-fields "OrderID" --permissions "anonymous:*" +dab add "SummaryofSalesbyQuarter" --source "[dbo].[Summary of Sales by Quarter]" --fields.include "ShippedDate,OrderID" --source.type "view" --source.key-fields "OrderID" --permissions "anonymous:*" @echo No primary key found for table/view 'Summary of Sales by Year', using first Id column (OrderID) as key field -dab add "SummaryofSalesbyYear" --source "[dbo].[Summary of Sales by Year]" --fields.include "ShippedDate,OrderID,Subtotal" --source.type "view" --source.key-fields "OrderID" --permissions "anonymous:*" +dab add "SummaryofSalesbyYear" --source "[dbo].[Summary of Sales by Year]" --fields.include "ShippedDate,OrderID" --source.type "view" --source.key-fields "OrderID" --permissions "anonymous:*" @echo Adding relationships -dab update CustomerCustomerDemo --relationship CustomerDemographics --target.entity CustomerDemographics --cardinality one -dab update CustomerDemographics --relationship CustomerCustomerDemo --target.entity CustomerCustomerDemo --cardinality many -dab update CustomerCustomerDemo --relationship Customers --target.entity Customers --cardinality one -dab update Customers --relationship CustomerCustomerDemo --target.entity CustomerCustomerDemo --cardinality many -dab update Employees --relationship Employees --target.entity Employees --cardinality one -dab update Employees --relationship Employees --target.entity Employees --cardinality many -dab update EmployeeTerritories --relationship Employees --target.entity Employees --cardinality one -dab update Employees --relationship EmployeeTerritories --target.entity EmployeeTerritories --cardinality many -dab update EmployeeTerritories --relationship Territories --target.entity Territories --cardinality one -dab update Territories --relationship EmployeeTerritories --target.entity EmployeeTerritories --cardinality many -dab update OrderDetails --relationship Orders --target.entity Orders --cardinality one -dab update Orders --relationship OrderDetails --target.entity OrderDetails --cardinality many -dab update OrderDetails --relationship Products --target.entity Products --cardinality one -dab update Products --relationship OrderDetails --target.entity OrderDetails --cardinality many -dab update Orders --relationship Customers --target.entity Customers --cardinality one -dab update Customers --relationship Orders --target.entity Orders --cardinality many -dab update Orders --relationship Employees --target.entity Employees --cardinality one -dab update Employees --relationship Orders --target.entity Orders --cardinality many -dab update Orders --relationship Shippers --target.entity Shippers --cardinality one -dab update Shippers --relationship Orders --target.entity Orders --cardinality many -dab update Products --relationship Categories --target.entity Categories --cardinality one -dab update Categories --relationship Products --target.entity Products --cardinality many -dab update Products --relationship Suppliers --target.entity Suppliers --cardinality one -dab update Suppliers --relationship Products --target.entity Products --cardinality many -dab update Territories --relationship Region --target.entity Region --cardinality one -dab update Region --relationship Territories --target.entity Territories --cardinality many +dab update CustomerCustomerDemo --relationship CustomerDemographic --target.entity CustomerDemographic --cardinality one +dab update CustomerDemographic --relationship CustomerCustomerDemo --target.entity CustomerCustomerDemo --cardinality many +dab update CustomerCustomerDemo --relationship Customer --target.entity Customer --cardinality one +dab update Customer --relationship CustomerCustomerDemo --target.entity CustomerCustomerDemo --cardinality many +dab update Employee --relationship Employee --target.entity Employee --cardinality one +dab update Employee --relationship Employee --target.entity Employee --cardinality many +dab update EmployeeTerritory --relationship Employee --target.entity Employee --cardinality one +dab update Employee --relationship EmployeeTerritory --target.entity EmployeeTerritory --cardinality many +dab update EmployeeTerritory --relationship Territory --target.entity Territory --cardinality one +dab update Territory --relationship EmployeeTerritory --target.entity EmployeeTerritory --cardinality many +dab update OrderDetail --relationship Order --target.entity Order --cardinality one +dab update Order --relationship OrderDetail --target.entity OrderDetail --cardinality many +dab update OrderDetail --relationship Product --target.entity Product --cardinality one +dab update Product --relationship OrderDetail --target.entity OrderDetail --cardinality many +dab update Order --relationship Customer --target.entity Customer --cardinality one +dab update Customer --relationship Order --target.entity Order --cardinality many +dab update Order --relationship Employee --target.entity Employee --cardinality one +dab update Employee --relationship Order --target.entity Order --cardinality many +dab update Order --relationship Shipper --target.entity Shipper --cardinality one +dab update Shipper --relationship Order --target.entity Order --cardinality many +dab update Product --relationship Category --target.entity Category --cardinality one +dab update Category --relationship Product --target.entity Product --cardinality many +dab update Product --relationship Supplier --target.entity Supplier --cardinality one +dab update Supplier --relationship Product --target.entity Product --cardinality many +dab update Territory --relationship Region --target.entity Region --cardinality one +dab update Region --relationship Territory --target.entity Territory --cardinality many @echo Adding stored procedures dab add "CustOrderHist" --source "[dbo].[CustOrderHist]" --source.type "stored-procedure" --permissions "anonymous:execute" --rest.methods "get" --graphql.operation "query" @echo **