|
| 1 | +/*- |
| 2 | + * #%L |
| 3 | + * JSQLParser library |
| 4 | + * %% |
| 5 | + * Copyright (C) 2004 - 2026 JSQLParser |
| 6 | + * %% |
| 7 | + * Dual licensed under GNU LGPL 2.1 or Apache License 2.0 |
| 8 | + * #L% |
| 9 | + */ |
| 10 | +package net.sf.jsqlparser.statement; |
| 11 | + |
| 12 | +import java.io.Serializable; |
| 13 | +import net.sf.jsqlparser.expression.StringValue; |
| 14 | +import net.sf.jsqlparser.parser.ASTNodeAccessImpl; |
| 15 | + |
| 16 | +/** |
| 17 | + * The {@code {FIELDS | COLUMNS}} and {@code LINES} clauses that describe the text file of a MySQL |
| 18 | + * {@code LOAD DATA} or {@code SELECT ... INTO OUTFILE}: |
| 19 | + * |
| 20 | + * <pre> |
| 21 | + * [{FIELDS | COLUMNS} [TERMINATED BY 'string'] [[OPTIONALLY] ENCLOSED BY 'char'] [ESCAPED BY 'char']] |
| 22 | + * [LINES [STARTING BY 'string'] [TERMINATED BY 'string']] |
| 23 | + * </pre> |
| 24 | + * |
| 25 | + * Each clause has at least one subclause. MySQL accepts the subclauses of a clause in any order and |
| 26 | + * more than once; like the server, the model keeps the last value of each and prints them in the |
| 27 | + * order above. {@code OPTIONALLY} stays set once a subclause has written it. |
| 28 | + * |
| 29 | + * @see <a href="https://dev.mysql.com/doc/refman/8.4/en/load-data.html">LOAD DATA</a> |
| 30 | + */ |
| 31 | +public class MySqlFileFormat extends ASTNodeAccessImpl implements Serializable { |
| 32 | + |
| 33 | + /** The spelling of the fields clause. */ |
| 34 | + public enum FieldsKeyword { |
| 35 | + FIELDS, COLUMNS |
| 36 | + } |
| 37 | + |
| 38 | + private FieldsKeyword fieldsKeyword; |
| 39 | + private StringValue fieldsTerminatedBy; |
| 40 | + private boolean fieldsOptionallyEnclosed; |
| 41 | + private StringValue fieldsEnclosedBy; |
| 42 | + private StringValue fieldsEscapedBy; |
| 43 | + private StringValue linesStartingBy; |
| 44 | + private StringValue linesTerminatedBy; |
| 45 | + |
| 46 | + /** The spelling of the fields clause; null prints FIELDS when a subclause is set. */ |
| 47 | + public FieldsKeyword getFieldsKeyword() { |
| 48 | + return fieldsKeyword; |
| 49 | + } |
| 50 | + |
| 51 | + public void setFieldsKeyword(FieldsKeyword fieldsKeyword) { |
| 52 | + this.fieldsKeyword = fieldsKeyword; |
| 53 | + } |
| 54 | + |
| 55 | + public MySqlFileFormat withFieldsKeyword(FieldsKeyword fieldsKeyword) { |
| 56 | + setFieldsKeyword(fieldsKeyword); |
| 57 | + return this; |
| 58 | + } |
| 59 | + |
| 60 | + public StringValue getFieldsTerminatedBy() { |
| 61 | + return fieldsTerminatedBy; |
| 62 | + } |
| 63 | + |
| 64 | + public void setFieldsTerminatedBy(StringValue fieldsTerminatedBy) { |
| 65 | + this.fieldsTerminatedBy = fieldsTerminatedBy; |
| 66 | + } |
| 67 | + |
| 68 | + public MySqlFileFormat withFieldsTerminatedBy(StringValue fieldsTerminatedBy) { |
| 69 | + setFieldsTerminatedBy(fieldsTerminatedBy); |
| 70 | + return this; |
| 71 | + } |
| 72 | + |
| 73 | + public boolean isFieldsOptionallyEnclosed() { |
| 74 | + return fieldsOptionallyEnclosed; |
| 75 | + } |
| 76 | + |
| 77 | + public void setFieldsOptionallyEnclosed(boolean fieldsOptionallyEnclosed) { |
| 78 | + this.fieldsOptionallyEnclosed = fieldsOptionallyEnclosed; |
| 79 | + } |
| 80 | + |
| 81 | + public MySqlFileFormat withFieldsOptionallyEnclosed(boolean fieldsOptionallyEnclosed) { |
| 82 | + setFieldsOptionallyEnclosed(fieldsOptionallyEnclosed); |
| 83 | + return this; |
| 84 | + } |
| 85 | + |
| 86 | + public StringValue getFieldsEnclosedBy() { |
| 87 | + return fieldsEnclosedBy; |
| 88 | + } |
| 89 | + |
| 90 | + public void setFieldsEnclosedBy(StringValue fieldsEnclosedBy) { |
| 91 | + this.fieldsEnclosedBy = fieldsEnclosedBy; |
| 92 | + } |
| 93 | + |
| 94 | + public MySqlFileFormat withFieldsEnclosedBy(StringValue fieldsEnclosedBy) { |
| 95 | + setFieldsEnclosedBy(fieldsEnclosedBy); |
| 96 | + return this; |
| 97 | + } |
| 98 | + |
| 99 | + public StringValue getFieldsEscapedBy() { |
| 100 | + return fieldsEscapedBy; |
| 101 | + } |
| 102 | + |
| 103 | + public void setFieldsEscapedBy(StringValue fieldsEscapedBy) { |
| 104 | + this.fieldsEscapedBy = fieldsEscapedBy; |
| 105 | + } |
| 106 | + |
| 107 | + public MySqlFileFormat withFieldsEscapedBy(StringValue fieldsEscapedBy) { |
| 108 | + setFieldsEscapedBy(fieldsEscapedBy); |
| 109 | + return this; |
| 110 | + } |
| 111 | + |
| 112 | + public StringValue getLinesStartingBy() { |
| 113 | + return linesStartingBy; |
| 114 | + } |
| 115 | + |
| 116 | + public void setLinesStartingBy(StringValue linesStartingBy) { |
| 117 | + this.linesStartingBy = linesStartingBy; |
| 118 | + } |
| 119 | + |
| 120 | + public MySqlFileFormat withLinesStartingBy(StringValue linesStartingBy) { |
| 121 | + setLinesStartingBy(linesStartingBy); |
| 122 | + return this; |
| 123 | + } |
| 124 | + |
| 125 | + public StringValue getLinesTerminatedBy() { |
| 126 | + return linesTerminatedBy; |
| 127 | + } |
| 128 | + |
| 129 | + public void setLinesTerminatedBy(StringValue linesTerminatedBy) { |
| 130 | + this.linesTerminatedBy = linesTerminatedBy; |
| 131 | + } |
| 132 | + |
| 133 | + public MySqlFileFormat withLinesTerminatedBy(StringValue linesTerminatedBy) { |
| 134 | + setLinesTerminatedBy(linesTerminatedBy); |
| 135 | + return this; |
| 136 | + } |
| 137 | + |
| 138 | + /** Whether the format prints a fields clause: a spelling or a subclause is set. */ |
| 139 | + public boolean hasFieldsClause() { |
| 140 | + return fieldsKeyword != null || fieldsTerminatedBy != null || fieldsEnclosedBy != null |
| 141 | + || fieldsEscapedBy != null; |
| 142 | + } |
| 143 | + |
| 144 | + /** Whether the format prints a lines clause: a subclause is set. */ |
| 145 | + public boolean hasLinesClause() { |
| 146 | + return linesStartingBy != null || linesTerminatedBy != null; |
| 147 | + } |
| 148 | + |
| 149 | + /** Whether the format prints nothing. */ |
| 150 | + public boolean isEmpty() { |
| 151 | + return !hasFieldsClause() && !hasLinesClause(); |
| 152 | + } |
| 153 | + |
| 154 | + /** Appends the fields clause and then the lines clause, separated by a space. */ |
| 155 | + public StringBuilder appendTo(StringBuilder builder) { |
| 156 | + if (hasFieldsClause()) { |
| 157 | + appendFieldsClause(builder); |
| 158 | + if (hasLinesClause()) { |
| 159 | + builder.append(' '); |
| 160 | + } |
| 161 | + } |
| 162 | + if (hasLinesClause()) { |
| 163 | + appendLinesClause(builder); |
| 164 | + } |
| 165 | + return builder; |
| 166 | + } |
| 167 | + |
| 168 | + private void appendFieldsClause(StringBuilder builder) { |
| 169 | + builder.append(fieldsKeyword != null ? fieldsKeyword : FieldsKeyword.FIELDS); |
| 170 | + if (fieldsTerminatedBy != null) { |
| 171 | + builder.append(" TERMINATED BY ").append(fieldsTerminatedBy); |
| 172 | + } |
| 173 | + if (fieldsEnclosedBy != null) { |
| 174 | + builder.append(fieldsOptionallyEnclosed ? " OPTIONALLY ENCLOSED BY " : " ENCLOSED BY ") |
| 175 | + .append(fieldsEnclosedBy); |
| 176 | + } |
| 177 | + if (fieldsEscapedBy != null) { |
| 178 | + builder.append(" ESCAPED BY ").append(fieldsEscapedBy); |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + private void appendLinesClause(StringBuilder builder) { |
| 183 | + builder.append("LINES"); |
| 184 | + if (linesStartingBy != null) { |
| 185 | + builder.append(" STARTING BY ").append(linesStartingBy); |
| 186 | + } |
| 187 | + if (linesTerminatedBy != null) { |
| 188 | + builder.append(" TERMINATED BY ").append(linesTerminatedBy); |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + @Override |
| 193 | + public String toString() { |
| 194 | + return appendTo(new StringBuilder()).toString(); |
| 195 | + } |
| 196 | +} |
0 commit comments