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
use sqlx_core::{statement::Statement, Either};
use ydb_grpc_bindings::generated::ydb;

use super::prelude::*;

const NO_COLUMNS: &[YdbColumn] = &[];

#[derive(Debug, Clone)]
pub(crate) struct NamedParameters {
    names: Vec<String>,
    types: Vec<YdbTypeInfo>,
}

impl From<std::collections::HashMap<String, ydb::Type>> for NamedParameters {
    fn from(value: std::collections::HashMap<String, ydb::Type>) -> Self {
        let cap = value.len();
        let (names,types) = value.into_iter()
        .filter_map(|(k,ty)|{Some((k, YdbTypeInfo::from(&ty.r#type?)))})
        .fold((Vec::with_capacity(cap), Vec::with_capacity(cap)), |(mut names, mut types), (n,t)|{
            names.push(n);
            types.push(t);
            (names, types)
        });
        Self{names, types}
    }
}

#[derive(Debug, Clone)]
pub struct YdbStatement {
    pub (crate) query_id: String,
    pub (crate) yql: String,
    pub (crate) parameters: NamedParameters,
}

impl YdbStatement {
    pub fn query_id(&self) -> &str {
        &self.query_id
    }
}

impl Statement<'_> for YdbStatement {
    type Database = Ydb;

    fn to_owned(&self) -> YdbStatement {
        self.clone()
    }

    fn sql(&self) -> &str {
        &self.yql
    }

    fn parameters(&self) -> Option<Either<&[YdbTypeInfo], usize>> {
        Some(Either::Left(&self.parameters.types))
    }

    fn columns(&self) -> &[YdbColumn] {
        //TODO: определиться, зачем тут колонки
        NO_COLUMNS
    }

    sqlx_core::impl_statement_query!(YdbArguments);
}