Skip to content

Commit 4e81ee5

Browse files
committed
Verify support for H2 database aliases in SQL scripts
This commit introduces a test to demonstrate how to define an alias in an SQL script executed against an embedded H2 database. Issue: SPR-15896
1 parent 582014e commit 4e81ee5

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

spring-jdbc/src/test/java/org/springframework/jdbc/datasource/init/AbstractDatabasePopulatorTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -38,7 +38,7 @@
3838
*/
3939
public abstract class AbstractDatabasePopulatorTests extends AbstractDatabaseInitializationTests {
4040

41-
private final ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator();
41+
protected final ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator();
4242

4343

4444
@Test

spring-jdbc/src/test/java/org/springframework/jdbc/datasource/init/H2DatabasePopulatorTests.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,8 +16,12 @@
1616

1717
package org.springframework.jdbc.datasource.init;
1818

19+
import org.junit.Test;
1920
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
2021

22+
import static org.hamcrest.Matchers.*;
23+
import static org.junit.Assert.*;
24+
2125
/**
2226
* @author Sam Brannen
2327
* @since 4.0.3
@@ -28,4 +32,22 @@ protected EmbeddedDatabaseType getEmbeddedDatabaseType() {
2832
return EmbeddedDatabaseType.H2;
2933
}
3034

35+
/**
36+
* https://jira.spring.io/browse/SPR-15896
37+
*
38+
* @since 5.0
39+
*/
40+
@Test
41+
public void scriptWithH2Alias() throws Exception {
42+
databasePopulator.addScript(usersSchema());
43+
databasePopulator.addScript(resource("db-test-data-h2-alias.sql"));
44+
// Set statement separator to double newline so that ";" is not
45+
// considered a statement separator within the source code of the
46+
// aliased function 'REVERSE'.
47+
databasePopulator.setSeparator("\n\n");
48+
DatabasePopulatorUtils.execute(databasePopulator, db);
49+
String sql = "select REVERSE(first_name) from users where last_name='Brannen'";
50+
assertThat(jdbcTemplate.queryForObject(sql, String.class), equalTo("maS"));
51+
}
52+
3153
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
DROP ALIAS IF EXISTS REVERSE;
2+
3+
-- REVERSE function borrowed from http://www.h2database.com/html/grammar.html#create_alias
4+
CREATE ALIAS REVERSE AS $$
5+
String reverse(String s) {
6+
return new StringBuilder(s).reverse().toString();
7+
}
8+
$$;
9+
10+
INSERT INTO users(first_name, last_name) values('Sam', 'Brannen');

0 commit comments

Comments
 (0)