tiberius/tds/codec/column_data/
text.rs

1use crate::{error::Error, sql_read_bytes::SqlReadBytes, tds::Collation, ColumnData};
2
3pub(crate) async fn decode<R>(
4    src: &mut R,
5    collation: Option<Collation>,
6) -> crate::Result<ColumnData<'static>>
7where
8    R: SqlReadBytes + Unpin,
9{
10    let ptr_len = src.read_u8().await? as usize;
11
12    if ptr_len == 0 {
13        return Ok(ColumnData::String(None));
14    }
15
16    for _ in 0..ptr_len {
17        src.read_u8().await?;
18    }
19
20    src.read_i32_le().await?; // days
21    src.read_u32_le().await?; // second fractions
22
23    let text = match collation {
24        // TEXT
25        Some(collation) => {
26            let encoder = collation.encoding()?;
27            let text_len = src.read_u32_le().await? as usize;
28            let mut buf = Vec::with_capacity(text_len);
29
30            for _ in 0..text_len {
31                buf.push(src.read_u8().await?);
32            }
33
34            encoder
35                .decode_without_bom_handling_and_without_replacement(buf.as_ref())
36                .ok_or_else(|| Error::Encoding("invalid sequence".into()))?
37                .to_string()
38        }
39        // NTEXT
40        None => {
41            let text_len = src.read_u32_le().await? as usize / 2;
42            let mut buf = Vec::with_capacity(text_len);
43
44            for _ in 0..text_len {
45                buf.push(src.read_u16_le().await?);
46            }
47
48            String::from_utf16(&buf[..])?
49        }
50    };
51
52    Ok(ColumnData::String(Some(text.into())))
53}