Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(db configurations): add microsoft sqlite driver and cfg
- Loading branch information
Showing
5 changed files
with
183 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/FluentNHibernate.Testing/Cfg/Db/MsSqliteConfigurationTester.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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))); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |