Skip to content
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -294,4 +294,5 @@ __pycache__/
*.odx.cs
*.xsd.cs

*license.txt
*license.txt
/src/UnitTests/Resources/test_tmp.db
15 changes: 12 additions & 3 deletions src/Dapper.Contrib.Linq2Dapper.sln
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26430.16
# Visual Studio Version 16
VisualStudioVersion = 16.0.30523.141
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dapper.Contrib.Linq2Dapper", "Linq2Dapper\Dapper.Contrib.Linq2Dapper.csproj", "{6790F728-CB22-44BE-972E-7287373720D4}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Dapper.Contrib.Linq2Dapper", "Linq2Dapper\Dapper.Contrib.Linq2Dapper.csproj", "{6790F728-CB22-44BE-972E-7287373720D4}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UnitTests", "UnitTests\UnitTests.csproj", "{FF3463B1-BCD0-45A1-8AE4-523A276521A6}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -15,8 +17,15 @@ Global
{6790F728-CB22-44BE-972E-7287373720D4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6790F728-CB22-44BE-972E-7287373720D4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6790F728-CB22-44BE-972E-7287373720D4}.Release|Any CPU.Build.0 = Release|Any CPU
{FF3463B1-BCD0-45A1-8AE4-523A276521A6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FF3463B1-BCD0-45A1-8AE4-523A276521A6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FF3463B1-BCD0-45A1-8AE4-523A276521A6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FF3463B1-BCD0-45A1-8AE4-523A276521A6}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {185D6FA1-D6B8-438C-ABF9-AAC47EDA1585}
EndGlobalSection
EndGlobal
89 changes: 17 additions & 72 deletions src/Linq2Dapper/Dapper.Contrib.Linq2Dapper.csproj
Original file line number Diff line number Diff line change
@@ -1,76 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{6790F728-CB22-44BE-972E-7287373720D4}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Dapper.Contrib.Linq2Dapper</RootNamespace>
<AssemblyName>Dapper.Contrib.Linq2Dapper</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
<TargetFramework>netstandard2.0</TargetFramework>
<Description>A simple light weight LINQ provider for Dapper.Net</Description>
<PackageProjectUrl>https://github.com/brentmannering/linq-to-dapper/</PackageProjectUrl>
<PackageTags>linq, dapper</PackageTags>
<Authors>Brent Mannering</Authors>
<PackageVersion>1.0.4</PackageVersion>
<PackageId>Linq2Dapper</PackageId>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="Dapper, Version=1.40.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Dapper.1.42\lib\net45\Dapper.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Configuration" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Exceptions\InvalidCacheItemException.cs" />
<Compile Include="Helpers\CacheHelper.cs" />
<Compile Include="Helpers\Constants.cs" />
<Compile Include="Helpers\QueryHelper.cs" />
<Compile Include="Extensions\DapperExtensions.cs" />
<Compile Include="Helpers\SqlWriter.cs" />
<Compile Include="Helpers\TableHelper.cs" />
<Compile Include="Helpers\TypeHelper.cs" />
<Compile Include="QueryProvider.cs" />
<Compile Include="Exceptions\InvalidQueryException.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Linq2Dapper.cs" />
<Compile Include="QueryBuilder.cs" />
</ItemGroup>

<ItemGroup>
<None Include="packages.config" />
<PackageReference Include="Dapper" Version="2.0.4" />
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="System.ComponentModel.Annotations" Version="4.7.0" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>



</Project>
2 changes: 2 additions & 0 deletions src/Linq2Dapper/Extensions/DapperExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,7 @@ public static async Task<IQueryable<T>> QueryAsync<T>(this IDbConnection dbConne
{
return await Task.Run(() => new Linq2Dapper<T>(dbConnection, expression: expression));
}


}
}
1 change: 1 addition & 0 deletions src/Linq2Dapper/Extensions/LinqExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using System.Linq;

namespace Dapper.Contrib.Linq.Extensions
{
Expand Down
15 changes: 15 additions & 0 deletions src/Linq2Dapper/Extensions/PropertyInfoExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System.Collections;
using System.Linq;
using System.Reflection;

namespace Dapper.Contrib.Linq2Dapper.Extensions
{
public static class PropertyInfoExtensions
{
public static bool IsStatic(this PropertyInfo source, bool nonPublic = false)
=> source.GetAccessors(nonPublic).Any(x => x.IsStatic);

public static bool IsList(this PropertyInfo source, bool nonPublic = false)
=> typeof(IEnumerable).IsAssignableFrom(source.PropertyType);
}
}
3 changes: 2 additions & 1 deletion src/Linq2Dapper/Helpers/QueryHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using Dapper.Contrib.Linq2Dapper.Extensions;

namespace Dapper.Contrib.Linq2Dapper.Helpers
{
Expand Down Expand Up @@ -257,7 +258,7 @@ internal static TableHelper GetTypeProperties(Type type)

// get properties add to cache
var properties = new Dictionary<string, string>();
type.GetProperties().ToList().ForEach(
type.GetProperties().Where(p => !p.IsStatic() && !p.IsList()).ToList().ForEach(
x =>
{
var col = (ColumnAttribute)x.GetCustomAttribute(typeof(ColumnAttribute));
Expand Down
13 changes: 11 additions & 2 deletions src/Linq2Dapper/Linq2Dapper.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Linq.Expressions;

Expand Down Expand Up @@ -55,6 +53,17 @@ public Linq2Dapper(IDbConnection connection, IQueryProvider provider = null, Exp

#region Properties

public string Sql
{
get
{
var provider = (QueryProvider<TData>)Provider;
provider.QueryBuilder.Evaluate(Expression);
return provider.QueryBuilder.Sql;
}
}


public IQueryProvider Provider { get; private set; }
public IDbConnection Connection { get; private set; }
public Expression Expression { get; private set; }
Expand Down
36 changes: 0 additions & 36 deletions src/Linq2Dapper/Properties/AssemblyInfo.cs

This file was deleted.

14 changes: 6 additions & 8 deletions src/Linq2Dapper/QueryProvider.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Linq.Expressions;
using Dapper.Contrib.Linq2Dapper.Exceptions;
Expand All @@ -13,12 +11,12 @@ namespace Dapper.Contrib.Linq2Dapper
internal class QueryProvider<TData> : IQueryProvider
{
private readonly IDbConnection _connection;
private readonly QueryBuilder<TData> _qb;
internal readonly QueryBuilder<TData> QueryBuilder;

public QueryProvider(IDbConnection connection)
{
_connection = connection;
_qb = new QueryBuilder<TData>();
QueryBuilder = new QueryBuilder<TData>();
}

public IQueryable CreateQuery(Expression expression)
Expand Down Expand Up @@ -58,15 +56,15 @@ private object Query(Expression expression, bool isEnumerable = false)
{
if (_connection.State != ConnectionState.Open) _connection.Open();

_qb.Evaluate(expression);
var data = _connection.Query<TData>(_qb.Sql, _qb.Parameters);
QueryBuilder.Evaluate(expression);
var data = _connection.Query<TData>(QueryBuilder.Sql, QueryBuilder.Parameters);

if (isEnumerable) return data;
return data.ElementAt(0);
}
catch (SqlException ex)
catch (InvalidOperationException ex)
{
throw new InvalidQueryException(ex.Message + " | " + _qb.Sql);
throw new InvalidQueryException(ex.Message + " | " + QueryBuilder.Sql);
}
finally
{
Expand Down
20 changes: 20 additions & 0 deletions src/UnitTests/Models/Machine.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text;

namespace UnitTests.Models
{
[Table("Machine")]
public class Machine
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime DeliveredOn { get; set; }
public bool IsCool { get; set; }

public static string MyStaticField { get;set;}
public List<bool> JustAList { get; set; }
public bool[] JustAnArray { get; set; }
}
}
Binary file added src/UnitTests/Resources/test.db
Binary file not shown.
74 changes: 74 additions & 0 deletions src/UnitTests/UnitTest1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System;
using System.Data;
using System.IO;
using System.Linq;
using Dapper;
using Dapper.Contrib.Linq2Dapper;
using Dapper.Contrib.Linq2Dapper.Extensions;
using Microsoft.Data.Sqlite;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using UnitTests.Models;

namespace UnitTests
{
[TestClass]
public class UnitTest1
{
public UnitTest1()
{
SqlMapper.AddTypeHandler(DateTimeHandler.Default);
}
[TestMethod]
public void TestMethod1()
{
var dbFolder = Path.GetFullPath(@"..\..\..\Resources\");
var dbFile = Path.Combine(dbFolder, "test.db");
var tmpDbFile = Path.Combine(dbFolder, "test_tmp.db");

File.Copy(dbFile,tmpDbFile,true);

var connectionString = $@"Filename={tmpDbFile}";
var connection = new SqliteConnection(connectionString);

connection.Execute(
"insert into Machine (Id,Name,DeliveredOn,IsCool) values (@Id,@Name,@DeliveredOn,@IsCool)",
new { Id = 0, Name = "A", DeliveredOn = new DateTime(2000, 1, 1).Ticks, IsCool = false });
connection.Execute(
"insert into Machine (Id,Name,DeliveredOn,IsCool) values (@Id,@Name,@DeliveredOn,@IsCool)",
new { Id = 1, Name = "B", DeliveredOn = new DateTime(2000, 1, 2).Ticks, IsCool = false });
connection.Execute(
"insert into Machine (Id,Name,DeliveredOn,IsCool) values (@Id,@Name,@DeliveredOn,@IsCool)",
new { Id = 2, Name = "C", DeliveredOn = new DateTime(2000, 1, 3).Ticks, IsCool = true });
connection.Execute(
"insert into Machine (Id,Name,DeliveredOn,IsCool) values (@Id,@Name,@DeliveredOn,@IsCool)",
new { Id = 3, Name = "D", DeliveredOn = new DateTime(2000, 1, 4).Ticks, IsCool = false });

var query = connection.Query<Machine>(x => x.Id == 2);

var results = query.AsList();
Assert.AreEqual(1,results.Count);
Assert.AreEqual(2, results.First().Id);
Assert.AreEqual(new DateTime(2000, 1, 3), results.First().DeliveredOn);


}

public class DateTimeHandler : SqlMapper.TypeHandler<DateTime>
{
public static readonly DateTimeHandler Default = new DateTimeHandler();

public override void SetValue(IDbDataParameter parameter, DateTime value)
{

parameter.Value = value.Ticks;
}

public override DateTime Parse(object value)
{
var ticks = Convert.ToInt64(value);
//var ticks = (long)value;
return new DateTime(ticks);
}
}
}
}
Loading