-
Notifications
You must be signed in to change notification settings - Fork 17
/
MySQLPost200.java
153 lines (119 loc) · 5.7 KB
/
MySQLPost200.java
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
/**
* This file is part of LWC (https://github.com/Hidendra/LWC)
*
* 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 com.griefcraft.migration;
import com.griefcraft.lwc.LWC;
import com.griefcraft.model.AccessRight;
import com.griefcraft.model.Protection;
import com.griefcraft.sql.Database.Type;
import com.griefcraft.sql.PhysDB;
import java.io.File;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Logger;
// Sort of just a convenience class, so as to not make the LWC class more cluttered than it is right now
public class MySQLPost200 implements MigrationUtility {
private static Logger logger = Logger.getLogger("Patcher");
/**
* Check for required SQLite->MySQL conversion
*/
public void run() {
LWC lwc = LWC.getInstance();
PhysDB physicalDatabase = lwc.getPhysicalDatabase();
// this patcher only does something exciting if you have mysql enabled
// :-)
if (physicalDatabase.getType() != Type.MySQL) {
return;
}
// this patcher only does something exciting if the old SQLite database
// still exists :-)
String database = lwc.getConfiguration().getString("database.path");
if (database == null || database.equals("")) {
return;
}
File file = new File(database);
if (!file.exists()) {
return;
}
logger.info("######################################################");
logger.info("######################################################");
logger.info("SQLite to MySQL conversion required");
logger.info("Loading SQLite");
// rev up those sqlite databases because I sure am hungry for some
// data...
PhysDB sqliteDatabase = new PhysDB(Type.SQLite);
try {
sqliteDatabase.connect();
sqliteDatabase.load();
logger.info("SQLite is good to go");
physicalDatabase.getConnection().setAutoCommit(false);
logger.info("Preliminary scan...............");
int startProtections = physicalDatabase.getProtectionCount();
int protections = sqliteDatabase.getProtectionCount();
int rights = sqliteDatabase.getRightsCount();
int expectedProtections = protections + startProtections;
logger.info("TO CONVERT:");
logger.info("Protections:\t" + protections);
logger.info("Rights:\t\t" + rights);
logger.info("");
if (protections > 0) {
logger.info("Converting: PROTECTIONS");
List<Protection> tmp = sqliteDatabase.loadProtections();
for (Protection protection : tmp) {
int x = protection.getX();
int y = protection.getY();
int z = protection.getZ();
// register it
physicalDatabase.registerProtection(protection.getBlockId(), protection.getType(), protection.getWorld(), protection.getOwner(), protection.getData(), x, y, z);
// get the new protection, to retrieve the id
Protection registered = physicalDatabase.loadProtection(protection.getWorld(), x, y, z);
// get the rights in the world
List<AccessRight> tmpRights = sqliteDatabase.loadRights(protection.getId());
// register the new rights using the newly registered protection
for (AccessRight right : tmpRights) {
physicalDatabase.registerProtectionRights(registered.getId(), right.getName(), right.getRights(), right.getType());
}
}
logger.info("COMMITTING");
physicalDatabase.getConnection().commit();
logger.info("OK , expecting: " + expectedProtections);
if (expectedProtections == (protections = physicalDatabase.getProtectionCount())) {
logger.info("OK.");
} else {
logger.info("Weird, only " + protections + " protections are in the database? Continuing...");
}
}
logger.info("Closing SQLite");
sqliteDatabase.getConnection().close();
logger.info("Renaming \"" + database + "\" to \"" + database + ".old\"");
if (!file.renameTo(new File(database + ".old"))) {
logger.info("NOTICE: FAILED TO RENAME lwc.db!! Please rename this manually!");
}
logger.info("SQLite to MySQL conversion is now complete!\n");
logger.info("Thank you!");
} catch (Exception e) {
logger.info("#### SEVERE ERROR: Something bad happened when converting the database (Oops!)");
e.printStackTrace();
}
try {
physicalDatabase.getConnection().setAutoCommit(true);
} catch (SQLException e) {
e.printStackTrace();
}
logger.info("######################################################");
logger.info("######################################################");
}
}