99 */
1010package net .sf .jsqlparser .statement .comment ;
1111
12+ import java .util .function .Consumer ;
1213import net .sf .jsqlparser .expression .StringValue ;
1314import net .sf .jsqlparser .schema .Column ;
1415import net .sf .jsqlparser .schema .Table ;
@@ -20,6 +21,7 @@ public class Comment implements Statement {
2021 private Table table ;
2122 private Column column ;
2223 private Table view ;
24+ private CommentTarget target ;
2325 private StringValue comment ;
2426
2527 @ Override
@@ -33,6 +35,9 @@ public Table getTable() {
3335
3436 public void setTable (Table table ) {
3537 this .table = table ;
38+ if (table != null ) {
39+ target = null ;
40+ }
3641 }
3742
3843 public Column getColumn () {
@@ -41,6 +46,9 @@ public Column getColumn() {
4146
4247 public void setColumn (Column column ) {
4348 this .column = column ;
49+ if (column != null ) {
50+ target = null ;
51+ }
4452 }
4553
4654 public Table getView () {
@@ -49,6 +57,39 @@ public Table getView() {
4957
5058 public void setView (Table view ) {
5159 this .view = view ;
60+ if (view != null ) {
61+ target = null ;
62+ }
63+ }
64+
65+ /** Additional catalog targets; the existing table, column and view accessors remain intact. */
66+ public CommentTarget getTarget () {
67+ return target ;
68+ }
69+
70+ public void setTarget (CommentTarget target ) {
71+ this .target = target ;
72+ if (target != null ) {
73+ table = null ;
74+ column = null ;
75+ view = null ;
76+ }
77+ }
78+
79+ public Comment withTarget (CommentTarget target ) {
80+ setTarget (target );
81+ return this ;
82+ }
83+
84+ /** Visits the relation explicitly named by this comment, without resolving catalog objects. */
85+ public void visitRelations (Consumer <Table > visitor ) {
86+ Table relation = table != null ? table
87+ : column != null ? column .getTable ()
88+ : view != null ? view
89+ : target != null ? target .getReferencedRelation () : null ;
90+ if (relation != null ) {
91+ visitor .accept (relation );
92+ }
5293 }
5394
5495 public StringValue getComment () {
@@ -61,17 +102,37 @@ public void setComment(StringValue comment) {
61102
62103 @ Override
63104 public String toString () {
64- String sql = "COMMENT ON " ;
105+ StringBuilder builder = new StringBuilder ();
106+ return appendTo (builder , builder ::append , builder ::append , builder ::append ).toString ();
107+ }
108+
109+ public StringBuilder appendTo (StringBuilder builder , Consumer <Table > relationWriter ,
110+ Consumer <Column > columnWriter , Consumer <StringValue > commentWriter ) {
111+ builder .append ("COMMENT ON " );
65112 if (table != null ) {
66- sql += "TABLE " + table + " " ;
113+ builder .append ("TABLE " );
114+ relationWriter .accept (table );
115+ builder .append (' ' );
67116 } else if (column != null ) {
68- sql += "COLUMN " + column + " " ;
117+ builder .append ("COLUMN " );
118+ columnWriter .accept (column );
119+ builder .append (' ' );
69120 } else if (view != null ) {
70- sql += "VIEW " + view + " " ;
121+ builder .append ("VIEW " );
122+ relationWriter .accept (view );
123+ builder .append (' ' );
124+ } else if (target != null ) {
125+ target .appendTo (builder , relationWriter );
126+ builder .append (' ' );
71127 }
72128 // a null comment stands for PostgreSQL's COMMENT ON ... IS NULL, which removes the comment
73- sql += "IS " + (comment != null ? comment : "NULL" );
74- return sql ;
129+ builder .append ("IS " );
130+ if (comment == null ) {
131+ builder .append ("NULL" );
132+ } else {
133+ commentWriter .accept (comment );
134+ }
135+ return builder ;
75136 }
76137
77138 public Comment withTable (Table table ) {
0 commit comments