Files
Marc Gravell aecffecae1 attempt NRT makeover (#1928)
* attempt NRT makeover

- annotate NRTs on GridReader API
- add protected access to some of the GridReader internals
- switch GridReader callbacks to be more type-independent

docs

lib updates; deal with yellow warnings and test failures (SQL server certs and cancellation surfacing differently)

fix break

simplify proposed GridReader changes to protected OnBeforeGrid and OnAfterGrid[Async]

Builds: Bump library versions (#1935)

Tests: Upgrade dependencies for Dependabot (#1936)

I'll do a pass at all of these later, but getting CVE versions out of the pipe.

Resolving 4 alerts here: https://github.com/DapperLib/Dapper/security/dependabot

merge and lib updates

allow Identity to be constructed on-the-fly inside GridReader

fix build warnings

include readme

rev minor

- enable [SkipLocalsInit] everywhere
- use NET5_0_OR_GREATER
 for remoting check

# Conflicts:
#	Dapper/CommandDefinition.cs
#	Dapper/DefaultTypeMap.cs
#	Dapper/SqlMapper.Async.cs
#	Dapper/SqlMapper.IDataReader.cs
#	Dapper/SqlMapper.Link.cs
#	Dapper/SqlMapper.cs
#	Directory.Build.props

* shipped, not unshipped

* fixup SqlBuilder; use is null / is not null

* use central package management (#1949)

* Nullable test tweaks

* Nuget: let's just remove it!

* 2 test fixes from review

* fix FindExplicitConstructor

* add GetPropertySetterOrThrow

* fix NRT on FindConstructor

* DapperRow: value is object?

* use NotNullWhen

* make constructor problem more obvious

* test fixes

---------

Co-authored-by: Nick Craver <nrcraver@gmail.com>
2023-09-12 15:58:49 +01:00

138 lines
5.1 KiB
C#

using System;
using System.Data;
using System.Data.Common;
using System.Globalization;
using System.Threading;
using Xunit;
namespace Dapper.Tests
{
public static class DatabaseProvider<TProvider> where TProvider : DatabaseProvider
{
public static TProvider Instance { get; } = Activator.CreateInstance<TProvider>();
}
public abstract class DatabaseProvider
{
public abstract DbProviderFactory Factory { get; }
public virtual void Dispose() { }
public abstract string GetConnectionString();
protected static string GetConnectionString(string name, string defaultConnectionString) =>
Environment.GetEnvironmentVariable(name) ?? defaultConnectionString;
public DbConnection GetOpenConnection()
{
var conn = Factory.CreateConnection()!;
conn.ConnectionString = GetConnectionString();
conn.Open();
if (conn.State != ConnectionState.Open) throw new InvalidOperationException("should be open!");
return conn;
}
public DbConnection GetClosedConnection()
{
var conn = Factory.CreateConnection()!;
conn.ConnectionString = GetConnectionString();
if (conn.State != ConnectionState.Closed) throw new InvalidOperationException("should be closed!");
return conn;
}
public DbParameter CreateRawParameter(string name, object value)
{
var p = Factory.CreateParameter()!;
p.ParameterName = name;
p.Value = value ?? DBNull.Value;
return p;
}
}
public abstract class SqlServerDatabaseProvider : DatabaseProvider
{
public override string GetConnectionString() => GetConnectionString(false);
private string GetConnectionString(bool mars)
{
var builder = Factory.CreateConnectionStringBuilder()!;
builder.ConnectionString = GetConnectionString("SqlServerConnectionString", "Data Source=.;Initial Catalog=tempdb;Integrated Security=True");
builder["TrustServerCertificate"] = true;
if (mars)
{
((dynamic)builder).MultipleActiveResultSets = true;
}
return builder.ConnectionString;
}
public DbConnection GetOpenConnection(bool mars)
{
var conn = Factory.CreateConnection()!;
conn.ConnectionString = GetConnectionString(mars);
conn.Open();
if (conn.State != ConnectionState.Open) throw new InvalidOperationException("should be open!");
return conn;
}
}
public sealed class SystemSqlClientProvider : SqlServerDatabaseProvider
{
public override DbProviderFactory Factory => System.Data.SqlClient.SqlClientFactory.Instance;
}
#if MSSQLCLIENT
public sealed class MicrosoftSqlClientProvider : SqlServerDatabaseProvider
{
public override DbProviderFactory Factory => Microsoft.Data.SqlClient.SqlClientFactory.Instance;
}
#endif
public abstract class TestBase<TProvider> : IDisposable where TProvider : DatabaseProvider
{
protected void SkipIfMsDataClient()
=> Skip.If<Microsoft.Data.SqlClient.SqlConnection>(connection);
protected DbConnection GetOpenConnection() => Provider.GetOpenConnection();
protected DbConnection GetClosedConnection() => Provider.GetClosedConnection();
protected DbConnection? _connection;
protected DbConnection connection => _connection ??= Provider.GetOpenConnection();
public TProvider Provider { get; } = DatabaseProvider<TProvider>.Instance;
protected static CultureInfo ActiveCulture
{
get { return Thread.CurrentThread.CurrentCulture; }
set { Thread.CurrentThread.CurrentCulture = value; }
}
static TestBase()
{
Console.WriteLine("Dapper: " + typeof(SqlMapper).AssemblyQualifiedName);
var provider = DatabaseProvider<TProvider>.Instance;
Console.WriteLine("Using Connectionstring: {0}", provider.GetConnectionString());
var factory = provider.Factory;
Console.WriteLine("Using Provider: {0}", factory.GetType().FullName);
Console.WriteLine(".NET: " + Environment.Version);
Console.Write("Loading native assemblies for SQL types...");
try
{
SqlServerTypesLoader.LoadNativeAssemblies(AppDomain.CurrentDomain.BaseDirectory);
Console.WriteLine("done.");
}
catch (Exception ex)
{
Console.WriteLine("failed.");
Console.Error.WriteLine(ex.Message);
}
}
public virtual void Dispose()
{
_connection?.Dispose();
_connection = null;
Provider?.Dispose();
}
}
public static class NonParallelDefinition
{
public const string Name = "NonParallel";
}
}