Package org.pgcodekeeper.core.schema
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
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 Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionvoidaddAllDeps(Collection<GenericColumn> deps) voidaddDep(GenericColumn dep) Adds a dependency to this statement.voidaddPrivilege(PgPrivilege privilege) Adds a privilege to this statement with database-specific filtering.voidappendAlterComments(PgStatement newObj, SQLScript script) Appends ALTER comment SQL if the comment has changed.abstract ObjectStateappendAlterSQL(PgStatement newCondition, SQLScript script) Fill script with object changes and return change typevoidappendComments(SQLScript script) Appends comment SQL to the script if this statement has comments.voidappendOwnerSQL(SQLScript script) Appends ALTER OWNER SQL statement to the script for this database object.booleancanDrop()Checks if this statement can be dropped.booleanChecks if this statement can be dropped before being recreated.booleanChecks if this statement has non-empty comments.voidClears all privileges from this statement and resets the hash.booleancompare(PgStatement obj) This method does not account for nested child PgStatements.booleanDeep part ofequals(Object).final PgStatementdeepCopy()PerformsshallowCopy()on this object and all its children.final booleanCompares this object and all its children with another statement.Gets the author of this statement.final StringGets the bare name without qualifiers or arguments.final Stream<PgStatement>Returns all subelements of current elementGets the comment associated with this statement.abstract voidgetCreationSQL(SQLScript script) Generates the SQL statements needed to create this database object.getDeps()final Stream<PgStatement>Returns all subtree elementsfinal voidgetDropSQL(SQLScript script) Generates DROP SQL for this statement using settings from the script.voidgetDropSQL(SQLScript script, boolean generateExists) Generates DROP SQL for this statement.Gets the name of the library this statement comes from.Gets the location information for this statement.getName()Gets the name of this statement.getObjectState(boolean isNeedDepcies, SQLScript script, int startSize) Determines the object state based on changes made to the script.getObjectState(SQLScript script, int startSize) Determines the object state based on changes made to the script.getOwner()Gets the parent statement that contains this statement.Gets an unmodifiable set of privileges for this statement.Gets the fully qualified name of this statement.Gets the SQL representation of this statement with optional formatting.Gets the type name of this statement for SQL generation.booleanChecks if this statement has any child statements.final inthashCode()booleanisLib()Checks if this statement comes from a library.booleanisOwned()Checks if this statement type supports ownership.booleanChecks if this statement is a sub-element of another statement.voidSets the author of this statement.voidsetComment(String comment) voidsetLibName(String libName) Sets the name of the library this statement comes from.voidsetLocation(PgObjLocation location) Sets the location information for this statement.voidvoidsetParent(PgStatement parent) Sets the parent statement for this statement.abstract PgStatementCopies all object properties into a new object and leaves all its children empty.toString()Methods inherited from interface org.pgcodekeeper.core.hasher.IHashable
computeHashMethods inherited from interface org.pgcodekeeper.core.schema.IStatement
getDatabase, getStatementType
-
Field Details
-
GO
- See Also:
-
-
Method Details
-
getName
Description copied from interface:IStatementGets the name of this statement.- Specified by:
getNamein interfaceIStatement- Returns:
- the statement name
-
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
Description copied from interface:IStatementGets the bare name without qualifiers or arguments.- Specified by:
getBareNamein interfaceIStatement- Returns:
- Always returns just the object's name.
-
getParent
Gets the parent statement that contains this statement.- Specified by:
getParentin interfaceIStatement- Returns:
- the parent statement, or null if this is a top-level statement
-
getLocation
Gets the location information for this statement.- Returns:
- the location where this statement is defined
-
setLocation
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
Gets the name of the library this statement comes from.- Returns:
- the library name, or null if not from a library
-
setLibName
Sets the name of the library this statement comes from.- Parameters:
libName- the library name to set
-
getAuthor
Gets the author of this statement.- Returns:
- the author name, or null if not specified
-
setAuthor
Sets the author of this statement.- Parameters:
author- the author name to set
-
setParent
Sets the parent statement for this statement.- Parameters:
parent- the parent statement to set- Throws:
IllegalStateException- if this statement already has a parent
-
getDeps
-
addDep
Adds a dependency to this statement.- Parameters:
dep- the dependency to add
-
addAllDeps
-
getComment
Description copied from interface:IStatementGets the comment associated with this statement.- Specified by:
getCommentin interfaceIStatement- Returns:
- the comment, or null if no comment is set
-
setComment
-
getTypeName
Gets the type name of this statement for SQL generation.- Returns:
- the type name
-
appendComments
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
Appends ALTER comment SQL if the comment has changed.- Parameters:
newObj- the new statement to compare comments withscript- the SQL script to append ALTER comments to
-
getPrivileges
Gets an unmodifiable set of privileges for this statement.- Returns:
- unmodifiable set of privileges
-
addPrivilege
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
-
setOwner
-
appendOwnerSQL
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
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
Gets the SQL representation of this statement with optional formatting.- Parameters:
isFormatted- whether to apply formatting to the SQLsettings- the settings to use for SQL generation and formatting- Returns:
- the SQL string representation of this statement
-
getDropSQL
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
Generates DROP SQL for this statement.- Parameters:
script- the SQL script to append the DROP statement togenerateExists- whether to include "IF EXISTS" in the DROP statement
-
appendAlterSQL
Fill script with object changes and return change type- Parameters:
newCondition- new object statescript- script to collect changes- Returns:
- object change type
-
getObjectState
Determines the object state based on changes made to the script.- Parameters:
script- the SQL script to check for changesstartSize- the initial size of the script before changes- Returns:
- the object state indicating the type of change
-
getObjectState
Determines the object state based on changes made to the script.- Parameters:
isNeedDepcies- whether dependencies need to be consideredscript- the SQL script to check for changesstartSize- 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
Copies all object properties into a new object and leaves all its children empty.- Returns:
- shallow copy of a DB object.
-
deepCopy
PerformsshallowCopy()on this object and all its children.- Returns:
- a fully recursive copy of this statement.
-
compare
This method does not account for nested child PgStatements. Shallow version ofequals(Object) -
getTwin
- Returns:
- an element in another db sharing the same name and location
-
getDescendants
Returns all subtree elements -
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
Deep part ofequals(Object). Compares all object's child PgStatements for equality. -
equals
Compares this object and all its children with another statement.
-
hashCode
public final int hashCode()CallsIHashable.computeHash(org.pgcodekeeper.core.hasher.Hasher). Modifies that value with combined hashcode of all parents of this object in the tree to complementparentNamesEquals(PgStatement)andequals(Object)
Caches the hashcode value until recalculation is requested viaresetHash(). Always request recalculation when you change the hashed fields.
Do actual hashing inIHashable.computeHash(org.pgcodekeeper.core.hasher.Hasher).
-
getQualifiedName
Description copied from interface:IStatementGets the fully qualified name of this statement.- Specified by:
getQualifiedNamein interfaceIStatement- Returns:
- fully qualified (up to schema) dot-delimited object name. Identifiers are quoted.
-
toString
-