@@ -19,6 +19,7 @@ tests = do
19
19
, selectedColumns = SelectAll
20
20
, whereCondition = Nothing
21
21
, orderByClause = []
22
+ , distinctOnColumn = Nothing
22
23
, limit = Nothing
23
24
, offset = Nothing
24
25
}
@@ -27,12 +28,13 @@ tests = do
27
28
( " SELECT ? FROM ?"
28
29
, [PG. Plain " *" , PG. EscapeIdentifier " posts" ]
29
30
)
30
-
31
+
31
32
it " compile a select query with order by" do
32
33
let query = DynamicSQLQuery
33
34
{ table = " posts"
34
35
, selectedColumns = SelectAll
35
36
, whereCondition = Nothing
37
+ , distinctOnColumn = Nothing
36
38
, orderByClause = [OrderByClause { orderByColumn = " title" , orderByDirection = Desc }]
37
39
, limit = Nothing
38
40
, offset = Nothing
@@ -42,7 +44,7 @@ tests = do
42
44
( " SELECT ? FROM ? ORDER BY ? ?"
43
45
, [PG. Plain " *" , PG. EscapeIdentifier " posts" , PG. EscapeIdentifier " title" , PG. Plain " DESC" ]
44
46
)
45
-
47
+
46
48
it " compile a select query with multiple order bys" do
47
49
let query = DynamicSQLQuery
48
50
{ table = " posts"
@@ -52,6 +54,7 @@ tests = do
52
54
OrderByClause { orderByColumn = " createdAt" , orderByDirection = Desc },
53
55
OrderByClause { orderByColumn = " title" , orderByDirection = Asc }
54
56
]
57
+ , distinctOnColumn = Nothing
55
58
, limit = Nothing
56
59
, offset = Nothing
57
60
}
@@ -60,13 +63,14 @@ tests = do
60
63
( " SELECT ? FROM ? ORDER BY ? ?, ? ?"
61
64
, [PG. Plain " *" , PG. EscapeIdentifier " posts" , PG. EscapeIdentifier " created_at" , PG. Plain " DESC" , PG. EscapeIdentifier " title" , PG. Plain " " ]
62
65
)
63
-
66
+
64
67
it " compile a basic select query with a where condition" do
65
68
let query = DynamicSQLQuery
66
69
{ table = " posts"
67
70
, selectedColumns = SelectAll
68
71
, whereCondition = Just $ InfixOperatorExpression (ColumnExpression " userId" ) OpEqual (LiteralExpression (TextValue " b8553ce9-6a42-4a68-b5fc-259be3e2acdc" ))
69
72
, orderByClause = []
73
+ , distinctOnColumn = Nothing
70
74
, limit = Nothing
71
75
, offset = Nothing
72
76
}
@@ -82,6 +86,7 @@ tests = do
82
86
, selectedColumns = SelectAll
83
87
, whereCondition = Just $ InfixOperatorExpression (ColumnExpression " userId" ) OpEqual (LiteralExpression (TextValue " b8553ce9-6a42-4a68-b5fc-259be3e2acdc" ))
84
88
, orderByClause = [ OrderByClause { orderByColumn = " createdAt" , orderByDirection = Desc } ]
89
+ , distinctOnColumn = Nothing
85
90
, limit = Nothing
86
91
, offset = Nothing
87
92
}
@@ -96,6 +101,7 @@ tests = do
96
101
{ table = " posts"
97
102
, selectedColumns = SelectAll
98
103
, whereCondition = Just $ InfixOperatorExpression (ColumnExpression " userId" ) OpEqual (LiteralExpression (TextValue " b8553ce9-6a42-4a68-b5fc-259be3e2acdc" ))
104
+ , distinctOnColumn = Nothing
99
105
, orderByClause = []
100
106
, limit = Just 50
101
107
, offset = Nothing
@@ -112,6 +118,7 @@ tests = do
112
118
, selectedColumns = SelectAll
113
119
, whereCondition = Just $ InfixOperatorExpression (ColumnExpression " userId" ) OpEqual (LiteralExpression (TextValue " b8553ce9-6a42-4a68-b5fc-259be3e2acdc" ))
114
120
, orderByClause = []
121
+ , distinctOnColumn = Nothing
115
122
, limit = Nothing
116
123
, offset = Just 50
117
124
}
@@ -127,25 +134,27 @@ tests = do
127
134
, selectedColumns = SelectAll
128
135
, whereCondition = Just $ InfixOperatorExpression (ColumnExpression " userId" ) OpEqual (LiteralExpression (TextValue " b8553ce9-6a42-4a68-b5fc-259be3e2acdc" ))
129
136
, orderByClause = []
137
+ , distinctOnColumn = Nothing
130
138
, limit = Just 25
131
139
, offset = Just 50
132
140
}
133
-
141
+
134
142
compileQuery query `shouldBe`
135
143
( " SELECT ? FROM ? WHERE (?) = (?) LIMIT ? OFFSET ?"
136
144
, [PG. Plain " *" , PG. EscapeIdentifier " posts" , PG. EscapeIdentifier " user_id" , PG. Escape " b8553ce9-6a42-4a68-b5fc-259be3e2acdc" , PG. Plain " 25" , PG. Plain " 50" ]
137
145
)
138
-
146
+
139
147
it " compile 'field = NULL' conditions to 'field IS NULL'" do
140
148
let query = DynamicSQLQuery
141
149
{ table = " posts"
142
150
, selectedColumns = SelectAll
143
151
, whereCondition = Just $ InfixOperatorExpression (ColumnExpression " userId" ) OpEqual NullExpression
144
152
, orderByClause = []
153
+ , distinctOnColumn = Nothing
145
154
, limit = Nothing
146
155
, offset = Nothing
147
156
}
148
-
157
+
149
158
compileQuery query `shouldBe`
150
159
( " SELECT ? FROM ? WHERE (?) IS NULL"
151
160
, [PG. Plain " *" , PG. EscapeIdentifier " posts" , PG. EscapeIdentifier " user_id" ]
@@ -157,10 +166,11 @@ tests = do
157
166
, selectedColumns = SelectAll
158
167
, whereCondition = Just $ InfixOperatorExpression (ColumnExpression " userId" ) OpNotEqual NullExpression
159
168
, orderByClause = []
169
+ , distinctOnColumn = Nothing
160
170
, limit = Nothing
161
171
, offset = Nothing
162
172
}
163
-
173
+
164
174
compileQuery query `shouldBe`
165
175
( " SELECT ? FROM ? WHERE (?) IS NOT NULL"
166
176
, [PG. Plain " *" , PG. EscapeIdentifier " posts" , PG. EscapeIdentifier " user_id" ]
@@ -172,11 +182,28 @@ tests = do
172
182
, selectedColumns = SelectAll
173
183
, whereCondition = Just $ InfixOperatorExpression (ColumnExpression " ts" ) OpTSMatch (CallExpression { functionCall = ToTSQuery { text = " test" }})
174
184
, orderByClause = [ OrderByTSRank { tsvector = " ts" , tsquery = " test" } ]
185
+ , distinctOnColumn = Nothing
175
186
, limit = Nothing
176
187
, offset = Nothing
177
188
}
178
-
189
+
179
190
compileQuery query `shouldBe`
180
191
( " SELECT ? FROM ? WHERE (?) @@ (to_tsquery('english', ?)) ORDER BY ts_rank(?, to_tsquery('english', ?))"
181
192
, [PG. Plain " *" , PG. EscapeIdentifier " products" , PG. EscapeIdentifier " ts" , PG. Escape " test" , PG. EscapeIdentifier " ts" , PG. Escape " test" ]
182
- )
193
+ )
194
+
195
+ it " compile a basic select query with distinctOn" do
196
+ let query = DynamicSQLQuery
197
+ { table = " posts"
198
+ , selectedColumns = SelectAll
199
+ , whereCondition = Nothing
200
+ , orderByClause = []
201
+ , distinctOnColumn = Just " groupId"
202
+ , limit = Nothing
203
+ , offset = Nothing
204
+ }
205
+
206
+ compileQuery query `shouldBe`
207
+ ( " SELECT DISTINCT ON (?) ? FROM ?"
208
+ , [PG. EscapeIdentifier " group_id" , PG. Plain " *" , PG. EscapeIdentifier " posts" ]
209
+ )
0 commit comments