From 2ffa4725cdd24d7feeb1e5d16cdd82316488fcdf Mon Sep 17 00:00:00 2001 From: Thomas Risberg Date: Fri, 3 Feb 2012 16:41:57 -0500 Subject: [PATCH] Allow SELECT statements in ResourceDatabasePopulator ResourceDatabasePopulator is a component that underlies the database initialization support within Spring's jdbc: namespace, e.g.: Prior to this commit, ResourceDatabasePopulator#executeSqlScript's use of Statement#executeUpdate(sql) precluded the possibility of SELECT statements because returning a result is not permitted by this method and results in an exception being thrown. Whether this behavior is a function of the JDBC specification or an idiosyncracy of certain implementations does not matter as the issue can be worked around entirely. This commit eliminates use of #executeUpdate(sql) in favor of #execute(sql) followed by a call to #getUpdateCount, effectively allowing any kind of SQL statement to be executed during database initialization. Issue: SPR-8932 --- .../init/ResourceDatabasePopulator.java | 5 +++-- .../init/DatabasePopulatorTests.java | 21 +++++++++++++++---- .../datasource/init/db-test-data-select.sql | 3 +++ 3 files changed, 23 insertions(+), 6 deletions(-) create mode 100644 org.springframework.jdbc/src/test/resources/org/springframework/jdbc/datasource/init/db-test-data-select.sql diff --git a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/init/ResourceDatabasePopulator.java b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/init/ResourceDatabasePopulator.java index 3da2b77fd7..b33a9d008a 100644 --- a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/init/ResourceDatabasePopulator.java +++ b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/init/ResourceDatabasePopulator.java @@ -181,9 +181,10 @@ public class ResourceDatabasePopulator implements DatabasePopulator { for (String statement : statements) { lineNumber++; try { - int rowsAffected = stmt.executeUpdate(statement); + stmt.execute(statement); + int rowsAffected = stmt.getUpdateCount(); if (logger.isDebugEnabled()) { - logger.debug(rowsAffected + " rows affected by SQL: " + statement); + logger.debug(rowsAffected + " returned as updateCount for SQL: " + statement); } } catch (SQLException ex) { diff --git a/org.springframework.jdbc/src/test/java/org/springframework/jdbc/datasource/init/DatabasePopulatorTests.java b/org.springframework.jdbc/src/test/java/org/springframework/jdbc/datasource/init/DatabasePopulatorTests.java index 8154fa526c..04d59698cc 100644 --- a/org.springframework.jdbc/src/test/java/org/springframework/jdbc/datasource/init/DatabasePopulatorTests.java +++ b/org.springframework.jdbc/src/test/java/org/springframework/jdbc/datasource/init/DatabasePopulatorTests.java @@ -20,8 +20,6 @@ import static org.junit.Assert.assertEquals; import java.sql.Connection; -import javax.sql.DataSource; - import org.junit.After; import org.junit.Test; import org.springframework.core.io.ClassRelativeResourceLoader; @@ -49,7 +47,7 @@ public class DatabasePopulatorTests { assertEquals(name, jdbcTemplate.queryForObject("select NAME from T_TEST", String.class)); } - private void assertUsersDatabaseCreated(DataSource db) { + private void assertUsersDatabaseCreated() { assertEquals("Sam", jdbcTemplate.queryForObject("select first_name from users where last_name = 'Brannen'", String.class)); } @@ -191,7 +189,22 @@ public class DatabasePopulatorTests { connection.close(); } - assertUsersDatabaseCreated(db); + assertUsersDatabaseCreated(); + } + + @Test + public void testBuildWithSelectStatements() throws Exception { + databasePopulator.addScript(resourceLoader.getResource("db-schema.sql")); + databasePopulator.addScript(resourceLoader.getResource("db-test-data-select.sql")); + Connection connection = db.getConnection(); + try { + databasePopulator.populate(connection); + } finally { + connection.close(); + } + + assertEquals(1, jdbcTemplate.queryForInt("select COUNT(NAME) from T_TEST where NAME='Keith'")); + assertEquals(1, jdbcTemplate.queryForInt("select COUNT(NAME) from T_TEST where NAME='Dave'")); } } diff --git a/org.springframework.jdbc/src/test/resources/org/springframework/jdbc/datasource/init/db-test-data-select.sql b/org.springframework.jdbc/src/test/resources/org/springframework/jdbc/datasource/init/db-test-data-select.sql new file mode 100644 index 0000000000..a000cb1b96 --- /dev/null +++ b/org.springframework.jdbc/src/test/resources/org/springframework/jdbc/datasource/init/db-test-data-select.sql @@ -0,0 +1,3 @@ +insert into T_TEST (NAME) values ('Keith'); +insert into T_TEST (NAME) values ('Dave'); +select NAME from T_TEST where NAME = 'Keith';