-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmodel.go
60 lines (53 loc) · 1.75 KB
/
model.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
// apcore is a server framework for implementing an ActivityPub application.
// Copyright (C) 2020 Cory Slep
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package models
import (
"database/sql"
)
// Model handles managing a single database type.
type Model interface {
Prepare(*sql.DB, SqlDialect) error
CreateTable(*sql.Tx, SqlDialect) error
Close()
}
// stmtPair make a pair of **sql.Stmt and its associated SQL string.
//
// The goal is to populate *stmt based on the associated sqlStr.
type stmtPair struct {
stmt **sql.Stmt
sqlStr string
}
// prepareStmtPair is a mapper that populates the stmtPair.stmt.
func prepareStmtPair(db *sql.DB, s stmtPair) (err error) {
*s.stmt, err = db.Prepare(s.sqlStr)
return err
}
// stmtPairs are a list of stmtPair.
type stmtPairs []stmtPair
// prepareStmtPairs turns stmtPairs into a single error, with a side effect of
// populating all stmt.
func prepareStmtPairs(db *sql.DB, s stmtPairs) (err error) {
doIfNoErr := func(p stmtPair, fn func(*sql.DB, stmtPair) error) error {
if err == nil {
return fn(db, p)
}
return err
}
for _, p := range s {
err = doIfNoErr(p, prepareStmtPair)
}
return
}