Skip to content

Commit

Permalink
feat(db configurations): add microsoft sqlite driver and cfg
Browse files Browse the repository at this point in the history
  • Loading branch information
jrgcubano committed Apr 17, 2018
1 parent 40149ae commit 07d329b
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 35 deletions.
39 changes: 19 additions & 20 deletions src/FluentNHibernate.Specs/FluentNHibernate.Specs.csproj
@@ -1,11 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net461;netcoreapp2.0</TargetFrameworks>
<TargetFrameworks>net461;netcoreapp2.0</TargetFrameworks>
<NoWarn>1591</NoWarn>
<PlatformTarget>AnyCpu</PlatformTarget>
<DebugType Condition="'$(TargetFramework)' != '' AND '$(TargetFramework)' != 'netcoreapp2.0'">Full</DebugType>
</PropertyGroup>
</PropertyGroup>

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

Expand All @@ -14,39 +14,38 @@
<ProjectReference Include="..\FluentNHibernate\FluentNHibernate.csproj" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\FluentNHibernate.Specs.ExternalFixtures\FluentNHibernate.Specs.ExternalFixtures.csproj" />
<ProjectReference Include="..\FluentNHibernate\FluentNHibernate.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
<PackageReference Include="NHibernate" Version="5.1.0" />
<PackageReference Include="System.Data.SqlClient" Version="4.4.3" />
<PackageReference Include="FluentAssertions" Version="4.19.4" />
<PackageReference Include="Machine.Specifications" Version="0.12.0" />
<PackageReference Include="Machine.Specifications.Runner.Console" Version="0.9.3" />
<PackageReference Include="Machine.Specifications.Runner.VisualStudio" Version="2.6.1" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net461'">
<Reference Include="System.Configuration" />
<Reference Include="System.Runtime.Remoting" />
<Reference Include="System.ServiceModel" />
<Reference Include="System.Transactions" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="System.Xml.Serialization" />
<PackageReference Include="NHibernate" Version="5.1.0" />
<PackageReference Include="FluentAssertions" Version="4.19.4" />
<PackageReference Include="Machine.Specifications" Version="0.12.0" />
<PackageReference Include="Machine.Specifications.Runner.Console" Version="0.9.3" />
<PackageReference Include="Machine.Specifications.Runner.VisualStudio" Version="2.5.2" />
<PackageReference Include="Mono.Cecil" Version="0.9.6.1" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp2.0'">
<PackageReference Include="NHibernate" Version="5.1.0" />
<PackageReference Include="FluentAssertions" Version="4.19.4" />
<PackageReference Include="Machine.Specifications" Version="0.12.0" />
<PackageReference Include="Machine.Specifications.Runner.Console" Version="0.9.3" />
<PackageReference Include="Machine.Specifications.Runner.VisualStudio" Version="2.5.2" />
</ItemGroup>



<ItemGroup>
<Compile Include="..\CommonAssemblyInfo.cs">
<Link>Properties\CommonAssemblyInfo.cs</Link>
</Compile>
</Compile>
</ItemGroup>

<ItemGroup>
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
</ItemGroup>

</Project>
29 changes: 29 additions & 0 deletions src/FluentNHibernate.Testing/Cfg/Db/MsSqliteConfigurationTester.cs
@@ -0,0 +1,29 @@
using FluentNHibernate.Cfg.Db;
using NUnit.Framework;

namespace FluentNHibernate.Testing.Cfg.Db
{
[TestFixture]
public class MsSqliteConfigurationTester
{
[Test]
public void should_set_up_default_query_substitutions()
{
new MsSqliteConfiguration().ToProperties()["query.substitutions"].ShouldEqual("true=1;false=0");
}

[Test]
public void in_memory_should_set_up_expected_connection_string()
{
new MsSqliteConfiguration().InMemory()
.ToProperties()["connection.connection_string"].ShouldEqual("Data Source=:memory:");
}

[Test]
public void using_file_should_set_up_expected_connection_string()
{
new MsSqliteConfiguration().UsingFile("foo")
.ToProperties()["connection.connection_string"].ShouldEqual("Data Source=foo");
}
}
}
35 changes: 35 additions & 0 deletions src/FluentNHibernate/Cfg/Db/MsSqliteConfiguration.cs
@@ -0,0 +1,35 @@
using FluentNHibernate.Driver;
using NHibernate.Dialect;

namespace FluentNHibernate.Cfg.Db
{
public class MsSqliteConfiguration : PersistenceConfiguration<MsSqliteConfiguration>
{
public static MsSqliteConfiguration Standard
{
get { return new MsSqliteConfiguration(); }
}

public MsSqliteConfiguration()
{
Driver<MsSQLiteDriver>();
Dialect<SQLiteDialect>();
Raw("query.substitutions", "true=1;false=0");
}

public MsSqliteConfiguration InMemory()
{
Raw("connection.release_mode", "on_close");

return ConnectionString(c => c
.Is("Data Source=:memory:"));
}

public MsSqliteConfiguration UsingFile(string fileName)
{
return ConnectionString(c => c
.Is(string.Format("Data Source={0}", fileName)));
}
}

}
82 changes: 82 additions & 0 deletions src/FluentNHibernate/Driver/MsSQLiteDriver.cs
@@ -0,0 +1,82 @@
using NHibernate;
using NHibernate.Driver;
using NHibernate.Engine;
using System.Data;
using System.Data.Common;

namespace FluentNHibernate.Driver
{
/// <summary>
/// NHibernate driver for the Microsoft.Data.Sqlite data provider for .NETStandard2.0.
/// </summary>
/// <remarks>
/// <para>
/// In order to use this driver you must have the Microsoft.Data.Sqlite.dll assembly available
/// for NHibernate to load.
/// </para>
/// <para>
/// You can get the Microsoft.Data.Sqlite.dll assembly from
/// <a href="https://github.com/aspnet/Microsoft.Data.Sqlite">https://github.com/aspnet/Microsoft.Data.Sqlite</a>
/// </para>
/// <para>
/// Please check <a href="https://github.com/aspnet/Microsoft.Data.Sqlite">https://github.com/aspnet/Microsoft.Data.Sqlite</a> for more information regarding SQLite.
/// </para>
/// </remarks>
public class MsSQLiteDriver : ReflectionBasedDriver
{
/// <summary>
/// Initializes a new instance of <see cref="MsSQLiteDriver"/>.
/// </summary>
/// <exception cref="HibernateException">
/// Thrown when the <c>Sqlite.NET</c> assembly can not be loaded.
/// </exception>
public MsSQLiteDriver() : base(
"Microsoft.Data.Sqlite",
"Microsoft.Data.Sqlite",
"Microsoft.Data.Sqlite.SqliteConnection",
"Microsoft.Data.Sqlite.SqliteCommand")
{
}

public override DbConnection CreateConnection()
{
var connection = base.CreateConnection();
connection.StateChange += Connection_StateChange;
return connection;
}

private static void Connection_StateChange(object sender, StateChangeEventArgs e)
{
if ((e.OriginalState == ConnectionState.Broken || e.OriginalState == ConnectionState.Closed || e.OriginalState == ConnectionState.Connecting) &&
e.CurrentState == ConnectionState.Open)
{
var connection = (DbConnection)sender;
using (var command = connection.CreateCommand())
{
// Activated foreign keys if supported by SQLite. Unknown pragmas are ignored.
command.CommandText = "PRAGMA foreign_keys = ON";
command.ExecuteNonQuery();
}
}
}

public override IResultSetsCommand GetResultSetsCommand(ISessionImplementor session)
{
return new BasicResultSetsCommand(session);
}

public override bool UseNamedPrefixInSql => true;

public override bool UseNamedPrefixInParameter => true;

public override string NamedPrefix => "@";

public override bool SupportsMultipleOpenReaders => false;

public override bool SupportsMultipleQueries => true;

public override bool SupportsNullEnlistment => false;

public override bool HasDelayedDistributedTransactionCompletion => true;
}
}
33 changes: 18 additions & 15 deletions src/Shared.msbuild
@@ -1,40 +1,43 @@
<Project>

<PropertyGroup>
<PackageId>$(AssemblyName)</PackageId>
<Copyright>Copyright (c) James Gregory and contributors (Paul Batum, Hudson Akridge et al, Jorge Rodríguez Galán).</Copyright>
<Authors>James Gregory and contributors (Paul Batum, Hudson Akridge, Gleb Chermennov, Jorge Rodríguez Galán).</Authors>
<Company>James Gregory and contributors (Paul Batum, Hudson Akridge, Gleb Chermennov, Jorge Rodríguez Galán).</Company>
<PackageLicenseUrl>https://github.com/jagregory/fluent-nhibernate/raw/master/LICENSE</PackageLicenseUrl>
<PackageLicenseUrl>https://github.com/jagregory/fluent-nhibernate/raw/master/LICENSE</PackageLicenseUrl>
<PackageIconUrl>https://raw.githubusercontent.com/jagregory/fluent-nhibernate/master/docs/logo-nuget.png</PackageIconUrl>
<RepositoryUrl>https://github.com/jagregory/fluent-nhibernate</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageTags>ORM;DAL;NHibernate;Fluent;Conventions</PackageTags>
</PropertyGroup>

<PropertyGroup>
<GenerateAssemblyTitleAttribute>false</GenerateAssemblyTitleAttribute>
<GenerateAssemblyDescriptionAttribute>false</GenerateAssemblyDescriptionAttribute>
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
<GenerateAssemblyVersionAttribute>false</GenerateAssemblyVersionAttribute>
<GenerateAssemblyFileVersionAttribute>false</GenerateAssemblyFileVersionAttribute>
<GenerateAssemblyInformationalVersionAttribute>false</GenerateAssemblyInformationalVersionAttribute>
<GenerateAssemblyCopyrightAttribute>false</GenerateAssemblyCopyrightAttribute>
<NetStandardImplicitPackageVersion Condition=" '$(TargetFramework)' == 'netcoreapp2.0' ">2.0.0</NetStandardImplicitPackageVersion>
</PropertyGroup>

<PropertyGroup>
<NetStandardImplicitPackageVersion Condition=" '$(TargetFramework)' == 'netcoreapp2.0' ">2.0.0</NetStandardImplicitPackageVersion>
<!-- Framework constants -->
<PropertyGroup Condition=" '$(TargetFramework)' == 'net461' ">
<DefineConstants>$(DefineConstants);NETFX</DefineConstants>
</PropertyGroup>

<PropertyGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
<DefineConstants>$(DefineConstants);NETCORE</DefineConstants>
<DebugType>portable</DebugType>
</PropertyGroup>

<PropertyGroup Condition=" '$(TargetFramework)' == 'netcoreapp2.0' ">
<DefineConstants>$(DefineConstants);NETCORE</DefineConstants>
<DebugType>portable</DebugType>
</PropertyGroup>


<PropertyGroup>
<GenerateAssemblyTitleAttribute>false</GenerateAssemblyTitleAttribute>
<GenerateAssemblyDescriptionAttribute>false</GenerateAssemblyDescriptionAttribute>
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
<GenerateAssemblyVersionAttribute>false</GenerateAssemblyVersionAttribute>
<GenerateAssemblyFileVersionAttribute>false</GenerateAssemblyFileVersionAttribute>
<GenerateAssemblyInformationalVersionAttribute>false</GenerateAssemblyInformationalVersionAttribute>
<GenerateAssemblyCopyrightAttribute>false</GenerateAssemblyCopyrightAttribute>
</PropertyGroup>

</Project>

0 comments on commit 07d329b

Please sign in to comment.