icydb_core/db/query/builder/
field.rs1use crate::{
2 db::query::predicate::{CoercionId, CoercionSpec, CompareOp, ComparePredicate, Predicate},
3 traits::FieldValue,
4 value::Value,
5};
6
7#[derive(Clone, Copy, Eq, Hash, PartialEq)]
16pub struct FieldRef(&'static str);
17
18impl FieldRef {
19 #[must_use]
21 pub const fn new(name: &'static str) -> Self {
22 Self(name)
23 }
24
25 #[must_use]
27 pub const fn as_str(self) -> &'static str {
28 self.0
29 }
30
31 #[must_use]
37 pub fn eq(self, value: impl FieldValue) -> Predicate {
38 Predicate::Compare(ComparePredicate::with_coercion(
39 self.0,
40 CompareOp::Eq,
41 value.to_value(),
42 CoercionId::Strict,
43 ))
44 }
45
46 #[must_use]
48 pub fn text_eq_ci(self, value: impl FieldValue) -> Predicate {
49 Predicate::Compare(ComparePredicate::with_coercion(
50 self.0,
51 CompareOp::Eq,
52 value.to_value(),
53 CoercionId::TextCasefold,
54 ))
55 }
56
57 #[must_use]
59 pub fn ne(self, value: impl FieldValue) -> Predicate {
60 Predicate::Compare(ComparePredicate::with_coercion(
61 self.0,
62 CompareOp::Ne,
63 value.to_value(),
64 CoercionId::Strict,
65 ))
66 }
67
68 #[must_use]
70 pub fn lt(self, value: impl FieldValue) -> Predicate {
71 Predicate::Compare(ComparePredicate::with_coercion(
72 self.0,
73 CompareOp::Lt,
74 value.to_value(),
75 CoercionId::NumericWiden,
76 ))
77 }
78
79 #[must_use]
81 pub fn lte(self, value: impl FieldValue) -> Predicate {
82 Predicate::Compare(ComparePredicate::with_coercion(
83 self.0,
84 CompareOp::Lte,
85 value.to_value(),
86 CoercionId::NumericWiden,
87 ))
88 }
89
90 #[must_use]
92 pub fn gt(self, value: impl FieldValue) -> Predicate {
93 Predicate::Compare(ComparePredicate::with_coercion(
94 self.0,
95 CompareOp::Gt,
96 value.to_value(),
97 CoercionId::NumericWiden,
98 ))
99 }
100
101 #[must_use]
103 pub fn gte(self, value: impl FieldValue) -> Predicate {
104 Predicate::Compare(ComparePredicate::with_coercion(
105 self.0,
106 CompareOp::Gte,
107 value.to_value(),
108 CoercionId::NumericWiden,
109 ))
110 }
111
112 #[must_use]
114 pub fn in_list<I, V>(self, values: I) -> Predicate
115 where
116 I: IntoIterator<Item = V>,
117 V: FieldValue,
118 {
119 Predicate::Compare(ComparePredicate::with_coercion(
120 self.0,
121 CompareOp::In,
122 Value::List(values.into_iter().map(|v| v.to_value()).collect()),
123 CoercionId::Strict,
124 ))
125 }
126
127 #[must_use]
133 pub fn is_null(self) -> Predicate {
134 Predicate::IsNull {
135 field: self.0.to_string(),
136 }
137 }
138
139 #[must_use]
141 pub fn is_missing(self) -> Predicate {
142 Predicate::IsMissing {
143 field: self.0.to_string(),
144 }
145 }
146
147 #[must_use]
149 pub fn is_empty(self) -> Predicate {
150 Predicate::IsEmpty {
151 field: self.0.to_string(),
152 }
153 }
154
155 #[must_use]
157 pub fn is_not_empty(self) -> Predicate {
158 Predicate::IsNotEmpty {
159 field: self.0.to_string(),
160 }
161 }
162
163 #[must_use]
169 pub fn map_contains_key(self, key: impl FieldValue, coercion: CoercionId) -> Predicate {
170 Predicate::MapContainsKey {
171 field: self.0.to_string(),
172 key: key.to_value(),
173 coercion: CoercionSpec::new(coercion),
174 }
175 }
176
177 #[must_use]
179 pub fn map_contains_value(self, value: impl FieldValue, coercion: CoercionId) -> Predicate {
180 Predicate::MapContainsValue {
181 field: self.0.to_string(),
182 value: value.to_value(),
183 coercion: CoercionSpec::new(coercion),
184 }
185 }
186
187 #[must_use]
189 pub fn map_contains_entry(
190 self,
191 key: impl FieldValue,
192 value: impl FieldValue,
193 coercion: CoercionId,
194 ) -> Predicate {
195 Predicate::MapContainsEntry {
196 field: self.0.to_string(),
197 key: key.to_value(),
198 value: value.to_value(),
199 coercion: CoercionSpec::new(coercion),
200 }
201 }
202
203 #[must_use]
205 pub fn text_contains(self, value: impl FieldValue) -> Predicate {
206 Predicate::TextContains {
207 field: self.0.to_string(),
208 value: value.to_value(),
209 }
210 }
211
212 #[must_use]
214 pub fn text_contains_ci(self, value: impl FieldValue) -> Predicate {
215 Predicate::TextContainsCi {
216 field: self.0.to_string(),
217 value: value.to_value(),
218 }
219 }
220}
221
222impl AsRef<str> for FieldRef {
227 fn as_ref(&self) -> &str {
228 self.0
229 }
230}
231
232impl std::ops::Deref for FieldRef {
233 type Target = str;
234
235 fn deref(&self) -> &Self::Target {
236 self.0
237 }
238}