sqlmodel

package
v0.0.0-...-44a46bf Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 25, 2024 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
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

func GenDeleteSQL(changes ...*RowChange) (string, []interface{})

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

func GenInsertSQL(tp DMLType, changes ...*RowChange) (string, []interface{})

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

func GenUpdateSQL(changes ...*RowChange) (string, []any)

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

func SameTypeTargetAndColumns(lhs *RowChange, rhs *RowChange) bool

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.

const (
	DMLNull DMLType = iota
	DMLInsert
	DMLReplace
	DMLInsertOnDuplicateUpdate
	DMLUpdate
	DMLDelete
)

these constants represent types of row change.

func (DMLType) String

func (t DMLType) String() string

String implements fmt.Stringer interface.

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

func (r *RowChange) CausalityKeys() []string

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

func (r *RowChange) ColumnCount() int

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) GenSQL

func (r *RowChange) GenSQL(tp DMLType) (string, []interface{})

GenSQL generated a DML SQL for this RowChange.

func (*RowChange) GetApproximateDataSize

func (r *RowChange) GetApproximateDataSize() int64

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

func (r *RowChange) GetSourceTable() *cdcmodel.TableName

GetSourceTable returns TableName of the source table.

func (*RowChange) GetTargetTable

func (r *RowChange) GetTargetTable() *cdcmodel.TableName

GetTargetTable returns TableName of the target table.

func (*RowChange) HasNotNullUniqueIdx

func (r *RowChange) HasNotNullUniqueIdx() bool

HasNotNullUniqueIdx returns true when the target table structure has PK or UK whose columns are all NOT NULL.

func (*RowChange) IdentityKey

func (r *RowChange) IdentityKey() string

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

func (r *RowChange) IsIdentityUpdated() bool

IsIdentityUpdated returns true when the row is updated by the same values.

func (*RowChange) Reduce

func (r *RowChange) Reduce(preRowChange *RowChange)

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

func (r *RowChange) RowStrIdentity() []string

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

func (r *RowChange) SetApproximateDataSize(approximateDataSize int64)

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

func (r *RowChange) SourceTableInfo() *timodel.TableInfo

SourceTableInfo returns the TableInfo of source table.

func (*RowChange) SplitUpdate

func (r *RowChange) SplitUpdate() (*RowChange, *RowChange)

SplitUpdate will split current RowChangeUpdate into two RowChangeDelete and RowChangeInsert one. The behaviour is undefined for other types of RowChange.

func (*RowChange) String

func (r *RowChange) String() string

String implements Stringer interface.

func (*RowChange) TargetTableID

func (r *RowChange) TargetTableID() string

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

func (r *RowChange) UniqueNotNullIdx() *timodel.IndexInfo

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL