summaryrefslogtreecommitdiff
path: root/database/minecraft.go
blob: d12b4b821179e9aac9007e2846eb4330f142a43e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package database

import (
	"database/sql"
	"errors"
	"time"

	_ "github.com/mattn/go-sqlite3"
)

func SaveMinecraftStatus(db *sql.DB, status string) error {
	_, err := db.Exec(`INSERT INTO minecraft_status (id, status, updated_at) VALUES (1, ?, ?)
		ON CONFLICT(id) DO UPDATE SET status = excluded.status, updated_at = excluded.updated_at`,
		status, time.Now())
	return err
}

// GetMinecraftStatus returns the cached status JSON and when it was written. An
// empty string (with no error) means nothing has been cached yet.
func GetMinecraftStatus(db *sql.DB) (string, time.Time, error) {
	var status string
	var updatedAt time.Time
	err := db.QueryRow("SELECT status, updated_at FROM minecraft_status WHERE id = 1").Scan(&status, &updatedAt)
	if errors.Is(err, sql.ErrNoRows) {
		return "", time.Time{}, nil
	}
	return status, updatedAt, err
}