Skip to content

Commit

Permalink
Add new class OtherLiteralValue to decouple StringLiteralValue (#4224)
Browse files Browse the repository at this point in the history
* Add new class OtherLiteralValue to decouple StringLiteralValue

* move substring delimiter of string into StringLiteralValue
  • Loading branch information
terrymanu committed Feb 10, 2020
1 parent e7340f9 commit a36ae84
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.shardingsphere.sql.parser.sql.value.literal.impl;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.sql.parser.sql.value.literal.LiteralValue;

/**
* Other literal value.
*
* @author zhangliang
*/
@RequiredArgsConstructor
@Getter
public final class OtherLiteralValue implements LiteralValue<String> {

private final String value;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,19 @@
package org.apache.shardingsphere.sql.parser.sql.value.literal.impl;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.sql.parser.sql.value.literal.LiteralValue;

/**
* String literal value.
*
* @author panjuan
*/
@RequiredArgsConstructor
@Getter
public final class StringLiteralValue implements LiteralValue<String> {

private final String value;

public StringLiteralValue(final String value) {
this.value = value.substring(1, value.length() - 1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ hexadecimalLiterals
bitValueLiterals
: characterSetName_? BIT_NUM_ collateClause_?
;

booleanLiterals
: TRUE | FALSE
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementBaseVisitor;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.AggregationFunctionContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.BitExprContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.BitValueLiteralsContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.BooleanLiteralsContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.BooleanPrimaryContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.CastFunctionContext;
Expand All @@ -32,14 +33,17 @@
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.ColumnNamesContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.ConvertFunctionContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.DataTypeNameContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.DateTimeLiteralsContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.ExprContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.ExtractFunctionContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.FunctionCallContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.GroupConcatFunctionContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.HexadecimalLiteralsContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.IdentifierContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.IndexNameContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.IntervalExpressionContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.LiteralsContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.NullValueLiteralsContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.NumberLiteralsContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.OrderByClauseContext;
import org.apache.shardingsphere.sql.parser.autogen.MySQLStatementParser.OrderByItemContext;
Expand Down Expand Up @@ -85,11 +89,12 @@
import org.apache.shardingsphere.sql.parser.sql.segment.dml.predicate.value.PredicateRightValue;
import org.apache.shardingsphere.sql.parser.sql.segment.generic.SchemaSegment;
import org.apache.shardingsphere.sql.parser.sql.segment.generic.TableSegment;
import org.apache.shardingsphere.sql.parser.sql.value.literal.impl.BooleanLiteralValue;
import org.apache.shardingsphere.sql.parser.sql.value.collection.CollectionValue;
import org.apache.shardingsphere.sql.parser.sql.value.identifier.IdentifierValue;
import org.apache.shardingsphere.sql.parser.sql.value.literal.impl.StringLiteralValue;
import org.apache.shardingsphere.sql.parser.sql.value.literal.impl.BooleanLiteralValue;
import org.apache.shardingsphere.sql.parser.sql.value.literal.impl.NumberLiteralValue;
import org.apache.shardingsphere.sql.parser.sql.value.literal.impl.OtherLiteralValue;
import org.apache.shardingsphere.sql.parser.sql.value.literal.impl.StringLiteralValue;
import org.apache.shardingsphere.sql.parser.sql.value.parametermarker.ParameterMarkerValue;
import org.apache.shardingsphere.sql.parser.util.SQLUtil;

Expand Down Expand Up @@ -120,33 +125,63 @@ public final ASTNode visitLiterals(final LiteralsContext ctx) {
if (null != ctx.numberLiterals()) {
return visit(ctx.numberLiterals());
}
if (null != ctx.dateTimeLiterals()) {
return visit(ctx.dateTimeLiterals());
}
if (null != ctx.hexadecimalLiterals()) {
return visit(ctx.hexadecimalLiterals());
}
if (null != ctx.bitValueLiterals()) {
return visit(ctx.bitValueLiterals());
}
if (null != ctx.booleanLiterals()) {
return visit(ctx.booleanLiterals());
}
if (null != ctx.nullValueLiterals()) {
// TODO deal with null value
return new CommonExpressionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), ctx.getText());
return visit(ctx.nullValueLiterals());
}
// TODO deal with dateTimeLiterals, hexadecimalLiterals and bitValueLiterals
return new StringLiteralValue(ctx.getText());
throw new IllegalStateException("Literals must have string, number, dateTime, hex, bit, boolean or null.");
}

@Override
public final ASTNode visitStringLiterals(final StringLiteralsContext ctx) {
String text = ctx.getText();
return new StringLiteralValue(text.substring(1, text.length() - 1));
return new StringLiteralValue(ctx.getText());
}

@Override
public final ASTNode visitNumberLiterals(final NumberLiteralsContext ctx) {
return new NumberLiteralValue(ctx.getText());
}

@Override
public final ASTNode visitDateTimeLiterals(final DateTimeLiteralsContext ctx) {
// TODO deal with dateTimeLiterals
return new OtherLiteralValue(ctx.getText());
}

@Override
public final ASTNode visitHexadecimalLiterals(final HexadecimalLiteralsContext ctx) {
// TODO deal with hexadecimalLiterals
return new OtherLiteralValue(ctx.getText());
}

@Override
public final ASTNode visitBitValueLiterals(final BitValueLiteralsContext ctx) {
// TODO deal with bitValueLiterals
return new OtherLiteralValue(ctx.getText());
}

@Override
public final ASTNode visitBooleanLiterals(final BooleanLiteralsContext ctx) {
return new BooleanLiteralValue(ctx.getText());
}

@Override
public final ASTNode visitNullValueLiterals(final NullValueLiteralsContext ctx) {
// TODO deal with nullValueLiterals
return new OtherLiteralValue(ctx.getText());
}

@Override
public final ASTNode visitIdentifier(final IdentifierContext ctx) {
UnreservedWord_Context unreservedWord = ctx.unreservedWord_();
Expand Down Expand Up @@ -305,9 +340,8 @@ private ASTNode visitRemainPredicate(final PredicateContext ctx) {

@Override
public final ASTNode visitBitExpr(final BitExprContext ctx) {
SimpleExprContext simple = ctx.simpleExpr();
if (null != simple) {
return createExpressionSegment(visit(simple), ctx);
if (null != ctx.simpleExpr()) {
return createExpressionSegment(visit(ctx.simpleExpr()), ctx);
}
return new CommonExpressionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), ctx.getText());
}
Expand All @@ -322,6 +356,9 @@ private ASTNode createExpressionSegment(final ASTNode astNode, final ParserRuleC
if (astNode instanceof ParameterMarkerValue) {
return new ParameterMarkerExpressionSegment(context.start.getStartIndex(), context.stop.getStopIndex(), ((ParameterMarkerValue) astNode).getValue());
}
if (astNode instanceof OtherLiteralValue) {
return new CommonExpressionSegment(context.getStart().getStartIndex(), context.getStop().getStopIndex(), context.getText());
}
return astNode;
}

Expand Down

0 comments on commit a36ae84

Please sign in to comment.