-
Notifications
You must be signed in to change notification settings - Fork 19
/
sql.go
executable file
·206 lines (184 loc) · 6.98 KB
/
sql.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
package main
import (
"database/sql"
"io/ioutil"
"os"
"strings"
"time"
_ "github.com/go-sql-driver/mysql"
// _ "github.com/lib/pq"
)
const (
nilTimestamp = "0000-00-00 00:00:00"
mysqlDatetimeFormat = "2006-01-02 15:04:05"
)
var (
db *sql.DB
)
func connectToSQLServer() {
var err error
var sqlVersion string
var newInstall bool
db, err = sql.Open("mysql", config.DBusername+":"+config.DBpassword+"@"+config.DBhost+"/"+config.DBname+"?parseTime=true&collation=utf8mb4_unicode_ci")
if err != nil {
handleError(0, "Failed to connect to the database: %s\n", customError(err))
os.Exit(2)
}
// check if initialsetupdb.sql still exists
if _, err = os.Stat("initialsetupdb.sql"); err != nil {
handleError(0, "Initial setup file (initialsetupdb.sql) missing. Please reinstall gochan")
os.Exit(2)
}
// read the initial setup sql file into a string
initialSQLBytes, err := ioutil.ReadFile("initialsetupdb.sql")
if err != nil {
handleError(0, "failed: %s\n", customError(err))
os.Exit(2)
}
initialSQLStr := string(initialSQLBytes)
initialSQLStr = strings.NewReplacer("DBNAME", config.DBname, "DBPREFIX", config.DBprefix).Replace(initialSQLStr)
initialSQLArr := strings.Split(initialSQLStr, ";")
for _, statement := range initialSQLArr {
if statement != "" && statement != "\n" && statement != "\r\n" && strings.Index(statement, "--") != 0 {
if _, err := db.Exec(statement); err != nil {
handleError(0, "failed: %s\n", customError(err))
os.Exit(2)
}
}
}
sqlVersion = ""
err = queryRowSQL("SELECT `value` FROM `"+config.DBprefix+"info` WHERE `name` = 'version'",
[]interface{}{}, []interface{}{&version})
if err == sql.ErrNoRows {
newInstall = true
} else if err != nil {
handleError(0, "failed: %s\n", customError(err))
os.Exit(2)
}
if newInstall {
printf(0, "\nThis looks like a new install or one that needs updating, setting up the database...")
if _, err = db.Exec("INSERT INTO `" + config.DBname + "`.`" + config.DBprefix + "staff` " +
"(`username`, `password_checksum`, `salt`, `rank`) " +
"VALUES ('admin', '" + bcryptSum("password") + "', 'abc', 3)",
); err != nil {
handleError(0, "failed with error: %s\n", customError(err))
os.Exit(2)
}
}
if sqlVersion != version {
_, err = execSQL("INSERT INTO `"+config.DBprefix+"info` (`name`,`value`) VALUES('version',?)", version)
if err != nil && !strings.Contains(err.Error(), "Duplicate entry") {
handleError(0, "failed with error: %s\n", customError(err))
os.Exit(2)
}
}
checkDeprecatedSchema()
}
/*
* Automatically escapes the given values and caches the statement
* Example:
* var intVal int
* var stringVal string
* result, err := execSQL("INSERT INTO `tablename` (`intval`,`stringval`) VALUES(?,?)", intVal, stringVal)
*/
func execSQL(query string, values ...interface{}) (sql.Result, error) {
stmt, err := db.Prepare(query)
defer closeStatement(stmt)
if err != nil {
return nil, err
}
return stmt.Exec(values...)
}
/*
* Gets a row from the db with the values in values[] and fills the respective pointers in out[]
* Automatically escapes the given values and caches the query
* Example:
* id := 32
* var intVal int
* var stringVal string
* err := queryRowSQL("SELECT `intval`,`stringval` FROM `table` WHERE `id` = ?",
* []interface{}{&id},
* []interface{}{&intVal, &stringVal}
* )
*/
func queryRowSQL(query string, values []interface{}, out []interface{}) error {
stmt, err := db.Prepare(query)
defer closeStatement(stmt)
if err != nil {
return err
}
return stmt.QueryRow(values...).Scan(out...)
}
/*
* Gets all rows from the db with the values in values[] and fills the respective pointers in out[]
* Automatically escapes the given values and caches the query
* Example:
* rows, err := querySQL("SELECT * FROM `table`")
* if err == nil {
* for rows.Next() {
* var intVal int
* var stringVal string
* rows.Scan(&intVal, &stringVal)
* // do something with intVal and stringVal
* }
* }
*/
func querySQL(query string, a ...interface{}) (*sql.Rows, error) {
stmt, err := db.Prepare(query)
defer closeStatement(stmt)
if err != nil {
return nil, err
}
return stmt.Query(a...)
}
func getSQLDateTime() string {
return time.Now().Format(mysqlDatetimeFormat)
}
func getSpecificSQLDateTime(t time.Time) string {
return t.Format(mysqlDatetimeFormat)
}
// checkDeprecatedSchema checks the tables for outdated columns and column values
// and causes gochan to quit with an error message specific to the needed change
func checkDeprecatedSchema() {
var hasColumn int
var err error
execSQL("ALTER TABLE `"+config.DBprefix+"banlist` CHANGE COLUMN `id` `id` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT", nil)
execSQL("ALTER TABLE `"+config.DBprefix+"banlist` CHANGE COLUMN `expires` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP", nil)
if err = queryRowSQL(
"SELECT COUNT(*) FROM information_schema.COlUMNS WHERE `TABLE_SCHEMA` = '"+config.DBname+"' AND TABLE_NAME = '"+config.DBprefix+"banlist' AND COLUMN_NAME = 'appeal_message'",
[]interface{}{}, []interface{}{&hasColumn},
); err != nil {
println(0, "error checking for deprecated column: "+err.Error())
os.Exit(2)
return
}
if hasColumn > 0 {
// Running them one at a time, in case we get errors from individual queries
execSQL("ALTER TABLE `"+config.DBprefix+"banlist` CHANGE COLUMN `banned_by` `staff` VARCHAR(50) NOT NULL", nil)
execSQL("ALTER TABLE `"+config.DBprefix+"banlist` ADD COLUMN `type` TINYINT UNSIGNED NOT NULL DEFAULT '3'", nil)
execSQL("ALTER TABLE `"+config.DBprefix+"banlist` ADD COLUMN `name_is_regex` TINYINT(1) DEFAULT '0'", nil)
execSQL("ALTER TABLE `"+config.DBprefix+"banlist` ADD COLUMN `filename` VARCHAR(255) NOT NULL DEFAULT ''", nil)
execSQL("ALTER TABLE `"+config.DBprefix+"banlist` ADD COLUMN `file_checksum` VARCHAR(255) NOT NULL DEFAULT ''", nil)
execSQL("ALTER TABLE `"+config.DBprefix+"banlist` ADD COLUMN `permaban` TINYINT(1) DEFAULT '0'", nil)
execSQL("ALTER TABLE `"+config.DBprefix+"banlist` ADD COLUMN `can_appeal` TINYINT(1) DEFAULT '1'", nil)
execSQL("ALTER TABLE `"+config.DBprefix+"banlist` DROP COLUMN `message`", nil)
execSQL("ALTER TABLE `"+config.DBprefix+"boards` CHANGE COLUMN `boards` VARCHAR(255) NOT NULL DEFAULT ''", nil)
println(0, "The column `appeal_message` in table "+config.DBprefix+"banlist is deprecated. A new table , `"+config.DBprefix+"appeals` has been created for it, and the banlist table will be modified accordingly.")
println(0, "Just to be safe, you may want to check both tables to make sure everything is good.")
rows, err := querySQL("SELECT `id`,`appeal_message` FROM `" + config.DBprefix + "banlist`")
if err != nil {
println(0, "Error updating banlist schema: "+err.Error())
os.Exit(2)
return
}
for rows.Next() {
var id int
var appealMessage string
rows.Scan(&id, &appealMessage)
if appealMessage != "" {
execSQL("INSERT INTO `"+config.DBprefix+"appeals` (`ban`,`message`) VALUES(?,?)", &id, &appealMessage)
}
execSQL("ALTER TABLE `" + config.DBprefix + "banlist` DROP COLUMN `appeal_message`")
}
}
}