Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fcff1d1

Browse files
committed
sqlite: Implement search with tags
1 parent b35bae2 commit fcff1d1

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

chirpstack/src/storage/search.rs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,24 @@ pub async fn global_search(
198198
on u.id = tu.user_id
199199
where
200200
(?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+
)
202219
-- gateway
203220
union
204221
select
@@ -222,7 +239,24 @@ pub async fn global_search(
222239
on u.id = tu.user_id
223240
where
224241
(?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+
)
226260
-- tenant
227261
union
228262
select
@@ -279,6 +313,7 @@ pub async fn global_search(
279313
.bind::<fields::sql_types::Uuid, _>(&fields::Uuid::from(user_id))
280314
.bind::<diesel::sql_types::BigInt, _>(limit as i64)
281315
.bind::<diesel::sql_types::BigInt, _>(offset as i64)
316+
.bind::<fields::sql_types::JsonT, _>(tags)
282317
.load(&mut get_async_db_conn().await?).await?;
283318

284319
Ok(res)

0 commit comments

Comments
 (0)