scythe-core 0.6.9

Core SQL parsing, catalog building, and type inference for scythe
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
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
use sqlparser::parser::Parser;

use crate::dialect::SqlDialect;
use crate::errors::ScytheError;

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum QueryCommand {
    One,
    Opt,
    Many,
    Exec,
    ExecResult,
    ExecRows,
    Batch,
    Grouped,
}

impl std::fmt::Display for QueryCommand {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            QueryCommand::One => write!(f, "one"),
            QueryCommand::Opt => write!(f, "opt"),
            QueryCommand::Many => write!(f, "many"),
            QueryCommand::Exec => write!(f, "exec"),
            QueryCommand::ExecResult => write!(f, "exec_result"),
            QueryCommand::ExecRows => write!(f, "exec_rows"),
            QueryCommand::Batch => write!(f, "batch"),
            QueryCommand::Grouped => write!(f, "grouped"),
        }
    }
}

impl QueryCommand {
    fn from_str(s: &str) -> Result<Self, ScytheError> {
        match s {
            "one" => Ok(QueryCommand::One),
            "opt" => Ok(QueryCommand::Opt),
            "many" => Ok(QueryCommand::Many),
            "exec" => Ok(QueryCommand::Exec),
            "exec_result" => Ok(QueryCommand::ExecResult),
            "exec_rows" => Ok(QueryCommand::ExecRows),
            "batch" => Ok(QueryCommand::Batch),
            "grouped" => Ok(QueryCommand::Grouped),
            other => Err(ScytheError::invalid_annotation(format!(
                "invalid @returns value: {other}"
            ))),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParamDoc {
    pub name: String,
    pub description: String,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct JsonMapping {
    pub column: String,
    pub rust_type: String,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Annotations {
    pub name: String,
    pub command: QueryCommand,
    pub param_docs: Vec<ParamDoc>,
    pub nullable_overrides: Vec<String>,
    pub nonnull_overrides: Vec<String>,
    pub json_mappings: Vec<JsonMapping>,
    pub deprecated: Option<String>,
    pub optional_params: Vec<String>,
    pub group_by: Option<String>,
}

#[derive(Debug)]
pub struct Query {
    pub name: String,
    pub command: QueryCommand,
    pub sql: String,
    pub stmt: sqlparser::ast::Statement,
    pub annotations: Annotations,
}

/// Parse a single annotated SQL query into a `Query` using the PostgreSQL dialect.
pub fn parse_query(query_sql: &str) -> Result<Query, ScytheError> {
    parse_query_with_dialect(query_sql, &SqlDialect::PostgreSQL)
}

/// Parse a single annotated SQL query into a `Query` using the specified dialect.
pub fn parse_query_with_dialect(
    query_sql: &str,
    dialect: &SqlDialect,
) -> Result<Query, ScytheError> {
    let mut name: Option<String> = None;
    let mut command: Option<QueryCommand> = None;
    let mut param_docs = Vec::new();
    let mut nullable_overrides = Vec::new();
    let mut nonnull_overrides = Vec::new();
    let mut json_mappings = Vec::new();
    let mut deprecated: Option<String> = None;
    let mut optional_params = Vec::new();
    let mut group_by: Option<String> = None;

    let mut sql_lines = Vec::new();

    for line in query_sql.lines() {
        let trimmed = line.trim();

        // Check for annotation: "-- @..." or "--@..."
        let annotation_body = if let Some(rest) = trimmed.strip_prefix("--") {
            let rest = rest.trim_start();
            rest.strip_prefix('@')
        } else {
            None
        };

        if let Some(body) = annotation_body {
            // Parse the annotation keyword and value
            let (keyword, value) = match body.find(|c: char| c.is_whitespace()) {
                Some(pos) => (&body[..pos], body[pos..].trim()),
                None => (body, ""),
            };

            match keyword.to_ascii_lowercase().as_str() {
                "name" => {
                    name = Some(value.to_string());
                }
                "returns" => {
                    let cmd_str = value.strip_prefix(':').unwrap_or(value);
                    command = Some(QueryCommand::from_str(cmd_str)?);
                }
                "param" => {
                    // format: "<name>: <description>" or "<name>:<description>"
                    if let Some(colon_pos) = value.find(':') {
                        let param_name = value[..colon_pos].trim().to_string();
                        let description = value[colon_pos + 1..].trim().to_string();
                        param_docs.push(ParamDoc {
                            name: param_name,
                            description,
                        });
                    } else {
                        param_docs.push(ParamDoc {
                            name: value.to_string(),
                            description: String::new(),
                        });
                    }
                }
                "nullable" => {
                    for col in value.split(',') {
                        let col = col.trim();
                        if !col.is_empty() {
                            nullable_overrides.push(col.to_string());
                        }
                    }
                }
                "nonnull" => {
                    for col in value.split(',') {
                        let col = col.trim();
                        if !col.is_empty() {
                            nonnull_overrides.push(col.to_string());
                        }
                    }
                }
                "json" => {
                    // format: "<col> = <Type>"
                    if let Some(eq_pos) = value.find('=') {
                        let column = value[..eq_pos].trim().to_string();
                        let rust_type = value[eq_pos + 1..].trim().to_string();
                        json_mappings.push(JsonMapping { column, rust_type });
                    }
                }
                "deprecated" => {
                    deprecated = Some(value.to_string());
                }
                "group_by" => {
                    group_by = Some(value.to_string());
                }
                "optional" => {
                    for param in value.split(',') {
                        let param = param.trim();
                        if !param.is_empty() {
                            optional_params.push(param.to_string());
                        }
                    }
                }
                _ => {
                    // Unknown annotation — ignore or could error
                }
            }
        } else {
            sql_lines.push(line);
        }
    }

    let name = name.ok_or_else(|| ScytheError::missing_annotation("name"))?;
    let command = command.ok_or_else(|| ScytheError::missing_annotation("returns"))?;

    if command == QueryCommand::Grouped && group_by.is_none() {
        return Err(ScytheError::invalid_annotation(
            "@returns :grouped requires a @group_by annotation (e.g. @group_by users.id)",
        ));
    }

    let sql = sql_lines.join("\n").trim().to_string();

    if sql.is_empty() {
        return Err(ScytheError::syntax("empty SQL body"));
    }

    // Preprocess dialect-specific syntax before parsing:
    // - Oracle: strip `RETURNING ... INTO` output binds, convert `:N` → `?`
    // - MSSQL: convert `OUTPUT INSERTED.*` → `RETURNING` for parsing,
    //          convert `@pN` → `?` for parsing; keep original SQL for codegen
    let (sql, parse_sql) = if *dialect == SqlDialect::Oracle {
        let processed = preprocess_oracle_sql(&sql);
        (processed.clone(), processed)
    } else if *dialect == SqlDialect::MsSql {
        // For codegen: only convert @pN → ? placeholders (keep OUTPUT syntax)
        let codegen_sql = convert_mssql_placeholders(&sql);
        // For parsing: also convert OUTPUT INSERTED → RETURNING
        let parse_sql = preprocess_mssql_sql(&sql);
        (codegen_sql, parse_sql)
    } else {
        (sql.clone(), sql)
    };

    let parser_dialect = dialect.to_sqlparser_dialect();
    let statements = Parser::parse_sql(parser_dialect.as_ref(), &parse_sql)
        .map_err(|e| ScytheError::syntax(format!("syntax error: {}", e)))?;

    if statements.len() != 1 {
        // sqlparser may produce an extra empty statement from a trailing semicolon —
        // filter those out by checking for exactly one non-empty statement.
        let non_empty: Vec<_> = statements
            .into_iter()
            .filter(|s| {
                !matches!(s, sqlparser::ast::Statement::Flush { .. }) && format!("{s}") != ""
            })
            .collect();
        if non_empty.len() != 1 {
            return Err(ScytheError::syntax("expected exactly one SQL statement"));
        }
        let stmt = non_empty
            .into_iter()
            .next()
            .expect("filtered to exactly one statement");
        let annotations = Annotations {
            name: name.clone(),
            command: command.clone(),
            param_docs,
            nullable_overrides,
            nonnull_overrides,
            json_mappings,
            deprecated,
            optional_params,
            group_by: group_by.clone(),
        };
        return Ok(Query {
            name,
            command,
            sql,
            stmt,
            annotations,
        });
    }

    let stmt = statements
        .into_iter()
        .next()
        .expect("filtered to exactly one statement");

    let annotations = Annotations {
        name: name.clone(),
        command: command.clone(),
        param_docs,
        nullable_overrides,
        nonnull_overrides,
        json_mappings,
        deprecated,
        optional_params,
        group_by,
    };

    Ok(Query {
        name,
        command,
        sql,
        stmt,
        annotations,
    })
}

/// Preprocess Oracle SQL before parsing:
/// 1. Strip `INTO :N, :N, ...` suffix from `RETURNING ... INTO` clauses
/// 2. Convert `:N` positional placeholders to `?` (universally supported)
fn preprocess_oracle_sql(sql: &str) -> String {
    // Strip Oracle RETURNING ... INTO clause (output bind variables)
    // e.g. "INSERT ... RETURNING id, name INTO :4, :5" → "INSERT ... RETURNING id, name"
    let sql = strip_returning_into(sql);

    // Convert :N → ? (outside string literals)
    let mut result = String::with_capacity(sql.len());
    let mut chars = sql.chars().peekable();
    while let Some(ch) = chars.next() {
        if ch == '\'' {
            // Skip string literals
            result.push(ch);
            while let Some(inner) = chars.next() {
                result.push(inner);
                if inner == '\'' {
                    if chars.peek() == Some(&'\'') {
                        result.push(chars.next().unwrap());
                    } else {
                        break;
                    }
                }
            }
        } else if ch == ':' && chars.peek().is_some_and(|c| c.is_ascii_digit()) {
            // Convert :N → ?
            result.push('?');
            while chars.peek().is_some_and(|c| c.is_ascii_digit()) {
                chars.next();
            }
        } else {
            result.push(ch);
        }
    }
    result
}

/// Convert MSSQL `@pN` positional placeholders to `?` (outside string literals).
/// MsSqlDialect treats `@` as an identifier start, so `@p1` becomes an identifier
/// rather than a `Placeholder` token — preprocessing normalises it to `?`.
fn convert_mssql_placeholders(sql: &str) -> String {
    let mut result = String::with_capacity(sql.len());
    let mut chars = sql.chars().peekable();
    while let Some(ch) = chars.next() {
        if ch == '\'' {
            // Skip string literals verbatim
            result.push(ch);
            while let Some(inner) = chars.next() {
                result.push(inner);
                if inner == '\'' {
                    if chars.peek() == Some(&'\'') {
                        // Escaped quote inside string literal
                        result.push(chars.next().unwrap());
                    } else {
                        break;
                    }
                }
            }
        } else if ch == '@' && chars.peek().is_some_and(|c| *c == 'p' || *c == 'P') {
            // Peek ahead: must be `@p` followed by at least one digit
            let mut lookahead = chars.clone();
            lookahead.next(); // consume the 'p'/'P'
            if lookahead.peek().is_some_and(|c| c.is_ascii_digit()) {
                // It is an `@pN` placeholder — consume `p` and all digits
                chars.next(); // consume 'p'/'P'
                while chars.peek().is_some_and(|c| c.is_ascii_digit()) {
                    chars.next();
                }
                result.push('?');
            } else {
                result.push(ch);
            }
        } else {
            result.push(ch);
        }
    }
    result
}

/// Preprocess MSSQL SQL before parsing:
/// 1. Strip `OUTPUT INSERTED.col, ...` clauses and convert to RETURNING
/// 2. Convert `@pN` positional placeholders to `?`
fn preprocess_mssql_sql(sql: &str) -> String {
    // First pass: convert OUTPUT INSERTED.col to RETURNING col
    let sql = strip_and_convert_mssql_output(sql);
    // Second pass: convert @pN to ?
    convert_mssql_placeholders(&sql)
}

/// Strip MSSQL `OUTPUT INSERTED.col1, INSERTED.col2, ...` from INSERT statements
/// and convert it to a `RETURNING col1, col2, ...` clause.
/// The OUTPUT clause appears between the column list and VALUES clause:
///   INSERT INTO table (cols) OUTPUT INSERTED.col1, INSERTED.col2, ... VALUES (...)
/// becomes:
///   INSERT INTO table (cols) VALUES (...) RETURNING col1, col2, ...
fn strip_and_convert_mssql_output(sql: &str) -> String {
    // Case-insensitive search for OUTPUT keyword in INSERT statements
    let upper = sql.to_uppercase();

    // Only process INSERT statements with OUTPUT
    if !upper.contains("INSERT") || !upper.contains("OUTPUT") {
        return sql.to_string();
    }

    // Find the OUTPUT keyword
    if let Some(output_pos) = find_word_position(&upper, "OUTPUT") {
        // Check if this is actually part of an INSERT statement by finding INSERT before it
        let before_output = &upper[..output_pos];
        if !before_output.contains("INSERT") {
            return sql.to_string();
        }

        // Look for the VALUES keyword after OUTPUT
        let after_output = &upper[output_pos + "OUTPUT".len()..];
        if let Some(values_offset) = find_word_position(after_output, "VALUES") {
            let values_pos = output_pos + "OUTPUT".len() + values_offset;

            // Extract the OUTPUT column list (between OUTPUT and VALUES)
            let output_cols_str = &sql[output_pos + "OUTPUT".len()..values_pos];

            // Parse column names: strip "INSERTED." prefix from each column name
            let cols = parse_inserted_columns(output_cols_str);

            if !cols.is_empty() {
                // Build result: keep everything before OUTPUT, then VALUES clause,
                // then RETURNING clause (before any trailing semicolon)
                let before_output_sql = sql[..output_pos].trim_end();
                let after_values = sql[values_pos..].trim_end();
                let (values_body, trailing) = if let Some(stripped) = after_values.strip_suffix(';')
                {
                    (stripped, ";")
                } else {
                    (after_values, "")
                };

                return format!(
                    "{}\n{} RETURNING {}{}",
                    before_output_sql, values_body, cols, trailing
                );
            }
        }
    }

    sql.to_string()
}

/// Find the position of a word (case-insensitive) in the text.
/// The word must be a separate word, not part of another identifier.
fn find_word_position(text: &str, word: &str) -> Option<usize> {
    let mut pos = 0;
    let word_len = word.len();
    while let Some(idx) = text[pos..].find(word) {
        let abs_idx = pos + idx;

        // Check character before
        let before_ok = abs_idx == 0
            || !text
                .as_bytes()
                .get(abs_idx - 1)
                .is_some_and(|&b| b.is_ascii_alphanumeric() || b == b'_');

        // Check character after
        let after_idx = abs_idx + word_len;
        let after_ok = after_idx >= text.len()
            || !text
                .as_bytes()
                .get(after_idx)
                .is_some_and(|&b| b.is_ascii_alphanumeric() || b == b'_');

        if before_ok && after_ok {
            return Some(abs_idx);
        }
        pos = abs_idx + 1;
    }
    None
}

/// Parse INSERTED.col1, INSERTED.col2, ... and extract column names as "col1, col2, ..."
fn parse_inserted_columns(output_str: &str) -> String {
    let mut cols = Vec::new();

    for part in output_str.split(',') {
        let trimmed = part.trim();

        // Try to extract column name after INSERTED.
        if let Some(after_inserted) = trimmed
            .strip_prefix("INSERTED.")
            .or_else(|| trimmed.strip_prefix("inserted."))
            .or_else(|| trimmed.strip_prefix("INSERTED"))
            .or_else(|| trimmed.strip_prefix("inserted"))
        {
            let col_name = after_inserted.trim().to_string();
            if !col_name.is_empty() {
                cols.push(col_name);
            }
        }
    }

    cols.join(", ")
}

/// Strip the `INTO :N, :N, ...` suffix from an Oracle `RETURNING ... INTO` clause.
fn strip_returning_into(sql: &str) -> String {
    // Case-insensitive search for "INTO" after "RETURNING" at the end of the statement
    let upper = sql.to_uppercase();
    if let Some(ret_pos) = upper.rfind("RETURNING") {
        let after_returning = &upper[ret_pos + "RETURNING".len()..];
        if let Some(into_offset) = after_returning.find("INTO") {
            let into_pos = ret_pos + "RETURNING".len() + into_offset;
            // Keep everything before INTO, trim trailing whitespace/semicolons
            let trimmed = sql[..into_pos].trim_end();
            return trimmed.to_string();
        }
    }
    sql.to_string()
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::errors::ErrorCode;

    fn parse(sql: &str) -> Result<Query, ScytheError> {
        parse_query(sql)
    }

    #[test]
    fn test_basic_parse() {
        let input = "-- @name GetUsers\n-- @returns :many\nSELECT * FROM users;";
        let q = parse(input).unwrap();
        assert_eq!(q.name, "GetUsers");
        assert_eq!(q.command, QueryCommand::Many);
        assert!(q.sql.contains("SELECT"));
    }

    #[test]
    fn test_all_command_types() {
        let cases = vec![
            (":one", QueryCommand::One),
            (":many", QueryCommand::Many),
            (":exec", QueryCommand::Exec),
            (":exec_result", QueryCommand::ExecResult),
            (":exec_rows", QueryCommand::ExecRows),
        ];
        for (tag, expected) in cases {
            let input = format!("-- @name Q\n-- @returns {}\nSELECT 1", tag);
            let q = parse(&input).unwrap();
            assert_eq!(q.command, expected, "failed for {}", tag);
        }
    }

    #[test]
    fn test_case_insensitive_keywords() {
        let input = "-- @Name GetUsers\n-- @RETURNS :many\nSELECT 1";
        let q = parse(input).unwrap();
        assert_eq!(q.name, "GetUsers");
        assert_eq!(q.command, QueryCommand::Many);
    }

    #[test]
    fn test_missing_name_errors() {
        let input = "-- @returns :many\nSELECT 1";
        let err = parse(input).unwrap_err();
        assert_eq!(err.code, ErrorCode::MissingAnnotation);
        assert!(err.message.contains("name"));
    }

    #[test]
    fn test_missing_returns_errors() {
        let input = "-- @name Foo\nSELECT 1";
        let err = parse(input).unwrap_err();
        assert_eq!(err.code, ErrorCode::MissingAnnotation);
        assert!(err.message.contains("returns"));
    }

    #[test]
    fn test_invalid_returns_value() {
        let input = "-- @name Foo\n-- @returns :invalid\nSELECT 1";
        let err = parse(input).unwrap_err();
        assert_eq!(err.code, ErrorCode::InvalidAnnotation);
    }

    #[test]
    fn test_empty_name_value() {
        // An empty name is accepted by the parser (it stores "")
        let input = "-- @name\n-- @returns :one\nSELECT 1";
        let q = parse(input).unwrap();
        assert_eq!(q.name, "");
    }

    #[test]
    fn test_param_annotation() {
        let input = "-- @name Foo\n-- @returns :one\n-- @param id: the user ID\nSELECT 1";
        let q = parse(input).unwrap();
        assert_eq!(q.annotations.param_docs.len(), 1);
        assert_eq!(q.annotations.param_docs[0].name, "id");
        assert_eq!(q.annotations.param_docs[0].description, "the user ID");
    }

    #[test]
    fn test_param_no_description() {
        let input = "-- @name Foo\n-- @returns :one\n-- @param id\nSELECT 1";
        let q = parse(input).unwrap();
        assert_eq!(q.annotations.param_docs.len(), 1);
        assert_eq!(q.annotations.param_docs[0].name, "id");
        assert_eq!(q.annotations.param_docs[0].description, "");
    }

    #[test]
    fn test_nullable_annotation() {
        let input = "-- @name Foo\n-- @returns :one\n-- @nullable col1, col2\nSELECT 1";
        let q = parse(input).unwrap();
        assert_eq!(q.annotations.nullable_overrides, vec!["col1", "col2"]);
    }

    #[test]
    fn test_nonnull_annotation() {
        let input = "-- @name Foo\n-- @returns :one\n-- @nonnull col1\nSELECT 1";
        let q = parse(input).unwrap();
        assert_eq!(q.annotations.nonnull_overrides, vec!["col1"]);
    }

    #[test]
    fn test_json_annotation() {
        let input = "-- @name Foo\n-- @returns :one\n-- @json data = EventData\nSELECT 1";
        let q = parse(input).unwrap();
        assert_eq!(q.annotations.json_mappings.len(), 1);
        assert_eq!(q.annotations.json_mappings[0].column, "data");
        assert_eq!(q.annotations.json_mappings[0].rust_type, "EventData");
    }

    #[test]
    fn test_deprecated_annotation() {
        let input = "-- @name Foo\n-- @returns :one\n-- @deprecated Use V2\nSELECT 1";
        let q = parse(input).unwrap();
        assert_eq!(q.annotations.deprecated, Some("Use V2".to_string()));
    }

    #[test]
    fn test_sql_syntax_error() {
        let input = "-- @name Foo\n-- @returns :one\nSELCT * FROM users";
        let err = parse(input).unwrap_err();
        assert_eq!(err.code, ErrorCode::SyntaxError);
    }

    #[test]
    fn test_trailing_semicolon() {
        let input = "-- @name Foo\n-- @returns :one\nSELECT 1;";
        let q = parse(input).unwrap();
        assert_eq!(q.name, "Foo");
    }

    #[test]
    fn test_multiple_statements_error() {
        let input = "-- @name Foo\n-- @returns :one\nSELECT 1; SELECT 2;";
        let err = parse(input).unwrap_err();
        assert_eq!(err.code, ErrorCode::SyntaxError);
    }

    #[test]
    fn test_sql_preserved_without_annotations() {
        let input = "-- @name Foo\n-- @returns :one\nSELECT id, name FROM users WHERE id = $1";
        let q = parse(input).unwrap();
        assert_eq!(q.sql, "SELECT id, name FROM users WHERE id = $1");
    }

    #[test]
    fn test_returns_without_colon_prefix() {
        let input = "-- @name Foo\n-- @returns many\nSELECT 1";
        let q = parse(input).unwrap();
        assert_eq!(q.command, QueryCommand::Many);
    }

    #[test]
    fn test_batch_command() {
        let input = "-- @name Foo\n-- @returns :batch\nSELECT 1";
        let q = parse(input).unwrap();
        assert_eq!(q.command, QueryCommand::Batch);
    }

    #[test]
    fn test_grouped_command_with_group_by() {
        let input = "-- @name GetUsersWithOrders\n-- @returns :grouped\n-- @group_by users.id\nSELECT u.id, u.name FROM users u JOIN orders o ON o.user_id = u.id";
        let q = parse(input).unwrap();
        assert_eq!(q.command, QueryCommand::Grouped);
        assert_eq!(q.annotations.group_by, Some("users.id".to_string()));
    }

    #[test]
    fn test_grouped_command_without_group_by_errors() {
        let input = "-- @name Foo\n-- @returns :grouped\nSELECT 1";
        let err = parse(input).unwrap_err();
        assert_eq!(err.code, ErrorCode::InvalidAnnotation);
        assert!(err.message.contains("@group_by"));
    }

    #[test]
    fn test_group_by_without_grouped_is_ignored() {
        let input = "-- @name Foo\n-- @returns :many\n-- @group_by users.id\nSELECT 1";
        let q = parse(input).unwrap();
        assert_eq!(q.command, QueryCommand::Many);
        assert_eq!(q.annotations.group_by, Some("users.id".to_string()));
    }

    #[test]
    fn test_preprocess_oracle_colon_placeholders() {
        assert_eq!(
            preprocess_oracle_sql("SELECT * FROM users WHERE id = :1"),
            "SELECT * FROM users WHERE id = ?"
        );
        assert_eq!(
            preprocess_oracle_sql("INSERT INTO users (name, email) VALUES (:1, :2)"),
            "INSERT INTO users (name, email) VALUES (?, ?)"
        );
    }

    #[test]
    fn test_preprocess_oracle_preserves_string_literals() {
        assert_eq!(
            preprocess_oracle_sql("SELECT * FROM users WHERE name = ':1' AND id = :1"),
            "SELECT * FROM users WHERE name = ':1' AND id = ?"
        );
    }

    #[test]
    fn test_preprocess_oracle_strips_returning_into() {
        assert_eq!(
            preprocess_oracle_sql(
                "INSERT INTO users (name) VALUES (:1) RETURNING id, name INTO :2, :3"
            ),
            "INSERT INTO users (name) VALUES (?) RETURNING id, name"
        );
    }

    #[test]
    fn test_preprocess_oracle_full_insert_returning_into() {
        let sql = "INSERT INTO users (name, email, active) VALUES (:1, :2, :3) RETURNING id, name, email, active, created_at INTO :4, :5, :6, :7, :8";
        let result = preprocess_oracle_sql(sql);
        assert_eq!(
            result,
            "INSERT INTO users (name, email, active) VALUES (?, ?, ?) RETURNING id, name, email, active, created_at"
        );
    }

    #[test]
    fn test_preprocess_oracle_no_returning_into_unchanged() {
        assert_eq!(
            preprocess_oracle_sql("DELETE FROM users WHERE id = :1"),
            "DELETE FROM users WHERE id = ?"
        );
    }

    #[test]
    fn test_preprocess_mssql_single_placeholder() {
        assert_eq!(
            preprocess_mssql_sql("SELECT * FROM users WHERE id = @p1"),
            "SELECT * FROM users WHERE id = ?"
        );
    }

    #[test]
    fn test_preprocess_mssql_multiple_placeholders() {
        assert_eq!(
            preprocess_mssql_sql("INSERT INTO users (name, email) VALUES (@p1, @p2)"),
            "INSERT INTO users (name, email) VALUES (?, ?)"
        );
    }

    #[test]
    fn test_preprocess_mssql_preserves_string_literals() {
        assert_eq!(
            preprocess_mssql_sql("SELECT * FROM users WHERE name = '@p1' AND id = @p1"),
            "SELECT * FROM users WHERE name = '@p1' AND id = ?"
        );
    }

    #[test]
    fn test_preprocess_mssql_case_insensitive_p() {
        assert_eq!(
            preprocess_mssql_sql("SELECT * FROM users WHERE id = @P1"),
            "SELECT * FROM users WHERE id = ?"
        );
    }

    #[test]
    fn test_preprocess_mssql_non_placeholder_at_variable_unchanged() {
        // @variable (not @pN pattern) must not be touched
        assert_eq!(preprocess_mssql_sql("SELECT @myvar"), "SELECT @myvar");
    }

    #[test]
    fn test_preprocess_mssql_multi_digit_placeholder() {
        assert_eq!(preprocess_mssql_sql("SELECT @p10, @p2"), "SELECT ?, ?");
    }

    #[test]
    fn test_preprocess_mssql_output_inserted_simple() {
        let sql =
            "INSERT INTO users (id, name) OUTPUT INSERTED.id, INSERTED.name VALUES (@p1, @p2)";
        let result = preprocess_mssql_sql(sql);
        // Should convert OUTPUT INSERTED.col to RETURNING col and @pN to ?
        assert!(result.contains("RETURNING id, name"), "got: {}", result);
        assert!(result.contains("VALUES (?, ?)"), "got: {}", result);
        assert!(!result.contains("OUTPUT"), "got: {}", result);
    }

    #[test]
    fn test_preprocess_mssql_output_inserted_full_example() {
        let sql = "INSERT INTO users (id, name, email, active) OUTPUT INSERTED.id, INSERTED.name, INSERTED.email, INSERTED.active, INSERTED.created_at VALUES (@p1, @p2, @p3, @p4)";
        let result = preprocess_mssql_sql(sql);
        assert!(
            result.contains("RETURNING id, name, email, active, created_at"),
            "got: {}",
            result
        );
        assert!(result.contains("VALUES (?, ?, ?, ?)"), "got: {}", result);
    }

    #[test]
    fn test_preprocess_mssql_output_case_insensitive() {
        let sql = "INSERT INTO users (id) output inserted.id values (@p1)";
        let result = preprocess_mssql_sql(sql);
        assert!(result.contains("RETURNING id"), "got: {}", result);
        // The original lowercase "values" is preserved, then @p1 becomes ?
        assert!(
            result.contains("values (?)") || result.contains("VALUES (?)"),
            "got: {}",
            result
        );
    }

    #[test]
    fn test_preprocess_mssql_no_output_unchanged() {
        let sql = "INSERT INTO users (id, name) VALUES (@p1, @p2)";
        let result = preprocess_mssql_sql(sql);
        assert_eq!(result, "INSERT INTO users (id, name) VALUES (?, ?)");
    }

    #[test]
    fn test_preprocess_mssql_output_with_string_literal() {
        // @p1 inside a string should be preserved by placeholder conversion
        let sql =
            "INSERT INTO users (id, name) OUTPUT INSERTED.id, INSERTED.name VALUES (@p1, '@p2')";
        let result = preprocess_mssql_sql(sql);
        assert!(result.contains("RETURNING id, name"), "got: {}", result);
        assert!(result.contains("(?, '@p2')"), "got: {}", result);
    }

    #[test]
    fn test_preprocess_mssql_output_with_whitespace() {
        let sql =
            "INSERT INTO users (id, name)\nOUTPUT INSERTED.id,\n  INSERTED.name\nVALUES (@p1, @p2)";
        let result = preprocess_mssql_sql(sql);
        assert!(result.contains("RETURNING id, name"), "got: {}", result);
        assert!(result.contains("VALUES (?, ?)"), "got: {}", result);
    }
}