1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
use odbc_sys::{
    Desc, HDesc, HStmt, Handle, HandleType, Integer, Pointer, SQLSetDescFieldW, SmallInt,
    SqlDataType,
};
use std::marker::PhantomData;

use crate::Error;

use super::{error::ToResult, AsHandle};

/// Wraps a valid description handle.
pub struct Description<'d> {
    parent: PhantomData<&'d HStmt>,
    handle: HDesc,
}

unsafe impl<'c> AsHandle for Description<'c> {
    fn as_handle(&self) -> Handle {
        self.handle as Handle
    }

    fn handle_type(&self) -> HandleType {
        HandleType::Desc
    }
}

impl<'d> Description<'d> {
    /// # Safety
    ///
    /// `handle` must be a valid description handle.
    pub unsafe fn new(handle: HDesc) -> Self {
        Self {
            handle,
            parent: PhantomData,
        }
    }

    pub fn set_field_precision(
        &mut self,
        column_number: SmallInt,
        precision: Integer,
    ) -> Result<(), Error> {
        unsafe {
            SQLSetDescFieldW(
                self.handle,
                column_number,
                Desc::Precision,
                precision as Pointer,
                0,
            )
            .to_result(self)
        }
    }

    pub fn set_field_scale(
        &mut self,
        column_number: SmallInt,
        scale: Integer,
    ) -> Result<(), Error> {
        unsafe {
            SQLSetDescFieldW(self.handle, column_number, Desc::Scale, scale as Pointer, 0)
                .to_result(self)
        }
    }

    pub unsafe fn set_field_type(
        &mut self,
        column_number: SmallInt,
        data_type: SqlDataType,
    ) -> Result<(), Error> {
        SQLSetDescFieldW(
            self.handle,
            column_number,
            Desc::Type,
            data_type.0 as Pointer,
            0,
        )
        .to_result(self)
    }

    pub unsafe fn set_field_concise_type(
        &mut self,
        column_number: SmallInt,
        data_type: SqlDataType,
    ) -> Result<(), Error> {
        SQLSetDescFieldW(
            self.handle,
            column_number,
            Desc::ConciseType,
            data_type.0 as Pointer,
            0,
        )
        .to_result(self)
    }
}