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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

//! SQL Server identifier parsing and escaping utilities.
//!
//! This module provides functionality to parse multipart SQL Server identifiers
//! (server.database.schema.table) and escape them properly to prevent SQL injection.

use crate::core::TdsResult;

/// Indices for multipart identifier array
#[allow(dead_code)]
pub const SERVER_INDEX: usize = 0;
pub const CATALOG_INDEX: usize = 1;
pub const SCHEMA_INDEX: usize = 2;
pub const TABLE_INDEX: usize = 3;
pub const MAX_PARTS: usize = 4;

/// Parse a SQL Server multipart identifier.
///
/// Parses identifiers in the format: `[server].[database].[schema].[table]`
/// The parser uses a finite state machine to handle:
/// - Quoted identifiers with `[...]` or `"..."` syntax
/// - Escaped brackets (`]]` inside quoted identifiers)
/// - Whitespace handling
/// - Separator (`.`) detection
///
/// # Arguments
/// * `name` - The identifier string to parse (e.g., "db.schema.table")
/// * `allow_empty` - Whether to allow completely empty identifiers
///
/// # Returns
/// Array of 4 optional strings: [server, catalog, schema, table]
/// Missing parts are None, present parts are Some(String)
/// The array is right-justified (trailing positions filled first).
///
/// # Example
/// ```
/// use mssql_tds::sql_identifier::{parse_multipart_identifier, CATALOG_INDEX, SCHEMA_INDEX, TABLE_INDEX};
///
/// let parts = parse_multipart_identifier("MyDB.dbo.Users", false).unwrap();
/// assert_eq!(parts[CATALOG_INDEX], Some("MyDB".to_string()));
/// assert_eq!(parts[SCHEMA_INDEX], Some("dbo".to_string()));
/// assert_eq!(parts[TABLE_INDEX], Some("Users".to_string()));
/// ```
pub fn parse_multipart_identifier(
    name: &str,
    allow_empty: bool,
) -> TdsResult<[Option<String>; MAX_PARTS]> {
    // State machine states
    #[derive(Debug, PartialEq)]
    enum State {
        Init,         // Initial state
        InPart,       // Reading unquoted part
        InQuotedPart, // Reading quoted part
        AfterQuote,   // After closing quote
        AfterDot,     // After separator dot
    }

    let mut state = State::Init;
    let mut parts: Vec<String> = Vec::with_capacity(MAX_PARTS);
    let mut current_part = String::new();
    let mut quote_char: Option<char> = None;
    let chars: Vec<char> = name.chars().collect();
    let mut i = 0;

    while i < chars.len() {
        let ch = chars[i];

        match state {
            State::Init => {
                if ch.is_whitespace() {
                    // Skip leading whitespace
                    i += 1;
                    continue;
                } else if ch == '[' || ch == '"' {
                    // Start quoted identifier
                    quote_char = Some(ch);
                    state = State::InQuotedPart;
                } else if ch == '.' {
                    // Empty part before dot
                    if !allow_empty && parts.is_empty() {
                        return Err(crate::error::Error::UsageError(
                            "Empty identifier part is not allowed".to_string(),
                        ));
                    }
                    parts.push(String::new());
                    state = State::AfterDot;
                } else {
                    // Start unquoted identifier
                    current_part.push(ch);
                    state = State::InPart;
                }
            }
            State::InPart => {
                if ch == '.' {
                    // End of part
                    parts.push(current_part.trim_end().to_string());
                    current_part.clear();
                    state = State::AfterDot;
                } else {
                    current_part.push(ch);
                }
            }
            State::InQuotedPart => {
                let closing_quote = match quote_char {
                    Some('[') => ']',
                    Some('"') => '"',
                    _ => unreachable!(),
                };

                if ch == closing_quote {
                    // Check for escaped quote (]] or "")
                    if i + 1 < chars.len() && chars[i + 1] == closing_quote {
                        // Escaped quote - add one closing quote to the part
                        current_part.push(closing_quote);
                        i += 1; // Skip the next quote
                    } else {
                        // End of quoted part
                        state = State::AfterQuote;
                    }
                } else {
                    current_part.push(ch);
                }
            }
            State::AfterQuote => {
                if ch.is_whitespace() {
                    // Skip whitespace after quote
                    i += 1;
                    continue;
                } else if ch == '.' {
                    // End of part
                    parts.push(current_part.clone());
                    current_part.clear();
                    quote_char = None;
                    state = State::AfterDot;
                } else {
                    return Err(crate::error::Error::UsageError(format!(
                        "Unexpected character '{}' after closing quote",
                        ch
                    )));
                }
            }
            State::AfterDot => {
                if ch.is_whitespace() {
                    // Skip whitespace after dot
                    i += 1;
                    continue;
                } else if ch == '[' || ch == '"' {
                    // Start quoted identifier
                    quote_char = Some(ch);
                    state = State::InQuotedPart;
                } else if ch == '.' {
                    // Empty part
                    if !allow_empty {
                        return Err(crate::error::Error::UsageError(
                            "Empty identifier part is not allowed".to_string(),
                        ));
                    }
                    parts.push(String::new());
                } else {
                    // Start unquoted identifier
                    current_part.push(ch);
                    state = State::InPart;
                }
            }
        }

        i += 1;
    }

    // Handle final part
    match state {
        State::InPart | State::AfterQuote => {
            parts.push(current_part.trim_end().to_string());
        }
        State::InQuotedPart => {
            return Err(crate::error::Error::UsageError(
                "Unclosed quoted identifier".to_string(),
            ));
        }
        State::Init if !allow_empty => {
            return Err(crate::error::Error::UsageError(
                "Empty identifier is not allowed".to_string(),
            ));
        }
        _ => {}
    }

    // Validate part count
    if parts.len() > MAX_PARTS {
        return Err(crate::error::Error::UsageError(format!(
            "Too many identifier parts: {} (maximum is {})",
            parts.len(),
            MAX_PARTS
        )));
    }

    // Right-justify the parts array
    let mut result: [Option<String>; MAX_PARTS] = [None, None, None, None];
    let start_index = MAX_PARTS - parts.len();
    for (i, part) in parts.into_iter().enumerate() {
        result[start_index + i] = Some(part);
    }

    Ok(result)
}

/// Escape a SQL Server identifier by wrapping in brackets and escaping existing brackets.
///
/// # Arguments
/// * `name` - The identifier to escape
///
/// # Returns
/// The escaped identifier wrapped in square brackets
///
/// # Example
/// ```
/// use mssql_tds::sql_identifier::escape_identifier;
///
/// assert_eq!(escape_identifier("MyTable"), "[MyTable]");
/// assert_eq!(escape_identifier("My]Table"), "[My]]Table]");
/// ```
pub fn escape_identifier(name: &str) -> String {
    format!("[{}]", name.replace("]", "]]"))
}

/// Escape a string for use in a SQL literal (within '...').
///
/// This escapes single quotes by doubling them.
///
/// # Arguments
/// * `input` - The string to escape
///
/// # Returns
/// The escaped string suitable for use in a SQL literal
///
/// # Example
/// ```
/// use mssql_tds::sql_identifier::escape_string_literal;
///
/// assert_eq!(escape_string_literal("O'Brien"), "O''Brien");
/// assert_eq!(escape_string_literal("It's"), "It''s");
/// ```
pub fn escape_string_literal(input: &str) -> String {
    input.replace("'", "''")
}

/// Build a multipart name from components.
///
/// Reconstructs a qualified identifier from its parts, omitting leading None values.
///
/// # Arguments
/// * `parts` - Array of optional identifier parts [server, catalog, schema, table]
///
/// # Returns
/// The reconstructed identifier with each part escaped and separated by dots
///
/// # Example
/// ```
/// use mssql_tds::sql_identifier::build_multipart_name;
///
/// let parts = [None, Some("MyDB".to_string()), Some("dbo".to_string()), Some("Users".to_string())];
/// assert_eq!(build_multipart_name(&parts), "[MyDB].[dbo].[Users]");
/// ```
pub fn build_multipart_name(parts: &[Option<String>; MAX_PARTS]) -> String {
    let mut result = String::new();

    for part in parts.iter().flatten() {
        if !result.is_empty() {
            result.push('.');
        }
        result.push_str(&escape_identifier(part));
    }

    result
}

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

    #[test]
    fn test_parse_one_part() {
        let parts = parse_multipart_identifier("Users", false).unwrap();
        assert_eq!(parts[SERVER_INDEX], None);
        assert_eq!(parts[CATALOG_INDEX], None);
        assert_eq!(parts[SCHEMA_INDEX], None);
        assert_eq!(parts[TABLE_INDEX], Some("Users".to_string()));
    }

    #[test]
    fn test_parse_two_parts() {
        let parts = parse_multipart_identifier("dbo.Users", false).unwrap();
        assert_eq!(parts[SERVER_INDEX], None);
        assert_eq!(parts[CATALOG_INDEX], None);
        assert_eq!(parts[SCHEMA_INDEX], Some("dbo".to_string()));
        assert_eq!(parts[TABLE_INDEX], Some("Users".to_string()));
    }

    #[test]
    fn test_parse_three_parts() {
        let parts = parse_multipart_identifier("MyDB.dbo.Users", false).unwrap();
        assert_eq!(parts[SERVER_INDEX], None);
        assert_eq!(parts[CATALOG_INDEX], Some("MyDB".to_string()));
        assert_eq!(parts[SCHEMA_INDEX], Some("dbo".to_string()));
        assert_eq!(parts[TABLE_INDEX], Some("Users".to_string()));
    }

    #[test]
    fn test_parse_four_parts() {
        let parts = parse_multipart_identifier("Server.MyDB.dbo.Users", false).unwrap();
        assert_eq!(parts[SERVER_INDEX], Some("Server".to_string()));
        assert_eq!(parts[CATALOG_INDEX], Some("MyDB".to_string()));
        assert_eq!(parts[SCHEMA_INDEX], Some("dbo".to_string()));
        assert_eq!(parts[TABLE_INDEX], Some("Users".to_string()));
    }

    #[test]
    fn test_parse_quoted_identifier() {
        let parts = parse_multipart_identifier("[My Table]", false).unwrap();
        assert_eq!(parts[TABLE_INDEX], Some("My Table".to_string()));
    }

    #[test]
    fn test_parse_escaped_brackets() {
        let parts = parse_multipart_identifier("[My]]Table]", false).unwrap();
        assert_eq!(parts[TABLE_INDEX], Some("My]Table".to_string()));
    }

    #[test]
    fn test_parse_double_quotes() {
        let parts = parse_multipart_identifier("\"My Table\"", false).unwrap();
        assert_eq!(parts[TABLE_INDEX], Some("My Table".to_string()));
    }

    #[test]
    fn test_parse_mixed_quotes_and_unquoted() {
        let parts = parse_multipart_identifier("[My DB].dbo.[My Table]", false).unwrap();
        assert_eq!(parts[CATALOG_INDEX], Some("My DB".to_string()));
        assert_eq!(parts[SCHEMA_INDEX], Some("dbo".to_string()));
        assert_eq!(parts[TABLE_INDEX], Some("My Table".to_string()));
    }

    #[test]
    fn test_parse_whitespace_handling() {
        let parts = parse_multipart_identifier("  MyDB . dbo . Users  ", false).unwrap();
        assert_eq!(parts[CATALOG_INDEX], Some("MyDB".to_string()));
        assert_eq!(parts[SCHEMA_INDEX], Some("dbo".to_string()));
        assert_eq!(parts[TABLE_INDEX], Some("Users".to_string()));
    }

    #[test]
    fn test_parse_too_many_parts() {
        let result = parse_multipart_identifier("A.B.C.D.E", false);
        assert!(result.is_err());
    }

    #[test]
    fn test_parse_empty_not_allowed() {
        let result = parse_multipart_identifier("", false);
        assert!(result.is_err());
    }

    #[test]
    fn test_parse_empty_allowed() {
        let result = parse_multipart_identifier("", true);
        assert!(result.is_ok());
    }

    #[test]
    fn test_parse_unclosed_quote() {
        let result = parse_multipart_identifier("[MyTable", false);
        assert!(result.is_err());
    }

    #[test]
    fn test_escape_identifier() {
        assert_eq!(escape_identifier("MyTable"), "[MyTable]");
        assert_eq!(escape_identifier("My]Table"), "[My]]Table]");
        assert_eq!(escape_identifier("My]]Table"), "[My]]]]Table]");
        assert_eq!(escape_identifier(""), "[]");
    }

    #[test]
    fn test_escape_string_literal() {
        assert_eq!(escape_string_literal("O'Brien"), "O''Brien");
        assert_eq!(escape_string_literal("It's"), "It''s");
        assert_eq!(escape_string_literal("No quotes"), "No quotes");
        assert_eq!(escape_string_literal(""), "");
    }

    #[test]
    fn test_build_multipart_name() {
        let parts = [
            None,
            Some("MyDB".to_string()),
            Some("dbo".to_string()),
            Some("Users".to_string()),
        ];
        assert_eq!(build_multipart_name(&parts), "[MyDB].[dbo].[Users]");
    }

    #[test]
    fn test_build_multipart_name_with_special_chars() {
        let parts = [
            None,
            Some("My]DB".to_string()),
            Some("dbo".to_string()),
            Some("My]Table".to_string()),
        ];
        assert_eq!(build_multipart_name(&parts), "[My]]DB].[dbo].[My]]Table]");
    }

    #[test]
    fn test_build_multipart_name_single_part() {
        let parts = [None, None, None, Some("Users".to_string())];
        assert_eq!(build_multipart_name(&parts), "[Users]");
    }

    #[test]
    fn test_build_multipart_name_all_parts() {
        let parts = [
            Some("Server".to_string()),
            Some("DB".to_string()),
            Some("schema".to_string()),
            Some("table".to_string()),
        ];
        assert_eq!(
            build_multipart_name(&parts),
            "[Server].[DB].[schema].[table]"
        );
    }
}