-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdbUtils.js
More file actions
78 lines (65 loc) · 2.16 KB
/
dbUtils.js
File metadata and controls
78 lines (65 loc) · 2.16 KB
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
const fs = require("fs");
const pathUtils = require("path");
const mysql = require("mysql");
class DbUtils {
initialize() {
this.databaseConfig = JSON.parse(fs.readFileSync(
pathUtils.join(ostracodMultiplayer.configDirectory, "databaseConfig.json"),
"utf8",
));
this.databaseLock = false;
this.connection = null;
}
convertSqlErrorToText(error) {
return error.code + ": " + error.sqlMessage;
}
startTransaction(done) {
if (this.databaseLock) {
setTimeout(() => {
dbUtils.startTransaction(done);
}, 2);
} else {
this.databaseLock = true;
this.connection = mysql.createConnection({
host: this.databaseConfig.host,
user: this.databaseConfig.username,
password: this.databaseConfig.password,
database: this.databaseConfig.databaseName,
});
this.connection.connect((error) => {
if (error) {
console.log(dbUtils.convertSqlErrorToText(error));
return;
}
done();
});
}
}
finishTransaction() {
this.connection.destroy();
this.databaseLock = false;
}
performTransaction(operation, done) {
return niceUtils.performAsyncOperation((callback) => {
dbUtils.startTransaction(() => {
niceUtils.performAsyncOperation(operation, 0, () => {
dbUtils.finishTransaction();
callback();
});
});
}, 0, done);
}
performQuery(query, parameterList, done) {
if (!this.databaseLock) {
console.log("Missing lock!");
return;
}
return niceUtils.performAsyncOperation((callback) => {
this.connection.query(query, parameterList, callback);
}, 3, done);
}
}
const dbUtils = new DbUtils();
module.exports = { dbUtils };
const { niceUtils } = require("./niceUtils");
const { ostracodMultiplayer } = require("./ostracodMultiplayer");