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