FangYongs commented on code in PR #22417:
URL: https://github.com/apache/flink/pull/22417#discussion_r1175092974


##########
flink-table/flink-sql-jdbc-driver/src/main/java/org/apache/flink/table/jdbc/FlinkStatement.java:
##########
@@ -0,0 +1,137 @@
+/*
+ * 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.flink.table.jdbc;
+
+import org.apache.flink.table.api.ResultKind;
+import org.apache.flink.table.client.gateway.Executor;
+import org.apache.flink.table.client.gateway.StatementResult;
+import org.apache.flink.table.jdbc.utils.StringDataConverter;
+
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.SQLFeatureNotSupportedException;
+import java.util.concurrent.atomic.AtomicReference;
+
+/** Statement for flink jdbc driver. */
+public class FlinkStatement extends BaseStatement {
+    private final Connection connection;
+    private final Executor executor;
+    private final AtomicReference<FlinkResultSet> currentResults;
+    private volatile boolean closed;
+
+    public FlinkStatement(FlinkConnection connection) {
+        this.connection = connection;
+        this.executor = connection.getExecutor();
+        this.currentResults = new AtomicReference<>();
+    }
+
+    @Override
+    public ResultSet executeQuery(String sql) throws SQLException {
+        if (!execute(sql)) {
+            throw new SQLException(String.format("Statement[%s] is not a 
query.", sql));
+        }
+
+        return currentResults.get();
+    }
+
+    private void clearCurrentResults() throws SQLException {
+        FlinkResultSet resultSet = currentResults.get();
+        if (resultSet == null) {
+            return;
+        }
+        resultSet.close();
+        currentResults.set(null);
+    }
+
+    @Override
+    public void close() throws SQLException {
+        if (closed) {
+            return;
+        }
+
+        cancel();
+        closed = true;
+    }
+
+    @Override
+    public void cancel() throws SQLException {
+        checkClosed();
+        clearCurrentResults();
+    }
+
+    private void checkClosed() throws SQLException {
+        if (closed) {
+            throw new SQLException("This result set is already closed");
+        }
+    }
+
+    @Override
+    public boolean execute(String sql) throws SQLException {
+        checkClosed();
+        clearCurrentResults();
+
+        StatementResult result = executor.executeStatement(sql);
+        FlinkResultSet resultSet = new FlinkResultSet(this, result, 
StringDataConverter.CONVERTER);
+        currentResults.set(resultSet);
+
+        return resultSet.isQuery() || result.getResultKind() == 
ResultKind.SUCCESS_WITH_CONTENT;

Review Comment:
   I have removed the `isQuery()` method in ResultSet. 
   1. According to the `ResultSet`, `executeQuery` will only execute `SELECT` 
statement and the `StatementResult.isQueryResult` is `True`. 
   2. For other statement such as `SHOW JOBS`, `SHOW TABLES`, `INSERT` and 
other statements, Flink will return `StatementResult` with `isQueryResult` is 
`False` and `resultKind` is `SUCCESS_WITH_CONTENT`. I return `true` in 
`FlinkStatement.execute` method for them and users can get result set from 
`FlinkStatement`
   3. For regular jdbc, `INSERT` will not return a result set, but Flink will. 
I have added doc for `FlinkStatement.execute` to clarify this



-- 
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: issues-unsubscr...@flink.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to