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