Skip to content

Commit

Permalink
Initial 5.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
vbilopav committed Nov 28, 2023
1 parent a6b5e74 commit ec8ff04
Show file tree
Hide file tree
Showing 10 changed files with 217 additions and 30 deletions.
4 changes: 2 additions & 2 deletions Norm/Execute/NormExecute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public Norm Execute(string command,
this.sourceFilePath = sourceFilePath;
this.sourceLineNumber = sourceLineNumber;
using var cmd = CreateCommand(command);
cmd.ExecuteNonQuery();
this.recordsAffected = cmd.ExecuteNonQuery();
return this;
}

Expand All @@ -54,7 +54,7 @@ public Norm ExecuteFormat(FormattableString command,
this.sourceFilePath = sourceFilePath;
this.sourceLineNumber = sourceLineNumber;
using var cmd = CreateCommand(command);
cmd.ExecuteNonQuery();
this.recordsAffected = cmd.ExecuteNonQuery();
return this;
}
}
Expand Down
8 changes: 4 additions & 4 deletions Norm/Execute/NormExecuteAsync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ public async ValueTask<Norm> ExecuteAsync(string command,
using var cmd = await CreateCommandAsync(command);
if (cancellationToken.HasValue)
{
await cmd.ExecuteNonQueryAsync(cancellationToken.Value);
this.recordsAffected = await cmd.ExecuteNonQueryAsync(cancellationToken.Value);
}
else
{
await cmd.ExecuteNonQueryAsync();
this.recordsAffected = await cmd.ExecuteNonQueryAsync();
}
return this;
}
Expand Down Expand Up @@ -65,11 +65,11 @@ public async ValueTask<Norm> ExecuteFormatAsync(FormattableString command,
using var cmd = await CreateCommandAsync(command);
if (cancellationToken.HasValue)
{
await cmd.ExecuteNonQueryAsync(cancellationToken.Value);
this.recordsAffected = await cmd.ExecuteNonQueryAsync(cancellationToken.Value);
}
else
{
await cmd.ExecuteNonQueryAsync();
this.recordsAffected = await cmd.ExecuteNonQueryAsync();
}
return this;
}
Expand Down
10 changes: 4 additions & 6 deletions Norm/Extensions/Execute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,15 @@ public static partial class NormExtensions
///<param name="command">SQL command text.</param>
///<param name="parameters">Database parameters object (anonymous object or SqlParameter array).</param>
///<returns>Same DbConnection instance.</returns>
public static DbConnection Execute(this DbConnection connection, string command,
public static Norm Execute(this DbConnection connection, string command,
object parameters = null,
#pragma warning disable CS1573 // Parameter has no matching param tag in the XML comment (but other parameters do)
[CallerMemberName] string memberName = "",
[CallerFilePath] string sourceFilePath = "",
[CallerLineNumber] int sourceLineNumber = 0)
#pragma warning restore CS1573 // Parameter has no matching param tag in the XML comment (but other parameters do)
{
connection.Instance<Norm>().Execute(command, parameters, memberName, sourceFilePath, sourceLineNumber);
return connection;
return connection.Instance<Norm>().Execute(command, parameters, memberName, sourceFilePath, sourceLineNumber);
}
///<summary>
/// Parse interpolated (formattable) command as database parameters and execute resulting SQL.
Expand All @@ -32,16 +31,15 @@ public static DbConnection Execute(this DbConnection connection, string command,
///<param name="command">SQL command text.</param>
///<param name="parameters">Database parameters object (anonymous object or SqlParameter array).</param>
///<returns>Same DbConnection instance.</returns>
public static DbConnection ExecuteFormat(this DbConnection connection, FormattableString command,
public static Norm ExecuteFormat(this DbConnection connection, FormattableString command,
object parameters = null,
#pragma warning disable CS1573 // Parameter has no matching param tag in the XML comment (but other parameters do)
[CallerMemberName] string memberName = "",
[CallerFilePath] string sourceFilePath = "",
[CallerLineNumber] int sourceLineNumber = 0)
#pragma warning restore CS1573 // Parameter has no matching param tag in the XML comment (but other parameters do)
{
connection.Instance<Norm>().ExecuteFormat(command, parameters, memberName, sourceFilePath, sourceLineNumber);
return connection;
return connection.Instance<Norm>().ExecuteFormat(command, parameters, memberName, sourceFilePath, sourceLineNumber);
}
}
}
22 changes: 21 additions & 1 deletion Norm/Extensions/NormExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Threading;

namespace Norm
{

public static partial class NormExtensions
{
private static T Instance<T>(this DbConnection connection) where T : Norm
Expand Down Expand Up @@ -218,5 +218,25 @@ public static Norm WithUnknownResultType(this DbConnection connection, params bo
{
return connection.Instance<Norm>().WithUnknownResultType(list);
}

///<summary>
///Returns number of rows affected for this instance (rows changed, inserted, or deleted by execution of the SQL statement).
///This is a number returned by the command ExecuteNonQuery() call for the last executed command.
///Or, value of RecordsAffected reader property of the last read command.
///</summary>
public static int? GetRecordsAffected(this DbConnection connection)
{
return connection.Instance<Norm>().GetRecordsAffected();
}

/// <summary>
/// Creates a new Norm Instance
/// </summary>
/// <param name="connection"></param>
/// <returns></returns>
public static Norm Norm(this DbConnection connection)
{
return connection.Instance<Norm>();
}
}
}
75 changes: 69 additions & 6 deletions Norm/Norm.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
using System;
using System.Data;
using System.Data.Common;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Input;

namespace Norm
{
public partial class Norm
public partial class Norm : IDisposable, IAsyncDisposable
{
protected string commandText;
protected string commentHeader;
Expand Down Expand Up @@ -36,8 +38,9 @@ public partial class Norm
protected string memberName = null;
protected string sourceFilePath = null;
protected int sourceLineNumber = 0;
protected int? recordsAffected = null;
private string[] names = null;

public DatabaseType DbType => dbType;

public Norm(DbConnection connection)
Expand All @@ -60,10 +63,52 @@ public Norm(DbConnection connection)
}
}

public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}

public async ValueTask DisposeAsync()
{
await DisposeAsyncCore().ConfigureAwait(false);

Dispose(disposing: false);
#pragma warning disable CA1816 // Dispose methods should call SuppressFinalize
GC.SuppressFinalize(this);
#pragma warning restore CA1816 // Dispose methods should call SuppressFinalize
}

protected virtual void Dispose(bool disposing)
{
if (disposing)
{
Connection?.Dispose();
Connection = null;
transaction?.Dispose();
transaction = null;
}
}

protected virtual async ValueTask DisposeAsyncCore()
{
if (Connection != null)
{
await Connection.DisposeAsync().ConfigureAwait(false);
Connection = null;
}

if (transaction != null)
{
await transaction.DisposeAsync().ConfigureAwait(false);
transaction = null;
}
}

///<summary>
///returns DbConnection for this instance
///</summary>
public DbConnection Connection { get; }
public DbConnection Connection { get; private set; }

///<summary>
///returns CancellationToken for this instance or null if CancellationToken is not set
Expand All @@ -75,6 +120,16 @@ public Norm(DbConnection connection)
///</summary>
public Func<(string Name, int Ordinal, DbDataReader Reader), object> ReaderCallback { get => readerCallback; }

///<summary>
///Returns number of rows affected for this instance (rows changed, inserted, or deleted by execution of the SQL statement).
///This is a number returned by the command ExecuteNonQuery() call for the last executed command.
///Or, value of RecordsAffected reader property of the last read command.
///</summary>
public int? GetRecordsAffected()
{
return this.recordsAffected;
}

///<summary>
/// Set command type for the connection commands and return Norm instance.
/// Default command type for new instance is Text.
Expand Down Expand Up @@ -422,7 +477,9 @@ public async ValueTask<DbCommand> CreateCommandAsync(FormattableString command)
/// <returns>DbDataReader</returns>
public DbDataReader ExecuteReader(DbCommand cmd)
{
return cmd.ExecuteReader(this.behavior);
var reader = cmd.ExecuteReader(this.behavior);
this.recordsAffected = reader.RecordsAffected;
return reader;
}

/// <summary>
Expand All @@ -432,11 +489,17 @@ public DbDataReader ExecuteReader(DbCommand cmd)
/// <returns>DbDataReader</returns>
public async ValueTask<DbDataReader> ExecuteReaderAsync(DbCommand cmd)
{
DbDataReader reader;
if (this.cancellationToken.HasValue)
{
return await cmd.ExecuteReaderAsync(this.behavior, this.cancellationToken.Value);
reader = await cmd.ExecuteReaderAsync(this.behavior, this.cancellationToken.Value);
}
else
{
reader = await cmd.ExecuteReaderAsync(this.behavior);
}
return await cmd.ExecuteReaderAsync(this.behavior);
this.recordsAffected = reader.RecordsAffected;
return reader;
}
}
}
8 changes: 4 additions & 4 deletions Norm/Norm.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@
<Description>High performance micro-ORM modern Dapper replacement for .NET Standard 2.1 and higher</Description>
<Copyright>VB-Consulting</Copyright>
<PackageTags>Norm;norm;data acceess;orm;no-orm;micro-orm;sql</PackageTags>
<Version>5.3.9</Version>
<AssemblyVersion>5.3.9</AssemblyVersion>
<Version>5.4.0</Version>
<AssemblyVersion>5.4.0</AssemblyVersion>
<PackageReleaseNotes>https://github.com/vb-consulting/Norm.net/blob/master/changelog.md</PackageReleaseNotes>
<PackageId>Norm.net</PackageId>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<FileVersion>5.3.9</FileVersion>
<FileVersion>5.4.0</FileVersion>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<IncludeSymbols>true</IncludeSymbols>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>$(NoWarn);1591</NoWarn>
<DocumentationFile>bin\$(Configuration)\$(AssemblyName).xml</DocumentationFile>
<PackageVersion>5.3.9</PackageVersion>
<PackageVersion>5.4.0</PackageVersion>
</PropertyGroup>

<PropertyGroup Condition="'$(GITHUB_ACTIONS)' == 'true'">
Expand Down
9 changes: 6 additions & 3 deletions Tests/PostgreSqlUnitTests/FormattableUnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,17 @@ public void Execute_Format_With_Sql_Params_Test()
public void Execute_Format_Mixed_Params_Test()
{
using var connection = new NpgsqlConnection(fixture.ConnectionString);
var result = connection

connection
.Execute("begin")
.Execute("create table test (i int, t text, d date)")
.ExecuteFormat($"insert into test values ({1}, {new NpgsqlParameter("", "foo")}, {new DateTime(1977, 5, 19)})")
.ExecuteFormat($"insert into test values ({1}, {new NpgsqlParameter("", "foo")}, {new DateTime(1977, 5, 19)})");

var result = connection
.Read("select * from test")
.Single()
.ToDictionary(t => t.name, t => t.value);

Assert.Equal(1, result["i"]);
Assert.Equal("foo", result["t"]);
Assert.Equal(new DateTime(1977, 5, 19), result["d"]);
Expand Down
38 changes: 38 additions & 0 deletions Tests/PostgreSqlUnitTests/RowsAffectedUnitTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Norm;
using Npgsql;
using Xunit;

namespace PostgreSqlUnitTests;

[Collection("PostgreSqlDatabase")]
public class RowsAffectedUnitTests
{
private readonly PostgreSqlFixture fixture;

public RowsAffectedUnitTests(PostgreSqlFixture fixture)
{
this.fixture = fixture;
}

[Fact]
public void Execute_Format_Test()
{
using var connection = new NpgsqlConnection(fixture.ConnectionString);
connection.Execute("create temp table rows_affected_test (t text)");

var rowsAffected = connection
.Execute("insert into rows_affected_test values ('foo')")
.GetRecordsAffected();

Assert.Equal(1, rowsAffected);

var inst = connection.Norm();
inst.Read("select * from rows_affected_test").ToList();
rowsAffected = inst.GetRecordsAffected();

Assert.Equal(1, rowsAffected);
}
}
8 changes: 4 additions & 4 deletions Tests/PostgreSqlUnitTests/WithTransactionUnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public WithTransactionUnitTests(PostgreSqlFixture fixture)
[Fact]
public void WithTransaction_Rollback_Test()
{
using var connection = new NpgsqlConnection(fixture.ConnectionString)
.Execute("create temp table transaction_test1 (i int);");
using var connection = new NpgsqlConnection(fixture.ConnectionString);
connection.Execute("create temp table transaction_test1 (i int);");

using var transaction = connection.BeginTransaction();

Expand All @@ -41,8 +41,8 @@ public void WithTransaction_Rollback_Test()
[Fact]
public async Task WithTransaction_Rollback_Test_Async()
{
await using var connection = new NpgsqlConnection(fixture.ConnectionString)
.Execute("create temp table transaction_test2 (i int);");
await using var connection = new NpgsqlConnection(fixture.ConnectionString);
connection.Execute("create temp table transaction_test2 (i int);");

await using var transaction = await connection.BeginTransactionAsync();

Expand Down
Loading

0 comments on commit ec8ff04

Please sign in to comment.