/** * Execute the given SQL script using default settings for statement * separators, comment delimiters, and exception handling flags. * <p>Statement separators and comments will be removed before executing * individual statements within the supplied script. * <p><strong>Warning</strong>: this method does <em>not</em> release the * provided {@link Connection}. * @param connection the JDBC connection to use to execute the script; already * configured and ready to use * @param resource the resource (potentially associated with a specific encoding) * to load the SQL script from * @throws ScriptException if an error occurred while executing the SQL script * @see #executeSqlScript(Connection, EncodedResource, boolean, boolean, String, String, String, String) * @see #DEFAULT_STATEMENT_SEPARATOR * @see #DEFAULT_COMMENT_PREFIX * @see #DEFAULT_BLOCK_COMMENT_START_DELIMITER * @see #DEFAULT_BLOCK_COMMENT_END_DELIMITER * @see org.springframework.jdbc.datasource.DataSourceUtils#getConnection * @see org.springframework.jdbc.datasource.DataSourceUtils#releaseConnection */ public static void executeSqlScript(Connection connection, EncodedResource resource) throws ScriptException { executeSqlScript(connection, resource, false, false, DEFAULT_COMMENT_PREFIX, DEFAULT_STATEMENT_SEPARATOR, DEFAULT_BLOCK_COMMENT_START_DELIMITER, DEFAULT_BLOCK_COMMENT_END_DELIMITER); }
/** * {@inheritDoc} * @see #execute(DataSource) */ @Override public void populate(Connection connection) throws ScriptException { Assert.notNull(connection, "Connection must not be null"); for (Resource script : this.scripts) { EncodedResource encodedScript = new EncodedResource(script, this.sqlScriptEncoding); ScriptUtils.executeSqlScript(connection, encodedScript, this.continueOnError, this.ignoreFailedDrops, this.commentPrefix, this.separator, this.blockCommentStartDelimiter, this.blockCommentEndDelimiter); } }
/** * Execute the given SQL script using default settings for statement * separators, comment delimiters, and exception handling flags. * <p>Statement separators and comments will be removed before executing * individual statements within the supplied script. * <p><strong>Warning</strong>: this method does <em>not</em> release the * provided {@link Connection}. * @param connection the JDBC connection to use to execute the script; already * configured and ready to use * @param resource the resource to load the SQL script from; encoded with the * current platform's default encoding * @throws ScriptException if an error occurred while executing the SQL script * @see #executeSqlScript(Connection, EncodedResource, boolean, boolean, String, String, String, String) * @see #DEFAULT_STATEMENT_SEPARATOR * @see #DEFAULT_COMMENT_PREFIX * @see #DEFAULT_BLOCK_COMMENT_START_DELIMITER * @see #DEFAULT_BLOCK_COMMENT_END_DELIMITER * @see org.springframework.jdbc.datasource.DataSourceUtils#getConnection * @see org.springframework.jdbc.datasource.DataSourceUtils#releaseConnection */ public static void executeSqlScript(Connection connection, Resource resource) throws ScriptException { executeSqlScript(connection, new EncodedResource(resource)); }
@Before public void setUpSchema() throws SQLException { executeSqlScript(db.getConnection(), usersSchema()); }
@Test public void executeSqlScriptContainingMultiLineComments() throws SQLException { executeSqlScript(db.getConnection(), resource("test-data-with-multi-line-comments.sql")); assertUsersDatabaseCreated("Hoeller", "Brannen"); }
/** * @since 4.2 */ @Test public void executeSqlScriptContainingSingleQuotesNestedInsideDoubleQuotes() throws SQLException { executeSqlScript(db.getConnection(), resource("users-data-with-single-quotes-nested-in-double-quotes.sql")); assertUsersDatabaseCreated("Hoeller", "Brannen"); }
@Override public void casServerStarted() throws SQLException { try(Connection connection = dataSource.getConnection()){ for (String sqlSciptPath : sqlScriptPaths) { ScriptUtils.executeSqlScript(connection, new ClassPathResource(sqlSciptPath)); } } }
public static void main(String[] args) throws Exception { String url = "jdbc:postgresql://localhost:5432/quartz"; Class.forName("org.postgresql.Driver"); Connection db = DriverManager.getConnection(url, "quartz", "quartz"); System.out.println("Initializing database and creating tables"); db.setAutoCommit(false); ScriptUtils.executeSqlScript(db, new FileSystemResource("tables_postgres.sql")); db.setAutoCommit(true); db.close(); System.out.println("Database initialized"); }
/** * {@inheritDoc} * @see #execute(DataSource) */ @Override public void populate(Connection connection) throws ScriptException { Assert.notNull(connection, "Connection must not be null"); for (Resource script : this.scripts) { EncodedResource encodedScript = new EncodedResource(script, this.sqlScriptEncoding); ScriptUtils.executeSqlScript(connection, encodedScript, this.continueOnError, this.ignoreFailedDrops, this.commentPrefix, this.separator, this.blockCommentStartDelimiter, this.blockCommentEndDelimiter); } }
private void createTables() throws Exception { final InputStream createTableResource = this.getClass().getResourceAsStream("/sql/" + dbVendor + "/createTables.sql"); try (Connection connection = getConnection()) { ScriptUtils.executeSqlScript(connection, new EncodedResource(new InputStreamResource(createTableResource)), false, false, DEFAULT_COMMENT_PREFIX, getDefaultStatementSeparator(), DEFAULT_BLOCK_COMMENT_START_DELIMITER, DEFAULT_BLOCK_COMMENT_END_DELIMITER); } }
private void dropTables() throws Exception { final InputStream dropTablesResource = this.getClass().getResourceAsStream("/sql/" + dbVendor + "/dropTables.sql"); try (Connection connection = getConnection()) { ScriptUtils.executeSqlScript(connection, new EncodedResource(new InputStreamResource(dropTablesResource)), true, true, DEFAULT_COMMENT_PREFIX, getDefaultStatementSeparator(), DEFAULT_BLOCK_COMMENT_START_DELIMITER, DEFAULT_BLOCK_COMMENT_END_DELIMITER); } }
@Test @Ignore public void test() { if (drop) { try { transactionTemplate.execute((TransactionCallback<Void>) transactionStatus -> { Session session = entityManager.unwrap(Session.class); session.doWork(connection -> { ScriptUtils.executeSqlScript(connection, new EncodedResource( new ClassPathResource( String.format("flyway/db/%1$s/drop/drop.sql", databaseType) ) ), true, true, ScriptUtils.DEFAULT_COMMENT_PREFIX, ScriptUtils.DEFAULT_BLOCK_COMMENT_START_DELIMITER, ScriptUtils.DEFAULT_BLOCK_COMMENT_END_DELIMITER, ScriptUtils.DEFAULT_COMMENT_PREFIX); }); return null; }); } catch (TransactionException e) { LOGGER.error("Failure", e); } } } }
} else { try { ScriptUtils.executeSqlScript(databaseService.getDataSource().getConnection(), configurationDao.getApplicationContext().getResource("classpath:data/pfTestdata.sql")); } catch (Exception e) {