From 000b12509114003b3cd1ff1087a414ff6ddd3b5b Mon Sep 17 00:00:00 2001 From: Andrii Kurdiumov Date: Sat, 30 Nov 2024 19:06:23 +0500 Subject: [PATCH] Change default naming convention --- SqlMarshal.Tests/CrudGenerationTests.cs | 14 +++++++------- SqlMarshal.Tests/NameMapperTests.cs | 3 +++ SqlMarshal/Generator.cs | 24 ++++++++++++------------ 3 files changed, 22 insertions(+), 19 deletions(-) diff --git a/SqlMarshal.Tests/CrudGenerationTests.cs b/SqlMarshal.Tests/CrudGenerationTests.cs index a531f62..d81d2d1 100644 --- a/SqlMarshal.Tests/CrudGenerationTests.cs +++ b/SqlMarshal.Tests/CrudGenerationTests.cs @@ -60,7 +60,7 @@ partial class C var connection = this.connection; using var command = connection.CreateCommand(); - var sqlQuery = @""SELECT Id, Name FROM TestEntity""; + var sqlQuery = @""SELECT id, name FROM test_entity""; command.CommandText = sqlQuery; using var reader = command.ExecuteReader(); var result = new List(); @@ -139,7 +139,7 @@ partial class C idParameter, }; - var sqlQuery = @""SELECT Id, Name FROM TestEntity WHERE Id = @id""; + var sqlQuery = @""SELECT id, name FROM test_entity WHERE id = @id""; command.CommandText = sqlQuery; command.Parameters.AddRange(parameters); using var reader = command.ExecuteReader(System.Data.CommandBehavior.SingleResult | System.Data.CommandBehavior.SingleRow); @@ -209,7 +209,7 @@ public partial int Count() var connection = this.connection; using var command = connection.CreateCommand(); - var sqlQuery = @""SELECT COUNT(1) FROM TestEntity""; + var sqlQuery = @""SELECT COUNT(1) FROM test_entity""; command.CommandText = sqlQuery; var result = command.ExecuteScalar(); return (int)result!; @@ -267,7 +267,7 @@ public partial void DeleteAll() var connection = this.connection; using var command = connection.CreateCommand(); - var sqlQuery = @""DELETE FROM TestEntity""; + var sqlQuery = @""DELETE FROM test_entity""; command.CommandText = sqlQuery; command.ExecuteNonQuery(); } @@ -333,7 +333,7 @@ public partial void DeleteById(int id) idParameter, }; - var sqlQuery = @""DELETE FROM TestEntity WHERE Id = @id""; + var sqlQuery = @""DELETE FROM test_entity WHERE id = @id""; command.CommandText = sqlQuery; command.Parameters.AddRange(parameters); command.ExecuteNonQuery(); @@ -412,7 +412,7 @@ public partial void Update(int id, string name, string description) descriptionParameter, }; - var sqlQuery = @""UPDATE TestEntity SET Name = @name, Description = @description WHERE Id = @id""; + var sqlQuery = @""UPDATE test_entity SET name = @name, description = @description WHERE id = @id""; command.CommandText = sqlQuery; command.Parameters.AddRange(parameters); command.ExecuteNonQuery(); @@ -491,7 +491,7 @@ public partial void Insert(int id, string name, string description) descriptionParameter, }; - var sqlQuery = @""INSERT INTO TestEntity(Id, Name, Description) VALUES (@id, @name, @description)""; + var sqlQuery = @""INSERT INTO test_entity(id, name, description) VALUES (@id, @name, @description)""; command.CommandText = sqlQuery; command.Parameters.AddRange(parameters); command.ExecuteNonQuery(); diff --git a/SqlMarshal.Tests/NameMapperTests.cs b/SqlMarshal.Tests/NameMapperTests.cs index a2f6b83..c80bd87 100644 --- a/SqlMarshal.Tests/NameMapperTests.cs +++ b/SqlMarshal.Tests/NameMapperTests.cs @@ -13,6 +13,9 @@ public class NameMapperTests { [DataTestMethod] [DataRow("personId", "person_id")] + [DataRow("name", "name")] + [DataRow("Name", "name")] + [DataRow("PersonId", "person_id")] public void MyTestMethod(string parameterName, string expectedStoredProcedureParameter) { var storedProcedureParameter = NameMapper.MapName(parameterName); diff --git a/SqlMarshal/Generator.cs b/SqlMarshal/Generator.cs index 03336e0..f912f31 100644 --- a/SqlMarshal/Generator.cs +++ b/SqlMarshal/Generator.cs @@ -785,7 +785,7 @@ private void MapResults( for (var i = 0; i < properties.Count; i++) { builder.Append(" "); - builder.Append(properties[i].Name); + builder.Append(NameMapper.MapName(properties[i].Name)); if (i != properties.Count - 1) { builder.Append(","); @@ -793,7 +793,7 @@ private void MapResults( } builder.Append(" FROM "); - builder.Append(entityType.Name); + builder.Append(NameMapper.MapName(entityType.Name)); return builder.ToString(); } @@ -805,7 +805,7 @@ private void MapResults( for (var i = 0; i < properties.Count; i++) { builder.Append(" "); - builder.Append(properties[i].Name); + builder.Append(NameMapper.MapName(properties[i].Name)); if (i != properties.Count - 1) { builder.Append(","); @@ -813,7 +813,7 @@ private void MapResults( } builder.Append(" FROM "); - builder.Append(entityType.Name); + builder.Append(NameMapper.MapName(entityType.Name)); AppendFilterById(builder); return builder.ToString(); } @@ -822,7 +822,7 @@ private void MapResults( { var builder = new StringBuilder(); builder.Append("SELECT COUNT(1) FROM "); - builder.Append(entityType.Name); + builder.Append(NameMapper.MapName(entityType.Name)); return builder.ToString(); } @@ -830,7 +830,7 @@ private void MapResults( { var builder = new StringBuilder(); builder.Append("DELETE FROM "); - builder.Append(entityType.Name); + builder.Append(NameMapper.MapName(entityType.Name)); return builder.ToString(); } @@ -838,7 +838,7 @@ private void MapResults( { var builder = new StringBuilder(); builder.Append("DELETE FROM "); - builder.Append(entityType.Name); + builder.Append(NameMapper.MapName(entityType.Name)); AppendFilterById(builder); return builder.ToString(); } @@ -847,7 +847,7 @@ private void MapResults( { var builder = new StringBuilder(); builder.Append("UPDATE "); - builder.Append(entityType.Name); + builder.Append(NameMapper.MapName(entityType.Name)); builder.Append(" SET "); bool first = true; foreach (var parameter in methodGenerationContext.SqlParameters) @@ -869,7 +869,7 @@ private void MapResults( continue; } - builder.Append(entityProperty.Name); + builder.Append(NameMapper.MapName(entityProperty.Name)); builder.Append(" = "); builder.Append("@" + NameMapper.MapName(parameter.Name)); first = false; @@ -883,7 +883,7 @@ private void MapResults( { var builder = new StringBuilder(); builder.Append("INSERT INTO "); - builder.Append(entityType.Name); + builder.Append(NameMapper.MapName(entityType.Name)); builder.Append("("); bool first = true; foreach (var parameter in methodGenerationContext.SqlParameters) @@ -900,7 +900,7 @@ private void MapResults( continue; } - builder.Append(entityProperty.Name); + builder.Append(NameMapper.MapName(entityProperty.Name)); first = false; } @@ -942,7 +942,7 @@ void AppendFilterById(StringBuilder builder) return; } - builder.Append(idMember.Name); + builder.Append(NameMapper.MapName(idMember.Name)); builder.Append(" = "); builder.Append("@" + NameMapper.MapName(idMember.Name)); }