mssql_value_serializer/lib.rs
1/*!
2# mssql-value-serializer
3
4Convert Rust values into SQL Server-compatible literal expressions, enabling dynamic SQL generation without parameter count limitations.
5
6Prepared statements are commonly used to improve performance and security. By separating the SQL command from the data values, the database can cache execution plans and protect against SQL injection. Each variable passed to the query becomes a parameter, allowing safe reuse of the same statement with different values. However, because SQL Server enforces a strict 2100-parameter limit, queries that bind large collections of parameters—such as long `IN` lists or bulk inserts—can easily exceed this cap and trigger the **too many parameters** error.
7
8## Usage
9
10```rust
11use mssql_value_serializer::{SqlServerCharWrapper, SqlServerLiteralWrapper, SqlServerLiteralDynWrapper, SqlServerLiteralForValueListWrapper};
12
13let sql = format!("
14 SELECT
15 *
16 FROM
17 [table]
18 WHERE
19 [name] = {name}
20", name = SqlServerLiteralWrapper::new("David"));
21
22assert_eq!("
23 SELECT
24 *
25 FROM
26 [table]
27 WHERE
28 [name] = N'David'
29", sql);
30
31let sql = format!("
32 SELECT
33 *
34 FROM
35 [table]
36 WHERE
37 [name] = {name}
38", name = SqlServerCharWrapper::new("David")); // use `SqlServerCharWrapper` to format a value into a non-Unicode character string
39
40assert_eq!("
41 SELECT
42 *
43 FROM
44 [table]
45 WHERE
46 [name] = 'David'
47", sql);
48
49let sql = format!("
50 INSERT INTO [table]([id], [name], [disabled])
51 VALUES
52 ({values})
53", values = SqlServerLiteralForValueListWrapper::new(vec![SqlServerLiteralDynWrapper::from(2u32), SqlServerLiteralDynWrapper::from("David"), SqlServerLiteralDynWrapper::from(false)]));
54
55assert_eq!("
56 INSERT INTO [table]([id], [name], [disabled])
57 VALUES
58 (2, N'David', 0)
59", sql);
60```
61
62## Optional Features
63
64* `serde`: Implements `serde::Serialize` for wrapper types, enabling SQL Server literal serialization behavior.
65* `chrono` or `time`: Adds support for SQL Server date and time types.
66 * `chrono` and `stable-local`: If your local timezone does not observe daylight saving time (DST), enable this feature to use a fixed offset for `DateTime<Local>`, improving formatting performance.
67* `rust_decimal` or `bigdecimal`: Adds support for SQL Server decimal/numeric types.
68* `num-bigint`: Adds support for SQL Server decimal/numeric types (only integers).
69* `uuid`: Adds support for SQL Server UNIQUEIDENTIFIER type.
70*/
71
72#![allow(unexpected_cfgs)]
73#![cfg_attr(docsrs_1_92, feature(doc_cfg))]
74
75mod errors;
76mod functions;
77mod literals;
78mod traits;
79mod wrappers;
80
81pub use errors::*;
82pub use functions::*;
83pub(crate) use literals::*;
84pub use traits::*;
85pub use wrappers::*;