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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
use super::scalar::*;
use crate::{Filter, JsonCompare, ScalarFieldRef, ScalarFilter};

#[derive(Debug, Clone, Eq, Hash, PartialEq)]
pub enum JsonTargetType {
    String,
    Array,
}

#[derive(Debug, Clone, Eq, Hash, PartialEq)]
pub enum JsonFilterPath {
    String(String),
    Array(Vec<String>),
}

impl JsonCompare for ScalarFieldRef {
    fn json_contains<T>(&self, value: T, path: Option<JsonFilterPath>, target_type: JsonTargetType) -> Filter
    where
        T: Into<ConditionValue>,
    {
        Filter::from(ScalarFilter {
            condition: ScalarCondition::JsonCompare(JsonCondition {
                condition: Box::new(ScalarCondition::Contains(value.into())),
                path,
                target_type: Some(target_type),
            }),
            projection: ScalarProjection::Single(self.clone()),
            mode: QueryMode::Default,
        })
    }

    fn json_not_contains<T>(&self, value: T, path: Option<JsonFilterPath>, target_type: JsonTargetType) -> Filter
    where
        T: Into<ConditionValue>,
    {
        Filter::from(ScalarFilter {
            condition: ScalarCondition::JsonCompare(JsonCondition {
                condition: Box::new(ScalarCondition::NotContains(value.into())),
                path,
                target_type: Some(target_type),
            }),
            projection: ScalarProjection::Single(self.clone()),
            mode: QueryMode::Default,
        })
    }

    fn json_starts_with<T>(&self, value: T, path: Option<JsonFilterPath>, target_type: JsonTargetType) -> Filter
    where
        T: Into<ConditionValue>,
    {
        Filter::from(ScalarFilter {
            condition: ScalarCondition::JsonCompare(JsonCondition {
                condition: Box::new(ScalarCondition::StartsWith(value.into())),
                path,
                target_type: Some(target_type),
            }),
            projection: ScalarProjection::Single(self.clone()),
            mode: QueryMode::Default,
        })
    }

    fn json_not_starts_with<T>(&self, value: T, path: Option<JsonFilterPath>, target_type: JsonTargetType) -> Filter
    where
        T: Into<ConditionValue>,
    {
        Filter::from(ScalarFilter {
            condition: ScalarCondition::JsonCompare(JsonCondition {
                condition: Box::new(ScalarCondition::NotStartsWith(value.into())),
                path,
                target_type: Some(target_type),
            }),
            projection: ScalarProjection::Single(self.clone()),
            mode: QueryMode::Default,
        })
    }

    fn json_ends_with<T>(&self, value: T, path: Option<JsonFilterPath>, target_type: JsonTargetType) -> Filter
    where
        T: Into<ConditionValue>,
    {
        Filter::from(ScalarFilter {
            condition: ScalarCondition::JsonCompare(JsonCondition {
                condition: Box::new(ScalarCondition::EndsWith(value.into())),
                path,
                target_type: Some(target_type),
            }),
            projection: ScalarProjection::Single(self.clone()),
            mode: QueryMode::Default,
        })
    }

    fn json_not_ends_with<T>(&self, value: T, path: Option<JsonFilterPath>, target_type: JsonTargetType) -> Filter
    where
        T: Into<ConditionValue>,
    {
        Filter::from(ScalarFilter {
            condition: ScalarCondition::JsonCompare(JsonCondition {
                condition: Box::new(ScalarCondition::NotEndsWith(value.into())),
                path,
                target_type: Some(target_type),
            }),
            projection: ScalarProjection::Single(self.clone()),
            mode: QueryMode::Default,
        })
    }

    fn json_equals<T>(&self, value: T, path: Option<JsonFilterPath>) -> Filter
    where
        T: Into<ConditionValue>,
    {
        Filter::from(ScalarFilter {
            condition: ScalarCondition::JsonCompare(JsonCondition {
                condition: Box::new(ScalarCondition::Equals(value.into())),
                path,
                target_type: None,
            }),
            projection: ScalarProjection::Single(self.clone()),
            mode: QueryMode::Default,
        })
    }

    fn json_not_equals<T>(&self, value: T, path: Option<JsonFilterPath>) -> Filter
    where
        T: Into<ConditionValue>,
    {
        Filter::from(ScalarFilter {
            condition: ScalarCondition::JsonCompare(JsonCondition {
                condition: Box::new(ScalarCondition::NotEquals(value.into())),
                path,
                target_type: None,
            }),
            projection: ScalarProjection::Single(self.clone()),
            mode: QueryMode::Default,
        })
    }

    fn json_less_than<T>(&self, value: T, path: Option<JsonFilterPath>) -> Filter
    where
        T: Into<ConditionValue>,
    {
        Filter::from(ScalarFilter {
            condition: ScalarCondition::JsonCompare(JsonCondition {
                condition: Box::new(ScalarCondition::LessThan(value.into())),
                path,
                target_type: None,
            }),
            projection: ScalarProjection::Single(self.clone()),
            mode: QueryMode::Default,
        })
    }

    fn json_less_than_or_equals<T>(&self, value: T, path: Option<JsonFilterPath>) -> Filter
    where
        T: Into<ConditionValue>,
    {
        Filter::from(ScalarFilter {
            condition: ScalarCondition::JsonCompare(JsonCondition {
                condition: Box::new(ScalarCondition::LessThanOrEquals(value.into())),
                path,
                target_type: None,
            }),
            projection: ScalarProjection::Single(self.clone()),
            mode: QueryMode::Default,
        })
    }

    fn json_greater_than<T>(&self, value: T, path: Option<JsonFilterPath>) -> Filter
    where
        T: Into<ConditionValue>,
    {
        Filter::from(ScalarFilter {
            condition: ScalarCondition::JsonCompare(JsonCondition {
                condition: Box::new(ScalarCondition::GreaterThan(value.into())),
                path,
                target_type: None,
            }),
            projection: ScalarProjection::Single(self.clone()),
            mode: QueryMode::Default,
        })
    }

    fn json_greater_than_or_equals<T>(&self, value: T, path: Option<JsonFilterPath>) -> Filter
    where
        T: Into<ConditionValue>,
    {
        Filter::from(ScalarFilter {
            condition: ScalarCondition::JsonCompare(JsonCondition {
                condition: Box::new(ScalarCondition::GreaterThanOrEquals(value.into())),
                path,
                target_type: None,
            }),
            projection: ScalarProjection::Single(self.clone()),
            mode: QueryMode::Default,
        })
    }
}