rdbc_rs/driver/
stmt.rs

1use std::fmt::Display;
2
3use super::callback::BoxedCallback;
4
5/// SQL argument placeholder name
6#[derive(Debug, Clone, PartialEq)]
7pub enum ArgName {
8    String(String),
9    Offset(usize),
10}
11
12impl From<usize> for ArgName {
13    fn from(data: usize) -> Self {
14        ArgName::Offset(data)
15    }
16}
17
18impl From<String> for ArgName {
19    fn from(data: String) -> Self {
20        ArgName::String(data)
21    }
22}
23
24impl From<&str> for ArgName {
25    fn from(data: &str) -> Self {
26        ArgName::String(data.to_owned())
27    }
28}
29
30#[derive(Debug, Clone, PartialEq)]
31pub enum ArgValue {
32    I64(i64),
33    F64(f64),
34    String(String),
35    Bytes(Vec<u8>),
36    Null,
37}
38
39impl Display for ArgValue {
40    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
41        match self {
42            Self::I64(v) => write!(f, "{}", v),
43            Self::F64(v) => write!(f, "{}", v),
44            Self::String(v) => write!(f, "'{}'", v),
45            Self::Bytes(v) => write!(f, "{:x?}", v),
46            Self::Null => write!(f, "NULL"),
47        }
48    }
49}
50
51#[derive(Debug, Clone, PartialEq)]
52pub struct Argument {
53    pub name: ArgName,
54    pub value: ArgValue,
55}
56
57#[derive(Debug, Clone, PartialEq)]
58pub struct ExecResult {
59    pub last_insert_id: u64,
60    pub raws_affected: u64,
61}
62
63#[derive(Clone, Debug, PartialEq)]
64pub struct Column {
65    pub column_index: u64,
66    pub column_name: String,
67    pub column_decltype: String,
68    pub column_decltype_len: Option<u64>,
69}
70
71#[derive(Clone, Debug, PartialEq, Eq, Hash)]
72pub enum ColumnType {
73    I64,
74    F64,
75    String,
76    Bytes,
77    Null,
78}
79
80/// Statement driver provider trait
81pub trait Statement: Send {
82    /// Returns the number of placeholder parameters.
83    ///
84    /// May returns [`None`], if the driver doesn't know its number of placeholder
85    fn num_input(&self, callback: BoxedCallback<Option<usize>>);
86
87    /// Executes a query that doesn't return rows, such
88    /// as an INSERT or UPDATE.
89    fn execute(&mut self, args: Vec<Argument>, callback: BoxedCallback<ExecResult>);
90
91    /// executes a query that may return rows, such as a
92    /// SELECT.
93    fn query(&mut self, args: Vec<Argument>, callback: BoxedCallback<Box<dyn Rows>>);
94}
95
96pub trait Rows: Send {
97    fn colunms(&mut self, callback: BoxedCallback<Vec<Column>>);
98
99    fn next(&mut self, callback: BoxedCallback<bool>);
100
101    /// Get current row value by arg name.
102    fn get(
103        &mut self,
104        name: ArgName,
105        column_type: ColumnType,
106        callback: BoxedCallback<Option<ArgValue>>,
107    );
108}