Class SQLScript

java.lang.Object
org.pgcodekeeper.core.script.SQLScript

public final class SQLScript extends Object
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 Details

    • SQLScript

      public SQLScript(ISettings settings)
      Creates a new SQL script builder with specified settings.
      Parameters:
      settings - the settings containing database type and formatting options
  • Method Details

    • addCommentStatement

      public void addCommentStatement(String comment)
      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

      public void addStatement(StringBuilder sb)
      Adds a statement from StringBuilder to the MID phase.
      Parameters:
      sb - the StringBuilder containing the SQL statement
    • addStatement

      public void addStatement(String sql)
      Adds a SQL statement to the MID phase with separator.
      Parameters:
      sql - the SQL statement to add
    • addStatement

      public void addStatement(String sql, SQLActionType actionType)
      Adds a SQL statement to specified execution phase with separator.
      Parameters:
      sql - the SQL statement to add
      actionType - the execution phase for this statement
    • addStatementWithoutSeparator

      public void addStatementWithoutSeparator(String sql)
      Adds a SQL statement to the MID phase without separator.
      Parameters:
      sql - the SQL statement to add
    • addStatementWithoutSeparator

      public void addStatementWithoutSeparator(String sql, SQLActionType actionType)
      Adds a SQL statement to specified execution phase without separator.
      Parameters:
      sql - the SQL statement to add
      actionType - the execution phase for this statement
    • getSQLWithSeparator

      public String getSQLWithSeparator(String sql, boolean needSeparator)
      Gets SQL statement with appropriate database-specific separator if needed. Uses ';' for PostgreSQL and ClickHouse, 'GO' for Microsoft SQL.
      Parameters:
      sql - the SQL statement
      needSeparator - whether separator should be added
      Returns:
      SQL statement with separator if needed
    • addStatement

      public void addStatement(String sql, SQLActionType actionType, boolean needSeparator)
      Adds a SQL statement to specified execution phase with optional separator.
      Parameters:
      sql - the SQL statement to add
      actionType - the execution phase for this statement
      needSeparator - whether to add database-specific separator
    • addAllStatements

      public void addAllStatements(SQLScript script)
      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

      public String 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

      public ISettings getSettings()
      Returns the settings used by this script.
      Returns:
      the settings instance