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
use holochain_zome_types::prelude::*;
use rusqlite::types::ToSqlOutput;
#[cfg(test)]
mod test;
pub trait AsSql<'a> {
fn as_sql(&'a self) -> SqlOutput<'a>;
}
pub trait ToSqlStatement {
fn to_sql_statement(&self) -> String;
}
#[derive(Clone, Debug, PartialEq)]
pub struct SqlOutput<'a>(pub ToSqlOutput<'a>);
impl<'a> rusqlite::ToSql for SqlOutput<'a> {
fn to_sql(&self) -> rusqlite::Result<ToSqlOutput<'_>> {
rusqlite::ToSql::to_sql(&self.0)
}
}
impl<'a, T> AsSql<'a> for T
where
SqlOutput<'a>: From<&'a T>,
T: 'a,
{
fn as_sql(&'a self) -> SqlOutput {
self.into()
}
}
impl<'a, T> AsSql<'a> for Option<T>
where
SqlOutput<'a>: From<&'a T>,
T: 'a,
{
fn as_sql(&'a self) -> SqlOutput {
match self {
Some(d) => d.into(),
None => SqlOutput(ToSqlOutput::Owned(rusqlite::types::Value::Null)),
}
}
}
fn via_display(data: &impl std::fmt::Display) -> SqlOutput {
SqlOutput(ToSqlOutput::Owned(data.to_string().into()))
}
impl<'a> From<&'a ActionType> for SqlOutput<'a> {
fn from(d: &'a ActionType) -> Self {
via_display(d)
}
}
impl<'a> From<&'a EntryType> for SqlOutput<'a> {
fn from(d: &'a EntryType) -> Self {
via_display(d)
}
}
impl<'a> From<&'a LinkTag> for SqlOutput<'a> {
fn from(d: &'a LinkTag) -> Self {
SqlOutput(ToSqlOutput::Borrowed((&d.0[..]).into()))
}
}
impl<'a, 'b> From<&'b ZomeId> for SqlOutput<'a> {
fn from(d: &'b ZomeId) -> Self {
Self(d.0.into())
}
}
impl ToSqlStatement for LinkTypeFilter {
fn to_sql_statement(&self) -> String {
match self {
LinkTypeFilter::Types(types) => {
match types
.first()
.filter(|(_, t)| types.len() == 1 && t.len() == 1)
.and_then(|(z, t)| t.first().map(|t| (z, t)))
{
Some((zome_id, link_type)) => {
format!(
" AND zome_id = {} AND link_type = {} ",
zome_id.0, link_type.0
)
}
_ => {
let mut out = types
.iter()
.flat_map(|(zome_id, types)| {
let mut types: Vec<String> = types
.iter()
.flat_map(|t| {
[format!(" link_type = {} ", t.0), "OR".to_string()]
})
.collect();
types.pop();
[
format!(
" ( zome_id = {} AND ({}) ) ",
zome_id.0,
types.into_iter().collect::<String>()
),
"OR".to_string(),
]
})
.collect::<Vec<_>>();
out.pop();
if out.is_empty() {
String::new()
} else {
format!(" AND ({}) ", out.into_iter().collect::<String>())
}
}
}
}
LinkTypeFilter::Dependencies(dependencies) => {
let mut out = dependencies
.iter()
.flat_map(|z| [format!(" zome_id = {} ", z.0), "OR".to_string()])
.collect::<Vec<_>>();
out.pop();
if out.is_empty() {
String::new()
} else {
format!(" AND ({}) ", out.into_iter().collect::<String>())
}
}
}
}
}