You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@rocketmq.apache.org by GitBox <gi...@apache.org> on 2022/05/17 05:49:07 UTC

[GitHub] [rocketmq-connect] odbozhou commented on a diff in pull request #139: Upgrade rocketmq connect jdbc

odbozhou commented on code in PR #139:
URL: https://github.com/apache/rocketmq-connect/pull/139#discussion_r874389828


##########
connectors/rocketmq-connect-jdbc/src/main/java/org/apache/rocketmq/connect/jdbc/dialect/impl/OracleDatabaseDialect.java:
##########
@@ -0,0 +1,308 @@
+/*
+ * Copyright 2018 Confluent Inc.
+ *
+ * Licensed under the Confluent Community License (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.confluent.io/confluent-community-license
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OF ANY KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations under the License.
+ */
+
+package org.apache.rocketmq.connect.jdbc.dialect.impl;
+
+import io.openmessaging.connector.api.data.FieldType;
+import io.openmessaging.connector.api.data.Schema;
+import io.openmessaging.connector.api.errors.ConnectException;
+import org.apache.rocketmq.connect.jdbc.config.AbstractConfig;
+import org.apache.rocketmq.connect.jdbc.connector.JdbcSinkConfig;
+import org.apache.rocketmq.connect.jdbc.dialect.DatabaseDialect;
+import org.apache.rocketmq.connect.jdbc.dialect.DropOptions;
+import org.apache.rocketmq.connect.jdbc.dialect.PreparedStatementBinder;
+import org.apache.rocketmq.connect.jdbc.dialect.provider.DatabaseDialectProvider;
+import org.apache.rocketmq.connect.jdbc.schema.column.ColumnDefinition;
+import org.apache.rocketmq.connect.jdbc.schema.column.ColumnId;
+import org.apache.rocketmq.connect.jdbc.schema.table.TableDefinition;
+import org.apache.rocketmq.connect.jdbc.schema.table.TableId;
+import org.apache.rocketmq.connect.jdbc.sink.metadata.FieldsMetadata;
+import org.apache.rocketmq.connect.jdbc.sink.metadata.SchemaPair;
+import org.apache.rocketmq.connect.jdbc.sink.metadata.SinkRecordField;
+import org.apache.rocketmq.connect.jdbc.util.ExpressionBuilder;
+import org.apache.rocketmq.connect.jdbc.util.IdentifierRules;
+
+import java.io.ByteArrayInputStream;
+import java.io.StringReader;
+import java.nio.ByteBuffer;
+import java.sql.PreparedStatement;
+import java.sql.SQLException;
+import java.sql.Types;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+
+/**
+ * A  DatabaseDialect for Oracle.
+ */
+public class OracleDatabaseDialect extends GenericDatabaseDialect {
+
+  /**
+   * The provider for {@link OracleDatabaseDialect}.
+   */
+  public static class Provider extends DatabaseDialectProvider {
+    public Provider() {
+      super(OracleDatabaseDialect.class.getSimpleName(), "oracle");
+    }
+
+    @Override
+    public DatabaseDialect create(AbstractConfig config) {
+      return new OracleDatabaseDialect(config);
+    }
+  }
+
+  /**
+   * Create a new dialect instance with the given connector configuration.
+   *
+   * @param config the connector configuration; may not be null
+   */
+  public OracleDatabaseDialect(AbstractConfig config) {
+    super(config, new IdentifierRules(".", "\"", "\""));
+  }
+
+  @Override
+  protected String currentTimestampDatabaseQuery() {
+    return "select CURRENT_TIMESTAMP from dual";
+  }
+
+  @Override
+  protected String checkConnectionQuery() {
+    return "SELECT 1 FROM DUAL";
+  }
+
+  @Override
+  public StatementBinder statementBinder(
+      PreparedStatement statement,
+      JdbcSinkConfig.PrimaryKeyMode pkMode,
+      SchemaPair schemaPair,
+      FieldsMetadata fieldsMetadata,
+      TableDefinition tableDefinition,
+      JdbcSinkConfig.InsertMode insertMode
+  ) {
+    return new PreparedStatementBinder(
+        this,
+        statement,
+        pkMode,
+        schemaPair,
+        fieldsMetadata,
+        tableDefinition,
+        insertMode
+    );
+  }
+
+  @Override
+  public void bindField(
+      PreparedStatement statement,
+      int index,
+      Schema schema,
+      Object value,
+      ColumnDefinition colDef
+  ) throws SQLException {
+    if (value == null) {
+      statement.setObject(index, null);
+    } else {
+      boolean bound = maybeBindLogical(statement, index, schema, value, colDef);
+      if (!bound) {
+        bound = maybeBindPrimitive(statement, index, schema, value, colDef);
+      }
+      if (!bound) {
+        throw new ConnectException("Unsupported source data type: " + schema.getFieldType());
+      }
+    }
+  }
+
+  protected boolean maybeBindPrimitive(
+      PreparedStatement statement,
+      int index,
+      Schema schema,
+      Object value,
+      ColumnDefinition colDef
+  ) throws SQLException {
+    if (colDef == null) {
+      return super.maybeBindPrimitive(statement, index, schema, value);
+    }
+
+    if (schema.getFieldType() == FieldType.STRING) {
+      if (colDef.type() == Types.CLOB) {
+        statement.setCharacterStream(index, new StringReader((String) value));
+        return true;
+      } else if (colDef.type() == Types.NCLOB) {
+        statement.setNCharacterStream(index, new StringReader((String) value));
+        return true;
+      } else if (colDef.type() == Types.NVARCHAR || colDef.type() == Types.NCHAR) {
+        statement.setNString(index, (String) value);
+        return true;
+      } else {
+        return super.maybeBindPrimitive(statement, index, schema, value);
+      }
+    }
+
+    if (schema.getFieldType() == FieldType.BYTES && colDef.type() == Types.BLOB) {
+      if (value instanceof ByteBuffer) {
+        statement.setBlob(index, new ByteArrayInputStream(((ByteBuffer) value).array()));
+      } else if (value instanceof byte[]) {
+        statement.setBlob(index, new ByteArrayInputStream((byte[]) value));
+      } else {
+        return super.maybeBindPrimitive(statement, index, schema, value);
+      }
+      return true;
+    }
+    return super.maybeBindPrimitive(statement, index, schema, value);
+  }
+
+  @SuppressWarnings("checkstyle:CyclomaticComplexity")
+  @Override
+  protected String getSqlType(SinkRecordField field) {
+    if (field.schemaName() != null) {

Review Comment:
   useless code?



-- 
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: dev-unsubscribe@rocketmq.apache.org

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