Skip to content

Add missing schema name and remove deprecated packages #170

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ jobs:
run: dotnet test tests/Dapper.Tests.Contrib/Dapper.Tests.Contrib.csproj -c Release --logger GitHubActions /p:CI=true
env:
MySqlConnectionString: Server=localhost;Port=${{ job.services.mysql.ports[3306] }};Uid=root;Pwd=root;Database=test;Allow User Variables=true
OLEDBConnectionString: Provider=SQLOLEDB;Server=tcp:localhost,${{ job.services.sqlserver.ports[1433] }};Database=tempdb;User Id=sa;Password=Password.;
OLEDBConnectionString: Provider=SQLOLEDB;Server=tcp:localhost,${{ job.services.sqlserver.ports[1433] }};Database=tempdb;User Id=sa;Password=Password.;TrustServerCertificate=True;
PostgesConnectionString: Server=localhost;Port=${{ job.services.postgres.ports[5432] }};Database=test;User Id=postgres;Password=postgres;
SqlServerConnectionString: Server=tcp:localhost,${{ job.services.sqlserver.ports[1433] }};Database=tempdb;User Id=sa;Password=Password.;
SqlServerConnectionString: Server=tcp:localhost,${{ job.services.sqlserver.ports[1433] }};Database=tempdb;User Id=sa;Password=Password.;TrustServerCertificate=True;
- name: .NET Lib Pack
run: dotnet pack Build.csproj --no-build -c Release /p:PackageOutputPath=%CD%\.nupkgs /p:CI=true
7 changes: 4 additions & 3 deletions Dapper.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.28917.182
# Visual Studio Version 17
VisualStudioVersion = 17.11.35312.102
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{A34907DF-958A-4E4C-8491-84CF303FD13E}"
ProjectSection(SolutionItems) = preProject
Expand All @@ -11,12 +11,13 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
Directory.Build.props = Directory.Build.props
docs\index.md = docs\index.md
License.txt = License.txt
.github\workflows\main.yml = .github\workflows\main.yml
nuget.config = nuget.config
Readme.md = Readme.md
version.json = version.json
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Dapper.Contrib", "Dapper.Contrib\Dapper.Contrib.csproj", "{4E409F8F-CFBB-4332-8B0A-FD5A283051FD}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Dapper.Contrib", "src\Dapper.Contrib\Dapper.Contrib.csproj", "{4E409F8F-CFBB-4332-8B0A-FD5A283051FD}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Dapper.Tests.Contrib", "tests\Dapper.Tests.Contrib\Dapper.Tests.Contrib.csproj", "{DAB3C5B7-BCD1-4A5F-BB6B-50D2BB63DB4A}"
EndProject
Expand Down
4 changes: 2 additions & 2 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,10 @@ Special Attributes
----------
Dapper.Contrib makes use of some optional attributes:

* `[Table("Tablename")]` - use another table name instead of the (by default pluralized) name of the class
* `[Table("Tablename", Schema = "SchemaName")]` - use another table name instead of the (by default pluralized) name of the class, Schema can be optionally passed as well.

```csharp
[Table ("emps")]
[Table ("emps", Schema = "emp")]
public class Employee
{
public int Id { get; set; }
Expand Down
15 changes: 8 additions & 7 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
image: Visual Studio 2019
image: Visual Studio 2022

skip_branch_with_pr: true
skip_tags: true
Expand All @@ -9,14 +9,14 @@ skip_commits:
environment:
Appveyor: true
# Postgres
POSTGRES_PATH: C:\Program Files\PostgreSQL\9.6
POSTGRES_PATH: C:\Program Files\PostgreSQL\13
PGUSER: postgres
PGPASSWORD: Password12!
POSTGRES_ENV_POSTGRES_USER: postgres
POSTGRES_ENV_POSTGRES_PASSWORD: Password12!
POSTGRES_ENV_POSTGRES_DB: test
# MySQL
MYSQL_PATH: C:\Program Files\MySql\MySQL Server 5.7
MYSQL_PATH: C:\Program Files\MySQL\MySQL Server 8.0
MYSQL_PWD: Password12!
MYSQL_ENV_MYSQL_USER: root
MYSQL_ENV_MYSQL_PASSWORD: Password12!
Expand All @@ -25,16 +25,17 @@ environment:
MySqlConnectionString: Server=localhost;Database=test;Uid=root;Pwd=Password12!
OLEDBConnectionString: Provider=SQLOLEDB;Data Source=(local)\SQL2019;Initial Catalog=tempdb;User Id=sa;Password=Password12!
PostgesConnectionString: Server=localhost;Port=5432;User Id=postgres;Password=Password12!;Database=test
SqlServerConnectionString: Server=(local)\SQL2019;Database=tempdb;User ID=sa;Password=Password12!
SqlServerConnectionString: Server=(local)\SQL2019;Database=tempdb;User ID=sa;Password=Password12!;TrustServerCertificate=True

services:
- mysql
- postgresql
services:
- postgresql13

init:
- ps: Start-Service MySQL80
- git config --global core.autocrlf input
- SET PATH=%POSTGRES_PATH%\bin;%MYSQL_PATH%\bin;%PATH%
- net start MSSQL$SQL2019


nuget:
disable_publish_on_pr: true
Expand Down
12 changes: 10 additions & 2 deletions src/Dapper.Contrib/Dapper.Contrib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
<PackageTags>orm;sql;micro-orm;dapper</PackageTags>
<Description>The official collection of get, insert, update and delete helpers for Dapper.net. Also handles lists of entities and optional "dirty" tracking of interface-based entities.</Description>
<Authors>Sam Saffron;Johan Danforth</Authors>
<TargetFrameworks>net461;netstandard2.0;net5.0</TargetFrameworks>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<TreatWarningsAsErrors>false</TreatWarningsAsErrors>
<NoWarn>$(NoWarn);CA1050</NoWarn>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Dapper" Version="2.0.78" />
<PackageReference Include="Dapper" Version="2.1.35" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
<PackageReference Include="System.Reflection.Emit" Version="4.7.0" />
Expand All @@ -19,4 +19,12 @@
<ItemGroup Condition=" '$(TargetFramework)' == 'net461' ">
<Reference Include="Microsoft.CSharp" />
</ItemGroup>
<ItemGroup>
<PackageReference Update="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Update="Microsoft.SourceLink.GitHub" Version="8.0.0" />
<PackageReference Update="Nerdbank.GitVersioning" Version="3.6.143" />
</ItemGroup>
</Project>
31 changes: 22 additions & 9 deletions src/Dapper.Contrib/SqlMapperExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Collections.Concurrent;
using System.Reflection.Emit;
using System.Threading;

using System.Text;
using Dapper;

namespace Dapper.Contrib.Extensions
{

/// <summary>
/// The Dapper.Contrib extensions for Dapper
/// </summary>
Expand Down Expand Up @@ -187,7 +186,7 @@ public static T Get<T>(this IDbConnection connection, dynamic id, IDbTransaction

if (type.IsInterface)
{
if (!(connection.Query(sql, dynParams).FirstOrDefault() is IDictionary<string, object> res))
if (connection.Query(sql, dynParams).FirstOrDefault() is not IDictionary<string, object> res)
{
return null;
}
Expand Down Expand Up @@ -292,14 +291,21 @@ private static string GetTableName(Type type)
type.GetCustomAttribute<TableAttribute>(false)?.Name
?? (type.GetCustomAttributes(false).FirstOrDefault(attr => attr.GetType().Name == "TableAttribute") as dynamic)?.Name;

var tableAttrSchema = type.GetCustomAttribute<TableAttribute>(false)?.Schema;

if (!string.IsNullOrEmpty(tableAttrSchema))
{
tableAttrName = $"{tableAttrSchema}.{tableAttrName}";
}

if (tableAttrName != null)
{
name = tableAttrName;
}
else
{
name = type.Name + "s";
if (type.IsInterface && name.StartsWith("I"))
if (type.IsInterface && name.StartsWith('I'))
name = name.Substring(1);
}
}
Expand Down Expand Up @@ -601,7 +607,7 @@ public static T GetInterfaceProxy<T>()
return (T)Activator.CreateInstance(generatedType);
}

private static MethodInfo CreateIsDirtyProperty(TypeBuilder typeBuilder)
private static MethodBuilder CreateIsDirtyProperty(TypeBuilder typeBuilder)
{
var propType = typeof(bool);
var field = typeBuilder.DefineField("_" + nameof(IProxy.IsDirty), propType, FieldAttributes.Private);
Expand Down Expand Up @@ -642,7 +648,7 @@ private static MethodInfo CreateIsDirtyProperty(TypeBuilder typeBuilder)
return currSetPropMthdBldr;
}

private static void CreateProperty<T>(TypeBuilder typeBuilder, string propertyName, Type propType, MethodInfo setIsDirtyMethod, bool isIdentity)
private static void CreateProperty<T>(TypeBuilder typeBuilder, string propertyName, Type propType, MethodBuilder setIsDirtyMethod, bool isIdentity)
{
//Define the field and the property
var field = typeBuilder.DefineField("_" + propertyName, propType, FieldAttributes.Private);
Expand Down Expand Up @@ -710,15 +716,22 @@ public class TableAttribute : Attribute
/// Creates a table mapping to a specific name for Dapper.Contrib commands
/// </summary>
/// <param name="tableName">The name of this table in the database.</param>
public TableAttribute(string tableName)
/// <param name="schema">The schema name of this table in the database</param>
public TableAttribute(string tableName, string schema = "")
{
Name = tableName;
Schema = schema;
}

/// <summary>
/// The name of the table in the database
/// </summary>
public string Name { get; set; }

/// <summary>
/// The schema of the table in the database
/// </summary>
public string Schema { get; set; }
}

/// <summary>
Expand Down
26 changes: 22 additions & 4 deletions tests/Dapper.Tests.Contrib/Dapper.Tests.Contrib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<AssemblyName>Dapper.Tests.Contrib</AssemblyName>
<Description>Dapper Contrib Test Suite</Description>
<TargetFrameworks>netcoreapp3.1;net462;net5.0</TargetFrameworks>
<TargetFrameworks>net6.0;net7.0;net8.0</TargetFrameworks>
<NoWarn>$(NoWarn);CA1816;IDE0063;xUnit1004</NoWarn>
</PropertyGroup>
<ItemGroup>
Expand All @@ -13,11 +13,29 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Dapper.SqlBuilder" Version="2.0.78" />
<PackageReference Include="Microsoft.Data.Sqlite" Version="5.0.0" />
<PackageReference Include="MySqlConnector" Version="1.1.0" />
<PackageReference Include="System.Data.SqlClient" Version="4.8.2" />
<PackageReference Include="Microsoft.Data.SqlClient" Version="5.2.2" />
<PackageReference Include="Microsoft.Data.Sqlite" Version="8.0.8" />
<PackageReference Include="MySqlConnector" Version="2.3.7" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net462' ">
<Reference Include="Microsoft.CSharp" />
</ItemGroup>
<ItemGroup>
<PackageReference Update="GitHubActionsTestLogger" Version="2.4.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Update="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Update="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Update="Microsoft.SourceLink.GitHub" Version="8.0.0" />
<PackageReference Update="Nerdbank.GitVersioning" Version="3.6.143" />
<PackageReference Update="xunit" Version="2.9.0" />
<PackageReference Update="xunit.runner.visualstudio" Version="2.8.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project>
29 changes: 14 additions & 15 deletions tests/Dapper.Tests.Contrib/TestSuite.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Threading.Tasks;

using Dapper.Contrib.Extensions;
using FactAttribute = Dapper.Tests.Contrib.SkippableFactAttribute;
using Xunit;

namespace Dapper.Tests.Contrib
Expand Down Expand Up @@ -125,7 +124,7 @@ public async Task InsertGetUpdateDeleteWithExplicitKeyAsync()
var list2 = (await connection.QueryAsync<ObjectY>("select * from ObjectY").ConfigureAwait(false)).ToList();
Assert.Equal(list2.Count, originalyCount + 1);
o2 = await connection.GetAsync<ObjectY>(id).ConfigureAwait(false);
Assert.Equal(o2.ObjectYId, id);
Assert.Equal(id, o2.ObjectYId);
o2.Name = "Bar";
await connection.UpdateAsync(o2).ConfigureAwait(false);
o2 = await connection.GetAsync<ObjectY>(id).ConfigureAwait(false);
Expand Down Expand Up @@ -305,9 +304,9 @@ private async Task InsertHelperAsync<T>(Func<IEnumerable<User>, T> helper)
await connection.DeleteAllAsync<User>().ConfigureAwait(false);

var total = await connection.InsertAsync(helper(users)).ConfigureAwait(false);
Assert.Equal(total, numberOfEntities);
Assert.Equal(numberOfEntities, total);
users = connection.Query<User>("select * from Users").ToList();
Assert.Equal(users.Count, numberOfEntities);
Assert.Equal(numberOfEntities, users.Count);
}
}

Expand Down Expand Up @@ -343,9 +342,9 @@ private async Task UpdateHelperAsync<T>(Func<IEnumerable<User>, T> helper)
await connection.DeleteAllAsync<User>().ConfigureAwait(false);

var total = await connection.InsertAsync(helper(users)).ConfigureAwait(false);
Assert.Equal(total, numberOfEntities);
Assert.Equal(numberOfEntities, total);
users = connection.Query<User>("select * from Users").ToList();
Assert.Equal(users.Count, numberOfEntities);
Assert.Equal(numberOfEntities, users.Count);
foreach (var user in users)
{
user.Name += " updated";
Expand Down Expand Up @@ -377,7 +376,7 @@ public async Task DeleteListAsync()
private async Task DeleteHelperAsync<T>(Func<IEnumerable<User>, T> helper)
where T : class
{
const int numberOfEntities = 10;
int numberOfEntities = 10;

var users = new List<User>(numberOfEntities);
for (var i = 0; i < numberOfEntities; i++)
Expand All @@ -388,14 +387,14 @@ private async Task DeleteHelperAsync<T>(Func<IEnumerable<User>, T> helper)
await connection.DeleteAllAsync<User>().ConfigureAwait(false);

var total = await connection.InsertAsync(helper(users)).ConfigureAwait(false);
Assert.Equal(total, numberOfEntities);
Assert.Equal(numberOfEntities, total);
users = connection.Query<User>("select * from Users").ToList();
Assert.Equal(users.Count, numberOfEntities);
Assert.Equal(numberOfEntities, users.Count);

var usersToDelete = users.Take(10).ToList();
await connection.DeleteAsync(helper(usersToDelete)).ConfigureAwait(false);
users = connection.Query<User>("select * from Users").ToList();
Assert.Equal(users.Count, numberOfEntities - 10);
Assert.Equal(numberOfEntities - 10, users.Count);
}
}

Expand All @@ -413,11 +412,11 @@ public async Task GetAllAsync()
await connection.DeleteAllAsync<User>().ConfigureAwait(false);

var total = await connection.InsertAsync(users).ConfigureAwait(false);
Assert.Equal(total, numberOfEntities);
Assert.Equal(numberOfEntities, total);
users = (List<User>)await connection.GetAllAsync<User>().ConfigureAwait(false);
Assert.Equal(users.Count, numberOfEntities);
Assert.Equal(numberOfEntities, users.Count);
var iusers = await connection.GetAllAsync<IUser>().ConfigureAwait(false);
Assert.Equal(iusers.ToList().Count, numberOfEntities);
Assert.Equal(numberOfEntities, iusers.ToList().Count);
}
}

Expand All @@ -436,12 +435,12 @@ public async void GetAsyncAndGetAllAsyncWithNullableValues()
Assert.Equal(new DateTime(2011, 07, 14), value1.DateValue.Value);

var value2 = await connection.GetAsync<INullableDate>(id2).ConfigureAwait(false);
Assert.True(value2.DateValue == null);
Assert.Null(value2.DateValue);

var value3 = await connection.GetAllAsync<INullableDate>().ConfigureAwait(false);
var valuesList = value3.ToList();
Assert.Equal(new DateTime(2011, 07, 14), valuesList[0].DateValue.Value);
Assert.True(valuesList[1].DateValue == null);
Assert.Null(valuesList[1].DateValue);
}
}

Expand Down
Loading