2024-11-14 08:37:19 -05:00
package database
import (
"context"
2024-11-17 21:46:10 -05:00
"errors"
"github.com/jackc/pgx/v5/pgconn"
2024-11-14 08:37:19 -05:00
)
2024-11-15 12:18:32 -05:00
type talkgroupQuerier interface {
GetTalkgroupsWithLearnedBySysTGID ( ctx context . Context , ids TGTuples ) ( [ ] GetTalkgroupsRow , error )
GetTalkgroupsBySysTGID ( ctx context . Context , ids TGTuples ) ( [ ] GetTalkgroupsRow , error )
BulkSetTalkgroupTags ( ctx context . Context , tgs TGTuples , tags [ ] string ) error
}
2024-11-15 10:37:58 -05:00
type TGTuples [ 2 ] [ ] uint32
2024-11-15 08:46:29 -05:00
2024-11-17 21:46:10 -05:00
const TGConstraintName = "calls_system_talkgroup_fkey"
func IsTGConstraintViolation ( e error ) bool {
var err * pgconn . PgError
if errors . As ( e , & err ) && err . Code == "23503" && err . ConstraintName == TGConstraintName {
return true
}
return false
}
2024-11-15 10:37:58 -05:00
func MakeTGTuples ( cap int ) TGTuples {
return [ 2 ] [ ] uint32 {
make ( [ ] uint32 , 0 , cap ) ,
make ( [ ] uint32 , 0 , cap ) ,
2024-11-15 08:46:29 -05:00
}
2024-11-14 08:37:19 -05:00
}
2024-11-15 10:37:58 -05:00
func ( t * TGTuples ) Append ( sys , tg uint32 ) {
t [ 0 ] = append ( t [ 0 ] , sys )
t [ 1 ] = append ( t [ 1 ] , tg )
2024-11-14 08:37:19 -05:00
}
2024-11-15 10:37:58 -05:00
// Below queries are here because sqlc refuses to parse unnest(x, y)
2024-11-15 08:46:29 -05:00
2024-11-15 10:37:58 -05:00
const getTalkgroupsWithLearnedBySysTGID = ` SELECT
2024-11-17 21:46:10 -05:00
tg . id , tg . system_id , tg . tgid , tg . name , tg . alpha_tag , tg . tg_group , tg . frequency , tg . metadata , tg . tags , tg . alert , tg . alert_config , tg . weight , sys . id , sys . name , tg . learned
2024-11-14 08:37:19 -05:00
FROM talkgroups tg
JOIN systems sys ON tg . system_id = sys . id
2024-11-15 08:46:29 -05:00
JOIN UNNEST ( $ 1 : : INT4 [ ] , $ 2 : : INT4 [ ] ) AS tgt ( sys , tg ) ON ( tg . system_id = tgt . sys AND tg . tgid = tgt . tg )
2024-11-17 21:46:10 -05:00
WHERE tg . learned IS NOT TRUE
2024-11-14 08:37:19 -05:00
UNION
SELECT
2024-11-15 23:25:57 -05:00
tgl . id , tgl . system_id : : INT4 , tgl . tgid : : INT4 , tgl . name ,
2024-11-17 21:46:10 -05:00
tgl . alpha_tag , tgl . tg_group , NULL : : INTEGER , NULL : : JSONB ,
CASE WHEN tgl . tg_group IS NULL THEN NULL ELSE ARRAY [ tgl . tg_group ] END ,
2024-11-19 10:00:35 -05:00
NOT tgl . ignored , NULL : : JSONB , 1.0 , sys . id , sys . name , TRUE learned
2024-11-14 08:37:19 -05:00
FROM talkgroups_learned tgl
JOIN systems sys ON tgl . system_id = sys . id
2024-11-15 08:46:29 -05:00
JOIN UNNEST ( $ 1 : : INT4 [ ] , $ 2 : : INT4 [ ] ) AS tgt ( sys , tg ) ON ( tgl . system_id = tgt . sys AND tgl . tgid = tgt . tg ) ; `
2024-11-14 08:37:19 -05:00
2024-11-15 08:46:29 -05:00
type GetTalkgroupsRow struct {
2024-11-14 08:37:19 -05:00
Talkgroup Talkgroup ` json:"talkgroup" `
System System ` json:"system" `
}
2024-11-15 10:37:58 -05:00
func ( q * Queries ) GetTalkgroupsWithLearnedBySysTGID ( ctx context . Context , ids TGTuples ) ( [ ] GetTalkgroupsRow , error ) {
rows , err := q . db . Query ( ctx , getTalkgroupsWithLearnedBySysTGID , ids [ 0 ] , ids [ 1 ] )
2024-11-14 08:37:19 -05:00
if err != nil {
return nil , err
}
defer rows . Close ( )
2024-11-15 08:46:29 -05:00
var items [ ] GetTalkgroupsRow
2024-11-14 08:37:19 -05:00
for rows . Next ( ) {
2024-11-15 08:46:29 -05:00
var i GetTalkgroupsRow
2024-11-14 08:37:19 -05:00
if err := rows . Scan (
& i . Talkgroup . ID ,
& i . Talkgroup . SystemID ,
2024-11-15 11:34:54 -05:00
& i . Talkgroup . TGID ,
2024-11-14 08:37:19 -05:00
& i . Talkgroup . Name ,
& i . Talkgroup . AlphaTag ,
2024-11-17 21:46:10 -05:00
& i . Talkgroup . TGGroup ,
2024-11-14 08:37:19 -05:00
& i . Talkgroup . Frequency ,
& i . Talkgroup . Metadata ,
& i . Talkgroup . Tags ,
& i . Talkgroup . Alert ,
& i . Talkgroup . AlertConfig ,
& i . Talkgroup . Weight ,
& i . System . ID ,
& i . System . Name ,
2024-11-17 21:46:10 -05:00
& i . Talkgroup . Learned ,
2024-11-14 08:37:19 -05:00
) ; err != nil {
return nil , err
}
items = append ( items , i )
}
if err := rows . Err ( ) ; err != nil {
return nil , err
}
return items , nil
}
2024-11-15 10:37:58 -05:00
const getTalkgroupsBySysTGID = ` SELECT tg . id , tg . system_id , tg . tgid , tg . name , tg . alpha_tag , tg . tg_group , tg . frequency , tg . metadata , tg . tags , tg . alert , tg . alert_config , tg . weight , sys . id , sys . name FROM talkgroups tg
2024-11-14 08:37:19 -05:00
JOIN systems sys ON tg . system_id = sys . id
2024-11-17 21:46:10 -05:00
JOIN UNNEST ( $ 1 : : INT4 [ ] , $ 2 : : INT4 [ ] ) AS tgt ( sys , tg ) ON ( tg . system_id = tgt . sys AND tg . tgid = tgt . tg )
WHERE tg . learned IS NOT TRUE ; `
2024-11-14 08:37:19 -05:00
2024-11-15 10:37:58 -05:00
func ( q * Queries ) GetTalkgroupsBySysTGID ( ctx context . Context , ids TGTuples ) ( [ ] GetTalkgroupsRow , error ) {
rows , err := q . db . Query ( ctx , getTalkgroupsBySysTGID , ids [ 0 ] , ids [ 1 ] )
2024-11-14 08:37:19 -05:00
if err != nil {
return nil , err
}
defer rows . Close ( )
2024-11-15 08:46:29 -05:00
var items [ ] GetTalkgroupsRow
2024-11-14 08:37:19 -05:00
for rows . Next ( ) {
2024-11-15 08:46:29 -05:00
var i GetTalkgroupsRow
2024-11-14 08:37:19 -05:00
if err := rows . Scan (
& i . Talkgroup . ID ,
& i . Talkgroup . SystemID ,
2024-11-15 11:34:54 -05:00
& i . Talkgroup . TGID ,
2024-11-14 08:37:19 -05:00
& i . Talkgroup . Name ,
& i . Talkgroup . AlphaTag ,
2024-11-17 21:46:10 -05:00
& i . Talkgroup . TGGroup ,
2024-11-14 08:37:19 -05:00
& i . Talkgroup . Frequency ,
& i . Talkgroup . Metadata ,
& i . Talkgroup . Tags ,
& i . Talkgroup . Alert ,
& i . Talkgroup . AlertConfig ,
& i . Talkgroup . Weight ,
& i . System . ID ,
& i . System . Name ,
) ; err != nil {
return nil , err
}
items = append ( items , i )
}
if err := rows . Err ( ) ; err != nil {
return nil , err
}
return items , nil
}
2024-11-15 10:37:58 -05:00
const bulkSetTalkgroupTags = ` UPDATE talkgroups tg SET tags = $3 FROM UNNEST($1::INT4[], $2::INT4[]) AS tgt(sys, tg) WHERE (tg.system_id = tgt.sys AND tg.tgid = tgt.tg); `
func ( q * Queries ) BulkSetTalkgroupTags ( ctx context . Context , tgs TGTuples , tags [ ] string ) error {
_ , err := q . db . Exec ( ctx , bulkSetTalkgroupTags , tgs [ 0 ] , tgs [ 1 ] , tags )
return err
}