public static void executeDatabaseScript(DatabaseDelegate databaseDelegate, String scriptPath, String script) throws ScriptException { executeDatabaseScript(databaseDelegate, scriptPath, script, false, false, DEFAULT_COMMENT_PREFIX, DEFAULT_STATEMENT_SEPARATOR, DEFAULT_BLOCK_COMMENT_START_DELIMITER, DEFAULT_BLOCK_COMMENT_END_DELIMITER); }
/** * @see org.testcontainers.ext.ScriptUtils * @deprecated Needed only to keep binary compatibility for this internal API. Consider using database-agnostic ScriptUtils */ public static boolean containsSqlScriptDelimiters(String script, String delim) { return org.testcontainers.ext.ScriptUtils.containsSqlScriptDelimiters(script, delim); }
/** * @see org.testcontainers.ext.ScriptUtils * @deprecated Needed only to keep binary compatibility for this internal API. Consider using database-agnostic ScriptUtils */ public static void splitSqlScript(String resource, String script, String separator, String commentPrefix, String blockCommentStartDelimiter, String blockCommentEndDelimiter, List<String> statements) { org.testcontainers.ext.ScriptUtils.splitSqlScript(resource, script, separator, commentPrefix, blockCommentStartDelimiter, blockCommentEndDelimiter, statements); }
separator = DEFAULT_STATEMENT_SEPARATOR; if (!containsSqlScriptDelimiters(script, separator)) { separator = FALLBACK_STATEMENT_SEPARATOR; splitSqlScript(scriptPath, script, separator, commentPrefix, blockCommentStartDelimiter, blockCommentEndDelimiter, statements); throw new UncategorizedScriptException( "Failed to execute database script from resource [" + script + "]", ex);
/** * Load script from classpath and apply it to the given database * * @param databaseDelegate database delegate for script execution * @param initScriptPath the resource to load the init script from */ public static void runInitScript(DatabaseDelegate databaseDelegate, String initScriptPath) { try { URL resource = ScriptUtils.class.getClassLoader().getResource(initScriptPath); if (resource == null) { LOGGER.warn("Could not load classpath init script: {}", initScriptPath); throw new ScriptLoadException("Could not load classpath init script: " + initScriptPath + ". Resource not found."); } String scripts = IOUtils.toString(resource, StandardCharsets.UTF_8); executeDatabaseScript(databaseDelegate, initScriptPath, scripts); } catch (IOException e) { LOGGER.warn("Could not load classpath init script: {}", initScriptPath); throw new ScriptLoadException("Could not load classpath init script: " + initScriptPath, e); } catch (ScriptException e) { LOGGER.error("Error while executing init script: {}", initScriptPath, e); throw new UncategorizedScriptException("Error while executing init script: " + initScriptPath, e); } }
String blockCommentStartDelimiter, String blockCommentEndDelimiter, List<String> statements) { checkArgument(StringUtils.isNotEmpty(script), "script must not be null or empty"); checkArgument(separator != null, "separator must not be null"); checkArgument(StringUtils.isNotEmpty(commentPrefix), "commentPrefix must not be null or empty"); checkArgument(StringUtils.isNotEmpty(blockCommentStartDelimiter), "blockCommentStartDelimiter must not be null or empty"); checkArgument(StringUtils.isNotEmpty(blockCommentEndDelimiter), "blockCommentEndDelimiter must not be null or empty"); if (!inLiteral && containsSubstringAtOffset(script, commentPrefix, i)) { inLineComment = true; inLineComment = false; if (!inLiteral && containsSubstringAtOffset(script, blockCommentStartDelimiter, i)) { inBlockComment = true; if (!inLiteral && inBlockComment && containsSubstringAtOffset(script, blockCommentEndDelimiter, i)) { inBlockComment = false; if (!inLiteral && !inComment && containsSubstringAtOffset(script, "BEGIN", i)) { compoundStatementDepth++; if (!inLiteral && !inComment && containsSubstringAtOffset(script, "END", i)) { compoundStatementDepth--; throw new ScriptParseException(String.format("Missing block comment end delimiter [%s].", blockCommentEndDelimiter), resource);
@Override public void execute(String statement, String scriptPath, int lineNumber, boolean continueOnError, boolean ignoreFailedDrops) { try { ResultSet result = getConnection().execute(statement); if (result.wasApplied()) { log.debug("Statement {} was applied", statement); } else { throw new ScriptStatementFailedException(statement, lineNumber, scriptPath); } } catch (DriverException e) { throw new ScriptStatementFailedException(statement, lineNumber, scriptPath, e); } }
/** * Load init script content and apply it to the database if initScriptPath is set */ protected void runInitScriptIfRequired() { if (initScriptPath != null) { ScriptUtils.runInitScript(getDatabaseDelegate(), initScriptPath); } }
/** * Load init script content and apply it to the database if initScriptPath is set */ private void runInitScriptIfRequired() { if (initScriptPath != null) { try { URL resource = Thread.currentThread().getContextClassLoader().getResource(initScriptPath); if (resource == null) { logger().warn("Could not load classpath init script: {}", initScriptPath); throw new ScriptLoadException("Could not load classpath init script: " + initScriptPath + ". Resource not found."); } String cql = IOUtils.toString(resource, StandardCharsets.UTF_8); DatabaseDelegate databaseDelegate = getDatabaseDelegate(); ScriptUtils.executeDatabaseScript(databaseDelegate, initScriptPath, cql); } catch (IOException e) { logger().warn("Could not load classpath init script: {}", initScriptPath); throw new ScriptLoadException("Could not load classpath init script: " + initScriptPath, e); } catch (ScriptException e) { logger().error("Error while executing init script: {}", initScriptPath, e); throw new ScriptUtils.UncategorizedScriptException("Error while executing init script: " + initScriptPath, e); } } }
/** * @see org.testcontainers.ext.ScriptUtils * @deprecated Needed only to keep binary compatibility for this internal API. Consider using database-agnostic ScriptUtils */ public static void executeSqlScript(Connection connection, String scriptPath, String script) throws ScriptException { org.testcontainers.ext.ScriptUtils.executeDatabaseScript(new ContainerLessJdbcDelegate(connection), scriptPath, script); }
@Test public void testSplit() throws IOException { final String script = Resources.toString(Resources.getResource("splittable.sql"), Charsets.UTF_8); final List<String> statements = new ArrayList<>(); ScriptUtils.splitSqlScript("resourcename", script, ";", "--", "/*", "*/", statements); assertEquals(7, statements.size()); assertEquals("SELECT \"a /* string literal containing comment characters like -- here\"", statements.get(2)); assertEquals("SELECT \"a 'quoting' \\\"scenario ` involving BEGIN keyword\\\" here\"", statements.get(3)); assertEquals("SELECT * from `bar`", statements.get(4)); assertEquals("INSERT INTO bar (foo) VALUES ('hello world')", statements.get(6)); } }
/** * @see org.testcontainers.ext.ScriptUtils * @deprecated Needed only to keep binary compatibility for this internal API. Consider using database-agnostic ScriptUtils */ public static boolean containsSqlScriptDelimiters(String script, String delim) { return org.testcontainers.ext.ScriptUtils.containsSqlScriptDelimiters(script, delim); }
@Override public void execute(String statement, String scriptPath, int lineNumber, boolean continueOnError, boolean ignoreFailedDrops) { try { boolean rowsAffected = getConnection().execute(statement); log.debug("{} returned as updateCount for SQL: {}", rowsAffected, statement); } catch (SQLException ex) { boolean dropStatement = statement.trim().toLowerCase().startsWith("drop"); if (continueOnError || (dropStatement && ignoreFailedDrops)) { log.debug("Failed to execute SQL script statement at line {} of resource {}: {}", lineNumber, scriptPath, statement, ex); } else { throw new ScriptUtils.ScriptStatementFailedException(statement, lineNumber, scriptPath, ex); } } }
/** * Load init script content and apply it to the database if initScriptPath is set */ protected void runInitScriptIfRequired() { if (initScriptPath != null) { ScriptUtils.runInitScript(getDatabaseDelegate(), initScriptPath); } }
/** * @see org.testcontainers.ext.ScriptUtils * @deprecated Needed only to keep binary compatibility for this internal API. Consider using database-agnostic ScriptUtils */ public static void executeSqlScript(Connection connection, String scriptPath, String script, boolean continueOnError, boolean ignoreFailedDrops, String commentPrefix, String separator, String blockCommentStartDelimiter, String blockCommentEndDelimiter) throws ScriptException { org.testcontainers.ext.ScriptUtils.executeDatabaseScript(new ContainerLessJdbcDelegate(connection), scriptPath, script, continueOnError, ignoreFailedDrops, commentPrefix, separator, blockCommentStartDelimiter, blockCommentEndDelimiter); } }
/** * @see org.testcontainers.ext.ScriptUtils * @deprecated Needed only to keep binary compatibility for this internal API. Consider using database-agnostic ScriptUtils */ public static void splitSqlScript(String resource, String script, String separator, String commentPrefix, String blockCommentStartDelimiter, String blockCommentEndDelimiter, List<String> statements) { org.testcontainers.ext.ScriptUtils.splitSqlScript(resource, script, separator, commentPrefix, blockCommentStartDelimiter, blockCommentEndDelimiter, statements); }
/** * Run an init script from the classpath. * * @param connectionUrl {@link ConnectionUrl} instance representing JDBC Url with init script. * @param databaseDelegate database delegate to apply init scripts to the database * @throws SQLException on script or DB error */ private void runInitScriptIfRequired(final ConnectionUrl connectionUrl, DatabaseDelegate databaseDelegate) throws SQLException { if (connectionUrl.getInitScriptPath().isPresent()) { String initScriptPath = connectionUrl.getInitScriptPath().get(); try { URL resource = Thread.currentThread().getContextClassLoader().getResource(initScriptPath); if (resource == null) { LOGGER.warn("Could not load classpath init script: {}", initScriptPath); throw new SQLException("Could not load classpath init script: " + initScriptPath + ". Resource not found."); } String sql = IOUtils.toString(resource, StandardCharsets.UTF_8); ScriptUtils.executeDatabaseScript(databaseDelegate, initScriptPath, sql); } catch (IOException e) { LOGGER.warn("Could not load classpath init script: {}", initScriptPath); throw new SQLException("Could not load classpath init script: " + initScriptPath, e); } catch (ScriptException e) { LOGGER.error("Error while executing init script: {}", initScriptPath, e); throw new SQLException("Error while executing init script: " + initScriptPath, e); } } }
public static void executeDatabaseScript(DatabaseDelegate databaseDelegate, String scriptPath, String script) throws ScriptException { executeDatabaseScript(databaseDelegate, scriptPath, script, false, false, DEFAULT_COMMENT_PREFIX, DEFAULT_STATEMENT_SEPARATOR, DEFAULT_BLOCK_COMMENT_START_DELIMITER, DEFAULT_BLOCK_COMMENT_END_DELIMITER); }
/** * @see org.testcontainers.ext.ScriptUtils * @deprecated Needed only to keep binary compatibility for this internal API. Consider using database-agnostic ScriptUtils */ public static void executeSqlScript(Connection connection, String scriptPath, String script) throws ScriptException { org.testcontainers.ext.ScriptUtils.executeDatabaseScript(new ContainerLessJdbcDelegate(connection), scriptPath, script); }
/** * @see org.testcontainers.ext.ScriptUtils * @deprecated Needed only to keep binary compatibility for this internal API. Consider using database-agnostic ScriptUtils */ public static void executeSqlScript(Connection connection, String scriptPath, String script, boolean continueOnError, boolean ignoreFailedDrops, String commentPrefix, String separator, String blockCommentStartDelimiter, String blockCommentEndDelimiter) throws ScriptException { org.testcontainers.ext.ScriptUtils.executeDatabaseScript(new ContainerLessJdbcDelegate(connection), scriptPath, script, continueOnError, ignoreFailedDrops, commentPrefix, separator, blockCommentStartDelimiter, blockCommentEndDelimiter); } }