forked from oniony/TMSU
-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.go
157 lines (120 loc) · 3.08 KB
/
database.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Copyright 2011-2018 Paul Ruane.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU 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 General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package database
import (
"database/sql"
"errors"
_ "github.com/mattn/go-sqlite3" // initialised Sqlite3
"github.com/oniony/TMSU/common/log"
"os"
)
type Database struct {
db *sql.DB
}
func CreateAt(path string) error {
log.Infof(2, "creating database at '%v'.", path)
db, err := sql.Open("sqlite3", path)
if err != nil {
return DatabaseAccessError{path, err}
}
defer db.Close()
tx, err := db.Begin()
if err != nil {
return DatabaseTransactionError{path, err}
}
if err := upgrade(tx); err != nil {
return err
}
if err := tx.Commit(); err != nil {
return DatabaseTransactionError{path, err}
}
return nil
}
func OpenAt(path string) (*Database, error) {
log.Infof(2, "opening database at '%v'.", path)
_, err := os.Stat(path)
if err != nil {
switch {
case os.IsNotExist(err):
return nil, DatabaseNotFoundError{path}
default:
return nil, DatabaseAccessError{path, err}
}
}
db, err := sql.Open("sqlite3", path)
if err != nil {
return nil, DatabaseAccessError{path, err}
}
tx, err := db.Begin()
if err != nil {
return nil, DatabaseTransactionError{path, err}
}
if err := upgrade(tx); err != nil {
return nil, err
}
if err := tx.Commit(); err != nil {
return nil, DatabaseTransactionError{path, err}
}
return &Database{db}, nil
}
func (database *Database) Close() error {
return database.db.Close()
}
func (database *Database) Begin() (*Tx, error) {
tx, err := database.db.Begin()
if err != nil {
return nil, err
}
return &Tx{tx}, nil
}
type Tx struct {
tx *sql.Tx
}
func (tx *Tx) Exec(query string, args ...interface{}) (sql.Result, error) {
log.Info(3, query)
log.Infof(3, "params: %v", args)
return tx.tx.Exec(query, args...)
}
func (tx *Tx) Query(query string, args ...interface{}) (*sql.Rows, error) {
log.Info(3, query)
log.Infof(3, "params: %v", args)
return tx.tx.Query(query, args...)
}
func (tx *Tx) Commit() error {
log.Info(2, "committing transaction")
return tx.tx.Commit()
}
func (tx *Tx) Rollback() error {
log.Info(2, "rolling back transaction")
return tx.tx.Rollback()
}
// unexported
func readCount(rows *sql.Rows) (uint, error) {
if !rows.Next() {
return 0, errors.New("could not get count")
}
if rows.Err() != nil {
return 0, rows.Err()
}
var count uint
err := rows.Scan(&count)
if err != nil {
return 0, err
}
return count, nil
}
func collationFor(ignoreCase bool) string {
if ignoreCase {
return " COLLATE NOCASE"
}
return ""
}