You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by "libenchao (via GitHub)" <gi...@apache.org> on 2023/04/23 07:03:10 UTC

[GitHub] [flink] libenchao commented on a diff in pull request #22417: [FLINK-31543][jdbc-driver] Introduce statement for flink jdbc driver

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


##########
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:
   `resultSet.isQuery()` already contains `result.getResultKind() == ResultKind.SUCCESS_WITH_CONTENT`? We may need to make `FlinkResultSet#isQuery` more clear about what it really means.



##########
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;

Review Comment:
   Do we need to use `AtomicReference` here? I'm wondering that whether we need to guarantee 'thread-safe' for flink jdbc, if not, then I guess it's not necessary to use `AtomicReference`; if yes, we may need to rethink all APIs for `Connection`, `Statement`, `ResultSet`. What do you think about 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-unsubscribe@flink.apache.org

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