forked from mtusi2026/mtusi2026lab3
252 lines
5.7 KiB
Go
252 lines
5.7 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// versions:
|
|
// sqlc v1.31.1
|
|
// source: chats.sql
|
|
|
|
package sqlcgen
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
const addChatMembers = `-- name: AddChatMembers :exec
|
|
INSERT INTO chat_members (chat_id, user_id)
|
|
SELECT $1, members.member_id
|
|
FROM unnest($2::uuid[]) AS members(member_id)
|
|
`
|
|
|
|
type AddChatMembersParams struct {
|
|
ChatID uuid.UUID
|
|
UserIds []uuid.UUID
|
|
}
|
|
|
|
func (q *Queries) AddChatMembers(ctx context.Context, arg AddChatMembersParams) error {
|
|
_, err := q.db.Exec(ctx, addChatMembers, arg.ChatID, arg.UserIds)
|
|
return err
|
|
}
|
|
|
|
const bumpChatListVersions = `-- name: BumpChatListVersions :exec
|
|
INSERT INTO user_chat_versions (user_id, version)
|
|
SELECT DISTINCT m, 1
|
|
FROM unnest($1::uuid[]) AS m
|
|
ORDER BY m
|
|
ON CONFLICT (user_id) DO UPDATE SET version = user_chat_versions.version + 1
|
|
`
|
|
|
|
func (q *Queries) BumpChatListVersions(ctx context.Context, userIds []uuid.UUID) error {
|
|
_, err := q.db.Exec(ctx, bumpChatListVersions, userIds)
|
|
return err
|
|
}
|
|
|
|
const createChat = `-- name: CreateChat :one
|
|
INSERT INTO chats (title)
|
|
VALUES ($1)
|
|
RETURNING id, title, created_at
|
|
`
|
|
|
|
func (q *Queries) CreateChat(ctx context.Context, title string) (Chat, error) {
|
|
row := q.db.QueryRow(ctx, createChat, title)
|
|
var i Chat
|
|
err := row.Scan(&i.ID, &i.Title, &i.CreatedAt)
|
|
return i, err
|
|
}
|
|
|
|
const getChatForMember = `-- name: GetChatForMember :one
|
|
SELECT
|
|
c.id,
|
|
c.title,
|
|
c.created_at,
|
|
(
|
|
SELECT COALESCE(array_agg(m.user_id ORDER BY m.user_id), '{}')
|
|
FROM chat_members m
|
|
WHERE m.chat_id = c.id
|
|
)::uuid[] AS member_ids
|
|
FROM chats c
|
|
JOIN chat_members visible
|
|
ON visible.chat_id = c.id AND visible.user_id = $1
|
|
WHERE c.id = $2
|
|
`
|
|
|
|
type GetChatForMemberParams struct {
|
|
UserID uuid.UUID
|
|
ID uuid.UUID
|
|
}
|
|
|
|
type GetChatForMemberRow struct {
|
|
ID uuid.UUID
|
|
Title string
|
|
CreatedAt time.Time
|
|
MemberIds []uuid.UUID
|
|
}
|
|
|
|
func (q *Queries) GetChatForMember(ctx context.Context, arg GetChatForMemberParams) (GetChatForMemberRow, error) {
|
|
row := q.db.QueryRow(ctx, getChatForMember, arg.UserID, arg.ID)
|
|
var i GetChatForMemberRow
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Title,
|
|
&i.CreatedAt,
|
|
&i.MemberIds,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const getChatListVersion = `-- name: GetChatListVersion :one
|
|
SELECT COALESCE(MAX(version), 0)::bigint AS version
|
|
FROM user_chat_versions
|
|
WHERE user_id = $1
|
|
`
|
|
|
|
func (q *Queries) GetChatListVersion(ctx context.Context, userID uuid.UUID) (int64, error) {
|
|
row := q.db.QueryRow(ctx, getChatListVersion, userID)
|
|
var version int64
|
|
err := row.Scan(&version)
|
|
return version, err
|
|
}
|
|
|
|
const listChatsForMember = `-- name: ListChatsForMember :many
|
|
SELECT
|
|
c.id,
|
|
c.title,
|
|
c.created_at,
|
|
(
|
|
SELECT COALESCE(array_agg(m.user_id ORDER BY m.user_id), '{}')
|
|
FROM chat_members m
|
|
WHERE m.chat_id = c.id
|
|
)::uuid[] AS member_ids
|
|
FROM chats c
|
|
JOIN chat_members visible ON visible.chat_id = c.id
|
|
WHERE visible.user_id = $1
|
|
ORDER BY c.created_at, c.id
|
|
LIMIT $2
|
|
`
|
|
|
|
type ListChatsForMemberParams struct {
|
|
UserID uuid.UUID
|
|
RowLimit int32
|
|
}
|
|
|
|
type ListChatsForMemberRow struct {
|
|
ID uuid.UUID
|
|
Title string
|
|
CreatedAt time.Time
|
|
MemberIds []uuid.UUID
|
|
}
|
|
|
|
func (q *Queries) ListChatsForMember(ctx context.Context, arg ListChatsForMemberParams) ([]ListChatsForMemberRow, error) {
|
|
rows, err := q.db.Query(ctx, listChatsForMember, arg.UserID, arg.RowLimit)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []ListChatsForMemberRow{}
|
|
for rows.Next() {
|
|
var i ListChatsForMemberRow
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.Title,
|
|
&i.CreatedAt,
|
|
&i.MemberIds,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listChatsForMemberAfter = `-- name: ListChatsForMemberAfter :many
|
|
SELECT
|
|
c.id,
|
|
c.title,
|
|
c.created_at,
|
|
(
|
|
SELECT COALESCE(array_agg(m.user_id ORDER BY m.user_id), '{}')
|
|
FROM chat_members m
|
|
WHERE m.chat_id = c.id
|
|
)::uuid[] AS member_ids
|
|
FROM chats c
|
|
JOIN chat_members visible ON visible.chat_id = c.id
|
|
WHERE visible.user_id = $1
|
|
AND (c.created_at, c.id) > ($2::timestamptz, $3::uuid)
|
|
ORDER BY c.created_at, c.id
|
|
LIMIT $4
|
|
`
|
|
|
|
type ListChatsForMemberAfterParams struct {
|
|
UserID uuid.UUID
|
|
AfterCreatedAt time.Time
|
|
AfterID uuid.UUID
|
|
RowLimit int32
|
|
}
|
|
|
|
type ListChatsForMemberAfterRow struct {
|
|
ID uuid.UUID
|
|
Title string
|
|
CreatedAt time.Time
|
|
MemberIds []uuid.UUID
|
|
}
|
|
|
|
func (q *Queries) ListChatsForMemberAfter(ctx context.Context, arg ListChatsForMemberAfterParams) ([]ListChatsForMemberAfterRow, error) {
|
|
rows, err := q.db.Query(ctx, listChatsForMemberAfter,
|
|
arg.UserID,
|
|
arg.AfterCreatedAt,
|
|
arg.AfterID,
|
|
arg.RowLimit,
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []ListChatsForMemberAfterRow{}
|
|
for rows.Next() {
|
|
var i ListChatsForMemberAfterRow
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.Title,
|
|
&i.CreatedAt,
|
|
&i.MemberIds,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listMissingUserIDs = `-- name: ListMissingUserIDs :many
|
|
SELECT input.id::uuid AS missing_id
|
|
FROM unnest($1::uuid[]) AS input(id)
|
|
LEFT JOIN users u ON u.id = input.id
|
|
WHERE u.id IS NULL
|
|
`
|
|
|
|
func (q *Queries) ListMissingUserIDs(ctx context.Context, userIds []uuid.UUID) ([]uuid.UUID, error) {
|
|
rows, err := q.db.Query(ctx, listMissingUserIDs, userIds)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []uuid.UUID{}
|
|
for rows.Next() {
|
|
var missing_id uuid.UUID
|
|
if err := rows.Scan(&missing_id); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, missing_id)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|