Java Code Examples for com.alibaba.otter.canal.protocol.CanalEntry.EventType

The following examples show how to use com.alibaba.otter.canal.protocol.CanalEntry.EventType. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: canal-1.1.3   Source File: LogEventConvert.java    License: Apache License 2.0 6 votes vote down vote up
private Entry parseGTIDLogEvent(GtidLogEvent logEvent) {
    LogHeader logHeader = logEvent.getHeader();
    Pair.Builder builder = Pair.newBuilder();
    builder.setKey("gtid");
    builder.setValue(logEvent.getGtidStr());

    if (logEvent.getLastCommitted() != null) {
        builder.setKey("lastCommitted");
        builder.setValue(String.valueOf(logEvent.getLastCommitted()));
        builder.setKey("sequenceNumber");
        builder.setValue(String.valueOf(logEvent.getSequenceNumber()));
    }

    Header header = createHeader(logHeader, "", "", EventType.GTID);
    return createEntry(header, EntryType.GTIDLOG, builder.build().toByteString());
}
 
Example 2
Source Project: DBus   Source File: MysqlWrapperDefaultHandler.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 创建DBusMessageBuilder对象,同时生成ums schema
 */
private DbusMessageBuilder createBuilderWithSchema(MetaVersion version, MessageEntry msgEntry) {
    DbusMessageBuilder builder = new DbusMessageBuilder();
    String namespace;
    if (outputTablePartition()) {
        namespace = builder.buildNameSpace(Utils.getDataSourceNamespace(),
                msgEntry.getEntryHeader().getSchemaName(), msgEntry.getEntryHeader().getTableName(),
                version.getVersion(), msgEntry.getEntryHeader().getPartitionTableName());
    } else {
        namespace = builder.buildNameSpace(Utils.getDataSourceNamespace(),
                msgEntry.getEntryHeader().getSchemaName(), msgEntry.getEntryHeader().getTableName(),
                version.getVersion());
    }
    builder.build(DbusMessage.ProtocolType.DATA_INCREMENT_DATA, namespace, table.getBatchId());
    EventType eventType = msgEntry.getEntryHeader().getOperType();
    RowData rowData = msgEntry.getMsgColumn().getRowDataLst().get(0);
    List<Column> columns = Support.getFinalColumns(eventType, rowData);
    for (Column column : columns) {
        String colType = Support.getColumnType(column);
        // 这里避免,源端表中包含ums_id_/ums_ts_等字段
        if (Support.isSupported(colType) && !builder.getMessage().containsFiled(column.getName())) {
            builder.appendSchema(column.getName(), DataType.convertMysqlDataType(colType), true);
        }
    }
    return builder;
}
 
Example 3
Source Project: DBus   Source File: LogEventConvert.java    License: Apache License 2.0 6 votes vote down vote up
private Header.Builder createHeaderBuilder(String binlogFile, LogHeader logHeader, String schemaName, String tableName,
                                           EventType eventType) {
    // header会做信息冗余,方便以后做检索或者过滤
    Header.Builder headerBuilder = Header.newBuilder();
    headerBuilder.setVersion(version);
    headerBuilder.setLogfileName(binlogFile);
    headerBuilder.setLogfileOffset(logHeader.getLogPos() - logHeader.getEventLen());
    headerBuilder.setServerId(logHeader.getServerId());
    headerBuilder.setServerenCode(UTF_8);// 经过java输出后所有的编码为unicode
    headerBuilder.setExecuteTime(logHeader.getWhen() * 1000L);
    headerBuilder.setSourceType(Type.MYSQL);
    if (eventType != null) {
        headerBuilder.setEventType(eventType);
    }
    if (schemaName != null) {
        headerBuilder.setSchemaName(schemaName);
    }
    if (tableName != null) {
        headerBuilder.setTableName(tableName);
    }
    headerBuilder.setEventLength(logHeader.getEventLen());
    return headerBuilder;
}
 
Example 4
Source Project: DBus   Source File: LogEventConvert_old.java    License: Apache License 2.0 6 votes vote down vote up
private Header createHeader(String binlogFile, LogHeader logHeader, String schemaName, String tableName,
                            EventType eventType) {
    // header������Ϣ����,�����Ժ����������߹���
    Header.Builder headerBuilder = Header.newBuilder();
    headerBuilder.setVersion(version);
    headerBuilder.setLogfileName(binlogFile);
    headerBuilder.setLogfileOffset(logHeader.getLogPos() - logHeader.getEventLen());
    headerBuilder.setServerId(logHeader.getServerId());
    headerBuilder.setServerenCode(UTF_8);// ����java��������еı���Ϊunicode
    headerBuilder.setExecuteTime(logHeader.getWhen() * 1000L);
    headerBuilder.setSourceType(Type.MYSQL);
    if (eventType != null) {
        headerBuilder.setEventType(eventType);
    }
    if (schemaName != null) {
        headerBuilder.setSchemaName(schemaName);
    }
    if (tableName != null) {
        headerBuilder.setTableName(tableName);
    }
    headerBuilder.setEventLength(logHeader.getEventLen());
    return headerBuilder.build();
}
 
Example 5
Source Project: DBus   Source File: LogEventConvert.java    License: Apache License 2.0 6 votes vote down vote up
private Header.Builder createHeaderBuilder(String binlogFile, LogHeader logHeader, String schemaName, String tableName,
                                           EventType eventType) {
     // header会做信息冗余,方便以后做检索或者过滤
    Header.Builder headerBuilder = Header.newBuilder();
    headerBuilder.setVersion(version);
    headerBuilder.setLogfileName(binlogFile);
    headerBuilder.setLogfileOffset(logHeader.getLogPos() - logHeader.getEventLen());
    headerBuilder.setServerId(logHeader.getServerId());
    headerBuilder.setServerenCode(UTF_8);// 经过java输出后所有的编码为unicode
    headerBuilder.setExecuteTime(logHeader.getWhen() * 1000L);
    headerBuilder.setSourceType(Type.MYSQL);
    if (eventType != null) {
        headerBuilder.setEventType(eventType);
    }
    if (schemaName != null) {
        headerBuilder.setSchemaName(schemaName);
    }
    if (tableName != null) {
        headerBuilder.setTableName(tableName);
    }
    headerBuilder.setEventLength(logHeader.getEventLen());

    return headerBuilder;
}
 
Example 6
private void publishCanalEvent(Entry entry) {
    EventType eventType = entry.getHeader().getEventType();
    switch (eventType) {
        case INSERT:
            applicationContext.publishEvent(new InsertAbstractCanalEvent(entry));
            break;
        case UPDATE:
            applicationContext.publishEvent(new UpdateAbstractCanalEvent(entry));
            break;
        case DELETE:
            applicationContext.publishEvent(new DeleteAbstractCanalEvent(entry));
            break;
        default:
            break;
    }
}
 
Example 7
Source Project: canal   Source File: LogEventConvert.java    License: Apache License 2.0 6 votes vote down vote up
private Entry parseGTIDLogEvent(GtidLogEvent logEvent) {
    LogHeader logHeader = logEvent.getHeader();
    Pair.Builder builder = Pair.newBuilder();
    builder.setKey("gtid");
    builder.setValue(logEvent.getGtidStr());

    if (logEvent.getLastCommitted() != null) {
        builder.setKey("lastCommitted");
        builder.setValue(String.valueOf(logEvent.getLastCommitted()));
        builder.setKey("sequenceNumber");
        builder.setValue(String.valueOf(logEvent.getSequenceNumber()));
    }

    Header header = createHeader(logHeader, "", "", EventType.GTID);
    return createEntry(header, EntryType.GTIDLOG, builder.build().toByteString());
}
 
Example 8
Source Project: canal-1.1.3   Source File: LogEventConvert.java    License: Apache License 2.0 5 votes vote down vote up
private Entry parseHeartbeatLogEvent(HeartbeatLogEvent logEvent) {
    Header.Builder headerBuilder = Header.newBuilder();
    headerBuilder.setEventType(EventType.MHEARTBEAT);
    Entry.Builder entryBuilder = Entry.newBuilder();
    entryBuilder.setHeader(headerBuilder.build());
    entryBuilder.setEntryType(EntryType.HEARTBEAT);
    return entryBuilder.build();
}
 
Example 9
Source Project: canal-1.1.3   Source File: LogEventConvert.java    License: Apache License 2.0 5 votes vote down vote up
private Entry buildQueryEntry(String queryString, LogHeader logHeader, String tableName) {
    Header header = createHeader(logHeader, "", tableName, EventType.QUERY);
    RowChange.Builder rowChangeBuider = RowChange.newBuilder();
    rowChangeBuider.setSql(queryString);
    rowChangeBuider.setEventType(EventType.QUERY);
    return createEntry(header, EntryType.ROWDATA, rowChangeBuider.build().toByteString());
}
 
Example 10
Source Project: canal-1.1.3   Source File: LogEventConvert.java    License: Apache License 2.0 5 votes vote down vote up
private Entry buildQueryEntry(String queryString, LogHeader logHeader) {
    Header header = createHeader(logHeader, "", "", EventType.QUERY);
    RowChange.Builder rowChangeBuider = RowChange.newBuilder();
    rowChangeBuider.setSql(queryString);
    rowChangeBuider.setEventType(EventType.QUERY);
    return createEntry(header, EntryType.ROWDATA, rowChangeBuider.build().toByteString());
}
 
Example 11
Source Project: canal-1.1.3   Source File: EventTransactionBuffer.java    License: Apache License 2.0 5 votes vote down vote up
public void add(CanalEntry.Entry entry) throws InterruptedException {
    switch (entry.getEntryType()) {
        case TRANSACTIONBEGIN:
            flush();// 刷新上一次的数据
            put(entry);
            break;
        case TRANSACTIONEND:
            put(entry);
            flush();
            break;
        case ROWDATA:
            put(entry);
            // 针对非DML的数据,直接输出,不进行buffer控制
            EventType eventType = entry.getHeader().getEventType();
            if (eventType != null && !isDml(eventType)) {
                flush();
            }
            break;
        case HEARTBEAT:
            // master过来的heartbeat,说明binlog已经读完了,是idle状态
            put(entry);
            flush();
            break;
        default:
            break;
    }
}
 
Example 12
Source Project: canal-1.1.3   Source File: DruidDdlParserTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testAlert() {
    String queryString = "alter table retl_mark drop index emp_name";
    DdlResult result = DruidDdlParser.parse(queryString, "retl").get(0);
    Assert.assertNotNull(result);
    Assert.assertEquals("retl_mark", result.getTableName());

    queryString = "alter table retl.retl_mark drop index emp_name";
    result = DruidDdlParser.parse(queryString, "retl").get(0);
    Assert.assertNotNull(result);
    Assert.assertEquals("retl_mark", result.getTableName());

    queryString = "alter table \n retl.`retl_mark` drop index emp_name;";
    result = DruidDdlParser.parse(queryString, "retl").get(0);
    Assert.assertNotNull(result);
    Assert.assertEquals("retl_mark", result.getTableName());

    queryString = "alter table retl.retl_mark drop index emp_name , add index emp_name(id)";
    result = DruidDdlParser.parse(queryString, "retl").get(0);
    Assert.assertNotNull(result);
    Assert.assertEquals("retl_mark", result.getTableName());
    Assert.assertEquals(EventType.DINDEX, result.getType());

    result = DruidDdlParser.parse(queryString, "retl").get(1);
    Assert.assertNotNull(result);
    Assert.assertEquals("retl_mark", result.getTableName());
    Assert.assertEquals(EventType.CINDEX, result.getType());
}
 
Example 13
Source Project: DBus   Source File: Support.java    License: Apache License 2.0 5 votes vote down vote up
public static List<Column> getFinalColumns(EventType type, RowData rowData) {
    List<Column> Columns = null;
    if (type == EventType.INSERT || type == EventType.UPDATE) {
        Columns = rowData.getAfterColumnsList();
    } else if (type == EventType.DELETE) {
        Columns = rowData.getBeforeColumnsList();
    }
    return Columns;
}
 
Example 14
Source Project: DBus   Source File: LogEventConvert.java    License: Apache License 2.0 5 votes vote down vote up
private Entry buildQueryEntry(String queryString, LogHeader logHeader) {
    Header header = createHeader(binlogFileName, logHeader, "", "", EventType.QUERY);
    RowChange.Builder rowChangeBuider = RowChange.newBuilder();
    rowChangeBuider.setSql(queryString);
    rowChangeBuider.setEventType(EventType.QUERY);
    return createEntry(header, EntryType.ROWDATA, rowChangeBuider.build().toByteString());
}
 
Example 15
Source Project: DBus   Source File: LogEventConvert_old.java    License: Apache License 2.0 5 votes vote down vote up
private Entry buildQueryEntry(String queryString, LogHeader logHeader) {
    Header header = createHeader(binlogFileName, logHeader, "", "", EventType.QUERY);
    RowChange.Builder rowChangeBuider = RowChange.newBuilder();
    rowChangeBuider.setSql(queryString);
    rowChangeBuider.setEventType(EventType.QUERY);
    return createEntry(header, EntryType.ROWDATA, rowChangeBuider.build().toByteString());
}
 
Example 16
Source Project: DBus   Source File: LogEventConvert.java    License: Apache License 2.0 5 votes vote down vote up
private Entry buildQueryEntry(String queryString, LogHeader logHeader) {
    Header header = createHeader(binlogFileName, logHeader, "", "", EventType.QUERY);
    RowChange.Builder rowChangeBuider = RowChange.newBuilder();
    rowChangeBuider.setSql(queryString);
    rowChangeBuider.setEventType(EventType.QUERY);
    return createEntry(header, EntryType.ROWDATA, rowChangeBuider.build().toByteString());
}
 
Example 17
Source Project: canal   Source File: LogEventConvert.java    License: Apache License 2.0 5 votes vote down vote up
private Entry parseHeartbeatLogEvent(HeartbeatLogEvent logEvent) {
    Header.Builder headerBuilder = Header.newBuilder();
    headerBuilder.setEventType(EventType.MHEARTBEAT);
    Entry.Builder entryBuilder = Entry.newBuilder();
    entryBuilder.setHeader(headerBuilder.build());
    entryBuilder.setEntryType(EntryType.HEARTBEAT);
    return entryBuilder.build();
}
 
Example 18
Source Project: canal   Source File: LogEventConvert.java    License: Apache License 2.0 5 votes vote down vote up
private Entry buildQueryEntry(String queryString, LogHeader logHeader, String tableName) {
    Header header = createHeader(logHeader, "", tableName, EventType.QUERY);
    RowChange.Builder rowChangeBuider = RowChange.newBuilder();
    rowChangeBuider.setSql(queryString);
    rowChangeBuider.setEventType(EventType.QUERY);
    return createEntry(header, EntryType.ROWDATA, rowChangeBuider.build().toByteString());
}
 
Example 19
Source Project: canal   Source File: LogEventConvert.java    License: Apache License 2.0 5 votes vote down vote up
private Entry buildQueryEntry(String queryString, LogHeader logHeader) {
    Header header = createHeader(logHeader, "", "", EventType.QUERY);
    RowChange.Builder rowChangeBuider = RowChange.newBuilder();
    rowChangeBuider.setSql(queryString);
    rowChangeBuider.setEventType(EventType.QUERY);
    return createEntry(header, EntryType.ROWDATA, rowChangeBuider.build().toByteString());
}
 
Example 20
Source Project: canal   Source File: EventTransactionBuffer.java    License: Apache License 2.0 5 votes vote down vote up
public void add(CanalEntry.Entry entry) throws InterruptedException {
    switch (entry.getEntryType()) {
        case TRANSACTIONBEGIN:
            flush();// 刷新上一次的数据
            put(entry);
            break;
        case TRANSACTIONEND:
            put(entry);
            flush();
            break;
        case ROWDATA:
            put(entry);
            // 针对非DML的数据,直接输出,不进行buffer控制
            EventType eventType = entry.getHeader().getEventType();
            if (eventType != null && !isDml(eventType)) {
                flush();
            }
            break;
        case HEARTBEAT:
            // master过来的heartbeat,说明binlog已经读完了,是idle状态
            put(entry);
            flush();
            break;
        default:
            break;
    }
}
 
Example 21
Source Project: canal   Source File: DruidDdlParserTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testAlert() {
    String queryString = "alter table retl_mark drop index emp_name";
    DdlResult result = DruidDdlParser.parse(queryString, "retl").get(0);
    Assert.assertNotNull(result);
    Assert.assertEquals("retl_mark", result.getTableName());

    queryString = "alter table retl.retl_mark drop index emp_name";
    result = DruidDdlParser.parse(queryString, "retl").get(0);
    Assert.assertNotNull(result);
    Assert.assertEquals("retl_mark", result.getTableName());

    queryString = "alter table \n retl.`retl_mark` drop index emp_name;";
    result = DruidDdlParser.parse(queryString, "retl").get(0);
    Assert.assertNotNull(result);
    Assert.assertEquals("retl_mark", result.getTableName());

    queryString = "alter table retl.retl_mark drop index emp_name , add index emp_name(id)";
    result = DruidDdlParser.parse(queryString, "retl").get(0);
    Assert.assertNotNull(result);
    Assert.assertEquals("retl_mark", result.getTableName());
    Assert.assertEquals(EventType.DINDEX, result.getType());

    result = DruidDdlParser.parse(queryString, "retl").get(1);
    Assert.assertNotNull(result);
    Assert.assertEquals("retl_mark", result.getTableName());
    Assert.assertEquals(EventType.CINDEX, result.getType());
}
 
Example 22
Source Project: canal   Source File: DdlResultTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void getTypeOutputNull() {

  // Arrange
  final DdlResult objectUnderTest = new DdlResult();

  // Act
  final EventType actual = objectUnderTest.getType();

  // Assert result
  Assert.assertNull(actual);
}
 
Example 23
Source Project: canal-1.1.3   Source File: LogEventConvert.java    License: Apache License 2.0 4 votes vote down vote up
private boolean processFilter(String queryString, DdlResult result) {
    String schemaName = result.getSchemaName();
    String tableName = result.getTableName();
    // fixed issue https://github.com/alibaba/canal/issues/58
    // 更新下table meta cache
    if (tableMetaCache != null
        && (result.getType() == EventType.ALTER || result.getType() == EventType.ERASE || result.getType() == EventType.RENAME)) {
        // 对外返回,保证兼容,还是返回QUERY类型,这里暂不解析tableName,所以无法支持过滤
        for (DdlResult renameResult = result; renameResult != null; renameResult = renameResult.getRenameTableResult()) {
            String schemaName0 = renameResult.getSchemaName();
            String tableName0 = renameResult.getTableName();
            if (StringUtils.isNotEmpty(tableName0)) {
                // 如果解析到了正确的表信息,则根据全名进行清除
                tableMetaCache.clearTableMeta(schemaName0, tableName0);
            } else {
                // 如果无法解析正确的表信息,则根据schema进行清除
                tableMetaCache.clearTableMetaWithSchemaName(schemaName0);
            }
        }
    }

    // fixed issue https://github.com/alibaba/canal/issues/58
    if (result.getType() == EventType.ALTER || result.getType() == EventType.ERASE
        || result.getType() == EventType.CREATE || result.getType() == EventType.TRUNCATE
        || result.getType() == EventType.RENAME || result.getType() == EventType.CINDEX
        || result.getType() == EventType.DINDEX) { // 针对DDL类型

        if (filterQueryDdl) {
            return true;
        }

        if (StringUtils.isEmpty(tableName)
            || (result.getType() == EventType.RENAME && StringUtils.isEmpty(result.getOriTableName()))) {
            // 如果解析不出tableName,记录一下日志,方便bugfix,目前直接抛出异常,中断解析
            throw new CanalParseException("SimpleDdlParser process query failed. pls submit issue with this queryString: "
                                          + queryString + " , and DdlResult: " + result.toString());
            // return null;
        } else {
            // check name filter
            String name = schemaName + "." + tableName;
            if (nameFilter != null && !nameFilter.filter(name)) {
                if (result.getType() == EventType.RENAME) {
                    // rename校验只要源和目标满足一个就进行操作
                    if (nameFilter != null
                        && !nameFilter.filter(result.getOriSchemaName() + "." + result.getOriTableName())) {
                        return true;
                    }
                } else {
                    // 其他情况返回null
                    return true;
                }
            }

            if (nameBlackFilter != null && nameBlackFilter.filter(name)) {
                if (result.getType() == EventType.RENAME) {
                    // rename校验只要源和目标满足一个就进行操作
                    if (nameBlackFilter != null
                        && nameBlackFilter.filter(result.getOriSchemaName() + "." + result.getOriTableName())) {
                        return true;
                    }
                } else {
                    // 其他情况返回null
                    return true;
                }
            }
        }
    } else if (result.getType() == EventType.INSERT || result.getType() == EventType.UPDATE
               || result.getType() == EventType.DELETE) {
        // 对外返回,保证兼容,还是返回QUERY类型,这里暂不解析tableName,所以无法支持过滤
        if (filterQueryDml) {
            return true;
        }
    } else if (filterQueryDcl) {
        return true;
    }

    return false;
}
 
Example 24
Source Project: canal-1.1.3   Source File: LogEventConvert.java    License: Apache License 2.0 4 votes vote down vote up
private Header createHeader(LogHeader logHeader, String schemaName, String tableName, EventType eventType) {
    return createHeader(logHeader, schemaName, tableName, eventType, -1);
}
 
Example 25
Source Project: canal-1.1.3   Source File: DdlResult.java    License: Apache License 2.0 4 votes vote down vote up
public EventType getType() {
    return type;
}
 
Example 26
Source Project: canal-1.1.3   Source File: DdlResult.java    License: Apache License 2.0 4 votes vote down vote up
public void setType(EventType type) {
    this.type = type;
}
 
Example 27
Source Project: canal-1.1.3   Source File: SimpleDdlParser.java    License: Apache License 2.0 4 votes vote down vote up
public static DdlResult parse(String queryString, String schmeaName) {
    queryString = removeComment(queryString); // 去除/* */的sql注释内容
    DdlResult result = parseDdl(queryString, schmeaName, ALERT_PATTERN, 2);
    if (result != null) {
        result.setType(EventType.ALTER);
        return result;
    }

    result = parseDdl(queryString, schmeaName, CREATE_PATTERN, 2);
    if (result != null) {
        result.setType(EventType.CREATE);
        return result;
    }

    result = parseDdl(queryString, schmeaName, DROP_PATTERN, 2);
    if (result != null) {
        result.setType(EventType.ERASE);
        return result;
    }

    result = parseDdl(queryString, schmeaName, TRUNCATE_PATTERN, 2);
    if (result != null) {
        result.setType(EventType.TRUNCATE);
        return result;
    }

    result = parseRename(queryString, schmeaName, RENAME_PATTERN);
    if (result != null) {
        result.setType(EventType.RENAME);

        String[] renameStrings = queryString.split(",");
        if (renameStrings.length > 1) {
            DdlResult lastResult = result;
            for (int i = 1; i < renameStrings.length; i++) {
                DdlResult ddlResult = parseRename(renameStrings[i], schmeaName, RENAME_REMNANT_PATTERN);
                ddlResult.setType(EventType.RENAME);
                lastResult.setRenameTableResult(ddlResult);
                lastResult = ddlResult;
            }
        }

        return result;
    }

    result = parseDdl(queryString, schmeaName, CREATE_INDEX_PATTERN, 5);
    if (result != null) {
        result.setType(EventType.CINDEX);
        return result;
    }

    result = parseDdl(queryString, schmeaName, DROP_INDEX_PATTERN, 2);
    if (result != null) {
        result.setType(EventType.DINDEX);
        return result;
    }

    result = new DdlResult(schmeaName);
    if (isDml(queryString, INSERT_PATTERN)) {
        result.setType(EventType.INSERT);
        return result;
    }

    if (isDml(queryString, UPDATE_PATTERN)) {
        result.setType(EventType.UPDATE);
        return result;
    }

    if (isDml(queryString, DELETE_PATTERN)) {
        result.setType(EventType.DELETE);
        return result;
    }

    result.setType(EventType.QUERY);
    return result;
}
 
Example 28
Source Project: canal-1.1.3   Source File: EventTransactionBuffer.java    License: Apache License 2.0 4 votes vote down vote up
private boolean isDml(EventType eventType) {
    return eventType == EventType.INSERT || eventType == EventType.UPDATE || eventType == EventType.DELETE;
}
 
Example 29
Source Project: canal-1.1.3   Source File: SimpleDdlParserTest.java    License: Apache License 2.0 4 votes vote down vote up
@Test
public void testCreate() {
    String queryString = "CREATE TABLE retl_mark ( `ID` int(11)";
    DdlResult result = SimpleDdlParser.parse(queryString, "retl");
    Assert.assertNotNull(result);
    Assert.assertEquals("retl", result.getSchemaName());
    Assert.assertEquals("retl_mark", result.getTableName());

    queryString = "CREATE TABLE IF NOT EXISTS retl.retl_mark ( `ID` int(11)";
    result = SimpleDdlParser.parse(queryString, "retl");
    Assert.assertNotNull(result);
    Assert.assertEquals("retl", result.getSchemaName());
    Assert.assertEquals("retl_mark", result.getTableName());

    queryString = "CREATE TABLE IF NOT EXISTS `retl_mark` ( `ID` int(11)";
    result = SimpleDdlParser.parse(queryString, "retl");
    Assert.assertNotNull(result);
    Assert.assertEquals("retl", result.getSchemaName());
    Assert.assertEquals("retl_mark", result.getTableName());

    queryString = "CREATE TABLE IF NOT EXISTS `retl.retl_mark` ( `ID` int(11)";
    result = SimpleDdlParser.parse(queryString, "retl");
    Assert.assertNotNull(result);
    Assert.assertEquals("retl", result.getSchemaName());
    Assert.assertEquals("retl_mark", result.getTableName());

    queryString = "CREATE TABLE  `retl`.`retl_mark` (\n  `ID` int(10) unsigned NOT NULL";
    result = SimpleDdlParser.parse(queryString, "retl");
    Assert.assertNotNull(result);
    Assert.assertEquals("retl", result.getSchemaName());
    Assert.assertEquals("retl_mark", result.getTableName());

    queryString = "CREATE TABLE  `retl`.`retl_mark`(\n  `ID` int(10) unsigned NOT NULL";
    result = SimpleDdlParser.parse(queryString, "retl");
    Assert.assertNotNull(result);
    Assert.assertEquals("retl", result.getSchemaName());
    Assert.assertEquals("retl_mark", result.getTableName());

    queryString = "CREATE table `bak591`.`j_order_log_back_201309` like j_order_log";
    result = SimpleDdlParser.parse(queryString, "bak");
    Assert.assertNotNull(result);
    Assert.assertEquals("bak591", result.getSchemaName());
    Assert.assertEquals("j_order_log_back_201309", result.getTableName());

    queryString = "CREATE DEFINER=sco*[email protected]% PROCEDURE SC_CPN_CODES_SAVE_ACTION(IN cosmosPassportId CHAR(32), IN orderId CHAR(32), IN codeIds TEXT) BEGIN SET @orderId = orderId; SET @timeNow = NOW(); START TRANSACTION; DELETE FROMsc_ord_couponWHEREORDER_ID= @orderId; SET @i=1; SET @numbers = FN_GET_ELEMENTS_COUNT(codeIds, '|'); WHILE @i <= @numbers DO SET @codeId = FN_FIND_ELEMENT_BYINDEX(codeIds, '|', @i); SET @orderCodeId = UUID32(); INSERT INTOsc_ord_coupon(ID,CREATE_BY,CREATE_TIME,UPDATE_BY,UPDATE_TIME,ORDER_ID,CODE_ID`) VALUES(@orderCodeId, cosmosPassportId, @timeNow, cosmosPassportId, @timeNow, @orderId, @codeId); SET @i = @i + 1; END WHILE; COMMIT; END";
    result = SimpleDdlParser.parse(queryString, "bak");
    Assert.assertEquals(EventType.QUERY, result.getType());
}
 
Example 30
Source Project: canal-1.1.3   Source File: DruidDdlParserTest.java    License: Apache License 2.0 4 votes vote down vote up
@Test
public void testCreate() {
    String queryString = "CREATE TABLE retl_mark ( `ID` int(11) )";
    DdlResult result = DruidDdlParser.parse(queryString, "retl").get(0);
    Assert.assertNotNull(result);
    Assert.assertEquals("retl", result.getSchemaName());
    Assert.assertEquals("retl_mark", result.getTableName());

    queryString = "CREATE TABLE IF NOT EXISTS retl.retl_mark ( `ID` int(11) )";
    result = DruidDdlParser.parse(queryString, "retl").get(0);
    Assert.assertNotNull(result);
    Assert.assertEquals("retl", result.getSchemaName());
    Assert.assertEquals("retl_mark", result.getTableName());

    queryString = "CREATE TABLE IF NOT EXISTS `retl_mark` ( `ID` int(11) )";
    result = DruidDdlParser.parse(queryString, "retl").get(0);
    Assert.assertNotNull(result);
    Assert.assertEquals("retl", result.getSchemaName());
    Assert.assertEquals("retl_mark", result.getTableName());

    queryString = "CREATE TABLE  `retl`.`retl_mark` (\n  `ID` int(10) unsigned NOT NULL )";
    result = DruidDdlParser.parse(queryString, "retl").get(0);
    Assert.assertNotNull(result);
    Assert.assertEquals("retl", result.getSchemaName());
    Assert.assertEquals("retl_mark", result.getTableName());

    queryString = "CREATE TABLE  `retl`.`retl_mark`(\n  `ID` int(10) unsigned NOT NULL )";
    result = SimpleDdlParser.parse(queryString, "retl");
    Assert.assertNotNull(result);
    Assert.assertEquals("retl", result.getSchemaName());
    Assert.assertEquals("retl_mark", result.getTableName());

    queryString = "CREATE table `bak591`.`j_order_log_back_201309` like j_order_log";
    result = DruidDdlParser.parse(queryString, "bak").get(0);
    Assert.assertNotNull(result);
    Assert.assertEquals("bak591", result.getSchemaName());
    Assert.assertEquals("j_order_log_back_201309", result.getTableName());

    queryString = "CREATE DEFINER=sco*[email protected]% PROCEDURE SC_CPN_CODES_SAVE_ACTION(IN cosmosPassportId CHAR(32), IN orderId CHAR(32), IN codeIds TEXT) BEGIN SET @orderId = orderId; SET @timeNow = NOW(); START TRANSACTION; DELETE FROMsc_ord_couponWHEREORDER_ID= @orderId; SET @i=1; SET @numbers = FN_GET_ELEMENTS_COUNT(codeIds, '|'); WHILE @i <= @numbers DO SET @codeId = FN_FIND_ELEMENT_BYINDEX(codeIds, '|', @i); SET @orderCodeId = UUID32(); INSERT INTOsc_ord_coupon(ID,CREATE_BY,CREATE_TIME,UPDATE_BY,UPDATE_TIME,ORDER_ID,CODE_ID`) VALUES(@orderCodeId, cosmosPassportId, @timeNow, cosmosPassportId, @timeNow, @orderId, @codeId); SET @i = @i + 1; END WHILE; COMMIT; END";
    result = DruidDdlParser.parse(queryString, "bak").get(0);
    Assert.assertEquals(EventType.QUERY, result.getType());

    queryString = "CREATE TABLE performance_schema.cond_instances(`ID` int(10) unsigned NOT NULL ) ";
    result = DruidDdlParser.parse(queryString, "retl").get(0);
    Assert.assertNotNull(result);
    Assert.assertEquals("performance_schema", result.getSchemaName());
    Assert.assertEquals("cond_instances", result.getTableName());
}