Package org.pgcodekeeper.core.script
Class SQLScript
java.lang.Object
org.pgcodekeeper.core.script.SQLScript
SQL script builder for database migration operations.
Collects and organizes SQL statements by execution phases (PRE, BEGIN, MID, END, POST)
and generates properly formatted SQL scripts with database-specific separators.
-
Constructor Summary
ConstructorsConstructorDescriptionSQLScript(DiffSettings diffSettings, String separator) Creates a new SQL script builder with specified settings. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddAllStatements(SQLScript script) Merges all statements from another SQL script into this one.voidaddCommentStatement(String comment) Adds a comment statement to the script.voidaddStatement(String sql) Adds a SQL statement to the MID phase with separator.voidAdds a statement from StringBuilder to the MID phase.voidaddStatement(String sql, SQLActionType actionType) Adds a SQL statement to specified execution phase with separator.voidaddStatement(String sql, SQLActionType actionType, boolean needSeparator) Adds a SQL statement to specified execution phase with optional separator.voidAdds a SQL statement to the MID phase without separator.voidaddStatementWithoutSeparator(String sql, SQLActionType actionType) Adds a SQL statement to specified execution phase without separator.Returns the diffSettings used by this script.Generates the complete SQL script with all statements in execution order.Returns the settings used by this script.intgetSize()Returns the total number of statements in this script.getSQLWithSeparator(String sql, boolean needSeparator) Gets SQL statement with appropriate database-specific separator if needed.booleanisEmpty()Checks if this script contains any statements.
-
Constructor Details
-
SQLScript
Creates a new SQL script builder with specified settings.- Parameters:
diffSettings- the settings containing database type and formatting optionsseparator- - statement separator
-
-
Method Details
-
addCommentStatement
Adds a comment statement to the script. Comments are placed either in POST phase (if commentsToEnd is enabled) or MID phase.- Parameters:
comment- the comment text to add
-
addStatement
Adds a statement from StringBuilder to the MID phase.- Parameters:
sb- the StringBuilder containing the SQL statement
-
addStatement
Adds a SQL statement to the MID phase with separator.- Parameters:
sql- the SQL statement to add
-
addStatement
Adds a SQL statement to specified execution phase with separator.- Parameters:
sql- the SQL statement to addactionType- the execution phase for this statement
-
addStatementWithoutSeparator
Adds a SQL statement to the MID phase without separator.- Parameters:
sql- the SQL statement to add
-
addStatementWithoutSeparator
Adds a SQL statement to specified execution phase without separator.- Parameters:
sql- the SQL statement to addactionType- the execution phase for this statement
-
getSQLWithSeparator
Gets SQL statement with appropriate database-specific separator if needed. Uses ';' for PostgreSQL and ClickHouse, 'GO' for Microsoft SQL.- Parameters:
sql- the SQL statementneedSeparator- whether separator should be added- Returns:
- SQL statement with separator if needed
-
addStatement
Adds a SQL statement to specified execution phase with optional separator.- Parameters:
sql- the SQL statement to addactionType- the execution phase for this statementneedSeparator- whether to add database-specific separator
-
addAllStatements
Merges all statements from another SQL script into this one. Preserves execution phases and adds statements without additional separators.- Parameters:
script- the script to merge statements from
-
getFullScript
Generates the complete SQL script with all statements in execution order. Statements are ordered by phases: PRE, BEGIN, MID, END, POST.- Returns:
- complete SQL script as string with double newline delimiters
-
getSize
public int getSize()Returns the total number of statements in this script.- Returns:
- total statement count across all phases
-
isEmpty
public boolean isEmpty()Checks if this script contains any statements.- Returns:
- true if script has no statements, false otherwise
-
getSettings
Returns the settings used by this script.- Returns:
- the settings instance
-
getDiffSettings
Returns the diffSettings used by this script.- Returns:
- the diffSettings instance
-