1
1
using System ;
2
+ using System . Collections . Generic ;
2
3
using System . Data ;
3
4
using System . Data . Common ;
4
5
using System . Text ;
@@ -18,6 +19,13 @@ namespace NHibernate.Dialect
18
19
/// </remarks>
19
20
public class SQLiteDialect : Dialect
20
21
{
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
+
21
29
/// <summary>
22
30
///
23
31
/// </summary>
@@ -94,8 +102,50 @@ protected virtual void RegisterFunctions()
94
102
95
103
// NH-3787: SQLite requires the cast in SQL too for not defaulting to string.
96
104
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
+ ConfigureBinaryGuid ( settings ) ;
118
+
119
+ // Re-register functions depending on settings.
120
+ RegisterFunctions ( ) ;
121
+ }
122
+
123
+ private void ConfigureBinaryGuid ( IDictionary < string , string > settings )
124
+ {
125
+ // We can use a SQLite specific setting to force it, but in common cases it
126
+ // should be detected automatically from the connection string below.
127
+ settings . TryGetValue ( Cfg . Environment . SqliteBinaryGuid , out var strBinaryGuid ) ;
128
+
129
+ if ( string . IsNullOrWhiteSpace ( strBinaryGuid ) )
130
+ {
131
+ string connectionString = Cfg . Environment . GetConfiguredConnectionString ( settings ) ;
132
+ if ( ! string . IsNullOrWhiteSpace ( connectionString ) )
133
+ {
134
+ var builder = new DbConnectionStringBuilder { ConnectionString = connectionString } ;
135
+
136
+ strBinaryGuid = GetConnectionStringProperty ( builder , "BinaryGuid" ) ;
137
+ }
138
+ }
139
+
140
+ // Note that "BinaryGuid=false" is supported by System.Data.SQLite but not Microsoft.Data.Sqlite.
141
+
142
+ _binaryGuid = string . IsNullOrWhiteSpace ( strBinaryGuid ) || bool . Parse ( strBinaryGuid ) ;
143
+ }
144
+
145
+ private string GetConnectionStringProperty ( DbConnectionStringBuilder builder , string propertyName )
146
+ {
147
+ builder . TryGetValue ( propertyName , out object propertyValue ) ;
148
+ return ( string ) propertyValue ;
99
149
}
100
150
101
151
#region private static readonly string[] DialectKeywords = { ... }
0 commit comments