Add talkgroup tags
This commit is contained in:
parent
72edd16d7b
commit
c79bfc0fde
6 changed files with 123 additions and 7 deletions
|
@ -67,11 +67,16 @@ type Talkgroup struct {
|
|||
System int32
|
||||
Name pgtype.Text
|
||||
Frequency pgtype.Int4
|
||||
GroupID pgtype.Int4
|
||||
Tgid int32
|
||||
AutoCreated pgtype.Bool
|
||||
Metadata []byte
|
||||
}
|
||||
|
||||
type TalkgroupsTag struct {
|
||||
TalkgroupID int32
|
||||
Tags []string
|
||||
}
|
||||
|
||||
type User struct {
|
||||
ID int32
|
||||
Username string
|
||||
|
|
88
pkg/gordio/database/talkgroups.sql.go
Normal file
88
pkg/gordio/database/talkgroups.sql.go
Normal file
|
@ -0,0 +1,88 @@
|
|||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.26.0
|
||||
// source: talkgroups.sql
|
||||
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
const getTalkgroupTags = `-- name: GetTalkgroupTags :one
|
||||
SELECT tags FROM talkgroups_tags
|
||||
WHERE talkgroup_id = $1
|
||||
`
|
||||
|
||||
func (q *Queries) GetTalkgroupTags(ctx context.Context, talkgroupID int32) ([]string, error) {
|
||||
row := q.db.QueryRow(ctx, getTalkgroupTags, talkgroupID)
|
||||
var tags []string
|
||||
err := row.Scan(&tags)
|
||||
return tags, err
|
||||
}
|
||||
|
||||
const getTalkgroupsWithAllTags = `-- name: GetTalkgroupsWithAllTags :many
|
||||
SELECT talkgroup_id, tags FROM talkgroups_tags
|
||||
WHERE tags && ARRAY[$1]
|
||||
`
|
||||
|
||||
func (q *Queries) GetTalkgroupsWithAllTags(ctx context.Context, tags []string) ([]TalkgroupsTag, error) {
|
||||
rows, err := q.db.Query(ctx, getTalkgroupsWithAllTags, tags)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []TalkgroupsTag
|
||||
for rows.Next() {
|
||||
var i TalkgroupsTag
|
||||
if err := rows.Scan(&i.TalkgroupID, &i.Tags); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const getTalkgroupsWithAnyTags = `-- name: GetTalkgroupsWithAnyTags :many
|
||||
SELECT talkgroup_id, tags FROM talkgroups_tags
|
||||
WHERE tags @> ARRAY[$1]
|
||||
`
|
||||
|
||||
func (q *Queries) GetTalkgroupsWithAnyTags(ctx context.Context, tags []string) ([]TalkgroupsTag, error) {
|
||||
rows, err := q.db.Query(ctx, getTalkgroupsWithAnyTags, tags)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []TalkgroupsTag
|
||||
for rows.Next() {
|
||||
var i TalkgroupsTag
|
||||
if err := rows.Scan(&i.TalkgroupID, &i.Tags); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const setTalkgroupTags = `-- name: SetTalkgroupTags :exec
|
||||
INSERT INTO talkgroups_tags(talkgroup_id, tags) VALUES($1, $2)
|
||||
ON CONFLICT (talkgroup_id) DO UPDATE
|
||||
SET tags = $2
|
||||
`
|
||||
|
||||
type SetTalkgroupTagsParams struct {
|
||||
TalkgroupID int32
|
||||
Tags []string
|
||||
}
|
||||
|
||||
func (q *Queries) SetTalkgroupTags(ctx context.Context, arg SetTalkgroupTagsParams) error {
|
||||
_, err := q.db.Exec(ctx, setTalkgroupTags, arg.TalkgroupID, arg.Tags)
|
||||
return err
|
||||
}
|
|
@ -28,12 +28,19 @@ CREATE TABLE IF NOT EXISTS talkgroups(
|
|||
system INTEGER REFERENCES systems(id) NOT NULL,
|
||||
name TEXT,
|
||||
frequency INTEGER,
|
||||
group_id INTEGER,
|
||||
tgid INTEGER,
|
||||
auto_created BOOLEAN,
|
||||
metadata JSONB,
|
||||
PRIMARY KEY (tgid, system)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS talkgroups_tags(
|
||||
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 INDEX IF NOT EXISTS talkgroup_tags_id_tags ON talkgroups_tags USING GIN (tags);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS calls(
|
||||
id UUID PRIMARY KEY,
|
||||
submitter INTEGER REFERENCES api_keys(id) ON DELETE SET NULL,
|
||||
|
|
16
sql/postgres/queries/talkgroups.sql
Normal file
16
sql/postgres/queries/talkgroups.sql
Normal file
|
@ -0,0 +1,16 @@
|
|||
-- name: GetTalkgroupsWithAnyTags :many
|
||||
SELECT * FROM talkgroups_tags
|
||||
WHERE tags @> ARRAY[$1];
|
||||
|
||||
-- name: GetTalkgroupsWithAllTags :many
|
||||
SELECT * FROM talkgroups_tags
|
||||
WHERE tags && ARRAY[$1];
|
||||
|
||||
-- name: GetTalkgroupTags :one
|
||||
SELECT tags FROM talkgroups_tags
|
||||
WHERE talkgroup_id = $1;
|
||||
|
||||
-- name: SetTalkgroupTags :exec
|
||||
INSERT INTO talkgroups_tags(talkgroup_id, tags) VALUES($1, $2)
|
||||
ON CONFLICT (talkgroup_id) DO UPDATE SET tags = $2;
|
||||
|
Loading…
Reference in a new issue