-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
176 additions
and
88 deletions.
There are no files selected for viewing
127 changes: 127 additions & 0 deletions
127
...n/java/org/dromara/hmily/tac/sqlparser/shardingsphere/common/handler/CommonAssembler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.