Support sqlScriptEncoding in schema.sql
Fixes gh-1165, fixes gh-1164
This commit is contained in:
parent
7e24c8499a
commit
9febd4a4cb
|
|
@ -135,6 +135,7 @@ class DataSourceInitializer implements ApplicationListener<DataSourceInitialized
|
|||
ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
|
||||
populator.setContinueOnError(this.properties.isContinueOnError());
|
||||
populator.setSeparator(this.properties.getSeparator());
|
||||
populator.setSqlScriptEncoding(this.properties.getSqlScriptEncoding());
|
||||
for (Resource resource : resources) {
|
||||
populator.addScript(resource);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -56,6 +56,8 @@ public class DataSourceProperties implements BeanClassLoaderAware, InitializingB
|
|||
|
||||
private String separator = ";";
|
||||
|
||||
private String sqlScriptEncoding;
|
||||
|
||||
private EmbeddedDatabaseConnection embeddedDatabaseConnection = EmbeddedDatabaseConnection.NONE;
|
||||
|
||||
private DriverClassNameProvider driverClassNameProvider = new DriverClassNameProvider();
|
||||
|
|
@ -196,6 +198,14 @@ public class DataSourceProperties implements BeanClassLoaderAware, InitializingB
|
|||
this.separator = separator;
|
||||
}
|
||||
|
||||
public String getSqlScriptEncoding() {
|
||||
return sqlScriptEncoding;
|
||||
}
|
||||
|
||||
public void setSqlScriptEncoding(String sqlScriptEncoding) {
|
||||
this.sqlScriptEncoding = sqlScriptEncoding;
|
||||
}
|
||||
|
||||
public ClassLoader getClassLoader() {
|
||||
return this.classLoader;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -143,6 +143,33 @@ public class DataSourceInitializerTests {
|
|||
template.queryForObject("SELECT COUNT(*) from SPAM", Integer.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDataSourceInitializedWithExplicitSqlScriptEncoding() throws Exception {
|
||||
this.context.register(DataSourceAutoConfiguration.class,
|
||||
PropertyPlaceholderAutoConfiguration.class);
|
||||
EnvironmentTestUtils.addEnvironment(
|
||||
this.context,
|
||||
"spring.datasource.initialize:true",
|
||||
"spring.datasource.sqlScriptEncoding:UTF-8",
|
||||
"spring.datasource.schema:"
|
||||
+ ClassUtils.addResourcePathToPackagePath(getClass(),
|
||||
"encoding-schema.sql"),
|
||||
"spring.datasource.data:"
|
||||
+ ClassUtils.addResourcePathToPackagePath(getClass(),
|
||||
"encoding-data.sql"));
|
||||
this.context.refresh();
|
||||
DataSource dataSource = this.context.getBean(DataSource.class);
|
||||
assertTrue(dataSource instanceof org.apache.tomcat.jdbc.pool.DataSource);
|
||||
assertNotNull(dataSource);
|
||||
JdbcOperations template = new JdbcTemplate(dataSource);
|
||||
assertEquals(new Integer(2),
|
||||
template.queryForObject("SELECT COUNT(*) from BAR", Integer.class));
|
||||
assertEquals("bar",
|
||||
template.queryForObject("SELECT name from BAR WHERE id=1", String.class));
|
||||
assertEquals("ばー",
|
||||
template.queryForObject("SELECT name from BAR WHERE id=2", String.class));
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableConfigurationProperties
|
||||
protected static class TwoDataSources {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,2 @@
|
|||
INSERT INTO BAR(id, name) VALUES (1, 'bar');
|
||||
INSERT INTO BAR(id, name) VALUES (2, 'ばー');
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
CREATE TABLE BAR (
|
||||
id INTEGER PRIMARY KEY,
|
||||
name VARCHAR(30),
|
||||
);
|
||||
|
|
@ -158,6 +158,7 @@ content into your application; rather pick only the properties that you need.
|
|||
spring.datasource.initialize=true # populate using data.sql
|
||||
spring.datasource.schema= # a schema (DDL) script resource reference
|
||||
spring.datasource.data= # a data (DML) script resource reference
|
||||
spring.datasource.sqlScriptEncoding= # a charset for reading SQL scripts
|
||||
spring.datasource.platform= # the platform to use in the schema resource (schema-${platform}.sql)
|
||||
spring.datasource.continueOnError=false # continue even if can't be initialized
|
||||
spring.datasource.separator=; # statement separator in SQL initialization scripts
|
||||
|
|
|
|||
Loading…
Reference in New Issue