2023-09-18 23:57:18 +00:00
|
|
|
// Code generated by sqlc. DO NOT EDIT.
|
|
|
|
// versions:
|
|
|
|
// sqlc v1.21.0
|
|
|
|
// source: query.sql
|
|
|
|
|
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
const addActivity = `-- name: AddActivity :one
|
2023-11-03 23:38:31 +00:00
|
|
|
INSERT INTO activity (
|
2023-09-18 23:57:18 +00:00
|
|
|
user_id,
|
|
|
|
document_id,
|
|
|
|
device_id,
|
|
|
|
start_time,
|
|
|
|
duration,
|
2023-11-03 23:38:31 +00:00
|
|
|
start_percentage,
|
|
|
|
end_percentage
|
2023-09-18 23:57:18 +00:00
|
|
|
)
|
|
|
|
VALUES (?, ?, ?, ?, ?, ?, ?)
|
2023-11-03 23:38:31 +00:00
|
|
|
RETURNING id, user_id, document_id, device_id, start_time, start_percentage, end_percentage, duration, created_at
|
2023-09-18 23:57:18 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
type AddActivityParams struct {
|
2023-11-03 23:38:31 +00:00
|
|
|
UserID string `json:"user_id"`
|
|
|
|
DocumentID string `json:"document_id"`
|
|
|
|
DeviceID string `json:"device_id"`
|
|
|
|
StartTime string `json:"start_time"`
|
|
|
|
Duration int64 `json:"duration"`
|
|
|
|
StartPercentage float64 `json:"start_percentage"`
|
|
|
|
EndPercentage float64 `json:"end_percentage"`
|
2023-09-18 23:57:18 +00:00
|
|
|
}
|
|
|
|
|
2023-11-03 23:38:31 +00:00
|
|
|
func (q *Queries) AddActivity(ctx context.Context, arg AddActivityParams) (Activity, error) {
|
2023-09-18 23:57:18 +00:00
|
|
|
row := q.db.QueryRowContext(ctx, addActivity,
|
|
|
|
arg.UserID,
|
|
|
|
arg.DocumentID,
|
|
|
|
arg.DeviceID,
|
|
|
|
arg.StartTime,
|
|
|
|
arg.Duration,
|
2023-11-03 23:38:31 +00:00
|
|
|
arg.StartPercentage,
|
|
|
|
arg.EndPercentage,
|
2023-09-18 23:57:18 +00:00
|
|
|
)
|
2023-11-03 23:38:31 +00:00
|
|
|
var i Activity
|
2023-09-18 23:57:18 +00:00
|
|
|
err := row.Scan(
|
|
|
|
&i.ID,
|
|
|
|
&i.UserID,
|
|
|
|
&i.DocumentID,
|
|
|
|
&i.DeviceID,
|
|
|
|
&i.StartTime,
|
2023-11-03 23:38:31 +00:00
|
|
|
&i.StartPercentage,
|
|
|
|
&i.EndPercentage,
|
2023-10-03 20:47:38 +00:00
|
|
|
&i.Duration,
|
2023-09-18 23:57:18 +00:00
|
|
|
&i.CreatedAt,
|
|
|
|
)
|
|
|
|
return i, err
|
|
|
|
}
|
|
|
|
|
2023-09-23 18:14:57 +00:00
|
|
|
const addMetadata = `-- name: AddMetadata :one
|
|
|
|
INSERT INTO metadata (
|
|
|
|
document_id,
|
|
|
|
title,
|
|
|
|
author,
|
|
|
|
description,
|
|
|
|
gbid,
|
|
|
|
olid,
|
|
|
|
isbn10,
|
|
|
|
isbn13
|
|
|
|
)
|
|
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
|
|
|
RETURNING id, document_id, title, author, description, gbid, olid, isbn10, isbn13, created_at
|
|
|
|
`
|
|
|
|
|
|
|
|
type AddMetadataParams struct {
|
|
|
|
DocumentID string `json:"document_id"`
|
|
|
|
Title *string `json:"title"`
|
|
|
|
Author *string `json:"author"`
|
|
|
|
Description *string `json:"description"`
|
|
|
|
Gbid *string `json:"gbid"`
|
|
|
|
Olid *string `json:"olid"`
|
|
|
|
Isbn10 *string `json:"isbn10"`
|
|
|
|
Isbn13 *string `json:"isbn13"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) AddMetadata(ctx context.Context, arg AddMetadataParams) (Metadatum, error) {
|
|
|
|
row := q.db.QueryRowContext(ctx, addMetadata,
|
|
|
|
arg.DocumentID,
|
|
|
|
arg.Title,
|
|
|
|
arg.Author,
|
|
|
|
arg.Description,
|
|
|
|
arg.Gbid,
|
|
|
|
arg.Olid,
|
|
|
|
arg.Isbn10,
|
|
|
|
arg.Isbn13,
|
|
|
|
)
|
|
|
|
var i Metadatum
|
|
|
|
err := row.Scan(
|
|
|
|
&i.ID,
|
|
|
|
&i.DocumentID,
|
|
|
|
&i.Title,
|
|
|
|
&i.Author,
|
|
|
|
&i.Description,
|
|
|
|
&i.Gbid,
|
|
|
|
&i.Olid,
|
|
|
|
&i.Isbn10,
|
|
|
|
&i.Isbn13,
|
|
|
|
&i.CreatedAt,
|
|
|
|
)
|
|
|
|
return i, err
|
|
|
|
}
|
|
|
|
|
2023-09-18 23:57:18 +00:00
|
|
|
const createUser = `-- name: CreateUser :execrows
|
2024-01-28 02:02:08 +00:00
|
|
|
INSERT INTO users (id, pass, auth_hash)
|
|
|
|
VALUES (?, ?, ?)
|
2023-09-18 23:57:18 +00:00
|
|
|
ON CONFLICT DO NOTHING
|
|
|
|
`
|
|
|
|
|
|
|
|
type CreateUserParams struct {
|
2024-01-28 02:02:08 +00:00
|
|
|
ID string `json:"id"`
|
|
|
|
Pass *string `json:"-"`
|
2024-01-29 03:17:58 +00:00
|
|
|
AuthHash *string `json:"auth_hash"`
|
2023-09-18 23:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) CreateUser(ctx context.Context, arg CreateUserParams) (int64, error) {
|
2024-01-28 02:02:08 +00:00
|
|
|
result, err := q.db.ExecContext(ctx, createUser, arg.ID, arg.Pass, arg.AuthHash)
|
2023-09-18 23:57:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return result.RowsAffected()
|
|
|
|
}
|
|
|
|
|
|
|
|
const deleteDocument = `-- name: DeleteDocument :execrows
|
|
|
|
UPDATE documents
|
|
|
|
SET
|
|
|
|
deleted = 1
|
|
|
|
WHERE id = ?1
|
|
|
|
`
|
|
|
|
|
|
|
|
func (q *Queries) DeleteDocument(ctx context.Context, id string) (int64, error) {
|
|
|
|
result, err := q.db.ExecContext(ctx, deleteDocument, id)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return result.RowsAffected()
|
|
|
|
}
|
|
|
|
|
|
|
|
const getActivity = `-- name: GetActivity :many
|
2023-10-03 20:47:38 +00:00
|
|
|
WITH filtered_activity AS (
|
|
|
|
SELECT
|
|
|
|
document_id,
|
2023-11-27 02:41:17 +00:00
|
|
|
device_id,
|
2023-10-03 20:47:38 +00:00
|
|
|
user_id,
|
|
|
|
start_time,
|
|
|
|
duration,
|
2023-11-27 02:41:17 +00:00
|
|
|
ROUND(CAST(start_percentage AS REAL) * 100, 2) AS start_percentage,
|
|
|
|
ROUND(CAST(end_percentage AS REAL) * 100, 2) AS end_percentage,
|
2023-11-03 23:38:31 +00:00
|
|
|
ROUND(CAST(end_percentage - start_percentage AS REAL) * 100, 2) AS read_percentage
|
2023-10-03 20:47:38 +00:00
|
|
|
FROM activity
|
|
|
|
WHERE
|
|
|
|
activity.user_id = ?1
|
|
|
|
AND (
|
|
|
|
(
|
|
|
|
CAST(?2 AS BOOLEAN) = TRUE
|
|
|
|
AND document_id = ?3
|
|
|
|
) OR ?2 = FALSE
|
|
|
|
)
|
|
|
|
ORDER BY start_time DESC
|
|
|
|
LIMIT ?5
|
|
|
|
OFFSET ?4
|
|
|
|
)
|
|
|
|
|
2023-09-21 00:35:01 +00:00
|
|
|
SELECT
|
|
|
|
document_id,
|
2023-11-27 02:41:17 +00:00
|
|
|
device_id,
|
2023-10-06 01:04:57 +00:00
|
|
|
CAST(STRFTIME('%Y-%m-%d %H:%M:%S', activity.start_time, users.time_offset) AS TEXT) AS start_time,
|
2023-09-21 00:35:01 +00:00
|
|
|
title,
|
|
|
|
author,
|
|
|
|
duration,
|
2023-11-27 02:41:17 +00:00
|
|
|
start_percentage,
|
|
|
|
end_percentage,
|
2023-11-03 23:38:31 +00:00
|
|
|
read_percentage
|
2023-10-03 20:47:38 +00:00
|
|
|
FROM filtered_activity AS activity
|
2023-09-21 00:35:01 +00:00
|
|
|
LEFT JOIN documents ON documents.id = activity.document_id
|
|
|
|
LEFT JOIN users ON users.id = activity.user_id
|
2023-09-18 23:57:18 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
type GetActivityParams struct {
|
2023-09-21 00:35:01 +00:00
|
|
|
UserID string `json:"user_id"`
|
|
|
|
DocFilter bool `json:"doc_filter"`
|
|
|
|
DocumentID string `json:"document_id"`
|
|
|
|
Offset int64 `json:"offset"`
|
|
|
|
Limit int64 `json:"limit"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type GetActivityRow struct {
|
2023-11-27 02:41:17 +00:00
|
|
|
DocumentID string `json:"document_id"`
|
|
|
|
DeviceID string `json:"device_id"`
|
|
|
|
StartTime string `json:"start_time"`
|
|
|
|
Title *string `json:"title"`
|
|
|
|
Author *string `json:"author"`
|
|
|
|
Duration int64 `json:"duration"`
|
|
|
|
StartPercentage float64 `json:"start_percentage"`
|
|
|
|
EndPercentage float64 `json:"end_percentage"`
|
|
|
|
ReadPercentage float64 `json:"read_percentage"`
|
2023-09-18 23:57:18 +00:00
|
|
|
}
|
|
|
|
|
2023-09-21 00:35:01 +00:00
|
|
|
func (q *Queries) GetActivity(ctx context.Context, arg GetActivityParams) ([]GetActivityRow, error) {
|
2023-09-18 23:57:18 +00:00
|
|
|
rows, err := q.db.QueryContext(ctx, getActivity,
|
|
|
|
arg.UserID,
|
|
|
|
arg.DocFilter,
|
|
|
|
arg.DocumentID,
|
|
|
|
arg.Offset,
|
|
|
|
arg.Limit,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
2023-09-21 00:35:01 +00:00
|
|
|
var items []GetActivityRow
|
2023-09-18 23:57:18 +00:00
|
|
|
for rows.Next() {
|
2023-09-21 00:35:01 +00:00
|
|
|
var i GetActivityRow
|
2023-09-18 23:57:18 +00:00
|
|
|
if err := rows.Scan(
|
|
|
|
&i.DocumentID,
|
2023-11-27 02:41:17 +00:00
|
|
|
&i.DeviceID,
|
2023-09-18 23:57:18 +00:00
|
|
|
&i.StartTime,
|
2023-09-21 00:35:01 +00:00
|
|
|
&i.Title,
|
|
|
|
&i.Author,
|
2023-09-18 23:57:18 +00:00
|
|
|
&i.Duration,
|
2023-11-27 02:41:17 +00:00
|
|
|
&i.StartPercentage,
|
|
|
|
&i.EndPercentage,
|
2023-11-03 23:38:31 +00:00
|
|
|
&i.ReadPercentage,
|
2023-09-18 23:57:18 +00:00
|
|
|
); 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 getDailyReadStats = `-- name: GetDailyReadStats :many
|
2023-09-21 00:35:01 +00:00
|
|
|
WITH RECURSIVE last_30_days AS (
|
|
|
|
SELECT DATE('now', time_offset) AS date
|
|
|
|
FROM users WHERE users.id = ?1
|
2023-09-18 23:57:18 +00:00
|
|
|
UNION ALL
|
2023-09-19 23:29:55 +00:00
|
|
|
SELECT DATE(date, '-1 days')
|
2023-09-18 23:57:18 +00:00
|
|
|
FROM last_30_days
|
|
|
|
LIMIT 30
|
|
|
|
),
|
2023-10-03 20:47:38 +00:00
|
|
|
filtered_activity AS (
|
|
|
|
SELECT
|
2023-11-03 23:38:31 +00:00
|
|
|
user_id,
|
2023-10-03 20:47:38 +00:00
|
|
|
start_time,
|
2023-11-03 23:38:31 +00:00
|
|
|
duration
|
2023-10-03 20:47:38 +00:00
|
|
|
FROM activity
|
|
|
|
WHERE start_time > DATE('now', '-31 days')
|
|
|
|
AND activity.user_id = ?1
|
|
|
|
),
|
|
|
|
activity_days AS (
|
2023-09-18 23:57:18 +00:00
|
|
|
SELECT
|
2023-10-03 11:37:14 +00:00
|
|
|
SUM(duration) AS seconds_read,
|
2023-09-21 00:35:01 +00:00
|
|
|
DATE(start_time, time_offset) AS day
|
2023-10-03 20:47:38 +00:00
|
|
|
FROM filtered_activity AS activity
|
2023-09-21 00:35:01 +00:00
|
|
|
LEFT JOIN users ON users.id = activity.user_id
|
2023-09-18 23:57:18 +00:00
|
|
|
GROUP BY day
|
|
|
|
)
|
|
|
|
SELECT
|
|
|
|
CAST(date AS TEXT),
|
|
|
|
CAST(CASE
|
|
|
|
WHEN seconds_read IS NULL THEN 0
|
|
|
|
ELSE seconds_read / 60
|
|
|
|
END AS INTEGER) AS minutes_read
|
|
|
|
FROM last_30_days
|
2023-10-03 20:47:38 +00:00
|
|
|
LEFT JOIN activity_days ON activity_days.day == last_30_days.date
|
2023-09-18 23:57:18 +00:00
|
|
|
ORDER BY date DESC
|
|
|
|
LIMIT 30
|
|
|
|
`
|
|
|
|
|
|
|
|
type GetDailyReadStatsRow struct {
|
|
|
|
Date string `json:"date"`
|
|
|
|
MinutesRead int64 `json:"minutes_read"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) GetDailyReadStats(ctx context.Context, userID string) ([]GetDailyReadStatsRow, error) {
|
|
|
|
rows, err := q.db.QueryContext(ctx, getDailyReadStats, userID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
var items []GetDailyReadStatsRow
|
|
|
|
for rows.Next() {
|
|
|
|
var i GetDailyReadStatsRow
|
|
|
|
if err := rows.Scan(&i.Date, &i.MinutesRead); 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 getDatabaseInfo = `-- name: GetDatabaseInfo :one
|
|
|
|
SELECT
|
2023-10-03 11:37:14 +00:00
|
|
|
(SELECT COUNT(rowid) FROM activity WHERE activity.user_id = ?1) AS activity_size,
|
|
|
|
(SELECT COUNT(rowid) FROM documents) AS documents_size,
|
|
|
|
(SELECT COUNT(rowid) FROM document_progress WHERE document_progress.user_id = ?1) AS progress_size,
|
|
|
|
(SELECT COUNT(rowid) FROM devices WHERE devices.user_id = ?1) AS devices_size
|
2023-09-18 23:57:18 +00:00
|
|
|
LIMIT 1
|
|
|
|
`
|
|
|
|
|
|
|
|
type GetDatabaseInfoRow struct {
|
|
|
|
ActivitySize int64 `json:"activity_size"`
|
|
|
|
DocumentsSize int64 `json:"documents_size"`
|
|
|
|
ProgressSize int64 `json:"progress_size"`
|
|
|
|
DevicesSize int64 `json:"devices_size"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) GetDatabaseInfo(ctx context.Context, userID string) (GetDatabaseInfoRow, error) {
|
|
|
|
row := q.db.QueryRowContext(ctx, getDatabaseInfo, userID)
|
|
|
|
var i GetDatabaseInfoRow
|
|
|
|
err := row.Scan(
|
|
|
|
&i.ActivitySize,
|
|
|
|
&i.DocumentsSize,
|
|
|
|
&i.ProgressSize,
|
|
|
|
&i.DevicesSize,
|
|
|
|
)
|
|
|
|
return i, err
|
|
|
|
}
|
|
|
|
|
|
|
|
const getDeletedDocuments = `-- name: GetDeletedDocuments :many
|
|
|
|
SELECT documents.id
|
|
|
|
FROM documents
|
|
|
|
WHERE
|
|
|
|
documents.deleted = true
|
|
|
|
AND documents.id IN (/*SLICE:document_ids*/?)
|
|
|
|
`
|
|
|
|
|
|
|
|
func (q *Queries) GetDeletedDocuments(ctx context.Context, documentIds []string) ([]string, error) {
|
|
|
|
query := getDeletedDocuments
|
|
|
|
var queryParams []interface{}
|
|
|
|
if len(documentIds) > 0 {
|
|
|
|
for _, v := range documentIds {
|
|
|
|
queryParams = append(queryParams, v)
|
|
|
|
}
|
|
|
|
query = strings.Replace(query, "/*SLICE:document_ids*/?", strings.Repeat(",?", len(documentIds))[1:], 1)
|
|
|
|
} else {
|
|
|
|
query = strings.Replace(query, "/*SLICE:document_ids*/?", "NULL", 1)
|
|
|
|
}
|
|
|
|
rows, err := q.db.QueryContext(ctx, query, queryParams...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
var items []string
|
|
|
|
for rows.Next() {
|
|
|
|
var id string
|
|
|
|
if err := rows.Scan(&id); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
items = append(items, id)
|
|
|
|
}
|
|
|
|
if err := rows.Close(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := rows.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return items, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
const getDevice = `-- name: GetDevice :one
|
2023-10-03 20:47:38 +00:00
|
|
|
SELECT id, user_id, device_name, last_synced, created_at, sync FROM devices
|
2023-09-18 23:57:18 +00:00
|
|
|
WHERE id = ?1 LIMIT 1
|
|
|
|
`
|
|
|
|
|
|
|
|
func (q *Queries) GetDevice(ctx context.Context, deviceID string) (Device, error) {
|
|
|
|
row := q.db.QueryRowContext(ctx, getDevice, deviceID)
|
|
|
|
var i Device
|
|
|
|
err := row.Scan(
|
|
|
|
&i.ID,
|
|
|
|
&i.UserID,
|
|
|
|
&i.DeviceName,
|
2023-10-03 20:47:38 +00:00
|
|
|
&i.LastSynced,
|
2023-09-18 23:57:18 +00:00
|
|
|
&i.CreatedAt,
|
|
|
|
&i.Sync,
|
|
|
|
)
|
|
|
|
return i, err
|
|
|
|
}
|
|
|
|
|
|
|
|
const getDevices = `-- name: GetDevices :many
|
2023-09-27 22:58:47 +00:00
|
|
|
SELECT
|
2023-11-27 02:41:17 +00:00
|
|
|
devices.id,
|
2023-09-27 22:58:47 +00:00
|
|
|
devices.device_name,
|
2023-10-06 01:04:57 +00:00
|
|
|
CAST(STRFTIME('%Y-%m-%d %H:%M:%S', devices.created_at, users.time_offset) AS TEXT) AS created_at,
|
|
|
|
CAST(STRFTIME('%Y-%m-%d %H:%M:%S', devices.last_synced, users.time_offset) AS TEXT) AS last_synced
|
2023-10-03 20:47:38 +00:00
|
|
|
FROM devices
|
|
|
|
JOIN users ON users.id = devices.user_id
|
|
|
|
WHERE users.id = ?1
|
2023-10-14 01:06:49 +00:00
|
|
|
ORDER BY devices.last_synced DESC
|
2023-09-18 23:57:18 +00:00
|
|
|
`
|
|
|
|
|
2023-09-27 22:58:47 +00:00
|
|
|
type GetDevicesRow struct {
|
2023-11-27 02:41:17 +00:00
|
|
|
ID string `json:"id"`
|
2023-09-27 22:58:47 +00:00
|
|
|
DeviceName string `json:"device_name"`
|
|
|
|
CreatedAt string `json:"created_at"`
|
2023-10-03 20:47:38 +00:00
|
|
|
LastSynced string `json:"last_synced"`
|
2023-09-18 23:57:18 +00:00
|
|
|
}
|
|
|
|
|
2023-09-27 22:58:47 +00:00
|
|
|
func (q *Queries) GetDevices(ctx context.Context, userID string) ([]GetDevicesRow, error) {
|
|
|
|
rows, err := q.db.QueryContext(ctx, getDevices, userID)
|
2023-09-18 23:57:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
2023-09-27 22:58:47 +00:00
|
|
|
var items []GetDevicesRow
|
2023-09-18 23:57:18 +00:00
|
|
|
for rows.Next() {
|
2023-09-27 22:58:47 +00:00
|
|
|
var i GetDevicesRow
|
2023-11-27 02:41:17 +00:00
|
|
|
if err := rows.Scan(
|
|
|
|
&i.ID,
|
|
|
|
&i.DeviceName,
|
|
|
|
&i.CreatedAt,
|
|
|
|
&i.LastSynced,
|
|
|
|
); err != nil {
|
2023-09-18 23:57:18 +00:00
|
|
|
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 getDocument = `-- name: GetDocument :one
|
2023-10-01 23:17:22 +00:00
|
|
|
SELECT id, md5, filepath, coverfile, title, author, series, series_index, lang, description, words, gbid, olid, isbn10, isbn13, synced, deleted, updated_at, created_at FROM documents
|
2023-09-18 23:57:18 +00:00
|
|
|
WHERE id = ?1 LIMIT 1
|
|
|
|
`
|
|
|
|
|
|
|
|
func (q *Queries) GetDocument(ctx context.Context, documentID string) (Document, error) {
|
|
|
|
row := q.db.QueryRowContext(ctx, getDocument, documentID)
|
|
|
|
var i Document
|
|
|
|
err := row.Scan(
|
|
|
|
&i.ID,
|
|
|
|
&i.Md5,
|
|
|
|
&i.Filepath,
|
2023-09-23 18:14:57 +00:00
|
|
|
&i.Coverfile,
|
2023-09-18 23:57:18 +00:00
|
|
|
&i.Title,
|
|
|
|
&i.Author,
|
|
|
|
&i.Series,
|
|
|
|
&i.SeriesIndex,
|
|
|
|
&i.Lang,
|
|
|
|
&i.Description,
|
2023-10-01 23:17:22 +00:00
|
|
|
&i.Words,
|
2023-09-23 18:14:57 +00:00
|
|
|
&i.Gbid,
|
2023-09-18 23:57:18 +00:00
|
|
|
&i.Olid,
|
2023-09-23 18:14:57 +00:00
|
|
|
&i.Isbn10,
|
|
|
|
&i.Isbn13,
|
2023-09-18 23:57:18 +00:00
|
|
|
&i.Synced,
|
|
|
|
&i.Deleted,
|
|
|
|
&i.UpdatedAt,
|
|
|
|
&i.CreatedAt,
|
|
|
|
)
|
|
|
|
return i, err
|
|
|
|
}
|
|
|
|
|
2024-01-01 04:12:46 +00:00
|
|
|
const getDocumentProgress = `-- name: GetDocumentProgress :one
|
|
|
|
SELECT
|
|
|
|
document_progress.user_id, document_progress.document_id, document_progress.device_id, document_progress.percentage, document_progress.progress, document_progress.created_at,
|
|
|
|
devices.device_name
|
|
|
|
FROM document_progress
|
|
|
|
JOIN devices ON document_progress.device_id = devices.id
|
|
|
|
WHERE
|
|
|
|
document_progress.user_id = ?1
|
|
|
|
AND document_progress.document_id = ?2
|
|
|
|
ORDER BY
|
|
|
|
document_progress.created_at
|
|
|
|
DESC
|
|
|
|
LIMIT 1
|
|
|
|
`
|
|
|
|
|
|
|
|
type GetDocumentProgressParams struct {
|
|
|
|
UserID string `json:"user_id"`
|
|
|
|
DocumentID string `json:"document_id"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type GetDocumentProgressRow struct {
|
|
|
|
UserID string `json:"user_id"`
|
|
|
|
DocumentID string `json:"document_id"`
|
|
|
|
DeviceID string `json:"device_id"`
|
|
|
|
Percentage float64 `json:"percentage"`
|
|
|
|
Progress string `json:"progress"`
|
|
|
|
CreatedAt string `json:"created_at"`
|
|
|
|
DeviceName string `json:"device_name"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) GetDocumentProgress(ctx context.Context, arg GetDocumentProgressParams) (GetDocumentProgressRow, error) {
|
|
|
|
row := q.db.QueryRowContext(ctx, getDocumentProgress, arg.UserID, arg.DocumentID)
|
|
|
|
var i GetDocumentProgressRow
|
|
|
|
err := row.Scan(
|
|
|
|
&i.UserID,
|
|
|
|
&i.DocumentID,
|
|
|
|
&i.DeviceID,
|
|
|
|
&i.Percentage,
|
|
|
|
&i.Progress,
|
|
|
|
&i.CreatedAt,
|
|
|
|
&i.DeviceName,
|
|
|
|
)
|
|
|
|
return i, err
|
|
|
|
}
|
|
|
|
|
2023-09-23 02:12:36 +00:00
|
|
|
const getDocumentWithStats = `-- name: GetDocumentWithStats :one
|
|
|
|
SELECT
|
2023-10-03 20:47:38 +00:00
|
|
|
docs.id,
|
|
|
|
docs.title,
|
|
|
|
docs.author,
|
|
|
|
docs.description,
|
|
|
|
docs.isbn10,
|
|
|
|
docs.isbn13,
|
|
|
|
docs.filepath,
|
|
|
|
docs.words,
|
|
|
|
|
2024-01-24 04:00:51 +00:00
|
|
|
CAST(COALESCE(dus.total_wpm, 0.0) AS INTEGER) AS wpm,
|
2023-11-03 23:38:31 +00:00
|
|
|
COALESCE(dus.read_percentage, 0) AS read_percentage,
|
2023-10-03 20:47:38 +00:00
|
|
|
COALESCE(dus.total_time_seconds, 0) AS total_time_seconds,
|
2023-10-06 01:04:57 +00:00
|
|
|
STRFTIME('%Y-%m-%d %H:%M:%S', COALESCE(dus.last_read, "1970-01-01"), users.time_offset)
|
2023-10-03 20:47:38 +00:00
|
|
|
AS last_read,
|
2023-11-03 23:38:31 +00:00
|
|
|
ROUND(CAST(CASE
|
2023-10-03 20:47:38 +00:00
|
|
|
WHEN dus.percentage IS NULL THEN 0.0
|
2023-11-03 23:38:31 +00:00
|
|
|
WHEN (dus.percentage * 100.0) > 97.0 THEN 100.0
|
|
|
|
ELSE dus.percentage * 100.0
|
|
|
|
END AS REAL), 2) AS percentage,
|
2023-10-01 23:17:22 +00:00
|
|
|
CAST(CASE
|
2023-10-03 20:47:38 +00:00
|
|
|
WHEN dus.total_time_seconds IS NULL THEN 0.0
|
|
|
|
ELSE
|
2023-11-03 23:38:31 +00:00
|
|
|
CAST(dus.total_time_seconds AS REAL)
|
|
|
|
/ (dus.read_percentage * 100.0)
|
|
|
|
END AS INTEGER) AS seconds_per_percent
|
2023-10-03 20:47:38 +00:00
|
|
|
FROM documents AS docs
|
2023-09-23 02:12:36 +00:00
|
|
|
LEFT JOIN users ON users.id = ?1
|
2023-10-03 20:47:38 +00:00
|
|
|
LEFT JOIN
|
|
|
|
document_user_statistics AS dus
|
|
|
|
ON dus.document_id = docs.id AND dus.user_id = ?1
|
|
|
|
WHERE users.id = ?1
|
|
|
|
AND docs.id = ?2
|
2023-09-23 02:12:36 +00:00
|
|
|
LIMIT 1
|
|
|
|
`
|
|
|
|
|
|
|
|
type GetDocumentWithStatsParams struct {
|
|
|
|
UserID string `json:"user_id"`
|
|
|
|
DocumentID string `json:"document_id"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type GetDocumentWithStatsRow struct {
|
2023-11-03 23:38:31 +00:00
|
|
|
ID string `json:"id"`
|
|
|
|
Title *string `json:"title"`
|
|
|
|
Author *string `json:"author"`
|
|
|
|
Description *string `json:"description"`
|
|
|
|
Isbn10 *string `json:"isbn10"`
|
|
|
|
Isbn13 *string `json:"isbn13"`
|
|
|
|
Filepath *string `json:"filepath"`
|
|
|
|
Words *int64 `json:"words"`
|
|
|
|
Wpm int64 `json:"wpm"`
|
|
|
|
ReadPercentage float64 `json:"read_percentage"`
|
|
|
|
TotalTimeSeconds int64 `json:"total_time_seconds"`
|
|
|
|
LastRead interface{} `json:"last_read"`
|
|
|
|
Percentage float64 `json:"percentage"`
|
|
|
|
SecondsPerPercent int64 `json:"seconds_per_percent"`
|
2023-09-23 02:12:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) GetDocumentWithStats(ctx context.Context, arg GetDocumentWithStatsParams) (GetDocumentWithStatsRow, error) {
|
|
|
|
row := q.db.QueryRowContext(ctx, getDocumentWithStats, arg.UserID, arg.DocumentID)
|
|
|
|
var i GetDocumentWithStatsRow
|
|
|
|
err := row.Scan(
|
|
|
|
&i.ID,
|
|
|
|
&i.Title,
|
|
|
|
&i.Author,
|
|
|
|
&i.Description,
|
2023-09-23 18:14:57 +00:00
|
|
|
&i.Isbn10,
|
|
|
|
&i.Isbn13,
|
2023-10-03 20:47:38 +00:00
|
|
|
&i.Filepath,
|
|
|
|
&i.Words,
|
|
|
|
&i.Wpm,
|
2023-11-03 23:38:31 +00:00
|
|
|
&i.ReadPercentage,
|
2023-10-01 23:17:22 +00:00
|
|
|
&i.TotalTimeSeconds,
|
2023-09-23 02:12:36 +00:00
|
|
|
&i.LastRead,
|
|
|
|
&i.Percentage,
|
2023-11-03 23:38:31 +00:00
|
|
|
&i.SecondsPerPercent,
|
2023-09-23 02:12:36 +00:00
|
|
|
)
|
|
|
|
return i, err
|
|
|
|
}
|
|
|
|
|
2023-09-18 23:57:18 +00:00
|
|
|
const getDocuments = `-- name: GetDocuments :many
|
2023-10-01 23:17:22 +00:00
|
|
|
SELECT id, md5, filepath, coverfile, title, author, series, series_index, lang, description, words, gbid, olid, isbn10, isbn13, synced, deleted, updated_at, created_at FROM documents
|
2023-09-18 23:57:18 +00:00
|
|
|
ORDER BY created_at DESC
|
|
|
|
LIMIT ?2
|
|
|
|
OFFSET ?1
|
|
|
|
`
|
|
|
|
|
|
|
|
type GetDocumentsParams struct {
|
|
|
|
Offset int64 `json:"offset"`
|
|
|
|
Limit int64 `json:"limit"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) GetDocuments(ctx context.Context, arg GetDocumentsParams) ([]Document, error) {
|
|
|
|
rows, err := q.db.QueryContext(ctx, getDocuments, arg.Offset, arg.Limit)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
var items []Document
|
|
|
|
for rows.Next() {
|
|
|
|
var i Document
|
|
|
|
if err := rows.Scan(
|
|
|
|
&i.ID,
|
|
|
|
&i.Md5,
|
|
|
|
&i.Filepath,
|
2023-09-23 18:14:57 +00:00
|
|
|
&i.Coverfile,
|
2023-09-18 23:57:18 +00:00
|
|
|
&i.Title,
|
|
|
|
&i.Author,
|
|
|
|
&i.Series,
|
|
|
|
&i.SeriesIndex,
|
|
|
|
&i.Lang,
|
|
|
|
&i.Description,
|
2023-10-01 23:17:22 +00:00
|
|
|
&i.Words,
|
2023-09-23 18:14:57 +00:00
|
|
|
&i.Gbid,
|
2023-09-18 23:57:18 +00:00
|
|
|
&i.Olid,
|
2023-09-23 18:14:57 +00:00
|
|
|
&i.Isbn10,
|
|
|
|
&i.Isbn13,
|
2023-09-18 23:57:18 +00:00
|
|
|
&i.Synced,
|
|
|
|
&i.Deleted,
|
|
|
|
&i.UpdatedAt,
|
|
|
|
&i.CreatedAt,
|
|
|
|
); 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
|
|
|
|
}
|
|
|
|
|
2023-11-18 04:10:59 +00:00
|
|
|
const getDocumentsSize = `-- name: GetDocumentsSize :one
|
|
|
|
SELECT
|
|
|
|
COUNT(rowid) AS length
|
|
|
|
FROM documents AS docs
|
|
|
|
WHERE ?1 IS NULL OR (
|
|
|
|
docs.title LIKE ?1 OR
|
|
|
|
docs.author LIKE ?1
|
|
|
|
)
|
|
|
|
LIMIT 1
|
|
|
|
`
|
|
|
|
|
|
|
|
func (q *Queries) GetDocumentsSize(ctx context.Context, query interface{}) (int64, error) {
|
|
|
|
row := q.db.QueryRowContext(ctx, getDocumentsSize, query)
|
|
|
|
var length int64
|
|
|
|
err := row.Scan(&length)
|
|
|
|
return length, err
|
|
|
|
}
|
|
|
|
|
2023-09-18 23:57:18 +00:00
|
|
|
const getDocumentsWithStats = `-- name: GetDocumentsWithStats :many
|
|
|
|
SELECT
|
2023-10-03 20:47:38 +00:00
|
|
|
docs.id,
|
|
|
|
docs.title,
|
|
|
|
docs.author,
|
|
|
|
docs.description,
|
|
|
|
docs.isbn10,
|
|
|
|
docs.isbn13,
|
|
|
|
docs.filepath,
|
|
|
|
docs.words,
|
|
|
|
|
2024-01-24 04:00:51 +00:00
|
|
|
CAST(COALESCE(dus.total_wpm, 0.0) AS INTEGER) AS wpm,
|
2023-11-03 23:38:31 +00:00
|
|
|
COALESCE(dus.read_percentage, 0) AS read_percentage,
|
2023-10-03 20:47:38 +00:00
|
|
|
COALESCE(dus.total_time_seconds, 0) AS total_time_seconds,
|
2023-10-06 01:04:57 +00:00
|
|
|
STRFTIME('%Y-%m-%d %H:%M:%S', COALESCE(dus.last_read, "1970-01-01"), users.time_offset)
|
2023-10-03 20:47:38 +00:00
|
|
|
AS last_read,
|
2023-11-03 23:38:31 +00:00
|
|
|
ROUND(CAST(CASE
|
2023-10-03 20:47:38 +00:00
|
|
|
WHEN dus.percentage IS NULL THEN 0.0
|
2023-11-03 23:38:31 +00:00
|
|
|
WHEN (dus.percentage * 100.0) > 97.0 THEN 100.0
|
|
|
|
ELSE dus.percentage * 100.0
|
|
|
|
END AS REAL), 2) AS percentage,
|
|
|
|
|
2023-10-03 20:47:38 +00:00
|
|
|
CASE
|
|
|
|
WHEN dus.total_time_seconds IS NULL THEN 0.0
|
|
|
|
ELSE
|
|
|
|
ROUND(
|
|
|
|
CAST(dus.total_time_seconds AS REAL)
|
2023-11-03 23:38:31 +00:00
|
|
|
/ (dus.read_percentage * 100.0)
|
2023-10-03 20:47:38 +00:00
|
|
|
)
|
2023-11-03 23:38:31 +00:00
|
|
|
END AS seconds_per_percent
|
2023-10-03 20:47:38 +00:00
|
|
|
FROM documents AS docs
|
2023-09-21 00:35:01 +00:00
|
|
|
LEFT JOIN users ON users.id = ?1
|
2023-10-03 20:47:38 +00:00
|
|
|
LEFT JOIN
|
|
|
|
document_user_statistics AS dus
|
|
|
|
ON dus.document_id = docs.id AND dus.user_id = ?1
|
2023-11-18 02:24:19 +00:00
|
|
|
WHERE
|
|
|
|
docs.deleted = false AND (
|
|
|
|
?2 IS NULL OR (
|
|
|
|
docs.title LIKE ?2 OR
|
|
|
|
docs.author LIKE ?2
|
|
|
|
)
|
|
|
|
)
|
2023-10-03 20:47:38 +00:00
|
|
|
ORDER BY dus.last_read DESC, docs.created_at DESC
|
2023-11-18 02:24:19 +00:00
|
|
|
LIMIT ?4
|
|
|
|
OFFSET ?3
|
2023-09-18 23:57:18 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
type GetDocumentsWithStatsParams struct {
|
2023-11-18 02:24:19 +00:00
|
|
|
UserID string `json:"user_id"`
|
|
|
|
Query interface{} `json:"query"`
|
|
|
|
Offset int64 `json:"offset"`
|
|
|
|
Limit int64 `json:"limit"`
|
2023-09-18 23:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type GetDocumentsWithStatsRow struct {
|
2023-11-03 23:38:31 +00:00
|
|
|
ID string `json:"id"`
|
|
|
|
Title *string `json:"title"`
|
|
|
|
Author *string `json:"author"`
|
|
|
|
Description *string `json:"description"`
|
|
|
|
Isbn10 *string `json:"isbn10"`
|
|
|
|
Isbn13 *string `json:"isbn13"`
|
|
|
|
Filepath *string `json:"filepath"`
|
|
|
|
Words *int64 `json:"words"`
|
|
|
|
Wpm int64 `json:"wpm"`
|
|
|
|
ReadPercentage float64 `json:"read_percentage"`
|
|
|
|
TotalTimeSeconds int64 `json:"total_time_seconds"`
|
|
|
|
LastRead interface{} `json:"last_read"`
|
|
|
|
Percentage float64 `json:"percentage"`
|
|
|
|
SecondsPerPercent interface{} `json:"seconds_per_percent"`
|
2023-09-18 23:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) GetDocumentsWithStats(ctx context.Context, arg GetDocumentsWithStatsParams) ([]GetDocumentsWithStatsRow, error) {
|
2023-11-18 02:24:19 +00:00
|
|
|
rows, err := q.db.QueryContext(ctx, getDocumentsWithStats,
|
|
|
|
arg.UserID,
|
|
|
|
arg.Query,
|
|
|
|
arg.Offset,
|
|
|
|
arg.Limit,
|
|
|
|
)
|
2023-09-18 23:57:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
var items []GetDocumentsWithStatsRow
|
|
|
|
for rows.Next() {
|
|
|
|
var i GetDocumentsWithStatsRow
|
|
|
|
if err := rows.Scan(
|
|
|
|
&i.ID,
|
|
|
|
&i.Title,
|
|
|
|
&i.Author,
|
|
|
|
&i.Description,
|
2023-09-23 18:14:57 +00:00
|
|
|
&i.Isbn10,
|
|
|
|
&i.Isbn13,
|
2023-10-03 20:47:38 +00:00
|
|
|
&i.Filepath,
|
|
|
|
&i.Words,
|
|
|
|
&i.Wpm,
|
2023-11-03 23:38:31 +00:00
|
|
|
&i.ReadPercentage,
|
2023-10-01 23:17:22 +00:00
|
|
|
&i.TotalTimeSeconds,
|
2023-09-18 23:57:18 +00:00
|
|
|
&i.LastRead,
|
|
|
|
&i.Percentage,
|
2023-11-03 23:38:31 +00:00
|
|
|
&i.SecondsPerPercent,
|
2023-09-18 23:57:18 +00:00
|
|
|
); 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 getLastActivity = `-- name: GetLastActivity :one
|
|
|
|
SELECT start_time
|
|
|
|
FROM activity
|
|
|
|
WHERE device_id = ?1
|
|
|
|
AND user_id = ?2
|
|
|
|
ORDER BY start_time DESC LIMIT 1
|
|
|
|
`
|
|
|
|
|
|
|
|
type GetLastActivityParams struct {
|
|
|
|
DeviceID string `json:"device_id"`
|
|
|
|
UserID string `json:"user_id"`
|
|
|
|
}
|
|
|
|
|
2023-10-06 01:04:57 +00:00
|
|
|
func (q *Queries) GetLastActivity(ctx context.Context, arg GetLastActivityParams) (string, error) {
|
2023-09-18 23:57:18 +00:00
|
|
|
row := q.db.QueryRowContext(ctx, getLastActivity, arg.DeviceID, arg.UserID)
|
2023-10-06 01:04:57 +00:00
|
|
|
var start_time string
|
2023-09-18 23:57:18 +00:00
|
|
|
err := row.Scan(&start_time)
|
|
|
|
return start_time, err
|
|
|
|
}
|
|
|
|
|
|
|
|
const getMissingDocuments = `-- name: GetMissingDocuments :many
|
2023-10-01 23:17:22 +00:00
|
|
|
SELECT documents.id, documents.md5, documents.filepath, documents.coverfile, documents.title, documents.author, documents.series, documents.series_index, documents.lang, documents.description, documents.words, documents.gbid, documents.olid, documents.isbn10, documents.isbn13, documents.synced, documents.deleted, documents.updated_at, documents.created_at FROM documents
|
2023-09-18 23:57:18 +00:00
|
|
|
WHERE
|
|
|
|
documents.filepath IS NOT NULL
|
|
|
|
AND documents.deleted = false
|
|
|
|
AND documents.id NOT IN (/*SLICE:document_ids*/?)
|
|
|
|
`
|
|
|
|
|
|
|
|
func (q *Queries) GetMissingDocuments(ctx context.Context, documentIds []string) ([]Document, error) {
|
|
|
|
query := getMissingDocuments
|
|
|
|
var queryParams []interface{}
|
|
|
|
if len(documentIds) > 0 {
|
|
|
|
for _, v := range documentIds {
|
|
|
|
queryParams = append(queryParams, v)
|
|
|
|
}
|
|
|
|
query = strings.Replace(query, "/*SLICE:document_ids*/?", strings.Repeat(",?", len(documentIds))[1:], 1)
|
|
|
|
} else {
|
|
|
|
query = strings.Replace(query, "/*SLICE:document_ids*/?", "NULL", 1)
|
|
|
|
}
|
|
|
|
rows, err := q.db.QueryContext(ctx, query, queryParams...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
var items []Document
|
|
|
|
for rows.Next() {
|
|
|
|
var i Document
|
|
|
|
if err := rows.Scan(
|
|
|
|
&i.ID,
|
|
|
|
&i.Md5,
|
|
|
|
&i.Filepath,
|
2023-09-23 18:14:57 +00:00
|
|
|
&i.Coverfile,
|
2023-09-18 23:57:18 +00:00
|
|
|
&i.Title,
|
|
|
|
&i.Author,
|
|
|
|
&i.Series,
|
|
|
|
&i.SeriesIndex,
|
|
|
|
&i.Lang,
|
|
|
|
&i.Description,
|
2023-10-01 23:17:22 +00:00
|
|
|
&i.Words,
|
2023-09-23 18:14:57 +00:00
|
|
|
&i.Gbid,
|
2023-09-18 23:57:18 +00:00
|
|
|
&i.Olid,
|
2023-09-23 18:14:57 +00:00
|
|
|
&i.Isbn10,
|
|
|
|
&i.Isbn13,
|
2023-09-18 23:57:18 +00:00
|
|
|
&i.Synced,
|
|
|
|
&i.Deleted,
|
|
|
|
&i.UpdatedAt,
|
|
|
|
&i.CreatedAt,
|
|
|
|
); 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
|
|
|
|
}
|
|
|
|
|
2024-01-01 04:12:46 +00:00
|
|
|
const getProgress = `-- name: GetProgress :many
|
2023-09-18 23:57:18 +00:00
|
|
|
SELECT
|
2024-01-01 04:12:46 +00:00
|
|
|
documents.title,
|
|
|
|
documents.author,
|
|
|
|
devices.device_name,
|
|
|
|
ROUND(CAST(progress.percentage AS REAL) * 100, 2) AS percentage,
|
|
|
|
progress.document_id,
|
|
|
|
progress.user_id,
|
|
|
|
CAST(STRFTIME('%Y-%m-%d %H:%M:%S', progress.created_at, users.time_offset) AS TEXT) AS created_at
|
|
|
|
FROM document_progress AS progress
|
|
|
|
LEFT JOIN users ON progress.user_id = users.id
|
|
|
|
LEFT JOIN devices ON progress.device_id = devices.id
|
|
|
|
LEFT JOIN documents ON progress.document_id = documents.id
|
2023-09-18 23:57:18 +00:00
|
|
|
WHERE
|
2024-01-01 04:12:46 +00:00
|
|
|
progress.user_id = ?1
|
|
|
|
AND (
|
|
|
|
(
|
|
|
|
CAST(?2 AS BOOLEAN) = TRUE
|
|
|
|
AND document_id = ?3
|
|
|
|
) OR ?2 = FALSE
|
|
|
|
)
|
|
|
|
ORDER BY created_at DESC
|
|
|
|
LIMIT ?5
|
|
|
|
OFFSET ?4
|
2023-09-18 23:57:18 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
type GetProgressParams struct {
|
|
|
|
UserID string `json:"user_id"`
|
2024-01-01 04:12:46 +00:00
|
|
|
DocFilter bool `json:"doc_filter"`
|
2023-09-18 23:57:18 +00:00
|
|
|
DocumentID string `json:"document_id"`
|
2024-01-01 04:12:46 +00:00
|
|
|
Offset int64 `json:"offset"`
|
|
|
|
Limit int64 `json:"limit"`
|
2023-09-18 23:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type GetProgressRow struct {
|
2024-01-01 04:12:46 +00:00
|
|
|
Title *string `json:"title"`
|
|
|
|
Author *string `json:"author"`
|
|
|
|
DeviceName string `json:"device_name"`
|
2023-10-06 01:04:57 +00:00
|
|
|
Percentage float64 `json:"percentage"`
|
2024-01-01 04:12:46 +00:00
|
|
|
DocumentID string `json:"document_id"`
|
|
|
|
UserID string `json:"user_id"`
|
2023-10-06 01:04:57 +00:00
|
|
|
CreatedAt string `json:"created_at"`
|
2023-09-18 23:57:18 +00:00
|
|
|
}
|
|
|
|
|
2024-01-01 04:12:46 +00:00
|
|
|
func (q *Queries) GetProgress(ctx context.Context, arg GetProgressParams) ([]GetProgressRow, error) {
|
|
|
|
rows, err := q.db.QueryContext(ctx, getProgress,
|
|
|
|
arg.UserID,
|
|
|
|
arg.DocFilter,
|
|
|
|
arg.DocumentID,
|
|
|
|
arg.Offset,
|
|
|
|
arg.Limit,
|
2023-09-18 23:57:18 +00:00
|
|
|
)
|
2024-01-01 04:12:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
var items []GetProgressRow
|
|
|
|
for rows.Next() {
|
|
|
|
var i GetProgressRow
|
|
|
|
if err := rows.Scan(
|
|
|
|
&i.Title,
|
|
|
|
&i.Author,
|
|
|
|
&i.DeviceName,
|
|
|
|
&i.Percentage,
|
|
|
|
&i.DocumentID,
|
|
|
|
&i.UserID,
|
|
|
|
&i.CreatedAt,
|
|
|
|
); 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
|
2023-09-18 23:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const getUser = `-- name: GetUser :one
|
2024-01-28 02:02:08 +00:00
|
|
|
SELECT id, pass, auth_hash, admin, time_offset, created_at FROM users
|
2023-09-18 23:57:18 +00:00
|
|
|
WHERE id = ?1 LIMIT 1
|
|
|
|
`
|
|
|
|
|
|
|
|
func (q *Queries) GetUser(ctx context.Context, userID string) (User, error) {
|
|
|
|
row := q.db.QueryRowContext(ctx, getUser, userID)
|
|
|
|
var i User
|
|
|
|
err := row.Scan(
|
|
|
|
&i.ID,
|
|
|
|
&i.Pass,
|
2024-01-28 02:02:08 +00:00
|
|
|
&i.AuthHash,
|
2023-09-18 23:57:18 +00:00
|
|
|
&i.Admin,
|
2023-09-21 00:35:01 +00:00
|
|
|
&i.TimeOffset,
|
2023-09-18 23:57:18 +00:00
|
|
|
&i.CreatedAt,
|
|
|
|
)
|
|
|
|
return i, err
|
|
|
|
}
|
|
|
|
|
2024-01-24 04:00:51 +00:00
|
|
|
const getUserStatistics = `-- name: GetUserStatistics :many
|
|
|
|
SELECT
|
|
|
|
user_id,
|
|
|
|
|
|
|
|
CAST(SUM(total_words_read) AS INTEGER) AS total_words_read,
|
|
|
|
CAST(SUM(total_time_seconds) AS INTEGER) AS total_seconds,
|
2024-01-24 23:43:33 +00:00
|
|
|
ROUND(COALESCE(CAST(SUM(total_words_read) AS REAL) / (SUM(total_time_seconds) / 60.0), 0.0), 2)
|
2024-01-24 04:00:51 +00:00
|
|
|
AS total_wpm,
|
|
|
|
|
|
|
|
CAST(SUM(yearly_words_read) AS INTEGER) AS yearly_words_read,
|
|
|
|
CAST(SUM(yearly_time_seconds) AS INTEGER) AS yearly_seconds,
|
2024-01-24 23:43:33 +00:00
|
|
|
ROUND(COALESCE(CAST(SUM(yearly_words_read) AS REAL) / (SUM(yearly_time_seconds) / 60.0), 0.0), 2)
|
2024-01-24 04:00:51 +00:00
|
|
|
AS yearly_wpm,
|
|
|
|
|
|
|
|
CAST(SUM(monthly_words_read) AS INTEGER) AS monthly_words_read,
|
|
|
|
CAST(SUM(monthly_time_seconds) AS INTEGER) AS monthly_seconds,
|
2024-01-24 23:43:33 +00:00
|
|
|
ROUND(COALESCE(CAST(SUM(monthly_words_read) AS REAL) / (SUM(monthly_time_seconds) / 60.0), 0.0), 2)
|
2024-01-24 04:00:51 +00:00
|
|
|
AS monthly_wpm,
|
|
|
|
|
|
|
|
CAST(SUM(weekly_words_read) AS INTEGER) AS weekly_words_read,
|
|
|
|
CAST(SUM(weekly_time_seconds) AS INTEGER) AS weekly_seconds,
|
2024-01-24 23:43:33 +00:00
|
|
|
ROUND(COALESCE(CAST(SUM(weekly_words_read) AS REAL) / (SUM(weekly_time_seconds) / 60.0), 0.0), 2)
|
2024-01-24 04:00:51 +00:00
|
|
|
AS weekly_wpm
|
|
|
|
|
|
|
|
FROM document_user_statistics
|
|
|
|
WHERE total_words_read > 0
|
|
|
|
GROUP BY user_id
|
|
|
|
ORDER BY total_wpm DESC
|
|
|
|
`
|
|
|
|
|
|
|
|
type GetUserStatisticsRow struct {
|
|
|
|
UserID string `json:"user_id"`
|
|
|
|
TotalWordsRead int64 `json:"total_words_read"`
|
|
|
|
TotalSeconds int64 `json:"total_seconds"`
|
|
|
|
TotalWpm float64 `json:"total_wpm"`
|
|
|
|
YearlyWordsRead int64 `json:"yearly_words_read"`
|
|
|
|
YearlySeconds int64 `json:"yearly_seconds"`
|
|
|
|
YearlyWpm float64 `json:"yearly_wpm"`
|
|
|
|
MonthlyWordsRead int64 `json:"monthly_words_read"`
|
|
|
|
MonthlySeconds int64 `json:"monthly_seconds"`
|
|
|
|
MonthlyWpm float64 `json:"monthly_wpm"`
|
|
|
|
WeeklyWordsRead int64 `json:"weekly_words_read"`
|
|
|
|
WeeklySeconds int64 `json:"weekly_seconds"`
|
|
|
|
WeeklyWpm float64 `json:"weekly_wpm"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) GetUserStatistics(ctx context.Context) ([]GetUserStatisticsRow, error) {
|
|
|
|
rows, err := q.db.QueryContext(ctx, getUserStatistics)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
var items []GetUserStatisticsRow
|
|
|
|
for rows.Next() {
|
|
|
|
var i GetUserStatisticsRow
|
|
|
|
if err := rows.Scan(
|
|
|
|
&i.UserID,
|
|
|
|
&i.TotalWordsRead,
|
|
|
|
&i.TotalSeconds,
|
|
|
|
&i.TotalWpm,
|
|
|
|
&i.YearlyWordsRead,
|
|
|
|
&i.YearlySeconds,
|
|
|
|
&i.YearlyWpm,
|
|
|
|
&i.MonthlyWordsRead,
|
|
|
|
&i.MonthlySeconds,
|
|
|
|
&i.MonthlyWpm,
|
|
|
|
&i.WeeklyWordsRead,
|
|
|
|
&i.WeeklySeconds,
|
|
|
|
&i.WeeklyWpm,
|
|
|
|
); 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
|
|
|
|
}
|
|
|
|
|
2023-10-03 20:47:38 +00:00
|
|
|
const getUserStreaks = `-- name: GetUserStreaks :many
|
|
|
|
SELECT user_id, "window", max_streak, max_streak_start_date, max_streak_end_date, current_streak, current_streak_start_date, current_streak_end_date FROM user_streaks
|
|
|
|
WHERE user_id = ?1
|
2023-09-18 23:57:18 +00:00
|
|
|
`
|
|
|
|
|
2023-10-03 20:47:38 +00:00
|
|
|
func (q *Queries) GetUserStreaks(ctx context.Context, userID string) ([]UserStreak, error) {
|
|
|
|
rows, err := q.db.QueryContext(ctx, getUserStreaks, userID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
var items []UserStreak
|
|
|
|
for rows.Next() {
|
|
|
|
var i UserStreak
|
|
|
|
if err := rows.Scan(
|
|
|
|
&i.UserID,
|
|
|
|
&i.Window,
|
|
|
|
&i.MaxStreak,
|
|
|
|
&i.MaxStreakStartDate,
|
|
|
|
&i.MaxStreakEndDate,
|
|
|
|
&i.CurrentStreak,
|
|
|
|
&i.CurrentStreakStartDate,
|
|
|
|
&i.CurrentStreakEndDate,
|
|
|
|
); 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
|
2023-09-18 23:57:18 +00:00
|
|
|
}
|
|
|
|
|
2024-01-20 19:26:26 +00:00
|
|
|
const getUsers = `-- name: GetUsers :many
|
2024-01-28 02:02:08 +00:00
|
|
|
SELECT id, pass, auth_hash, admin, time_offset, created_at FROM users
|
2024-01-20 19:26:26 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
func (q *Queries) GetUsers(ctx context.Context) ([]User, error) {
|
|
|
|
rows, err := q.db.QueryContext(ctx, getUsers)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
var items []User
|
|
|
|
for rows.Next() {
|
|
|
|
var i User
|
|
|
|
if err := rows.Scan(
|
|
|
|
&i.ID,
|
|
|
|
&i.Pass,
|
2024-01-28 02:02:08 +00:00
|
|
|
&i.AuthHash,
|
2024-01-20 19:26:26 +00:00
|
|
|
&i.Admin,
|
|
|
|
&i.TimeOffset,
|
|
|
|
&i.CreatedAt,
|
|
|
|
); 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
|
|
|
|
}
|
|
|
|
|
2023-09-18 23:57:18 +00:00
|
|
|
const getWantedDocuments = `-- name: GetWantedDocuments :many
|
2023-09-19 23:29:55 +00:00
|
|
|
SELECT
|
|
|
|
CAST(value AS TEXT) AS id,
|
|
|
|
CAST((documents.filepath IS NULL) AS BOOLEAN) AS want_file,
|
2023-11-03 23:38:31 +00:00
|
|
|
CAST((documents.id IS NULL) AS BOOLEAN) AS want_metadata
|
2023-09-18 23:57:18 +00:00
|
|
|
FROM json_each(?1)
|
|
|
|
LEFT JOIN documents
|
|
|
|
ON value = documents.id
|
|
|
|
WHERE (
|
|
|
|
documents.id IS NOT NULL
|
2023-09-19 23:29:55 +00:00
|
|
|
AND documents.deleted = false
|
2023-11-03 23:38:31 +00:00
|
|
|
AND documents.filepath IS NULL
|
2023-09-18 23:57:18 +00:00
|
|
|
)
|
|
|
|
OR (documents.id IS NULL)
|
|
|
|
OR CAST(?1 AS TEXT) != CAST(?1 AS TEXT)
|
|
|
|
`
|
|
|
|
|
2023-09-19 23:29:55 +00:00
|
|
|
type GetWantedDocumentsRow struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
WantFile bool `json:"want_file"`
|
|
|
|
WantMetadata bool `json:"want_metadata"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) GetWantedDocuments(ctx context.Context, documentIds string) ([]GetWantedDocumentsRow, error) {
|
2023-09-18 23:57:18 +00:00
|
|
|
rows, err := q.db.QueryContext(ctx, getWantedDocuments, documentIds)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
2023-09-19 23:29:55 +00:00
|
|
|
var items []GetWantedDocumentsRow
|
2023-09-18 23:57:18 +00:00
|
|
|
for rows.Next() {
|
2023-09-19 23:29:55 +00:00
|
|
|
var i GetWantedDocumentsRow
|
|
|
|
if err := rows.Scan(&i.ID, &i.WantFile, &i.WantMetadata); err != nil {
|
2023-09-18 23:57:18 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2023-09-19 23:29:55 +00:00
|
|
|
items = append(items, i)
|
2023-09-18 23:57:18 +00:00
|
|
|
}
|
|
|
|
if err := rows.Close(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := rows.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return items, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
const updateProgress = `-- name: UpdateProgress :one
|
|
|
|
INSERT OR REPLACE INTO document_progress (
|
|
|
|
user_id,
|
|
|
|
document_id,
|
|
|
|
device_id,
|
|
|
|
percentage,
|
|
|
|
progress
|
|
|
|
)
|
|
|
|
VALUES (?, ?, ?, ?, ?)
|
|
|
|
RETURNING user_id, document_id, device_id, percentage, progress, created_at
|
|
|
|
`
|
|
|
|
|
|
|
|
type UpdateProgressParams struct {
|
|
|
|
UserID string `json:"user_id"`
|
|
|
|
DocumentID string `json:"document_id"`
|
|
|
|
DeviceID string `json:"device_id"`
|
|
|
|
Percentage float64 `json:"percentage"`
|
|
|
|
Progress string `json:"progress"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) UpdateProgress(ctx context.Context, arg UpdateProgressParams) (DocumentProgress, error) {
|
|
|
|
row := q.db.QueryRowContext(ctx, updateProgress,
|
|
|
|
arg.UserID,
|
|
|
|
arg.DocumentID,
|
|
|
|
arg.DeviceID,
|
|
|
|
arg.Percentage,
|
|
|
|
arg.Progress,
|
|
|
|
)
|
|
|
|
var i DocumentProgress
|
|
|
|
err := row.Scan(
|
|
|
|
&i.UserID,
|
|
|
|
&i.DocumentID,
|
|
|
|
&i.DeviceID,
|
|
|
|
&i.Percentage,
|
|
|
|
&i.Progress,
|
|
|
|
&i.CreatedAt,
|
|
|
|
)
|
|
|
|
return i, err
|
|
|
|
}
|
|
|
|
|
2024-02-02 01:05:35 +00:00
|
|
|
const updateSettings = `-- name: UpdateSettings :one
|
|
|
|
INSERT INTO settings (name, value)
|
|
|
|
VALUES (?, ?)
|
|
|
|
ON CONFLICT DO UPDATE
|
|
|
|
SET
|
|
|
|
name = COALESCE(excluded.name, name),
|
|
|
|
value = COALESCE(excluded.value, value)
|
|
|
|
RETURNING id, name, value, created_at
|
|
|
|
`
|
|
|
|
|
|
|
|
type UpdateSettingsParams struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Value string `json:"value"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) UpdateSettings(ctx context.Context, arg UpdateSettingsParams) (Setting, error) {
|
|
|
|
row := q.db.QueryRowContext(ctx, updateSettings, arg.Name, arg.Value)
|
|
|
|
var i Setting
|
|
|
|
err := row.Scan(
|
|
|
|
&i.ID,
|
|
|
|
&i.Name,
|
|
|
|
&i.Value,
|
|
|
|
&i.CreatedAt,
|
|
|
|
)
|
|
|
|
return i, err
|
|
|
|
}
|
|
|
|
|
2023-09-27 22:58:47 +00:00
|
|
|
const updateUser = `-- name: UpdateUser :one
|
|
|
|
UPDATE users
|
|
|
|
SET
|
|
|
|
pass = COALESCE(?1, pass),
|
2024-01-28 02:02:08 +00:00
|
|
|
auth_hash = COALESCE(?2, auth_hash),
|
|
|
|
time_offset = COALESCE(?3, time_offset)
|
|
|
|
WHERE id = ?4
|
|
|
|
RETURNING id, pass, auth_hash, admin, time_offset, created_at
|
2023-09-27 22:58:47 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
type UpdateUserParams struct {
|
|
|
|
Password *string `json:"-"`
|
2024-01-29 03:17:58 +00:00
|
|
|
AuthHash *string `json:"auth_hash"`
|
2023-09-27 22:58:47 +00:00
|
|
|
TimeOffset *string `json:"time_offset"`
|
|
|
|
UserID string `json:"user_id"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) UpdateUser(ctx context.Context, arg UpdateUserParams) (User, error) {
|
2024-01-28 02:02:08 +00:00
|
|
|
row := q.db.QueryRowContext(ctx, updateUser,
|
|
|
|
arg.Password,
|
|
|
|
arg.AuthHash,
|
|
|
|
arg.TimeOffset,
|
|
|
|
arg.UserID,
|
|
|
|
)
|
2023-09-27 22:58:47 +00:00
|
|
|
var i User
|
|
|
|
err := row.Scan(
|
|
|
|
&i.ID,
|
|
|
|
&i.Pass,
|
2024-01-28 02:02:08 +00:00
|
|
|
&i.AuthHash,
|
2023-09-27 22:58:47 +00:00
|
|
|
&i.Admin,
|
|
|
|
&i.TimeOffset,
|
|
|
|
&i.CreatedAt,
|
|
|
|
)
|
|
|
|
return i, err
|
|
|
|
}
|
|
|
|
|
2023-09-18 23:57:18 +00:00
|
|
|
const upsertDevice = `-- name: UpsertDevice :one
|
2023-10-03 20:47:38 +00:00
|
|
|
INSERT INTO devices (id, user_id, last_synced, device_name)
|
|
|
|
VALUES (?, ?, ?, ?)
|
2023-09-18 23:57:18 +00:00
|
|
|
ON CONFLICT DO UPDATE
|
|
|
|
SET
|
2023-10-03 20:47:38 +00:00
|
|
|
device_name = COALESCE(excluded.device_name, device_name),
|
|
|
|
last_synced = COALESCE(excluded.last_synced, last_synced)
|
|
|
|
RETURNING id, user_id, device_name, last_synced, created_at, sync
|
2023-09-18 23:57:18 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
type UpsertDeviceParams struct {
|
2023-10-06 01:04:57 +00:00
|
|
|
ID string `json:"id"`
|
|
|
|
UserID string `json:"user_id"`
|
|
|
|
LastSynced string `json:"last_synced"`
|
|
|
|
DeviceName string `json:"device_name"`
|
2023-09-18 23:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) UpsertDevice(ctx context.Context, arg UpsertDeviceParams) (Device, error) {
|
2023-10-03 20:47:38 +00:00
|
|
|
row := q.db.QueryRowContext(ctx, upsertDevice,
|
|
|
|
arg.ID,
|
|
|
|
arg.UserID,
|
|
|
|
arg.LastSynced,
|
|
|
|
arg.DeviceName,
|
|
|
|
)
|
2023-09-18 23:57:18 +00:00
|
|
|
var i Device
|
|
|
|
err := row.Scan(
|
|
|
|
&i.ID,
|
|
|
|
&i.UserID,
|
|
|
|
&i.DeviceName,
|
2023-10-03 20:47:38 +00:00
|
|
|
&i.LastSynced,
|
2023-09-18 23:57:18 +00:00
|
|
|
&i.CreatedAt,
|
|
|
|
&i.Sync,
|
|
|
|
)
|
|
|
|
return i, err
|
|
|
|
}
|
|
|
|
|
|
|
|
const upsertDocument = `-- name: UpsertDocument :one
|
|
|
|
INSERT INTO documents (
|
|
|
|
id,
|
|
|
|
md5,
|
|
|
|
filepath,
|
2023-09-23 18:14:57 +00:00
|
|
|
coverfile,
|
2023-09-18 23:57:18 +00:00
|
|
|
title,
|
|
|
|
author,
|
|
|
|
series,
|
|
|
|
series_index,
|
|
|
|
lang,
|
|
|
|
description,
|
2023-10-01 23:17:22 +00:00
|
|
|
words,
|
2023-09-23 18:14:57 +00:00
|
|
|
olid,
|
|
|
|
gbid,
|
|
|
|
isbn10,
|
|
|
|
isbn13
|
2023-09-18 23:57:18 +00:00
|
|
|
)
|
2023-10-01 23:17:22 +00:00
|
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
2023-09-18 23:57:18 +00:00
|
|
|
ON CONFLICT DO UPDATE
|
|
|
|
SET
|
|
|
|
md5 = COALESCE(excluded.md5, md5),
|
|
|
|
filepath = COALESCE(excluded.filepath, filepath),
|
2023-09-23 18:14:57 +00:00
|
|
|
coverfile = COALESCE(excluded.coverfile, coverfile),
|
2023-09-18 23:57:18 +00:00
|
|
|
title = COALESCE(excluded.title, title),
|
|
|
|
author = COALESCE(excluded.author, author),
|
|
|
|
series = COALESCE(excluded.series, series),
|
|
|
|
series_index = COALESCE(excluded.series_index, series_index),
|
|
|
|
lang = COALESCE(excluded.lang, lang),
|
|
|
|
description = COALESCE(excluded.description, description),
|
2023-10-01 23:17:22 +00:00
|
|
|
words = COALESCE(excluded.words, words),
|
2023-09-23 18:14:57 +00:00
|
|
|
olid = COALESCE(excluded.olid, olid),
|
|
|
|
gbid = COALESCE(excluded.gbid, gbid),
|
|
|
|
isbn10 = COALESCE(excluded.isbn10, isbn10),
|
|
|
|
isbn13 = COALESCE(excluded.isbn13, isbn13)
|
2023-10-01 23:17:22 +00:00
|
|
|
RETURNING id, md5, filepath, coverfile, title, author, series, series_index, lang, description, words, gbid, olid, isbn10, isbn13, synced, deleted, updated_at, created_at
|
2023-09-18 23:57:18 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
type UpsertDocumentParams struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
Md5 *string `json:"md5"`
|
|
|
|
Filepath *string `json:"filepath"`
|
2023-09-23 18:14:57 +00:00
|
|
|
Coverfile *string `json:"coverfile"`
|
2023-09-18 23:57:18 +00:00
|
|
|
Title *string `json:"title"`
|
|
|
|
Author *string `json:"author"`
|
|
|
|
Series *string `json:"series"`
|
|
|
|
SeriesIndex *int64 `json:"series_index"`
|
|
|
|
Lang *string `json:"lang"`
|
|
|
|
Description *string `json:"description"`
|
2023-10-01 23:17:22 +00:00
|
|
|
Words *int64 `json:"words"`
|
2023-09-18 23:57:18 +00:00
|
|
|
Olid *string `json:"-"`
|
2023-09-23 18:14:57 +00:00
|
|
|
Gbid *string `json:"gbid"`
|
|
|
|
Isbn10 *string `json:"isbn10"`
|
|
|
|
Isbn13 *string `json:"isbn13"`
|
2023-09-18 23:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (q *Queries) UpsertDocument(ctx context.Context, arg UpsertDocumentParams) (Document, error) {
|
|
|
|
row := q.db.QueryRowContext(ctx, upsertDocument,
|
|
|
|
arg.ID,
|
|
|
|
arg.Md5,
|
|
|
|
arg.Filepath,
|
2023-09-23 18:14:57 +00:00
|
|
|
arg.Coverfile,
|
2023-09-18 23:57:18 +00:00
|
|
|
arg.Title,
|
|
|
|
arg.Author,
|
|
|
|
arg.Series,
|
|
|
|
arg.SeriesIndex,
|
|
|
|
arg.Lang,
|
|
|
|
arg.Description,
|
2023-10-01 23:17:22 +00:00
|
|
|
arg.Words,
|
2023-09-18 23:57:18 +00:00
|
|
|
arg.Olid,
|
2023-09-23 18:14:57 +00:00
|
|
|
arg.Gbid,
|
|
|
|
arg.Isbn10,
|
|
|
|
arg.Isbn13,
|
2023-09-18 23:57:18 +00:00
|
|
|
)
|
|
|
|
var i Document
|
|
|
|
err := row.Scan(
|
|
|
|
&i.ID,
|
|
|
|
&i.Md5,
|
|
|
|
&i.Filepath,
|
2023-09-23 18:14:57 +00:00
|
|
|
&i.Coverfile,
|
2023-09-18 23:57:18 +00:00
|
|
|
&i.Title,
|
|
|
|
&i.Author,
|
|
|
|
&i.Series,
|
|
|
|
&i.SeriesIndex,
|
|
|
|
&i.Lang,
|
|
|
|
&i.Description,
|
2023-10-01 23:17:22 +00:00
|
|
|
&i.Words,
|
2023-09-23 18:14:57 +00:00
|
|
|
&i.Gbid,
|
2023-09-18 23:57:18 +00:00
|
|
|
&i.Olid,
|
2023-09-23 18:14:57 +00:00
|
|
|
&i.Isbn10,
|
|
|
|
&i.Isbn13,
|
2023-09-18 23:57:18 +00:00
|
|
|
&i.Synced,
|
|
|
|
&i.Deleted,
|
|
|
|
&i.UpdatedAt,
|
|
|
|
&i.CreatedAt,
|
|
|
|
)
|
|
|
|
return i, err
|
|
|
|
}
|