Skip to content

Commit

Permalink
Add mysql charset sql parsing (#32282)
Browse files Browse the repository at this point in the history
* Add mysql charset sql parsing

* Add mysql charset sql parsing
  • Loading branch information
FlyingZC authored Jul 26, 2024
1 parent bc673ba commit 0efdc32
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.CaseStatementContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.ChangeColumnContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.CharsetNameContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.CollationNameContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.ColumnDefinitionContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.CompoundStatementContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.CreateDatabaseContext;
Expand Down Expand Up @@ -101,6 +102,7 @@
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.TableConstraintDefContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.TableElementContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.TableNameContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.TextOrIdentifierContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.TruncateTableContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.ValidStatementContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.WhileStatementContext;
Expand Down Expand Up @@ -279,6 +281,12 @@ public ASTNode visitCreateTableOptions(final CreateTableOptionsContext ctx) {
result.setEngine((EngineSegment) visit(each.engineRef()));
} else if (null != each.COMMENT()) {
result.setCommentSegment(new CommentSegment(each.string_().getText(), each.string_().getStart().getStartIndex(), each.string_().getStop().getStopIndex()));
} else if (null != each.defaultCharset()) {
Optional.ofNullable(each.defaultCharset().charsetName()).map(CharsetNameContext::textOrIdentifier).map(TextOrIdentifierContext::identifier)
.ifPresent(optional -> result.setCharsetName(optional.getText()));
} else if (null != each.defaultCollation()) {
Optional.ofNullable(each.defaultCollation().collationName()).map(CollationNameContext::textOrIdentifier).map(TextOrIdentifierContext::identifier)
.ifPresent(optional -> result.setCollateName(optional.getText()));
}
}
return result;
Expand Down Expand Up @@ -576,6 +584,11 @@ public ASTNode visitColumnDefinition(final ColumnDefinitionContext ctx) {
ColumnDefinitionSegment result = new ColumnDefinitionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), column, dataTypeSegment, isPrimaryKey, false);
result.getReferencedTables().addAll(getReferencedTables(ctx));
result.setAutoIncrement(isAutoIncrement);
if (null != ctx.fieldDefinition().dataType().charsetWithOptBinary()) {
result.setCharsetName(ctx.fieldDefinition().dataType().charsetWithOptBinary().charsetName().textOrIdentifier().identifier().IDENTIFIER_().getText());
}
ctx.fieldDefinition().columnAttribute().stream().filter(each -> each.collateClause() != null).findFirst()
.ifPresent(optional -> result.setCollateName(optional.collateClause().collationName().textOrIdentifier().identifier().IDENTIFIER_().getText()));
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,10 @@ public final class ColumnDefinitionSegment implements CreateDefinitionSegment {

@Setter
private boolean isRef;

@Setter
private String charsetName;

@Setter
private String collateName;
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ public final class CreateTableOptionSegment implements CreateDefinitionSegment {

private CommentSegment commentSegment;

private String charsetName;

private String collateName;

/**
* Get engine.
*
Expand Down

0 comments on commit 0efdc32

Please sign in to comment.