2026-09-20 12:27:30 +00:00
|
|
|
package db
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"database/sql"
|
|
|
|
|
"fmt"
|
|
|
|
|
"time"
|
|
|
|
|
"uuid"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// DBTX is the interface satisfied by both *sql.DB and *sql.Tx.
|
|
|
|
|
type DBTX interface {
|
|
|
|
|
ExecContext(context.Context, string, ...any) (sql.Result, error)
|
|
|
|
|
QueryContext(context.Context, string, ...any) (*sql.Rows, error)
|
|
|
|
|
QueryRowContext(context.Context, string, ...any) *sql.Row
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Queries provides database query methods.
|
|
|
|
|
type Queries struct{ db DBTX }
|
|
|
|
|
|
|
|
|
|
// New creates a Queries backed by the given DBTX.
|
|
|
|
|
func New(db DBTX) *Queries { return &Queries{db: db} }
|
|
|
|
|
|
|
|
|
|
// WithTx returns a Queries that runs within the given transaction.
|
|
|
|
|
func (q *Queries) WithTx(tx *sql.Tx) *Queries { return &Queries{db: tx} }
|
|
|
|
|
|
|
|
|
|
const insertUser = `
|
|
|
|
|
INSERT INTO users (id, name, password, role)
|
|
|
|
|
VALUES (?, ?, ?, ?)
|
|
|
|
|
RETURNING created_at, deactivated_at, id, name, password, role, updated_at
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
// InsertUserParams holds arguments for InsertUser.
|
|
|
|
|
type InsertUserParams struct {
|
|
|
|
|
Name string
|
|
|
|
|
Password string
|
|
|
|
|
Role UserRole
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// InsertUser creates a new user.
|
|
|
|
|
func (q *Queries) InsertUser(ctx context.Context, arg InsertUserParams) (*User, error) {
|
|
|
|
|
row := q.db.QueryRowContext(
|
|
|
|
|
ctx,
|
|
|
|
|
insertUser,
|
|
|
|
|
uuid.New().String(),
|
|
|
|
|
arg.Name,
|
|
|
|
|
arg.Password,
|
|
|
|
|
arg.Role,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
u User
|
|
|
|
|
createdAt string
|
|
|
|
|
updatedAt string
|
|
|
|
|
)
|
2026-10-03 03:42:52 +00:00
|
|
|
err := row.Scan(
|
2026-09-20 12:27:30 +00:00
|
|
|
&createdAt,
|
|
|
|
|
&u.DeactivatedAt,
|
|
|
|
|
&u.Id,
|
|
|
|
|
&u.Name,
|
|
|
|
|
&u.Password,
|
|
|
|
|
&u.Role,
|
|
|
|
|
&updatedAt,
|
2026-10-03 03:42:52 +00:00
|
|
|
)
|
|
|
|
|
if err != nil {
|
2026-09-20 12:27:30 +00:00
|
|
|
return nil, fmt.Errorf(
|
|
|
|
|
"InsertUser: %w", err,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
if u.CreatedAt, err = time.Parse(time.RFC3339, createdAt); err != nil {
|
|
|
|
|
return nil, fmt.Errorf("InsertUser: parse created_at: %w", err)
|
|
|
|
|
}
|
|
|
|
|
if u.UpdatedAt, err = time.Parse(time.RFC3339, updatedAt); err != nil {
|
|
|
|
|
return nil, fmt.Errorf("InsertUser: parse updated_at: %w", err)
|
|
|
|
|
}
|
|
|
|
|
return &u, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const selectUserId = `
|
|
|
|
|
SELECT created_at, deactivated_at, id, name, password, role, updated_at
|
|
|
|
|
FROM users
|
|
|
|
|
WHERE id = ?
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
// SelectUserId returns a single user by ID.
|
|
|
|
|
func (q *Queries) SelectUserId(ctx context.Context, id string) (*User, error) {
|
|
|
|
|
row := q.db.QueryRowContext(ctx, selectUserId, id)
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
u User
|
|
|
|
|
createdAt string
|
|
|
|
|
updatedAt string
|
|
|
|
|
)
|
|
|
|
|
err := row.Scan(
|
|
|
|
|
&createdAt,
|
|
|
|
|
&u.DeactivatedAt,
|
|
|
|
|
&u.Id,
|
|
|
|
|
&u.Name,
|
|
|
|
|
&u.Password,
|
|
|
|
|
&u.Role,
|
|
|
|
|
&updatedAt,
|
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("SelectUserId: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if u.CreatedAt, err = time.Parse(time.RFC3339, createdAt); err != nil {
|
|
|
|
|
return nil, fmt.Errorf("SelectUserId: parse created_at: %w", err)
|
|
|
|
|
}
|
|
|
|
|
if u.UpdatedAt, err = time.Parse(time.RFC3339, updatedAt); err != nil {
|
|
|
|
|
return nil, fmt.Errorf("SelectUserId: parse updated_at: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &u, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const insertConversation = `
|
|
|
|
|
INSERT INTO conversations (id, title, user_id)
|
|
|
|
|
VALUES (?, ?, ?)
|
|
|
|
|
RETURNING created_at, id, title, updated_at, user_id
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
// InsertConversationParams holds arguments for InsertConversation.
|
|
|
|
|
type InsertConversationParams struct {
|
|
|
|
|
Title *string
|
|
|
|
|
UserId string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// InsertConversation creates a new conversation.
|
|
|
|
|
func (q *Queries) InsertConversation(ctx context.Context, arg InsertConversationParams) (*Conversation, error) {
|
|
|
|
|
row := q.db.QueryRowContext(
|
|
|
|
|
ctx,
|
|
|
|
|
insertConversation,
|
|
|
|
|
uuid.New().String(),
|
|
|
|
|
arg.Title,
|
|
|
|
|
arg.UserId,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
c Conversation
|
|
|
|
|
createdAt string
|
|
|
|
|
updatedAt string
|
|
|
|
|
err error
|
|
|
|
|
)
|
|
|
|
|
if err := row.Scan(
|
|
|
|
|
&createdAt,
|
|
|
|
|
&c.Id,
|
|
|
|
|
&c.Title,
|
|
|
|
|
&updatedAt,
|
|
|
|
|
&c.UserId,
|
|
|
|
|
); err != nil {
|
|
|
|
|
return nil, fmt.Errorf("InsertConversation: %w", err)
|
|
|
|
|
}
|
|
|
|
|
if c.CreatedAt, err = time.Parse(time.RFC3339, createdAt); err != nil {
|
|
|
|
|
return nil, fmt.Errorf("InsertConversation: parse created_at: %w", err)
|
|
|
|
|
}
|
|
|
|
|
if c.UpdatedAt, err = time.Parse(time.RFC3339, updatedAt); err != nil {
|
|
|
|
|
return nil, fmt.Errorf("InsertConversation: parse updated_at: %w", err)
|
|
|
|
|
}
|
|
|
|
|
return &c, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const selectConversationId = `
|
|
|
|
|
SELECT created_at, id, title, updated_at, user_id
|
|
|
|
|
FROM conversations
|
|
|
|
|
WHERE id = ?
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
// SelectConversationId returns a single conversation by ID.
|
|
|
|
|
func (q *Queries) SelectConversationId(ctx context.Context, id string) (*Conversation, error) {
|
|
|
|
|
row := q.db.QueryRowContext(ctx, selectConversationId, id)
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
c Conversation
|
|
|
|
|
createdAt string
|
|
|
|
|
updatedAt string
|
|
|
|
|
)
|
|
|
|
|
err := row.Scan(
|
|
|
|
|
&createdAt,
|
|
|
|
|
&c.Id,
|
|
|
|
|
&c.Title,
|
|
|
|
|
&updatedAt,
|
|
|
|
|
&c.UserId,
|
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("SelectConversationId: %w", err)
|
|
|
|
|
}
|
|
|
|
|
if c.CreatedAt, err = time.Parse(time.RFC3339, createdAt); err != nil {
|
|
|
|
|
return nil, fmt.Errorf("SelectConversationId: parse created_at: %w", err)
|
|
|
|
|
}
|
|
|
|
|
if c.UpdatedAt, err = time.Parse(time.RFC3339, updatedAt); err != nil {
|
|
|
|
|
return nil, fmt.Errorf("SelectConversationId: parse updated_at: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &c, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const selectConversationIdUserId = `
|
|
|
|
|
SELECT created_at, id, title, updated_at, user_id
|
|
|
|
|
FROM conversations
|
|
|
|
|
WHERE id = ? AND user_id = ?
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
// SelectConversationIdUserId returns a conversation by ID when it belongs to
|
|
|
|
|
// the specified user.
|
|
|
|
|
func (q *Queries) SelectConversationIdUserId(
|
|
|
|
|
ctx context.Context,
|
|
|
|
|
id string,
|
|
|
|
|
userId string,
|
|
|
|
|
) (*Conversation, error) {
|
|
|
|
|
row := q.db.QueryRowContext(ctx, selectConversationIdUserId, id, userId)
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
c Conversation
|
|
|
|
|
createdAt string
|
|
|
|
|
updatedAt string
|
|
|
|
|
)
|
|
|
|
|
err := row.Scan(
|
|
|
|
|
&createdAt,
|
|
|
|
|
&c.Id,
|
|
|
|
|
&c.Title,
|
|
|
|
|
&updatedAt,
|
|
|
|
|
&c.UserId,
|
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("SelectConversationIdUserId: %w", err)
|
|
|
|
|
}
|
|
|
|
|
if c.CreatedAt, err = time.Parse(time.RFC3339, createdAt); err != nil {
|
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
|
"SelectConversationIdUserId: parse created_at: %w", err,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
if c.UpdatedAt, err = time.Parse(time.RFC3339, updatedAt); err != nil {
|
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
|
"SelectConversationIdUserId: parse updated_at: %w", err,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &c, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const selectConversationsUserId = `
|
|
|
|
|
SELECT created_at, id, title, updated_at, user_id
|
|
|
|
|
FROM conversations
|
|
|
|
|
WHERE user_id = ?
|
|
|
|
|
ORDER BY updated_at DESC
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
// SelectConversationsUserId returns all conversations for a user,
|
|
|
|
|
// ordered by most recently updated.
|
|
|
|
|
func (q *Queries) SelectConversationsUserId(ctx context.Context, userId string) ([]Conversation, error) {
|
|
|
|
|
rows, err := q.db.QueryContext(ctx, selectConversationsUserId, userId)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("SelectConversationsUserId: %w", err)
|
|
|
|
|
}
|
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
|
|
var conversations []Conversation
|
|
|
|
|
for rows.Next() {
|
|
|
|
|
var (
|
|
|
|
|
c Conversation
|
|
|
|
|
createdAt string
|
|
|
|
|
updatedAt string
|
|
|
|
|
)
|
|
|
|
|
if err := rows.Scan(
|
|
|
|
|
&createdAt,
|
|
|
|
|
&c.Id,
|
|
|
|
|
&c.Title,
|
|
|
|
|
&updatedAt,
|
|
|
|
|
&c.UserId,
|
|
|
|
|
); err != nil {
|
|
|
|
|
return nil, fmt.Errorf("SelectConversationsUserId: %w", err)
|
|
|
|
|
}
|
|
|
|
|
if c.CreatedAt, err = time.Parse(time.RFC3339, createdAt); err != nil {
|
|
|
|
|
return nil, fmt.Errorf("SelectConversationsUserId: parse created_at: %w", err)
|
|
|
|
|
}
|
|
|
|
|
if c.UpdatedAt, err = time.Parse(time.RFC3339, updatedAt); err != nil {
|
|
|
|
|
return nil, fmt.Errorf("SelectConversationsUserId: parse updated_at: %w", err)
|
|
|
|
|
}
|
|
|
|
|
conversations = append(conversations, c)
|
|
|
|
|
}
|
|
|
|
|
if err := rows.Err(); err != nil {
|
|
|
|
|
return nil, fmt.Errorf("SelectConversationsUserId: %w", err)
|
|
|
|
|
}
|
|
|
|
|
return conversations, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const deleteConversationId = `
|
|
|
|
|
DELETE FROM conversations
|
|
|
|
|
WHERE id = ?
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
// DeleteConversation deletes a conversation by ID. Cascades to
|
|
|
|
|
// all messages in the conversation.
|
|
|
|
|
func (q *Queries) DeleteConversationId(ctx context.Context, id string) error {
|
|
|
|
|
_, err := q.db.ExecContext(ctx, deleteConversationId, id)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("DeleteConversationId: %w", err)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const insertMessage = `
|
|
|
|
|
INSERT INTO messages (id, content, conversation_id, role, sequence)
|
|
|
|
|
VALUES (?, ?, ?, ?, ?)
|
|
|
|
|
RETURNING content, conversation_id, created_at, id, role, sequence, updated_at
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
// InsertMessageParams holds arguments for InsertMessage.
|
|
|
|
|
type InsertMessageParams struct {
|
|
|
|
|
Content string
|
|
|
|
|
ConversationId string
|
|
|
|
|
Role MessageRole
|
|
|
|
|
Sequence int
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// InsertMessage creates a new message.
|
|
|
|
|
func (q *Queries) InsertMessage(ctx context.Context, arg InsertMessageParams) (*Message, error) {
|
|
|
|
|
row := q.db.QueryRowContext(
|
|
|
|
|
ctx,
|
|
|
|
|
insertMessage,
|
|
|
|
|
uuid.New().String(),
|
|
|
|
|
arg.Content,
|
|
|
|
|
arg.ConversationId,
|
|
|
|
|
arg.Role,
|
|
|
|
|
arg.Sequence,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
m Message
|
|
|
|
|
createdAt string
|
|
|
|
|
updatedAt string
|
|
|
|
|
err error
|
|
|
|
|
)
|
|
|
|
|
if err := row.Scan(
|
|
|
|
|
&m.Content,
|
|
|
|
|
&m.ConversationId,
|
|
|
|
|
&createdAt,
|
|
|
|
|
&m.Id,
|
|
|
|
|
&m.Role,
|
|
|
|
|
&m.Sequence,
|
|
|
|
|
&updatedAt,
|
|
|
|
|
); err != nil {
|
|
|
|
|
return nil, fmt.Errorf("InsertMessage: %w", err)
|
|
|
|
|
}
|
|
|
|
|
if m.CreatedAt, err = time.Parse(time.RFC3339, createdAt); err != nil {
|
|
|
|
|
return nil, fmt.Errorf("InsertMessage: parse created_at: %w", err)
|
|
|
|
|
}
|
|
|
|
|
if m.UpdatedAt, err = time.Parse(time.RFC3339, updatedAt); err != nil {
|
|
|
|
|
return nil, fmt.Errorf("InsertMessage: parse updated_at: %w", err)
|
|
|
|
|
}
|
|
|
|
|
return &m, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const selectMessagesConversationId = `
|
|
|
|
|
SELECT content, conversation_id, created_at, id, role, sequence, updated_at
|
|
|
|
|
FROM messages
|
|
|
|
|
WHERE conversation_id = ?
|
|
|
|
|
ORDER BY sequence
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
// SelectMessagesConversationId returns all messages for a
|
|
|
|
|
// conversation, ordered by sequence.
|
|
|
|
|
func (q *Queries) SelectMessagesConversationId(ctx context.Context, conversationId string) ([]Message, error) {
|
|
|
|
|
rows, err := q.db.QueryContext(ctx, selectMessagesConversationId, conversationId)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("SelectMessagesConversationId: %w", err)
|
|
|
|
|
}
|
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
|
|
var messages []Message
|
|
|
|
|
for rows.Next() {
|
|
|
|
|
var (
|
|
|
|
|
m Message
|
|
|
|
|
createdAt string
|
|
|
|
|
updatedAt string
|
|
|
|
|
)
|
|
|
|
|
if err := rows.Scan(
|
|
|
|
|
&m.Content,
|
|
|
|
|
&m.ConversationId,
|
|
|
|
|
&createdAt,
|
|
|
|
|
&m.Id,
|
|
|
|
|
&m.Role,
|
|
|
|
|
&m.Sequence,
|
|
|
|
|
&updatedAt,
|
|
|
|
|
); err != nil {
|
|
|
|
|
return nil, fmt.Errorf("SelectMessagesConversationId: %w", err)
|
|
|
|
|
}
|
|
|
|
|
if m.CreatedAt, err = time.Parse(time.RFC3339, createdAt); err != nil {
|
|
|
|
|
return nil, fmt.Errorf("SelectMessagesConversationId: parse created_at: %w", err)
|
|
|
|
|
}
|
|
|
|
|
if m.UpdatedAt, err = time.Parse(time.RFC3339, updatedAt); err != nil {
|
|
|
|
|
return nil, fmt.Errorf("SelectMessagesConversationId: parse updated_at: %w", err)
|
|
|
|
|
}
|
|
|
|
|
messages = append(messages, m)
|
|
|
|
|
}
|
|
|
|
|
if err := rows.Err(); err != nil {
|
|
|
|
|
return nil, fmt.Errorf("SelectMessagesConversationId: %w", err)
|
|
|
|
|
}
|
|
|
|
|
return messages, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const updateMessageContent = `
|
|
|
|
|
UPDATE messages
|
|
|
|
|
SET content = ?, updated_at = strftime('%Y-%m-%dT%H:%M:%fZ', 'now')
|
|
|
|
|
WHERE id = ?
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
// UpdateMessageContentParams holds arguments for UpdateMessageContent.
|
|
|
|
|
type UpdateMessageContentParams struct {
|
|
|
|
|
Content string
|
|
|
|
|
Id string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// UpdateMessageContent sets the content JSON for a message.
|
|
|
|
|
func (q *Queries) UpdateMessageContent(ctx context.Context, arg UpdateMessageContentParams) error {
|
|
|
|
|
_, err := q.db.ExecContext(
|
|
|
|
|
ctx,
|
|
|
|
|
updateMessageContent,
|
|
|
|
|
arg.Content,
|
|
|
|
|
arg.Id,
|
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("UpdateMessageContent: %w", err)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const deleteMessageId = `
|
|
|
|
|
DELETE FROM messages
|
|
|
|
|
WHERE id = ?
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
// DeleteMessage deletes a message by ID.
|
|
|
|
|
func (q *Queries) DeleteMessageId(ctx context.Context, id string) error {
|
|
|
|
|
_, err := q.db.ExecContext(ctx, deleteMessageId, id)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("DeleteMessageId: %w", err)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const selectMaxSequence = `
|
|
|
|
|
SELECT COALESCE(MAX(sequence), 0)
|
|
|
|
|
FROM messages
|
|
|
|
|
WHERE conversation_id = ?
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
// NextSequence returns the next available sequence number for
|
|
|
|
|
// a conversation.
|
|
|
|
|
func (q *Queries) NextSequence(ctx context.Context, conversationId string) (int, error) {
|
|
|
|
|
var seq int
|
|
|
|
|
err := q.db.QueryRowContext(ctx, selectMaxSequence, conversationId).Scan(&seq)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return 0, fmt.Errorf("NextSequence: %w", err)
|
|
|
|
|
}
|
|
|
|
|
return seq + 1, nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-03 03:42:52 +00:00
|
|
|
const insertExecution = `
|
|
|
|
|
INSERT INTO executions (id, message_id, status, tool_call_id)
|
|
|
|
|
VALUES (?, ?, ?, ?)
|
2026-09-20 12:27:30 +00:00
|
|
|
RETURNING created_at, id, message_id, status, tool_call_id, updated_at
|
|
|
|
|
`
|
|
|
|
|
|
2026-10-03 03:42:52 +00:00
|
|
|
// InsertExecutionParams holds arguments for InsertExecution.
|
|
|
|
|
type InsertExecutionParams struct {
|
2026-09-20 12:27:30 +00:00
|
|
|
MessageId string
|
2026-10-03 03:42:52 +00:00
|
|
|
Status ExecutionStatus
|
2026-09-20 12:27:30 +00:00
|
|
|
ToolCallId string
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-03 03:42:52 +00:00
|
|
|
// InsertExecution creates a new execution row.
|
|
|
|
|
func (q *Queries) InsertExecution(ctx context.Context, arg InsertExecutionParams) (*Execution, error) {
|
2026-09-20 12:27:30 +00:00
|
|
|
row := q.db.QueryRowContext(
|
|
|
|
|
ctx,
|
2026-10-03 03:42:52 +00:00
|
|
|
insertExecution,
|
2026-09-20 12:27:30 +00:00
|
|
|
uuid.New().String(),
|
|
|
|
|
arg.MessageId,
|
2026-10-03 03:42:52 +00:00
|
|
|
arg.Status,
|
2026-09-20 12:27:30 +00:00
|
|
|
arg.ToolCallId,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
2026-10-03 03:42:52 +00:00
|
|
|
e Execution
|
2026-09-20 12:27:30 +00:00
|
|
|
createdAt string
|
|
|
|
|
updatedAt string
|
|
|
|
|
)
|
|
|
|
|
err := row.Scan(
|
|
|
|
|
&createdAt,
|
2026-10-03 03:42:52 +00:00
|
|
|
&e.Id,
|
|
|
|
|
&e.MessageId,
|
|
|
|
|
&e.Status,
|
|
|
|
|
&e.ToolCallId,
|
2026-09-20 12:27:30 +00:00
|
|
|
&updatedAt,
|
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
2026-10-03 03:42:52 +00:00
|
|
|
return nil, fmt.Errorf(
|
|
|
|
|
"InsertExecution: %w", err,
|
|
|
|
|
)
|
2026-09-20 12:27:30 +00:00
|
|
|
}
|
2026-10-03 03:42:52 +00:00
|
|
|
if e.CreatedAt, err = time.Parse(time.RFC3339, createdAt); err != nil {
|
|
|
|
|
return nil, fmt.Errorf("InsertExecution: parse created_at: %w", err)
|
2026-09-20 12:27:30 +00:00
|
|
|
}
|
2026-10-03 03:42:52 +00:00
|
|
|
if e.UpdatedAt, err = time.Parse(time.RFC3339, updatedAt); err != nil {
|
|
|
|
|
return nil, fmt.Errorf("InsertExecution: parse updated_at: %w", err)
|
2026-09-20 12:27:30 +00:00
|
|
|
}
|
2026-10-03 03:42:52 +00:00
|
|
|
return &e, nil
|
2026-09-20 12:27:30 +00:00
|
|
|
}
|
|
|
|
|
|
2026-10-03 03:42:52 +00:00
|
|
|
const selectExecutionId = `
|
2026-09-20 12:27:30 +00:00
|
|
|
SELECT created_at, id, message_id, status, tool_call_id, updated_at
|
2026-10-03 03:42:52 +00:00
|
|
|
FROM executions
|
|
|
|
|
WHERE id = ?
|
|
|
|
|
`
|
|
|
|
|
|
|
|
|
|
// SelectExecutionId returns a single execution row by ID.
|
|
|
|
|
func (q *Queries) SelectExecutionId(ctx context.Context, id string) (*Execution, error) {
|
|
|
|
|
row := q.db.QueryRowContext(ctx, selectExecutionId, id)
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
e Execution
|
|
|
|
|
createdAt string
|
|
|
|
|
updatedAt string
|
|
|
|
|
)
|
|
|
|
|
err := row.Scan(
|
|
|
|
|
&createdAt,
|
|
|
|
|
&e.Id,
|
|
|
|
|
&e.MessageId,
|
|
|
|
|
&e.Status,
|
|
|
|
|
&e.ToolCallId,
|
|
|
|
|
&updatedAt,
|
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("SelectExecutionId: %w", err)
|
|
|
|
|
}
|
|
|
|
|
if e.CreatedAt, err = time.Parse(time.RFC3339, createdAt); err != nil {
|
|
|
|
|
return nil, fmt.Errorf("SelectExecutionId: parse created_at: %w", err)
|
|
|
|
|
}
|
|
|
|
|
if e.UpdatedAt, err = time.Parse(time.RFC3339, updatedAt); err != nil {
|
|
|
|
|
return nil, fmt.Errorf("SelectExecutionId: parse updated_at: %w", err)
|
|
|
|
|
}
|
|
|
|
|
return &e, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const selectExecutionsMessageId = `
|
|
|
|
|
SELECT created_at, id, message_id, status, tool_call_id, updated_at
|
|
|
|
|
FROM executions
|
2026-09-20 12:27:30 +00:00
|
|
|
WHERE message_id = ?
|
|
|
|
|
ORDER BY created_at
|
|
|
|
|
`
|
|
|
|
|
|
2026-10-03 03:42:52 +00:00
|
|
|
// SelectExecutionsMessageId returns all execution rows for a message,
|
2026-09-20 12:27:30 +00:00
|
|
|
// ordered by creation time.
|
2026-10-03 03:42:52 +00:00
|
|
|
func (q *Queries) SelectExecutionsMessageId(ctx context.Context, messageId string) ([]Execution, error) {
|
|
|
|
|
rows, err := q.db.QueryContext(ctx, selectExecutionsMessageId, messageId)
|
2026-09-20 12:27:30 +00:00
|
|
|
if err != nil {
|
2026-10-03 03:42:52 +00:00
|
|
|
return nil, fmt.Errorf("SelectExecutionsMessageId: %w", err)
|
2026-09-20 12:27:30 +00:00
|
|
|
}
|
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
2026-10-03 03:42:52 +00:00
|
|
|
var executions []Execution
|
2026-09-20 12:27:30 +00:00
|
|
|
for rows.Next() {
|
|
|
|
|
var (
|
2026-10-03 03:42:52 +00:00
|
|
|
e Execution
|
2026-09-20 12:27:30 +00:00
|
|
|
createdAt string
|
|
|
|
|
updatedAt string
|
|
|
|
|
)
|
|
|
|
|
if err := rows.Scan(
|
|
|
|
|
&createdAt,
|
2026-10-03 03:42:52 +00:00
|
|
|
&e.Id,
|
|
|
|
|
&e.MessageId,
|
|
|
|
|
&e.Status,
|
|
|
|
|
&e.ToolCallId,
|
2026-09-20 12:27:30 +00:00
|
|
|
&updatedAt,
|
|
|
|
|
); err != nil {
|
2026-10-03 03:42:52 +00:00
|
|
|
return nil, fmt.Errorf("SelectExecutionsMessageId: %w", err)
|
2026-09-20 12:27:30 +00:00
|
|
|
}
|
2026-10-03 03:42:52 +00:00
|
|
|
if e.CreatedAt, err = time.Parse(time.RFC3339, createdAt); err != nil {
|
|
|
|
|
return nil, fmt.Errorf("SelectExecutionsMessageId: parse created_at: %w", err)
|
2026-09-20 12:27:30 +00:00
|
|
|
}
|
2026-10-03 03:42:52 +00:00
|
|
|
if e.UpdatedAt, err = time.Parse(time.RFC3339, updatedAt); err != nil {
|
|
|
|
|
return nil, fmt.Errorf("SelectExecutionsMessageId: parse updated_at: %w", err)
|
2026-09-20 12:27:30 +00:00
|
|
|
}
|
2026-10-03 03:42:52 +00:00
|
|
|
executions = append(executions, e)
|
2026-09-20 12:27:30 +00:00
|
|
|
}
|
|
|
|
|
if err := rows.Err(); err != nil {
|
2026-10-03 03:42:52 +00:00
|
|
|
return nil, fmt.Errorf("SelectExecutionsMessageId: %w", err)
|
2026-09-20 12:27:30 +00:00
|
|
|
}
|
2026-10-03 03:42:52 +00:00
|
|
|
return executions, nil
|
2026-09-20 12:27:30 +00:00
|
|
|
}
|
|
|
|
|
|
2026-10-03 03:42:52 +00:00
|
|
|
const selectPendingExecutions = `
|
2026-09-20 12:27:30 +00:00
|
|
|
SELECT created_at, id, message_id, status, tool_call_id, updated_at
|
2026-10-03 03:42:52 +00:00
|
|
|
FROM executions
|
2026-09-20 12:27:30 +00:00
|
|
|
WHERE status = 'pending'
|
|
|
|
|
ORDER BY created_at
|
|
|
|
|
`
|
|
|
|
|
|
2026-10-03 03:42:52 +00:00
|
|
|
// SelectPendingExecutions returns all pending execution rows across all
|
2026-09-20 12:27:30 +00:00
|
|
|
// conversations, ordered by creation time. This serves as the global
|
|
|
|
|
// inbox for the UI.
|
2026-10-03 03:42:52 +00:00
|
|
|
func (q *Queries) SelectPendingExecutions(ctx context.Context) ([]Execution, error) {
|
|
|
|
|
rows, err := q.db.QueryContext(ctx, selectPendingExecutions)
|
2026-09-20 12:27:30 +00:00
|
|
|
if err != nil {
|
2026-10-03 03:42:52 +00:00
|
|
|
return nil, fmt.Errorf("SelectPendingExecutions: %w", err)
|
2026-09-20 12:27:30 +00:00
|
|
|
}
|
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
2026-10-03 03:42:52 +00:00
|
|
|
var executions []Execution
|
2026-09-20 12:27:30 +00:00
|
|
|
for rows.Next() {
|
|
|
|
|
var (
|
2026-10-03 03:42:52 +00:00
|
|
|
e Execution
|
2026-09-20 12:27:30 +00:00
|
|
|
createdAt string
|
|
|
|
|
updatedAt string
|
|
|
|
|
)
|
|
|
|
|
if err := rows.Scan(
|
|
|
|
|
&createdAt,
|
2026-10-03 03:42:52 +00:00
|
|
|
&e.Id,
|
|
|
|
|
&e.MessageId,
|
|
|
|
|
&e.Status,
|
|
|
|
|
&e.ToolCallId,
|
2026-09-20 12:27:30 +00:00
|
|
|
&updatedAt,
|
|
|
|
|
); err != nil {
|
2026-10-03 03:42:52 +00:00
|
|
|
return nil, fmt.Errorf("SelectPendingExecutions: %w", err)
|
2026-09-20 12:27:30 +00:00
|
|
|
}
|
2026-10-03 03:42:52 +00:00
|
|
|
if e.CreatedAt, err = time.Parse(time.RFC3339, createdAt); err != nil {
|
|
|
|
|
return nil, fmt.Errorf("SelectPendingExecutions: parse created_at: %w", err)
|
2026-09-20 12:27:30 +00:00
|
|
|
}
|
2026-10-03 03:42:52 +00:00
|
|
|
if e.UpdatedAt, err = time.Parse(time.RFC3339, updatedAt); err != nil {
|
|
|
|
|
return nil, fmt.Errorf("SelectPendingExecutions: parse updated_at: %w", err)
|
2026-09-20 12:27:30 +00:00
|
|
|
}
|
2026-10-03 03:42:52 +00:00
|
|
|
executions = append(executions, e)
|
2026-09-20 12:27:30 +00:00
|
|
|
}
|
|
|
|
|
if err := rows.Err(); err != nil {
|
2026-10-03 03:42:52 +00:00
|
|
|
return nil, fmt.Errorf("SelectPendingExecutions: %w", err)
|
2026-09-20 12:27:30 +00:00
|
|
|
}
|
2026-10-03 03:42:52 +00:00
|
|
|
return executions, nil
|
2026-09-20 12:27:30 +00:00
|
|
|
}
|
|
|
|
|
|
2026-10-03 03:42:52 +00:00
|
|
|
const updateExecutionStatus = `
|
|
|
|
|
UPDATE executions
|
2026-09-20 12:27:30 +00:00
|
|
|
SET status = ?, updated_at = strftime('%Y-%m-%dT%H:%M:%fZ', 'now')
|
|
|
|
|
WHERE id = ?
|
|
|
|
|
`
|
|
|
|
|
|
2026-10-03 03:42:52 +00:00
|
|
|
// UpdateExecutionStatusParams holds arguments for UpdateExecutionStatus.
|
|
|
|
|
type UpdateExecutionStatusParams struct {
|
2026-09-20 12:27:30 +00:00
|
|
|
Id string
|
2026-10-03 03:42:52 +00:00
|
|
|
Status ExecutionStatus
|
2026-09-20 12:27:30 +00:00
|
|
|
}
|
|
|
|
|
|
2026-10-03 03:42:52 +00:00
|
|
|
// UpdateExecutionStatus transitions an execution row's status and bumps
|
2026-09-20 12:27:30 +00:00
|
|
|
// updated_at. The caller is responsible for valid transitions.
|
2026-10-03 03:42:52 +00:00
|
|
|
func (q *Queries) UpdateExecutionStatus(ctx context.Context, arg UpdateExecutionStatusParams) error {
|
2026-09-20 12:27:30 +00:00
|
|
|
_, err := q.db.ExecContext(
|
|
|
|
|
ctx,
|
2026-10-03 03:42:52 +00:00
|
|
|
updateExecutionStatus,
|
2026-09-20 12:27:30 +00:00
|
|
|
arg.Status,
|
|
|
|
|
arg.Id,
|
|
|
|
|
)
|
|
|
|
|
if err != nil {
|
2026-10-03 03:42:52 +00:00
|
|
|
return fmt.Errorf("UpdateExecutionStatus: %w", err)
|
2026-09-20 12:27:30 +00:00
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|