mssql-client 0.10.0

High-level async SQL Server client with type-state connection management
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
//! SQL Server Change Tracking support.
//!
//! This module provides helper types and utilities for working with SQL Server's
//! built-in Change Tracking feature, which enables efficient incremental data
//! synchronization scenarios.
//!
//! ## Overview
//!
//! SQL Server Change Tracking automatically tracks changes (inserts, updates,
//! deletes) to table rows. Applications can query for changes since a specific
//! version to implement incremental sync patterns.
//!
//! ## Usage
//!
//! ```rust,ignore
//! use mssql_client::change_tracking::{ChangeOperation, ChangeTrackingQuery};
//!
//! // Get current version for baseline
//! let current_version: i64 = client
//!     .query("SELECT CHANGE_TRACKING_CURRENT_VERSION()")
//!     .await?
//!     .first()
//!     .and_then(|r| r.try_get(0))
//!     .unwrap_or(0);
//!
//! // Later, query for changes since that version
//! let query = ChangeTrackingQuery::changes("Products", last_sync_version);
//! let changes: Vec<ChangedRow> = client.query(&query.to_sql()).await?
//!     .map(|row| ChangedRow::from_row(&row))
//!     .collect();
//!
//! for change in changes {
//!     match change.operation {
//!         ChangeOperation::Insert => println!("New row: {:?}", change.primary_key),
//!         ChangeOperation::Update => println!("Updated row: {:?}", change.primary_key),
//!         ChangeOperation::Delete => println!("Deleted row: {:?}", change.primary_key),
//!     }
//! }
//! ```
//!
//! ## Prerequisites
//!
//! Change Tracking must be enabled on the database and table:
//!
//! ```sql
//! -- Enable on database
//! ALTER DATABASE MyDB SET CHANGE_TRACKING = ON
//!     (CHANGE_RETENTION = 2 DAYS, AUTO_CLEANUP = ON);
//!
//! -- Enable on table
//! ALTER TABLE Products ENABLE CHANGE_TRACKING
//!     WITH (TRACK_COLUMNS_UPDATED = ON);
//! ```
//!
//! ## Key Concepts
//!
//! - **Version**: A monotonically increasing value representing a point in time
//! - **SYS_CHANGE_OPERATION**: I (Insert), U (Update), D (Delete)
//! - **SYS_CHANGE_VERSION**: The version when the row was last changed
//! - **SYS_CHANGE_CREATION_VERSION**: The version when the row was inserted
//!
//! ## References
//!
//! - [About Change Tracking](https://learn.microsoft.com/en-us/sql/relational-databases/track-changes/about-change-tracking-sql-server)
//! - [CHANGETABLE function](https://learn.microsoft.com/en-us/sql/relational-databases/system-functions/changetable-transact-sql)

use std::fmt;

use bytes::Bytes;

/// The type of change operation tracked by SQL Server Change Tracking.
///
/// This corresponds to the `SYS_CHANGE_OPERATION` column in `CHANGETABLE` results.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ChangeOperation {
    /// A new row was inserted (I).
    Insert,
    /// An existing row was updated (U).
    Update,
    /// A row was deleted (D).
    Delete,
}

impl ChangeOperation {
    /// Parse a change operation from its single-character SQL Server representation.
    ///
    /// # Arguments
    ///
    /// * `s` - A string containing 'I', 'U', or 'D'
    ///
    /// # Returns
    ///
    /// The parsed `ChangeOperation`, or `None` if the input is invalid.
    #[must_use]
    pub fn from_sql(s: &str) -> Option<Self> {
        match s.trim().to_uppercase().as_str() {
            "I" => Some(Self::Insert),
            "U" => Some(Self::Update),
            "D" => Some(Self::Delete),
            _ => None,
        }
    }

    /// Get the SQL Server single-character representation.
    #[must_use]
    pub const fn as_sql(&self) -> &'static str {
        match self {
            Self::Insert => "I",
            Self::Update => "U",
            Self::Delete => "D",
        }
    }

    /// Check if this is an insert operation.
    #[must_use]
    pub const fn is_insert(&self) -> bool {
        matches!(self, Self::Insert)
    }

    /// Check if this is an update operation.
    #[must_use]
    pub const fn is_update(&self) -> bool {
        matches!(self, Self::Update)
    }

    /// Check if this is a delete operation.
    #[must_use]
    pub const fn is_delete(&self) -> bool {
        matches!(self, Self::Delete)
    }
}

impl fmt::Display for ChangeOperation {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Insert => write!(f, "INSERT"),
            Self::Update => write!(f, "UPDATE"),
            Self::Delete => write!(f, "DELETE"),
        }
    }
}

/// Metadata from a Change Tracking query result.
///
/// Contains the system columns returned by `CHANGETABLE(CHANGES ...)`.
#[derive(Debug, Clone)]
#[non_exhaustive]
pub struct ChangeMetadata {
    /// The version when the row was last changed.
    pub version: i64,
    /// The version when the row was created (inserted).
    /// This is `None` for delete operations.
    pub creation_version: Option<i64>,
    /// The type of change (Insert, Update, Delete).
    pub operation: ChangeOperation,
    /// Binary mask of changed columns.
    /// Use `CHANGE_TRACKING_IS_COLUMN_IN_MASK()` to interpret.
    pub changed_columns: Option<Bytes>,
    /// Application-defined change context.
    pub context: Option<Bytes>,
}

impl ChangeMetadata {
    /// Create new change metadata.
    #[must_use]
    pub fn new(
        version: i64,
        creation_version: Option<i64>,
        operation: ChangeOperation,
        changed_columns: Option<Bytes>,
        context: Option<Bytes>,
    ) -> Self {
        Self {
            version,
            creation_version,
            operation,
            changed_columns,
            context,
        }
    }

    /// Create metadata for an insert operation.
    #[must_use]
    pub fn insert(version: i64) -> Self {
        Self {
            version,
            creation_version: Some(version),
            operation: ChangeOperation::Insert,
            changed_columns: None,
            context: None,
        }
    }

    /// Create metadata for an update operation.
    #[must_use]
    pub fn update(version: i64, creation_version: i64) -> Self {
        Self {
            version,
            creation_version: Some(creation_version),
            operation: ChangeOperation::Update,
            changed_columns: None,
            context: None,
        }
    }

    /// Create metadata for a delete operation.
    #[must_use]
    pub fn delete(version: i64) -> Self {
        Self {
            version,
            creation_version: None,
            operation: ChangeOperation::Delete,
            changed_columns: None,
            context: None,
        }
    }
}

/// Query builder for Change Tracking operations.
///
/// Helps construct proper SQL queries for common Change Tracking patterns.
///
/// # Example
///
/// ```rust
/// use mssql_client::change_tracking::ChangeTrackingQuery;
///
/// // Query for all changes since version 42
/// let query = ChangeTrackingQuery::changes("Products", 42);
/// assert!(query.to_sql().contains("CHANGETABLE"));
///
/// // Query with specific columns
/// let query = ChangeTrackingQuery::changes("Orders", 100)
///     .with_columns(&["OrderId", "CustomerId", "OrderDate"]);
/// let sql = query.to_sql();
/// assert!(sql.contains("OrderId"));
/// ```
#[derive(Debug, Clone)]
pub struct ChangeTrackingQuery {
    table_name: String,
    last_sync_version: i64,
    columns: Option<Vec<String>>,
    primary_keys: Option<Vec<String>>,
    alias: String,
    force_seek: bool,
}

impl ChangeTrackingQuery {
    /// Create a query for changes to a table since a specific version.
    ///
    /// This generates a `CHANGETABLE(CHANGES table_name, last_sync_version)` query.
    ///
    /// # Arguments
    ///
    /// * `table_name` - The name of the table to query changes for
    /// * `last_sync_version` - The version from the previous sync (0 for initial)
    ///
    /// # Example
    ///
    /// ```rust
    /// use mssql_client::change_tracking::ChangeTrackingQuery;
    ///
    /// let query = ChangeTrackingQuery::changes("Products", 42);
    /// ```
    #[must_use]
    pub fn changes(table_name: impl Into<String>, last_sync_version: i64) -> Self {
        Self {
            table_name: table_name.into(),
            last_sync_version,
            columns: None,
            primary_keys: None,
            alias: "CT".into(),
            force_seek: false,
        }
    }

    /// Specify which data columns to include (in addition to change tracking columns).
    ///
    /// If not specified, only change tracking system columns are returned.
    ///
    /// # Arguments
    ///
    /// * `columns` - Column names to include in the result
    #[must_use]
    pub fn with_columns(mut self, columns: &[&str]) -> Self {
        self.columns = Some(columns.iter().map(|&s| s.to_string()).collect());
        self
    }

    /// Specify the primary key columns for the table.
    ///
    /// This is needed when you want to join change tracking results
    /// with the original table to get current row data.
    #[must_use]
    pub fn with_primary_keys(mut self, keys: &[&str]) -> Self {
        self.primary_keys = Some(keys.iter().map(|&s| s.to_string()).collect());
        self
    }

    /// Set the table alias for the CHANGETABLE result.
    #[must_use]
    pub fn with_alias(mut self, alias: impl Into<String>) -> Self {
        self.alias = alias.into();
        self
    }

    /// Enable FORCESEEK hint for the query.
    ///
    /// This can improve performance in some scenarios.
    #[must_use]
    pub fn with_force_seek(mut self) -> Self {
        self.force_seek = true;
        self
    }

    /// Generate the SQL query string.
    ///
    /// This returns a query that can be executed directly.
    #[must_use]
    pub fn to_sql(&self) -> String {
        let force_seek = if self.force_seek { ", FORCESEEK" } else { "" };

        // Build the SELECT column list
        let select_cols = self.build_select_columns();

        format!(
            "SELECT {} FROM CHANGETABLE(CHANGES {}, {}{})",
            select_cols, self.table_name, self.last_sync_version, force_seek
        )
    }

    /// Generate a SQL query that joins with the original table.
    ///
    /// This is useful when you need both the change tracking metadata
    /// and the current row data (for inserts and updates).
    ///
    /// # Arguments
    ///
    /// * `data_columns` - Columns from the data table to include
    ///
    /// # Example
    ///
    /// ```rust
    /// use mssql_client::change_tracking::ChangeTrackingQuery;
    ///
    /// let query = ChangeTrackingQuery::changes("Products", 42)
    ///     .with_primary_keys(&["ProductId"]);
    /// let sql = query.to_sql_with_data(&["Name", "Price", "Stock"]);
    /// assert!(sql.contains("LEFT OUTER JOIN"));
    /// ```
    #[must_use]
    pub fn to_sql_with_data(&self, data_columns: &[&str]) -> String {
        let force_seek = if self.force_seek { ", FORCESEEK" } else { "" };
        let alias = &self.alias;

        // Build change tracking columns
        let ct_cols = format!(
            "{alias}.SYS_CHANGE_VERSION, {alias}.SYS_CHANGE_CREATION_VERSION, \
             {alias}.SYS_CHANGE_OPERATION, {alias}.SYS_CHANGE_COLUMNS, {alias}.SYS_CHANGE_CONTEXT"
        );

        // Build data columns (prefixed with table alias)
        let data_cols: String = data_columns
            .iter()
            .map(|c| format!("T.{c}"))
            .collect::<Vec<_>>()
            .join(", ");

        // Build primary key columns from change tracking
        let pk_cols: String = self
            .primary_keys
            .as_ref()
            .map(|pks| {
                pks.iter()
                    .map(|pk| format!("{alias}.{pk}"))
                    .collect::<Vec<_>>()
                    .join(", ")
            })
            .unwrap_or_default();

        // Build join condition
        let join_condition: String = self
            .primary_keys
            .as_ref()
            .map(|pks| {
                pks.iter()
                    .map(|pk| format!("{alias}.{pk} = T.{pk}"))
                    .collect::<Vec<_>>()
                    .join(" AND ")
            })
            .unwrap_or_else(|| "1=1".into());

        let select_cols = if pk_cols.is_empty() {
            format!("{ct_cols}, {data_cols}")
        } else {
            format!("{ct_cols}, {pk_cols}, {data_cols}")
        };

        format!(
            "SELECT {select_cols} \
             FROM CHANGETABLE(CHANGES {table}, {version}{force_seek}) AS {alias} \
             LEFT OUTER JOIN {table} AS T ON {join_condition}",
            table = self.table_name,
            version = self.last_sync_version,
        )
    }

    fn build_select_columns(&self) -> String {
        let alias = &self.alias;

        // Always include change tracking system columns
        let mut cols = vec![
            format!("{alias}.SYS_CHANGE_VERSION"),
            format!("{alias}.SYS_CHANGE_CREATION_VERSION"),
            format!("{alias}.SYS_CHANGE_OPERATION"),
            format!("{alias}.SYS_CHANGE_COLUMNS"),
            format!("{alias}.SYS_CHANGE_CONTEXT"),
        ];

        // Add primary key columns if specified
        if let Some(ref pks) = self.primary_keys {
            for pk in pks {
                cols.push(format!("{alias}.{pk}"));
            }
        }

        // Add data columns if specified
        if let Some(ref data_cols) = self.columns {
            for col in data_cols {
                cols.push(format!("{alias}.{col}"));
            }
        }

        cols.join(", ")
    }
}

/// Helper functions for Change Tracking operations.
pub struct ChangeTracking;

impl ChangeTracking {
    /// Generate SQL to get the current change tracking version.
    ///
    /// Returns the global change tracking version number.
    ///
    /// # Example
    ///
    /// ```rust
    /// use mssql_client::change_tracking::ChangeTracking;
    ///
    /// let sql = ChangeTracking::current_version_sql();
    /// assert_eq!(sql, "SELECT CHANGE_TRACKING_CURRENT_VERSION()");
    /// ```
    #[must_use]
    pub const fn current_version_sql() -> &'static str {
        "SELECT CHANGE_TRACKING_CURRENT_VERSION()"
    }

    /// Generate SQL to get the minimum valid version for a table.
    ///
    /// If a client's last sync version is less than this, it must
    /// perform a full re-sync instead of incremental sync.
    ///
    /// # Arguments
    ///
    /// * `table_name` - The name of the table
    ///
    /// # Example
    ///
    /// ```rust
    /// use mssql_client::change_tracking::ChangeTracking;
    ///
    /// let sql = ChangeTracking::min_valid_version_sql("Products");
    /// assert!(sql.contains("CHANGE_TRACKING_MIN_VALID_VERSION"));
    /// ```
    #[must_use]
    pub fn min_valid_version_sql(table_name: &str) -> String {
        format!("SELECT CHANGE_TRACKING_MIN_VALID_VERSION(OBJECT_ID(N'{table_name}'))")
    }

    /// Generate SQL to check if a column is in a change mask.
    ///
    /// Used to determine which specific columns changed in an update operation.
    ///
    /// # Arguments
    ///
    /// * `table_name` - The table name
    /// * `column_name` - The column to check
    /// * `mask_variable` - The name of the variable holding the change mask
    ///
    /// # Example
    ///
    /// ```rust
    /// use mssql_client::change_tracking::ChangeTracking;
    ///
    /// let sql = ChangeTracking::column_in_mask_sql("Products", "Price", "@mask");
    /// assert!(sql.contains("CHANGE_TRACKING_IS_COLUMN_IN_MASK"));
    /// ```
    #[must_use]
    pub fn column_in_mask_sql(table_name: &str, column_name: &str, mask_variable: &str) -> String {
        format!(
            "SELECT CHANGE_TRACKING_IS_COLUMN_IN_MASK(\
             COLUMNPROPERTY(OBJECT_ID(N'{table_name}'), N'{column_name}', 'ColumnId'), \
             {mask_variable})"
        )
    }

    /// Generate SQL to enable change tracking on a database.
    ///
    /// # Arguments
    ///
    /// * `database_name` - The database name
    /// * `retention_days` - How long to retain change data
    /// * `auto_cleanup` - Whether to automatically clean up old data
    ///
    /// # Example
    ///
    /// ```rust
    /// use mssql_client::change_tracking::ChangeTracking;
    ///
    /// let sql = ChangeTracking::enable_database_sql("MyDB", 2, true);
    /// assert!(sql.contains("SET CHANGE_TRACKING = ON"));
    /// ```
    #[must_use]
    pub fn enable_database_sql(
        database_name: &str,
        retention_days: u32,
        auto_cleanup: bool,
    ) -> String {
        let cleanup = if auto_cleanup { "ON" } else { "OFF" };
        format!(
            "ALTER DATABASE [{database_name}] SET CHANGE_TRACKING = ON \
             (CHANGE_RETENTION = {retention_days} DAYS, AUTO_CLEANUP = {cleanup})"
        )
    }

    /// Generate SQL to enable change tracking on a table.
    ///
    /// # Arguments
    ///
    /// * `table_name` - The table name
    /// * `track_columns_updated` - Whether to track which columns were updated
    ///
    /// # Example
    ///
    /// ```rust
    /// use mssql_client::change_tracking::ChangeTracking;
    ///
    /// let sql = ChangeTracking::enable_table_sql("Products", true);
    /// assert!(sql.contains("ENABLE CHANGE_TRACKING"));
    /// ```
    #[must_use]
    pub fn enable_table_sql(table_name: &str, track_columns_updated: bool) -> String {
        let track_cols = if track_columns_updated { "ON" } else { "OFF" };
        format!(
            "ALTER TABLE [{table_name}] ENABLE CHANGE_TRACKING \
             WITH (TRACK_COLUMNS_UPDATED = {track_cols})"
        )
    }

    /// Generate SQL to disable change tracking on a table.
    #[must_use]
    pub fn disable_table_sql(table_name: &str) -> String {
        format!("ALTER TABLE [{table_name}] DISABLE CHANGE_TRACKING")
    }

    /// Generate SQL to disable change tracking on a database.
    #[must_use]
    pub fn disable_database_sql(database_name: &str) -> String {
        format!("ALTER DATABASE [{database_name}] SET CHANGE_TRACKING = OFF")
    }
}

/// Result of checking if a sync version is still valid.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum SyncVersionStatus {
    /// The sync version is valid and incremental sync can proceed.
    Valid,
    /// The sync version is too old; a full re-sync is required.
    TooOld,
    /// Change tracking is not enabled or the table doesn't exist.
    NotEnabled,
}

impl SyncVersionStatus {
    /// Check sync version validity from the min_valid_version result.
    ///
    /// # Arguments
    ///
    /// * `last_sync_version` - The client's last synchronized version
    /// * `min_valid_version` - Result from `CHANGE_TRACKING_MIN_VALID_VERSION()`
    ///
    /// # Returns
    ///
    /// The sync status indicating whether incremental sync is possible.
    #[must_use]
    pub fn check(last_sync_version: i64, min_valid_version: Option<i64>) -> Self {
        match min_valid_version {
            None => Self::NotEnabled,
            Some(min) if last_sync_version >= min => Self::Valid,
            Some(_) => Self::TooOld,
        }
    }

    /// Check if incremental sync is possible.
    #[must_use]
    pub const fn can_sync_incrementally(&self) -> bool {
        matches!(self, Self::Valid)
    }

    /// Check if a full re-sync is required.
    #[must_use]
    pub const fn requires_full_sync(&self) -> bool {
        matches!(self, Self::TooOld)
    }
}

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

    #[test]
    fn test_change_operation_from_sql() {
        assert_eq!(
            ChangeOperation::from_sql("I"),
            Some(ChangeOperation::Insert)
        );
        assert_eq!(
            ChangeOperation::from_sql("U"),
            Some(ChangeOperation::Update)
        );
        assert_eq!(
            ChangeOperation::from_sql("D"),
            Some(ChangeOperation::Delete)
        );
        assert_eq!(
            ChangeOperation::from_sql("i"),
            Some(ChangeOperation::Insert)
        );
        assert_eq!(
            ChangeOperation::from_sql(" U "),
            Some(ChangeOperation::Update)
        );
        assert_eq!(ChangeOperation::from_sql("X"), None);
        assert_eq!(ChangeOperation::from_sql(""), None);
    }

    #[test]
    fn test_change_operation_as_sql() {
        assert_eq!(ChangeOperation::Insert.as_sql(), "I");
        assert_eq!(ChangeOperation::Update.as_sql(), "U");
        assert_eq!(ChangeOperation::Delete.as_sql(), "D");
    }

    #[test]
    fn test_change_operation_predicates() {
        assert!(ChangeOperation::Insert.is_insert());
        assert!(!ChangeOperation::Insert.is_update());
        assert!(!ChangeOperation::Insert.is_delete());

        assert!(!ChangeOperation::Update.is_insert());
        assert!(ChangeOperation::Update.is_update());
        assert!(!ChangeOperation::Update.is_delete());

        assert!(!ChangeOperation::Delete.is_insert());
        assert!(!ChangeOperation::Delete.is_update());
        assert!(ChangeOperation::Delete.is_delete());
    }

    #[test]
    fn test_change_metadata_constructors() {
        let insert = ChangeMetadata::insert(42);
        assert_eq!(insert.version, 42);
        assert_eq!(insert.creation_version, Some(42));
        assert_eq!(insert.operation, ChangeOperation::Insert);

        let update = ChangeMetadata::update(50, 42);
        assert_eq!(update.version, 50);
        assert_eq!(update.creation_version, Some(42));
        assert_eq!(update.operation, ChangeOperation::Update);

        let delete = ChangeMetadata::delete(60);
        assert_eq!(delete.version, 60);
        assert_eq!(delete.creation_version, None);
        assert_eq!(delete.operation, ChangeOperation::Delete);
    }

    #[test]
    fn test_change_tracking_query_basic() {
        let query = ChangeTrackingQuery::changes("Products", 42);
        let sql = query.to_sql();

        assert!(sql.contains("CHANGETABLE(CHANGES Products, 42)"));
        assert!(sql.contains("SYS_CHANGE_VERSION"));
        assert!(sql.contains("SYS_CHANGE_OPERATION"));
    }

    #[test]
    fn test_change_tracking_query_with_columns() {
        let query = ChangeTrackingQuery::changes("Products", 42).with_columns(&["Name", "Price"]);
        let sql = query.to_sql();

        assert!(sql.contains("CT.Name"));
        assert!(sql.contains("CT.Price"));
    }

    #[test]
    fn test_change_tracking_query_with_primary_keys() {
        let query = ChangeTrackingQuery::changes("Products", 42).with_primary_keys(&["ProductId"]);
        let sql = query.to_sql();

        assert!(sql.contains("CT.ProductId"));
    }

    #[test]
    fn test_change_tracking_query_force_seek() {
        let query = ChangeTrackingQuery::changes("Products", 42).with_force_seek();
        let sql = query.to_sql();

        assert!(sql.contains("FORCESEEK"));
    }

    #[test]
    fn test_change_tracking_query_with_data() {
        let query = ChangeTrackingQuery::changes("Products", 42).with_primary_keys(&["ProductId"]);
        let sql = query.to_sql_with_data(&["Name", "Price"]);

        assert!(sql.contains("LEFT OUTER JOIN Products AS T"));
        assert!(sql.contains("CT.ProductId = T.ProductId"));
        assert!(sql.contains("T.Name"));
        assert!(sql.contains("T.Price"));
    }

    #[test]
    fn test_change_tracking_helper_sql() {
        assert_eq!(
            ChangeTracking::current_version_sql(),
            "SELECT CHANGE_TRACKING_CURRENT_VERSION()"
        );

        let min_sql = ChangeTracking::min_valid_version_sql("Products");
        assert!(min_sql.contains("CHANGE_TRACKING_MIN_VALID_VERSION"));
        assert!(min_sql.contains("Products"));

        let mask_sql = ChangeTracking::column_in_mask_sql("Products", "Price", "@mask");
        assert!(mask_sql.contains("CHANGE_TRACKING_IS_COLUMN_IN_MASK"));
        assert!(mask_sql.contains("Price"));
        assert!(mask_sql.contains("@mask"));
    }

    #[test]
    fn test_change_tracking_enable_sql() {
        let db_sql = ChangeTracking::enable_database_sql("MyDB", 7, true);
        assert!(db_sql.contains("[MyDB]"));
        assert!(db_sql.contains("7 DAYS"));
        assert!(db_sql.contains("AUTO_CLEANUP = ON"));

        let table_sql = ChangeTracking::enable_table_sql("Products", true);
        assert!(table_sql.contains("[Products]"));
        assert!(table_sql.contains("TRACK_COLUMNS_UPDATED = ON"));
    }

    #[test]
    fn test_sync_version_status() {
        // Valid case
        assert!(SyncVersionStatus::check(100, Some(50)).can_sync_incrementally());
        assert!(SyncVersionStatus::check(50, Some(50)).can_sync_incrementally());

        // Too old case
        assert!(SyncVersionStatus::check(40, Some(50)).requires_full_sync());

        // Not enabled case
        let status = SyncVersionStatus::check(100, None);
        assert_eq!(status, SyncVersionStatus::NotEnabled);
        assert!(!status.can_sync_incrementally());
    }
}