From a38204aac932802dc0e3b3e53b7bf483305dcdbb Mon Sep 17 00:00:00 2001 From: "DESKTOP-IOHGBT9\\AnyHDD" Date: Sat, 27 Dec 2025 15:21:14 +0300 Subject: [PATCH 1/2] Simplify proprty selection for update. Update tests --- .../SqlServer/QMapInsertBenchmark.cs | 30 +++++++++++++++++++ QMap.SqlBuilder.Tests/SqlBuilderParseTests.cs | 4 +-- .../Abstractions/IUpdateBuilder.cs | 2 +- QMap.SqlBuilder/QueryBuilderExtensions.cs | 4 +-- QMap.SqlBuilder/StatementsBuilders.cs | 2 +- QMap.Tests/QMapConnectionExtensionTests.cs | 8 ++--- QMap/QMapConnectionExtension.cs | 4 +-- 7 files changed, 42 insertions(+), 12 deletions(-) create mode 100644 QMap.Benchmarks/Benchmarks/SqlServer/QMapInsertBenchmark.cs diff --git a/QMap.Benchmarks/Benchmarks/SqlServer/QMapInsertBenchmark.cs b/QMap.Benchmarks/Benchmarks/SqlServer/QMapInsertBenchmark.cs new file mode 100644 index 0000000..35fe48c --- /dev/null +++ b/QMap.Benchmarks/Benchmarks/SqlServer/QMapInsertBenchmark.cs @@ -0,0 +1,30 @@ +using AutoFixture; +using BenchmarkDotNet.Attributes; +using Dapper.Contrib.Extensions; +using QMap.SqlServer; +using QMap.Tests.Share.DataBase; +using System.Data.SqlClient; + +namespace QMap.Benchmarks.Benchmarks.SqlServer +{ + public class QMapInsertBenchmark + { + public static string ConnectionString = "Server=localhost;Database=TestDb;Integrated Security=true;TrustServerCertificate=Yes;Encrypt=false"; + + [Benchmark] + public void QMapInsertMethodBenchmark() + { + using var connection = new SqlConnection(ConnectionString).Adapt(); + + connection.Open(); + + var entity = new Fixture().Build() + .Without(t => t.Id) + .Create(); + + connection.Insert(entity, e => e.Id); + + connection.Close(); + } + } +} diff --git a/QMap.SqlBuilder.Tests/SqlBuilderParseTests.cs b/QMap.SqlBuilder.Tests/SqlBuilderParseTests.cs index 0da845e..cc0170e 100644 --- a/QMap.SqlBuilder.Tests/SqlBuilderParseTests.cs +++ b/QMap.SqlBuilder.Tests/SqlBuilderParseTests.cs @@ -110,7 +110,7 @@ public void BuildUpdateNoThrowsErros() entity.IntField = 2048; var sql = queryBuilder - .Update(connectionFake, out var _, () => entity.IntField, entity.IntField) + .Update(connectionFake, out var _, (e) => entity.IntField, entity.IntField) .Where((TypesTestEntity e) => e.Id > 0, out var parameters) .Build(); @@ -138,7 +138,7 @@ public void BuildUpdateThrowsInvalidOperationExceptionWhenGetNoProperty() Assert.Throws(() => { var sql = queryBuilder - .Update(connectionFake, out var _, () => entity.ToString(), "") + .Update(connectionFake, out var _, (e) => entity.ToString(), "") .Where((TypesTestEntity e) => e.Id > 0, out var parameters) .Build(); }); diff --git a/QMap.SqlBuilder/Abstractions/IUpdateBuilder.cs b/QMap.SqlBuilder/Abstractions/IUpdateBuilder.cs index abed71f..a9c79da 100644 --- a/QMap.SqlBuilder/Abstractions/IUpdateBuilder.cs +++ b/QMap.SqlBuilder/Abstractions/IUpdateBuilder.cs @@ -6,6 +6,6 @@ namespace QMap.SqlBuilder.Abstractions { public interface IUpdateBuilder : IQueryBuilder { - IUpdateBuilder BuildUpdate(Expression> propertySelector, V value); + IUpdateBuilder BuildUpdate(Expression> propertySelector, TProperty value); } } diff --git a/QMap.SqlBuilder/QueryBuilderExtensions.cs b/QMap.SqlBuilder/QueryBuilderExtensions.cs index 76bd87e..81e8d79 100644 --- a/QMap.SqlBuilder/QueryBuilderExtensions.cs +++ b/QMap.SqlBuilder/QueryBuilderExtensions.cs @@ -45,10 +45,10 @@ public static ISelectBuilder Select(this IQueryBuilder queryBuilder, Type entity .BuidSelect(entity); } - public static IUpdateBuilder Update(this IQueryBuilder queryBuilder, IQMapConnection connection, out Dictionary parameters, Expression> propertySelectors, V value) + public static IUpdateBuilder Update(this IQueryBuilder queryBuilder, IQMapConnection connection, out Dictionary parameters, Expression> propertySelectors, TProperty value) { var builder = new UpdateBuilder(queryBuilder.SqlDialect) - .BuildUpdate(propertySelectors, value); + .BuildUpdate(propertySelectors, value); parameters = builder.Parameters; diff --git a/QMap.SqlBuilder/StatementsBuilders.cs b/QMap.SqlBuilder/StatementsBuilders.cs index 2fabd1c..beae7cd 100644 --- a/QMap.SqlBuilder/StatementsBuilders.cs +++ b/QMap.SqlBuilder/StatementsBuilders.cs @@ -296,7 +296,7 @@ public UpdateBuilder(ISqlDialect sqlDialect) : base(sqlDialect) { } - public IUpdateBuilder BuildUpdate(Expression> propertySelector, V value) + public IUpdateBuilder BuildUpdate(Expression> propertySelector, TProperty value) { var memberExpression = propertySelector.Body as MemberExpression; diff --git a/QMap.Tests/QMapConnectionExtensionTests.cs b/QMap.Tests/QMapConnectionExtensionTests.cs index bf0f158..e5dbcf8 100644 --- a/QMap.Tests/QMapConnectionExtensionTests.cs +++ b/QMap.Tests/QMapConnectionExtensionTests.cs @@ -301,7 +301,7 @@ public void UpdateNoThrowsErrors() var newValue = new Random().Next(); entity.IntField = newValue; //TSQL errors when parse True constant - connection.Update(() => entity.IntField, newValue, (TypesTestEntity e) => e.Id == entity.Id); + connection.Update((e) => entity.IntField, newValue, (TypesTestEntity e) => e.Id == entity.Id); Assert.True(context.TypesTestEntity.Find(new object[] {entity.Id}).IntField == newValue); @@ -342,7 +342,7 @@ public void UpdateUpdatesOnlyMathedEntities() connection.Open(); //TSQL errors when parse True constant - connection.Update(() => updateEntity.IntField, newValue,(TypesTestEntity e) => e.Id == updateEntity.Id); + connection.Update((e) => updateEntity.IntField, newValue,(TypesTestEntity e) => e.Id == updateEntity.Id); Assert.True(context.TypesTestEntity.Find(new object[] { updateEntity.Id }).IntField == updateEntity.IntField); Assert.True(context.TypesTestEntity.Where((e) => e.Id != updateEntity.Id).All(e => e.IntField != newValue)); @@ -461,7 +461,7 @@ public void Update_Should_Not_Pass_Drop_Statemant() context.TypesTestEntity.Add(entity); context.SaveChanges(); - connection.Update(() => entity.StringField, "\'DROP TABLE TypesTestEntity;--", (TypesTestEntity t) => t.IntField > 0); + connection.Update((e) => entity.StringField, "\'DROP TABLE TypesTestEntity;--", (TypesTestEntity t) => t.IntField > 0); context.TypesTestEntity.Count(); }); @@ -490,7 +490,7 @@ public void Update_Should_Not_Pass_Drop_Statemant_When_Injection_In_Where() context.TypesTestEntity.Add(entity); context.SaveChanges(); - connection.Update(() => entity.StringField, "\'DROP TABLE TypesTestEntity;--", (TypesTestEntity t) => t.StringField != "\'DROP TABLE TypesTestEntity;--"); + connection.Update((e) => entity.StringField, "\'DROP TABLE TypesTestEntity;--", (TypesTestEntity t) => t.StringField != "\'DROP TABLE TypesTestEntity;--"); context.TypesTestEntity.Count(); }); diff --git a/QMap/QMapConnectionExtension.cs b/QMap/QMapConnectionExtension.cs index 17efc18..edd6102 100644 --- a/QMap/QMapConnectionExtension.cs +++ b/QMap/QMapConnectionExtension.cs @@ -106,12 +106,12 @@ public static void Insert(this IQMapConnection connection, T entity) command.ExecuteNonQuery(); } - public static void Update(this IQMapConnection connection, Expression> propertySelector, V value, LambdaExpression predicate) where T : class, new() + public static void Update(this IQMapConnection connection, Expression> propertySelector, TProperty value, LambdaExpression predicate) where T : class, new() { var command = connection.CreateCommand(); var sql = new StatementsBuilders(connection.Dialect) - .Update(connection, out var parameters, propertySelector, value) + .Update(connection, out var parameters, propertySelector, value) .Where(predicate, out var parameters1) .Build(); From b169a986383c7b380ba8a3607f7d67bcd10a6a12 Mon Sep 17 00:00:00 2001 From: "DESKTOP-IOHGBT9\\AnyHDD" Date: Sat, 27 Dec 2025 15:26:49 +0300 Subject: [PATCH 2/2] Add package --- QMap.Benchmarks/QMap.Benchmarks.csproj | 1 + 1 file changed, 1 insertion(+) diff --git a/QMap.Benchmarks/QMap.Benchmarks.csproj b/QMap.Benchmarks/QMap.Benchmarks.csproj index d1268bc..f988a97 100644 --- a/QMap.Benchmarks/QMap.Benchmarks.csproj +++ b/QMap.Benchmarks/QMap.Benchmarks.csproj @@ -10,6 +10,7 @@ +