Skip to content

Commit 07d329b

Browse files
committed
feat(db configurations): add microsoft sqlite driver and cfg
1 parent 40149ae commit 07d329b

File tree

5 files changed

+183
-35
lines changed

5 files changed

+183
-35
lines changed
Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>net461;netcoreapp2.0</TargetFrameworks>
4+
<TargetFrameworks>net461;netcoreapp2.0</TargetFrameworks>
55
<NoWarn>1591</NoWarn>
66
<PlatformTarget>AnyCpu</PlatformTarget>
77
<DebugType Condition="'$(TargetFramework)' != '' AND '$(TargetFramework)' != 'netcoreapp2.0'">Full</DebugType>
8-
</PropertyGroup>
8+
</PropertyGroup>
99

1010
<Import Project="$(MSBuildThisFileDirectory)..\Shared.msbuild" />
1111

@@ -14,39 +14,38 @@
1414
<ProjectReference Include="..\FluentNHibernate\FluentNHibernate.csproj" />
1515
</ItemGroup>
1616

17+
<ItemGroup>
18+
<ProjectReference Include="..\FluentNHibernate.Specs.ExternalFixtures\FluentNHibernate.Specs.ExternalFixtures.csproj" />
19+
<ProjectReference Include="..\FluentNHibernate\FluentNHibernate.csproj" />
20+
</ItemGroup>
21+
22+
<ItemGroup>
23+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
24+
<PackageReference Include="NHibernate" Version="5.1.0" />
25+
<PackageReference Include="System.Data.SqlClient" Version="4.4.3" />
26+
<PackageReference Include="FluentAssertions" Version="4.19.4" />
27+
<PackageReference Include="Machine.Specifications" Version="0.12.0" />
28+
<PackageReference Include="Machine.Specifications.Runner.Console" Version="0.9.3" />
29+
<PackageReference Include="Machine.Specifications.Runner.VisualStudio" Version="2.6.1" />
30+
</ItemGroup>
31+
1732
<ItemGroup Condition="'$(TargetFramework)' == 'net461'">
1833
<Reference Include="System.Configuration" />
1934
<Reference Include="System.Runtime.Remoting" />
2035
<Reference Include="System.ServiceModel" />
2136
<Reference Include="System.Transactions" />
2237
<Reference Include="System.Data.DataSetExtensions" />
2338
<Reference Include="System.Xml.Serialization" />
24-
<PackageReference Include="NHibernate" Version="5.1.0" />
25-
<PackageReference Include="FluentAssertions" Version="4.19.4" />
26-
<PackageReference Include="Machine.Specifications" Version="0.12.0" />
27-
<PackageReference Include="Machine.Specifications.Runner.Console" Version="0.9.3" />
28-
<PackageReference Include="Machine.Specifications.Runner.VisualStudio" Version="2.5.2" />
2939
<PackageReference Include="Mono.Cecil" Version="0.9.6.1" />
3040
</ItemGroup>
3141

32-
<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp2.0'">
33-
<PackageReference Include="NHibernate" Version="5.1.0" />
34-
<PackageReference Include="FluentAssertions" Version="4.19.4" />
35-
<PackageReference Include="Machine.Specifications" Version="0.12.0" />
36-
<PackageReference Include="Machine.Specifications.Runner.Console" Version="0.9.3" />
37-
<PackageReference Include="Machine.Specifications.Runner.VisualStudio" Version="2.5.2" />
38-
</ItemGroup>
39-
40-
41-
4242
<ItemGroup>
4343
<Compile Include="..\CommonAssemblyInfo.cs">
4444
<Link>Properties\CommonAssemblyInfo.cs</Link>
45-
</Compile>
45+
</Compile>
4646
</ItemGroup>
47-
47+
4848
<ItemGroup>
4949
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
5050
</ItemGroup>
51-
5251
</Project>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using FluentNHibernate.Cfg.Db;
2+
using NUnit.Framework;
3+
4+
namespace FluentNHibernate.Testing.Cfg.Db
5+
{
6+
[TestFixture]
7+
public class MsSqliteConfigurationTester
8+
{
9+
[Test]
10+
public void should_set_up_default_query_substitutions()
11+
{
12+
new MsSqliteConfiguration().ToProperties()["query.substitutions"].ShouldEqual("true=1;false=0");
13+
}
14+
15+
[Test]
16+
public void in_memory_should_set_up_expected_connection_string()
17+
{
18+
new MsSqliteConfiguration().InMemory()
19+
.ToProperties()["connection.connection_string"].ShouldEqual("Data Source=:memory:");
20+
}
21+
22+
[Test]
23+
public void using_file_should_set_up_expected_connection_string()
24+
{
25+
new MsSqliteConfiguration().UsingFile("foo")
26+
.ToProperties()["connection.connection_string"].ShouldEqual("Data Source=foo");
27+
}
28+
}
29+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using FluentNHibernate.Driver;
2+
using NHibernate.Dialect;
3+
4+
namespace FluentNHibernate.Cfg.Db
5+
{
6+
public class MsSqliteConfiguration : PersistenceConfiguration<MsSqliteConfiguration>
7+
{
8+
public static MsSqliteConfiguration Standard
9+
{
10+
get { return new MsSqliteConfiguration(); }
11+
}
12+
13+
public MsSqliteConfiguration()
14+
{
15+
Driver<MsSQLiteDriver>();
16+
Dialect<SQLiteDialect>();
17+
Raw("query.substitutions", "true=1;false=0");
18+
}
19+
20+
public MsSqliteConfiguration InMemory()
21+
{
22+
Raw("connection.release_mode", "on_close");
23+
24+
return ConnectionString(c => c
25+
.Is("Data Source=:memory:"));
26+
}
27+
28+
public MsSqliteConfiguration UsingFile(string fileName)
29+
{
30+
return ConnectionString(c => c
31+
.Is(string.Format("Data Source={0}", fileName)));
32+
}
33+
}
34+
35+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
using NHibernate;
2+
using NHibernate.Driver;
3+
using NHibernate.Engine;
4+
using System.Data;
5+
using System.Data.Common;
6+
7+
namespace FluentNHibernate.Driver
8+
{
9+
/// <summary>
10+
/// NHibernate driver for the Microsoft.Data.Sqlite data provider for .NETStandard2.0.
11+
/// </summary>
12+
/// <remarks>
13+
/// <para>
14+
/// In order to use this driver you must have the Microsoft.Data.Sqlite.dll assembly available
15+
/// for NHibernate to load.
16+
/// </para>
17+
/// <para>
18+
/// You can get the Microsoft.Data.Sqlite.dll assembly from
19+
/// <a href="https://github.com/aspnet/Microsoft.Data.Sqlite">https://github.com/aspnet/Microsoft.Data.Sqlite</a>
20+
/// </para>
21+
/// <para>
22+
/// Please check <a href="https://github.com/aspnet/Microsoft.Data.Sqlite">https://github.com/aspnet/Microsoft.Data.Sqlite</a> for more information regarding SQLite.
23+
/// </para>
24+
/// </remarks>
25+
public class MsSQLiteDriver : ReflectionBasedDriver
26+
{
27+
/// <summary>
28+
/// Initializes a new instance of <see cref="MsSQLiteDriver"/>.
29+
/// </summary>
30+
/// <exception cref="HibernateException">
31+
/// Thrown when the <c>Sqlite.NET</c> assembly can not be loaded.
32+
/// </exception>
33+
public MsSQLiteDriver() : base(
34+
"Microsoft.Data.Sqlite",
35+
"Microsoft.Data.Sqlite",
36+
"Microsoft.Data.Sqlite.SqliteConnection",
37+
"Microsoft.Data.Sqlite.SqliteCommand")
38+
{
39+
}
40+
41+
public override DbConnection CreateConnection()
42+
{
43+
var connection = base.CreateConnection();
44+
connection.StateChange += Connection_StateChange;
45+
return connection;
46+
}
47+
48+
private static void Connection_StateChange(object sender, StateChangeEventArgs e)
49+
{
50+
if ((e.OriginalState == ConnectionState.Broken || e.OriginalState == ConnectionState.Closed || e.OriginalState == ConnectionState.Connecting) &&
51+
e.CurrentState == ConnectionState.Open)
52+
{
53+
var connection = (DbConnection)sender;
54+
using (var command = connection.CreateCommand())
55+
{
56+
// Activated foreign keys if supported by SQLite. Unknown pragmas are ignored.
57+
command.CommandText = "PRAGMA foreign_keys = ON";
58+
command.ExecuteNonQuery();
59+
}
60+
}
61+
}
62+
63+
public override IResultSetsCommand GetResultSetsCommand(ISessionImplementor session)
64+
{
65+
return new BasicResultSetsCommand(session);
66+
}
67+
68+
public override bool UseNamedPrefixInSql => true;
69+
70+
public override bool UseNamedPrefixInParameter => true;
71+
72+
public override string NamedPrefix => "@";
73+
74+
public override bool SupportsMultipleOpenReaders => false;
75+
76+
public override bool SupportsMultipleQueries => true;
77+
78+
public override bool SupportsNullEnlistment => false;
79+
80+
public override bool HasDelayedDistributedTransactionCompletion => true;
81+
}
82+
}

src/Shared.msbuild

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,43 @@
11
<Project>
2-
2+
33
<PropertyGroup>
44
<PackageId>$(AssemblyName)</PackageId>
55
<Copyright>Copyright (c) James Gregory and contributors (Paul Batum, Hudson Akridge et al, Jorge Rodríguez Galán).</Copyright>
66
<Authors>James Gregory and contributors (Paul Batum, Hudson Akridge, Gleb Chermennov, Jorge Rodríguez Galán).</Authors>
77
<Company>James Gregory and contributors (Paul Batum, Hudson Akridge, Gleb Chermennov, Jorge Rodríguez Galán).</Company>
8-
<PackageLicenseUrl>https://github.com/jagregory/fluent-nhibernate/raw/master/LICENSE</PackageLicenseUrl>
8+
<PackageLicenseUrl>https://github.com/jagregory/fluent-nhibernate/raw/master/LICENSE</PackageLicenseUrl>
99
<PackageIconUrl>https://raw.githubusercontent.com/jagregory/fluent-nhibernate/master/docs/logo-nuget.png</PackageIconUrl>
1010
<RepositoryUrl>https://github.com/jagregory/fluent-nhibernate</RepositoryUrl>
1111
<RepositoryType>git</RepositoryType>
1212
<PackageTags>ORM;DAL;NHibernate;Fluent;Conventions</PackageTags>
1313
</PropertyGroup>
1414

1515
<PropertyGroup>
16-
<GenerateAssemblyTitleAttribute>false</GenerateAssemblyTitleAttribute>
17-
<GenerateAssemblyDescriptionAttribute>false</GenerateAssemblyDescriptionAttribute>
18-
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
19-
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
20-
<GenerateAssemblyVersionAttribute>false</GenerateAssemblyVersionAttribute>
21-
<GenerateAssemblyFileVersionAttribute>false</GenerateAssemblyFileVersionAttribute>
22-
<GenerateAssemblyInformationalVersionAttribute>false</GenerateAssemblyInformationalVersionAttribute>
23-
<GenerateAssemblyCopyrightAttribute>false</GenerateAssemblyCopyrightAttribute>
16+
<NetStandardImplicitPackageVersion Condition=" '$(TargetFramework)' == 'netcoreapp2.0' ">2.0.0</NetStandardImplicitPackageVersion>
2417
</PropertyGroup>
2518

26-
<PropertyGroup>
27-
<NetStandardImplicitPackageVersion Condition=" '$(TargetFramework)' == 'netcoreapp2.0' ">2.0.0</NetStandardImplicitPackageVersion>
19+
<!-- Framework constants -->
20+
<PropertyGroup Condition=" '$(TargetFramework)' == 'net461' ">
21+
<DefineConstants>$(DefineConstants);NETFX</DefineConstants>
2822
</PropertyGroup>
29-
3023
<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
3124
<DefineConstants>$(DefineConstants);NETCORE</DefineConstants>
3225
<DebugType>portable</DebugType>
3326
</PropertyGroup>
34-
3527
<PropertyGroup Condition=" '$(TargetFramework)' == 'netcoreapp2.0' ">
3628
<DefineConstants>$(DefineConstants);NETCORE</DefineConstants>
3729
<DebugType>portable</DebugType>
3830
</PropertyGroup>
39-
31+
32+
<PropertyGroup>
33+
<GenerateAssemblyTitleAttribute>false</GenerateAssemblyTitleAttribute>
34+
<GenerateAssemblyDescriptionAttribute>false</GenerateAssemblyDescriptionAttribute>
35+
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
36+
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
37+
<GenerateAssemblyVersionAttribute>false</GenerateAssemblyVersionAttribute>
38+
<GenerateAssemblyFileVersionAttribute>false</GenerateAssemblyFileVersionAttribute>
39+
<GenerateAssemblyInformationalVersionAttribute>false</GenerateAssemblyInformationalVersionAttribute>
40+
<GenerateAssemblyCopyrightAttribute>false</GenerateAssemblyCopyrightAttribute>
41+
</PropertyGroup>
42+
4043
</Project>

0 commit comments

Comments
 (0)