sql_query_analyzer 0.13.0

Static analysis tool for SQL queries with 32 built-in rules for performance, security, and style
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
// SPDX-FileCopyrightText: 2025 RAprogramm
// SPDX-License-Identifier: MIT

use sql_query_analyzer::{
    config::RulesConfig,
    query::{SqlDialect, parse_queries},
    rules::{RuleRunner, Severity},
    schema::Schema
};

fn analyze_query(sql: &str) -> Vec<String> {
    let queries = parse_queries(sql, SqlDialect::Generic).unwrap();
    let runner = RuleRunner::new();
    let report = runner.analyze(&queries);
    report
        .violations
        .iter()
        .map(|v| v.rule_id.to_string())
        .collect()
}

fn analyze_with_schema(sql: &str, schema_sql: &str) -> Vec<String> {
    let queries = parse_queries(sql, SqlDialect::Generic).unwrap();
    let schema = Schema::parse(schema_sql, SqlDialect::Generic).unwrap();
    let runner = RuleRunner::with_schema_and_config(schema, RulesConfig::default());
    let report = runner.analyze(&queries);
    report
        .violations
        .iter()
        .map(|v| v.rule_id.to_string())
        .collect()
}

#[test]
fn test_select_star_without_limit() {
    let violations = analyze_query("SELECT * FROM users");
    assert!(violations.contains(&"PERF001".to_string()));
}

#[test]
fn test_select_star_with_limit() {
    let violations = analyze_query("SELECT * FROM users LIMIT 10");
    assert!(!violations.contains(&"PERF001".to_string()));
}

#[test]
fn test_leading_wildcard() {
    let violations = analyze_query("SELECT * FROM users WHERE name LIKE '%test'");
    assert!(violations.contains(&"PERF002".to_string()));
}

#[test]
fn test_trailing_wildcard_ok() {
    let violations = analyze_query("SELECT * FROM users WHERE name LIKE 'test%' LIMIT 10");
    assert!(!violations.contains(&"PERF002".to_string()));
}

#[test]
fn test_large_offset() {
    let violations = analyze_query("SELECT * FROM users LIMIT 10 OFFSET 5000");
    assert!(violations.contains(&"PERF004".to_string()));
}

#[test]
fn test_small_offset_ok() {
    let violations = analyze_query("SELECT * FROM users LIMIT 10 OFFSET 100");
    assert!(!violations.contains(&"PERF004".to_string()));
}

#[test]
fn test_select_without_where() {
    let violations = analyze_query("SELECT * FROM users");
    assert!(violations.contains(&"PERF011".to_string()));
}

#[test]
fn test_select_with_where() {
    let violations = analyze_query("SELECT * FROM users WHERE id = 1 LIMIT 10");
    assert!(!violations.contains(&"PERF011".to_string()));
}

#[test]
fn test_select_star_style() {
    let violations = analyze_query("SELECT * FROM users LIMIT 10");
    assert!(violations.contains(&"STYLE001".to_string()));
}

#[test]
fn test_ordinal_in_order_by() {
    let violations = analyze_query("SELECT id, name FROM users WHERE id > 0 ORDER BY 1 LIMIT 5");
    assert!(violations.contains(&"STYLE004".to_string()));
}

#[test]
fn test_ordinal_in_group_by() {
    let violations =
        analyze_query("SELECT name, COUNT(*) FROM users WHERE id > 0 GROUP BY 1 LIMIT 5");
    assert!(violations.contains(&"STYLE004".to_string()));
}

#[test]
fn test_ordinal_in_order_by_list() {
    let violations =
        analyze_query("SELECT id, name FROM users WHERE id > 0 ORDER BY name, 2 LIMIT 5");
    assert!(violations.contains(&"STYLE004".to_string()));
}

#[test]
fn test_explicit_order_by_ok() {
    let violations =
        analyze_query("SELECT id, name FROM users WHERE id > 0 ORDER BY name DESC LIMIT 5");
    assert!(!violations.contains(&"STYLE004".to_string()));
}

#[test]
fn test_limit_count_not_ordinal() {
    let violations =
        analyze_query("SELECT id, name FROM users WHERE id > 0 ORDER BY name LIMIT 1");
    assert!(!violations.contains(&"STYLE004".to_string()));
}

#[test]
fn test_function_args_not_ordinal() {
    let violations = analyze_query(
        "SELECT id, name FROM users WHERE id > 0 ORDER BY COALESCE(name, 1) LIMIT 5"
    );
    assert!(!violations.contains(&"STYLE004".to_string()));
}

#[test]
fn test_count_star_without_where() {
    let violations = analyze_query("SELECT COUNT(*) FROM users");
    assert!(violations.contains(&"PERF012".to_string()));
}

#[test]
fn test_count_one_without_where() {
    let violations = analyze_query("SELECT COUNT(1) FROM orders");
    assert!(violations.contains(&"PERF012".to_string()));
}

#[test]
fn test_count_with_where_ok() {
    let violations = analyze_query("SELECT COUNT(*) FROM users WHERE status = 'active'");
    assert!(!violations.contains(&"PERF012".to_string()));
}

#[test]
fn test_select_without_count_not_perf012() {
    let violations = analyze_query("SELECT id FROM users");
    assert!(!violations.contains(&"PERF012".to_string()));
}

#[test]
fn test_distinct_with_join_flagged() {
    let violations = analyze_query(
        "SELECT DISTINCT u.name FROM users u JOIN orders o ON o.user_id = u.id WHERE u.id > 0 LIMIT 10"
    );
    assert!(violations.contains(&"PERF014".to_string()));
}

#[test]
fn test_distinct_star_is_warning() {
    let queries = parse_queries(
        "SELECT DISTINCT * FROM users u JOIN orders o ON o.user_id = u.id WHERE u.id > 0 LIMIT 10",
        SqlDialect::Generic
    )
    .unwrap();
    let report = RuleRunner::new().analyze(&queries);
    let violation = report
        .violations
        .iter()
        .find(|v| v.rule_id == "PERF014")
        .unwrap();
    assert_eq!(violation.severity, Severity::Warning);
}

#[test]
fn test_distinct_single_table_ok() {
    let violations = analyze_query("SELECT DISTINCT status FROM orders WHERE id > 0 LIMIT 10");
    assert!(!violations.contains(&"PERF014".to_string()));
}

#[test]
fn test_having_without_aggregate_flagged() {
    let violations = analyze_query(
        "SELECT status, COUNT(*) FROM orders WHERE id > 0 GROUP BY status HAVING status = 'active' LIMIT 10"
    );
    assert!(violations.contains(&"PERF018".to_string()));
}

#[test]
fn test_having_with_aggregate_ok() {
    let violations = analyze_query(
        "SELECT status, COUNT(*) FROM orders WHERE id > 0 GROUP BY status HAVING COUNT(*) > 10 LIMIT 10"
    );
    assert!(!violations.contains(&"PERF018".to_string()));
}

#[test]
fn test_no_having_not_flagged() {
    let violations =
        analyze_query("SELECT status, COUNT(*) FROM orders WHERE id > 0 GROUP BY status LIMIT 10");
    assert!(!violations.contains(&"PERF018".to_string()));
}

#[test]
fn test_three_level_nesting_flagged_info() {
    let queries = parse_queries(
        "SELECT id FROM a WHERE x IN (SELECT y FROM b WHERE z IN (SELECT w FROM c WHERE v = 1)) LIMIT 10",
        SqlDialect::Generic
    )
    .unwrap();
    let report = RuleRunner::new().analyze(&queries);
    let violation = report
        .violations
        .iter()
        .find(|v| v.rule_id == "PERF020")
        .unwrap();
    assert_eq!(violation.severity, Severity::Info);
}

#[test]
fn test_single_subquery_not_flagged() {
    let violations =
        analyze_query("SELECT id FROM a WHERE x IN (SELECT y FROM b WHERE z = 1) LIMIT 10");
    assert!(!violations.contains(&"PERF020".to_string()));
}

#[test]
fn test_five_level_nesting_is_error() {
    let queries = parse_queries(
        "SELECT id FROM a WHERE x IN (SELECT y FROM b WHERE z IN (SELECT w FROM c WHERE v IN (SELECT u FROM d WHERE t IN (SELECT s FROM e WHERE r = 1)))) LIMIT 10",
        SqlDialect::Generic
    )
    .unwrap();
    let report = RuleRunner::new().analyze(&queries);
    let violation = report
        .violations
        .iter()
        .find(|v| v.rule_id == "PERF020")
        .unwrap();
    assert_eq!(violation.severity, Severity::Error);
}

fn in_list_query(n: usize) -> String {
    let values: Vec<String> = (1..=n).map(|i| i.to_string()).collect();
    format!(
        "SELECT id FROM users WHERE id IN ({}) LIMIT 10",
        values.join(", ")
    )
}

#[test]
fn test_large_in_clause_flagged() {
    let violations = analyze_query(&in_list_query(60));
    assert!(violations.contains(&"PERF019".to_string()));
}

#[test]
fn test_small_in_clause_ok() {
    let violations = analyze_query(&in_list_query(10));
    assert!(!violations.contains(&"PERF019".to_string()));
}

#[test]
fn test_huge_in_clause_is_error() {
    let queries = parse_queries(&in_list_query(1500), SqlDialect::Generic).unwrap();
    let report = RuleRunner::new().analyze(&queries);
    let violation = report
        .violations
        .iter()
        .find(|v| v.rule_id == "PERF019")
        .unwrap();
    assert_eq!(violation.severity, Severity::Error);
}

#[test]
fn test_in_subquery_not_counted() {
    let violations = analyze_query(
        "SELECT id FROM users WHERE id IN (SELECT user_id FROM orders WHERE total > 0) LIMIT 10"
    );
    assert!(!violations.contains(&"PERF019".to_string()));
}

#[test]
fn test_order_by_rand_mysql() {
    let violations = analyze_query("SELECT id FROM users ORDER BY RAND() LIMIT 5");
    assert!(violations.contains(&"PERF013".to_string()));
}

#[test]
fn test_order_by_random_postgres() {
    let violations = analyze_query("SELECT id FROM users ORDER BY RANDOM() LIMIT 5");
    assert!(violations.contains(&"PERF013".to_string()));
}

#[test]
fn test_order_by_newid_mssql() {
    let violations = analyze_query("SELECT id FROM users ORDER BY NEWID() LIMIT 5");
    assert!(violations.contains(&"PERF013".to_string()));
}

#[test]
fn test_order_by_column_ok() {
    let violations = analyze_query("SELECT id FROM users WHERE id > 1 ORDER BY id LIMIT 5");
    assert!(!violations.contains(&"PERF013".to_string()));
}

#[test]
fn test_rand_in_where_not_order_by_ok() {
    let violations =
        analyze_query("SELECT id FROM users WHERE id >= FLOOR(RAND() * 100) ORDER BY id LIMIT 5");
    assert!(!violations.contains(&"PERF013".to_string()));
}

#[test]
fn test_explicit_columns_ok() {
    let violations = analyze_query("SELECT id, name FROM users LIMIT 10");
    assert!(!violations.contains(&"STYLE001".to_string()));
}

#[test]
fn test_injection_tautology_quoted() {
    let violations = analyze_query("SELECT id FROM users WHERE name = '' OR '1' = '1' LIMIT 10");
    assert!(violations.contains(&"SEC006".to_string()));
}

#[test]
fn test_injection_tautology_numeric() {
    let violations = analyze_query("SELECT id FROM users WHERE id = 5 OR 1 = 1 LIMIT 10");
    assert!(violations.contains(&"SEC006".to_string()));
}

#[test]
fn test_injection_tautology_empty_strings() {
    let violations = analyze_query("SELECT id FROM users WHERE name = 'a' OR '' = '' LIMIT 10");
    assert!(violations.contains(&"SEC006".to_string()));
}

#[test]
fn test_or_on_columns_not_tautology() {
    let violations = analyze_query("SELECT id FROM users WHERE id = 1 OR id = 2 LIMIT 10");
    assert!(!violations.contains(&"SEC006".to_string()));
}

#[test]
fn test_or_different_literals_not_tautology() {
    let violations =
        analyze_query("SELECT id FROM users WHERE name = 'a' OR status = 'b' LIMIT 10");
    assert!(!violations.contains(&"SEC006".to_string()));
}

#[test]
fn test_exec_string_flagged() {
    let violations = analyze_query("EXECUTE my_procedure");
    assert!(violations.contains(&"SEC007".to_string()));
}

#[test]
fn test_select_not_dynamic_sql() {
    let violations = analyze_query("SELECT id FROM users WHERE id = 1 LIMIT 5");
    assert!(!violations.contains(&"SEC007".to_string()));
}

#[test]
fn test_grant_statement_flagged() {
    let violations = analyze_query("GRANT SELECT ON users TO analyst");
    assert!(violations.contains(&"SEC005".to_string()));
}

#[test]
fn test_revoke_statement_flagged() {
    let violations = analyze_query("REVOKE DELETE ON orders FROM intern");
    assert!(violations.contains(&"SEC005".to_string()));
}

#[test]
fn test_broad_grant_is_error() {
    let queries = parse_queries(
        "GRANT ALL PRIVILEGES ON users TO new_user",
        SqlDialect::Generic
    )
    .unwrap();
    let report = RuleRunner::new().analyze(&queries);
    let violation = report
        .violations
        .iter()
        .find(|v| v.rule_id == "SEC005")
        .unwrap();
    assert_eq!(violation.severity, Severity::Error);
}

#[test]
fn test_select_not_privilege_change() {
    let violations = analyze_query("SELECT id FROM grants WHERE id = 1 LIMIT 5");
    assert!(!violations.contains(&"SEC005".to_string()));
}

#[test]
fn test_hardcoded_credential_insert() {
    let violations =
        analyze_query("INSERT INTO users (email, password) VALUES ('a@b.c', 'admin123')");
    assert!(violations.contains(&"SEC008".to_string()));
}

#[test]
fn test_hardcoded_credential_update_assignment() {
    let violations = analyze_query("UPDATE users SET api_key = 'sk-live-abc123' WHERE id = 1");
    assert!(violations.contains(&"SEC008".to_string()));
}

#[test]
fn test_hardcoded_credential_prefixed_column() {
    let violations = analyze_query("UPDATE users SET user_password = 'hunter2' WHERE id = 1");
    assert!(violations.contains(&"SEC008".to_string()));
}

#[test]
fn test_insert_without_sensitive_columns_ok() {
    let violations = analyze_query("INSERT INTO users (email, name) VALUES ('a@b.c', 'Alice')");
    assert!(!violations.contains(&"SEC008".to_string()));
}

#[test]
fn test_author_column_not_credential() {
    let violations = analyze_query("UPDATE posts SET author = 'Alice' WHERE id = 1");
    assert!(!violations.contains(&"SEC008".to_string()));
}

#[test]
fn test_update_without_where() {
    let violations = analyze_query("UPDATE users SET status = 'inactive'");
    assert!(violations.contains(&"SEC001".to_string()));
}

#[test]
fn test_update_with_where() {
    let violations = analyze_query("UPDATE users SET status = 'inactive' WHERE id = 1");
    assert!(!violations.contains(&"SEC001".to_string()));
}

#[test]
fn test_delete_without_where() {
    let violations = analyze_query("DELETE FROM users");
    assert!(violations.contains(&"SEC002".to_string()));
}

#[test]
fn test_delete_with_where() {
    let violations = analyze_query("DELETE FROM users WHERE id = 1");
    assert!(!violations.contains(&"SEC002".to_string()));
}

#[test]
fn test_union_without_all() {
    let violations = analyze_query("SELECT id FROM users UNION SELECT id FROM admins");
    assert!(violations.contains(&"PERF010".to_string()));
}

#[test]
fn test_union_all_ok() {
    let violations = analyze_query("SELECT id FROM users UNION ALL SELECT id FROM admins");
    assert!(!violations.contains(&"PERF010".to_string()));
}

#[test]
fn test_distinct_with_order_by() {
    let violations = analyze_query("SELECT DISTINCT status FROM orders ORDER BY status");
    assert!(violations.contains(&"PERF006".to_string()));
}

#[test]
fn test_join_on_non_indexed_column_flagged() {
    let schema = r#"
        CREATE TABLE users (id INT PRIMARY KEY);
        CREATE TABLE orders (id INT PRIMARY KEY, user_id INT);
    "#;
    let violations = analyze_with_schema(
        "SELECT u.id FROM users u JOIN orders o ON o.user_id = u.id WHERE u.id > 0 LIMIT 10",
        schema
    );
    assert!(violations.contains(&"SCHEMA004".to_string()));
}

#[test]
fn test_join_on_indexed_column_ok() {
    let schema = r#"
        CREATE TABLE users (id INT PRIMARY KEY);
        CREATE TABLE orders (id INT PRIMARY KEY, user_id INT);
        CREATE INDEX idx_orders_user_id ON orders(user_id);
    "#;
    let violations = analyze_with_schema(
        "SELECT u.id FROM users u JOIN orders o ON o.user_id = u.id WHERE u.id > 0 LIMIT 10",
        schema
    );
    assert!(!violations.contains(&"SCHEMA004".to_string()));
}

#[test]
fn test_schema_missing_index() {
    let schema = "CREATE TABLE users (id INT PRIMARY KEY, email VARCHAR(255))";
    let violations = analyze_with_schema(
        "SELECT * FROM users WHERE email = 'test@test.com' LIMIT 10",
        schema
    );
    assert!(violations.contains(&"SCHEMA001".to_string()));
}

#[test]
fn test_schema_with_index() {
    let schema = r#"
        CREATE TABLE users (id INT PRIMARY KEY, email VARCHAR(255));
        CREATE INDEX idx_email ON users(email);
    "#;
    let violations = analyze_with_schema(
        "SELECT * FROM users WHERE email = 'test@test.com' LIMIT 10",
        schema
    );
    assert!(!violations.contains(&"SCHEMA001".to_string()));
}

#[test]
fn test_rule_disabled() {
    let queries = parse_queries("SELECT * FROM users", SqlDialect::Generic).unwrap();
    let config = RulesConfig {
        disabled: vec![
            "PERF001".to_string(),
            "PERF011".to_string(),
            "STYLE001".to_string(),
        ],
        ..Default::default()
    };
    let runner = RuleRunner::with_config(config);
    let report = runner.analyze(&queries);
    let rule_ids: Vec<_> = report.violations.iter().map(|v| v.rule_id).collect();
    assert!(!rule_ids.contains(&"PERF001"));
    assert!(!rule_ids.contains(&"PERF011"));
    assert!(!rule_ids.contains(&"STYLE001"));
}

#[test]
fn test_severity_override() {
    let queries = parse_queries("SELECT * FROM users", SqlDialect::Generic).unwrap();
    let mut severity = std::collections::HashMap::new();
    severity.insert("STYLE001".to_string(), "error".to_string());
    let config = RulesConfig {
        disabled: vec![],
        severity
    };
    let runner = RuleRunner::with_config(config);
    let report = runner.analyze(&queries);
    let style_violation = report.violations.iter().find(|v| v.rule_id == "STYLE001");
    assert!(style_violation.is_some());
    assert_eq!(style_violation.unwrap().severity, Severity::Error);
}

#[test]
fn test_error_count() {
    let queries = parse_queries("DELETE FROM users", SqlDialect::Generic).unwrap();
    let runner = RuleRunner::new();
    let report = runner.analyze(&queries);
    assert!(report.error_count() > 0);
}

#[test]
fn test_warning_count() {
    let queries = parse_queries("SELECT * FROM users", SqlDialect::Generic).unwrap();
    let runner = RuleRunner::new();
    let report = runner.analyze(&queries);
    assert!(report.warning_count() > 0);
}

#[test]
fn test_no_violations_for_good_query() {
    let queries = parse_queries(
        "SELECT id, name FROM users WHERE id = 1 LIMIT 10",
        SqlDialect::Generic
    )
    .unwrap();
    let runner = RuleRunner::new();
    let report = runner.analyze(&queries);
    assert_eq!(report.error_count(), 0);
}

#[test]
fn test_multiple_violations() {
    let queries = parse_queries("SELECT * FROM users", SqlDialect::Generic).unwrap();
    let runner = RuleRunner::new();
    let report = runner.analyze(&queries);
    assert!(report.violations.len() >= 2);
}

#[test]
fn test_insert_no_violations() {
    let queries = parse_queries(
        "INSERT INTO users (id, name) VALUES (1, 'test')",
        SqlDialect::Generic
    )
    .unwrap();
    let runner = RuleRunner::new();
    let report = runner.analyze(&queries);
    assert_eq!(report.error_count(), 0);
    assert_eq!(report.warning_count(), 0);
}

#[test]
fn test_scalar_subquery() {
    let violations = analyze_query(
        "SELECT id, (SELECT COUNT(*) FROM orders WHERE orders.user_id = users.id) FROM users LIMIT 10"
    );
    assert!(violations.contains(&"PERF007".to_string()));
}

#[test]
fn test_function_on_column_year() {
    let violations = analyze_query("SELECT * FROM orders WHERE YEAR(created_at) = 2024 LIMIT 10");
    assert!(violations.contains(&"PERF008".to_string()));
}

#[test]
fn test_function_on_column_upper() {
    let violations = analyze_query("SELECT * FROM users WHERE UPPER(name) = 'JOHN' LIMIT 10");
    assert!(violations.contains(&"PERF008".to_string()));
}

#[test]
fn test_function_on_column_lower() {
    let violations =
        analyze_query("SELECT * FROM users WHERE LOWER(email) = 'test@test.com' LIMIT 10");
    assert!(violations.contains(&"PERF008".to_string()));
}

#[test]
fn test_function_on_column_trim() {
    let violations = analyze_query("SELECT * FROM users WHERE TRIM(name) = 'John' LIMIT 10");
    assert!(violations.contains(&"PERF008".to_string()));
}

#[test]
fn test_function_on_column_cast() {
    let violations = analyze_query("SELECT * FROM users WHERE CAST(id AS VARCHAR) = '1' LIMIT 10");
    assert!(violations.contains(&"PERF008".to_string()));
}

#[test]
fn test_function_on_column_coalesce() {
    let violations =
        analyze_query("SELECT * FROM users WHERE COALESCE(status, 'unknown') = 'active' LIMIT 10");
    assert!(violations.contains(&"PERF008".to_string()));
}

#[test]
fn test_not_in_with_subquery() {
    let violations =
        analyze_query("SELECT * FROM users WHERE id NOT IN (SELECT user_id FROM banned) LIMIT 10");
    assert!(violations.contains(&"PERF009".to_string()));
}

#[test]
fn test_or_instead_of_in() {
    let violations = analyze_query(
        "SELECT * FROM users WHERE status = 'a' OR status = 'b' OR status = 'c' OR status = 'd' LIMIT 10"
    );
    assert!(violations.contains(&"PERF003".to_string()));
}

#[test]
fn test_cartesian_product() {
    let violations = analyze_query("SELECT * FROM users, orders LIMIT 10");
    assert!(violations.contains(&"PERF005".to_string()));
}

#[test]
fn test_cartesian_product_with_where() {
    let violations =
        analyze_query("SELECT * FROM users, orders WHERE users.id = orders.user_id LIMIT 10");
    assert!(!violations.contains(&"PERF005".to_string()));
}

#[test]
fn test_leading_wildcard_double_quote() {
    let violations = analyze_query(r#"SELECT * FROM users WHERE name LIKE "%test" LIMIT 10"#);
    assert!(violations.contains(&"PERF002".to_string()));
}

#[test]
fn test_select_star_double_space() {
    let violations = analyze_query("SELECT  * FROM users");
    assert!(violations.contains(&"PERF001".to_string()));
}

#[test]
fn test_join_missing_alias() {
    let violations = analyze_query(
        "SELECT users.id FROM users INNER JOIN orders ON users.id = orders.user_id LIMIT 10"
    );
    assert!(violations.contains(&"STYLE002".to_string()));
}

#[test]
fn test_schema_join_column_missing_index() {
    let schema = r#"
        CREATE TABLE users (id INT PRIMARY KEY);
        CREATE TABLE orders (id INT PRIMARY KEY, user_id INT);
    "#;
    let violations = analyze_with_schema(
        "SELECT * FROM users u INNER JOIN orders o ON u.id = o.user_id LIMIT 10",
        schema
    );
    assert!(violations.contains(&"SCHEMA001".to_string()));
}

#[test]
fn test_schema_order_by_missing_index() {
    let schema = "CREATE TABLE users (id INT PRIMARY KEY, name VARCHAR(255))";
    let violations = analyze_with_schema("SELECT * FROM users ORDER BY name LIMIT 10", schema);
    assert!(violations.contains(&"SCHEMA003".to_string()));
}

#[test]
fn test_schema_column_not_in_schema() {
    let schema = "CREATE TABLE users (id INT PRIMARY KEY, name VARCHAR(255))";
    let violations = analyze_with_schema(
        "SELECT * FROM users WHERE nonexistent_col = 'test' LIMIT 10",
        schema
    );
    assert!(violations.contains(&"SCHEMA002".to_string()));
}

#[test]
fn test_schema_large_table_no_index() {
    let schema = r#"
        CREATE TABLE users (id INT PRIMARY KEY, email VARCHAR(255));
        INSERT INTO users VALUES (1, 'a');
        INSERT INTO users VALUES (2, 'b');
        INSERT INTO users VALUES (3, 'c');
    "#;
    let violations =
        analyze_with_schema("SELECT * FROM users WHERE email = 'test' LIMIT 10", schema);
    assert!(violations.contains(&"SCHEMA001".to_string()));
}

#[test]
fn test_multiple_queries() {
    let violations = analyze_query("SELECT * FROM users; DELETE FROM orders");
    assert!(violations.contains(&"PERF001".to_string()));
    assert!(violations.contains(&"SEC002".to_string()));
}

#[test]
fn test_truncate_detected() {
    let violations = analyze_query("TRUNCATE TABLE users");
    assert!(violations.contains(&"SEC003".to_string()));
}

#[test]
fn test_truncate_without_table_keyword() {
    let violations = analyze_query("TRUNCATE users");
    assert!(violations.contains(&"SEC003".to_string()));
}

#[test]
fn test_truncate_multiple_tables() {
    let violations = analyze_query("TRUNCATE TABLE users, orders");
    assert!(violations.contains(&"SEC003".to_string()));
}

#[test]
fn test_drop_table_detected() {
    let violations = analyze_query("DROP TABLE users");
    assert!(violations.contains(&"SEC004".to_string()));
}

#[test]
fn test_drop_table_if_exists() {
    let violations = analyze_query("DROP TABLE IF EXISTS users");
    assert!(violations.contains(&"SEC004".to_string()));
}

#[test]
fn test_drop_database_detected() {
    let violations = analyze_query("DROP DATABASE production");
    assert!(violations.contains(&"SEC004".to_string()));
}

#[test]
fn test_drop_index_detected() {
    let violations = analyze_query("DROP INDEX idx_users_email");
    assert!(violations.contains(&"SEC004".to_string()));
}