use composite key for talkgroups

This commit is contained in:
Daniel 2024-07-17 19:45:46 -04:00
parent 57449e99a0
commit 20c21beca9
4 changed files with 29 additions and 19 deletions

View file

@ -63,16 +63,16 @@ type System struct {
}
type Talkgroup struct {
ID int32
System int32
Tgid int32
Name pgtype.Text
Frequency pgtype.Int4
Tgid int32
AutoCreated pgtype.Bool
Metadata []byte
}
type TalkgroupsTag struct {
SystemID int32
TalkgroupID int32
Tags []string
}

View file

@ -22,23 +22,28 @@ func (q *Queries) GetTalkgroupTags(ctx context.Context, talkgroupID int32) ([]st
}
const getTalkgroupsWithAllTags = `-- name: GetTalkgroupsWithAllTags :many
SELECT talkgroup_id FROM talkgroups_tags
SELECT system_id, talkgroup_id FROM talkgroups_tags
WHERE tags && ARRAY[$1]
`
func (q *Queries) GetTalkgroupsWithAllTags(ctx context.Context, tags []string) ([]int32, error) {
type GetTalkgroupsWithAllTagsRow struct {
SystemID int32
TalkgroupID int32
}
func (q *Queries) GetTalkgroupsWithAllTags(ctx context.Context, tags []string) ([]GetTalkgroupsWithAllTagsRow, error) {
rows, err := q.db.Query(ctx, getTalkgroupsWithAllTags, tags)
if err != nil {
return nil, err
}
defer rows.Close()
var items []int32
var items []GetTalkgroupsWithAllTagsRow
for rows.Next() {
var talkgroup_id int32
if err := rows.Scan(&talkgroup_id); err != nil {
var i GetTalkgroupsWithAllTagsRow
if err := rows.Scan(&i.SystemID, &i.TalkgroupID); err != nil {
return nil, err
}
items = append(items, talkgroup_id)
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
@ -47,23 +52,28 @@ func (q *Queries) GetTalkgroupsWithAllTags(ctx context.Context, tags []string) (
}
const getTalkgroupsWithAnyTags = `-- name: GetTalkgroupsWithAnyTags :many
SELECT talkgroup_id FROM talkgroups_tags
SELECT system_id, talkgroup_id FROM talkgroups_tags
WHERE tags @> ARRAY[$1]
`
func (q *Queries) GetTalkgroupsWithAnyTags(ctx context.Context, tags []string) ([]int32, error) {
type GetTalkgroupsWithAnyTagsRow struct {
SystemID int32
TalkgroupID int32
}
func (q *Queries) GetTalkgroupsWithAnyTags(ctx context.Context, tags []string) ([]GetTalkgroupsWithAnyTagsRow, error) {
rows, err := q.db.Query(ctx, getTalkgroupsWithAnyTags, tags)
if err != nil {
return nil, err
}
defer rows.Close()
var items []int32
var items []GetTalkgroupsWithAnyTagsRow
for rows.Next() {
var talkgroup_id int32
if err := rows.Scan(&talkgroup_id); err != nil {
var i GetTalkgroupsWithAnyTagsRow
if err := rows.Scan(&i.SystemID, &i.TalkgroupID); err != nil {
return nil, err
}
items = append(items, talkgroup_id)
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err

View file

@ -26,21 +26,21 @@ CREATE TABLE IF NOT EXISTS systems(
);
CREATE TABLE IF NOT EXISTS talkgroups(
id INTEGER NOT NULL,
system INTEGER REFERENCES systems(id) NOT NULL,
tgid INTEGER,
name TEXT,
frequency INTEGER,
tgid INTEGER,
auto_created BOOLEAN,
metadata JSONB,
PRIMARY KEY (tgid, system)
);
CREATE TABLE IF NOT EXISTS talkgroups_tags(
system_id INTEGER NOT NULL REFERENCES systems(id),
talkgroup_id INTEGER NOT NULL REFERENCES talkgroups(id),
tags TEXT[] NOT NULL DEFAULT '{}'
);
CREATE UNIQUE INDEX IF NOT EXISTS talkgroups_tags_id_talkgroup_id ON talkgroups_tags(talkgroup_id);
CREATE UNIQUE INDEX IF NOT EXISTS talkgroups_tags_id_talkgroup_id ON talkgroups_tags(system_id, talkgroup_id);
CREATE INDEX IF NOT EXISTS talkgroup_tags_id_tags ON talkgroups_tags USING GIN (tags);
CREATE TABLE IF NOT EXISTS calls(

View file

@ -1,9 +1,9 @@
-- name: GetTalkgroupsWithAnyTags :many
SELECT talkgroup_id FROM talkgroups_tags
SELECT system_id, talkgroup_id FROM talkgroups_tags
WHERE tags @> ARRAY[$1];
-- name: GetTalkgroupsWithAllTags :many
SELECT talkgroup_id FROM talkgroups_tags
SELECT system_id, talkgroup_id FROM talkgroups_tags
WHERE tags && ARRAY[$1];
-- name: GetTalkgroupTags :one