-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
DialectFactoryImpl.java
166 lines (149 loc) · 5.76 KB
/
DialectFactoryImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.engine.jdbc.dialect.internal;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;
import org.hibernate.HibernateException;
import org.hibernate.annotations.common.util.StringHelper;
import org.hibernate.boot.registry.selector.spi.StrategySelectionException;
import org.hibernate.boot.registry.selector.spi.StrategySelector;
import org.hibernate.cfg.AvailableSettings;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.jdbc.dialect.spi.DialectFactory;
import org.hibernate.engine.jdbc.dialect.spi.DialectResolutionInfo;
import org.hibernate.engine.jdbc.dialect.spi.DialectResolutionInfoSource;
import org.hibernate.engine.jdbc.dialect.spi.DialectResolver;
import org.hibernate.internal.CoreLogging;
import org.hibernate.internal.CoreMessageLogger;
import org.hibernate.service.spi.ServiceRegistryAwareService;
import org.hibernate.service.spi.ServiceRegistryImplementor;
import static org.hibernate.internal.log.DeprecationLogger.DEPRECATION_LOGGER;
/**
* Standard implementation of the {@link DialectFactory} service.
*
* @author Steve Ebersole
*/
public class DialectFactoryImpl implements DialectFactory, ServiceRegistryAwareService {
private static final CoreMessageLogger LOG = CoreLogging.messageLogger( "SQL dialect" );
private StrategySelector strategySelector;
private DialectResolver dialectResolver;
@Override
public void injectServices(ServiceRegistryImplementor serviceRegistry) {
this.strategySelector = serviceRegistry.getService( StrategySelector.class );
this.dialectResolver = serviceRegistry.getService( DialectResolver.class );
}
/**
* Intended only for use from testing.
*
* @param dialectResolver The DialectResolver to use
*/
public void setDialectResolver(DialectResolver dialectResolver) {
this.dialectResolver = dialectResolver;
}
@Override
public Dialect buildDialect(Map configValues, DialectResolutionInfoSource resolutionInfoSource) throws HibernateException {
final Object dialectReference = configValues.get( AvailableSettings.DIALECT );
Dialect dialect = !isEmpty(dialectReference) ?
constructDialect(dialectReference, resolutionInfoSource) :
determineDialect(resolutionInfoSource);
logSelectedDialect( dialect );
return dialect;
}
private static void logSelectedDialect(Dialect dialect) {
LOG.usingDialect( dialect );
Class<? extends Dialect> dialectClass = dialect.getClass();
if ( dialectClass.isAnnotationPresent(Deprecated.class) ) {
Class<?> superDialectClass = dialectClass.getSuperclass();
if ( !superDialectClass.isAnnotationPresent(Deprecated.class)
&& !superDialectClass.equals(Dialect.class) ) {
DEPRECATION_LOGGER.deprecatedDialect( dialectClass.getSimpleName(), superDialectClass.getName() );
}
else {
DEPRECATION_LOGGER.deprecatedDialect( dialectClass.getSimpleName() );
}
}
}
@SuppressWarnings("SimplifiableIfStatement")
private boolean isEmpty(Object dialectReference) {
if ( dialectReference != null ) {
// the referenced value is not null
if ( dialectReference instanceof String ) {
// if it is a String, it might still be empty though...
return StringHelper.isEmpty( (String) dialectReference );
}
return false;
}
return true;
}
private Dialect constructDialect(Object dialectReference, DialectResolutionInfoSource resolutionInfoSource) {
try {
Dialect dialect = strategySelector.resolveStrategy(
Dialect.class,
dialectReference,
(Dialect) null,
(dialectClass) -> {
try {
try {
if (resolutionInfoSource != null) {
return dialectClass.getConstructor(DialectResolutionInfo.class).newInstance(
resolutionInfoSource.getDialectResolutionInfo()
);
}
}
catch (NoSuchMethodException nsme) {
}
return dialectClass.newInstance();
}
catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
throw new StrategySelectionException(
String.format( "Could not instantiate named dialect class [%s]", dialectClass.getName() ),
e
);
}
}
);
if ( dialect == null ) {
throw new HibernateException( "Unable to construct requested dialect [" + dialectReference + "]" );
}
return dialect;
}
catch (HibernateException e) {
throw e;
}
catch (Exception e) {
throw new HibernateException( "Unable to construct requested dialect [" + dialectReference + "]", e );
}
}
/**
* Determine the appropriate Dialect to use given the connection.
*
* @param resolutionInfoSource Access to DialectResolutionInfo used to resolve the Dialect.
*
* @return The appropriate dialect instance.
*
* @throws HibernateException No connection given or no resolver could make
* the determination from the given connection.
*/
private Dialect determineDialect(DialectResolutionInfoSource resolutionInfoSource) {
if ( resolutionInfoSource == null ) {
throw new HibernateException(
"Unable to determine Dialect without JDBC metadata "
+ "(please set 'javax.persistence.jdbc.url', 'hibernate.connection.url', or 'hibernate.dialect')"
);
}
final DialectResolutionInfo info = resolutionInfoSource.getDialectResolutionInfo();
final Dialect dialect = dialectResolver.resolveDialect( info );
if ( dialect == null ) {
throw new HibernateException(
"Unable to determine Dialect for " + info.getDatabaseName() + " "
+ info.getDatabaseMajorVersion() + "." + info.getDatabaseMinorVersion()
+ " (please set 'hibernate.dialect' or register a Dialect resolver)"
);
}
return dialect;
}
}