vibesql-types 0.2.0

Type system for vibesql SQL database engine
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
//! SQL mode configuration for dialect-specific behavior.

#![allow(clippy::derivable_impls)]

mod config;
mod operators;
mod strings;
pub mod types;

pub use config::MySqlModeFlags;
// Re-export operator types and traits
pub use operators::{ConcatOperator, DivisionBehavior, OperatorBehavior};
// Re-export string types and traits
pub use strings::{Collation, StringBehavior};

/// SQL compatibility mode
///
/// VibeSQL supports different SQL dialect modes to match the behavior
/// of different database systems. This is necessary because SQL standards
/// allow implementation-defined behavior in certain areas.
///
/// ## Differences by Mode
///
/// See SQL_COMPATIBILITY_MODE.md in the repository root for a comprehensive list
/// of behavioral differences between modes.
///
/// ### Division Operator (`/`)
/// - **MySQL**: `INTEGER / INTEGER → DECIMAL` (floating-point division)
///   - Example: `83 / 6 = 13.8333`
/// - **SQLite**: `INTEGER / INTEGER → INTEGER` (truncated division)
///   - Example: `83 / 6 = 13`
///
/// ## Default Mode
///
/// MySQL mode is the default to maximize compatibility with the
/// dolthub/sqllogictest test suite, which was generated from MySQL 8.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum SqlMode {
    /// MySQL 8.0+ compatibility mode (default)
    ///
    /// - Division returns DECIMAL (floating-point)
    /// - Other MySQL-specific behaviors controlled by flags
    MySQL { flags: MySqlModeFlags },

    /// SQLite 3 compatibility mode
    ///
    /// - Division returns INTEGER (truncated)
    /// - Other SQLite-specific behaviors
    ///
    /// Note: Currently not fully implemented. Many features will error
    /// with "TODO: SQLite mode not yet supported" messages.
    SQLite,
}

impl Default for SqlMode {
    fn default() -> Self {
        // Default to MySQL mode for SQLLogicTest compatibility
        // The dolthub/sqllogictest corpus was regenerated against MySQL 8.x
        // and expects MySQL semantics including decimal division
        // (INTEGER / INTEGER → DECIMAL)
        SqlMode::MySQL { flags: MySqlModeFlags::default() }
    }
}

impl std::fmt::Display for SqlMode {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            SqlMode::MySQL { .. } => write!(f, "mysql"),
            SqlMode::SQLite => write!(f, "sqlite"),
        }
    }
}

impl std::str::FromStr for SqlMode {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        // First try simple mode names
        match s.to_lowercase().as_str() {
            "mysql" => return Ok(SqlMode::MySQL { flags: MySqlModeFlags::default() }),
            // MySQL mode with SQLite division semantics
            // This is used by SQLLogicTest where MySQL syntax is needed (e.g., CAST...AS SIGNED)
            // but division should behave like SQLite (INTEGER / INTEGER → INTEGER)
            // because the expected results were generated using SQLite.
            "mysql_slt" => {
                return Ok(SqlMode::MySQL {
                    flags: MySqlModeFlags::with_sqlite_division_semantics(),
                })
            }
            "sqlite" => return Ok(SqlMode::SQLite),
            _ => {}
        }

        // Try parsing as MySQL comma-separated mode flags
        // e.g., "STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,..."
        // Also handles leading/trailing commas and empty segments
        parse_mysql_mode_flags(s)
    }
}

/// Parse MySQL comma-separated mode flags
///
/// This handles strings like:
/// - "STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,
///   NO_ENGINE_SUBSTITUTION"
/// - ",STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,..." (leading comma from REPLACE() removing first mode)
/// - "STRICT_TRANS_TABLES," (trailing comma)
/// - "MODE1,,MODE2" (empty segments)
///
/// MySQL mode flags we recognize and map:
/// - STRICT_TRANS_TABLES → strict_mode
/// - PIPES_AS_CONCAT → pipes_as_concat
/// - ANSI_QUOTES → ansi_quotes
/// - ANSI → pipes_as_concat + ansi_quotes
///
/// Other MySQL modes (NO_ZERO_IN_DATE, NO_ZERO_DATE, ERROR_FOR_DIVISION_BY_ZERO,
/// NO_ENGINE_SUBSTITUTION, ONLY_FULL_GROUP_BY, etc.) are accepted but ignored
/// as they don't affect our behavior.
fn parse_mysql_mode_flags(s: &str) -> Result<SqlMode, String> {
    let mut flags = MySqlModeFlags::default();

    // Split by comma and process each mode
    for mode in s.split(',') {
        // Skip empty segments (handles leading/trailing commas and consecutive commas)
        let mode = mode.trim();
        if mode.is_empty() {
            continue;
        }

        // Match MySQL mode flags (case-insensitive)
        match mode.to_uppercase().as_str() {
            // Modes we actually implement
            "STRICT_TRANS_TABLES" | "STRICT_ALL_TABLES" => {
                flags.strict_mode = true;
            }
            "PIPES_AS_CONCAT" => {
                flags.pipes_as_concat = true;
            }
            "ANSI_QUOTES" => {
                flags.ansi_quotes = true;
            }
            "ANSI" => {
                // ANSI mode is a combination
                flags.pipes_as_concat = true;
                flags.ansi_quotes = true;
            }

            // Common MySQL modes we accept but don't implement
            // These are standard MySQL 8.0 defaults and commonly used modes
            "NO_ZERO_IN_DATE"
            | "NO_ZERO_DATE"
            | "ERROR_FOR_DIVISION_BY_ZERO"
            | "NO_ENGINE_SUBSTITUTION"
            | "ONLY_FULL_GROUP_BY"
            | "NO_AUTO_CREATE_USER"
            | "NO_AUTO_VALUE_ON_ZERO"
            | "NO_BACKSLASH_ESCAPES"
            | "NO_DIR_IN_CREATE"
            | "NO_UNSIGNED_SUBTRACTION"
            | "PAD_CHAR_TO_FULL_LENGTH"
            | "REAL_AS_FLOAT"
            | "TIME_TRUNCATE_FRACTIONAL"
            | "IGNORE_SPACE"
            | "TRADITIONAL"
            | "ALLOW_INVALID_DATES"
            | "HIGH_NOT_PRECEDENCE" => {
                // Accepted but not implemented - no action needed
            }

            // Unknown mode - but we're lenient to allow future MySQL versions
            // We only error on truly unrecognized patterns
            other => {
                // Check if it looks like a MySQL mode (alphanumeric with underscores)
                if other.chars().all(|c| c.is_ascii_alphanumeric() || c == '_') {
                    // Looks like a MySQL mode we don't know - accept it silently
                    // This allows forward compatibility with new MySQL modes
                } else {
                    return Err(format!(
                        "Unknown SQL mode: '{}'. Valid modes: mysql, mysql_slt, sqlite, or MySQL mode flags",
                        s
                    ));
                }
            }
        }
    }

    Ok(SqlMode::MySQL { flags })
}

impl SqlMode {
    /// Get the MySQL mode flags, if in MySQL mode
    pub fn mysql_flags(&self) -> Option<&MySqlModeFlags> {
        match self {
            SqlMode::MySQL { flags } => Some(flags),
            SqlMode::SQLite => None,
        }
    }
}

// Supported collations for each SQL mode
#[allow(dead_code)]
const MYSQL_SUPPORTED_COLLATIONS: &[Collation] =
    &[Collation::Binary, Collation::Utf8Binary, Collation::Utf8GeneralCi];

#[allow(dead_code)]
const SQLITE_SUPPORTED_COLLATIONS: &[Collation] =
    &[Collation::Binary, Collation::NoCase, Collation::Rtrim];

impl StringBehavior for SqlMode {
    fn default_string_comparison_case_sensitive(&self) -> bool {
        match self {
            SqlMode::MySQL { .. } => false, // MySQL defaults to case-insensitive
            SqlMode::SQLite => true,        // SQLite defaults to case-sensitive
        }
    }

    fn default_collation(&self) -> Collation {
        match self {
            SqlMode::MySQL { .. } => Collation::Utf8GeneralCi, // MySQL's default collation
            SqlMode::SQLite => Collation::Binary,              // SQLite's default collation
        }
    }

    fn supported_collations(&self) -> &[Collation] {
        match self {
            SqlMode::MySQL { .. } => MYSQL_SUPPORTED_COLLATIONS,
            SqlMode::SQLite => SQLITE_SUPPORTED_COLLATIONS,
        }
    }
}

impl OperatorBehavior for SqlMode {
    fn integer_division_behavior(&self) -> DivisionBehavior {
        match self {
            SqlMode::MySQL { .. } => DivisionBehavior::Decimal,
            SqlMode::SQLite => DivisionBehavior::Integer,
        }
    }

    fn supports_xor(&self) -> bool {
        match self {
            SqlMode::MySQL { .. } => true,
            SqlMode::SQLite => false,
        }
    }

    fn supports_integer_div_operator(&self) -> bool {
        match self {
            SqlMode::MySQL { .. } => true,
            SqlMode::SQLite => false,
        }
    }

    fn string_concat_operator(&self) -> ConcatOperator {
        match self {
            SqlMode::MySQL { .. } => ConcatOperator::Function,
            SqlMode::SQLite => ConcatOperator::PipePipe,
        }
    }
}

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

    #[test]
    fn test_default_is_mysql() {
        // Default is MySQL for SQLLogicTest compatibility (MySQL 8.x test suite)
        let mode = SqlMode::default();
        assert!(matches!(mode, SqlMode::MySQL { .. }));
    }

    #[test]
    fn test_mysql_mode_has_default_flags() {
        // Test that MySQL mode can be constructed with default flags
        let mode = SqlMode::MySQL { flags: MySqlModeFlags::default() };
        if let SqlMode::MySQL { flags } = mode {
            assert_eq!(flags, MySqlModeFlags::default());
        } else {
            panic!("Expected MySQL mode");
        }
    }

    #[test]
    fn test_division_behavior() {
        use crate::{
            sql_mode::types::{TypeBehavior, ValueType},
            SqlValue,
        };

        let mysql_mode = SqlMode::MySQL { flags: MySqlModeFlags::default() };
        // MySQL returns Numeric for division
        assert_eq!(
            mysql_mode.division_result_type(&SqlValue::Integer(5), &SqlValue::Integer(2)),
            ValueType::Numeric
        );

        let sqlite_mode = SqlMode::SQLite;
        // SQLite returns Integer for int/int division
        assert_eq!(
            sqlite_mode.division_result_type(&SqlValue::Integer(5), &SqlValue::Integer(2)),
            ValueType::Integer
        );
    }

    #[test]
    fn test_mysql_flags_accessor() {
        let mysql_mode = SqlMode::MySQL { flags: MySqlModeFlags::with_pipes_as_concat() };
        assert!(mysql_mode.mysql_flags().is_some());
        assert!(mysql_mode.mysql_flags().unwrap().pipes_as_concat);

        let sqlite_mode = SqlMode::SQLite;
        assert!(sqlite_mode.mysql_flags().is_none());
    }

    #[test]
    fn test_sqlmode_with_flags() {
        let mode = SqlMode::MySQL {
            flags: MySqlModeFlags { pipes_as_concat: true, ..Default::default() },
        };

        match mode {
            SqlMode::MySQL { flags } => {
                assert!(flags.pipes_as_concat);
                assert!(!flags.ansi_quotes);
                assert!(!flags.strict_mode);
            }
            _ => panic!("Expected MySQL mode"),
        }
    }

    #[test]
    fn test_sqlmode_with_custom_flags() {
        let mode = SqlMode::MySQL { flags: MySqlModeFlags::ansi() };

        match mode {
            SqlMode::MySQL { flags } => {
                assert!(flags.pipes_as_concat);
                assert!(flags.ansi_quotes);
                assert!(!flags.strict_mode);
            }
            _ => panic!("Expected MySQL mode"),
        }
    }

    #[test]
    fn test_mysql_string_comparison() {
        let mode = SqlMode::MySQL { flags: MySqlModeFlags::default() };
        assert!(!mode.default_string_comparison_case_sensitive());
        assert_eq!(mode.default_collation(), Collation::Utf8GeneralCi);
    }

    #[test]
    fn test_sqlite_string_comparison() {
        let mode = SqlMode::SQLite;
        assert!(mode.default_string_comparison_case_sensitive());
        assert_eq!(mode.default_collation(), Collation::Binary);
    }

    #[test]
    fn test_mysql_supported_collations() {
        let mode = SqlMode::MySQL { flags: MySqlModeFlags::default() };
        let collations = mode.supported_collations();
        assert_eq!(collations.len(), 3);
        assert!(collations.contains(&Collation::Binary));
        assert!(collations.contains(&Collation::Utf8Binary));
        assert!(collations.contains(&Collation::Utf8GeneralCi));
    }

    #[test]
    fn test_sqlite_supported_collations() {
        let mode = SqlMode::SQLite;
        let collations = mode.supported_collations();
        assert_eq!(collations.len(), 3);
        assert!(collations.contains(&Collation::Binary));
        assert!(collations.contains(&Collation::NoCase));
        assert!(collations.contains(&Collation::Rtrim));
    }

    #[test]
    fn test_collation_case_sensitivity_consistency() {
        // MySQL default collation should be case-insensitive
        let mysql_mode = SqlMode::MySQL { flags: MySqlModeFlags::default() };
        assert!(!mysql_mode.default_string_comparison_case_sensitive());
        assert_eq!(mysql_mode.default_collation(), Collation::Utf8GeneralCi);

        // SQLite default collation should be case-sensitive
        let sqlite_mode = SqlMode::SQLite;
        assert!(sqlite_mode.default_string_comparison_case_sensitive());
        assert_eq!(sqlite_mode.default_collation(), Collation::Binary);
    }

    // Tests for OperatorBehavior trait implementation

    #[test]
    fn test_integer_division_behavior() {
        let mysql_mode = SqlMode::MySQL { flags: MySqlModeFlags::default() };
        assert_eq!(mysql_mode.integer_division_behavior(), DivisionBehavior::Decimal);

        let sqlite_mode = SqlMode::SQLite;
        assert_eq!(sqlite_mode.integer_division_behavior(), DivisionBehavior::Integer);
    }

    #[test]
    fn test_xor_support() {
        let mysql_mode = SqlMode::MySQL { flags: MySqlModeFlags::default() };
        assert!(mysql_mode.supports_xor());

        let sqlite_mode = SqlMode::SQLite;
        assert!(!sqlite_mode.supports_xor());
    }

    #[test]
    fn test_integer_div_operator_support() {
        let mysql_mode = SqlMode::MySQL { flags: MySqlModeFlags::default() };
        assert!(mysql_mode.supports_integer_div_operator());

        let sqlite_mode = SqlMode::SQLite;
        assert!(!sqlite_mode.supports_integer_div_operator());
    }

    #[test]
    fn test_string_concat_operator() {
        let mysql_mode = SqlMode::MySQL { flags: MySqlModeFlags::default() };
        assert_eq!(mysql_mode.string_concat_operator(), ConcatOperator::Function);

        let sqlite_mode = SqlMode::SQLite;
        assert_eq!(sqlite_mode.string_concat_operator(), ConcatOperator::PipePipe);
    }

    // Tests for FromStr and Display implementations

    #[test]
    fn test_display_mysql() {
        let mode = SqlMode::MySQL { flags: MySqlModeFlags::default() };
        assert_eq!(mode.to_string(), "mysql");
    }

    #[test]
    fn test_display_sqlite() {
        let mode = SqlMode::SQLite;
        assert_eq!(mode.to_string(), "sqlite");
    }

    #[test]
    fn test_from_str_mysql() {
        let mode: SqlMode = "mysql".parse().unwrap();
        assert!(matches!(mode, SqlMode::MySQL { .. }));
    }

    #[test]
    fn test_from_str_sqlite() {
        let mode: SqlMode = "sqlite".parse().unwrap();
        assert!(matches!(mode, SqlMode::SQLite));
    }

    #[test]
    fn test_from_str_case_insensitive() {
        let mode1: SqlMode = "MYSQL".parse().unwrap();
        assert!(matches!(mode1, SqlMode::MySQL { .. }));

        let mode2: SqlMode = "SQLite".parse().unwrap();
        assert!(matches!(mode2, SqlMode::SQLite));

        let mode3: SqlMode = "MySql".parse().unwrap();
        assert!(matches!(mode3, SqlMode::MySQL { .. }));
    }

    #[test]
    fn test_from_str_mysql_slt() {
        // mysql_slt mode: MySQL syntax with SQLite division semantics
        let mode: SqlMode = "mysql_slt".parse().unwrap();
        match mode {
            SqlMode::MySQL { flags } => {
                assert!(flags.sqlite_division_semantics);
                assert!(!flags.pipes_as_concat);
                assert!(!flags.ansi_quotes);
                assert!(!flags.strict_mode);
            }
            _ => panic!("Expected MySQL mode"),
        }
    }

    #[test]
    fn test_from_str_invalid() {
        // Invalid patterns with special characters should still error
        let result: Result<SqlMode, _> = "invalid!@#".parse();
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(err.contains("Unknown SQL mode"));
    }

    // Tests for MySQL mode flags parsing (issue #3074)

    #[test]
    fn test_from_str_mysql_mode_flags() {
        // Standard MySQL 8.0 default mode string
        let mode: SqlMode =
            "ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION"
                .parse()
                .unwrap();
        match mode {
            SqlMode::MySQL { flags } => {
                assert!(flags.strict_mode);
                assert!(!flags.pipes_as_concat);
                assert!(!flags.ansi_quotes);
            }
            _ => panic!("Expected MySQL mode"),
        }
    }

    #[test]
    fn test_from_str_leading_comma() {
        // Leading comma (from REPLACE() removing first mode like ONLY_FULL_GROUP_BY)
        let mode: SqlMode =
            ",STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION"
                .parse()
                .unwrap();
        match mode {
            SqlMode::MySQL { flags } => {
                assert!(flags.strict_mode);
            }
            _ => panic!("Expected MySQL mode"),
        }
    }

    #[test]
    fn test_from_str_trailing_comma() {
        // Trailing comma
        let mode: SqlMode = "STRICT_TRANS_TABLES,".parse().unwrap();
        match mode {
            SqlMode::MySQL { flags } => {
                assert!(flags.strict_mode);
            }
            _ => panic!("Expected MySQL mode"),
        }
    }

    #[test]
    fn test_from_str_empty_segments() {
        // Empty segments between commas
        let mode: SqlMode = "STRICT_TRANS_TABLES,,PIPES_AS_CONCAT".parse().unwrap();
        match mode {
            SqlMode::MySQL { flags } => {
                assert!(flags.strict_mode);
                assert!(flags.pipes_as_concat);
            }
            _ => panic!("Expected MySQL mode"),
        }
    }

    #[test]
    fn test_from_str_empty_string() {
        // Empty string should result in default flags
        let mode: SqlMode = "".parse().unwrap();
        match mode {
            SqlMode::MySQL { flags } => {
                assert!(!flags.strict_mode);
                assert!(!flags.pipes_as_concat);
                assert!(!flags.ansi_quotes);
            }
            _ => panic!("Expected MySQL mode"),
        }
    }

    #[test]
    fn test_from_str_only_commas() {
        // Only commas should result in default flags
        let mode: SqlMode = ",,,".parse().unwrap();
        match mode {
            SqlMode::MySQL { flags } => {
                assert!(!flags.strict_mode);
                assert!(!flags.pipes_as_concat);
                assert!(!flags.ansi_quotes);
            }
            _ => panic!("Expected MySQL mode"),
        }
    }

    #[test]
    fn test_from_str_ansi_mode() {
        // ANSI mode should set both pipes_as_concat and ansi_quotes
        let mode: SqlMode = "ANSI".parse().unwrap();
        match mode {
            SqlMode::MySQL { flags } => {
                assert!(flags.pipes_as_concat);
                assert!(flags.ansi_quotes);
            }
            _ => panic!("Expected MySQL mode"),
        }
    }

    #[test]
    fn test_from_str_pipes_as_concat() {
        let mode: SqlMode = "PIPES_AS_CONCAT".parse().unwrap();
        match mode {
            SqlMode::MySQL { flags } => {
                assert!(flags.pipes_as_concat);
                assert!(!flags.ansi_quotes);
            }
            _ => panic!("Expected MySQL mode"),
        }
    }

    #[test]
    fn test_from_str_ansi_quotes() {
        let mode: SqlMode = "ANSI_QUOTES".parse().unwrap();
        match mode {
            SqlMode::MySQL { flags } => {
                assert!(!flags.pipes_as_concat);
                assert!(flags.ansi_quotes);
            }
            _ => panic!("Expected MySQL mode"),
        }
    }

    #[test]
    fn test_from_str_mode_flags_case_insensitive() {
        // Mode flags should be case-insensitive
        let mode: SqlMode = "strict_trans_tables,pipes_as_concat".parse().unwrap();
        match mode {
            SqlMode::MySQL { flags } => {
                assert!(flags.strict_mode);
                assert!(flags.pipes_as_concat);
            }
            _ => panic!("Expected MySQL mode"),
        }
    }

    #[test]
    fn test_from_str_mode_flags_with_whitespace() {
        // Whitespace around modes should be trimmed
        let mode: SqlMode = " STRICT_TRANS_TABLES , PIPES_AS_CONCAT ".parse().unwrap();
        match mode {
            SqlMode::MySQL { flags } => {
                assert!(flags.strict_mode);
                assert!(flags.pipes_as_concat);
            }
            _ => panic!("Expected MySQL mode"),
        }
    }

    #[test]
    fn test_from_str_unknown_mode_accepted() {
        // Unknown MySQL-like modes should be accepted silently (forward compatibility)
        let mode: SqlMode = "SOME_FUTURE_MODE,STRICT_TRANS_TABLES".parse().unwrap();
        match mode {
            SqlMode::MySQL { flags } => {
                assert!(flags.strict_mode);
            }
            _ => panic!("Expected MySQL mode"),
        }
    }

    #[test]
    fn test_roundtrip() {
        // MySQL roundtrip
        let mysql = SqlMode::MySQL { flags: MySqlModeFlags::default() };
        let mysql_str = mysql.to_string();
        let mysql_parsed: SqlMode = mysql_str.parse().unwrap();
        assert!(matches!(mysql_parsed, SqlMode::MySQL { .. }));

        // SQLite roundtrip
        let sqlite = SqlMode::SQLite;
        let sqlite_str = sqlite.to_string();
        let sqlite_parsed: SqlMode = sqlite_str.parse().unwrap();
        assert!(matches!(sqlite_parsed, SqlMode::SQLite));
    }
}