|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.seatunnel.connectors.seatunnel.cdc.mysql.source; |
| 19 | + |
| 20 | +import org.apache.seatunnel.api.table.catalog.TableIdentifier; |
| 21 | +import org.apache.seatunnel.api.table.catalog.TablePath; |
| 22 | +import org.apache.seatunnel.api.table.event.AlterTableColumnEvent; |
| 23 | +import org.apache.seatunnel.api.table.event.AlterTableColumnsEvent; |
| 24 | +import org.apache.seatunnel.api.table.event.SchemaChangeEvent; |
| 25 | +import org.apache.seatunnel.api.table.type.SeaTunnelDataType; |
| 26 | +import org.apache.seatunnel.connectors.cdc.base.config.JdbcSourceConfig; |
| 27 | +import org.apache.seatunnel.connectors.cdc.base.config.SourceConfig; |
| 28 | +import org.apache.seatunnel.connectors.cdc.base.schema.AbstractSchemaChangeResolver; |
| 29 | +import org.apache.seatunnel.connectors.cdc.base.utils.SourceRecordUtils; |
| 30 | +import org.apache.seatunnel.connectors.seatunnel.cdc.mysql.source.parser.CustomMySqlAntlrDdlParser; |
| 31 | +import org.apache.seatunnel.connectors.seatunnel.jdbc.internal.dialect.DatabaseIdentifier; |
| 32 | + |
| 33 | +import org.apache.commons.lang3.StringUtils; |
| 34 | +import org.apache.kafka.connect.source.SourceRecord; |
| 35 | + |
| 36 | +import io.debezium.relational.Tables; |
| 37 | + |
| 38 | +import java.util.List; |
| 39 | +import java.util.Objects; |
| 40 | + |
| 41 | +public class MySqlSchemaChangeResolver extends AbstractSchemaChangeResolver { |
| 42 | + private transient Tables tables; |
| 43 | + private transient CustomMySqlAntlrDdlParser customMySqlAntlrDdlParser; |
| 44 | + |
| 45 | + public MySqlSchemaChangeResolver(SourceConfig.Factory<JdbcSourceConfig> sourceConfigFactory) { |
| 46 | + super(sourceConfigFactory.create(0)); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public SchemaChangeEvent resolve(SourceRecord record, SeaTunnelDataType dataType) { |
| 51 | + TablePath tablePath = SourceRecordUtils.getTablePath(record); |
| 52 | + String ddl = SourceRecordUtils.getDdl(record); |
| 53 | + if (Objects.isNull(customMySqlAntlrDdlParser)) { |
| 54 | + this.customMySqlAntlrDdlParser = |
| 55 | + new CustomMySqlAntlrDdlParser( |
| 56 | + tablePath, this.jdbcSourceConfig.getDbzConnectorConfig()); |
| 57 | + } |
| 58 | + if (Objects.isNull(tables)) { |
| 59 | + this.tables = new Tables(); |
| 60 | + } |
| 61 | + customMySqlAntlrDdlParser.setCurrentDatabase(tablePath.getDatabaseName()); |
| 62 | + customMySqlAntlrDdlParser.setCurrentSchema(tablePath.getSchemaName()); |
| 63 | + // Parse DDL statement using Debezium's Antlr parser |
| 64 | + customMySqlAntlrDdlParser.parse(ddl, tables); |
| 65 | + List<AlterTableColumnEvent> parsedEvents = |
| 66 | + customMySqlAntlrDdlParser.getAndClearParsedEvents(); |
| 67 | + AlterTableColumnsEvent alterTableColumnsEvent = |
| 68 | + new AlterTableColumnsEvent( |
| 69 | + TableIdentifier.of( |
| 70 | + StringUtils.EMPTY, |
| 71 | + tablePath.getDatabaseName(), |
| 72 | + tablePath.getSchemaName(), |
| 73 | + tablePath.getTableName()), |
| 74 | + parsedEvents); |
| 75 | + alterTableColumnsEvent.setStatement(ddl); |
| 76 | + alterTableColumnsEvent.setSourceDialectName(DatabaseIdentifier.MYSQL); |
| 77 | + return parsedEvents.isEmpty() ? null : alterTableColumnsEvent; |
| 78 | + } |
| 79 | +} |
0 commit comments