Class PgStatement

java.lang.Object
org.pgcodekeeper.core.schema.PgStatement
All Implemented Interfaces:
IHashable, IStatement
Direct Known Subclasses:
AbstractColumn, AbstractConstraint, AbstractDatabase, AbstractFunction, AbstractIndex, AbstractPolicy, AbstractSchema, AbstractSequence, AbstractStatistics, AbstractTrigger, AbstractType, ChDictionary, ChFunction, ChRole, ChUser, MsAssembly, MsRole, MsUser, PgCast, PgCollation, PgDomain, PgEventTrigger, PgExtension, PgForeignDataWrapper, PgFtsConfiguration, PgFtsDictionary, PgFtsParser, PgFtsTemplate, PgOperator, PgRule, PgServer, PgStatementContainer, PgUserMapping

public abstract class PgStatement extends Object implements IStatement, IHashable
Abstract base class for all database statements and objects. Provides common functionality including naming, ownership, privileges, dependencies, and metadata management. All changes to hashed fields of extending classes must be followed by a resetHash() call.
Author:
Alexander Levsha
  • Field Details

  • Method Details

    • getName

      public String getName()
      Description copied from interface: IStatement
      Gets the name of this statement.
      Specified by:
      getName in interface IStatement
      Returns:
      the statement name
    • getDbType

      public DatabaseType getDbType()
    • canDrop

      public boolean canDrop()
      Checks if this statement can be dropped.
      Returns:
      true if the statement can be dropped
    • isSubElement

      public boolean isSubElement()
      Checks if this statement is a sub-element of another statement.
      Returns:
      true if this is a sub-element
    • isOwned

      public boolean isOwned()
      Checks if this statement type supports ownership.
      Returns:
      true if the statement can have an owner
    • getBareName

      public final String getBareName()
      Description copied from interface: IStatement
      Gets the bare name without qualifiers or arguments.
      Specified by:
      getBareName in interface IStatement
      Returns:
      Always returns just the object's name.
    • getParent

      public PgStatement getParent()
      Gets the parent statement that contains this statement.
      Specified by:
      getParent in interface IStatement
      Returns:
      the parent statement, or null if this is a top-level statement
    • getLocation

      public PgObjLocation getLocation()
      Gets the location information for this statement.
      Returns:
      the location where this statement is defined
    • setLocation

      public void setLocation(PgObjLocation location)
      Sets the location information for this statement.
      Parameters:
      location - the location where this statement is defined
    • isLib

      public boolean isLib()
      Checks if this statement comes from a library.
      Returns:
      true if this statement is from a library
    • getLibName

      public String getLibName()
      Gets the name of the library this statement comes from.
      Returns:
      the library name, or null if not from a library
    • setLibName

      public void setLibName(String libName)
      Sets the name of the library this statement comes from.
      Parameters:
      libName - the library name to set
    • getAuthor

      public String getAuthor()
      Gets the author of this statement.
      Returns:
      the author name, or null if not specified
    • setAuthor

      public void setAuthor(String author)
      Sets the author of this statement.
      Parameters:
      author - the author name to set
    • setParent

      public void setParent(PgStatement parent)
      Sets the parent statement for this statement.
      Parameters:
      parent - the parent statement to set
      Throws:
      IllegalStateException - if this statement already has a parent
    • getDeps

      public Set<GenericColumn> getDeps()
    • addDep

      public void addDep(GenericColumn dep)
      Adds a dependency to this statement.
      Parameters:
      dep - the dependency to add
    • addAllDeps

      public void addAllDeps(Collection<GenericColumn> deps)
    • getComment

      public String getComment()
      Description copied from interface: IStatement
      Gets the comment associated with this statement.
      Specified by:
      getComment in interface IStatement
      Returns:
      the comment, or null if no comment is set
    • setComment

      public void setComment(String comment)
    • getTypeName

      public String getTypeName()
      Gets the type name of this statement for SQL generation.
      Returns:
      the type name
    • appendComments

      public void appendComments(SQLScript script)
      Appends comment SQL to the script if this statement has comments.
      Parameters:
      script - the SQL script to append comments to
    • checkComments

      public boolean checkComments()
      Checks if this statement has non-empty comments.
      Returns:
      true if the statement has comments
    • appendAlterComments

      public void appendAlterComments(PgStatement newObj, SQLScript script)
      Appends ALTER comment SQL if the comment has changed.
      Parameters:
      newObj - the new statement to compare comments with
      script - the SQL script to append ALTER comments to
    • getPrivileges

      public Set<PgPrivilege> getPrivileges()
      Gets an unmodifiable set of privileges for this statement.
      Returns:
      unmodifiable set of privileges
    • addPrivilege

      public void addPrivilege(PgPrivilege privilege)
      Adds a privilege to this statement with database-specific filtering. For PostgreSQL, applies ownership and permission filtering. For MS SQL and ClickHouse, adds privileges directly.
      Parameters:
      privilege - the privilege to add
      Throws:
      IllegalArgumentException - if database type is unsupported
    • clearPrivileges

      public void clearPrivileges()
      Clears all privileges from this statement and resets the hash.
    • getOwner

      public String getOwner()
    • setOwner

      public void setOwner(String owner)
    • appendOwnerSQL

      public void appendOwnerSQL(SQLScript script)
      Appends ALTER OWNER SQL statement to the script for this database object.
      Parameters:
      script - the SQL script to append the owner statement to
      Throws:
      IllegalArgumentException - if database type is unsupported
    • getCreationSQL

      public abstract void getCreationSQL(SQLScript script)
      Generates the SQL statements needed to create this database object. This is an abstract method that must be implemented by subclasses to provide the specific CREATE SQL for each object type.
      Parameters:
      script - the SQL script to append creation statements to
    • getSQL

      public String getSQL(boolean isFormatted, ISettings settings)
      Gets the SQL representation of this statement with optional formatting.
      Parameters:
      isFormatted - whether to apply formatting to the SQL
      settings - the settings to use for SQL generation and formatting
      Returns:
      the SQL string representation of this statement
    • getDropSQL

      public final void getDropSQL(SQLScript script)
      Generates DROP SQL for this statement using settings from the script.
      Parameters:
      script - the SQL script to append the DROP statement to
    • canDropBeforeCreate

      public boolean canDropBeforeCreate()
      Checks if this statement can be dropped before being recreated. Override in subclasses that support drop-before-create behavior.
      Returns:
      true if the statement can be dropped before recreation
    • getDropSQL

      public void getDropSQL(SQLScript script, boolean generateExists)
      Generates DROP SQL for this statement.
      Parameters:
      script - the SQL script to append the DROP statement to
      generateExists - whether to include "IF EXISTS" in the DROP statement
    • appendAlterSQL

      public abstract ObjectState appendAlterSQL(PgStatement newCondition, SQLScript script)
      Fill script with object changes and return change type
      Parameters:
      newCondition - new object state
      script - script to collect changes
      Returns:
      object change type
    • getObjectState

      public ObjectState getObjectState(SQLScript script, int startSize)
      Determines the object state based on changes made to the script.
      Parameters:
      script - the SQL script to check for changes
      startSize - the initial size of the script before changes
      Returns:
      the object state indicating the type of change
    • getObjectState

      public ObjectState getObjectState(boolean isNeedDepcies, SQLScript script, int startSize)
      Determines the object state based on changes made to the script.
      Parameters:
      isNeedDepcies - whether dependencies need to be considered
      script - the SQL script to check for changes
      startSize - the initial size of the script before changes
      Returns:
      the object state: NOTHING if no changes, ALTER_WITH_DEP if dependencies needed, ALTER otherwise
    • shallowCopy

      public abstract PgStatement shallowCopy()
      Copies all object properties into a new object and leaves all its children empty.
      Returns:
      shallow copy of a DB object.
    • deepCopy

      public final PgStatement deepCopy()
      Performs shallowCopy() on this object and all its children.
      Returns:
      a fully recursive copy of this statement.
    • compare

      public boolean compare(PgStatement obj)
      This method does not account for nested child PgStatements. Shallow version of equals(Object)
    • getTwin

      public PgStatement getTwin(AbstractDatabase db)
      Returns:
      an element in another db sharing the same name and location
    • getDescendants

      public final Stream<PgStatement> getDescendants()
      Returns all subtree elements
    • getChildren

      public final Stream<PgStatement> getChildren()
      Returns all subelements of current element
    • hasChildren

      public boolean hasChildren()
      Checks if this statement has any child statements.
      Returns:
      true if this statement has children, false otherwise
    • compareChildren

      public boolean compareChildren(PgStatement obj)
      Deep part of equals(Object). Compares all object's child PgStatements for equality.
    • equals

      public final boolean equals(Object obj)
      Compares this object and all its children with another statement.

      Overrides:
      equals in class Object
    • hashCode

      public final int hashCode()
      Calls IHashable.computeHash(org.pgcodekeeper.core.hasher.Hasher). Modifies that value with combined hashcode of all parents of this object in the tree to complement parentNamesEquals(PgStatement) and equals(Object)
      Caches the hashcode value until recalculation is requested via resetHash(). Always request recalculation when you change the hashed fields.
      Do actual hashing in IHashable.computeHash(org.pgcodekeeper.core.hasher.Hasher).

      Overrides:
      hashCode in class Object
    • getQualifiedName

      public String getQualifiedName()
      Description copied from interface: IStatement
      Gets the fully qualified name of this statement.
      Specified by:
      getQualifiedName in interface IStatement
      Returns:
      fully qualified (up to schema) dot-delimited object name. Identifiers are quoted.
    • toString

      public String toString()
      Overrides:
      toString in class Object