Repository navigation
Expand file tree
/
Copy pathMySqlFileFormat.java
More file actions
196 lines (164 loc) · 6.17 KB
/
Copy pathMySqlFileFormat.java
File metadata and controls
196 lines (164 loc) · 6.17 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*-
* #%L
* JSQLParser library
* %%
* Copyright (C) 2004 - 2026 JSQLParser
* %%
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
* #L%
*/
package net.sf.jsqlparser.statement;
import java.io.Serializable;
import net.sf.jsqlparser.expression.StringValue;
import net.sf.jsqlparser.parser.ASTNodeAccessImpl;
/**
* The {@code {FIELDS | COLUMNS}} and {@code LINES} clauses that describe the text file of a MySQL
* {@code LOAD DATA} or {@code SELECT ... INTO OUTFILE}:
*
* <pre>
* [{FIELDS | COLUMNS} [TERMINATED BY 'string'] [[OPTIONALLY] ENCLOSED BY 'char'] [ESCAPED BY 'char']]
* [LINES [STARTING BY 'string'] [TERMINATED BY 'string']]
* </pre>
*
* Each clause has at least one subclause. MySQL accepts the subclauses of a clause in any order and
* more than once; like the server, the model keeps the last value of each and prints them in the
* order above. {@code OPTIONALLY} stays set once a subclause has written it.
*
* @see <a href="https://dev.mysql.com/doc/refman/8.4/en/load-data.html">LOAD DATA</a>
*/
public class MySqlFileFormat extends ASTNodeAccessImpl implements Serializable {
/** The spelling of the fields clause. */
public enum FieldsKeyword {
FIELDS, COLUMNS
}
private FieldsKeyword fieldsKeyword;
private StringValue fieldsTerminatedBy;
private boolean fieldsOptionallyEnclosed;
private StringValue fieldsEnclosedBy;
private StringValue fieldsEscapedBy;
private StringValue linesStartingBy;
private StringValue linesTerminatedBy;
/** The spelling of the fields clause; null prints FIELDS when a subclause is set. */
public FieldsKeyword getFieldsKeyword() {
return fieldsKeyword;
}
public void setFieldsKeyword(FieldsKeyword fieldsKeyword) {
this.fieldsKeyword = fieldsKeyword;
}
public MySqlFileFormat withFieldsKeyword(FieldsKeyword fieldsKeyword) {
setFieldsKeyword(fieldsKeyword);
return this;
}
public StringValue getFieldsTerminatedBy() {
return fieldsTerminatedBy;
}
public void setFieldsTerminatedBy(StringValue fieldsTerminatedBy) {
this.fieldsTerminatedBy = fieldsTerminatedBy;
}
public MySqlFileFormat withFieldsTerminatedBy(StringValue fieldsTerminatedBy) {
setFieldsTerminatedBy(fieldsTerminatedBy);
return this;
}
public boolean isFieldsOptionallyEnclosed() {
return fieldsOptionallyEnclosed;
}
public void setFieldsOptionallyEnclosed(boolean fieldsOptionallyEnclosed) {
this.fieldsOptionallyEnclosed = fieldsOptionallyEnclosed;
}
public MySqlFileFormat withFieldsOptionallyEnclosed(boolean fieldsOptionallyEnclosed) {
setFieldsOptionallyEnclosed(fieldsOptionallyEnclosed);
return this;
}
public StringValue getFieldsEnclosedBy() {
return fieldsEnclosedBy;
}
public void setFieldsEnclosedBy(StringValue fieldsEnclosedBy) {
this.fieldsEnclosedBy = fieldsEnclosedBy;
}
public MySqlFileFormat withFieldsEnclosedBy(StringValue fieldsEnclosedBy) {
setFieldsEnclosedBy(fieldsEnclosedBy);
return this;
}
public StringValue getFieldsEscapedBy() {
return fieldsEscapedBy;
}
public void setFieldsEscapedBy(StringValue fieldsEscapedBy) {
this.fieldsEscapedBy = fieldsEscapedBy;
}
public MySqlFileFormat withFieldsEscapedBy(StringValue fieldsEscapedBy) {
setFieldsEscapedBy(fieldsEscapedBy);
return this;
}
public StringValue getLinesStartingBy() {
return linesStartingBy;
}
public void setLinesStartingBy(StringValue linesStartingBy) {
this.linesStartingBy = linesStartingBy;
}
public MySqlFileFormat withLinesStartingBy(StringValue linesStartingBy) {
setLinesStartingBy(linesStartingBy);
return this;
}
public StringValue getLinesTerminatedBy() {
return linesTerminatedBy;
}
public void setLinesTerminatedBy(StringValue linesTerminatedBy) {
this.linesTerminatedBy = linesTerminatedBy;
}
public MySqlFileFormat withLinesTerminatedBy(StringValue linesTerminatedBy) {
setLinesTerminatedBy(linesTerminatedBy);
return this;
}
/** Whether the format prints a fields clause: a spelling or a subclause is set. */
public boolean hasFieldsClause() {
return fieldsKeyword != null || fieldsTerminatedBy != null || fieldsEnclosedBy != null
|| fieldsEscapedBy != null;
}
/** Whether the format prints a lines clause: a subclause is set. */
public boolean hasLinesClause() {
return linesStartingBy != null || linesTerminatedBy != null;
}
/** Whether the format prints nothing. */
public boolean isEmpty() {
return !hasFieldsClause() && !hasLinesClause();
}
/** Appends the fields clause and then the lines clause, separated by a space. */
public StringBuilder appendTo(StringBuilder builder) {
if (hasFieldsClause()) {
appendFieldsClause(builder);
if (hasLinesClause()) {
builder.append(' ');
}
}
if (hasLinesClause()) {
appendLinesClause(builder);
}
return builder;
}
private void appendFieldsClause(StringBuilder builder) {
builder.append(fieldsKeyword != null ? fieldsKeyword : FieldsKeyword.FIELDS);
if (fieldsTerminatedBy != null) {
builder.append(" TERMINATED BY ").append(fieldsTerminatedBy);
}
if (fieldsEnclosedBy != null) {
builder.append(fieldsOptionallyEnclosed ? " OPTIONALLY ENCLOSED BY " : " ENCLOSED BY ")
.append(fieldsEnclosedBy);
}
if (fieldsEscapedBy != null) {
builder.append(" ESCAPED BY ").append(fieldsEscapedBy);
}
}
private void appendLinesClause(StringBuilder builder) {
builder.append("LINES");
if (linesStartingBy != null) {
builder.append(" STARTING BY ").append(linesStartingBy);
}
if (linesTerminatedBy != null) {
builder.append(" TERMINATED BY ").append(linesTerminatedBy);
}
}
@Override
public String toString() {
return appendTo(new StringBuilder()).toString();
}
}