1use squawk_syntax::ast;
2
3fn has_foreign_key_constraint(create_table: &ast::CreateTable) -> bool {
4 if let Some(table_arg_list) = create_table.table_arg_list() {
5 for arg in table_arg_list.args() {
6 match arg {
7 ast::TableArg::TableConstraint(ast::TableConstraint::ForeignKeyConstraint(_)) => {
8 return true;
9 }
10 ast::TableArg::Column(column) => {
11 if column
12 .constraints()
13 .any(|c| matches!(c, ast::ColumnConstraint::ReferencesConstraint(_)))
14 {
15 return true;
16 }
17 }
18 _ => (),
19 }
20 }
21 }
22 false
23}
24
25pub fn possibly_slow_stmt(stmt: &ast::Stmt) -> bool {
27 match stmt {
33 ast::Stmt::CreateTable(create_table) => has_foreign_key_constraint(create_table),
35 | ast::Stmt::AlterAggregate(_)
36 | ast::Stmt::AlterCollation(_)
37 | ast::Stmt::AlterConversion(_)
38 | ast::Stmt::AlterDatabase(_)
39 | ast::Stmt::AlterDefaultPrivileges(_)
40 | ast::Stmt::AlterDomain(_)
41 | ast::Stmt::AlterEventTrigger(_)
42 | ast::Stmt::AlterExtension(_)
43 | ast::Stmt::AlterForeignDataWrapper(_)
44 | ast::Stmt::AlterForeignTable(_)
45 | ast::Stmt::AlterFunction(_)
46 | ast::Stmt::AlterGroup(_)
47 | ast::Stmt::AlterIndex(_)
48 | ast::Stmt::AlterLanguage(_)
49 | ast::Stmt::AlterLargeObject(_)
50 | ast::Stmt::AlterMaterializedView(_)
51 | ast::Stmt::AlterOperator(_)
52 | ast::Stmt::AlterOperatorClass(_)
53 | ast::Stmt::AlterOperatorFamily(_)
54 | ast::Stmt::AlterPolicy(_)
55 | ast::Stmt::AlterProcedure(_)
56 | ast::Stmt::AlterPublication(_)
57 | ast::Stmt::AlterRole(_)
58 | ast::Stmt::AlterRoutine(_)
59 | ast::Stmt::AlterRule(_)
60 | ast::Stmt::AlterSchema(_)
61 | ast::Stmt::AlterSequence(_)
62 | ast::Stmt::AlterServer(_)
63 | ast::Stmt::AlterStatistics(_)
64 | ast::Stmt::AlterSubscription(_)
65 | ast::Stmt::AlterSystem(_)
66 | ast::Stmt::AlterTable(_)
67 | ast::Stmt::AlterTablespace(_)
68 | ast::Stmt::AlterTextSearchConfiguration(_)
69 | ast::Stmt::AlterTextSearchDictionary(_)
70 | ast::Stmt::AlterTextSearchParser(_)
71 | ast::Stmt::AlterTextSearchTemplate(_)
72 | ast::Stmt::AlterTrigger(_)
73 | ast::Stmt::AlterType(_)
74 | ast::Stmt::AlterUser(_)
75 | ast::Stmt::AlterUserMapping(_)
76 | ast::Stmt::AlterView(_)
77 | ast::Stmt::CreateAccessMethod(_)
78 | ast::Stmt::CreateAggregate(_)
79 | ast::Stmt::CreateCast(_)
80 | ast::Stmt::CreateCollation(_)
81 | ast::Stmt::CreateConversion(_)
82 | ast::Stmt::CreateDatabase(_)
83 | ast::Stmt::CreateDomain(_)
84 | ast::Stmt::CreateEventTrigger(_)
85 | ast::Stmt::CreateExtension(_)
86 | ast::Stmt::CreateForeignDataWrapper(_)
87 | ast::Stmt::CreateForeignTable(_)
88 | ast::Stmt::CreateFunction(_)
89 | ast::Stmt::CreateGroup(_)
90 | ast::Stmt::CreateIndex(_)
91 | ast::Stmt::CreateLanguage(_)
92 | ast::Stmt::CreateMaterializedView(_)
93 | ast::Stmt::CreateOperator(_)
94 | ast::Stmt::CreateOperatorClass(_)
95 | ast::Stmt::CreateOperatorFamily(_)
96 | ast::Stmt::CreatePolicy(_)
97 | ast::Stmt::CreateProcedure(_)
98 | ast::Stmt::CreatePublication(_)
99 | ast::Stmt::CreateRole(_)
100 | ast::Stmt::CreateRule(_)
101 | ast::Stmt::CreateSchema(_)
102 | ast::Stmt::CreateSequence(_)
103 | ast::Stmt::CreateServer(_)
104 | ast::Stmt::CreateStatistics(_)
105 | ast::Stmt::CreateSubscription(_)
106 | ast::Stmt::CreateTableAs(_)
107 | ast::Stmt::CreateTablespace(_)
108 | ast::Stmt::CreateTextSearchConfiguration(_)
109 | ast::Stmt::CreateTextSearchDictionary(_)
110 | ast::Stmt::CreateTextSearchParser(_)
111 | ast::Stmt::CreateTextSearchTemplate(_)
112 | ast::Stmt::CreateTransform(_)
113 | ast::Stmt::CreateTrigger(_)
114 | ast::Stmt::CreateType(_)
115 | ast::Stmt::CreateUser(_)
116 | ast::Stmt::CreateUserMapping(_)
117 | ast::Stmt::CreateView(_)
118 | ast::Stmt::DropAccessMethod(_)
119 | ast::Stmt::DropAggregate(_)
120 | ast::Stmt::DropCast(_)
121 | ast::Stmt::DropCollation(_)
122 | ast::Stmt::DropConversion(_)
123 | ast::Stmt::DropDatabase(_)
124 | ast::Stmt::DropDomain(_)
125 | ast::Stmt::DropEventTrigger(_)
126 | ast::Stmt::DropExtension(_)
127 | ast::Stmt::DropForeignDataWrapper(_)
128 | ast::Stmt::DropForeignTable(_)
129 | ast::Stmt::DropFunction(_)
130 | ast::Stmt::DropGroup(_)
131 | ast::Stmt::DropIndex(_)
132 | ast::Stmt::DropLanguage(_)
133 | ast::Stmt::DropMaterializedView(_)
134 | ast::Stmt::DropOperator(_)
135 | ast::Stmt::DropOperatorClass(_)
136 | ast::Stmt::DropOperatorFamily(_)
137 | ast::Stmt::DropOwned(_)
138 | ast::Stmt::DropPolicy(_)
139 | ast::Stmt::DropProcedure(_)
140 | ast::Stmt::DropPublication(_)
141 | ast::Stmt::DropRole(_)
142 | ast::Stmt::DropRoutine(_)
143 | ast::Stmt::DropRule(_)
144 | ast::Stmt::DropSchema(_)
145 | ast::Stmt::DropSequence(_)
146 | ast::Stmt::DropServer(_)
147 | ast::Stmt::DropStatistics(_)
148 | ast::Stmt::DropSubscription(_)
149 | ast::Stmt::DropTable(_)
150 | ast::Stmt::DropTablespace(_)
151 | ast::Stmt::DropTextSearchConfig(_)
152 | ast::Stmt::DropTextSearchDict(_)
153 | ast::Stmt::DropTextSearchParser(_)
154 | ast::Stmt::DropTextSearchTemplate(_)
155 | ast::Stmt::DropTransform(_)
156 | ast::Stmt::DropTrigger(_)
157 | ast::Stmt::DropType(_)
158 | ast::Stmt::DropUser(_)
159 | ast::Stmt::DropUserMapping(_)
160 | ast::Stmt::DropView(_)
161 | ast::Stmt::Cluster(_)
163 | ast::Stmt::CommentOn(_)
164 | ast::Stmt::ImportForeignSchema(_)
165 | ast::Stmt::Load(_)
166 | ast::Stmt::Lock(_)
167 | ast::Stmt::Refresh(_)
168 | ast::Stmt::Reindex(_)
169 | ast::Stmt::Truncate(_)
170 | ast::Stmt::Vacuum(_)
171 => true,
172 ast::Stmt::Analyze(_)
173 | ast::Stmt::Begin(_)
174 | ast::Stmt::Call(_)
175 | ast::Stmt::Checkpoint(_)
176 | ast::Stmt::Close(_)
177 | ast::Stmt::Commit(_)
178 | ast::Stmt::Copy(_)
179 | ast::Stmt::Deallocate(_)
180 | ast::Stmt::Declare(_)
181 | ast::Stmt::Delete(_)
182 | ast::Stmt::Discard(_)
183 | ast::Stmt::Do(_)
184 | ast::Stmt::Execute(_)
185 | ast::Stmt::Explain(_)
186 | ast::Stmt::Fetch(_)
187 | ast::Stmt::Grant(_)
188 | ast::Stmt::Insert(_)
189 | ast::Stmt::Listen(_)
190 | ast::Stmt::Merge(_)
191 | ast::Stmt::Move(_)
192 | ast::Stmt::Notify(_)
193 | ast::Stmt::ParenSelect(_)
194 | ast::Stmt::Prepare(_)
195 | ast::Stmt::PrepareTransaction(_)
196 | ast::Stmt::Reassign(_)
197 | ast::Stmt::ReleaseSavepoint(_)
198 | ast::Stmt::Reset(_)
199 | ast::Stmt::Revoke(_)
200 | ast::Stmt::Rollback(_)
201 | ast::Stmt::Savepoint(_)
202 | ast::Stmt::SecurityLabel(_)
203 | ast::Stmt::Select(_)
204 | ast::Stmt::SelectInto(_)
205 | ast::Stmt::Set(_)
206 | ast::Stmt::SetConstraints(_)
207 | ast::Stmt::SetRole(_)
208 | ast::Stmt::SetSessionAuth(_)
209 | ast::Stmt::ResetSessionAuth(_)
210 | ast::Stmt::SetTransaction(_)
211 | ast::Stmt::Show(_)
212 | ast::Stmt::Table(_)
213 | ast::Stmt::Unlisten(_)
214 | ast::Stmt::Update(_)
215 | ast::Stmt::Values(_) => false,
216 }
217}
218
219#[cfg(test)]
220mod tests {
221 use super::*;
222 use squawk_syntax::SourceFile;
223
224 #[test]
225 fn alter_table() {
226 let sql = "ALTER TABLE users ADD COLUMN email TEXT;";
227 let file = SourceFile::parse(sql);
228 let stmts = file.tree().stmts().next().unwrap();
229 assert!(possibly_slow_stmt(&stmts));
230 }
231
232 #[test]
233 fn select() {
234 let sql = "select 1;";
235 let file = SourceFile::parse(sql);
236 let stmts = file.tree().stmts().next().unwrap();
237 assert!(!possibly_slow_stmt(&stmts));
238 }
239
240 #[test]
241 fn create_table_without_foreign_key() {
242 let sql = "create table foo (id integer generated by default as identity primary key);";
243 let file = SourceFile::parse(sql);
244 let stmts = file.tree().stmts().next().unwrap();
245 assert!(!possibly_slow_stmt(&stmts));
246 }
247
248 #[test]
249 fn create_table_with_foreign_key() {
250 let sql = "create table foo (id integer, user_id integer references users(id));";
251 let file = SourceFile::parse(sql);
252 let stmts = file.tree().stmts().next().unwrap();
253 assert!(possibly_slow_stmt(&stmts));
254 }
255
256 #[test]
257 fn create_table_with_table_level_foreign_key() {
258 let sql = "create table foo (id integer, user_id integer, foreign key (user_id) references users(id));";
259 let file = SourceFile::parse(sql);
260 let stmts = file.tree().stmts().next().unwrap();
261 assert!(possibly_slow_stmt(&stmts));
262 }
263}