-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathComment.java
More file actions
executable file
·152 lines (131 loc) · 4.04 KB
/
Copy pathComment.java
File metadata and controls
executable file
·152 lines (131 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*-
* #%L
* JSQLParser library
* %%
* Copyright (C) 2004 - 2019 JSQLParser
* %%
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
* #L%
*/
package net.sf.jsqlparser.statement.comment;
import java.util.function.Consumer;
import net.sf.jsqlparser.expression.StringValue;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.StatementVisitor;
public class Comment implements Statement {
private Table table;
private Column column;
private Table view;
private CommentTarget target;
private StringValue comment;
@Override
public <T, S> T accept(StatementVisitor<T> statementVisitor, S context) {
return statementVisitor.visit(this, context);
}
public Table getTable() {
return table;
}
public void setTable(Table table) {
this.table = table;
if (table != null) {
target = null;
}
}
public Column getColumn() {
return column;
}
public void setColumn(Column column) {
this.column = column;
if (column != null) {
target = null;
}
}
public Table getView() {
return view;
}
public void setView(Table view) {
this.view = view;
if (view != null) {
target = null;
}
}
/** Additional catalog targets; the existing table, column and view accessors remain intact. */
public CommentTarget getTarget() {
return target;
}
public void setTarget(CommentTarget target) {
this.target = target;
if (target != null) {
table = null;
column = null;
view = null;
}
}
public Comment withTarget(CommentTarget target) {
setTarget(target);
return this;
}
/** Visits the relation explicitly named by this comment, without resolving catalog objects. */
public void visitRelations(Consumer<Table> visitor) {
Table relation = table != null ? table
: column != null ? column.getTable()
: view != null ? view
: target != null ? target.getReferencedRelation() : null;
if (relation != null) {
visitor.accept(relation);
}
}
public StringValue getComment() {
return comment;
}
public void setComment(StringValue comment) {
this.comment = comment;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
return appendTo(builder, builder::append, builder::append, builder::append).toString();
}
public StringBuilder appendTo(StringBuilder builder, Consumer<Table> relationWriter,
Consumer<Column> columnWriter, Consumer<StringValue> commentWriter) {
builder.append("COMMENT ON ");
if (table != null) {
builder.append("TABLE ");
relationWriter.accept(table);
builder.append(' ');
} else if (column != null) {
builder.append("COLUMN ");
columnWriter.accept(column);
builder.append(' ');
} else if (view != null) {
builder.append("VIEW ");
relationWriter.accept(view);
builder.append(' ');
} else if (target != null) {
target.appendTo(builder, relationWriter);
builder.append(' ');
}
// a null comment stands for PostgreSQL's COMMENT ON ... IS NULL, which removes the comment
builder.append("IS ");
if (comment == null) {
builder.append("NULL");
} else {
commentWriter.accept(comment);
}
return builder;
}
public Comment withTable(Table table) {
this.setTable(table);
return this;
}
public Comment withColumn(Column column) {
this.setColumn(column);
return this;
}
public Comment withComment(StringValue comment) {
this.setComment(comment);
return this;
}
}