sql-orm 0.2.0-rc.2

Public API crate for the sql-orm workspace.
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
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
use crate::context::SharedConnection;
use sql_orm_core::{FromRow, OrmError, SqlTypeMapping, SqlValue};
use sql_orm_query::CompiledQuery;
use sql_orm_tiberius::ExecuteResult;
use std::collections::BTreeSet;
use std::marker::PhantomData;

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
/// Explicit execution classification for raw SQL.
///
/// Raw SQL is not inspected to decide retry safety. Typed raw queries default
/// to `RawNoRetry`; callers must opt into `ReadOnly` deliberately when the SQL
/// is idempotent and safe to retry.
pub enum RawSqlExecution {
    /// Read-only SQL that may use configured read retry policy.
    ReadOnly,
    /// SQL with side effects.
    Write,
    /// Migration or schema-management SQL.
    Migration,
    /// Raw SQL that must not use automatic retry.
    RawNoRetry,
}

impl RawSqlExecution {
    const fn query_execution(self) -> sql_orm_query::QueryExecution {
        match self {
            Self::ReadOnly => sql_orm_query::QueryExecution::ReadOnly,
            Self::Write => sql_orm_query::QueryExecution::Write,
            Self::Migration => sql_orm_query::QueryExecution::Migration,
            Self::RawNoRetry => sql_orm_query::QueryExecution::RawNoRetry,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
/// SQL Server query hints supported by typed raw queries.
///
/// Hints are rendered by the ORM at the end of the raw SQL as an
/// `OPTION (...)` clause. Do not also write a top-level `OPTION (...)` clause
/// manually in the SQL string.
pub enum QueryHint {
    /// Adds `OPTION (RECOMPILE)` to the query.
    Recompile,
}

impl QueryHint {
    const fn sql(self) -> &'static str {
        match self {
            Self::Recompile => "RECOMPILE",
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct RawPlaceholderPlan {
    max_index: usize,
}

impl RawPlaceholderPlan {
    const fn expected_param_count(&self) -> usize {
        self.max_index
    }
}

/// Converts one raw SQL parameter into an ORM `SqlValue`.
///
/// Raw SQL placeholders are positional and must be written as `@P1`, `@P2`,
/// and so on. Reusing the same placeholder index reuses the same parameter
/// value.
pub trait RawParam {
    /// Converts this value into a SQL Server parameter value.
    fn into_sql_value(self) -> SqlValue;
}

macro_rules! impl_raw_param_via_sql_type_mapping {
    ($($ty:ty),+ $(,)?) => {
        $(
            impl RawParam for $ty {
                fn into_sql_value(self) -> SqlValue {
                    <Self as SqlTypeMapping>::to_sql_value(self)
                }
            }
        )+
    };
}

impl_raw_param_via_sql_type_mapping!(
    bool,
    i32,
    i64,
    f64,
    String,
    Vec<u8>,
    uuid::Uuid,
    rust_decimal::Decimal,
    chrono::NaiveDate,
    chrono::NaiveDateTime,
);

impl RawParam for SqlValue {
    fn into_sql_value(self) -> SqlValue {
        self
    }
}

impl RawParam for &str {
    fn into_sql_value(self) -> SqlValue {
        SqlValue::String(self.to_string())
    }
}

impl<T> RawParam for Option<T>
where
    T: RawParam,
{
    fn into_sql_value(self) -> SqlValue {
        self.map(RawParam::into_sql_value).unwrap_or(SqlValue::Null)
    }
}

/// Converts a parameter collection into raw SQL parameter values.
///
/// Implemented for `()`, `Vec<T>` where `T: RawParam`, and tuples up to 12
/// elements.
pub trait RawParams {
    /// Converts this collection into positional SQL values.
    fn into_sql_values(self) -> Vec<SqlValue>;
}

impl RawParams for () {
    fn into_sql_values(self) -> Vec<SqlValue> {
        Vec::new()
    }
}

impl<T> RawParams for Vec<T>
where
    T: RawParam,
{
    fn into_sql_values(self) -> Vec<SqlValue> {
        self.into_iter().map(RawParam::into_sql_value).collect()
    }
}

macro_rules! impl_raw_params_tuple {
    ($($name:ident),+ $(,)?) => {
        impl<$($name),+> RawParams for ($($name,)+)
        where
            $($name: RawParam),+
        {
            #[allow(non_snake_case)]
            fn into_sql_values(self) -> Vec<SqlValue> {
                let ($($name,)+) = self;
                vec![$($name.into_sql_value()),+]
            }
        }
    };
}

impl_raw_params_tuple!(A);
impl_raw_params_tuple!(A, B);
impl_raw_params_tuple!(A, B, C);
impl_raw_params_tuple!(A, B, C, D);
impl_raw_params_tuple!(A, B, C, D, E);
impl_raw_params_tuple!(A, B, C, D, E, F);
impl_raw_params_tuple!(A, B, C, D, E, F, G);
impl_raw_params_tuple!(A, B, C, D, E, F, G, H);
impl_raw_params_tuple!(A, B, C, D, E, F, G, H, I);
impl_raw_params_tuple!(A, B, C, D, E, F, G, H, I, J);
impl_raw_params_tuple!(A, B, C, D, E, F, G, H, I, J, K);
impl_raw_params_tuple!(A, B, C, D, E, F, G, H, I, J, K, L);

#[derive(Clone)]
/// Typed raw SQL query that materializes rows as `T`.
///
/// `T` must implement `FromRow`. Raw SQL is executed exactly as written after
/// parameter validation and optional query hint rendering; it does not apply
/// ORM tenant or soft-delete filters automatically.
pub struct RawQuery<T> {
    connection: SharedConnection,
    sql: String,
    params: Vec<SqlValue>,
    query_hints: BTreeSet<QueryHint>,
    execution: RawSqlExecution,
    _row: PhantomData<fn() -> T>,
}

impl<T> RawQuery<T>
where
    T: FromRow + Send,
{
    pub(crate) fn new(connection: SharedConnection, sql: impl Into<String>) -> Self {
        Self {
            connection,
            sql: sql.into(),
            params: Vec::new(),
            query_hints: BTreeSet::new(),
            execution: RawSqlExecution::RawNoRetry,
            _row: PhantomData,
        }
    }

    /// Appends one positional parameter.
    pub fn param<P>(mut self, value: P) -> Self
    where
        P: RawParam,
    {
        self.params.push(value.into_sql_value());
        self
    }

    /// Appends multiple positional parameters.
    pub fn params<P>(mut self, values: P) -> Self
    where
        P: RawParams,
    {
        self.params.extend(values.into_sql_values());
        self
    }

    /// Adds a SQL Server query hint to render at the end of the raw query.
    ///
    /// Repeated hints are deduplicated with stable ordering.
    pub fn query_hint(mut self, hint: QueryHint) -> Self {
        self.query_hints.insert(hint);
        self
    }

    /// Marks this raw query as read-only and eligible for configured read retry.
    ///
    /// Use this only for idempotent SQL that does not mutate state, acquire
    /// non-repeatable locks, call side-effecting procedures, or depend on
    /// per-execution side effects.
    pub fn read_only(mut self) -> Self {
        self.execution = RawSqlExecution::ReadOnly;
        self
    }

    /// Forces this raw query to bypass automatic retry.
    pub fn no_retry(mut self) -> Self {
        self.execution = RawSqlExecution::RawNoRetry;
        self
    }

    /// Executes the query and materializes all rows.
    pub async fn all(self) -> Result<Vec<T>, OrmError> {
        let compiled = self.compiled_query()?;
        let mut connection = self.connection.lock().await?;
        connection.fetch_all(compiled).await
    }

    /// Executes the query and materializes the first row, if any.
    pub async fn first(self) -> Result<Option<T>, OrmError> {
        let compiled = self.compiled_query()?;
        let mut connection = self.connection.lock().await?;
        connection.fetch_one(compiled).await
    }

    fn compiled_query(&self) -> Result<CompiledQuery, OrmError> {
        compiled_raw_query_with_hints(
            &self.sql,
            self.params.clone(),
            &self.query_hints,
            self.execution,
        )
    }
}

#[derive(Clone)]
/// Raw SQL command for statements that do not materialize rows.
///
/// Use this for `INSERT`, `UPDATE`, `DELETE`, DDL, or stored procedure calls
/// where the caller only needs the execution result. Query hints are supported
/// only on `RawQuery<T>`.
pub struct RawCommand {
    connection: SharedConnection,
    sql: String,
    params: Vec<SqlValue>,
    execution: RawSqlExecution,
}

impl RawCommand {
    pub(crate) fn new(connection: SharedConnection, sql: impl Into<String>) -> Self {
        Self {
            connection,
            sql: sql.into(),
            params: Vec::new(),
            execution: RawSqlExecution::Write,
        }
    }

    /// Appends one positional parameter.
    pub fn param<P>(mut self, value: P) -> Self
    where
        P: RawParam,
    {
        self.params.push(value.into_sql_value());
        self
    }

    /// Appends multiple positional parameters.
    pub fn params<P>(mut self, values: P) -> Self
    where
        P: RawParams,
    {
        self.params.extend(values.into_sql_values());
        self
    }

    /// Marks this raw command as migration or schema-management SQL.
    pub fn migration(mut self) -> Self {
        self.execution = RawSqlExecution::Migration;
        self
    }

    /// Forces this raw command to bypass automatic retry.
    pub fn no_retry(mut self) -> Self {
        self.execution = RawSqlExecution::RawNoRetry;
        self
    }

    /// Executes the command and returns affected-row information.
    pub async fn execute(self) -> Result<ExecuteResult, OrmError> {
        let compiled = self.compiled_query()?;
        let mut connection = self.connection.lock().await?;
        connection.execute(compiled).await
    }

    fn compiled_query(&self) -> Result<CompiledQuery, OrmError> {
        compiled_raw_query_with_execution(&self.sql, self.params.clone(), self.execution)
    }
}

#[cfg(test)]
fn compiled_raw_query(sql: &str, params: Vec<SqlValue>) -> Result<CompiledQuery, OrmError> {
    compiled_raw_query_with_execution(sql, params, RawSqlExecution::RawNoRetry)
}

fn compiled_raw_query_with_execution(
    sql: &str,
    params: Vec<SqlValue>,
    execution: RawSqlExecution,
) -> Result<CompiledQuery, OrmError> {
    compiled_raw_query_with_hints(sql, params, &BTreeSet::new(), execution)
}

fn compiled_raw_query_with_hints(
    sql: &str,
    params: Vec<SqlValue>,
    query_hints: &BTreeSet<QueryHint>,
    execution: RawSqlExecution,
) -> Result<CompiledQuery, OrmError> {
    validate_raw_sql_parameters(sql, params.len())?;

    let sql = render_raw_sql_with_hints(sql, query_hints)?;

    Ok(CompiledQuery::with_execution(
        sql,
        params,
        execution.query_execution(),
    ))
}

fn render_raw_sql_with_hints(
    sql: &str,
    query_hints: &BTreeSet<QueryHint>,
) -> Result<String, OrmError> {
    if query_hints.is_empty() {
        return Ok(sql.to_string());
    }

    if contains_top_level_option_clause(sql) {
        return Err(OrmError::compile(
            "raw SQL already contains OPTION (...); remove it before using query_hint(...)",
        ));
    }

    let mut sql = sql.trim_end().trim_end_matches(';').trim_end().to_string();
    let hints = query_hints
        .iter()
        .copied()
        .map(QueryHint::sql)
        .collect::<Vec<_>>()
        .join(", ");

    sql.push_str(" OPTION (");
    sql.push_str(&hints);
    sql.push(')');

    Ok(sql)
}

pub(crate) fn validate_raw_sql_parameters(sql: &str, param_count: usize) -> Result<(), OrmError> {
    let plan = analyze_placeholders(sql)?;

    if plan.expected_param_count() != param_count {
        return Err(OrmError::compile(format!(
            "raw SQL parameter count mismatch: SQL expects {} parameter(s), received {}",
            plan.expected_param_count(),
            param_count
        )));
    }

    Ok(())
}

fn analyze_placeholders(sql: &str) -> Result<RawPlaceholderPlan, OrmError> {
    let bytes = sql.as_bytes();
    let mut index = 0;
    let mut placeholders = BTreeSet::new();

    while index + 2 < bytes.len() {
        if let Some(next_index) = skip_sql_non_code(bytes, index) {
            index = next_index;
            continue;
        }

        if bytes[index] == b'@' && bytes[index + 1] == b'P' && bytes[index + 2].is_ascii_digit() {
            index += 2;
            let start = index;

            while index < bytes.len() && bytes[index].is_ascii_digit() {
                index += 1;
            }

            let raw_index = sql[start..index].parse::<usize>().map_err(|_| {
                OrmError::compile("raw SQL placeholder index is larger than supported")
            })?;

            if raw_index == 0 {
                return Err(OrmError::compile("raw SQL placeholders must start at @P1"));
            }

            placeholders.insert(raw_index);
            continue;
        }

        index += 1;
    }

    let max_index = placeholders.iter().next_back().copied().unwrap_or(0);
    for expected in 1..=max_index {
        if !placeholders.contains(&expected) {
            return Err(OrmError::compile(format!(
                "raw SQL placeholders must be continuous from @P1 to @P{}",
                max_index
            )));
        }
    }

    Ok(RawPlaceholderPlan { max_index })
}

fn skip_sql_non_code(bytes: &[u8], index: usize) -> Option<usize> {
    match bytes[index] {
        b'\'' => Some(skip_quoted_string(bytes, index)),
        b'[' => Some(skip_bracket_identifier(bytes, index)),
        b'"' => Some(skip_double_quoted_identifier(bytes, index)),
        b'-' if index + 1 < bytes.len() && bytes[index + 1] == b'-' => {
            Some(skip_line_comment(bytes, index))
        }
        b'/' if index + 1 < bytes.len() && bytes[index + 1] == b'*' => {
            Some(skip_block_comment(bytes, index))
        }
        _ => None,
    }
}

fn skip_quoted_string(bytes: &[u8], mut index: usize) -> usize {
    index += 1;
    while index < bytes.len() {
        if bytes[index] == b'\'' {
            index += 1;
            if index < bytes.len() && bytes[index] == b'\'' {
                index += 1;
                continue;
            }
            break;
        }
        index += 1;
    }
    index
}

fn skip_bracket_identifier(bytes: &[u8], mut index: usize) -> usize {
    index += 1;
    while index < bytes.len() {
        if bytes[index] == b']' {
            index += 1;
            if index < bytes.len() && bytes[index] == b']' {
                index += 1;
                continue;
            }
            break;
        }
        index += 1;
    }
    index
}

fn skip_double_quoted_identifier(bytes: &[u8], mut index: usize) -> usize {
    index += 1;
    while index < bytes.len() {
        if bytes[index] == b'"' {
            index += 1;
            if index < bytes.len() && bytes[index] == b'"' {
                index += 1;
                continue;
            }
            break;
        }
        index += 1;
    }
    index
}

fn skip_line_comment(bytes: &[u8], mut index: usize) -> usize {
    index += 2;
    while index < bytes.len() && !matches!(bytes[index], b'\n' | b'\r') {
        index += 1;
    }
    index
}

fn skip_block_comment(bytes: &[u8], mut index: usize) -> usize {
    index += 2;
    while index + 1 < bytes.len() {
        if bytes[index] == b'*' && bytes[index + 1] == b'/' {
            return index + 2;
        }
        index += 1;
    }
    bytes.len()
}

fn contains_top_level_option_clause(sql: &str) -> bool {
    let bytes = sql.as_bytes();
    let mut index = 0;
    let mut depth = 0_i32;

    while index < bytes.len() {
        match bytes[index] {
            b'\'' => {
                index += 1;
                while index < bytes.len() {
                    if bytes[index] == b'\'' {
                        index += 1;
                        if index < bytes.len() && bytes[index] == b'\'' {
                            index += 1;
                            continue;
                        }
                        break;
                    }
                    index += 1;
                }
            }
            b'[' => {
                index += 1;
                while index < bytes.len() {
                    if bytes[index] == b']' {
                        index += 1;
                        break;
                    }
                    index += 1;
                }
            }
            b'-' if index + 1 < bytes.len() && bytes[index + 1] == b'-' => {
                index += 2;
                while index < bytes.len() && !matches!(bytes[index], b'\n' | b'\r') {
                    index += 1;
                }
            }
            b'/' if index + 1 < bytes.len() && bytes[index + 1] == b'*' => {
                index += 2;
                while index + 1 < bytes.len() {
                    if bytes[index] == b'*' && bytes[index + 1] == b'/' {
                        index += 2;
                        break;
                    }
                    index += 1;
                }
            }
            b'(' => {
                depth += 1;
                index += 1;
            }
            b')' => {
                depth = depth.saturating_sub(1);
                index += 1;
            }
            _ if depth == 0 && starts_with_keyword(sql, index, "OPTION") => {
                let after_keyword = index + "OPTION".len();
                let mut cursor = after_keyword;
                while cursor < bytes.len() && bytes[cursor].is_ascii_whitespace() {
                    cursor += 1;
                }
                return cursor < bytes.len() && bytes[cursor] == b'(';
            }
            _ => index += 1,
        }
    }

    false
}

fn starts_with_keyword(sql: &str, index: usize, keyword: &str) -> bool {
    let bytes = sql.as_bytes();
    let keyword_bytes = keyword.as_bytes();

    if index + keyword_bytes.len() > bytes.len() {
        return false;
    }

    if !bytes[index..index + keyword_bytes.len()].eq_ignore_ascii_case(keyword_bytes) {
        return false;
    }

    let before_is_boundary = index == 0 || !is_identifier_byte(bytes[index - 1]);
    let after = index + keyword_bytes.len();
    let after_is_boundary = after == bytes.len() || !is_identifier_byte(bytes[after]);

    before_is_boundary && after_is_boundary
}

fn is_identifier_byte(byte: u8) -> bool {
    byte.is_ascii_alphanumeric() || byte == b'_'
}

#[cfg(test)]
mod tests {
    use super::RawSqlExecution;
    use super::{
        QueryHint, RawParam, RawParams, compiled_raw_query, compiled_raw_query_with_execution,
        compiled_raw_query_with_hints, contains_top_level_option_clause,
        validate_raw_sql_parameters,
    };
    use chrono::NaiveDate;
    use rust_decimal::Decimal;
    use sql_orm_core::{OrmErrorKind, SqlValue};
    use sql_orm_query::QueryExecution;
    use std::collections::BTreeSet;
    use uuid::Uuid;

    #[test]
    fn validates_continuous_placeholders_by_max_index() {
        validate_raw_sql_parameters("SELECT @P1, @P2, @P3", 3).unwrap();
    }

    #[test]
    fn validates_continuous_placeholders_through_highest_index() {
        validate_raw_sql_parameters(
            "SELECT @P1, @P2, @P3, @P4, @P5, @P6, @P7, @P8, @P9, @P10, @P11, @P12",
            12,
        )
        .unwrap();
    }

    #[test]
    fn allows_repeated_placeholder_to_reuse_one_param() {
        validate_raw_sql_parameters("SELECT @P1 WHERE owner_id = @P1", 1).unwrap();
    }

    #[test]
    fn rejects_extra_params_without_placeholders() {
        let error = validate_raw_sql_parameters("SELECT 1", 1).unwrap_err();

        assert_eq!(error.kind(), OrmErrorKind::Compile);
        assert!(error.message().contains("expects 0 parameter"));
    }

    #[test]
    fn rejects_missing_params() {
        let error = validate_raw_sql_parameters("SELECT @P1, @P2", 1).unwrap_err();

        assert!(error.message().contains("expects 2 parameter"));
    }

    #[test]
    fn rejects_non_continuous_placeholders() {
        let error = validate_raw_sql_parameters("SELECT @P1, @P3", 2).unwrap_err();

        assert!(error.message().contains("continuous from @P1 to @P3"));
    }

    #[test]
    fn rejects_zero_index_placeholder() {
        let error = validate_raw_sql_parameters("SELECT @P0", 0).unwrap_err();

        assert!(error.message().contains("start at @P1"));
    }

    #[test]
    fn ignores_placeholder_text_inside_sql_non_code_regions() {
        let sql = r#"
            SELECT @P1 AS value,
                   '@P2 literal '' @P3 escaped quote' AS string_value,
                   [@P4 identifier] AS bracket_identifier,
                   "@P5 quoted identifier" AS quoted_identifier
            -- @P6 line comment
            /* @P7 block comment */
            WHERE label = @P1
        "#;

        validate_raw_sql_parameters(sql, 1).unwrap();
    }

    #[test]
    fn ignores_placeholder_text_in_raw_sql_without_parameters() {
        let sql = r#"
            SELECT '@P1 is documentation' AS literal,
                   [@P2 is an identifier] AS identifier
            -- @P3 is a comment
            /* @P4 is also a comment */
        "#;

        validate_raw_sql_parameters(sql, 0).unwrap();
    }

    #[test]
    fn counts_placeholders_after_ignored_sql_regions() {
        let sql = "SELECT '@P1 ignored' AS label -- @P2 ignored\nWHERE id = @P1 AND code = @P2";

        validate_raw_sql_parameters(sql, 2).unwrap();
    }

    #[test]
    fn raw_params_tuple_preserves_order_and_values() {
        let values = (
            true,
            7_i32,
            9_i64,
            3.5_f64,
            "draft",
            String::from("owned"),
            vec![1_u8, 2],
            Uuid::nil(),
            Decimal::new(1234, 2),
            NaiveDate::from_ymd_opt(2026, 4, 26).unwrap(),
            NaiveDate::from_ymd_opt(2026, 4, 26)
                .unwrap()
                .and_hms_opt(10, 20, 30)
                .unwrap(),
            SqlValue::Null,
        )
            .into_sql_values();

        assert_eq!(
            values,
            vec![
                SqlValue::Bool(true),
                SqlValue::I32(7),
                SqlValue::I64(9),
                SqlValue::F64(3.5),
                SqlValue::String("draft".to_string()),
                SqlValue::String("owned".to_string()),
                SqlValue::Bytes(vec![1, 2]),
                SqlValue::Uuid(Uuid::nil()),
                SqlValue::Decimal(Decimal::new(1234, 2)),
                SqlValue::Date(NaiveDate::from_ymd_opt(2026, 4, 26).unwrap()),
                SqlValue::DateTime(
                    NaiveDate::from_ymd_opt(2026, 4, 26)
                        .unwrap()
                        .and_hms_opt(10, 20, 30)
                        .unwrap()
                ),
                SqlValue::Null,
            ]
        );
    }

    #[test]
    fn raw_param_option_none_maps_to_null() {
        assert_eq!(Option::<i64>::None.into_sql_value(), SqlValue::Null);
    }

    #[test]
    fn raw_param_option_some_maps_inner_value() {
        assert_eq!(Some(42_i64).into_sql_value(), SqlValue::I64(42));
    }

    #[test]
    fn raw_params_vec_preserves_order() {
        let values = vec![1_i64, 2_i64, 3_i64].into_sql_values();

        assert_eq!(
            values,
            vec![SqlValue::I64(1), SqlValue::I64(2), SqlValue::I64(3)]
        );
    }

    #[test]
    fn raw_params_unit_maps_to_empty_params() {
        assert_eq!(().into_sql_values(), Vec::<SqlValue>::new());
    }

    #[test]
    fn compiled_raw_query_preserves_sql_and_parameter_order() {
        let params = (
            SqlValue::Null,
            true,
            7_i32,
            9_i64,
            3.5_f64,
            "draft",
            vec![1_u8, 2],
            Uuid::nil(),
            Decimal::new(1234, 2),
            NaiveDate::from_ymd_opt(2026, 4, 26).unwrap(),
            NaiveDate::from_ymd_opt(2026, 4, 26)
                .unwrap()
                .and_hms_opt(10, 20, 30)
                .unwrap(),
        )
            .into_sql_values();

        let compiled = compiled_raw_query(
            "SELECT @P1, @P2, @P3, @P4, @P5, @P6, @P7, @P8, @P9, @P10, @P11",
            params,
        )
        .unwrap();

        assert_eq!(
            compiled.sql,
            "SELECT @P1, @P2, @P3, @P4, @P5, @P6, @P7, @P8, @P9, @P10, @P11"
        );
        assert_eq!(
            compiled.params,
            vec![
                SqlValue::Null,
                SqlValue::Bool(true),
                SqlValue::I32(7),
                SqlValue::I64(9),
                SqlValue::F64(3.5),
                SqlValue::String("draft".to_string()),
                SqlValue::Bytes(vec![1, 2]),
                SqlValue::Uuid(Uuid::nil()),
                SqlValue::Decimal(Decimal::new(1234, 2)),
                SqlValue::Date(NaiveDate::from_ymd_opt(2026, 4, 26).unwrap()),
                SqlValue::DateTime(
                    NaiveDate::from_ymd_opt(2026, 4, 26)
                        .unwrap()
                        .and_hms_opt(10, 20, 30)
                        .unwrap()
                ),
            ]
        );
    }

    #[test]
    fn compiled_raw_query_allows_repeated_placeholder_with_single_param() {
        let compiled = compiled_raw_query(
            "SELECT * FROM users WHERE owner_id = @P1 OR reviewer_id = @P1",
            vec![SqlValue::I64(42)],
        )
        .unwrap();

        assert_eq!(compiled.params, vec![SqlValue::I64(42)]);
    }

    #[test]
    fn compiled_raw_query_appends_recompile_hint_after_parameters() {
        let hints = BTreeSet::from([QueryHint::Recompile]);
        let compiled = compiled_raw_query_with_hints(
            "SELECT * FROM users WHERE owner_id = @P1",
            vec![SqlValue::I64(42)],
            &hints,
            RawSqlExecution::ReadOnly,
        )
        .unwrap();

        assert_eq!(
            compiled.sql,
            "SELECT * FROM users WHERE owner_id = @P1 OPTION (RECOMPILE)"
        );
        assert_eq!(compiled.params, vec![SqlValue::I64(42)]);
        assert_eq!(compiled.execution, QueryExecution::ReadOnly);
    }

    #[test]
    fn compiled_raw_query_deduplicates_repeated_query_hints() {
        let hints = BTreeSet::from([QueryHint::Recompile, QueryHint::Recompile]);
        let compiled =
            compiled_raw_query_with_hints("SELECT 1", vec![], &hints, RawSqlExecution::ReadOnly)
                .unwrap();

        assert_eq!(compiled.sql, "SELECT 1 OPTION (RECOMPILE)");
    }

    #[test]
    fn compiled_raw_query_places_hint_before_trailing_semicolon() {
        let hints = BTreeSet::from([QueryHint::Recompile]);
        let compiled = compiled_raw_query_with_hints(
            "SELECT 1;   ",
            vec![],
            &hints,
            RawSqlExecution::ReadOnly,
        )
        .unwrap();

        assert_eq!(compiled.sql, "SELECT 1 OPTION (RECOMPILE)");
    }

    #[test]
    fn compiled_raw_query_rejects_existing_top_level_option_clause_with_hints() {
        let hints = BTreeSet::from([QueryHint::Recompile]);
        let error = compiled_raw_query_with_hints(
            "SELECT 1 OPTION (MAXDOP 1)",
            vec![],
            &hints,
            RawSqlExecution::ReadOnly,
        )
        .unwrap_err();

        assert_eq!(error.kind(), OrmErrorKind::Compile);
        assert!(error.message().contains("already contains OPTION"));
    }

    #[test]
    fn raw_query_defaults_to_no_retry_execution_classification() {
        let compiled = compiled_raw_query("SELECT 1", vec![]).unwrap();

        assert_eq!(compiled.execution, QueryExecution::RawNoRetry);
    }

    #[test]
    fn raw_sql_execution_classification_is_explicit() {
        let read_only =
            compiled_raw_query_with_execution("SELECT 1", vec![], RawSqlExecution::ReadOnly)
                .unwrap();
        let write = compiled_raw_query_with_execution(
            "UPDATE users SET active = 1",
            vec![],
            RawSqlExecution::Write,
        )
        .unwrap();
        let migration = compiled_raw_query_with_execution(
            "ALTER TABLE users ADD active bit NOT NULL DEFAULT 1",
            vec![],
            RawSqlExecution::Migration,
        )
        .unwrap();

        assert_eq!(read_only.execution, QueryExecution::ReadOnly);
        assert_eq!(write.execution, QueryExecution::Write);
        assert_eq!(migration.execution, QueryExecution::Migration);
    }

    #[test]
    fn detects_top_level_option_clause_without_matching_strings_or_nested_queries() {
        assert!(contains_top_level_option_clause(
            "SELECT 1 OPTION (RECOMPILE)"
        ));
        assert!(!contains_top_level_option_clause(
            "SELECT 'OPTION (RECOMPILE)' AS text_value"
        ));
        assert!(!contains_top_level_option_clause(
            "SELECT * FROM (SELECT 1 OPTION (RECOMPILE)) AS nested"
        ));
    }

    #[test]
    fn compiled_raw_query_rejects_non_continuous_placeholders() {
        let error = compiled_raw_query(
            "SELECT * FROM users WHERE owner_id = @P1 OR reviewer_id = @P3",
            vec![SqlValue::I64(42), SqlValue::I64(7)],
        )
        .unwrap_err();

        assert!(error.message().contains("continuous from @P1 to @P3"));
    }
}