Skip to content

Commit

Permalink
Refactor SQLBindEngine (#32041)
Browse files Browse the repository at this point in the history
* Refactor SQLBindEngine

* Refactor SQLBindEngine
  • Loading branch information
terrymanu committed Jul 9, 2024
1 parent 731ff75 commit 1050fd5
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,13 @@
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContextFactory;
import org.apache.shardingsphere.infra.binder.statement.ddl.CursorStatementBinder;
import org.apache.shardingsphere.infra.binder.statement.dml.DeleteStatementBinder;
import org.apache.shardingsphere.infra.binder.statement.dml.InsertStatementBinder;
import org.apache.shardingsphere.infra.binder.statement.dml.MergeStatementBinder;
import org.apache.shardingsphere.infra.binder.statement.dml.SelectStatementBinder;
import org.apache.shardingsphere.infra.binder.statement.dml.UpdateStatementBinder;
import org.apache.shardingsphere.infra.binder.type.DDLStatementBindEngine;
import org.apache.shardingsphere.infra.binder.type.DMLStatementBindEngine;
import org.apache.shardingsphere.infra.hint.HintValueContext;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.sql.parser.statement.core.statement.SQLStatement;
import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.CursorStatement;
import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.DDLStatement;
import org.apache.shardingsphere.sql.parser.statement.core.statement.dml.DMLStatement;
import org.apache.shardingsphere.sql.parser.statement.core.statement.dml.DeleteStatement;
import org.apache.shardingsphere.sql.parser.statement.core.statement.dml.InsertStatement;
import org.apache.shardingsphere.sql.parser.statement.core.statement.dml.MergeStatement;
import org.apache.shardingsphere.sql.parser.statement.core.statement.dml.SelectStatement;
import org.apache.shardingsphere.sql.parser.statement.core.statement.dml.UpdateStatement;

import java.util.List;

Expand All @@ -53,51 +43,27 @@ public final class SQLBindEngine {
private final HintValueContext hintValueContext;

/**
* Bind SQL statement with metadata.
* Bind SQL statement.
*
* @param sqlStatement SQL statement
* @param params parameters
* @return SQL statement context
*/
public SQLStatementContext bind(final SQLStatement sqlStatement, final List<Object> params) {
return SQLStatementContextFactory.newInstance(metaData, bind(sqlStatement), params, currentDatabaseName);
SQLStatement boundSQLStatement = isNeedBind() ? bindSQLStatement(sqlStatement) : sqlStatement;
return SQLStatementContextFactory.newInstance(metaData, boundSQLStatement, params, currentDatabaseName);
}

private SQLStatement bind(final SQLStatement statement) {
if (hintValueContext.findHintDataSourceName().isPresent()) {
return statement;
}
if (statement instanceof DMLStatement) {
return bindDMLStatement(statement);
}
if (statement instanceof DDLStatement) {
return bindDDLStatement(statement);
}
return statement;
private boolean isNeedBind() {
return !hintValueContext.findHintDataSourceName().isPresent();
}

private SQLStatement bindDMLStatement(final SQLStatement statement) {
if (statement instanceof SelectStatement) {
return new SelectStatementBinder().bind((SelectStatement) statement, metaData, currentDatabaseName);
}
if (statement instanceof InsertStatement) {
return new InsertStatementBinder().bind((InsertStatement) statement, metaData, currentDatabaseName);
}
if (statement instanceof UpdateStatement) {
return new UpdateStatementBinder().bind((UpdateStatement) statement, metaData, currentDatabaseName);
}
if (statement instanceof DeleteStatement) {
return new DeleteStatementBinder().bind((DeleteStatement) statement, metaData, currentDatabaseName);
}
if (statement instanceof MergeStatement) {
return new MergeStatementBinder().bind((MergeStatement) statement, metaData, currentDatabaseName);
private SQLStatement bindSQLStatement(final SQLStatement statement) {
if (statement instanceof DMLStatement) {
return new DMLStatementBindEngine(metaData, currentDatabaseName).bind((DMLStatement) statement);
}
return statement;
}

private SQLStatement bindDDLStatement(final SQLStatement statement) {
if (statement instanceof CursorStatement) {
return new CursorStatementBinder().bind((CursorStatement) statement, metaData, currentDatabaseName);
if (statement instanceof DDLStatement) {
return new DDLStatementBindEngine(metaData, currentDatabaseName).bind((DDLStatement) statement);
}
return statement;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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.infra.binder.type;

import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.infra.binder.statement.ddl.CursorStatementBinder;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.CursorStatement;
import org.apache.shardingsphere.sql.parser.statement.core.statement.ddl.DDLStatement;

/**
* DDL statement bind engine.
*/
@RequiredArgsConstructor
public final class DDLStatementBindEngine {

private final ShardingSphereMetaData metaData;

private final String currentDatabaseName;

/**
* Bind DDL statement.
*
* @param statement to be bound DDL statement
* @return bound DDL statement
*/
public DDLStatement bind(final DDLStatement statement) {
if (statement instanceof CursorStatement) {
return new CursorStatementBinder().bind((CursorStatement) statement, metaData, currentDatabaseName);
}
return statement;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* 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.infra.binder.type;

import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.infra.binder.statement.dml.DeleteStatementBinder;
import org.apache.shardingsphere.infra.binder.statement.dml.InsertStatementBinder;
import org.apache.shardingsphere.infra.binder.statement.dml.MergeStatementBinder;
import org.apache.shardingsphere.infra.binder.statement.dml.SelectStatementBinder;
import org.apache.shardingsphere.infra.binder.statement.dml.UpdateStatementBinder;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.sql.parser.statement.core.statement.dml.DMLStatement;
import org.apache.shardingsphere.sql.parser.statement.core.statement.dml.DeleteStatement;
import org.apache.shardingsphere.sql.parser.statement.core.statement.dml.InsertStatement;
import org.apache.shardingsphere.sql.parser.statement.core.statement.dml.MergeStatement;
import org.apache.shardingsphere.sql.parser.statement.core.statement.dml.SelectStatement;
import org.apache.shardingsphere.sql.parser.statement.core.statement.dml.UpdateStatement;

/**
* DML statement bind engine.
*/
@RequiredArgsConstructor
public final class DMLStatementBindEngine {

private final ShardingSphereMetaData metaData;

private final String currentDatabaseName;

/**
* Bind DML statement.
*
* @param statement to be bound DML statement
* @return bound DML statement
*/
public DMLStatement bind(final DMLStatement statement) {
if (statement instanceof SelectStatement) {
return new SelectStatementBinder().bind((SelectStatement) statement, metaData, currentDatabaseName);
}
if (statement instanceof InsertStatement) {
return new InsertStatementBinder().bind((InsertStatement) statement, metaData, currentDatabaseName);
}
if (statement instanceof UpdateStatement) {
return new UpdateStatementBinder().bind((UpdateStatement) statement, metaData, currentDatabaseName);
}
if (statement instanceof DeleteStatement) {
return new DeleteStatementBinder().bind((DeleteStatement) statement, metaData, currentDatabaseName);
}
if (statement instanceof MergeStatement) {
return new MergeStatementBinder().bind((MergeStatement) statement, metaData, currentDatabaseName);
}
return statement;
}
}

0 comments on commit 1050fd5

Please sign in to comment.