-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathconstraint.go
108 lines (93 loc) · 3.69 KB
/
constraint.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright 2020 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package descpb
import (
"strconv"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/catpb"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
)
// CompositeKeyMatchMethodValue allows the conversion from a
// tree.ReferenceCompositeKeyMatchMethod to a ForeignKeyReference_Match.
var CompositeKeyMatchMethodValue = [...]ForeignKeyReference_Match{
tree.MatchSimple: ForeignKeyReference_SIMPLE,
tree.MatchFull: ForeignKeyReference_FULL,
tree.MatchPartial: ForeignKeyReference_PARTIAL,
}
// ForeignKeyReferenceMatchValue allows the conversion from a
// ForeignKeyReference_Match to a tree.ReferenceCompositeKeyMatchMethod.
// This should match CompositeKeyMatchMethodValue.
var ForeignKeyReferenceMatchValue = [...]tree.CompositeKeyMatchMethod{
ForeignKeyReference_SIMPLE: tree.MatchSimple,
ForeignKeyReference_FULL: tree.MatchFull,
ForeignKeyReference_PARTIAL: tree.MatchPartial,
}
// String implements the fmt.Stringer interface.
func (x ForeignKeyReference_Match) String() string {
switch x {
case ForeignKeyReference_SIMPLE:
return "MATCH SIMPLE"
case ForeignKeyReference_FULL:
return "MATCH FULL"
case ForeignKeyReference_PARTIAL:
return "MATCH PARTIAL"
default:
return strconv.Itoa(int(x))
}
}
// ForeignKeyReferenceActionType allows the conversion between a
// tree.ReferenceAction and a ForeignKeyReference_Action.
var ForeignKeyReferenceActionType = [...]tree.ReferenceAction{
catpb.ForeignKeyAction_NO_ACTION: tree.NoAction,
catpb.ForeignKeyAction_RESTRICT: tree.Restrict,
catpb.ForeignKeyAction_SET_DEFAULT: tree.SetDefault,
catpb.ForeignKeyAction_SET_NULL: tree.SetNull,
catpb.ForeignKeyAction_CASCADE: tree.Cascade,
}
// ForeignKeyReferenceActionValue allows the conversion between a
// catpb.ForeignKeyAction_Action and a tree.ReferenceAction.
var ForeignKeyReferenceActionValue = [...]catpb.ForeignKeyAction{
tree.NoAction: catpb.ForeignKeyAction_NO_ACTION,
tree.Restrict: catpb.ForeignKeyAction_RESTRICT,
tree.SetDefault: catpb.ForeignKeyAction_SET_DEFAULT,
tree.SetNull: catpb.ForeignKeyAction_SET_NULL,
tree.Cascade: catpb.ForeignKeyAction_CASCADE,
}
// ConstraintType is used to identify the type of a constraint.
type ConstraintType string
const (
// ConstraintTypePK identifies a PRIMARY KEY constraint.
ConstraintTypePK ConstraintType = "PRIMARY KEY"
// ConstraintTypeFK identifies a FOREIGN KEY constraint.
ConstraintTypeFK ConstraintType = "FOREIGN KEY"
// ConstraintTypeUnique identifies a UNIQUE constraint.
ConstraintTypeUnique ConstraintType = "UNIQUE"
// ConstraintTypeCheck identifies a CHECK constraint.
ConstraintTypeCheck ConstraintType = "CHECK"
)
// ConstraintDetail describes a constraint.
//
// TODO(ajwerner): Lift this up a level of abstraction next to the
// Immutable and have it store those for the ReferencedTable.
type ConstraintDetail struct {
Kind ConstraintType
ConstraintID ConstraintID
Columns []string
Details string
Unvalidated bool
// Only populated for PK and Unique Constraints with an index.
Index *IndexDescriptor
// Only populated for Unique Constraints without an index.
UniqueWithoutIndexConstraint *UniqueWithoutIndexConstraint
// Only populated for FK Constraints.
FK *ForeignKeyConstraint
ReferencedTable *TableDescriptor
// Only populated for Check Constraints.
CheckConstraint *TableDescriptor_CheckConstraint
}