syntax = "proto2";
package com.tnp.search;

import "gogo.proto";

option java_package ="com.pingcap.kafkareader.proto";
option java_outer_classname = "BinLogInfo";


option (gogoproto.marshaler_all) = true;
option (gogoproto.sizer_all) = true;
option (gogoproto.unmarshaler_all) = true;

// for date and time type, string_value is set
// protoc  --java_out=src/main/java src/main/resources/proto/binlog.proto
// for int type int64 or uint64_value is set
// for float, double, double_value is set
// for decimal, string_value is set in human-readable now
// for bit, bytes_value is set

// for text and char type, string_value is set
// for blob and binary type, bytes_value is set
// for enum, set, uint64_value is set
// for json, bytes_value is set
message Column {
  optional bool is_null = 1 [ default = false ];
  optional int64 int64_value = 2;
  optional uint64 uint64_value = 3;
  optional double double_value = 4;
  optional bytes bytes_value = 5;
  optional string string_value = 6;
}

message ColumnInfo {
  optional string name = 1 [ (gogoproto.nullable) = false ];
  // lower case column field type in mysql
  // https://dev.mysql.com/doc/refman/8.0/en/data-types.html
  // for numeric type: int bigint smallint tinyint float double decimal bit
  // for string type: text longtext mediumtext char tinytext varchar
  // blob longblob mediumblob binary tinyblob varbinary
  // enum set
  // for json type: json
  optional string mysql_type = 2 [ (gogoproto.nullable) = false ];
  optional bool is_primary_key = 3 [ (gogoproto.nullable) = false ];
}

message Row { repeated Column columns = 1; }

enum MutationType {
  Insert = 0;
  Update = 1;
  Delete = 2;
}

//  Table contains mutations in a table.
message Table {
  optional string schema_name = 1;
  optional string table_name = 2;
  repeated ColumnInfo column_info = 3;
  repeated TableMutation mutations = 4;
}

message TableMutation {
  required MutationType type = 1;
  //update之后新的值
  required Row row = 2;
  // for Update MutationType only[update之前的老的值]
  optional Row change_row = 3;
}

message DMLData {
  // tables contains all the table changes.
  repeated Table tables = 1;
}

message DDLData {
  // the current database use
  optional string schema_name = 1;
  // the relate table
  optional string table_name = 2;
  // ddl_query is the original ddl statement query.
  optional bytes ddl_query = 3;
}

enum BinlogType {
  DML = 0; //  has dml_data
  DDL = 1; //  has ddl_query
}

// Binlog contains all the changes in a transaction.
message Binlog {
  optional BinlogType type = 1 [ (gogoproto.nullable) = false ];
  optional int64 commit_ts = 2 [ (gogoproto.nullable) = false ];
  // dml_data is marshalled from DML type
  optional DMLData dml_data = 3;
  optional DDLData ddl_data = 4;
}