Crate mssql_value_serializer

Crate mssql_value_serializer 

Source
Expand description

§mssql-value-serializer

Convert Rust values into SQL Server-compatible literal expressions, enabling dynamic SQL generation without parameter count limitations.

Prepared 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.

§Usage

use mssql_value_serializer::{SqlServerCharWrapper, SqlServerLiteralWrapper, SqlServerLiteralDynWrapper, SqlServerLiteralForValueListWrapper};

let sql = format!("
    SELECT
        *
    FROM
        [table]
    WHERE
        [name] = {name}
", name = SqlServerLiteralWrapper::new("David"));

assert_eq!("
    SELECT
        *
    FROM
        [table]
    WHERE
        [name] = N'David'
", sql);

let sql = format!("
    SELECT
        *
    FROM
        [table]
    WHERE
        [name] = {name}
", name = SqlServerCharWrapper::new("David")); // use `SqlServerCharWrapper` to format a value into a non-Unicode character string

assert_eq!("
    SELECT
        *
    FROM
        [table]
    WHERE
        [name] = 'David'
", sql);

let sql = format!("
    INSERT INTO [table]([id], [name], [disabled])
        VALUES
            ({values})
", values = SqlServerLiteralForValueListWrapper::new(vec![SqlServerLiteralDynWrapper::from(2u32), SqlServerLiteralDynWrapper::from("David"), SqlServerLiteralDynWrapper::from(false)]));

assert_eq!("
    INSERT INTO [table]([id], [name], [disabled])
        VALUES
            (2, N'David', 0)
", sql);

§Optional Features

  • serde: Implements serde::Serialize for wrapper types, enabling SQL Server literal serialization behavior.
  • chrono or time: Adds support for SQL Server date and time types.
    • 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.
  • rust_decimal or bigdecimal: Adds support for SQL Server decimal/numeric types.
  • num-bigint: Adds support for SQL Server decimal/numeric types (only integers).
  • uuid: Adds support for SQL Server UNIQUEIDENTIFIER type.

Structs§

SqlLiteralErrorWithIndex
Errors that occur when serializing a SQL Server literal for a value list.
SqlServerCharWrapper
A wrapper type for string values.
SqlServerLiteralForValueListWrapper
A wrapper type for any value implementing SqlServerLiteralForValueList.
SqlServerLiteralWrapper
A wrapper type for any value implementing SqlServerLiteral.

Enums§

SqlLiteralError
Errors that occur when serializing a SQL Server literal.
SqlServerLiteralDynWrapper
A wrapper type that can hold either a borrowed or owned [dyn SqlServerLiteral] value.

Traits§

SqlServerLiteral
Represents a type that can be converted into a valid SQL Server literal.
SqlServerLiteralForValueList
Represents a type that can serialize a collection of SQL Server literal values.

Functions§

append_sql_literal_for_value_list
Appends a comma-separated list of SQL Server literals to the provided output string.