-
Notifications
You must be signed in to change notification settings - Fork 19
/
sections.go
134 lines (122 loc) · 3.63 KB
/
sections.go
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package gcsql
import (
"context"
"database/sql"
"errors"
)
var (
// AllSections provides a quick and simple way to access a list of all non-hidden sections without
// having to do any SQL queries. It and AllBoards are updated by ResetBoardSectionArrays
AllSections []Section
)
// GetAllSections gets a list of all existing sections, optionally omitting hidden ones
func GetAllSections(onlyNonHidden bool) ([]Section, error) {
query := `SELECT
id, name, abbreviation, position, hidden
FROM DBPREFIXsections`
if onlyNonHidden {
query += " WHERE hidden = FALSE "
}
query += " ORDER BY position ASC, name ASC"
rows, cancel, err := QueryTimeoutSQL(nil, query)
if err != nil {
return nil, err
}
defer func() {
cancel()
rows.Close()
}()
var sections []Section
for rows.Next() {
var section Section
err = rows.Scan(§ion.ID, §ion.Name, §ion.Abbreviation, §ion.Position, §ion.Hidden)
if err != nil {
return nil, err
}
sections = append(sections, section)
}
return sections, rows.Close()
}
// getOrCreateDefaultSectionID creates the default section if no sections have been created yet,
// returns default section ID if it exists
func getOrCreateDefaultSectionID() (sectionID int, err error) {
const query = `SELECT id FROM DBPREFIXsections WHERE name = 'Main'`
var id int
err = QueryRowTimeoutSQL(nil, query, nil, []any{&id})
if errors.Is(err, sql.ErrNoRows) {
var section *Section
if section, err = NewSection("Main", "main", false, -1); err != nil {
return 0, err
}
return section.ID, err
}
if err != nil {
return 0, err //other error
}
return id, nil
}
func GetSectionFromID(id int) (*Section, error) {
const query = `SELECT id, name, abbreviation, position, hidden FROM DBPREFIXsections WHERE id = ?`
var section Section
err := QueryRowTimeoutSQL(nil, query, []any{id}, []any{
§ion.ID, §ion.Name, §ion.Abbreviation, §ion.Position, §ion.Hidden,
})
if err != nil {
return nil, err
}
return §ion, err
}
func DeleteSection(id int) error {
const query = `DELETE FROM DBPREFIXsections WHERE id = ?`
_, err := ExecSQL(query, id)
if err != nil {
return err
}
return ResetBoardSectionArrays()
}
// NewSection creates a new board section in the database and returns a *Section struct pointer.
// If position < 0, it will use the ID
func NewSection(name string, abbreviation string, hidden bool, position int) (*Section, error) {
const sqlINSERT = `INSERT INTO DBPREFIXsections (name, abbreviation, hidden, position) VALUES (?,?,?,?)`
const sqlPosition = `SELECT COALESCE(MAX(position) + 1, 1) FROM DBPREFIXsections`
tx, err := BeginTx()
if err != nil {
return nil, err
}
ctx, cancel := context.WithTimeout(context.Background(), gcdb.defaultTimeout)
defer func() {
cancel()
tx.Rollback()
}()
if position < 0 {
// position not specified
err = QueryRowContextSQL(ctx, tx, sqlPosition, nil, []any{&position})
if errors.Is(err, sql.ErrNoRows) {
position = 1
} else if err != nil {
return nil, err
}
}
if _, err = ExecContextSQL(ctx, tx, sqlINSERT, name, abbreviation, hidden, position); err != nil {
return nil, err
}
id, err := getLatestID("DBPREFIXsections", tx)
if err != nil {
return nil, err
}
if err = tx.Commit(); err != nil {
return nil, err
}
return &Section{
ID: id,
Name: name,
Abbreviation: abbreviation,
Position: position,
Hidden: hidden,
}, nil
}
func (s *Section) UpdateValues() error {
const query = `UPDATE DBPREFIXsections set name = ?, abbreviation = ?, position = ?, hidden = ? WHERE id = ?`
_, err := ExecTimeoutSQL(nil, query, s.Name, s.Abbreviation, s.Position, s.Hidden, s.ID)
return err
}