mirror of
https://github.com/J3vb/OwnCord.git
synced 2026-09-03 03:50:00 +03:00
- 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
467 lines
12 KiB
Go
467 lines
12 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// versions:
|
|
// sqlc v1.30.0
|
|
// source: messages.sql
|
|
|
|
package dbgen
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
)
|
|
|
|
const createMessage = `-- name: CreateMessage :execresult
|
|
INSERT INTO messages (channel_id, user_id, content, reply_to) VALUES (?, ?, ?, ?)
|
|
`
|
|
|
|
type CreateMessageParams struct {
|
|
ChannelID int64 `json:"channelId"`
|
|
UserID int64 `json:"userId"`
|
|
Content string `json:"content"`
|
|
ReplyTo *int64 `json:"replyTo"`
|
|
}
|
|
|
|
func (q *Queries) CreateMessage(ctx context.Context, arg CreateMessageParams) (sql.Result, error) {
|
|
return q.db.ExecContext(ctx, createMessage,
|
|
arg.ChannelID,
|
|
arg.UserID,
|
|
arg.Content,
|
|
arg.ReplyTo,
|
|
)
|
|
}
|
|
|
|
const editMessageContent = `-- name: EditMessageContent :exec
|
|
UPDATE messages SET content = ?, edited_at = datetime('now') WHERE id = ?
|
|
`
|
|
|
|
type EditMessageContentParams struct {
|
|
Content string `json:"content"`
|
|
ID int64 `json:"id"`
|
|
}
|
|
|
|
func (q *Queries) EditMessageContent(ctx context.Context, arg EditMessageContentParams) error {
|
|
_, err := q.db.ExecContext(ctx, editMessageContent, arg.Content, arg.ID)
|
|
return err
|
|
}
|
|
|
|
const getChannelUnreadCounts = `-- name: GetChannelUnreadCounts :many
|
|
SELECT c.id,
|
|
COALESCE(MAX(m.id), 0) AS last_msg_id,
|
|
COUNT(CASE WHEN m.id > COALESCE(rs.last_message_id, 0) AND m.deleted = 0 THEN 1 END) AS unread
|
|
FROM channels c
|
|
LEFT JOIN messages m ON m.channel_id = c.id AND m.deleted = 0
|
|
LEFT JOIN read_states rs ON rs.channel_id = c.id AND rs.user_id = ?
|
|
WHERE c.type = 'text'
|
|
GROUP BY c.id
|
|
`
|
|
|
|
type GetChannelUnreadCountsRow struct {
|
|
ID int64 `json:"id"`
|
|
LastMsgID interface{} `json:"lastMsgId"`
|
|
Unread int64 `json:"unread"`
|
|
}
|
|
|
|
func (q *Queries) GetChannelUnreadCounts(ctx context.Context, userID int64) ([]GetChannelUnreadCountsRow, error) {
|
|
rows, err := q.db.QueryContext(ctx, getChannelUnreadCounts, userID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []GetChannelUnreadCountsRow{}
|
|
for rows.Next() {
|
|
var i GetChannelUnreadCountsRow
|
|
if err := rows.Scan(&i.ID, &i.LastMsgID, &i.Unread); 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 getLatestMessageID = `-- name: GetLatestMessageID :one
|
|
SELECT COALESCE(MAX(id), 0) FROM messages WHERE channel_id = ? AND deleted = 0
|
|
`
|
|
|
|
func (q *Queries) GetLatestMessageID(ctx context.Context, channelID int64) (interface{}, error) {
|
|
row := q.db.QueryRowContext(ctx, getLatestMessageID, channelID)
|
|
var coalesce interface{}
|
|
err := row.Scan(&coalesce)
|
|
return coalesce, err
|
|
}
|
|
|
|
const getMessage = `-- name: GetMessage :one
|
|
SELECT id, channel_id, user_id, content, reply_to, edited_at, deleted, pinned, timestamp
|
|
FROM messages WHERE id = ?
|
|
`
|
|
|
|
func (q *Queries) GetMessage(ctx context.Context, id int64) (Message, error) {
|
|
row := q.db.QueryRowContext(ctx, getMessage, id)
|
|
var i Message
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.ChannelID,
|
|
&i.UserID,
|
|
&i.Content,
|
|
&i.ReplyTo,
|
|
&i.EditedAt,
|
|
&i.Deleted,
|
|
&i.Pinned,
|
|
&i.Timestamp,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const getMessagesByChannel = `-- name: GetMessagesByChannel :many
|
|
SELECT m.id, m.channel_id, m.user_id, m.content, m.reply_to,
|
|
m.edited_at, m.deleted, m.pinned, m.timestamp,
|
|
u.username, u.avatar
|
|
FROM messages m JOIN users u ON m.user_id = u.id
|
|
WHERE m.channel_id = ? AND m.deleted = 0
|
|
ORDER BY m.id DESC LIMIT ?
|
|
`
|
|
|
|
type GetMessagesByChannelParams struct {
|
|
ChannelID int64 `json:"channelId"`
|
|
Limit int64 `json:"limit"`
|
|
}
|
|
|
|
type GetMessagesByChannelRow struct {
|
|
ID int64 `json:"id"`
|
|
ChannelID int64 `json:"channelId"`
|
|
UserID int64 `json:"userId"`
|
|
Content string `json:"content"`
|
|
ReplyTo *int64 `json:"replyTo"`
|
|
EditedAt *string `json:"editedAt"`
|
|
Deleted int64 `json:"deleted"`
|
|
Pinned int64 `json:"pinned"`
|
|
Timestamp string `json:"timestamp"`
|
|
Username string `json:"username"`
|
|
Avatar *string `json:"avatar"`
|
|
}
|
|
|
|
func (q *Queries) GetMessagesByChannel(ctx context.Context, arg GetMessagesByChannelParams) ([]GetMessagesByChannelRow, error) {
|
|
rows, err := q.db.QueryContext(ctx, getMessagesByChannel, arg.ChannelID, arg.Limit)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []GetMessagesByChannelRow{}
|
|
for rows.Next() {
|
|
var i GetMessagesByChannelRow
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.ChannelID,
|
|
&i.UserID,
|
|
&i.Content,
|
|
&i.ReplyTo,
|
|
&i.EditedAt,
|
|
&i.Deleted,
|
|
&i.Pinned,
|
|
&i.Timestamp,
|
|
&i.Username,
|
|
&i.Avatar,
|
|
); 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 getMessagesByChannelBeforeCursor = `-- name: GetMessagesByChannelBeforeCursor :many
|
|
SELECT m.id, m.channel_id, m.user_id, m.content, m.reply_to,
|
|
m.edited_at, m.deleted, m.pinned, m.timestamp,
|
|
u.username, u.avatar
|
|
FROM messages m JOIN users u ON m.user_id = u.id
|
|
WHERE m.channel_id = ? AND m.id < ? AND m.deleted = 0
|
|
ORDER BY m.id DESC LIMIT ?
|
|
`
|
|
|
|
type GetMessagesByChannelBeforeCursorParams struct {
|
|
ChannelID int64 `json:"channelId"`
|
|
ID int64 `json:"id"`
|
|
Limit int64 `json:"limit"`
|
|
}
|
|
|
|
type GetMessagesByChannelBeforeCursorRow struct {
|
|
ID int64 `json:"id"`
|
|
ChannelID int64 `json:"channelId"`
|
|
UserID int64 `json:"userId"`
|
|
Content string `json:"content"`
|
|
ReplyTo *int64 `json:"replyTo"`
|
|
EditedAt *string `json:"editedAt"`
|
|
Deleted int64 `json:"deleted"`
|
|
Pinned int64 `json:"pinned"`
|
|
Timestamp string `json:"timestamp"`
|
|
Username string `json:"username"`
|
|
Avatar *string `json:"avatar"`
|
|
}
|
|
|
|
func (q *Queries) GetMessagesByChannelBeforeCursor(ctx context.Context, arg GetMessagesByChannelBeforeCursorParams) ([]GetMessagesByChannelBeforeCursorRow, error) {
|
|
rows, err := q.db.QueryContext(ctx, getMessagesByChannelBeforeCursor, arg.ChannelID, arg.ID, arg.Limit)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []GetMessagesByChannelBeforeCursorRow{}
|
|
for rows.Next() {
|
|
var i GetMessagesByChannelBeforeCursorRow
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.ChannelID,
|
|
&i.UserID,
|
|
&i.Content,
|
|
&i.ReplyTo,
|
|
&i.EditedAt,
|
|
&i.Deleted,
|
|
&i.Pinned,
|
|
&i.Timestamp,
|
|
&i.Username,
|
|
&i.Avatar,
|
|
); 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 getMessagesForAPI = `-- name: GetMessagesForAPI :many
|
|
SELECT m.id, m.channel_id, m.user_id, u.username, u.avatar,
|
|
m.content, m.reply_to, m.edited_at, m.deleted, m.pinned, m.timestamp
|
|
FROM messages m JOIN users u ON m.user_id = u.id
|
|
WHERE m.channel_id = ? AND m.deleted = 0
|
|
ORDER BY m.id DESC LIMIT ?
|
|
`
|
|
|
|
type GetMessagesForAPIParams struct {
|
|
ChannelID int64 `json:"channelId"`
|
|
Limit int64 `json:"limit"`
|
|
}
|
|
|
|
type GetMessagesForAPIRow struct {
|
|
ID int64 `json:"id"`
|
|
ChannelID int64 `json:"channelId"`
|
|
UserID int64 `json:"userId"`
|
|
Username string `json:"username"`
|
|
Avatar *string `json:"avatar"`
|
|
Content string `json:"content"`
|
|
ReplyTo *int64 `json:"replyTo"`
|
|
EditedAt *string `json:"editedAt"`
|
|
Deleted int64 `json:"deleted"`
|
|
Pinned int64 `json:"pinned"`
|
|
Timestamp string `json:"timestamp"`
|
|
}
|
|
|
|
func (q *Queries) GetMessagesForAPI(ctx context.Context, arg GetMessagesForAPIParams) ([]GetMessagesForAPIRow, error) {
|
|
rows, err := q.db.QueryContext(ctx, getMessagesForAPI, arg.ChannelID, arg.Limit)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []GetMessagesForAPIRow{}
|
|
for rows.Next() {
|
|
var i GetMessagesForAPIRow
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.ChannelID,
|
|
&i.UserID,
|
|
&i.Username,
|
|
&i.Avatar,
|
|
&i.Content,
|
|
&i.ReplyTo,
|
|
&i.EditedAt,
|
|
&i.Deleted,
|
|
&i.Pinned,
|
|
&i.Timestamp,
|
|
); 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 getMessagesForAPIBeforeCursor = `-- name: GetMessagesForAPIBeforeCursor :many
|
|
SELECT m.id, m.channel_id, m.user_id, u.username, u.avatar,
|
|
m.content, m.reply_to, m.edited_at, m.deleted, m.pinned, m.timestamp
|
|
FROM messages m JOIN users u ON m.user_id = u.id
|
|
WHERE m.channel_id = ? AND m.id < ? AND m.deleted = 0
|
|
ORDER BY m.id DESC LIMIT ?
|
|
`
|
|
|
|
type GetMessagesForAPIBeforeCursorParams struct {
|
|
ChannelID int64 `json:"channelId"`
|
|
ID int64 `json:"id"`
|
|
Limit int64 `json:"limit"`
|
|
}
|
|
|
|
type GetMessagesForAPIBeforeCursorRow struct {
|
|
ID int64 `json:"id"`
|
|
ChannelID int64 `json:"channelId"`
|
|
UserID int64 `json:"userId"`
|
|
Username string `json:"username"`
|
|
Avatar *string `json:"avatar"`
|
|
Content string `json:"content"`
|
|
ReplyTo *int64 `json:"replyTo"`
|
|
EditedAt *string `json:"editedAt"`
|
|
Deleted int64 `json:"deleted"`
|
|
Pinned int64 `json:"pinned"`
|
|
Timestamp string `json:"timestamp"`
|
|
}
|
|
|
|
func (q *Queries) GetMessagesForAPIBeforeCursor(ctx context.Context, arg GetMessagesForAPIBeforeCursorParams) ([]GetMessagesForAPIBeforeCursorRow, error) {
|
|
rows, err := q.db.QueryContext(ctx, getMessagesForAPIBeforeCursor, arg.ChannelID, arg.ID, arg.Limit)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []GetMessagesForAPIBeforeCursorRow{}
|
|
for rows.Next() {
|
|
var i GetMessagesForAPIBeforeCursorRow
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.ChannelID,
|
|
&i.UserID,
|
|
&i.Username,
|
|
&i.Avatar,
|
|
&i.Content,
|
|
&i.ReplyTo,
|
|
&i.EditedAt,
|
|
&i.Deleted,
|
|
&i.Pinned,
|
|
&i.Timestamp,
|
|
); 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 getPinnedMessageRows = `-- name: GetPinnedMessageRows :many
|
|
SELECT m.id, m.channel_id, m.user_id, u.username, u.avatar,
|
|
m.content, m.reply_to, m.edited_at, m.deleted, m.pinned, m.timestamp
|
|
FROM messages m JOIN users u ON m.user_id = u.id
|
|
WHERE m.channel_id = ? AND m.pinned = 1 AND m.deleted = 0
|
|
ORDER BY m.id DESC
|
|
`
|
|
|
|
type GetPinnedMessageRowsRow struct {
|
|
ID int64 `json:"id"`
|
|
ChannelID int64 `json:"channelId"`
|
|
UserID int64 `json:"userId"`
|
|
Username string `json:"username"`
|
|
Avatar *string `json:"avatar"`
|
|
Content string `json:"content"`
|
|
ReplyTo *int64 `json:"replyTo"`
|
|
EditedAt *string `json:"editedAt"`
|
|
Deleted int64 `json:"deleted"`
|
|
Pinned int64 `json:"pinned"`
|
|
Timestamp string `json:"timestamp"`
|
|
}
|
|
|
|
func (q *Queries) GetPinnedMessageRows(ctx context.Context, channelID int64) ([]GetPinnedMessageRowsRow, error) {
|
|
rows, err := q.db.QueryContext(ctx, getPinnedMessageRows, channelID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []GetPinnedMessageRowsRow{}
|
|
for rows.Next() {
|
|
var i GetPinnedMessageRowsRow
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.ChannelID,
|
|
&i.UserID,
|
|
&i.Username,
|
|
&i.Avatar,
|
|
&i.Content,
|
|
&i.ReplyTo,
|
|
&i.EditedAt,
|
|
&i.Deleted,
|
|
&i.Pinned,
|
|
&i.Timestamp,
|
|
); 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 setMessagePinned = `-- name: SetMessagePinned :execresult
|
|
UPDATE messages SET pinned = ? WHERE id = ? AND deleted = 0
|
|
`
|
|
|
|
type SetMessagePinnedParams struct {
|
|
Pinned int64 `json:"pinned"`
|
|
ID int64 `json:"id"`
|
|
}
|
|
|
|
func (q *Queries) SetMessagePinned(ctx context.Context, arg SetMessagePinnedParams) (sql.Result, error) {
|
|
return q.db.ExecContext(ctx, setMessagePinned, arg.Pinned, arg.ID)
|
|
}
|
|
|
|
const softDeleteMessage = `-- name: SoftDeleteMessage :exec
|
|
UPDATE messages SET deleted = 1 WHERE id = ?
|
|
`
|
|
|
|
func (q *Queries) SoftDeleteMessage(ctx context.Context, id int64) error {
|
|
_, err := q.db.ExecContext(ctx, softDeleteMessage, id)
|
|
return err
|
|
}
|
|
|
|
const updateReadState = `-- name: UpdateReadState :exec
|
|
INSERT INTO read_states (user_id, channel_id, last_message_id)
|
|
VALUES (?, ?, ?)
|
|
ON CONFLICT(user_id, channel_id) DO UPDATE SET last_message_id = excluded.last_message_id
|
|
`
|
|
|
|
type UpdateReadStateParams struct {
|
|
UserID int64 `json:"userId"`
|
|
ChannelID int64 `json:"channelId"`
|
|
LastMessageID int64 `json:"lastMessageId"`
|
|
}
|
|
|
|
func (q *Queries) UpdateReadState(ctx context.Context, arg UpdateReadStateParams) error {
|
|
_, err := q.db.ExecContext(ctx, updateReadState, arg.UserID, arg.ChannelID, arg.LastMessageID)
|
|
return err
|
|
}
|