Documentation ¶
Index ¶
- Constants
- func ColValAsStr(v interface{}) string
- func GenDeleteSQL(changes ...*RowChange) (string, []interface{})
- func GenInsertSQL(tp DMLType, changes ...*RowChange) (string, []interface{})
- func GenUpdateSQL(changes ...*RowChange) (string, []any)
- func SameTypeTargetAndColumns(lhs *RowChange, rhs *RowChange) bool
- type DMLType
- type RowChange
- func (r *RowChange) CausalityKeys() []string
- func (r *RowChange) ColumnCount() int
- func (r *RowChange) GenSQL(tp DMLType) (string, []interface{})
- func (r *RowChange) GetApproximateDataSize() int64
- func (r *RowChange) GetPostValues() []interface{}
- func (r *RowChange) GetPreValues() []interface{}
- func (r *RowChange) GetSourceTable() *cdcmodel.TableName
- func (r *RowChange) GetTargetTable() *cdcmodel.TableName
- func (r *RowChange) HasNotNullUniqueIdx() bool
- func (r *RowChange) IdentityKey() string
- func (r *RowChange) IdentityValues() ([]interface{}, []interface{})
- func (r *RowChange) IsIdentityUpdated() bool
- func (r *RowChange) Reduce(preRowChange *RowChange)
- func (r *RowChange) RowIdentity() []interface{}
- func (r *RowChange) RowStrIdentity() []string
- func (r *RowChange) RowValues() []interface{}
- func (r *RowChange) SetApproximateDataSize(approximateDataSize int64)
- func (r *RowChange) SetWhereHandle(whereHandle *WhereHandle)
- func (r *RowChange) SourceTableInfo() *timodel.TableInfo
- func (r *RowChange) SplitUpdate() (*RowChange, *RowChange)
- func (r *RowChange) String() string
- func (r *RowChange) TargetTableID() string
- func (r *RowChange) Type() RowChangeType
- func (r *RowChange) UniqueNotNullIdx() *timodel.IndexInfo
- type RowChangeType
- type WhereHandle
Constants ¶
const ( // CommonIndexColumnsCount means common columns count of an index, index contains 1, 2, // , 3 or 4 columns are common, but index contains 5 columns or more are not that common, // so we use 4 as the common index column count. It will be used to pre-allocate slice space. CommonIndexColumnsCount = 4 )
Variables ¶
This section is empty.
Functions ¶
func ColValAsStr ¶
func ColValAsStr(v interface{}) string
ColValAsStr convert column value as string
func GenDeleteSQL ¶
GenDeleteSQL generates the DELETE SQL and its arguments. Input `changes` should have same target table and same columns for WHERE (typically same PK/NOT NULL UK), otherwise the behaviour is undefined.
func GenInsertSQL ¶
GenInsertSQL generates the INSERT SQL and its arguments. Input `changes` should have same target table and same modifiable columns, otherwise the behaviour is undefined.
func GenUpdateSQL ¶
GenUpdateSQL generates the UPDATE SQL and its arguments. Input `changes` should have same target table and same columns for WHERE (typically same PK/NOT NULL UK), otherwise the behaviour is undefined.
func SameTypeTargetAndColumns ¶
SameTypeTargetAndColumns check whether two row changes have same type, target and columns, so they can be merged to a multi-value DML.
Types ¶
type DMLType ¶
type DMLType int
DMLType indicates the type of DML.
type RowChange ¶
type RowChange struct {
// contains filtered or unexported fields
}
RowChange represents a row change, it can be further converted into DML SQL. It also provides some utility functions about calculating causality of two row changes, merging successive row changes into one row change, etc.
func NewRowChange ¶
func NewRowChange( sourceTable *cdcmodel.TableName, targetTable *cdcmodel.TableName, preValues []interface{}, postValues []interface{}, sourceTableInfo *timodel.TableInfo, downstreamTableInfo *timodel.TableInfo, tiCtx sessionctx.Context, ) *RowChange
NewRowChange creates a new RowChange. preValues stands for values exists before this change, postValues stands for values exists after this change. These parameters can be nil: - targetTable: when same as sourceTable or not applicable - preValues: when INSERT - postValues: when DELETE - targetTableInfo: when same as sourceTableInfo or not applicable - tiSessionCtx: will use default sessionCtx which is UTC timezone All arguments must not be changed after assigned to RowChange, any modification (like convert []byte to string) should be done before NewRowChange.
func (*RowChange) CausalityKeys ¶
CausalityKeys returns all string representation of causality keys. If two row changes has the same causality keys, they must be replicated sequentially.
func (*RowChange) ColumnCount ¶
ColumnCount returns the number of columns of this RowChange. TiDB TableInfo contains some internal columns like expression index, they are not included in this count.
func (*RowChange) GetApproximateDataSize ¶
GetApproximateDataSize returns internal approximateDataSize, it could be zero if this value is not set.
func (*RowChange) GetPostValues ¶
func (r *RowChange) GetPostValues() []interface{}
GetPostValues is only used in tests.
func (*RowChange) GetPreValues ¶
func (r *RowChange) GetPreValues() []interface{}
GetPreValues is only used in tests.
func (*RowChange) GetSourceTable ¶
GetSourceTable returns TableName of the source table.
func (*RowChange) GetTargetTable ¶
GetTargetTable returns TableName of the target table.
func (*RowChange) HasNotNullUniqueIdx ¶
HasNotNullUniqueIdx returns true when the target table structure has PK or UK whose columns are all NOT NULL.
func (*RowChange) IdentityKey ¶
IdentityKey returns a string generated by IdentityValues. If RowChange.IsIdentityUpdated, the behaviour is undefined.
func (*RowChange) IdentityValues ¶
func (r *RowChange) IdentityValues() ([]interface{}, []interface{})
IdentityValues returns the two group of values that can be used to identify the row. That is to say, if two row changes has same IdentityValues, they are changes of the same row. We can use this property to only replicate latest changes of one row. We always use same index for same table structure to get IdentityValues. two groups returned are from preValues and postValues.
func (*RowChange) IsIdentityUpdated ¶
IsIdentityUpdated returns true when the row is updated by the same values.
func (*RowChange) Reduce ¶
Reduce will merge two row changes of same row into one row changes, e.g., INSERT{1} + UPDATE{1 -> 2} -> INSERT{2}. Receiver will be changed in-place.
func (*RowChange) RowIdentity ¶
func (r *RowChange) RowIdentity() []interface{}
RowIdentity returns the identity of this row change, caller should call IsIdentityUpdated/SplitUpdate before calling this method to make sure it's not updating the identity itself. we extract identity from preValues for update/delete, postValues for insert. if there's no primary key, return all values.
func (*RowChange) RowStrIdentity ¶
RowStrIdentity returns the identity of the row change as string slice
func (*RowChange) RowValues ¶
func (r *RowChange) RowValues() []interface{}
RowValues returns the values of this row change for INSERT and UPDATE, it is the post values. for DELETE, it is the pre values.
func (*RowChange) SetApproximateDataSize ¶
SetApproximateDataSize sets the approximate size of row change.
func (*RowChange) SetWhereHandle ¶
func (r *RowChange) SetWhereHandle(whereHandle *WhereHandle)
SetWhereHandle can be used when caller has cached whereHandle, to avoid every RowChange lazily initialize it.
func (*RowChange) SourceTableInfo ¶
SourceTableInfo returns the TableInfo of source table.
func (*RowChange) SplitUpdate ¶
SplitUpdate will split current RowChangeUpdate into two RowChangeDelete and RowChangeInsert one. The behaviour is undefined for other types of RowChange.
func (*RowChange) TargetTableID ¶
TargetTableID returns a ID string for target table.
func (*RowChange) Type ¶
func (r *RowChange) Type() RowChangeType
Type returns the RowChangeType of this RowChange. Caller can future decide the DMLType when generate DML from it.
func (*RowChange) UniqueNotNullIdx ¶
UniqueNotNullIdx returns the unique and not null index.
type RowChangeType ¶
type RowChangeType int
RowChangeType is the type of row change.
const ( RowChangeNull RowChangeType = iota RowChangeInsert RowChangeUpdate RowChangeDelete )
these constants represent types of row change.
func (RowChangeType) String ¶
func (t RowChangeType) String() string
String implements fmt.Stringer interface.
type WhereHandle ¶
type WhereHandle struct { UniqueNotNullIdx *model.IndexInfo // If the index and columns have no NOT NULL constraint, but all data is NOT // NULL, we can still use it. // every index that is UNIQUE should be added to UniqueIdxs, even for // PK and NOT NULL. UniqueIdxs []*model.IndexInfo }
WhereHandle is used to generate a WHERE clause in SQL.
func GetWhereHandle ¶
func GetWhereHandle(source, target *model.TableInfo) *WhereHandle
GetWhereHandle calculates a WhereHandle by source/target TableInfo's indices, columns and state. Other component can cache the result.