forked from documize/community
/
init.go
229 lines (181 loc) · 5.67 KB
/
init.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
//
// This software (Documize Community Edition) is licensed under
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
//
// You can operate outside the AGPL restrictions by purchasing
// Documize Enterprise Edition and obtaining a commercial license
// by contacting <sales@documize.com>.
//
// https://documize.com
package request
import (
"fmt"
"os"
"strings"
"time"
"github.com/jmoiron/sqlx"
"github.com/documize/community/core/database"
"github.com/documize/community/core/environment"
"github.com/documize/community/core/log"
"github.com/documize/community/core/utility"
"github.com/documize/community/core/web"
)
var connectionString string
// Db is the central connection to the database, used by all database requests.
var Db *sqlx.DB
var searches *SearchManager
type databaseRequest struct {
Transaction *sqlx.Tx
OrgID string
}
func (dr *databaseRequest) MakeTx() (err error) {
if dr.Transaction != nil {
return nil
}
dr.Transaction, err = Db.Beginx()
return err
}
func init() {
var err error
environment.GetString(&connectionString, "db", true,
`'username:password@protocol(hostname:port)/databasename" for example "fred:bloggs@tcp(localhost:3306)/documize"`,
func(*string, string) bool {
Db, err = sqlx.Open("mysql", stdConn(connectionString))
if err != nil {
log.Error("Unable to setup database", err)
}
Db.SetMaxIdleConns(30)
Db.SetMaxOpenConns(100)
Db.SetConnMaxLifetime(time.Second * 14400)
err = Db.Ping()
if err != nil {
log.Error("Unable to connect to database, connection string should be of the form: '"+
"username:password@tcp(host:3306)/database'", err)
os.Exit(2)
}
// go into setup mode if required
if web.SiteMode != web.SiteModeOffline {
if database.Check(Db, connectionString) {
if err := database.Migrate(true /* the config table exists */); err != nil {
log.Error("Unable to run database migration: ", err)
os.Exit(2)
}
} else {
log.Info("database.Check(Db) !OK, going into setup mode")
}
}
return false // value not changed
})
}
var stdParams = map[string]string{
"charset": "utf8",
"parseTime": "True",
}
func stdConn(cs string) string {
queryBits := strings.Split(cs, "?")
ret := queryBits[0] + "?"
retFirst := true
if len(queryBits) == 2 {
paramBits := strings.Split(queryBits[1], "&")
for _, pb := range paramBits {
found := false
if assignBits := strings.Split(pb, "="); len(assignBits) == 2 {
_, found = stdParams[strings.TrimSpace(assignBits[0])]
}
if !found { // if we can't work out what it is, put it through
if retFirst {
retFirst = false
} else {
ret += "&"
}
ret += pb
}
}
}
for k, v := range stdParams {
if retFirst {
retFirst = false
} else {
ret += "&"
}
ret += k + "=" + v
}
return ret
}
type baseManager struct {
}
func (m *baseManager) Delete(tx *sqlx.Tx, table string, id string) (rows int64, err error) {
err = nil
stmt, err := tx.Preparex("DELETE FROM " + table + " WHERE refid=?")
if err != nil {
log.Error(fmt.Sprintf("Unable to prepare delete of row in table %s", table), err)
return
}
defer utility.Close(stmt)
result, err := stmt.Exec(id)
if err != nil {
log.Error(fmt.Sprintf("Unable to delete row in table %s", table), err)
return
}
rows, err = result.RowsAffected()
return
}
func (m *baseManager) DeleteConstrained(tx *sqlx.Tx, table string, orgID, id string) (rows int64, err error) {
stmt, err := tx.Preparex("DELETE FROM " + table + " WHERE orgid=? AND refid=?")
if err != nil {
log.Error(fmt.Sprintf("Unable to prepare constrained delete of row in table %s", table), err)
return
}
defer utility.Close(stmt)
result, err := stmt.Exec(orgID, id)
if err != nil {
log.Error(fmt.Sprintf("Unable to delete row in table %s", table), err)
return
}
rows, err = result.RowsAffected()
return
}
func (m *baseManager) DeleteConstrainedWithID(tx *sqlx.Tx, table string, orgID, id string) (rows int64, err error) {
stmt, err := tx.Preparex("DELETE FROM " + table + " WHERE orgid=? AND id=?")
if err != nil {
log.Error(fmt.Sprintf("Unable to prepare ConstrainedWithID delete of row in table %s", table), err)
return
}
defer utility.Close(stmt)
result, err := stmt.Exec(orgID, id)
if err != nil {
log.Error(fmt.Sprintf("Unable to delete row in table %s", table), err)
return
}
rows, err = result.RowsAffected()
return
}
func (m *baseManager) DeleteWhere(tx *sqlx.Tx, statement string) (rows int64, err error) {
result, err := tx.Exec(statement)
if err != nil {
log.Error(fmt.Sprintf("Unable to delete rows: %s", statement), err)
return
}
rows, err = result.RowsAffected()
return
}
// Audit inserts a record into the audit table.
func (m *baseManager) Audit(c Context, action, document, page string) {
_, err := Db.Exec("INSERT INTO audit (orgid, userid, documentid, pageid, action, created) VALUES (?, ?, ?, ?, ?, ?)", c.OrgID, c.UserID, document, page, action, time.Now().UTC())
if err != nil {
log.Error(fmt.Sprintf("Unable record audit for action %s, user %s, customer %s", action, c.UserID, c.OrgID), err)
}
}
// SQLPrepareError returns a string detailing the location of the error.
func (m *baseManager) SQLPrepareError(method string, id string) string {
return fmt.Sprintf("Unable to prepare SQL for %s, ID %s", method, id)
}
// SQLSelectError returns a string detailing the location of the error.
func (m *baseManager) SQLSelectError(method string, id string) string {
return fmt.Sprintf("Unable to execute SQL for %s, ID %s", method, id)
}
const (
// AuditGetDocument means someone viewed a document
AuditGetDocument string = "get-document"
)