Crate tds_protocol

Crate tds_protocol 

Source
Expand description

§tds-protocol

Pure implementation of the MS-TDS (Tabular Data Stream) protocol used by Microsoft SQL Server.

This crate provides no_std compatible packet structures, token parsing, and serialization for TDS protocol versions 7.4 through 8.0.

§Features

  • std (default): Enable standard library support
  • alloc: Enable allocation without full std (requires alloc crate)
  • encoding (default): Enable collation-aware string encoding/decoding via the Collation::encoding() method. Uses the encoding_rs crate.

§Collation-Aware VARCHAR Decoding

When the encoding feature is enabled (default), this crate provides comprehensive support for decoding VARCHAR data with locale-specific character encodings. This is essential for databases using non-ASCII collations (e.g., Japanese, Chinese, Korean, Cyrillic, Arabic, etc.).

§Supported Encodings

Code PageEncodingLanguages
874Windows-874 (TIS-620)Thai
932Shift_JISJapanese
936GBK/GB18030Simplified Chinese
949EUC-KRKorean
950Big5Traditional Chinese
1250Windows-1250Central/Eastern European
1251Windows-1251Cyrillic
1252Windows-1252Western European (default)
1253Windows-1253Greek
1254Windows-1254Turkish
1255Windows-1255Hebrew
1256Windows-1256Arabic
1257Windows-1257Baltic
1258Windows-1258Vietnamese
UTF-8UTF-8SQL Server 2019+ collations

§Example

use tds_protocol::Collation;

// Japanese collation (LCID 0x0411 = Japanese_CI_AS)
let collation = Collation { lcid: 0x0411, sort_id: 0 };
if let Some(encoding) = collation.encoding() {
    // encoding is Shift_JIS
    let (decoded, _, _) = encoding.decode(varchar_bytes);
}

// Check if UTF-8 collation (SQL Server 2019+)
let utf8_collation = Collation { lcid: 0x0800_0409, sort_id: 0 };
assert!(utf8_collation.is_utf8()); // true, no transcoding needed

§Design Philosophy

This crate is intentionally IO-agnostic. It contains no networking logic and makes no assumptions about the async runtime. Higher-level crates build upon this foundation to provide async I/O capabilities.

§Example

use tds_protocol::{PacketHeader, PacketType, PacketStatus};

let header = PacketHeader {
    packet_type: PacketType::SqlBatch,
    status: PacketStatus::END_OF_MESSAGE,
    length: 100,
    spid: 0,
    packet_id: 1,
    window: 0,
};

Re-exports§

pub use error::ProtocolError;
pub use login7::FeatureExtension;
pub use login7::FeatureId;
pub use login7::Login7;
pub use login7::OptionFlags1;
pub use login7::OptionFlags2;
pub use login7::OptionFlags3;
pub use login7::TypeFlags;
pub use packet::DEFAULT_PACKET_SIZE;
pub use packet::MAX_PACKET_SIZE;
pub use packet::PACKET_HEADER_SIZE;
pub use packet::PacketHeader;
pub use packet::PacketStatus;
pub use packet::PacketType;
pub use prelogin::EncryptionLevel;
pub use prelogin::PreLogin;
pub use prelogin::PreLoginOption;
pub use rpc::ParamFlags;
pub use rpc::ProcId;
pub use rpc::RpcOptionFlags;
pub use rpc::RpcParam;
pub use rpc::RpcRequest;
pub use rpc::TypeInfo as RpcTypeInfo;
pub use sql_batch::SqlBatch;
pub use sql_batch::encode_sql_batch;
pub use sql_batch::encode_sql_batch_with_transaction;
pub use token::ColMetaData;
pub use token::Collation;
pub use token::ColumnData;
pub use token::Done;
pub use token::DoneInProc;
pub use token::DoneProc;
pub use token::DoneStatus;
pub use token::EnvChange;
pub use token::EnvChangeType;
pub use token::EnvChangeValue;
pub use token::FeatureExtAck;
pub use token::FedAuthInfo;
pub use token::LoginAck;
pub use token::NbcRow;
pub use token::Order;
pub use token::RawRow;
pub use token::ReturnValue;
pub use token::ServerError;
pub use token::ServerInfo;
pub use token::SessionState;
pub use token::SspiToken;
pub use token::Token;
pub use token::TokenParser;
pub use token::TokenType;
pub use token::TypeInfo;
pub use tvp::TVP_END_TOKEN;
pub use tvp::TVP_ROW_TOKEN;
pub use tvp::TVP_TYPE_ID;
pub use tvp::TvpColumnDef as TvpWireColumnDef;
pub use tvp::TvpColumnFlags;
pub use tvp::TvpEncoder;
pub use tvp::TvpWireType;
pub use tvp::encode_tvp_bit;
pub use tvp::encode_tvp_date;
pub use tvp::encode_tvp_datetime2;
pub use tvp::encode_tvp_datetimeoffset;
pub use tvp::encode_tvp_decimal;
pub use tvp::encode_tvp_float;
pub use tvp::encode_tvp_guid;
pub use tvp::encode_tvp_int;
pub use tvp::encode_tvp_null;
pub use tvp::encode_tvp_nvarchar;
pub use tvp::encode_tvp_time;
pub use tvp::encode_tvp_varbinary;
pub use types::ColumnFlags;
pub use types::TypeId;
pub use types::Updateable;
pub use version::SqlServerVersion;
pub use version::TdsVersion;
pub use crypto::ALGORITHM_AEAD_AES_256_CBC_HMAC_SHA256;
pub use crypto::COLUMN_FLAG_ENCRYPTED;
pub use crypto::CekTable;
pub use crypto::CekTableEntry;
pub use crypto::CekValue;
pub use crypto::ColumnCryptoInfo;
pub use crypto::CryptoMetadata;
pub use crypto::ENCRYPTION_TYPE_DETERMINISTIC;
pub use crypto::ENCRYPTION_TYPE_RANDOMIZED;
pub use crypto::EncryptionTypeWire;
pub use crypto::NORMALIZATION_RULE_VERSION;
pub use crypto::is_column_encrypted;

Modules§

codec
Codec utilities for TDS protocol encoding and decoding.
collation
Collation encoding support for SQL Server VARCHAR decoding.
crypto
Always Encrypted cryptography metadata for TDS protocol.
error
Protocol-level error types.
login7
TDS LOGIN7 packet construction.
packet
TDS packet header definitions.
prelogin
TDS pre-login packet handling.
rpc
RPC (Remote Procedure Call) request encoding.
sql_batch
SQL batch request encoding.
token
TDS token stream definitions.
tvp
Table-Valued Parameter (TVP) wire format encoding.
types
TDS data type definitions.
version
TDS protocol version definitions.