~evanj/cms

ref: afc228cb59683180b3b2957aceb6bf1aa0bf0d0e cms/internal/s/db/action.go -rw-r--r-- 1.1 KiB
afc228cbEvan J Feat(db): Tweaking db.SetMaxOpenConns, db.SetMaxIdleConns, and 1 year, 14 days 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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 {
	t, err := db.Begin()
	if err != nil {
		return err
	}
	defer t.Rollback()

	if err := db.actionNew(t, o, at); err != nil {
		return err
	}

	return t.Commit()
}

func (db *DB) actionNew(t *sql.Tx, o org.Org, at time.Time) error {
	_, err := t.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
}