~evanj/cms

ref: 9d2dfb0b32eb388682adedc84de30562f01806be cms/internal/s/db/action.go -rw-r--r-- 919 bytes
9d2dfb0bEvan J Feat(db): Mostly complete removing potentially deadlocking DB code. Only 8 months ago
                                                                                
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package db

import (
	"database/sql"
	"time"

	"git.sr.ht/~evanj/cms/internal/m/org"
)

func (db *DB) ActionNew(o org.Org, at time.Time) error {
	_, err := db.Exec("INSERT INTO cms_action (ORG_ID, AT) VALUES (?, ?)", o.ID(), at.Format("2006-01-02 03:04:05"))
	return err
}

func (db *DB) ActionGetCount(o org.Org, from, to time.Time) (int, error) {
	t, err := db.Begin()
	if err != nil {
		return 0, err
	}
	defer t.Rollback()

	i, err := db.actionGetCount(t, o, from, to)
	if err != nil {
		return 0, err
	}

	return i, t.Commit()
}

func (db *DB) actionGetCount(t *sql.Tx, o org.Org, from, to time.Time) (int, error) {
	var (
		count int
		q     = "SELECT COUNT(*) FROM cms_action WHERE cms_action.ORG_ID=? AND AT>? AND AT<?"
	)

	a := from.Format("2006-01-02 03:04:05")
	b := to.Format("2006-01-02 03:04:05")
	if err := t.QueryRow(q, o.ID(), a, b).Scan(&count); err != nil {
		return 0, err
	}

	return count, nil
}