Collation

Struct Collation 

Source
pub struct Collation {
    pub lcid: u32,
    pub sort_id: u8,
}
Expand description

SQL Server collation.

Collations in SQL Server define the character encoding and sorting rules for string data. For VARCHAR columns, the collation determines which code page (character encoding) is used to store the data.

§Encoding Support

When the encoding feature is enabled, the Collation::encoding() method returns the appropriate encoding_rs::Encoding for decoding VARCHAR data.

§Example

use tds_protocol::token::Collation;

let collation = Collation { lcid: 0x0804, sort_id: 0 }; // Chinese (PRC)
if let Some(encoding) = collation.encoding() {
    let (decoded, _, _) = encoding.decode(raw_bytes);
    // decoded is now proper Chinese text
}

Fields§

§lcid: u32

Locale ID (LCID).

The LCID encodes both the language and region. The lower 16 bits contain the primary language ID, and bits 16-19 contain the sort ID for some collations.

For UTF-8 collations (SQL Server 2019+), bit 27 (0x0800_0000) is set.

§sort_id: u8

Sort ID.

Used with certain collations to specify sorting behavior.

Implementations§

Source§

impl Collation

Source

pub fn encoding(&self) -> Option<&'static Encoding>

Returns the character encoding for this collation.

This method maps the collation’s LCID to the appropriate character encoding from the encoding_rs crate.

§Returns
  • Some(&Encoding) - The encoding to use for decoding VARCHAR data
  • None - If the collation uses UTF-8 (no transcoding needed) or the LCID is not recognized (caller should use Windows-1252 fallback)
§UTF-8 Collations

SQL Server 2019+ supports UTF-8 collations (identified by the _UTF8 suffix). These return None because no transcoding is needed.

§Example
let collation = Collation { lcid: 0x0419, sort_id: 0 }; // Russian
if let Some(encoding) = collation.encoding() {
    // encoding is Windows-1251 for Cyrillic
    let (text, _, had_errors) = encoding.decode(&raw_bytes);
}
Source

pub fn is_utf8(&self) -> bool

Returns whether this collation uses UTF-8 encoding.

UTF-8 collations were introduced in SQL Server 2019 and are identified by the _UTF8 suffix in the collation name.

Source

pub fn code_page(&self) -> Option<u16>

Returns the Windows code page number for this collation.

Useful for error messages and debugging.

§Returns

The code page number (e.g., 1252 for Western European, 932 for Japanese).

Source

pub fn encoding_name(&self) -> &'static str

Returns the encoding name for this collation.

Useful for error messages and debugging.

Trait Implementations§

Source§

impl Clone for Collation

Source§

fn clone(&self) -> Collation

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Collation

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Collation

Source§

fn default() -> Collation

Returns the “default value” for a type. Read more
Source§

impl Copy for Collation

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.