Top Description Fields Constructors Methods
org.springframework.r2dbc.connection.init

public abstract Class ScriptUtils

extends Object
Class Inheritance
Imports
java.io.IOException, .InputStream, .InputStreamReader, .LineNumberReader, java.util.ArrayList, .List, java.util.concurrent.atomic.AtomicInteger, java.util.stream.Collectors, io.r2dbc.spi.Connection, .Result, org.apache.commons.logging.Log, .LogFactory, org.reactivestreams.Publisher, reactor.core.publisher.Flux, .Mono, org.springframework.core.io.Resource, org.springframework.core.io.buffer.DataBufferFactory, .DataBufferUtils, .DefaultDataBufferFactory, org.springframework.core.io.support.EncodedResource, org.springframework.lang.Nullable, org.springframework.util.Assert, .StringUtils

Generic utility methods for working with SQL scripts in conjunction with R2DBC.

Mainly for internal use within the framework.

Authors
Thomas Risberg, Sam Brannen, Juergen Hoeller, Keith Donald, Dave Syer, Chris Beams, Oliver Gierke, Chris Baldwin, Nicolas Debeissat, Phillip Webb, Mark Paluch
Since
5.3
See Also
org.springframework.jdbc.datasource.init.ScriptUtils

Field Summary

Modifier and TypeField and Description
public static final String
DEFAULT_BLOCK_COMMENT_END_DELIMITER

Default end delimiter for block comments within SQL scripts: "*/".

public static final String
DEFAULT_BLOCK_COMMENT_START_DELIMITER

Default start delimiter for block comments within SQL scripts: "/*".

public static final String[]
DEFAULT_COMMENT_PREFIXES

Default prefixes for single-line comments within SQL scripts: ["--"].

public static final String
DEFAULT_STATEMENT_SEPARATOR

Default statement separator within SQL scripts: ";".

public static final String
EOF_STATEMENT_SEPARATOR

End of file (EOF) SQL statement separator: "^^^ END OF SCRIPT ^^^".

public static final String
FALLBACK_STATEMENT_SEPARATOR

Fallback statement separator within SQL scripts: "\n".

private static final Log

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
private static void
pack-priv static boolean
containsStatementSeparator(EncodedResource
the resource from which the script was read, or null if unknown
resource
,
String
the SQL script to search within
script
,
String
the statement separator to search for
separator
,
String[]
the prefixes that identify single-line comments (typically "--")
commentPrefixes
,
String
the start block comment delimiter (typically "/*")
blockCommentStartDelimiter
,
String
the end block comment delimiter (typically "*/")
blockCommentEndDelimiter
)

Determine if the provided SQL script contains the specified statement separator.

public static Mono<Void>
executeSqlScript(Connection
the R2DBC connection to use to execute the script; already configured and ready to use
connection
,
Resource
the resource to load the SQL script from; encoded with the current platform's default encoding
resource
)

Execute the given SQL script using default settings for statement separators, comment delimiters, and exception handling flags.

public static Mono<Void>
executeSqlScript(Connection
the R2DBC connection to use to execute the script; already configured and ready to use
connection
,
EncodedResource
the resource (potentially associated with a specific encoding) to load the SQL script from
resource
)

Execute the given SQL script using default settings for statement separators, comment delimiters, and exception handling flags.

public static Mono<Void>
executeSqlScript(Connection
the R2DBC connection to use to execute the script; already configured and ready to use
connection
,
EncodedResource
the resource (potentially associated with a specific encoding) to load the SQL script from
resource
,
DataBufferFactory
the factory to create data buffers with
dataBufferFactory
,
boolean
whether to continue without throwing an exception in the event of an error
continueOnError
,
boolean
whether to continue in the event of specifically an error on a DROP statement
ignoreFailedDrops
,
String
the prefix that identifies single-line comments in the SQL script (typically "--")
commentPrefix
,
String
the script statement separator; defaults to ; if not specified and falls back to as a last resort; may be set to ^^^ END OF SCRIPT ^^^ to signal that the script contains a single statement without a separator
separator
,
String
the start block comment delimiter
blockCommentStartDelimiter
,
String
the end block comment delimiter
blockCommentEndDelimiter
)

Execute the given SQL script.

public static Mono<Void>
executeSqlScript(Connection
the R2DBC connection to use to execute the script; already configured and ready to use
connection
,
EncodedResource
the resource (potentially associated with a specific encoding) to load the SQL script from
resource
,
DataBufferFactory
the factory to create data buffers with
dataBufferFactory
,
boolean
whether to continue without throwing an exception in the event of an error
continueOnError
,
boolean
whether to continue in the event of specifically an error on a DROP statement
ignoreFailedDrops
,
String[]
the prefixes that identify single-line comments in the SQL script (typically "--")
commentPrefixes
,
String
the script statement separator; defaults to ; if not specified and falls back to as a last resort; may be set to ^^^ END OF SCRIPT ^^^ to signal that the script contains a single statement without a separator
separator
,
String
the start block comment delimiter
blockCommentStartDelimiter
,
String
the end block comment delimiter
blockCommentEndDelimiter
)

Execute the given SQL script.

pack-priv static Mono<String>

Returns:

a Mono of String containing the script lines that completes once the resource has been loaded
readScript
(EncodedResource
the EncodedResource containing the script to be processed
resource
,
DataBufferFactory
the factory to create data buffers with
dataBufferFactory
,
String
the statement separator in the SQL script (typically ";")
separator
)

Read a script from the provided resource, using the supplied statement separator, and build a String containing the lines.

private static String

Returns:

a String containing the script lines
readScript
(LineNumberReader
the LineNumberReader containing the script to be processed
lineNumberReader
,
String
the statement separator in the SQL script (typically ";")
separator
)

Read a script from the provided LineNumberReader and build a String containing the lines.

private static Publisher<? extends Void>
runStatement(String statement, Connection connection, EncodedResource resource, boolean continueOnError, boolean ignoreFailedDrops, AtomicInteger statementNumber)

pack-priv static List<String>

Returns:

a list of statements
splitSqlScript
(EncodedResource
the resource from which the script was read
resource
,
String
the SQL script
script
,
String
text separating each statement (typically a ';' or newline character)
separator
,
String[]
the prefixes that identify SQL line comments (typically "--")
commentPrefixes
,
String
the start block comment delimiter; never null or empty
blockCommentStartDelimiter
,
String
the end block comment delimiter; never null or empty
blockCommentEndDelimiter
)

Split an SQL script into separate statements delimited by the provided separator string and return a List containing each individual statement.

private static boolean
startsWithAny(String script, String[] prefixes, int offset)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

DEFAULT_BLOCK_COMMENT_END_DELIMITERback to summary
public static final String DEFAULT_BLOCK_COMMENT_END_DELIMITER

Default end delimiter for block comments within SQL scripts: "*/".

DEFAULT_BLOCK_COMMENT_START_DELIMITERback to summary
public static final String DEFAULT_BLOCK_COMMENT_START_DELIMITER

Default start delimiter for block comments within SQL scripts: "/*".

DEFAULT_COMMENT_PREFIXESback to summary
public static final String[] DEFAULT_COMMENT_PREFIXES

Default prefixes for single-line comments within SQL scripts: ["--"].

DEFAULT_STATEMENT_SEPARATORback to summary
public static final String DEFAULT_STATEMENT_SEPARATOR

Default statement separator within SQL scripts: ";".

EOF_STATEMENT_SEPARATORback to summary
public static final String EOF_STATEMENT_SEPARATOR

End of file (EOF) SQL statement separator: "^^^ END OF SCRIPT ^^^".

This value may be supplied as the separator to executeSqlScript(Connection, EncodedResource, DataBufferFactory, boolean, boolean, String[], String, String, String) to denote that an SQL script contains a single statement (potentially spanning multiple lines) with no explicit statement separator. Note that such a script should not actually contain this value; it is merely a virtual statement separator.

FALLBACK_STATEMENT_SEPARATORback to summary
public static final String FALLBACK_STATEMENT_SEPARATOR

Fallback statement separator within SQL scripts: "\n".

Used if neither a custom separator nor the DEFAULT_STATEMENT_SEPARATOR is present in a given script.

loggerback to summary
private static final Log logger

Constructor Detail

ScriptUtilsback to summary
public ScriptUtils()

Method Detail

appendSeparatorToScriptIfNecessaryback to summary
private static void appendSeparatorToScriptIfNecessary(StringBuilder scriptBuilder, String separator)
containsStatementSeparatorback to summary
pack-priv static boolean containsStatementSeparator(EncodedResource resource, String script, String separator, String[] commentPrefixes, String blockCommentStartDelimiter, String blockCommentEndDelimiter)

Determine if the provided SQL script contains the specified statement separator.

This method is intended to be used to find the string separating each SQL statement — for example, a ';' character.

Any occurrence of the separator within the script will be ignored if it is within a literal block of text enclosed in single quotes (') or double quotes ("), if it is escaped with a backslash (\), or if it is within a single-line comment or block comment.

Parameters
resource:EncodedResource

the resource from which the script was read, or null if unknown

script:String

the SQL script to search within

separator:String

the statement separator to search for

commentPrefixes:String[]

the prefixes that identify single-line comments (typically "--")

blockCommentStartDelimiter:String

the start block comment delimiter (typically "/*")

blockCommentEndDelimiter:String

the end block comment delimiter (typically "*/")

Since
5.3.8
executeSqlScriptback to summary
public static Mono<Void> executeSqlScript(Connection connection, Resource resource)

Execute the given SQL script using default settings for statement separators, comment delimiters, and exception handling flags.

Statement separators and comments will be removed before executing individual statements within the supplied script.

Warning

this method does not release the provided Connection.

Parameters
connection:Connection

the R2DBC connection to use to execute the script; already configured and ready to use

resource:Resource

the resource to load the SQL script from; encoded with the current platform's default encoding

Exceptions
ScriptException:
if an error occurred while executing the SQL script
See Also
executeSqlScript(Connection, EncodedResource, DataBufferFactory, boolean, boolean, String[], String, String, String), DEFAULT_STATEMENT_SEPARATOR, DEFAULT_COMMENT_PREFIXES, DEFAULT_BLOCK_COMMENT_START_DELIMITER, DEFAULT_BLOCK_COMMENT_END_DELIMITER, org.springframework.r2dbc.connection.ConnectionFactoryUtils#getConnection, org.springframework.r2dbc.connection.ConnectionFactoryUtils#releaseConnection
executeSqlScriptback to summary
public static Mono<Void> executeSqlScript(Connection connection, EncodedResource resource)

Execute the given SQL script using default settings for statement separators, comment delimiters, and exception handling flags.

Statement separators and comments will be removed before executing individual statements within the supplied script.

Warning

this method does not release the provided Connection.

Parameters
connection:Connection

the R2DBC connection to use to execute the script; already configured and ready to use

resource:EncodedResource

the resource (potentially associated with a specific encoding) to load the SQL script from

Exceptions
ScriptException:
if an error occurred while executing the SQL script
See Also
executeSqlScript(Connection, EncodedResource, DataBufferFactory, boolean, boolean, String[], String, String, String), DEFAULT_STATEMENT_SEPARATOR, DEFAULT_COMMENT_PREFIXES, DEFAULT_BLOCK_COMMENT_START_DELIMITER, DEFAULT_BLOCK_COMMENT_END_DELIMITER, org.springframework.r2dbc.connection.ConnectionFactoryUtils#getConnection, org.springframework.r2dbc.connection.ConnectionFactoryUtils#releaseConnection
executeSqlScriptback to summary
public static Mono<Void> executeSqlScript(Connection connection, EncodedResource resource, DataBufferFactory dataBufferFactory, boolean continueOnError, boolean ignoreFailedDrops, String commentPrefix, String separator, String blockCommentStartDelimiter, String blockCommentEndDelimiter)

Execute the given SQL script.

Statement separators and comments will be removed before executing individual statements within the supplied script.

Warning

this method does not release the provided Connection.

Parameters
connection:Connection

the R2DBC connection to use to execute the script; already configured and ready to use

resource:EncodedResource

the resource (potentially associated with a specific encoding) to load the SQL script from

dataBufferFactory:DataBufferFactory

the factory to create data buffers with

continueOnError:boolean

whether to continue without throwing an exception in the event of an error

ignoreFailedDrops:boolean

whether to continue in the event of specifically an error on a DROP statement

commentPrefix:String

the prefix that identifies single-line comments in the SQL script (typically "--")

separator:String

the script statement separator; defaults to ; if not specified and falls back to as a last resort; may be set to ^^^ END OF SCRIPT ^^^ to signal that the script contains a single statement without a separator

blockCommentStartDelimiter:String

the start block comment delimiter

blockCommentEndDelimiter:String

the end block comment delimiter

Exceptions
ScriptException:
if an error occurred while executing the SQL script
See Also
DEFAULT_STATEMENT_SEPARATOR, FALLBACK_STATEMENT_SEPARATOR, EOF_STATEMENT_SEPARATOR, org.springframework.r2dbc.connection.ConnectionFactoryUtils#getConnection, org.springframework.r2dbc.connection.ConnectionFactoryUtils#releaseConnection
executeSqlScriptback to summary
public static Mono<Void> executeSqlScript(Connection connection, EncodedResource resource, DataBufferFactory dataBufferFactory, boolean continueOnError, boolean ignoreFailedDrops, String[] commentPrefixes, String separator, String blockCommentStartDelimiter, String blockCommentEndDelimiter)

Execute the given SQL script.

Statement separators and comments will be removed before executing individual statements within the supplied script.

Warning

this method does not release the provided Connection.

Parameters
connection:Connection

the R2DBC connection to use to execute the script; already configured and ready to use

resource:EncodedResource

the resource (potentially associated with a specific encoding) to load the SQL script from

dataBufferFactory:DataBufferFactory

the factory to create data buffers with

continueOnError:boolean

whether to continue without throwing an exception in the event of an error

ignoreFailedDrops:boolean

whether to continue in the event of specifically an error on a DROP statement

commentPrefixes:String[]

the prefixes that identify single-line comments in the SQL script (typically "--")

separator:String

the script statement separator; defaults to ; if not specified and falls back to as a last resort; may be set to ^^^ END OF SCRIPT ^^^ to signal that the script contains a single statement without a separator

blockCommentStartDelimiter:String

the start block comment delimiter

blockCommentEndDelimiter:String

the end block comment delimiter

Exceptions
ScriptException:
if an error occurred while executing the SQL script
See Also
DEFAULT_STATEMENT_SEPARATOR, FALLBACK_STATEMENT_SEPARATOR, EOF_STATEMENT_SEPARATOR, org.springframework.r2dbc.connection.ConnectionFactoryUtils#getConnection, org.springframework.r2dbc.connection.ConnectionFactoryUtils#releaseConnection
readScriptback to summary
pack-priv static Mono<String> readScript(EncodedResource resource, DataBufferFactory dataBufferFactory, String separator)

Read a script from the provided resource, using the supplied statement separator, and build a String containing the lines.

Parameters
resource:EncodedResource

the EncodedResource containing the script to be processed

dataBufferFactory:DataBufferFactory

the factory to create data buffers with

separator:String

the statement separator in the SQL script (typically ";")

Returns:Mono<String>

a Mono of String containing the script lines that completes once the resource has been loaded

readScriptback to summary
private static String readScript(LineNumberReader lineNumberReader, String separator) throws IOException

Read a script from the provided LineNumberReader and build a String containing the lines.

Parameters
lineNumberReader:LineNumberReader

the LineNumberReader containing the script to be processed

separator:String

the statement separator in the SQL script (typically ";")

Returns:String

a String containing the script lines

Exceptions
IOException:
in case of I/O errors
runStatementback to summary
private static Publisher<? extends Void> runStatement(String statement, Connection connection, EncodedResource resource, boolean continueOnError, boolean ignoreFailedDrops, AtomicInteger statementNumber)
splitSqlScriptback to summary
pack-priv static List<String> splitSqlScript(EncodedResource resource, String script, String separator, String[] commentPrefixes, String blockCommentStartDelimiter, String blockCommentEndDelimiter)

Split an SQL script into separate statements delimited by the provided separator string and return a List containing each individual statement.

Within the script, the provided commentPrefixes will be honored: any text beginning with one of the comment prefixes and extending to the end of the line will be omitted from the output. Similarly, the provided blockCommentStartDelimiter and blockCommentEndDelimiter delimiters will be honored: any text enclosed in a block comment will be omitted from the output. In addition, multiple adjacent whitespace characters will be collapsed into a single space.

Parameters
resource:EncodedResource

the resource from which the script was read

script:String

the SQL script

separator:String

text separating each statement (typically a ';' or newline character)

commentPrefixes:String[]

the prefixes that identify SQL line comments (typically "--")

blockCommentStartDelimiter:String

the start block comment delimiter; never null or empty

blockCommentEndDelimiter:String

the end block comment delimiter; never null or empty

Returns:List<String>

a list of statements

Exceptions
ScriptException:
if an error occurred while splitting the SQL script
startsWithAnyback to summary
private static boolean startsWithAny(String script, String[] prefixes, int offset)