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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
use core::f32;
use std::convert::TryFrom;
use sqlx_clickhouse_ext::sqlx_core::error::Error;
#[cfg(feature = "chrono")]
use sqlx_clickhouse_ext::sqlx_core::types::chrono::NaiveDate;
#[cfg(feature = "bigdecimal")]
use sqlx_clickhouse_ext::sqlx_core::types::BigDecimal;
#[cfg(feature = "uuid")]
use sqlx_clickhouse_ext::sqlx_core::types::Uuid;
pub(crate) enum ClickhousePgType {
Char,
Int2,
Int4,
Int8,
Float4,
Float8,
Varchar,
#[cfg(feature = "chrono")]
Date,
#[cfg(feature = "bigdecimal")]
Numeric,
#[cfg(feature = "uuid")]
Uuid,
}
impl TryFrom<(&str, usize)> for ClickhousePgType {
type Error = Error;
fn try_from(t: (&str, usize)) -> Result<Self, Self::Error> {
let (s, index) = t;
match s {
"\"CHAR\"" => Ok(Self::Char),
"INT2" => Ok(Self::Int2),
"INT4" => Ok(Self::Int4),
"INT8" => Ok(Self::Int8),
"FLOAT4" => Ok(Self::Float4),
"FLOAT8" => Ok(Self::Float8),
"VARCHAR" => Ok(Self::Varchar),
#[cfg(feature = "chrono")]
"DATE" => Ok(Self::Date),
#[cfg(not(feature = "chrono"))]
"DATE" => Err(Error::ColumnDecode {
index: format!("{:?}", index),
source: format!("unknown SQL type `{}`, should enable chrono feature", s).into(),
}),
#[cfg(feature = "bigdecimal")]
"NUMERIC" => Ok(Self::Numeric),
#[cfg(not(feature = "bigdecimal"))]
"NUMERIC" => Err(Error::ColumnDecode {
index: format!("{:?}", index),
source: format!("unknown SQL type `{}`, should enable bigdecimal feature", s)
.into(),
}),
#[cfg(feature = "uuid")]
"UUID" => Ok(Self::Uuid),
#[cfg(not(feature = "uuid"))]
"UUID" => Err(Error::ColumnDecode {
index: format!("{:?}", index),
source: format!("unknown SQL type `{}`, should enable uuid feature", s).into(),
}),
_ => Err(Error::ColumnDecode {
index: format!("{:?}", index),
source: format!("unknown SQL type `{}`", s).into(),
}),
}
}
}
#[derive(PartialEq, Debug, Clone)]
pub enum ClickhousePgValue {
Char(i8),
I16(i16),
I32(i32),
I64(i64),
F32(f32),
F64(f64),
String(String),
#[cfg(feature = "chrono")]
NaiveDate(NaiveDate),
#[cfg(feature = "bigdecimal")]
BigDecimal(BigDecimal),
#[cfg(feature = "uuid")]
Uuid(Uuid),
}
impl From<i8> for ClickhousePgValue {
fn from(val: i8) -> Self {
Self::Char(val)
}
}
impl From<i16> for ClickhousePgValue {
fn from(val: i16) -> Self {
Self::I16(val)
}
}
impl From<u8> for ClickhousePgValue {
fn from(val: u8) -> Self {
Self::I16(val.into())
}
}
impl From<i32> for ClickhousePgValue {
fn from(val: i32) -> Self {
Self::I32(val)
}
}
impl From<u16> for ClickhousePgValue {
fn from(val: u16) -> Self {
Self::I32(val.into())
}
}
impl From<i64> for ClickhousePgValue {
fn from(val: i64) -> Self {
Self::I64(val)
}
}
impl From<u32> for ClickhousePgValue {
fn from(val: u32) -> Self {
Self::I64(val.into())
}
}
impl From<f32> for ClickhousePgValue {
fn from(val: f32) -> Self {
Self::F32(val)
}
}
impl From<f64> for ClickhousePgValue {
fn from(val: f64) -> Self {
Self::F64(val)
}
}
impl From<String> for ClickhousePgValue {
fn from(val: String) -> Self {
Self::String(val)
}
}
impl From<&str> for ClickhousePgValue {
fn from(val: &str) -> Self {
Self::String(val.into())
}
}
#[cfg(feature = "chrono")]
impl From<NaiveDate> for ClickhousePgValue {
fn from(val: NaiveDate) -> Self {
Self::NaiveDate(val)
}
}
#[cfg(feature = "bigdecimal")]
impl From<BigDecimal> for ClickhousePgValue {
fn from(val: BigDecimal) -> Self {
Self::BigDecimal(val)
}
}
#[cfg(feature = "uuid")]
impl From<Uuid> for ClickhousePgValue {
fn from(val: Uuid) -> Self {
Self::Uuid(val)
}
}