1
+ using System ;
1
2
using NHibernate . Cfg ;
2
3
using NHibernate . Engine ;
3
4
using NHibernate . Mapping ;
4
5
using System . Collections . Generic ;
6
+ using NHibernate . Connection ;
5
7
6
8
namespace NHibernate . Tool . hbm2ddl
7
9
{
8
10
// Candidate to be exstensions of ISessionFactory and Configuration
9
11
public static class SchemaMetadataUpdater
10
12
{
11
- public static void Update ( ISessionFactory sessionFactory )
13
+ public static void Update ( ISessionFactoryImplementor sessionFactory )
12
14
{
13
- var factory = ( ISessionFactoryImplementor ) sessionFactory ;
14
- var dialect = factory . Dialect ;
15
- var connectionHelper = new SuppliedConnectionProviderConnectionHelper ( factory . ConnectionProvider ) ;
16
- factory . Dialect . Keywords . UnionWith ( GetReservedWords ( dialect , connectionHelper ) ) ;
15
+ var reservedWords = GetReservedWords ( sessionFactory . ConnectionProvider , sessionFactory . Dialect ) ;
16
+ sessionFactory . Dialect . Keywords . UnionWith ( reservedWords ) ;
17
17
}
18
18
19
+ public static void Update ( Configuration configuration , Dialect . Dialect dialect )
20
+ {
21
+ dialect . Keywords . UnionWith ( GetReservedWords ( configuration , dialect ) ) ;
22
+ }
23
+
24
+ [ Obsolete ( "Use the overload that passes dialect so keywords will be updated and persisted before auto-quoting" ) ]
19
25
public static void QuoteTableAndColumns ( Configuration configuration )
20
26
{
21
- ISet < string > reservedDb = GetReservedWords ( configuration . GetDerivedProperties ( ) ) ;
27
+ // Instantiates a new instance of the dialect so doesn't benefit from the Update call.
28
+ var dialect = Dialect . Dialect . GetDialect ( configuration . GetDerivedProperties ( ) ) ;
29
+ Update ( configuration , dialect ) ;
30
+ QuoteTableAndColumns ( configuration , dialect ) ;
31
+ }
32
+
33
+ public static void QuoteTableAndColumns ( Configuration configuration , Dialect . Dialect dialect )
34
+ {
35
+ ISet < string > reservedDb = dialect . Keywords ;
36
+
22
37
foreach ( var cm in configuration . ClassMappings )
23
38
{
24
39
QuoteTable ( cm . Table , reservedDb ) ;
@@ -29,30 +44,43 @@ public static void QuoteTableAndColumns(Configuration configuration)
29
44
}
30
45
}
31
46
32
- private static ISet < string > GetReservedWords ( IDictionary < string , string > cfgProperties )
47
+ private static ISet < string > GetReservedWords ( Configuration configuration , Dialect . Dialect dialect )
33
48
{
34
- var dialect = Dialect . Dialect . GetDialect ( cfgProperties ) ;
35
- var connectionHelper = new ManagedProviderConnectionHelper ( cfgProperties ) ;
36
- return GetReservedWords ( dialect , connectionHelper ) ;
49
+ IConnectionHelper connectionHelper = new ManagedProviderConnectionHelper ( configuration . GetDerivedProperties ( ) ) ;
50
+ connectionHelper . Prepare ( ) ;
51
+ try
52
+ {
53
+ return GetReservedWords ( dialect , connectionHelper ) ;
54
+ }
55
+ finally
56
+ {
57
+ connectionHelper . Release ( ) ;
58
+ }
37
59
}
38
60
39
- private static ISet < string > GetReservedWords ( Dialect . Dialect dialect , IConnectionHelper connectionHelper )
61
+ private static ISet < string > GetReservedWords ( IConnectionProvider connectionProvider , Dialect . Dialect dialect )
40
62
{
41
- ISet < string > reservedDb = new HashSet < string > ( ) ;
63
+ IConnectionHelper connectionHelper = new SuppliedConnectionProviderConnectionHelper ( connectionProvider ) ;
42
64
connectionHelper . Prepare ( ) ;
43
65
try
44
66
{
45
- var metaData = dialect . GetDataBaseSchema ( connectionHelper . Connection ) ;
46
- foreach ( var rw in metaData . GetReservedWords ( ) )
47
- {
48
- reservedDb . Add ( rw . ToLowerInvariant ( ) ) ;
49
- }
67
+ return GetReservedWords ( dialect , connectionHelper ) ;
50
68
}
51
69
finally
52
70
{
53
71
connectionHelper . Release ( ) ;
54
72
}
55
- return reservedDb ;
73
+ }
74
+
75
+ private static ISet < string > GetReservedWords ( Dialect . Dialect dialect , IConnectionHelper connectionHelper )
76
+ {
77
+ ISet < string > reservedWords = new HashSet < string > ( dialect . Keywords ) ;
78
+ var metaData = dialect . GetDataBaseSchema ( connectionHelper . Connection ) ;
79
+ foreach ( var rw in metaData . GetReservedWords ( ) )
80
+ {
81
+ reservedWords . Add ( rw . ToLowerInvariant ( ) ) ;
82
+ }
83
+ return reservedWords ;
56
84
}
57
85
58
86
private static void QuoteTable ( Table table , ICollection < string > reservedDb )
0 commit comments