icydb_core/db/query/builder/
field.rs1use crate::{
7 db::predicate::{CoercionId, CompareOp, ComparePredicate, Predicate},
8 traits::FieldValue,
9 value::Value,
10};
11use derive_more::Deref;
12
13#[derive(Clone, Copy, Deref, Eq, Hash, PartialEq)]
22pub struct FieldRef(&'static str);
23
24impl FieldRef {
25 #[must_use]
27 pub const fn new(name: &'static str) -> Self {
28 Self(name)
29 }
30
31 #[must_use]
33 pub const fn as_str(self) -> &'static str {
34 self.0
35 }
36
37 #[must_use]
43 pub fn eq(self, value: impl FieldValue) -> Predicate {
44 Predicate::Compare(ComparePredicate::with_coercion(
45 self.0,
46 CompareOp::Eq,
47 value.to_value(),
48 CoercionId::Strict,
49 ))
50 }
51
52 #[must_use]
54 pub fn text_eq_ci(self, value: impl FieldValue) -> Predicate {
55 Predicate::Compare(ComparePredicate::with_coercion(
56 self.0,
57 CompareOp::Eq,
58 value.to_value(),
59 CoercionId::TextCasefold,
60 ))
61 }
62
63 #[must_use]
65 pub fn ne(self, value: impl FieldValue) -> Predicate {
66 Predicate::Compare(ComparePredicate::with_coercion(
67 self.0,
68 CompareOp::Ne,
69 value.to_value(),
70 CoercionId::Strict,
71 ))
72 }
73
74 #[must_use]
76 pub fn lt(self, value: impl FieldValue) -> Predicate {
77 Predicate::Compare(ComparePredicate::with_coercion(
78 self.0,
79 CompareOp::Lt,
80 value.to_value(),
81 CoercionId::NumericWiden,
82 ))
83 }
84
85 #[must_use]
87 pub fn lte(self, value: impl FieldValue) -> Predicate {
88 Predicate::Compare(ComparePredicate::with_coercion(
89 self.0,
90 CompareOp::Lte,
91 value.to_value(),
92 CoercionId::NumericWiden,
93 ))
94 }
95
96 #[must_use]
98 pub fn gt(self, value: impl FieldValue) -> Predicate {
99 Predicate::Compare(ComparePredicate::with_coercion(
100 self.0,
101 CompareOp::Gt,
102 value.to_value(),
103 CoercionId::NumericWiden,
104 ))
105 }
106
107 #[must_use]
109 pub fn gte(self, value: impl FieldValue) -> Predicate {
110 Predicate::Compare(ComparePredicate::with_coercion(
111 self.0,
112 CompareOp::Gte,
113 value.to_value(),
114 CoercionId::NumericWiden,
115 ))
116 }
117
118 #[must_use]
120 pub fn in_list<I, V>(self, values: I) -> Predicate
121 where
122 I: IntoIterator<Item = V>,
123 V: FieldValue,
124 {
125 Predicate::Compare(ComparePredicate::with_coercion(
126 self.0,
127 CompareOp::In,
128 Value::List(values.into_iter().map(|v| v.to_value()).collect()),
129 CoercionId::Strict,
130 ))
131 }
132
133 #[must_use]
139 pub fn is_null(self) -> Predicate {
140 Predicate::IsNull {
141 field: self.0.to_string(),
142 }
143 }
144
145 #[must_use]
147 pub fn is_missing(self) -> Predicate {
148 Predicate::IsMissing {
149 field: self.0.to_string(),
150 }
151 }
152
153 #[must_use]
155 pub fn is_empty(self) -> Predicate {
156 Predicate::IsEmpty {
157 field: self.0.to_string(),
158 }
159 }
160
161 #[must_use]
163 pub fn is_not_empty(self) -> Predicate {
164 Predicate::IsNotEmpty {
165 field: self.0.to_string(),
166 }
167 }
168
169 #[must_use]
171 pub fn text_contains(self, value: impl FieldValue) -> Predicate {
172 Predicate::TextContains {
173 field: self.0.to_string(),
174 value: value.to_value(),
175 }
176 }
177
178 #[must_use]
180 pub fn text_contains_ci(self, value: impl FieldValue) -> Predicate {
181 Predicate::TextContainsCi {
182 field: self.0.to_string(),
183 value: value.to_value(),
184 }
185 }
186}
187
188impl AsRef<str> for FieldRef {
193 fn as_ref(&self) -> &str {
194 self.0
195 }
196}