Class AbstractStatement

java.lang.Object
org.pgcodekeeper.core.database.base.schema.AbstractStatement
All Implemented Interfaces:
IStatement, IHashable
Direct Known Subclasses:
ChAbstractStatement, MsAbstractStatement, PgAbstractStatement

public abstract class AbstractStatement 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
  • Method Details

    • 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
    • appendAlterComments

      public void appendAlterComments(AbstractStatement 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
    • appendPrivileges

      public void appendPrivileges(SQLScript script)
    • getSQL

      public String getSQL(boolean isFormatted, ISettings settings)
      Description copied from interface: IStatement
      Gets the SQL representation of this statement with optional formatting.
      Specified by:
      getSQL in interface IStatement
      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
    • getDropSQL

      public void getDropSQL(SQLScript script, boolean generateExists)
      Description copied from interface: IStatement
      Generates DROP SQL for this statement.
      Specified by:
      getDropSQL in interface IStatement
      Parameters:
      script - the SQL script to append the DROP statement to
      generateExists - whether to include "IF EXISTS" in the DROP statement
    • canDropBeforeCreate

      public boolean canDropBeforeCreate()
      Description copied from interface: IStatement
      Checks if this statement can be dropped before being recreated. Override in subclasses that support drop-before-create behavior.
      Specified by:
      canDropBeforeCreate in interface IStatement
      Returns:
      true if the statement can be dropped before recreation
    • 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
    • canDrop

      public boolean canDrop()
      Specified by:
      canDrop in interface IStatement
      Returns:
      true if the statement can be dropped
    • 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
    • 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 AbstractStatement 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 ObjectLocation getLocation()
      Description copied from interface: IStatement
      Gets the location information for this statement.
      Specified by:
      getLocation in interface IStatement
      Returns:
      the location where this statement is defined
    • setLocation

      public void setLocation(ObjectLocation location)
      Sets the location information for this statement.
      Specified by:
      setLocation in interface IStatement
      Parameters:
      location - the location where this statement is defined
    • isLib

      public boolean isLib()
      Specified by:
      isLib in interface IStatement
      Returns:
      true if this statement is from a library
    • getLibName

      public String getLibName()
      Description copied from interface: IStatement
      Gets the name of the library this statement comes from.
      Specified by:
      getLibName in interface IStatement
      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.
      Specified by:
      setLibName in interface IStatement
      Parameters:
      libName - the library name to set
    • getAuthor

      public String getAuthor()
      Gets the author of this statement.
      Specified by:
      getAuthor in interface IStatement
      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(AbstractStatement parent)
      Sets the parent statement for this statement.
      Parameters:
      parent - the parent statement to set
      Throws:
      IllegalStateException - if this statement already has a parent
    • getDependencies

      public Set<ObjectReference> getDependencies()
      Specified by:
      getDependencies in interface IStatement
      Returns:
      all object dependencies
    • addDependency

      public void addDependency(ObjectReference dep)
      Description copied from interface: IStatement
      Adds a dependency to this statement.
      Specified by:
      addDependency in interface IStatement
      Parameters:
      dep - the dependency to add
    • 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
    • checkComments

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

      public void setComment(String comment)
      Specified by:
      setComment in interface IStatement
    • getPrivileges

      public Set<IPrivilege> getPrivileges()
      Gets an unmodifiable set of privileges for this statement.
      Specified by:
      getPrivileges in interface IStatement
      Returns:
      unmodifiable set of privileges
    • addPrivilege

      public void addPrivilege(IPrivilege privilege)
      Adds a privilege to this statement.
      Parameters:
      privilege - the privilege to add
      Throws:
      IllegalArgumentException - if database type is unsupported
    • clearPrivileges

      public void clearPrivileges()
      Description copied from interface: IStatement
      Removes all privileges from this statement.
      Specified by:
      clearPrivileges in interface IStatement
    • getOwner

      public String getOwner()
      Description copied from interface: IStatement
      Returns owner of the object
      Specified by:
      getOwner in interface IStatement
    • setOwner

      public void setOwner(String owner)
      Specified by:
      setOwner in interface IStatement
    • getTwin

      public IStatement getTwin(IDatabase db)
      Specified by:
      getTwin in interface IStatement
      Returns:
      an element in another db sharing the same name and location
    • getDescendants

      public final Stream<AbstractStatement> getDescendants()
      Description copied from interface: IStatement
      Returns all subtree elements
      Specified by:
      getDescendants in interface IStatement
    • getChildren

      public final Stream<AbstractStatement> getChildren()
      Description copied from interface: IStatement
      Returns all subelements of current element
      Specified by:
      getChildren in interface IStatement
    • hasChildren

      public boolean hasChildren()
      Checks if this statement has any child statements.
      Specified by:
      hasChildren in interface IStatement
      Returns:
      true if this statement has children, false otherwise
    • fillDescendantsList

      public void fillDescendantsList(List<Collection<? extends AbstractStatement>> l)
    • fillChildrenList

      public void fillChildrenList(List<Collection<? extends AbstractStatement>> l)
    • 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.
    • getSeparator

      public String getSeparator()
      Specified by:
      getSeparator in interface IStatement
    • 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(AbstractStatement) 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
    • equals

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

      Overrides:
      equals in class Object
    • compare

      public boolean compare(IStatement obj)
      This method does not account for nested child PgStatements. Shallow version of equals(Object)
      Specified by:
      compare in interface IStatement
    • compareChildren

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

      public final IStatement deepCopy()
      Description copied from interface: IStatement
      Performs IStatement.shallowCopy() on this object and all its children.
      Specified by:
      deepCopy in interface IStatement
      Returns:
      a fully recursive copy of this statement.
    • shallowCopy

      public final AbstractStatement shallowCopy()
      Description copied from interface: IStatement
      Copies all object properties into a new object and leaves all its children empty.
      Specified by:
      shallowCopy in interface IStatement
      Returns:
      shallow copy of a DB object.
    • toString

      public String toString()
      Overrides:
      toString in class Object