lowka commented on code in PR #6654:
URL: https://github.com/apache/ignite-3/pull/6654#discussion_r2387737333


##########
modules/jdbc/src/main/java/org/apache/ignite/internal/jdbc2/JdbcConnection2.java:
##########
@@ -0,0 +1,696 @@
+/*
+ * 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.ignite.internal.jdbc2;
+
+import static java.sql.ResultSet.CONCUR_READ_ONLY;
+import static java.sql.ResultSet.HOLD_CURSORS_OVER_COMMIT;
+import static java.sql.ResultSet.TYPE_FORWARD_ONLY;
+import static 
org.apache.ignite.internal.jdbc.proto.SqlStateCode.CONNECTION_CLOSED;
+
+import java.sql.Array;
+import java.sql.Blob;
+import java.sql.CallableStatement;
+import java.sql.Clob;
+import java.sql.Connection;
+import java.sql.DatabaseMetaData;
+import java.sql.NClob;
+import java.sql.PreparedStatement;
+import java.sql.SQLClientInfoException;
+import java.sql.SQLException;
+import java.sql.SQLFeatureNotSupportedException;
+import java.sql.SQLWarning;
+import java.sql.SQLXML;
+import java.sql.Savepoint;
+import java.sql.ShardingKey;
+import java.sql.Statement;
+import java.sql.Struct;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Properties;
+import java.util.concurrent.Executor;
+import org.apache.ignite.client.IgniteClient;
+import org.apache.ignite.internal.jdbc.ConnectionProperties;
+import org.apache.ignite.internal.jdbc.JdbcDatabaseMetadata;
+import org.apache.ignite.internal.jdbc.proto.JdbcQueryEventHandler;
+import org.apache.ignite.internal.jdbc.proto.SqlStateCode;
+import org.apache.ignite.internal.lang.IgniteExceptionMapperUtil;
+import org.apache.ignite.internal.sql.SqlCommon;
+import org.apache.ignite.lang.util.IgniteNameUtils;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * {@link Connection} implementation backed by the thin client.
+ */
+public class JdbcConnection2 implements Connection {
+
+    private final IgniteClient client;
+
+    private final JdbcDatabaseMetadata metadata;
+
+    private String schemaName;
+
+    private volatile boolean closed;
+
+    private int txIsolation;
+
+    private boolean autoCommit;
+
+    private boolean readOnly;
+
+    private int networkTimeoutMillis;
+
+    /**
+     * Creates new connection.
+     *
+     * @param client Client.
+     * @param eventHandler Event handler.
+     * @param props Connection properties.
+     */
+    public JdbcConnection2(
+            IgniteClient client,
+            JdbcQueryEventHandler eventHandler,
+            ConnectionProperties props
+    ) throws SQLException {
+        autoCommit = true;
+
+        networkTimeoutMillis = props.getConnectionTimeout();
+        txIsolation = TRANSACTION_NONE;
+        schemaName = readSchemaName(props.getSchema());
+
+        this.client = client;
+        //noinspection ThisEscapedInObjectConstruction
+        metadata = new JdbcDatabaseMetadata(this, eventHandler, 
props.getUrl(), props.getUsername());
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public Statement createStatement() throws SQLException {
+        return createStatement(TYPE_FORWARD_ONLY, CONCUR_READ_ONLY, 
HOLD_CURSORS_OVER_COMMIT);
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public Statement createStatement(int resSetType, int resSetConcurrency) 
throws SQLException {
+        return createStatement(resSetType, resSetConcurrency, 
HOLD_CURSORS_OVER_COMMIT);
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public Statement createStatement(int resSetType, int resSetConcurrency,
+            int resSetHoldability) throws SQLException {
+        ensureNotClosed();
+
+        checkCursorOptions(resSetType, resSetConcurrency, resSetHoldability);
+
+        throw new UnsupportedOperationException();
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public PreparedStatement prepareStatement(String sql) throws SQLException {
+        return prepareStatement(sql, TYPE_FORWARD_ONLY, CONCUR_READ_ONLY, 
HOLD_CURSORS_OVER_COMMIT);
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public PreparedStatement prepareStatement(String sql, int 
autoGeneratedKeys) throws SQLException {
+        ensureNotClosed();
+
+        throw new SQLFeatureNotSupportedException("Auto generated keys are not 
supported.");
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public PreparedStatement prepareStatement(String sql, int resSetType,
+            int resSetConcurrency) throws SQLException {
+        return prepareStatement(sql, resSetType, resSetConcurrency, 
HOLD_CURSORS_OVER_COMMIT);
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public PreparedStatement prepareStatement(String sql, int resSetType, int 
resSetConcurrency,
+            int resSetHoldability) throws SQLException {
+        ensureNotClosed();
+
+        checkCursorOptions(resSetType, resSetConcurrency, resSetHoldability);
+
+        if (sql == null) {
+            throw new SQLException("SQL string cannot be null.");
+        }
+
+        throw new UnsupportedOperationException();
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public PreparedStatement prepareStatement(String sql, int[] colIndexes) 
throws SQLException {
+        ensureNotClosed();
+
+        throw new SQLFeatureNotSupportedException("Auto generated keys are not 
supported.");
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public PreparedStatement prepareStatement(String sql, String[] colNames) 
throws SQLException {
+        ensureNotClosed();
+
+        throw new SQLFeatureNotSupportedException("Auto generated keys are not 
supported.");
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public String nativeSQL(String sql) throws SQLException {
+        ensureNotClosed();
+
+        Objects.requireNonNull(sql);
+
+        return sql;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public void setAutoCommit(boolean autoCommit) throws SQLException {
+        ensureNotClosed();
+
+        if (autoCommit != this.autoCommit) {
+            this.autoCommit = autoCommit;
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public boolean getAutoCommit() throws SQLException {
+        ensureNotClosed();
+
+        return autoCommit;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public void commit() throws SQLException {
+        ensureNotClosed();
+
+        if (autoCommit) {
+            throw new SQLException("Transaction cannot be committed explicitly 
in auto-commit mode.");
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public void rollback() throws SQLException {
+        ensureNotClosed();
+
+        if (autoCommit) {
+            throw new SQLException("Transaction cannot be rolled back 
explicitly in auto-commit mode.");
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public void rollback(Savepoint savepoint) throws SQLException {
+        ensureNotClosed();
+
+        if (savepoint == null) {
+            throw new SQLException("Invalid savepoint.");
+        }
+
+        if (autoCommit) {
+            throw new SQLException("Auto-commit mode.");
+        }
+
+        throw new SQLFeatureNotSupportedException("Savepoints are not 
supported.");
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public void close() throws SQLException {
+        if (isClosed()) {
+            return;
+        }
+
+        closed = true;
+
+        try {
+            client.close();
+        } catch (Exception e) {
+            throw new SQLException("Exception occurred while closing.", 
IgniteExceptionMapperUtil.mapToPublicException(e));
+        }
+    }
+
+    /**
+     * Ensures that connection is not closed.
+     *
+     * @throws SQLException If connection is closed.
+     */
+    public void ensureNotClosed() throws SQLException {
+        if (closed) {
+            throw new SQLException("Connection is closed.", CONNECTION_CLOSED);
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public boolean isClosed() throws SQLException {
+        return closed;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public DatabaseMetaData getMetaData() throws SQLException {
+        ensureNotClosed();
+
+        return metadata;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public void setReadOnly(boolean readOnly) throws SQLException {
+        ensureNotClosed();
+
+        this.readOnly = readOnly;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public boolean isReadOnly() throws SQLException {
+        ensureNotClosed();
+
+        return readOnly;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public void setCatalog(String catalog) throws SQLException {
+        ensureNotClosed();
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public String getCatalog() throws SQLException {
+        ensureNotClosed();
+
+        return JdbcDatabaseMetadata.CATALOG_NAME;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public void setTransactionIsolation(int level) throws SQLException {
+        ensureNotClosed();
+
+        switch (level) {
+            case TRANSACTION_READ_UNCOMMITTED:
+            case TRANSACTION_READ_COMMITTED:
+            case TRANSACTION_REPEATABLE_READ:
+            case TRANSACTION_SERIALIZABLE:
+            case TRANSACTION_NONE:
+                break;
+
+            default:
+                throw new SQLException("Invalid transaction isolation level.", 
SqlStateCode.INVALID_TRANSACTION_LEVEL);
+        }
+
+        txIsolation = level;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public int getTransactionIsolation() throws SQLException {
+        ensureNotClosed();
+
+        return txIsolation;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    @Nullable
+    public SQLWarning getWarnings() throws SQLException {
+        ensureNotClosed();
+
+        return null;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public void clearWarnings() throws SQLException {
+        ensureNotClosed();
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public Map<String, Class<?>> getTypeMap() throws SQLException {
+        ensureNotClosed();
+
+        throw new SQLFeatureNotSupportedException("Types mapping is not 
supported.");
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public void setTypeMap(Map<String, Class<?>> map) throws SQLException {
+        ensureNotClosed();
+
+        throw new SQLFeatureNotSupportedException("Types mapping is not 
supported.");
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public void setHoldability(int holdability) throws SQLException {
+        ensureNotClosed();
+
+        if (holdability != HOLD_CURSORS_OVER_COMMIT) {
+            throw new SQLException("Invalid result set holdability value.");
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public int getHoldability() throws SQLException {
+        ensureNotClosed();
+
+        return HOLD_CURSORS_OVER_COMMIT;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public Savepoint setSavepoint() throws SQLException {
+        ensureNotClosed();
+
+        if (autoCommit) {
+            throw new SQLException("Savepoint cannot be set in auto-commit 
mode.");
+        }
+
+        throw new SQLFeatureNotSupportedException("Savepoints are not 
supported.");
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public Savepoint setSavepoint(String name) throws SQLException {
+        ensureNotClosed();
+
+        if (name == null) {
+            throw new SQLException("Savepoint name cannot be null.");
+        }
+
+        if (autoCommit) {
+            throw new SQLException("Savepoint cannot be set in auto-commit 
mode.");
+        }
+
+        throw new SQLFeatureNotSupportedException("Savepoints are not 
supported.");
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public void releaseSavepoint(Savepoint savepoint) throws SQLException {
+        ensureNotClosed();
+
+        if (savepoint == null) {
+            throw new SQLException("Savepoint cannot be null.");
+        }
+
+        throw new SQLFeatureNotSupportedException("Savepoints are not 
supported.");
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public CallableStatement prepareCall(String sql) throws SQLException {
+        ensureNotClosed();
+
+        throw new SQLFeatureNotSupportedException("Callable functions are not 
supported.");
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public CallableStatement prepareCall(String sql, int resSetType, int 
resSetConcurrency)
+            throws SQLException {
+        ensureNotClosed();
+
+        throw new SQLFeatureNotSupportedException("Callable functions are not 
supported.");
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public CallableStatement prepareCall(String sql, int resSetType, int 
resSetConcurrency,
+            int resSetHoldability) throws SQLException {
+        ensureNotClosed();
+
+        throw new SQLFeatureNotSupportedException("Callable functions are not 
supported.");
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public Clob createClob() throws SQLException {
+        ensureNotClosed();
+
+        throw new SQLFeatureNotSupportedException("SQL-specific types are not 
supported.");
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public Blob createBlob() throws SQLException {
+        ensureNotClosed();
+
+        throw new SQLFeatureNotSupportedException("SQL-specific types are not 
supported.");
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public NClob createNClob() throws SQLException {
+        ensureNotClosed();
+
+        throw new SQLFeatureNotSupportedException("SQL-specific types are not 
supported.");
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public SQLXML createSQLXML() throws SQLException {
+        ensureNotClosed();
+
+        throw new SQLFeatureNotSupportedException("SQL-specific types are not 
supported.");
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public boolean isValid(int timeout) throws SQLException {
+        if (timeout < 0) {
+            throw new SQLException("Invalid timeout: " + timeout);
+        }
+
+        return !closed;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public void setClientInfo(String name, String val) throws 
SQLClientInfoException {
+        if (closed) {
+            throw new SQLClientInfoException("Connection is closed.", null);
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public void setClientInfo(Properties props) throws SQLClientInfoException {
+        if (closed) {
+            throw new SQLClientInfoException("Connection is closed.", null);
+        }
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public @Nullable String getClientInfo(String name) throws SQLException {
+        ensureNotClosed();
+
+        return null;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public Properties getClientInfo() throws SQLException {
+        ensureNotClosed();
+
+        return new Properties();
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public Array createArrayOf(String typeName, Object[] elements) throws 
SQLException {
+        ensureNotClosed();
+
+        throw new SQLFeatureNotSupportedException("SQL-specific types are not 
supported.");
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public Struct createStruct(String typeName, Object[] attrs) throws 
SQLException {
+        ensureNotClosed();
+
+        throw new SQLFeatureNotSupportedException("SQL-specific types are not 
supported.");
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public void setSchema(String schema) throws SQLException {
+        ensureNotClosed();
+
+        this.schemaName = readSchemaName(schema);
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public String getSchema() throws SQLException {
+        ensureNotClosed();
+
+        return schemaName;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public void abort(Executor executor) throws SQLException {
+        if (executor == null) {
+            throw new SQLException("Executor cannot be null.");
+        }
+
+        close();
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public final void setNetworkTimeout(Executor executor, int ms) throws 
SQLException {
+        ensureNotClosed();
+
+        if (ms < 0) {
+            throw new SQLException("Network timeout cannot be negative.");
+        }
+
+        networkTimeoutMillis = ms;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public int getNetworkTimeout() throws SQLException {
+        ensureNotClosed();
+
+        return networkTimeoutMillis;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public void beginRequest() throws SQLException {
+        ensureNotClosed();
+
+        // No-op
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public void endRequest() throws SQLException {
+        ensureNotClosed();
+
+        // No-op
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public boolean setShardingKeyIfValid(ShardingKey shardingKey, ShardingKey 
superShardingKey,
+            int timeout) throws SQLException {
+        ensureNotClosed();
+
+        throw new SQLFeatureNotSupportedException("Sharding keys are not 
supported.");
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public boolean setShardingKeyIfValid(ShardingKey shardingKey, int timeout) 
throws SQLException {
+        ensureNotClosed();
+
+        throw new SQLFeatureNotSupportedException("Sharding keys are not 
supported.");
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public void setShardingKey(ShardingKey shardingKey, ShardingKey 
superShardingKey) throws SQLException {
+        ensureNotClosed();
+
+        throw new SQLFeatureNotSupportedException("Sharding keys are not 
supported.");
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public void setShardingKey(ShardingKey shardingKey) throws SQLException {
+        ensureNotClosed();
+
+        throw new SQLFeatureNotSupportedException("Sharding keys are not 
supported.");
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public <T> T unwrap(Class<T> iface) throws SQLException {
+        if (!isWrapperFor(iface)) {
+            throw new SQLException("Connection is not a wrapper for " + 
iface.getName());
+        }
+
+        return (T) this;
+    }
+
+    /** {@inheritDoc} */
+    @Override
+    public boolean isWrapperFor(Class<?> iface) throws SQLException {
+        return iface != null && iface.isAssignableFrom(JdbcConnection2.class);
+    }
+
+    private static void checkCursorOptions(
+            int resSetType,
+            int resSetConcurrency,
+            int resHoldability
+    ) throws SQLFeatureNotSupportedException {
+
+        if (resSetType != TYPE_FORWARD_ONLY) {
+            throw new SQLFeatureNotSupportedException("Invalid result set type 
(only forward is supported).");
+        }
+
+        if (resSetConcurrency != CONCUR_READ_ONLY) {
+            throw new SQLFeatureNotSupportedException("Invalid concurrency 
(updates are not supported).");
+        }
+
+        if (resHoldability != HOLD_CURSORS_OVER_COMMIT) {

Review Comment:
   Thanks. 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