Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
/*-
* #%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.select;

import java.io.Serializable;
import net.sf.jsqlparser.expression.StringValue;
import net.sf.jsqlparser.parser.ASTNodeAccessImpl;

public class MySqlSelectIntoClause extends ASTNodeAccessImpl implements Serializable {

public enum Position {
BEFORE_FROM, TRAILING
}

public enum Type {
OUTFILE, DUMPFILE
}

public enum FieldsKeyword {
FIELDS, COLUMNS
}

private Position position = Position.TRAILING;
private Type type;
private StringValue fileName;
private String characterSet;
private FieldsKeyword fieldsKeyword;
private StringValue fieldsTerminatedBy;
private boolean fieldsOptionallyEnclosed;
private StringValue fieldsEnclosedBy;
private StringValue fieldsEscapedBy;
private StringValue linesStartingBy;
private StringValue linesTerminatedBy;

public Position getPosition() {
return position;
}

public void setPosition(Position position) {
this.position = position;
}

public MySqlSelectIntoClause withPosition(Position position) {
this.setPosition(position);
return this;
}

public Type getType() {
return type;
}

public void setType(Type type) {
this.type = type;
}

public StringValue getFileName() {
return fileName;
}

public void setFileName(StringValue fileName) {
this.fileName = fileName;
}

public String getCharacterSet() {
return characterSet;
}

public void setCharacterSet(String characterSet) {
this.characterSet = characterSet;
}

public FieldsKeyword getFieldsKeyword() {
return fieldsKeyword;
}

public void setFieldsKeyword(FieldsKeyword fieldsKeyword) {
this.fieldsKeyword = fieldsKeyword;
}

public StringValue getFieldsTerminatedBy() {
return fieldsTerminatedBy;
}

public void setFieldsTerminatedBy(StringValue fieldsTerminatedBy) {
this.fieldsTerminatedBy = fieldsTerminatedBy;
}

public boolean isFieldsOptionallyEnclosed() {
return fieldsOptionallyEnclosed;
}

public void setFieldsOptionallyEnclosed(boolean fieldsOptionallyEnclosed) {
this.fieldsOptionallyEnclosed = fieldsOptionallyEnclosed;
}

public StringValue getFieldsEnclosedBy() {
return fieldsEnclosedBy;
}

public void setFieldsEnclosedBy(StringValue fieldsEnclosedBy) {
this.fieldsEnclosedBy = fieldsEnclosedBy;
}

public StringValue getFieldsEscapedBy() {
return fieldsEscapedBy;
}

public void setFieldsEscapedBy(StringValue fieldsEscapedBy) {
this.fieldsEscapedBy = fieldsEscapedBy;
}

public StringValue getLinesStartingBy() {
return linesStartingBy;
}

public void setLinesStartingBy(StringValue linesStartingBy) {
this.linesStartingBy = linesStartingBy;
}

public StringValue getLinesTerminatedBy() {
return linesTerminatedBy;
}

public void setLinesTerminatedBy(StringValue linesTerminatedBy) {
this.linesTerminatedBy = linesTerminatedBy;
}

public boolean hasFieldsClause() {
return fieldsKeyword != null || fieldsTerminatedBy != null || fieldsEnclosedBy != null
|| fieldsEscapedBy != null;
}

public boolean hasLinesClause() {
return linesStartingBy != null || linesTerminatedBy != null;
}

public StringBuilder appendTo(StringBuilder builder) {
builder.append("INTO ").append(type);
appendFileName(builder);
appendCharacterSet(builder);
appendFieldsClause(builder);
appendLinesClause(builder);
return builder;
}

private void appendFileName(StringBuilder builder) {
if (fileName != null) {
builder.append(" ").append(fileName);
}
}

private void appendCharacterSet(StringBuilder builder) {
if (characterSet != null) {
builder.append(" CHARACTER SET ").append(characterSet);
}
}

private void appendFieldsClause(StringBuilder builder) {
if (!hasFieldsClause()) {
return;
}

builder.append(" ").append(fieldsKeyword != null ? fieldsKeyword : FieldsKeyword.FIELDS);

if (fieldsTerminatedBy != null) {
builder.append(" TERMINATED BY ").append(fieldsTerminatedBy);
}
if (fieldsEnclosedBy != null) {
builder.append(" ");
if (fieldsOptionallyEnclosed) {
builder.append("OPTIONALLY ");
}
builder.append("ENCLOSED BY ").append(fieldsEnclosedBy);
}
if (fieldsEscapedBy != null) {
builder.append(" ESCAPED BY ").append(fieldsEscapedBy);
}
}

private void appendLinesClause(StringBuilder builder) {
if (!hasLinesClause()) {
return;
}

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();
}
}
25 changes: 25 additions & 0 deletions src/main/java/net/sf/jsqlparser/statement/select/PlainSelect.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class PlainSelect extends Select {
private BigQuerySelectQualifier bigQuerySelectQualifier = null;
private List<SelectItem<?>> selectItems;
private List<Table> intoTables;
private MySqlSelectIntoClause mySqlSelectIntoClause;
private FromItem fromItem;
private List<LateralView> lateralViews;
private List<Join> joins;
Expand Down Expand Up @@ -142,6 +143,14 @@ public void setIntoTables(List<Table> intoTables) {
this.intoTables = intoTables;
}

public MySqlSelectIntoClause getMySqlSelectIntoClause() {
return mySqlSelectIntoClause;
}

public void setMySqlSelectIntoClause(MySqlSelectIntoClause mySqlSelectIntoClause) {
this.mySqlSelectIntoClause = mySqlSelectIntoClause;
}

public List<SelectItem<?>> getSelectItems() {
return selectItems;
}
Expand Down Expand Up @@ -564,6 +573,12 @@ public StringBuilder appendSelectBodyTo(StringBuilder builder) {
}
}

if (mySqlSelectIntoClause != null
&& mySqlSelectIntoClause
.getPosition() == MySqlSelectIntoClause.Position.BEFORE_FROM) {
builder.append(" ").append(mySqlSelectIntoClause);
}

if (fromItem != null) {
builder.append(" FROM ");
if (isUsingOnly) {
Expand Down Expand Up @@ -646,6 +661,11 @@ public String toString() {
StringBuilder builder = new StringBuilder();
super.appendTo(builder);

if (mySqlSelectIntoClause != null
&& mySqlSelectIntoClause.getPosition() == MySqlSelectIntoClause.Position.TRAILING) {
builder.append(" ").append(mySqlSelectIntoClause);
}

if (settings != null && !settings.isEmpty()) {
builder.append(" SETTINGS ");
UpdateSet.appendUpdateSetsTo(builder, settings);
Expand Down Expand Up @@ -698,6 +718,11 @@ public PlainSelect withIntoTables(List<Table> intoTables) {
return this;
}

public PlainSelect withMySqlSelectIntoClause(MySqlSelectIntoClause mySqlSelectIntoClause) {
this.setMySqlSelectIntoClause(mySqlSelectIntoClause);
return this;
}

public PlainSelect withWhere(Expression where) {
this.setWhere(where);
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,18 @@ public <S> T visit(PlainSelect plainSelect, S context) {
selectItem.accept(selectItemVisitor, context);
}

if (plainSelect.getMySqlSelectIntoClause() != null) {
MySqlSelectIntoClause mySqlSelectIntoClause = plainSelect.getMySqlSelectIntoClause();
expressionVisitor.visitExpression(mySqlSelectIntoClause.getFileName(), context);
expressionVisitor.visitExpression(mySqlSelectIntoClause.getFieldsTerminatedBy(),
context);
expressionVisitor.visitExpression(mySqlSelectIntoClause.getFieldsEnclosedBy(), context);
expressionVisitor.visitExpression(mySqlSelectIntoClause.getFieldsEscapedBy(), context);
expressionVisitor.visitExpression(mySqlSelectIntoClause.getLinesStartingBy(), context);
expressionVisitor.visitExpression(mySqlSelectIntoClause.getLinesTerminatedBy(),
context);
}

fromItemVisitor.visitTables(plainSelect.getIntoTables(), context);
fromItemVisitor.visitFromItem(plainSelect.getFromItem(), context);

Expand Down
12 changes: 12 additions & 0 deletions src/main/java/net/sf/jsqlparser/util/deparser/SelectDeParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import net.sf.jsqlparser.statement.select.Join;
import net.sf.jsqlparser.statement.select.LateralSubSelect;
import net.sf.jsqlparser.statement.select.LateralView;
import net.sf.jsqlparser.statement.select.MySqlSelectIntoClause;
import net.sf.jsqlparser.statement.select.Offset;
import net.sf.jsqlparser.statement.select.OptimizeFor;
import net.sf.jsqlparser.statement.select.OrderByElement;
Expand Down Expand Up @@ -250,6 +251,12 @@ public <S> StringBuilder visit(PlainSelect plainSelect, S context) {
}
}

if (plainSelect.getMySqlSelectIntoClause() != null
&& plainSelect.getMySqlSelectIntoClause()
.getPosition() == MySqlSelectIntoClause.Position.BEFORE_FROM) {
builder.append(" ").append(plainSelect.getMySqlSelectIntoClause());
}

if (plainSelect.getFromItem() != null) {
builder.append(" FROM ");
if (plainSelect.isUsingOnly()) {
Expand Down Expand Up @@ -369,6 +376,11 @@ public <S> StringBuilder visit(PlainSelect plainSelect, S context) {
builder.append(" SKIP LOCKED");
}
}
if (plainSelect.getMySqlSelectIntoClause() != null
&& plainSelect.getMySqlSelectIntoClause()
.getPosition() == MySqlSelectIntoClause.Position.TRAILING) {
builder.append(" ").append(plainSelect.getMySqlSelectIntoClause());
}
if (plainSelect.getSettings() != null && !plainSelect.getSettings().isEmpty()) {
builder.append(" SETTINGS ");
deparseUpdateSets(plainSelect.getSettings(), builder, expressionVisitor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
package net.sf.jsqlparser.util.validation.validator;

import java.util.List;

import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.MySQLIndexHint;
import net.sf.jsqlparser.expression.SQLServerHints;
Expand All @@ -26,6 +25,7 @@
import net.sf.jsqlparser.statement.select.Join;
import net.sf.jsqlparser.statement.select.LateralSubSelect;
import net.sf.jsqlparser.statement.select.MinusOp;
import net.sf.jsqlparser.statement.select.MySqlSelectIntoClause;
import net.sf.jsqlparser.statement.select.Offset;
import net.sf.jsqlparser.statement.select.ParenthesedFromItem;
import net.sf.jsqlparser.statement.select.ParenthesedSelect;
Expand Down Expand Up @@ -122,6 +122,8 @@ public <S> Void visit(PlainSelect plainSelect, S context) {
validateOptionalExpression(plainSelect.getPreWhere());
validateOptionalExpression(plainSelect.getWhere());
validateOptionalExpression(plainSelect.getOracleHierarchical());
validateOptional(plainSelect.getMySqlSelectIntoClause(),
this::validateMySqlSelectIntoClause);

if (plainSelect.getGroupBy() != null) {
plainSelect.getGroupBy().accept(getValidator(GroupByValidator.class), context);
Expand All @@ -147,6 +149,15 @@ public <S> Void visit(PlainSelect plainSelect, S context) {
return null;
}

private void validateMySqlSelectIntoClause(MySqlSelectIntoClause mySqlSelectIntoClause) {
validateOptionalExpression(mySqlSelectIntoClause.getFileName());
validateOptionalExpression(mySqlSelectIntoClause.getFieldsTerminatedBy());
validateOptionalExpression(mySqlSelectIntoClause.getFieldsEnclosedBy());
validateOptionalExpression(mySqlSelectIntoClause.getFieldsEscapedBy());
validateOptionalExpression(mySqlSelectIntoClause.getLinesStartingBy());
validateOptionalExpression(mySqlSelectIntoClause.getLinesTerminatedBy());
}

@Override
public <S> Void visit(SelectItem<?> selectExpressionItem, S context) {
selectExpressionItem.getExpression().accept(getValidator(ExpressionValidator.class),
Expand Down
Loading