github.com/cockroachdb/cockroachdb-parser@v0.23.3-0.20240213214944-911057d40c9a/pkg/sql/sem/tree/constraint.go (about) 1 // Copyright 2022 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 package tree 12 13 import ( 14 "strconv" 15 16 "github.com/cockroachdb/cockroachdb-parser/pkg/sql/sem/semenumpb" 17 ) 18 19 // ReferenceAction is the method used to maintain referential integrity through 20 // foreign keys. 21 type ReferenceAction semenumpb.ForeignKeyAction 22 23 // The values for ReferenceAction. It has a one-to-one mapping to semenumpb.ForeignKeyAction. 24 const ( 25 NoAction ReferenceAction = iota 26 Restrict 27 SetNull 28 SetDefault 29 Cascade 30 ) 31 32 // ReferenceActions contains the actions specified to maintain referential 33 // integrity through foreign keys for different operations. 34 type ReferenceActions struct { 35 Delete ReferenceAction 36 Update ReferenceAction 37 } 38 39 // Format implements the NodeFormatter interface. 40 func (node *ReferenceActions) Format(ctx *FmtCtx) { 41 if node.Delete != NoAction { 42 ctx.WriteString(" ON DELETE ") 43 ctx.WriteString(node.Delete.String()) 44 } 45 if node.Update != NoAction { 46 ctx.WriteString(" ON UPDATE ") 47 ctx.WriteString(node.Update.String()) 48 } 49 } 50 51 // ForeignKeyReferenceActionType allows the conversion between a 52 // tree.ReferenceAction and a ForeignKeyReference_Action. 53 var ForeignKeyReferenceActionType = [...]ReferenceAction{ 54 semenumpb.ForeignKeyAction_NO_ACTION: NoAction, 55 semenumpb.ForeignKeyAction_RESTRICT: Restrict, 56 semenumpb.ForeignKeyAction_SET_DEFAULT: SetDefault, 57 semenumpb.ForeignKeyAction_SET_NULL: SetNull, 58 semenumpb.ForeignKeyAction_CASCADE: Cascade, 59 } 60 61 // ForeignKeyReferenceActionValue allows the conversion between a 62 // semenumpb.ForeignKeyAction_Action and a tree.ReferenceAction. 63 var ForeignKeyReferenceActionValue = [...]semenumpb.ForeignKeyAction{ 64 NoAction: semenumpb.ForeignKeyAction_NO_ACTION, 65 Restrict: semenumpb.ForeignKeyAction_RESTRICT, 66 SetDefault: semenumpb.ForeignKeyAction_SET_DEFAULT, 67 SetNull: semenumpb.ForeignKeyAction_SET_NULL, 68 Cascade: semenumpb.ForeignKeyAction_CASCADE, 69 } 70 71 // String implements the fmt.Stringer interface. 72 func (x ReferenceAction) String() string { 73 switch x { 74 case Restrict: 75 return "RESTRICT" 76 case SetDefault: 77 return "SET DEFAULT" 78 case SetNull: 79 return "SET NULL" 80 case Cascade: 81 return "CASCADE" 82 default: 83 return strconv.Itoa(int(x)) 84 } 85 } 86 87 // CompositeKeyMatchMethod is the algorithm use when matching composite keys. 88 // See https://github.com/cockroachdb/cockroachdb-parser/issues/20305 or 89 // https://www.postgresql.org/docs/11/sql-createtable.html for details on the 90 // different composite foreign key matching methods. 91 type CompositeKeyMatchMethod semenumpb.Match 92 93 // The values for CompositeKeyMatchMethod. 94 const ( 95 MatchSimple CompositeKeyMatchMethod = iota 96 MatchFull 97 MatchPartial // Note: PARTIAL not actually supported at this point. 98 ) 99 100 // CompositeKeyMatchMethodType allows the conversion from a 101 // ForeignKeyReference_Match to a tree.ReferenceCompositeKeyMatchMethod. 102 // This should match CompositeKeyMatchMethodValue. 103 var CompositeKeyMatchMethodType = [...]CompositeKeyMatchMethod{ 104 semenumpb.Match_SIMPLE: MatchSimple, 105 semenumpb.Match_FULL: MatchFull, 106 semenumpb.Match_PARTIAL: MatchPartial, 107 } 108 109 // CompositeKeyMatchMethodValue allows the conversion from a 110 // tree.ReferenceCompositeKeyMatchMethod to a ForeignKeyReference_Match. 111 var CompositeKeyMatchMethodValue = [...]semenumpb.Match{ 112 MatchSimple: semenumpb.Match_SIMPLE, 113 MatchFull: semenumpb.Match_FULL, 114 MatchPartial: semenumpb.Match_PARTIAL, 115 } 116 117 // String implements the fmt.Stringer interface. 118 func (x CompositeKeyMatchMethod) String() string { 119 switch x { 120 case MatchSimple: 121 return "MATCH SIMPLE" 122 case MatchFull: 123 return "MATCH FULL" 124 case MatchPartial: 125 return "MATCH PARTIAL" 126 default: 127 return strconv.Itoa(int(x)) 128 } 129 }