Files
OwnCord/Server/db/dbgen/dm.sql.go
T
J3vb 2a46b95111 feat: adopt sqlc for type-safe database access (Phase A Step 2)
- Add sqlc.yaml config (SQLite engine, db/queries/sqlite/, db/dbgen/ output)
- Pin sqlc v1.30.0 in sqlc.version
- Add Makefile with sqlc-install, sqlc-generate, sqlc-verify targets
- Write 14 SQL query files covering all DB domains (users, sessions,
  invites, channels, messages, reactions, voice, roles, attachments,
  admin, dm, blocks, lockouts, profile)
- Commit generated db/dbgen/ package (querier interface + typed fns)
- FTS5 search queries remain hand-written in message_queries.go;
  transactional multi-step operations unchanged in Go
2026-04-06 09:12:59 +02:00

231 lines
6.6 KiB
Go

// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.30.0
// source: dm.sql
package dbgen
import (
"context"
"database/sql"
)
const closeDM = `-- name: CloseDM :exec
DELETE FROM dm_open_state WHERE user_id = ? AND channel_id = ?
`
type CloseDMParams struct {
UserID int64 `json:"userId"`
ChannelID int64 `json:"channelId"`
}
func (q *Queries) CloseDM(ctx context.Context, arg CloseDMParams) error {
_, err := q.db.ExecContext(ctx, closeDM, arg.UserID, arg.ChannelID)
return err
}
const findExistingDMChannel = `-- name: FindExistingDMChannel :one
SELECT dp1.channel_id
FROM dm_participants dp1
JOIN dm_participants dp2 ON dp1.channel_id = dp2.channel_id
JOIN channels c ON c.id = dp1.channel_id
WHERE dp1.user_id = ? AND dp2.user_id = ? AND c.type = 'dm'
LIMIT 1
`
type FindExistingDMChannelParams struct {
UserID int64 `json:"userId"`
UserID_2 int64 `json:"userId2"`
}
func (q *Queries) FindExistingDMChannel(ctx context.Context, arg FindExistingDMChannelParams) (int64, error) {
row := q.db.QueryRowContext(ctx, findExistingDMChannel, arg.UserID, arg.UserID_2)
var channel_id int64
err := row.Scan(&channel_id)
return channel_id, err
}
const getDMParticipantIDs = `-- name: GetDMParticipantIDs :many
SELECT user_id FROM dm_participants WHERE channel_id = ?
`
func (q *Queries) GetDMParticipantIDs(ctx context.Context, channelID int64) ([]int64, error) {
rows, err := q.db.QueryContext(ctx, getDMParticipantIDs, channelID)
if err != nil {
return nil, err
}
defer rows.Close()
items := []int64{}
for rows.Next() {
var user_id int64
if err := rows.Scan(&user_id); err != nil {
return nil, err
}
items = append(items, user_id)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const getUserDMChannels = `-- name: GetUserDMChannels :many
SELECT
c.id AS channel_id,
u.id AS recipient_id,
u.username AS recipient_username,
COALESCE(u.avatar, '') AS recipient_avatar,
u.status AS recipient_status,
lm.id AS last_message_id,
COALESCE(lm.content, '') AS last_message,
COALESCE(lm.timestamp, '') AS last_message_at,
COUNT(CASE WHEN m_unread.id > COALESCE(rs.last_message_id, 0)
AND m_unread.deleted = 0 THEN 1 END) AS unread_count
FROM dm_open_state dos
JOIN channels c ON c.id = dos.channel_id AND c.type = 'dm'
JOIN dm_participants dp ON dp.channel_id = c.id AND dp.user_id != ?
JOIN users u ON u.id = dp.user_id
LEFT JOIN messages lm ON lm.id = (
SELECT MAX(id) FROM messages WHERE channel_id = c.id AND deleted = 0
)
LEFT JOIN messages m_unread ON m_unread.channel_id = c.id
LEFT JOIN read_states rs ON rs.channel_id = c.id AND rs.user_id = ?
WHERE dos.user_id = ?
GROUP BY c.id
ORDER BY COALESCE(lm.timestamp, dos.opened_at) DESC
`
type GetUserDMChannelsParams struct {
UserID int64 `json:"userId"`
UserID_2 int64 `json:"userId2"`
UserID_3 int64 `json:"userId3"`
}
type GetUserDMChannelsRow struct {
ChannelID int64 `json:"channelId"`
RecipientID int64 `json:"recipientId"`
RecipientUsername string `json:"recipientUsername"`
RecipientAvatar string `json:"recipientAvatar"`
RecipientStatus string `json:"recipientStatus"`
LastMessageID *int64 `json:"lastMessageId"`
LastMessage string `json:"lastMessage"`
LastMessageAt string `json:"lastMessageAt"`
UnreadCount int64 `json:"unreadCount"`
}
func (q *Queries) GetUserDMChannels(ctx context.Context, arg GetUserDMChannelsParams) ([]GetUserDMChannelsRow, error) {
rows, err := q.db.QueryContext(ctx, getUserDMChannels, arg.UserID, arg.UserID_2, arg.UserID_3)
if err != nil {
return nil, err
}
defer rows.Close()
items := []GetUserDMChannelsRow{}
for rows.Next() {
var i GetUserDMChannelsRow
if err := rows.Scan(
&i.ChannelID,
&i.RecipientID,
&i.RecipientUsername,
&i.RecipientAvatar,
&i.RecipientStatus,
&i.LastMessageID,
&i.LastMessage,
&i.LastMessageAt,
&i.UnreadCount,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Close(); err != nil {
return nil, err
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const insertDMChannel = `-- name: InsertDMChannel :execresult
INSERT INTO channels (name, type) VALUES ('', 'dm')
`
func (q *Queries) InsertDMChannel(ctx context.Context) (sql.Result, error) {
return q.db.ExecContext(ctx, insertDMChannel)
}
const insertDMOpenState = `-- name: InsertDMOpenState :exec
INSERT OR IGNORE INTO dm_open_state (user_id, channel_id) VALUES (?, ?), (?, ?)
`
type InsertDMOpenStateParams struct {
UserID int64 `json:"userId"`
ChannelID int64 `json:"channelId"`
UserID_2 int64 `json:"userId2"`
ChannelID_2 int64 `json:"channelId2"`
}
func (q *Queries) InsertDMOpenState(ctx context.Context, arg InsertDMOpenStateParams) error {
_, err := q.db.ExecContext(ctx, insertDMOpenState,
arg.UserID,
arg.ChannelID,
arg.UserID_2,
arg.ChannelID_2,
)
return err
}
const insertDMParticipants = `-- name: InsertDMParticipants :exec
INSERT INTO dm_participants (channel_id, user_id) VALUES (?, ?), (?, ?)
`
type InsertDMParticipantsParams struct {
ChannelID int64 `json:"channelId"`
UserID int64 `json:"userId"`
ChannelID_2 int64 `json:"channelId2"`
UserID_2 int64 `json:"userId2"`
}
func (q *Queries) InsertDMParticipants(ctx context.Context, arg InsertDMParticipantsParams) error {
_, err := q.db.ExecContext(ctx, insertDMParticipants,
arg.ChannelID,
arg.UserID,
arg.ChannelID_2,
arg.UserID_2,
)
return err
}
const isDMParticipant = `-- name: IsDMParticipant :one
SELECT user_id FROM dm_participants WHERE user_id = ? AND channel_id = ?
`
type IsDMParticipantParams struct {
UserID int64 `json:"userId"`
ChannelID int64 `json:"channelId"`
}
func (q *Queries) IsDMParticipant(ctx context.Context, arg IsDMParticipantParams) (int64, error) {
row := q.db.QueryRowContext(ctx, isDMParticipant, arg.UserID, arg.ChannelID)
var user_id int64
err := row.Scan(&user_id)
return user_id, err
}
const openDM = `-- name: OpenDM :exec
INSERT OR IGNORE INTO dm_open_state (user_id, channel_id) VALUES (?, ?)
`
type OpenDMParams struct {
UserID int64 `json:"userId"`
ChannelID int64 `json:"channelId"`
}
func (q *Queries) OpenDM(ctx context.Context, arg OpenDMParams) error {
_, err := q.db.ExecContext(ctx, openDM, arg.UserID, arg.ChannelID)
return err
}