Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
efdf451e
Commit
efdf451e
authored
May 31, 2017
by
Stephane Nicoll
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Polish "Auto-detect jOOQ dialect"
Closes gh-9355
parent
1b4c5dff
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
243 additions
and
21 deletions
+243
-21
JooqProperties.java
...ringframework/boot/autoconfigure/jooq/JooqProperties.java
+3
-3
SqlDialectLookup.java
...ngframework/boot/autoconfigure/jooq/SqlDialectLookup.java
+3
-5
JooqPropertiesTest.java
...framework/boot/autoconfigure/jooq/JooqPropertiesTest.java
+125
-0
SqlDialectLookupTests.java
...mework/boot/autoconfigure/jooq/SqlDialectLookupTests.java
+105
-0
spring-boot-features.adoc
spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc
+7
-13
No files found.
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jooq/JooqProperties.java
View file @
efdf451e
...
...
@@ -49,14 +49,14 @@ public class JooqProperties {
/**
* Determine the {@link SQLDialect} to use based on this configuration and the primary
* {@link DataSource}.
* @param dataSource the
auto-configured
data source
* @return
{@code SQLDialect
}
* @param dataSource the data source
* @return
the {@code SQLDialect} to use for that {@link DataSource
}
*/
public
SQLDialect
determineSqlDialect
(
DataSource
dataSource
)
{
if
(
this
.
sqlDialect
!=
null
)
{
return
this
.
sqlDialect
;
}
return
S
QL
DialectLookup
.
getDialect
(
dataSource
);
return
S
ql
DialectLookup
.
getDialect
(
dataSource
);
}
}
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jooq/S
QL
DialectLookup.java
→
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jooq/S
ql
DialectLookup.java
View file @
efdf451e
...
...
@@ -33,13 +33,11 @@ import org.springframework.jdbc.support.MetaDataAccessException;
/**
* Utility to lookup well known {@link SQLDialect SQLDialects} from a {@link DataSource}.
*
* Note: This lookup only supports the SQL dialects that the open source edition of jOOQ supports.
*
* @author Michael Simons
*/
final
class
S
QL
DialectLookup
{
final
class
S
ql
DialectLookup
{
private
static
final
Log
logger
=
LogFactory
.
getLog
(
S
QL
DialectLookup
.
class
);
private
static
final
Log
logger
=
LogFactory
.
getLog
(
S
ql
DialectLookup
.
class
);
private
static
final
Map
<
DatabaseDriver
,
SQLDialect
>
LOOKUP
;
...
...
@@ -55,7 +53,7 @@ final class SQLDialectLookup {
LOOKUP
=
Collections
.
unmodifiableMap
(
map
);
}
private
S
QL
DialectLookup
()
{
private
S
ql
DialectLookup
()
{
}
/**
...
...
spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jooq/JooqPropertiesTest.java
0 → 100644
View file @
efdf451e
/*
* Copyright 2012-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org
.
springframework
.
boot
.
autoconfigure
.
jooq
;
import
java.sql.Connection
;
import
java.sql.DatabaseMetaData
;
import
java.sql.SQLException
;
import
javax.sql.DataSource
;
import
org.jooq.SQLDialect
;
import
org.junit.After
;
import
org.junit.Test
;
import
org.springframework.boot.context.properties.EnableConfigurationProperties
;
import
org.springframework.boot.test.util.TestPropertyValues
;
import
org.springframework.context.annotation.AnnotationConfigApplicationContext
;
import
org.springframework.context.annotation.Configuration
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
import
static
org
.
mockito
.
BDDMockito
.
given
;
import
static
org
.
mockito
.
Mockito
.
mock
;
import
static
org
.
mockito
.
Mockito
.
never
;
import
static
org
.
mockito
.
Mockito
.
verify
;
/**
* Tests for {@link JooqProperties}.
*
* @author Stephane Nicoll
*/
public
class
JooqPropertiesTest
{
private
AnnotationConfigApplicationContext
context
;
@After
public
void
close
()
{
if
(
this
.
context
!=
null
)
{
this
.
context
.
close
();
}
}
@Test
public
void
determineSqlDialectNoCheckIfDialectIsSet
()
throws
SQLException
{
JooqProperties
properties
=
load
(
"spring.jooq.sql-dialect=postgres"
);
DataSource
dataSource
=
mockStandaloneDataSource
();
SQLDialect
sqlDialect
=
properties
.
determineSqlDialect
(
dataSource
);
assertThat
(
sqlDialect
).
isEqualTo
(
SQLDialect
.
POSTGRES
);
verify
(
dataSource
,
never
()).
getConnection
();
}
@Test
public
void
determineSqlDialectWithKnownUrl
()
{
JooqProperties
properties
=
load
();
SQLDialect
sqlDialect
=
properties
.
determineSqlDialect
(
mockDataSource
(
"jdbc:h2:mem:testdb"
));
assertThat
(
sqlDialect
).
isEqualTo
(
SQLDialect
.
H2
);
}
@Test
public
void
determineSqlDialectWithKnownUrlAndUserConfig
()
{
JooqProperties
properties
=
load
(
"spring.jooq.sql-dialect=mysql"
);
SQLDialect
sqlDialect
=
properties
.
determineSqlDialect
(
mockDataSource
(
"jdbc:h2:mem:testdb"
));
assertThat
(
sqlDialect
).
isEqualTo
(
SQLDialect
.
MYSQL
);
}
@Test
public
void
determineSqlDialectWithUnknownUrl
()
{
JooqProperties
properties
=
load
();
SQLDialect
sqlDialect
=
properties
.
determineSqlDialect
(
mockDataSource
(
"jdbc:unknown://localhost"
));
assertThat
(
sqlDialect
).
isEqualTo
(
SQLDialect
.
DEFAULT
);
}
private
DataSource
mockStandaloneDataSource
()
throws
SQLException
{
DataSource
ds
=
mock
(
DataSource
.
class
);
given
(
ds
.
getConnection
()).
willThrow
(
SQLException
.
class
);
return
ds
;
}
private
DataSource
mockDataSource
(
String
jdbcUrl
)
{
DataSource
ds
=
mock
(
DataSource
.
class
);
try
{
DatabaseMetaData
metadata
=
mock
(
DatabaseMetaData
.
class
);
given
(
metadata
.
getURL
()).
willReturn
(
jdbcUrl
);
Connection
connection
=
mock
(
Connection
.
class
);
given
(
connection
.
getMetaData
()).
willReturn
(
metadata
);
given
(
ds
.
getConnection
()).
willReturn
(
connection
);
}
catch
(
SQLException
e
)
{
// Do nothing
}
return
ds
;
}
private
JooqProperties
load
(
String
...
environment
)
{
AnnotationConfigApplicationContext
ctx
=
new
AnnotationConfigApplicationContext
();
TestPropertyValues
.
of
(
environment
).
applyTo
(
ctx
);
ctx
.
register
(
TestConfiguration
.
class
);
ctx
.
refresh
();
this
.
context
=
ctx
;
return
this
.
context
.
getBean
(
JooqProperties
.
class
);
}
@Configuration
@EnableConfigurationProperties
(
JooqProperties
.
class
)
static
class
TestConfiguration
{
}
}
spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jooq/S
QL
DialectLookupTests.java
→
spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jooq/S
ql
DialectLookupTests.java
View file @
efdf451e
...
...
@@ -29,76 +29,77 @@ import static org.mockito.BDDMockito.given;
import
static
org
.
mockito
.
Mockito
.
mock
;
/**
* Tests for {@link S
QL
DialectLookup}.
* Tests for {@link S
ql
DialectLookup}.
*
* @author Michael Simons
* @author Stephane Nicoll
*/
public
class
S
QL
DialectLookupTests
{
public
class
S
ql
DialectLookupTests
{
@Test
public
void
get
Database
WhenDataSourceIsNullShouldReturnDefault
()
throws
Exception
{
assertThat
(
S
QL
DialectLookup
.
getDialect
(
null
)).
isEqualTo
(
SQLDialect
.
DEFAULT
);
public
void
get
SqlDialect
WhenDataSourceIsNullShouldReturnDefault
()
throws
Exception
{
assertThat
(
S
ql
DialectLookup
.
getDialect
(
null
)).
isEqualTo
(
SQLDialect
.
DEFAULT
);
}
@Test
public
void
get
Database
WhenDataSourceIsUnknownShouldReturnDefault
()
throws
Exception
{
testGet
Database
(
"jdbc:idontexist:"
,
SQLDialect
.
DEFAULT
);
public
void
get
SqlDialect
WhenDataSourceIsUnknownShouldReturnDefault
()
throws
Exception
{
testGet
SqlDialect
(
"jdbc:idontexist:"
,
SQLDialect
.
DEFAULT
);
}
@Test
public
void
get
Database
WhenDerbyShouldReturnDerby
()
throws
Exception
{
testGet
Database
(
"jdbc:derby:"
,
SQLDialect
.
DERBY
);
public
void
get
SqlDialect
WhenDerbyShouldReturnDerby
()
throws
Exception
{
testGet
SqlDialect
(
"jdbc:derby:"
,
SQLDialect
.
DERBY
);
}
@Test
public
void
get
Database
WhenH2ShouldReturnH2
()
throws
Exception
{
testGet
Database
(
"jdbc:h2:"
,
SQLDialect
.
H2
);
public
void
get
SqlDialect
WhenH2ShouldReturnH2
()
throws
Exception
{
testGet
SqlDialect
(
"jdbc:h2:"
,
SQLDialect
.
H2
);
}
@Test
public
void
get
Database
WhenHsqldbShouldReturnHsqldb
()
throws
Exception
{
testGet
Database
(
"jdbc:hsqldb:"
,
SQLDialect
.
HSQLDB
);
public
void
get
SqlDialect
WhenHsqldbShouldReturnHsqldb
()
throws
Exception
{
testGet
SqlDialect
(
"jdbc:hsqldb:"
,
SQLDialect
.
HSQLDB
);
}
@Test
public
void
get
Database
WhenMysqlShouldReturnMysql
()
throws
Exception
{
testGet
Database
(
"jdbc:mysql:"
,
SQLDialect
.
MYSQL
);
public
void
get
SqlDialect
WhenMysqlShouldReturnMysql
()
throws
Exception
{
testGet
SqlDialect
(
"jdbc:mysql:"
,
SQLDialect
.
MYSQL
);
}
@Test
public
void
get
Database
WhenOracleShouldReturnOracle
()
throws
Exception
{
testGet
Database
(
"jdbc:oracle:"
,
SQLDialect
.
DEFAULT
);
public
void
get
SqlDialect
WhenOracleShouldReturnOracle
()
throws
Exception
{
testGet
SqlDialect
(
"jdbc:oracle:"
,
SQLDialect
.
DEFAULT
);
}
@Test
public
void
get
Database
WhenPostgresShouldReturnPostgres
()
throws
Exception
{
testGet
Database
(
"jdbc:postgresql:"
,
SQLDialect
.
POSTGRES
);
public
void
get
SqlDialect
WhenPostgresShouldReturnPostgres
()
throws
Exception
{
testGet
SqlDialect
(
"jdbc:postgresql:"
,
SQLDialect
.
POSTGRES
);
}
@Test
public
void
get
Database
WhenSqlserverShouldReturnSqlserver
()
throws
Exception
{
testGet
Database
(
"jdbc:sqlserver:"
,
SQLDialect
.
DEFAULT
);
public
void
get
SqlDialect
WhenSqlserverShouldReturnSqlserver
()
throws
Exception
{
testGet
SqlDialect
(
"jdbc:sqlserver:"
,
SQLDialect
.
DEFAULT
);
}
@Test
public
void
get
Database
WhenDb2ShouldReturnDb2
()
throws
Exception
{
testGet
Database
(
"jdbc:db2:"
,
SQLDialect
.
DEFAULT
);
public
void
get
SqlDialect
WhenDb2ShouldReturnDb2
()
throws
Exception
{
testGet
SqlDialect
(
"jdbc:db2:"
,
SQLDialect
.
DEFAULT
);
}
@Test
public
void
get
Database
WhenInformixShouldReturnInformix
()
throws
Exception
{
testGet
Database
(
"jdbc:informix-sqli:"
,
SQLDialect
.
DEFAULT
);
public
void
get
SqlDialect
WhenInformixShouldReturnInformix
()
throws
Exception
{
testGet
SqlDialect
(
"jdbc:informix-sqli:"
,
SQLDialect
.
DEFAULT
);
}
private
void
testGet
Database
(
String
url
,
SQLDialect
expected
)
throws
Exception
{
private
void
testGet
SqlDialect
(
String
url
,
SQLDialect
expected
)
throws
Exception
{
DataSource
dataSource
=
mock
(
DataSource
.
class
);
Connection
connection
=
mock
(
Connection
.
class
);
DatabaseMetaData
metaData
=
mock
(
DatabaseMetaData
.
class
);
given
(
dataSource
.
getConnection
()).
willReturn
(
connection
);
given
(
connection
.
getMetaData
()).
willReturn
(
metaData
);
given
(
metaData
.
getURL
()).
willReturn
(
url
);
SQLDialect
s
QLDialect
=
SQL
DialectLookup
.
getDialect
(
dataSource
);
assertThat
(
s
QL
Dialect
).
isEqualTo
(
expected
);
SQLDialect
s
qlDialect
=
Sql
DialectLookup
.
getDialect
(
dataSource
);
assertThat
(
s
ql
Dialect
).
isEqualTo
(
expected
);
}
}
spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc
View file @
efdf451e
...
...
@@ -3213,23 +3213,17 @@ You can then use the `DSLContext` to construct your queries:
==== Customizing jOOQ
Spring Boot tries to determine the best SQL dialect for your datasource, but you can customize
the dialect used by jOOQ by setting `spring.jooq.sql-dialect` in your `application.properties`.
Spring Boot uses `SQLDialect.DEFAULT` if it cannot determine the type of your database.
==== jOOQ SQL dialect
Spring Boot determines the SQL dialect to use for your datasource unless the
`spring.jooq.sql-dialect` property has been configured. If the dialect couldn't be
detected, `DEFAULT` is used.
Spring Boot can only auto-configure dialects supported by the open source version of jOOQ. Dialects
supported by the commercial edition only have to be configured manually. For example, to specify
Oracle you would add:
NOTE: Spring Boot can only auto-configure dialects supported by the open source version of
jOOQ.
[source,properties,indent=0]
----
spring.jooq.sql-dialect=ORACLE12C
----
Refer to `org.jooq.SQLDialect` for all valid dialects.
==== Customizing jOOQ
More advanced customizations can be achieved by defining your own `@Bean` definitions
which will be used when the jOOQ `Configuration` is created. You can define beans for
the following jOOQ Types:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment