Skip to content

Commit 030b090

Browse files
committed
SQLiteDialect: Detect BinaryGuid setting and apply correct GUID-to-string expression.
Fixes GH-2110.
1 parent 72459f8 commit 030b090

File tree

1 file changed

+36
-2
lines changed

1 file changed

+36
-2
lines changed

src/NHibernate/Dialect/SQLiteDialect.cs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Data;
34
using System.Data.Common;
45
using System.Text;
@@ -18,6 +19,13 @@ namespace NHibernate.Dialect
1819
/// </remarks>
1920
public class SQLiteDialect : Dialect
2021
{
22+
/// <summary>
23+
/// The effective value of the BinaryGuid connection string parameter.
24+
/// The default value in SQLite is true.
25+
/// </summary>
26+
private bool _binaryGuid = true;
27+
28+
2129
/// <summary>
2230
///
2331
/// </summary>
@@ -94,8 +102,34 @@ protected virtual void RegisterFunctions()
94102

95103
// NH-3787: SQLite requires the cast in SQL too for not defaulting to string.
96104
RegisterFunction("transparentcast", new CastFunction());
97-
98-
RegisterFunction("strguid", new SQLFunctionTemplate(NHibernateUtil.String, "substr(hex(?1), 7, 2) || substr(hex(?1), 5, 2) || substr(hex(?1), 3, 2) || substr(hex(?1), 1, 2) || '-' || substr(hex(?1), 11, 2) || substr(hex(?1), 9, 2) || '-' || substr(hex(?1), 15, 2) || substr(hex(?1), 13, 2) || '-' || substr(hex(?1), 17, 4) || '-' || substr(hex(?1), 21) "));
105+
106+
if (_binaryGuid)
107+
RegisterFunction("strguid", new SQLFunctionTemplate(NHibernateUtil.String, "substr(hex(?1), 7, 2) || substr(hex(?1), 5, 2) || substr(hex(?1), 3, 2) || substr(hex(?1), 1, 2) || '-' || substr(hex(?1), 11, 2) || substr(hex(?1), 9, 2) || '-' || substr(hex(?1), 15, 2) || substr(hex(?1), 13, 2) || '-' || substr(hex(?1), 17, 4) || '-' || substr(hex(?1), 21) "));
108+
else
109+
RegisterFunction("strguid", new SQLFunctionTemplate(NHibernateUtil.String, "cast(?1 as char)"));
110+
}
111+
112+
113+
public override void Configure(IDictionary<string, string> settings)
114+
{
115+
base.Configure(settings);
116+
117+
if (settings.TryGetValue(Cfg.Environment.ConnectionString, out string connectionString))
118+
{
119+
var builder = new DbConnectionStringBuilder { ConnectionString = connectionString };
120+
121+
string strBinaryGuid = GetConnectionStringProperty(builder, "BinaryGuid");
122+
_binaryGuid = strBinaryGuid == null || bool.Parse(strBinaryGuid);
123+
}
124+
125+
// Re-register functions depending on settings.
126+
RegisterFunctions();
127+
}
128+
129+
private string GetConnectionStringProperty(DbConnectionStringBuilder builder, string propertyName)
130+
{
131+
builder.TryGetValue(propertyName, out object propertyValue);
132+
return (string) propertyValue;
99133
}
100134

101135
#region private static readonly string[] DialectKeywords = { ... }

0 commit comments

Comments
 (0)