Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add insert statement parser #317

Merged
merged 1 commit into from
Jun 17, 2021
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,127 @@
/*
* Copyright 2017-2021 Dromara.org
*
* Licensed 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.dromara.hmily.tac.sqlparser.shardingsphere.common.handler;

import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.ColumnSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.BetweenExpression;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.BinaryOperationExpression;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.ExpressionSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.InExpression;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.complex.CommonExpressionSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.simple.LiteralExpressionSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.simple.ParameterMarkerExpressionSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.item.ExpressionProjectionSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.OwnerSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.TableNameSegment;
import org.dromara.hmily.tac.sqlparser.model.common.segment.dml.column.HmilyColumnSegment;
import org.dromara.hmily.tac.sqlparser.model.common.segment.dml.expr.HmilyBinaryOperationExpression;
import org.dromara.hmily.tac.sqlparser.model.common.segment.dml.expr.HmilyExpressionSegment;
import org.dromara.hmily.tac.sqlparser.model.common.segment.dml.expr.complex.HmilyCommonExpressionSegment;
import org.dromara.hmily.tac.sqlparser.model.common.segment.dml.expr.simple.HmilyLiteralExpressionSegment;
import org.dromara.hmily.tac.sqlparser.model.common.segment.dml.expr.simple.HmilyParameterMarkerExpressionSegment;
import org.dromara.hmily.tac.sqlparser.model.common.segment.dml.item.HmilyExpressionProjectionSegment;
import org.dromara.hmily.tac.sqlparser.model.common.segment.generic.HmilyAliasSegment;
import org.dromara.hmily.tac.sqlparser.model.common.segment.generic.HmilyOwnerSegment;
import org.dromara.hmily.tac.sqlparser.model.common.segment.generic.table.HmilySimpleTableSegment;
import org.dromara.hmily.tac.sqlparser.model.common.segment.generic.table.HmilyTableNameSegment;
import org.dromara.hmily.tac.sqlparser.model.common.value.identifier.HmilyIdentifierValue;

public final class CommonAssembler {

/**
* Assemble hmily simple table segment.
*
* @param simpleTableSegment simple table segment
* @return hmily simple table segment
*/
public static HmilySimpleTableSegment assembleHmilySimpleTableSegment(final SimpleTableSegment simpleTableSegment) {
TableNameSegment tableNameSegment = simpleTableSegment.getTableName();
HmilyIdentifierValue hmilyIdentifierValue = new HmilyIdentifierValue(tableNameSegment.getIdentifier().getValue());
HmilyTableNameSegment hmilyTableNameSegment = new HmilyTableNameSegment(tableNameSegment.getStartIndex(), tableNameSegment.getStopIndex(), hmilyIdentifierValue);
HmilyOwnerSegment hmilyOwnerSegment = null;
OwnerSegment ownerSegment;
if (simpleTableSegment.getOwner().isPresent()) {
ownerSegment = simpleTableSegment.getOwner().get();
hmilyOwnerSegment = new HmilyOwnerSegment(ownerSegment.getStartIndex(), ownerSegment.getStopIndex(),
new HmilyIdentifierValue(ownerSegment.getIdentifier().getValue()));
}
HmilyAliasSegment hmilyAliasSegment = null;
String aliasSegmentString;
if (simpleTableSegment.getAlias().isPresent()) {
aliasSegmentString = simpleTableSegment.getAlias().get();
hmilyAliasSegment = new HmilyAliasSegment(0, 0, new HmilyIdentifierValue(aliasSegmentString));
}
HmilySimpleTableSegment hmilySimpleTableSegment = new HmilySimpleTableSegment(hmilyTableNameSegment);
hmilySimpleTableSegment.setOwner(hmilyOwnerSegment);
hmilySimpleTableSegment.setAlias(hmilyAliasSegment);
return hmilySimpleTableSegment;
}

/**
* Assemble hmily column segment.
*
* @param column column
* @return hmily column segment
*/
public static HmilyColumnSegment assembleHmilyColumnSegment(final ColumnSegment column) {
HmilyIdentifierValue hmilyIdentifierValue = new HmilyIdentifierValue(column.getIdentifier().getValue());
HmilyColumnSegment result = new HmilyColumnSegment(column.getStartIndex(), column.getStopIndex(), hmilyIdentifierValue);
column.getOwner().ifPresent(ownerSegment -> {
HmilyIdentifierValue identifierValue = new HmilyIdentifierValue(ownerSegment.getIdentifier().getValue());
result.setOwner(new HmilyOwnerSegment(ownerSegment.getStartIndex(), ownerSegment.getStopIndex(), identifierValue));
});
return result;
}

/**
* Assemble hmily expression segment.
*
* @param expression expression
* @return hmily expression segment
*/
public static HmilyExpressionSegment assembleHmilyExpressionSegment(final ExpressionSegment expression) {
HmilyExpressionSegment result = null;
if (expression instanceof BinaryOperationExpression) {
HmilyExpressionSegment hmilyLeft = assembleHmilyExpressionSegment(((BinaryOperationExpression) expression).getLeft());
HmilyExpressionSegment hmilyRight = assembleHmilyExpressionSegment(((BinaryOperationExpression) expression).getRight());
result = new HmilyBinaryOperationExpression(expression.getStartIndex(), expression.getStopIndex(), hmilyLeft, hmilyRight,
((BinaryOperationExpression) expression).getOperator(), ((BinaryOperationExpression) expression).getText());
} else if (expression instanceof ColumnSegment) {
result = CommonAssembler.assembleHmilyColumnSegment((ColumnSegment) expression);
} else if (expression instanceof CommonExpressionSegment) {
result = new HmilyCommonExpressionSegment(expression.getStartIndex(),
expression.getStopIndex(), ((CommonExpressionSegment) expression).getText());
} else if (expression instanceof ExpressionProjectionSegment) {
result = new HmilyExpressionProjectionSegment(expression.getStartIndex(),
expression.getStopIndex(), ((ExpressionProjectionSegment) expression).getText());
} else if (expression instanceof LiteralExpressionSegment) {
result = new HmilyLiteralExpressionSegment(expression.getStartIndex(),
expression.getStopIndex(), ((LiteralExpressionSegment) expression).getLiterals());
} else if (expression instanceof ParameterMarkerExpressionSegment) {
result = new HmilyParameterMarkerExpressionSegment(expression.getStartIndex(),
expression.getStopIndex(), ((ParameterMarkerExpressionSegment) expression).getParameterMarkerIndex());
} else if (expression instanceof InExpression && ((InExpression) expression).getLeft() instanceof ColumnSegment) {
// TODO
ColumnSegment column = (ColumnSegment) ((InExpression) expression).getLeft();
} else if (expression instanceof BetweenExpression && ((BetweenExpression) expression).getLeft() instanceof ColumnSegment) {
// TODO
ColumnSegment column = (ColumnSegment) ((BetweenExpression) expression).getLeft();
}
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,22 @@

package org.dromara.hmily.tac.sqlparser.shardingsphere.common.handler;

import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.assignment.InsertValuesSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.ColumnSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.column.InsertColumnsSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.ExpressionSegment;
import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.InsertStatement;
import org.dromara.hmily.tac.sqlparser.model.common.segment.dml.assignment.HmilyInsertValuesSegment;
import org.dromara.hmily.tac.sqlparser.model.common.segment.dml.column.HmilyColumnSegment;
import org.dromara.hmily.tac.sqlparser.model.common.segment.dml.column.HmilyInsertColumnsSegment;
import org.dromara.hmily.tac.sqlparser.model.common.segment.dml.expr.HmilyExpressionSegment;
import org.dromara.hmily.tac.sqlparser.model.common.segment.generic.table.HmilySimpleTableSegment;
import org.dromara.hmily.tac.sqlparser.model.common.statement.dml.HmilyInsertStatement;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

public final class InsertStatementAssembler {

/**
Expand All @@ -29,6 +42,37 @@ public final class InsertStatementAssembler {
* @return hmily insert statement
*/
public static HmilyInsertStatement assembleHmilyInsertStatement(final InsertStatement insertStatement, final HmilyInsertStatement hmilyInsertStatement) {
HmilySimpleTableSegment hmilySimpleTableSegment = CommonAssembler.assembleHmilySimpleTableSegment(insertStatement.getTable());
HmilyInsertColumnsSegment hmilyInsertColumnsSegment = null;
if (insertStatement.getInsertColumns().isPresent()) {
hmilyInsertColumnsSegment = assembleHmilyInsertColumnsSegment(insertStatement.getInsertColumns().get());
}
Collection<HmilyInsertValuesSegment> hmilyInsertValuesSegments = assembleHmilyInsertValuesSegments(insertStatement.getValues());
hmilyInsertStatement.setTable(hmilySimpleTableSegment);
hmilyInsertStatement.setInsertColumns(hmilyInsertColumnsSegment);
for (HmilyInsertValuesSegment each : hmilyInsertValuesSegments) {
hmilyInsertStatement.getValues().add(each);
}
return hmilyInsertStatement;
}

private static HmilyInsertColumnsSegment assembleHmilyInsertColumnsSegment(final InsertColumnsSegment insertColumnsSegment) {
Collection<HmilyColumnSegment> hmilyColumnSegments = new LinkedList<>();
for (ColumnSegment each : insertColumnsSegment.getColumns()) {
hmilyColumnSegments.add(CommonAssembler.assembleHmilyColumnSegment(each));
}
return new HmilyInsertColumnsSegment(insertColumnsSegment.getStartIndex(), insertColumnsSegment.getStopIndex(), hmilyColumnSegments);
}

private static Collection<HmilyInsertValuesSegment> assembleHmilyInsertValuesSegments(final Collection<InsertValuesSegment> insertValuesSegments) {
Collection<HmilyInsertValuesSegment> hmilyInsertValuesSegments = new LinkedList<>();
for (InsertValuesSegment each : insertValuesSegments) {
List<HmilyExpressionSegment> hmilyExpressionSegments = new LinkedList<>();
for (ExpressionSegment expressionSegment : each.getValues()) {
hmilyExpressionSegments.add(CommonAssembler.assembleHmilyExpressionSegment(expressionSegment));
}
hmilyInsertValuesSegments.add(new HmilyInsertValuesSegment(each.getStartIndex(), each.getStopIndex(), hmilyExpressionSegments));
}
return hmilyInsertValuesSegments;
}
}
Loading