@@ -198,7 +198,24 @@ pub async fn global_search(
198
198
on u.id = tu.user_id
199
199
where
200
200
(?3 = true or u.id = ?4)
201
- and (d.name like ?2 or hex(d.dev_eui) like ?2 or hex(d.dev_addr) like ?2)
201
+ and (
202
+ d.name like ?2 or hex(d.dev_eui) like ?2 or hex(d.dev_addr) like ?2
203
+ or (
204
+ ?7 != '{}'
205
+ and 0 = (
206
+ -- this makes sure tags are present
207
+ -- by counting number of different top level json values
208
+ select
209
+ count(*)
210
+ from json_each(?7) search_tag
211
+ left join json_each(d.tags) item_tag
212
+ on search_tag.key = item_tag.key
213
+ where
214
+ -- `is not` is like `!=` but handles null
215
+ search_tag.value is not item_tag.value
216
+ )
217
+ )
218
+ )
202
219
-- gateway
203
220
union
204
221
select
@@ -222,7 +239,24 @@ pub async fn global_search(
222
239
on u.id = tu.user_id
223
240
where
224
241
(?3 = true or u.id = ?4)
225
- and (g.name like ?2 or hex(g.gateway_id) like ?2)
242
+ and (
243
+ g.name like ?2 or hex(g.gateway_id) like ?2
244
+ or (
245
+ ?7 != '{}'
246
+ and 0 = (
247
+ -- this makes sure tags are present
248
+ -- by counting number of different top level json values
249
+ select
250
+ count(*)
251
+ from json_each(?7) search_tag
252
+ left join json_each(g.tags) item_tag
253
+ on search_tag.key = item_tag.key
254
+ where
255
+ -- `is not` is like `!=` but handles null
256
+ search_tag.value is not item_tag.value
257
+ )
258
+ )
259
+ )
226
260
-- tenant
227
261
union
228
262
select
@@ -279,6 +313,7 @@ pub async fn global_search(
279
313
. bind :: < fields:: sql_types:: Uuid , _ > ( & fields:: Uuid :: from ( user_id) )
280
314
. bind :: < diesel:: sql_types:: BigInt , _ > ( limit as i64 )
281
315
. bind :: < diesel:: sql_types:: BigInt , _ > ( offset as i64 )
316
+ . bind :: < fields:: sql_types:: JsonT , _ > ( tags)
282
317
. load ( & mut get_async_db_conn ( ) . await ?) . await ?;
283
318
284
319
Ok ( res)
0 commit comments