1use crate::{
2 cond::{Eq, Gt, Lt},
3 driver::{Driver, PushPrql},
4 sort::{Order, Sort},
5};
6
7pub fn col(name: &'static str) -> ColumnName {
8 ColumnName { name }
9}
10
11pub fn json<Col>(col: Col) -> Json<Col> {
12 Json { col }
13}
14
15#[derive(Clone, Copy, Debug, Eq, PartialEq)]
16pub struct ColumnName {
17 pub name: &'static str,
18}
19
20impl ColumnName {
21 pub fn asc(self) -> Sort<Self> {
22 Sort {
23 order: Order::Asc,
24 by: self,
25 }
26 }
27
28 pub fn desc(self) -> Sort<Self> {
29 Sort {
30 order: Order::Desc,
31 by: self,
32 }
33 }
34
35 pub fn eq<RHS>(&self, rhs: RHS) -> Eq<&Self, RHS> {
36 Eq { lhs: self, rhs }
37 }
38
39 pub fn gt<RHS>(&self, rhs: RHS) -> Gt<&Self, RHS> {
40 Gt { lhs: self, rhs }
41 }
42
43 pub fn lt<RHS>(&self, rhs: RHS) -> Lt<&Self, RHS> {
44 Lt { lhs: self, rhs }
45 }
46}
47
48impl PushPrql for ColumnName {
49 fn push_to_driver(&self, driver: &mut Driver) {
50 driver.push(self.name);
51 }
52}
53
54#[derive(Clone, Copy, Debug, Eq, PartialEq)]
55pub struct Json<Col> {
56 pub col: Col,
57}
58
59impl<Col> Json<Col> {
60 pub fn at(self, n: usize) -> JsonAccessor<Col> {
61 JsonAccessor {
62 col: self.col,
63 op: JsonOp::At(n),
64 }
65 }
66
67 pub fn get(self, k: &'static str) -> JsonAccessor<Col> {
68 JsonAccessor {
69 col: self.col,
70 op: JsonOp::Get(k),
71 }
72 }
73
74 pub fn get_text(self, k: &'static str) -> JsonAccessor<Col> {
75 JsonAccessor {
76 col: self.col,
77 op: JsonOp::GetText(k),
78 }
79 }
80}
81
82#[derive(Clone, Copy, Debug, Eq, PartialEq)]
83pub enum JsonOp {
84 At(usize), Get(&'static str), GetText(&'static str), }
88
89#[derive(Clone, Copy, Debug, Eq, PartialEq)]
90pub struct JsonAccessor<Col> {
91 pub col: Col,
92 pub op: JsonOp,
93}
94
95impl<Col> JsonAccessor<Col> {
96 pub fn asc(self) -> Sort<Self> {
97 Sort {
98 order: Order::Asc,
99 by: self,
100 }
101 }
102
103 pub fn desc(self) -> Sort<Self> {
104 Sort {
105 order: Order::Desc,
106 by: self,
107 }
108 }
109
110 pub fn eq<RHS>(&self, rhs: RHS) -> Eq<&Self, RHS> {
111 Eq { lhs: self, rhs }
112 }
113
114 pub fn gt<RHS>(&self, rhs: RHS) -> Gt<&Self, RHS> {
115 Gt { lhs: self, rhs }
116 }
117
118 pub fn lt<RHS>(&self, rhs: RHS) -> Lt<&Self, RHS> {
119 Lt { lhs: self, rhs }
120 }
121}
122
123impl<Col> PushPrql for JsonAccessor<Col>
124where
125 Col: PushPrql,
126{
127 fn push_to_driver(&self, driver: &mut Driver) {
128 driver.push("s\"");
129 self.col.push_to_driver(driver);
130 match self.op {
131 JsonOp::At(n) => {
132 driver.push("->");
133 driver.push(n);
134 }
135 JsonOp::Get(k) => {
136 driver.push("->'");
137 driver.push(k);
138 driver.push("'");
139 }
140 JsonOp::GetText(k) => {
141 driver.push("->>'");
142 driver.push(k);
143 driver.push("'");
144 }
145 }
146 driver.push('"');
147 }
148}