· 7 years ago · Jan 08, 2019, 09:10 PM
1package ru.deelter.database;
2
3import java.sql.Connection;
4import java.sql.PreparedStatement;
5import java.sql.ResultSet;
6import java.sql.SQLException;
7import java.util.logging.Level;
8
9import ru.deelter.Main;
10
11public class Database {
12
13 private static Main plugin;
14 private static boolean mysql = false;
15
16 public static void setupDatabase(Main instance) {
17 plugin = instance;
18
19 mysql = plugin.getConfig().getBoolean("Connection.UseMySQL");
20
21 if (mysql)
22 MySQL.setupMySQLDatabase(plugin);
23 else
24 SQLite.setupSQLiteDatabase(plugin);
25
26 openConnection();
27 setupTables(!mysql);
28 closeConnection();
29 }
30
31 public static Connection getConnection() {
32 if (mysql)
33 return MySQL.getConnection();
34 else
35 return SQLite.getConnection();
36 }
37
38 public static void openConnection() {
39 if (mysql)
40 MySQL.openConnection();
41 else
42 SQLite.openConnection();
43 }
44
45 public static void closeConnection() {
46 if (mysql)
47 MySQL.closeConnection();
48 else
49 SQLite.closeConnection();
50 }
51
52 private static void setupTables(boolean sqlite) {
53 String sql1 = "CREATE TABLE IF NOT EXISTS Antennas(" +
54 "`UUID` varchar(64) NOT NULL," +
55 "PRIMARY KEY (`UUID`)" +
56 ");";
57
58 try {
59 PreparedStatement stmt1;
60 if (sqlite) {
61 //SQLite.openConnection();
62 stmt1 = SQLite.getConnection().prepareStatement(sql1);
63 } else {
64 //MySQL.openConnection();
65 stmt1 = MySQL.getConnection().prepareStatement(sql1);
66 }
67 stmt1.executeUpdate();
68 //checkDatabase();
69 } catch (SQLException e) {
70 e.printStackTrace();
71 }/* finally {
72 if (sqlite)
73 SQLite.closeConnection();
74 else
75 MySQL.closeConnection();
76 }*/
77 }
78
79 /*private static void checkDatabase() {
80 String sql = "ALTER TABLE PlayerOptions ADD COLUMN FlyMode tinyint(1) NOT NULL DEFAULT 0;";
81 try {
82 PreparedStatement stmt = getConnection().prepareStatement(sql);
83 stmt.executeUpdate();
84 } catch (SQLException e) {
85 e.printStackTrace();
86 }
87 }*/
88
89// DB Migration
90 public static void migrateDatabase() {
91 plugin.getLogger().log(Level.SEVERE, "Starting migration of the data...");
92 try {
93 ResultSet rs;
94 PreparedStatement ps;
95 if (mysql) {
96 SQLite.setupSQLiteDatabase(plugin);
97 MySQL.openConnection();
98 SQLite.openConnection();
99 rs = MySQL.getConnection().createStatement().executeQuery("SELECT * FROM Nutrition");
100 } else {
101 MySQL.setupMySQLDatabase(plugin);
102 MySQL.openConnection();
103 SQLite.openConnection();
104 rs = SQLite.getConnection().createStatement().executeQuery("SELECT * FROM Nutrition");
105 }
106 setupTables(mysql);
107 if (mysql) {
108 rs.close();
109 rs = MySQL.getConnection().createStatement().executeQuery("SELECT * FROM RespawnLocs");
110 } else {
111 rs.close();
112 rs = SQLite.getConnection().createStatement().executeQuery("SELECT * FROM RespawnLocs");
113 }
114 while (rs.next()) {
115 // Getting data
116 String uuid = rs.getString("UUID");
117 String world = rs.getString("World");
118 int x = rs.getInt("X");
119 int y = rs.getInt("Y");
120 int z = rs.getInt("Z");
121 // Saving data
122 if (mysql)
123 ps = SQLite.getConnection().prepareStatement("REPLACE INTO RespawnLocs (UUID,world,X,Y,Z) VALUES(?,?,?,?,?)");
124 else
125 ps = MySQL.getConnection().prepareStatement("REPLACE INTO RespawnLocs (UUID,world,X,Y,Z) VALUES(?,?,?,?,?)");
126 ps.setString(1, uuid);
127 ps.setString(2, world);
128 ps.setInt(3, x);
129 ps.setInt(4, y);
130 ps.setInt(5, z);
131 ps.executeUpdate();
132 }
133 if (mysql) {
134 rs.close();
135 rs = MySQL.getConnection().createStatement().executeQuery("SELECT * FROM PlayerOptions");
136 } else {
137 rs.close();
138 rs = SQLite.getConnection().createStatement().executeQuery("SELECT * FROM PlayerOptions");
139 }
140 while (rs.next()) {
141 // Getting data
142 String uuid = rs.getString("UUID");
143 boolean autorespawn = rs.getBoolean("AutoRespawn");
144 boolean godmode = rs.getBoolean("GodMode");
145 // Saving data
146 if (mysql)
147 ps = SQLite.getConnection().prepareStatement("REPLACE INTO PlayerOptions (UUID,AutoRespawn,GodMode) VALUES(?,?,?)");
148 else
149 ps = MySQL.getConnection().prepareStatement("REPLACE INTO PlayerOptions (UUID,AutoRespawn,GodMode) VALUES(?,?,?)");
150 ps.setString(1, uuid);
151 ps.setBoolean(2, autorespawn);
152 ps.setBoolean(3, godmode);
153 ps.executeUpdate();
154 }
155 } catch (SQLException e) {
156 e.printStackTrace();
157 } finally {
158 MySQL.closeConnection();
159 SQLite.closeConnection();
160 if (mysql)
161 plugin.getConfig().set("Connection.UseMySQL", "false");
162 else
163 plugin.getConfig().set("Connection.UseMySQL", "true");
164 plugin.saveConfig();
165 plugin.getLogger().log(Level.SEVERE, "Done!");
166 plugin.getServer().shutdown();
167 }
168 }
169
170}