-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcolumn.rs
More file actions
84 lines (73 loc) · 2.38 KB
/
Copy pathcolumn.rs
File metadata and controls
84 lines (73 loc) · 2.38 KB
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
use cipherstash_client::schema::{ColumnConfig, ColumnType};
use eql_mapper::EqlTermVariant;
use postgres_types::Type;
use crate::Identifier;
#[derive(Debug, Clone, PartialEq)]
pub struct Column {
pub identifier: Identifier,
pub config: ColumnConfig,
pub postgres_type: Type,
pub eql_term: EqlTermVariant,
}
impl Column {
pub fn new(
identifier: Identifier,
config: ColumnConfig,
postgres_type: Option<Type>,
eql_term: EqlTermVariant,
) -> Column {
let postgres_type =
postgres_type.unwrap_or(column_type_to_postgres_type(&config.cast_type, eql_term));
Column {
identifier,
config,
postgres_type,
eql_term,
}
}
pub fn table_name(&self) -> String {
self.identifier.table.to_owned()
}
pub fn column_name(&self) -> String {
self.identifier.column.to_owned()
}
pub fn oid(&self) -> u32 {
self.postgres_type.oid()
}
pub fn cast_type(&self) -> ColumnType {
self.config.cast_type
}
pub fn eql_term(&self) -> EqlTermVariant {
self.eql_term
}
pub fn is_encryptable(&self) -> bool {
matches!(
self.eql_term,
EqlTermVariant::Full | EqlTermVariant::Partial
)
}
}
///
/// Maps a configured index type to a Postgres Type
///
/// JSONAccessors are mapped to a string for the client, but are JSONB for the server
///
fn column_type_to_postgres_type(
col_type: &ColumnType,
eql_term: EqlTermVariant,
) -> postgres_types::Type {
match (col_type, eql_term) {
(ColumnType::Boolean, _) => postgres_types::Type::BOOL,
(ColumnType::BigInt, _) => postgres_types::Type::INT8,
(ColumnType::BigUInt, _) => postgres_types::Type::INT8,
(ColumnType::Date, _) => postgres_types::Type::DATE,
(ColumnType::Decimal, _) => postgres_types::Type::NUMERIC,
(ColumnType::Float, _) => postgres_types::Type::FLOAT8,
(ColumnType::Int, _) => postgres_types::Type::INT4,
(ColumnType::SmallInt, _) => postgres_types::Type::INT2,
(ColumnType::Timestamp, _) => postgres_types::Type::TIMESTAMPTZ,
(ColumnType::Text, _) => postgres_types::Type::TEXT,
(ColumnType::Json, EqlTermVariant::JsonAccessor) => postgres_types::Type::TEXT,
(ColumnType::Json, _) => postgres_types::Type::JSONB,
}
}