@@ -17,6 +17,9 @@ package tree
17
17
import (
18
18
"fmt"
19
19
"strings"
20
+
21
+ "github.com/cockroachdb/cockroach/pkg/util/buildutil"
22
+ "github.com/cockroachdb/errors"
20
23
)
21
24
22
25
// Instructions for creating new types: If a type needs to satisfy an
@@ -142,25 +145,36 @@ type Statement interface {
142
145
StatementTag () string
143
146
}
144
147
145
- // canModifySchema is to be implemented by statements that can modify
146
- // the database schema but may have StatementReturnType() != DDL.
147
- // See CanModifySchema() below.
148
- type canModifySchema interface {
149
- modifiesSchema () bool
150
- }
151
-
152
148
// CanModifySchema returns true if the statement can modify
153
149
// the database schema.
154
150
func CanModifySchema (stmt Statement ) bool {
155
151
if stmt == nil {
156
152
// Some drivers send empty queries to test the connection.
157
153
return false
158
154
}
159
- if stmt .StatementReturnType () == DDL || stmt .StatementType () == TypeDDL {
155
+ if t := stmt .StatementType (); t == TypeDML {
156
+ // Return early for the common case of DML, which never modify schema.
157
+ if buildutil .CrdbTestBuild {
158
+ // Assert this invariant in test builds.
159
+ if stmt .StatementReturnType () == DDL {
160
+ panic (errors .AssertionFailedf ("DML statement %T has unexpected DDL return type" , stmt ))
161
+ }
162
+ // Also assert that the special cases are not TypeDML.
163
+ switch stmt .(type ) {
164
+ case * Discard , * SetZoneConfig :
165
+ panic (errors .AssertionFailedf ("%T has unexpected DDL statement type" , stmt ))
166
+ }
167
+ }
168
+ return false
169
+ } else if t == TypeDDL || stmt .StatementReturnType () == DDL {
160
170
return true
161
171
}
162
- scm , ok := stmt .(canModifySchema )
163
- return ok && scm .modifiesSchema ()
172
+ // Special cases for non-DDL statements which modify the schema.
173
+ switch stmt .(type ) {
174
+ case * Discard , * SetZoneConfig :
175
+ return true
176
+ }
177
+ return false
164
178
}
165
179
166
180
// CanWriteData returns true if the statement can modify data.
@@ -473,9 +487,6 @@ func (*AlterPolicy) StatementTag() string { return AlterPolicyTag }
473
487
474
488
func (* AlterPolicy ) hiddenFromShowQueries () {}
475
489
476
- // modifiesSchema implements the canModifySchema interface.
477
- func (* AlterPolicy ) modifiesSchema () bool { return true }
478
-
479
490
// StatementReturnType implements the Statement interface.
480
491
func (* AlterTable ) StatementReturnType () StatementReturnType { return DDL }
481
492
@@ -1055,9 +1066,6 @@ func (*CreatePolicy) StatementTag() string { return CreatePolicyTag }
1055
1066
1056
1067
func (* CreatePolicy ) hiddenFromShowQueries () {}
1057
1068
1058
- // modifiesSchema implements the canModifySchema interface.
1059
- func (* CreatePolicy ) modifiesSchema () bool { return true }
1060
-
1061
1069
// StatementReturnType implements the Statement interface.
1062
1070
func (n * CreateSchema ) StatementReturnType () StatementReturnType { return DDL }
1063
1071
@@ -1069,9 +1077,6 @@ func (n *CreateSchema) StatementTag() string {
1069
1077
return CreateSchemaTag
1070
1078
}
1071
1079
1072
- // modifiesSchema implements the canModifySchema interface.
1073
- func (* CreateSchema ) modifiesSchema () bool { return true }
1074
-
1075
1080
// StatementReturnType implements the Statement interface.
1076
1081
func (n * CreateTable ) StatementReturnType () StatementReturnType { return DDL }
1077
1082
@@ -1086,9 +1091,6 @@ func (n *CreateTable) StatementTag() string {
1086
1091
return "CREATE TABLE"
1087
1092
}
1088
1093
1089
- // modifiesSchema implements the canModifySchema interface.
1090
- func (* CreateTable ) modifiesSchema () bool { return true }
1091
-
1092
1094
// StatementReturnType implements the Statement interface.
1093
1095
func (* CreateType ) StatementReturnType () StatementReturnType { return DDL }
1094
1096
@@ -1098,8 +1100,6 @@ func (*CreateType) StatementType() StatementType { return TypeDDL }
1098
1100
// StatementTag implements the Statement interface.
1099
1101
func (* CreateType ) StatementTag () string { return "CREATE TYPE" }
1100
1102
1101
- func (* CreateType ) modifiesSchema () bool { return true }
1102
-
1103
1103
// StatementReturnType implements the Statement interface.
1104
1104
func (* CreateRole ) StatementReturnType () StatementReturnType { return DDL }
1105
1105
@@ -1168,9 +1168,6 @@ func (d *Discard) StatementTag() string {
1168
1168
return "DISCARD"
1169
1169
}
1170
1170
1171
- // modifiesSchema implements the canModifySchema interface.
1172
- func (* Discard ) modifiesSchema () bool { return true }
1173
-
1174
1171
// StatementReturnType implements the Statement interface.
1175
1172
func (n * DeclareCursor ) StatementReturnType () StatementReturnType { return Ack }
1176
1173
@@ -1218,9 +1215,6 @@ func (*DropPolicy) StatementTag() string { return DropPolicyTag }
1218
1215
1219
1216
func (* DropPolicy ) hiddenFromShowQueries () {}
1220
1217
1221
- // modifiesSchema implements the canModifySchema interface.
1222
- func (* DropPolicy ) modifiesSchema () bool { return true }
1223
-
1224
1218
// StatementReturnType implements the Statement interface.
1225
1219
func (* DropTable ) StatementReturnType () StatementReturnType { return DDL }
1226
1220
@@ -2348,9 +2342,6 @@ func (*Truncate) StatementType() StatementType { return TypeDDL }
2348
2342
// StatementTag returns a short string identifying the type of statement.
2349
2343
func (* Truncate ) StatementTag () string { return TruncateTag }
2350
2344
2351
- // modifiesSchema implements the canModifySchema interface.
2352
- func (* Truncate ) modifiesSchema () bool { return true }
2353
-
2354
2345
// StatementReturnType implements the Statement interface.
2355
2346
func (n * Update ) StatementReturnType () StatementReturnType { return n .Returning .statementReturnType () }
2356
2347
0 commit comments