1313import java .util .function .Consumer ;
1414import net .sf .jsqlparser .schema .Table ;
1515import net .sf .jsqlparser .statement .RoutineReference ;
16+ import net .sf .jsqlparser .statement .create .table .ColDataType ;
1617
1718/** A catalog object addressed by COMMENT, rather than a function invocation or a query. */
1819public class CommentTarget implements Serializable {
1920 public enum Kind {
20- INDEX , SCHEMA , SEQUENCE , DOMAIN , TYPE , MATERIALIZED_VIEW , FUNCTION , CONSTRAINT
21+ INDEX , SCHEMA , SEQUENCE , DOMAIN , TYPE , MATERIALIZED_VIEW , FUNCTION , CONSTRAINT ,
22+
23+ TRIGGER , POLICY , RULE , PROCEDURE , ROUTINE , AGGREGATE , CAST ,
24+
25+ COLLATION , FOREIGN_TABLE , STATISTICS , DATABASE , ROLE , TABLESPACE , ACCESS_METHOD , EXTENSION ,
26+
27+ FOREIGN_DATA_WRAPPER , LANGUAGE , SERVER , EVENT_TRIGGER , PUBLICATION , SUBSCRIPTION ;
28+
29+ /**
30+ * Whether the target is a routine signature, exposed by {@link CommentTarget#getRoutine()}.
31+ */
32+ public boolean isRoutine () {
33+ return this == FUNCTION || this == PROCEDURE || this == ROUTINE || this == AGGREGATE ;
34+ }
35+
36+ /**
37+ * Whether the target is named within the table or domain of
38+ * {@link CommentTarget#getRelation()}.
39+ */
40+ public boolean isOwnedByRelation () {
41+ return this == CONSTRAINT || this == TRIGGER || this == POLICY || this == RULE ;
42+ }
2143 }
2244
2345 private Kind kind ;
2446 private Table name ;
2547 private RoutineReference routine ;
2648 private Table relation ;
2749 private boolean onDomain ;
50+ private boolean procedural ;
51+ private ColDataType sourceType ;
52+ private ColDataType targetType ;
2853
2954 public Kind getKind () {
3055 return kind ;
@@ -34,7 +59,10 @@ public void setKind(Kind kind) {
3459 this .kind = kind ;
3560 }
3661
37- /** The object's identifier; using Table preserves the individual name components. */
62+ /**
63+ * The object's identifier; using Table preserves the individual name components. Routine and
64+ * cast targets have no name.
65+ */
3866 public Table getName () {
3967 return name ;
4068 }
@@ -51,7 +79,10 @@ public void setRoutine(RoutineReference routine) {
5179 this .routine = routine ;
5280 }
5381
54- /** The table or domain owning a constraint, distinguished by {@link #isOnDomain()}. */
82+ /**
83+ * The table owning a constraint, trigger, policy or rule, or the domain owning a constraint,
84+ * distinguished by {@link #isOnDomain()}.
85+ */
5586 public Table getRelation () {
5687 return relation ;
5788 }
@@ -68,23 +99,62 @@ public void setOnDomain(boolean onDomain) {
6899 this .onDomain = onDomain ;
69100 }
70101
71- /** Returns only an explicitly named table/view, never an index, type, function or domain. */
102+ /** Whether a {@link Kind#LANGUAGE} target was written as PROCEDURAL LANGUAGE. */
103+ public boolean isProcedural () {
104+ return procedural ;
105+ }
106+
107+ public void setProcedural (boolean procedural ) {
108+ this .procedural = procedural ;
109+ }
110+
111+ /** The source type of a {@link Kind#CAST} target. */
112+ public ColDataType getSourceType () {
113+ return sourceType ;
114+ }
115+
116+ public void setSourceType (ColDataType sourceType ) {
117+ this .sourceType = sourceType ;
118+ }
119+
120+ /** The target type of a {@link Kind#CAST} target. */
121+ public ColDataType getTargetType () {
122+ return targetType ;
123+ }
124+
125+ public void setTargetType (ColDataType targetType ) {
126+ this .targetType = targetType ;
127+ }
128+
129+ /**
130+ * Returns only an explicitly named table, foreign table or view, never an index, type,
131+ * function, statistics object or domain.
132+ */
72133 public Table getReferencedRelation () {
73- if (kind == Kind . MATERIALIZED_VIEW ) {
134+ if (namesRelation () ) {
74135 return name ;
75136 }
76- return kind == Kind .CONSTRAINT && !onDomain ? relation : null ;
137+ return kind != null && kind .isOwnedByRelation () && !onDomain ? relation : null ;
138+ }
139+
140+ private boolean namesRelation () {
141+ return kind == Kind .MATERIALIZED_VIEW || kind == Kind .FOREIGN_TABLE ;
77142 }
78143
79144 public StringBuilder appendTo (StringBuilder builder , Consumer <Table > relationWriter ) {
145+ if (kind == Kind .LANGUAGE && procedural ) {
146+ builder .append ("PROCEDURAL " );
147+ }
80148 builder .append (kind .name ().replace ('_' , ' ' )).append (' ' );
81- if (kind == Kind . FUNCTION ) {
149+ if (kind . isRoutine () ) {
82150 builder .append (routine );
83- } else if (kind == Kind .MATERIALIZED_VIEW ) {
151+ } else if (kind == Kind .CAST ) {
152+ builder .append ('(' ).append (sourceType ).append (" AS " ).append (targetType ).append (')' );
153+ } else if (namesRelation ()) {
84154 relationWriter .accept (name );
85155 } else {
86156 builder .append (name );
87- if (kind == Kind . CONSTRAINT ) {
157+ if (kind . isOwnedByRelation () ) {
88158 builder .append (" ON " );
89159 if (onDomain ) {
90160 builder .append ("DOMAIN " ).append (relation );
0 commit comments