Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions QMap.Benchmarks/Benchmarks/SqlServer/QMapInsertBenchmark.cs
Original file line number Diff line number Diff line change
@@ -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<TypesTestEntity>()
.Without(t => t.Id)
.Create();

connection.Insert(entity, e => e.Id);

connection.Close();
}
}
}
1 change: 1 addition & 0 deletions QMap.Benchmarks/QMap.Benchmarks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.14.0" />
<PackageReference Include="Dapper" Version="2.1.35" />
<PackageReference Include="Dapper.Contrib" Version="2.0.78" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.0" />
Expand Down
4 changes: 2 additions & 2 deletions QMap.SqlBuilder.Tests/SqlBuilderParseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public void BuildUpdateNoThrowsErros()
entity.IntField = 2048;

var sql = queryBuilder
.Update<TypesTestEntity, int>(connectionFake, out var _, () => entity.IntField, entity.IntField)
.Update<TypesTestEntity, int>(connectionFake, out var _, (e) => entity.IntField, entity.IntField)
.Where<TypesTestEntity>((TypesTestEntity e) => e.Id > 0, out var parameters)
.Build();

Expand Down Expand Up @@ -138,7 +138,7 @@ public void BuildUpdateThrowsInvalidOperationExceptionWhenGetNoProperty()
Assert.Throws<InvalidOperationException>(() =>
{
var sql = queryBuilder
.Update<TypesTestEntity, string>(connectionFake, out var _, () => entity.ToString(), "")
.Update<TypesTestEntity, string>(connectionFake, out var _, (e) => entity.ToString(), "")
.Where<TypesTestEntity>((TypesTestEntity e) => e.Id > 0, out var parameters)
.Build();
});
Expand Down
2 changes: 1 addition & 1 deletion QMap.SqlBuilder/Abstractions/IUpdateBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ namespace QMap.SqlBuilder.Abstractions
{
public interface IUpdateBuilder : IQueryBuilder
{
IUpdateBuilder BuildUpdate<T, V>(Expression<Func<V>> propertySelector, V value);
IUpdateBuilder BuildUpdate<T, TProperty>(Expression<Func<T, TProperty>> propertySelector, TProperty value);
}
}
4 changes: 2 additions & 2 deletions QMap.SqlBuilder/QueryBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ public static ISelectBuilder Select(this IQueryBuilder queryBuilder, Type entity
.BuidSelect(entity);
}

public static IUpdateBuilder Update<T, V>(this IQueryBuilder queryBuilder, IQMapConnection connection, out Dictionary<string, object> parameters, Expression<Func<V>> propertySelectors, V value)
public static IUpdateBuilder Update<T, TProperty>(this IQueryBuilder queryBuilder, IQMapConnection connection, out Dictionary<string, object> parameters, Expression<Func<T, TProperty>> propertySelectors, TProperty value)
{
var builder = new UpdateBuilder(queryBuilder.SqlDialect)
.BuildUpdate<T, V>(propertySelectors, value);
.BuildUpdate<T, TProperty>(propertySelectors, value);

parameters = builder.Parameters;

Expand Down
2 changes: 1 addition & 1 deletion QMap.SqlBuilder/StatementsBuilders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ public UpdateBuilder(ISqlDialect sqlDialect) : base(sqlDialect)
{
}

public IUpdateBuilder BuildUpdate<T, V>(Expression<Func<V>> propertySelector, V value)
public IUpdateBuilder BuildUpdate<T, TProperty>(Expression<Func<T ,TProperty>> propertySelector, TProperty value)
{
var memberExpression = propertySelector.Body as MemberExpression;

Expand Down
8 changes: 4 additions & 4 deletions QMap.Tests/QMapConnectionExtensionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ public void UpdateNoThrowsErrors()
var newValue = new Random().Next();
entity.IntField = newValue;
//TSQL errors when parse True constant
connection.Update<TypesTestEntity, int>(() => entity.IntField, newValue, (TypesTestEntity e) => e.Id == entity.Id);
connection.Update<TypesTestEntity, int>((e) => entity.IntField, newValue, (TypesTestEntity e) => e.Id == entity.Id);

Assert.True(context.TypesTestEntity.Find(new object[] {entity.Id}).IntField == newValue);

Expand Down Expand Up @@ -342,7 +342,7 @@ public void UpdateUpdatesOnlyMathedEntities()
connection.Open();

//TSQL errors when parse True constant
connection.Update<TypesTestEntity, int>(() => updateEntity.IntField, newValue,(TypesTestEntity e) => e.Id == updateEntity.Id);
connection.Update<TypesTestEntity, int>((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));
Expand Down Expand Up @@ -461,7 +461,7 @@ public void Update_Should_Not_Pass_Drop_Statemant()

context.TypesTestEntity.Add(entity);
context.SaveChanges();
connection.Update<TypesTestEntity, string>(() => entity.StringField, "\'DROP TABLE TypesTestEntity;--", (TypesTestEntity t) => t.IntField > 0);
connection.Update<TypesTestEntity, string>((e) => entity.StringField, "\'DROP TABLE TypesTestEntity;--", (TypesTestEntity t) => t.IntField > 0);

context.TypesTestEntity.Count();
});
Expand Down Expand Up @@ -490,7 +490,7 @@ public void Update_Should_Not_Pass_Drop_Statemant_When_Injection_In_Where()

context.TypesTestEntity.Add(entity);
context.SaveChanges();
connection.Update<TypesTestEntity, string>(() => entity.StringField, "\'DROP TABLE TypesTestEntity;--", (TypesTestEntity t) => t.StringField != "\'DROP TABLE TypesTestEntity;--");
connection.Update<TypesTestEntity, string>((e) => entity.StringField, "\'DROP TABLE TypesTestEntity;--", (TypesTestEntity t) => t.StringField != "\'DROP TABLE TypesTestEntity;--");

context.TypesTestEntity.Count();
});
Expand Down
4 changes: 2 additions & 2 deletions QMap/QMapConnectionExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,12 @@ public static void Insert<T>(this IQMapConnection connection, T entity)
command.ExecuteNonQuery();
}

public static void Update<T, V>(this IQMapConnection connection, Expression<Func<V>> propertySelector, V value, LambdaExpression predicate) where T : class, new()
public static void Update<T, TProperty>(this IQMapConnection connection, Expression<Func<T, TProperty>> propertySelector, TProperty value, LambdaExpression predicate) where T : class, new()
{
var command = connection.CreateCommand();

var sql = new StatementsBuilders(connection.Dialect)
.Update<T,V>(connection, out var parameters, propertySelector, value)
.Update<T, TProperty>(connection, out var parameters, propertySelector, value)
.Where<T>(predicate, out var parameters1)
.Build();

Expand Down
Loading