linghengqian commented on code in PR #37778:
URL: https://github.com/apache/shardingsphere/pull/37778#discussion_r2706776879


##########
jdbc/src/main/java/org/apache/shardingsphere/driver/executor/engine/distsql/DistSQLResultSet.java:
##########
@@ -0,0 +1,265 @@
+/*
+ * 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.driver.executor.engine.distsql;
+
+import com.google.common.base.Preconditions;
+import 
org.apache.shardingsphere.driver.jdbc.unsupported.AbstractUnsupportedGeneratedKeysResultSet;
+import 
org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
+
+import java.math.BigDecimal;
+import java.math.RoundingMode;
+import java.nio.charset.StandardCharsets;
+import java.sql.ResultSetMetaData;
+import java.sql.Statement;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
+
+/**
+ * DistSQL result set.
+ */
+public final class DistSQLResultSet extends 
AbstractUnsupportedGeneratedKeysResultSet {
+    
+    private final List<String> columnNames;
+    
+    private final Iterator<LocalDataQueryResultRow> rows;
+    
+    private final Statement statement;
+    
+    private LocalDataQueryResultRow currentRow;
+    
+    private boolean closed;
+    
+    public DistSQLResultSet(final Collection<String> columnNames, final 
Collection<LocalDataQueryResultRow> rows, final Statement statement) {
+        this.columnNames = new ArrayList<>(columnNames);
+        this.rows = rows.iterator();
+        this.statement = statement;
+    }
+    
+    @Override
+    public boolean isClosed() {
+        return closed;
+    }
+    
+    @Override
+    public boolean next() {
+        if (closed || !rows.hasNext()) {
+            currentRow = null;
+            return false;
+        }
+        currentRow = rows.next();
+        return true;
+    }
+    
+    @Override
+    public void close() {
+        closed = true;
+    }
+    
+    @Override
+    public ResultSetMetaData getMetaData() {
+        checkState();
+        return new DistSQLResultSetMetaData(columnNames);
+    }
+    
+    @Override
+    public boolean wasNull() {
+        checkState();
+        return false;

Review Comment:
   Fixed.



##########
jdbc/src/main/java/org/apache/shardingsphere/driver/executor/engine/distsql/DriverDistSQLExecutor.java:
##########
@@ -0,0 +1,120 @@
+/*
+ * 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.driver.executor.engine.distsql;
+
+import lombok.Getter;
+import lombok.RequiredArgsConstructor;
+import 
org.apache.shardingsphere.distsql.handler.engine.DistSQLConnectionContext;
+import 
org.apache.shardingsphere.distsql.handler.engine.query.DistSQLQueryExecuteEngine;
+import 
org.apache.shardingsphere.distsql.handler.engine.update.DistSQLUpdateExecuteEngine;
+import org.apache.shardingsphere.distsql.statement.DistSQLStatement;
+import 
org.apache.shardingsphere.distsql.statement.type.ral.queryable.QueryableRALStatement;
+import org.apache.shardingsphere.distsql.statement.type.rql.RQLStatement;
+import org.apache.shardingsphere.distsql.statement.type.rul.RULStatement;
+import 
org.apache.shardingsphere.driver.jdbc.core.connection.ShardingSphereConnection;
+import org.apache.shardingsphere.infra.session.query.QueryContext;
+import org.apache.shardingsphere.mode.manager.ContextManager;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+
+/**
+ * Driver DistSQL executor.
+ */
+@RequiredArgsConstructor
+public final class DriverDistSQLExecutor {
+    
+    private final ShardingSphereConnection connection;
+    
+    /**
+     * Execute DistSQL query.
+     *
+     * @param sqlStatement DistSQL statement
+     * @param queryContext query context
+     * @param statement statement
+     * @return result set
+     * @throws SQLException SQL exception
+     */
+    public ResultSet executeQuery(final DistSQLStatement sqlStatement, final 
QueryContext queryContext, final Statement statement) throws SQLException {
+        ContextManager contextManager = connection.getContextManager();
+        DistSQLConnectionContext distsqlConnectionContext = 
createDistSQLConnectionContext(queryContext);
+        DistSQLQueryExecuteEngine engine = new 
DistSQLQueryExecuteEngine(sqlStatement, connection.getCurrentDatabaseName(), 
contextManager, distsqlConnectionContext);
+        engine.executeQuery();
+        return new DistSQLResultSet(engine.getColumnNames(), engine.getRows(), 
statement);
+    }
+    
+    /**
+     * Execute DistSQL update.
+     *
+     * @param sqlStatement DistSQL statement
+     * @param queryContext query context
+     * @return update count
+     * @throws SQLException SQL exception
+     */
+    public int executeUpdate(final DistSQLStatement sqlStatement, final 
QueryContext queryContext) throws SQLException {
+        ContextManager contextManager = connection.getContextManager();
+        DistSQLConnectionContext distsqlConnectionContext = 
createDistSQLConnectionContext(queryContext);
+        DistSQLUpdateExecuteEngine engine = new 
DistSQLUpdateExecuteEngine(sqlStatement, connection.getCurrentDatabaseName(), 
contextManager, distsqlConnectionContext);
+        engine.executeUpdate();
+        return 0;
+    }
+    
+    /**
+     * Execute DistSQL.
+     *
+     * @param sqlStatement DistSQL statement
+     * @param queryContext query context
+     * @param statement statement
+     * @return execute result
+     * @throws SQLException SQL exception
+     */
+    public ExecuteResult execute(final DistSQLStatement sqlStatement, final 
QueryContext queryContext, final Statement statement) throws SQLException {
+        if (isQueryStatement(sqlStatement)) {
+            ResultSet resultSet = executeQuery(sqlStatement, queryContext, 
statement);
+            return new ExecuteResult(true, resultSet, 0);
+        }
+        int updateCount = executeUpdate(sqlStatement, queryContext);
+        return new ExecuteResult(false, null, updateCount);
+    }
+    
+    private boolean isQueryStatement(final DistSQLStatement sqlStatement) {
+        return sqlStatement instanceof RQLStatement || sqlStatement instanceof 
RULStatement
+                || sqlStatement instanceof QueryableRALStatement;
+    }
+    
+    private DistSQLConnectionContext createDistSQLConnectionContext(final 
QueryContext queryContext) {
+        return new DistSQLConnectionContext(queryContext, 1,
+                
connection.getContextManager().getMetaDataContexts().getMetaData().getDatabase(connection.getCurrentDatabaseName()).getProtocolType(),
+                connection.getDatabaseConnectionManager(), null);
+    }
+    
+    /**
+     * Execute result.
+     */
+    @Getter
+    @RequiredArgsConstructor
+    public static final class ExecuteResult {
+        
+        private final boolean hasResultSet;
+        
+        private final ResultSet resultSet;
+        
+        private final int updateCount;
+    }
+}

Review Comment:
   Fixed.



##########
jdbc/src/main/java/org/apache/shardingsphere/driver/executor/engine/distsql/DistSQLResultSetMetaData.java:
##########
@@ -0,0 +1,166 @@
+/*
+ * 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.driver.executor.engine.distsql;
+
+import com.google.common.base.Preconditions;
+import org.apache.shardingsphere.driver.jdbc.adapter.WrapperAdapter;
+
+import java.sql.ResultSetMetaData;
+import java.sql.Types;
+import java.util.List;
+
+/**
+ * Result set meta data for DistSQL.
+ */
+public final class DistSQLResultSetMetaData extends WrapperAdapter implements 
ResultSetMetaData {
+    
+    private final List<String> columnNames;
+    
+    public DistSQLResultSetMetaData(final List<String> columnNames) {
+        this.columnNames = columnNames;
+    }
+    
+    @Override
+    public int getColumnCount() {
+        return columnNames.size();
+    }
+    
+    @Override
+    public boolean isAutoIncrement(final int column) {
+        checkColumnIndex(column);
+        return false;
+    }
+    
+    @Override
+    public boolean isCaseSensitive(final int column) {
+        checkColumnIndex(column);
+        return true;
+    }
+    
+    @Override
+    public boolean isSearchable(final int column) {
+        checkColumnIndex(column);
+        return false;
+    }
+    
+    @Override
+    public boolean isCurrency(final int column) {
+        checkColumnIndex(column);
+        return false;
+    }
+    
+    @Override
+    public int isNullable(final int column) {
+        checkColumnIndex(column);
+        return columnNullable;
+    }
+    
+    @Override
+    public boolean isSigned(final int column) {
+        checkColumnIndex(column);
+        return false;
+    }
+    
+    @Override
+    public int getColumnDisplaySize(final int column) {
+        checkColumnIndex(column);
+        return 255;
+    }
+    
+    @Override
+    public String getColumnLabel(final int column) {
+        checkColumnIndex(column);
+        return columnNames.get(column - 1);
+    }
+    
+    @Override
+    public String getColumnName(final int column) {
+        checkColumnIndex(column);
+        return columnNames.get(column - 1);
+    }
+    
+    @Override
+    public String getSchemaName(final int column) {
+        checkColumnIndex(column);
+        return "";
+    }
+    
+    @Override
+    public int getPrecision(final int column) {
+        checkColumnIndex(column);
+        return 0;
+    }
+    
+    @Override
+    public int getScale(final int column) {
+        checkColumnIndex(column);
+        return 0;
+    }
+    
+    @Override
+    public String getTableName(final int column) {
+        checkColumnIndex(column);
+        return "";
+    }
+    
+    @Override
+    public String getCatalogName(final int column) {
+        checkColumnIndex(column);
+        return "";
+    }
+    
+    @Override
+    public int getColumnType(final int column) {
+        checkColumnIndex(column);
+        return Types.CHAR;
+    }
+    
+    @Override
+    public String getColumnTypeName(final int column) {
+        checkColumnIndex(column);
+        return "CHAR";
+    }
+    
+    @Override
+    public boolean isReadOnly(final int column) {
+        checkColumnIndex(column);
+        return true;
+    }
+    
+    @Override
+    public boolean isWritable(final int column) {
+        checkColumnIndex(column);
+        return false;
+    }
+    
+    @Override
+    public boolean isDefinitelyWritable(final int column) {
+        checkColumnIndex(column);
+        return false;
+    }
+    
+    @Override
+    public String getColumnClassName(final int column) {
+        checkColumnIndex(column);
+        return String.class.getName();
+    }
+    
+    private void checkColumnIndex(final int column) {
+        Preconditions.checkArgument(column >= 1 && column <= 
columnNames.size(), "Column index %s is out of range [1, %s]", column, 
columnNames.size());
+    }
+}

Review Comment:
   Fixed.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to