mssql-tds 0.1.0

Rust implementation of the TDS (Tabular Data Stream) protocol for SQL Server
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

use crate::core::TdsResult;
use crate::handler::handler_factory::NegotiatedSettings;
use crate::token::tokens::{EnvChangeContainer, EnvChangeToken, EnvChangeTokenSubType};
use tracing::{info, instrument};

/// Execution context tracks the state of the current connection session.
/// This includes transaction state and batch execution state.
#[derive(Debug)]
pub(crate) struct ExecutionContext {
    transaction_descriptor: u64,
    outstanding_requests: u32,
    has_open_batch: bool,
    #[cfg(test)]
    has_open_result_set: bool,
}

impl ExecutionContext {
    pub(crate) fn new() -> Self {
        Self {
            transaction_descriptor: 0,
            outstanding_requests: 1,
            has_open_batch: false,
            #[cfg(test)]
            has_open_result_set: false,
        }
    }

    pub(crate) fn get_transaction_descriptor(&self) -> u64 {
        self.transaction_descriptor
    }

    /// Returns true if a transaction is currently active.
    ///
    /// A transaction is considered active when the transaction_descriptor
    /// is non-zero, which occurs after a BEGIN TRANSACTION and before
    /// COMMIT or ROLLBACK.
    pub(crate) fn has_active_transaction(&self) -> bool {
        self.transaction_descriptor != 0
    }

    pub(crate) fn get_outstanding_requests(&self) -> u32 {
        self.outstanding_requests
    }

    #[instrument(skip(self))]
    pub fn has_open_batch(&self) -> bool {
        self.has_open_batch
    }

    #[cfg(test)]
    pub fn has_open_result_set(&self) -> bool {
        self.has_open_result_set
    }

    #[instrument(skip(self))]
    pub(crate) fn set_has_open_batch(&mut self, has_open_batch: bool) {
        self.has_open_batch = has_open_batch;
    }

    #[cfg(test)]
    #[instrument(skip(self))]
    pub(crate) fn set_has_open_result_set(&mut self, has_open_result_set: bool) {
        self.has_open_result_set = has_open_result_set;
    }

    /// Sets the transaction descriptor directly.
    ///
    /// Used by the connection-reset path to mirror the server discarding the
    /// transaction on a full RESETCONNECTION, and by tests (including downstream
    /// crates via `test-util`) to reach guards that depend on an active
    /// transaction.
    pub(crate) fn set_transaction_descriptor(&mut self, descriptor: u64) {
        self.transaction_descriptor = descriptor;
    }

    /// Applies an ENVCHANGE token to the connection session state.
    ///
    /// Transaction-descriptor changes are tracked on the execution context
    /// itself. Database / language / collation changes are written through to
    /// `negotiated_settings`, which is the authoritative store for the session
    /// values negotiated during and after login.
    pub(crate) fn capture_change_property(
        &mut self,
        change_token: &EnvChangeToken,
        negotiated_settings: &mut NegotiatedSettings,
    ) -> TdsResult<()> {
        let sub_type = change_token.sub_type;
        let change_type = &change_token.change_type;

        match &sub_type {
            EnvChangeTokenSubType::BeginTransaction
            | EnvChangeTokenSubType::CommitTransaction
            | EnvChangeTokenSubType::RollbackTransaction
            | EnvChangeTokenSubType::EnlistDtcTransaction
            | EnvChangeTokenSubType::DefectTransaction => {
                if let EnvChangeContainer::UInt64(u64_change) = change_type {
                    self.transaction_descriptor = *u64_change.new_value();
                    Ok(())
                } else {
                    Err(crate::error::Error::ProtocolError(format!(
                        "Expected UInt64 change container, but got: {change_token:?}",
                    )))
                }
            }
            EnvChangeTokenSubType::Database => {
                if let EnvChangeContainer::String(string_change) = change_type {
                    info!("Database change detected: {}", string_change.new_value());
                    negotiated_settings.database = string_change.new_value().clone();
                    Ok(())
                } else {
                    Err(crate::error::Error::ProtocolError(format!(
                        "Expected String change container, but got: {change_token:?}",
                    )))
                }
            }
            EnvChangeTokenSubType::Language => {
                if let EnvChangeContainer::String(string_change) = change_type {
                    negotiated_settings.language = string_change.new_value().clone();
                    Ok(())
                } else {
                    Err(crate::error::Error::ProtocolError(format!(
                        "Expected String change container, but got: {change_token:?}",
                    )))
                }
            }
            EnvChangeTokenSubType::SqlCollation => {
                if let EnvChangeContainer::SqlCollation(collation_change) = change_type {
                    info!("Collation change detected: {:?}", collation_change);
                    if let Some(collation) = *collation_change.new_value() {
                        negotiated_settings.database_collation = collation;
                    }
                    Ok(())
                } else {
                    Err(crate::error::Error::ProtocolError(format!(
                        "Expected Collation change container, but got: {change_token:?}",
                    )))
                }
            }
            EnvChangeTokenSubType::PacketSize => Err(crate::error::Error::ProtocolError(
                "packet_size change unexpected".to_string(),
            )),
            EnvChangeTokenSubType::CharacterSet => Err(crate::error::Error::UnimplementedFeature {
                feature: "CharacterSet environment change".to_string(),
                context: "capture_change_property".to_string(),
            }),
            EnvChangeTokenSubType::UnicodeDataSortingLocalId => {
                Err(crate::error::Error::UnimplementedFeature {
                    feature: "UnicodeDataSortingLocalId environment change".to_string(),
                    context: "capture_change_property".to_string(),
                })
            }
            EnvChangeTokenSubType::UnicodeDataSortingComparisonFlags => {
                Err(crate::error::Error::UnimplementedFeature {
                    feature: "UnicodeDataSortingComparisonFlags environment change".to_string(),
                    context: "capture_change_property".to_string(),
                })
            }
            EnvChangeTokenSubType::DatabaseMirroringPartner => {
                Err(crate::error::Error::UnimplementedFeature {
                    feature: "DatabaseMirroringPartner environment change".to_string(),
                    context: "capture_change_property".to_string(),
                })
            }
            EnvChangeTokenSubType::PromoteTransaction => {
                Err(crate::error::Error::UnimplementedFeature {
                    feature: "PromoteTransaction environment change".to_string(),
                    context: "capture_change_property".to_string(),
                })
            }
            EnvChangeTokenSubType::TransactionManagerAddress => {
                Err(crate::error::Error::UnimplementedFeature {
                    feature: "TransactionManagerAddress environment change".to_string(),
                    context: "capture_change_property".to_string(),
                })
            }
            EnvChangeTokenSubType::TransactionEnded => {
                Err(crate::error::Error::UnimplementedFeature {
                    feature: "TransactionEnded environment change".to_string(),
                    context: "capture_change_property".to_string(),
                })
            }
            EnvChangeTokenSubType::ResetConnection => {
                // Server acknowledgement that the connection was reset to login
                // defaults (in response to a RESETCONNECTION / RESETCONNECTIONSKIPTRAN
                // request). The full client-side transition — restoring the
                // negotiated database/language/collation and clearing
                // session-bound caches — is applied by
                // `TdsClient::on_reset_connection_ack`, which runs before this
                // token is captured, so nothing is done here beyond logging.
                info!("Connection reset acknowledged by server");
                Ok(())
            }
            EnvChangeTokenSubType::UserInstanceName => {
                Err(crate::error::Error::UnimplementedFeature {
                    feature: "UserInstanceName environment change".to_string(),
                    context: "capture_change_property".to_string(),
                })
            }
            EnvChangeTokenSubType::Routing => Err(crate::error::Error::UnimplementedFeature {
                feature: "Routing environment change".to_string(),
                context: "capture_change_property".to_string(),
            }),
            EnvChangeTokenSubType::Unknown(value) => {
                // Log unknown environment change subtypes but don't fail
                info!("Unknown environment change subtype: {}", value);
                Ok(())
            }
        }
    }
}

pub(crate) const ALREADY_EXECUTING_ERROR: &str = "There is an open batch on the current connection. It must be closed or fully consumed before executing another operation.";

#[cfg(test)]
mod tests {
    use super::*;
    use crate::token::tokens::{
        EnvChangeContainer, EnvChangeToken, EnvChangeTokenSubType, SqlCollation,
    };

    /// Fresh `NegotiatedSettings` for exercising `capture_change_property`'s
    /// database / language / collation write-through.
    fn new_ns() -> NegotiatedSettings {
        crate::handler::handler_factory::create_test_negotiated_settings_internal()
    }

    #[test]
    fn test_new_execution_context() {
        let ctx = ExecutionContext::new();
        assert_eq!(ctx.get_transaction_descriptor(), 0);
        assert_eq!(ctx.get_outstanding_requests(), 1);
        assert!(!ctx.has_open_batch());
        assert!(!ctx.has_open_result_set());
        assert!(!ctx.has_active_transaction());
    }

    #[test]
    fn test_has_active_transaction() {
        let mut ctx = ExecutionContext::new();

        // Initially no active transaction
        assert!(!ctx.has_active_transaction());
        assert_eq!(ctx.get_transaction_descriptor(), 0);

        // Simulate BEGIN TRANSACTION by setting a non-zero descriptor
        // (In practice this happens via ENVCHANGE token processing)
        let begin_txn_token = EnvChangeToken {
            sub_type: EnvChangeTokenSubType::BeginTransaction,
            change_type: (0u64, 12345678u64).into(),
        };
        ctx.capture_change_property(&begin_txn_token, &mut new_ns())
            .unwrap();

        // Now transaction is active
        assert!(ctx.has_active_transaction());
        assert_eq!(ctx.get_transaction_descriptor(), 12345678);

        // Simulate COMMIT TRANSACTION by setting descriptor back to 0
        let commit_txn_token = EnvChangeToken {
            sub_type: EnvChangeTokenSubType::CommitTransaction,
            change_type: (12345678u64, 0u64).into(),
        };
        ctx.capture_change_property(&commit_txn_token, &mut new_ns())
            .unwrap();

        // Transaction is no longer active
        assert!(!ctx.has_active_transaction());
        assert_eq!(ctx.get_transaction_descriptor(), 0);
    }

    #[test]
    fn test_set_has_open_batch() {
        let mut ctx = ExecutionContext::new();
        assert!(!ctx.has_open_batch());
        ctx.set_has_open_batch(true);
        assert!(ctx.has_open_batch());
        ctx.set_has_open_batch(false);
        assert!(!ctx.has_open_batch());
    }

    #[test]
    fn test_set_has_open_result_set() {
        let mut ctx = ExecutionContext::new();
        assert!(!ctx.has_open_result_set());
        ctx.set_has_open_result_set(true);
        assert!(ctx.has_open_result_set());
        ctx.set_has_open_result_set(false);
        assert!(!ctx.has_open_result_set());
    }

    #[test]
    fn test_capture_begin_transaction() {
        let mut ctx = ExecutionContext::new();
        let change_token = EnvChangeToken {
            sub_type: EnvChangeTokenSubType::BeginTransaction,
            change_type: EnvChangeContainer::from((0_u64, 12345_u64)),
        };
        ctx.capture_change_property(&change_token, &mut new_ns())
            .unwrap();
        assert_eq!(ctx.get_transaction_descriptor(), 12345);
    }

    #[test]
    fn test_capture_commit_transaction() {
        let mut ctx = ExecutionContext::new();
        ctx.transaction_descriptor = 999;
        let change_token = EnvChangeToken {
            sub_type: EnvChangeTokenSubType::CommitTransaction,
            change_type: EnvChangeContainer::from((999_u64, 0_u64)),
        };
        ctx.capture_change_property(&change_token, &mut new_ns())
            .unwrap();
        assert_eq!(ctx.get_transaction_descriptor(), 0);
    }

    #[test]
    fn test_capture_rollback_transaction() {
        let mut ctx = ExecutionContext::new();
        ctx.transaction_descriptor = 888;
        let change_token = EnvChangeToken {
            sub_type: EnvChangeTokenSubType::RollbackTransaction,
            change_type: EnvChangeContainer::from((888_u64, 0_u64)),
        };
        ctx.capture_change_property(&change_token, &mut new_ns())
            .unwrap();
        assert_eq!(ctx.get_transaction_descriptor(), 0);
    }

    #[test]
    fn test_capture_database_change() {
        let mut ctx = ExecutionContext::new();
        let mut ns = new_ns();
        let change_token = EnvChangeToken {
            sub_type: EnvChangeTokenSubType::Database,
            change_type: EnvChangeContainer::from(("OldDB".to_string(), "NewDB".to_string())),
        };
        ctx.capture_change_property(&change_token, &mut ns).unwrap();
        assert_eq!(ns.database, "NewDB");
    }

    #[test]
    fn test_capture_language_change() {
        let mut ctx = ExecutionContext::new();
        let mut ns = new_ns();
        let change_token = EnvChangeToken {
            sub_type: EnvChangeTokenSubType::Language,
            change_type: EnvChangeContainer::from(("".to_string(), "us_english".to_string())),
        };
        ctx.capture_change_property(&change_token, &mut ns).unwrap();
        assert_eq!(ns.language, "us_english");
    }

    #[test]
    fn test_capture_sql_collation() {
        let mut ctx = ExecutionContext::new();
        let mut ns = new_ns();
        let collation = SqlCollation {
            info: 0,
            lcid_language_id: 1033,
            col_flags: 0,
            sort_id: 52,
        };
        let change_token = EnvChangeToken {
            sub_type: EnvChangeTokenSubType::SqlCollation,
            change_type: EnvChangeContainer::from((Some(SqlCollation::default()), Some(collation))),
        };
        ctx.capture_change_property(&change_token, &mut ns).unwrap();
        assert_eq!(ns.database_collation.lcid_language_id, 1033);
    }

    #[test]
    fn test_database_change_writes_through_to_negotiated_settings() {
        let mut ctx = ExecutionContext::new();
        let mut ns = new_ns();
        let change_token = EnvChangeToken {
            sub_type: EnvChangeTokenSubType::Database,
            change_type: EnvChangeContainer::from(("master".to_string(), "tempdb".to_string())),
        };
        ctx.capture_change_property(&change_token, &mut ns).unwrap();
        assert_eq!(ns.database, "tempdb");
    }

    #[test]
    fn test_language_change_writes_through_to_negotiated_settings() {
        let mut ctx = ExecutionContext::new();
        let mut ns = new_ns();
        let change_token = EnvChangeToken {
            sub_type: EnvChangeTokenSubType::Language,
            change_type: EnvChangeContainer::from(("".to_string(), "français".to_string())),
        };
        ctx.capture_change_property(&change_token, &mut ns).unwrap();
        assert_eq!(ns.language, "français");
    }

    #[test]
    fn test_collation_change_writes_through_to_negotiated_settings() {
        let mut ctx = ExecutionContext::new();
        let mut ns = new_ns();
        let collation = SqlCollation {
            info: 0,
            lcid_language_id: 1036,
            col_flags: 0,
            sort_id: 52,
        };
        let change_token = EnvChangeToken {
            sub_type: EnvChangeTokenSubType::SqlCollation,
            change_type: EnvChangeContainer::from((Some(SqlCollation::default()), Some(collation))),
        };
        ctx.capture_change_property(&change_token, &mut ns).unwrap();
        assert_eq!(ns.database_collation.lcid_language_id, 1036);
    }

    #[test]
    fn test_capture_enlist_dtc_transaction() {
        let mut ctx = ExecutionContext::new();
        let change_token = EnvChangeToken {
            sub_type: EnvChangeTokenSubType::EnlistDtcTransaction,
            change_type: EnvChangeContainer::from((0_u64, 54321_u64)),
        };
        ctx.capture_change_property(&change_token, &mut new_ns())
            .unwrap();
        assert_eq!(ctx.get_transaction_descriptor(), 54321);
    }

    #[test]
    fn test_capture_defect_transaction() {
        let mut ctx = ExecutionContext::new();
        ctx.transaction_descriptor = 777;
        let change_token = EnvChangeToken {
            sub_type: EnvChangeTokenSubType::DefectTransaction,
            change_type: EnvChangeContainer::from((777_u64, 0_u64)),
        };
        ctx.capture_change_property(&change_token, &mut new_ns())
            .unwrap();
        assert_eq!(ctx.get_transaction_descriptor(), 0);
    }

    #[test]
    fn test_capture_unknown_subtype() {
        let mut ctx = ExecutionContext::new();
        let change_token = EnvChangeToken {
            sub_type: EnvChangeTokenSubType::Unknown(255),
            change_type: EnvChangeContainer::from((0_u64, 0_u64)),
        };
        // Should not error on unknown subtype
        assert!(
            ctx.capture_change_property(&change_token, &mut new_ns())
                .is_ok()
        );
    }

    #[test]
    fn test_capture_packet_size_error() {
        let mut ctx = ExecutionContext::new();
        let change_token = EnvChangeToken {
            sub_type: EnvChangeTokenSubType::PacketSize,
            change_type: EnvChangeContainer::from((0_u64, 0_u64)),
        };
        assert!(
            ctx.capture_change_property(&change_token, &mut new_ns())
                .is_err()
        );
    }

    #[test]
    fn test_already_executing_error_constant() {
        assert!(ALREADY_EXECUTING_ERROR.contains("open batch"));
    }

    // --- Type mismatch error tests ---

    #[test]
    fn test_transaction_with_non_uint64_container() {
        let mut ctx = ExecutionContext::new();
        for sub_type in [
            EnvChangeTokenSubType::BeginTransaction,
            EnvChangeTokenSubType::CommitTransaction,
            EnvChangeTokenSubType::RollbackTransaction,
            EnvChangeTokenSubType::EnlistDtcTransaction,
            EnvChangeTokenSubType::DefectTransaction,
        ] {
            let token = EnvChangeToken {
                sub_type,
                change_type: EnvChangeContainer::from(("a".to_string(), "b".to_string())),
            };
            let err = ctx
                .capture_change_property(&token, &mut new_ns())
                .unwrap_err();
            assert!(
                matches!(err, crate::error::Error::ProtocolError(ref msg) if msg.contains("UInt64")),
                "Expected ProtocolError for {sub_type:?}, got: {err:?}"
            );
        }
    }

    #[test]
    fn test_database_with_non_string_container() {
        let mut ctx = ExecutionContext::new();
        let token = EnvChangeToken {
            sub_type: EnvChangeTokenSubType::Database,
            change_type: EnvChangeContainer::from((0_u64, 1_u64)),
        };
        let err = ctx
            .capture_change_property(&token, &mut new_ns())
            .unwrap_err();
        assert!(
            matches!(err, crate::error::Error::ProtocolError(ref msg) if msg.contains("String"))
        );
    }

    #[test]
    fn test_language_with_non_string_container() {
        let mut ctx = ExecutionContext::new();
        let token = EnvChangeToken {
            sub_type: EnvChangeTokenSubType::Language,
            change_type: EnvChangeContainer::from((0_u64, 1_u64)),
        };
        let err = ctx
            .capture_change_property(&token, &mut new_ns())
            .unwrap_err();
        assert!(
            matches!(err, crate::error::Error::ProtocolError(ref msg) if msg.contains("String"))
        );
    }

    #[test]
    fn test_sql_collation_with_non_collation_container() {
        let mut ctx = ExecutionContext::new();
        let token = EnvChangeToken {
            sub_type: EnvChangeTokenSubType::SqlCollation,
            change_type: EnvChangeContainer::from(("a".to_string(), "b".to_string())),
        };
        let err = ctx
            .capture_change_property(&token, &mut new_ns())
            .unwrap_err();
        assert!(
            matches!(err, crate::error::Error::ProtocolError(ref msg) if msg.contains("Collation"))
        );
    }

    // --- Unimplemented feature tests ---

    #[test]
    fn test_unimplemented_env_change_subtypes() {
        let mut ctx = ExecutionContext::new();
        let dummy = EnvChangeContainer::from((0_u64, 0_u64));
        let unimplemented_subtypes = [
            EnvChangeTokenSubType::CharacterSet,
            EnvChangeTokenSubType::UnicodeDataSortingLocalId,
            EnvChangeTokenSubType::UnicodeDataSortingComparisonFlags,
            EnvChangeTokenSubType::DatabaseMirroringPartner,
            EnvChangeTokenSubType::PromoteTransaction,
            EnvChangeTokenSubType::TransactionManagerAddress,
            EnvChangeTokenSubType::TransactionEnded,
            EnvChangeTokenSubType::UserInstanceName,
            EnvChangeTokenSubType::Routing,
        ];
        for sub_type in unimplemented_subtypes {
            let token = EnvChangeToken {
                sub_type,
                change_type: dummy.clone(),
            };
            let err = ctx
                .capture_change_property(&token, &mut new_ns())
                .unwrap_err();
            assert!(
                matches!(err, crate::error::Error::UnimplementedFeature { .. }),
                "Expected UnimplementedFeature for {sub_type:?}, got: {err:?}"
            );
        }
    }

    #[test]
    fn test_reset_connection_env_change_is_accepted() {
        // The server sends a ResetConnection ENVCHANGE to acknowledge a
        // RESETCONNECTION / RESETCONNECTIONSKIPTRAN request. It must be
        // accepted gracefully rather than treated as an unimplemented feature.
        let mut ctx = ExecutionContext::new();
        let token = EnvChangeToken {
            sub_type: EnvChangeTokenSubType::ResetConnection,
            change_type: EnvChangeContainer::from((Vec::<u8>::new(), Vec::<u8>::new())),
        };
        assert!(ctx.capture_change_property(&token, &mut new_ns()).is_ok());
    }
}