MCPcopy
hub / github.com/pingcap/tidb / GetPrimaryKeyColumns

Function GetPrimaryKeyColumns

dumpling/export/sql.go:642–657  ·  view source on GitHub ↗

GetPrimaryKeyColumns gets all primary columns in ordinal order

(tctx *tcontext.Context, db *BaseConn, database, table string)

Source from the content-addressed store, hash-verified

640
641// GetPrimaryKeyColumns gets all primary columns in ordinal order
642func GetPrimaryKeyColumns(tctx *tcontext.Context, db *BaseConn, database, table string) ([]string, error) {
643 priKeyColsQuery := fmt.Sprintf("SHOW INDEX FROM `%s`.`%s`", escapeString(database), escapeString(table))
644 results, err := db.QuerySQLWithColumns(tctx, []string{"KEY_NAME", "COLUMN_NAME"}, priKeyColsQuery)
645 if err != nil {
646 return nil, err
647 }
648
649 cols := make([]string, 0, len(results))
650 for _, oneRow := range results {
651 keyName, columnName := oneRow[0], oneRow[1]
652 if keyName == "PRIMARY" {
653 cols = append(cols, columnName)
654 }
655 }
656 return cols, nil
657}
658
659// getNumericIndex picks up indices according to the following priority:
660// primary key > unique key with the smallest count > key with the max cardinality

Callers 2

buildOrderByClauseFunction · 0.85

Calls 2

QuerySQLWithColumnsMethod · 0.80
escapeStringFunction · 0.70

Tested by

no test coverage detected