Deprecate flatile datasource, force flat to sqlite conversion #344

This commit is contained in:
Xephi59
2015-12-26 15:38:16 +01:00
parent 72365bc6e4
commit 41e400e9dd
8 changed files with 62 additions and 415 deletions
@@ -1,84 +0,0 @@
package fr.xephi.authme.converter;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.settings.Settings;
import java.io.*;
/**
* @author Xephi59
* @version $Revision: 1.0 $
*/
public class FlatToSql implements Converter {
private static String tableName;
private static String columnName;
private static String columnPassword;
private static String columnIp;
private static String columnLastLogin;
private static String lastlocX;
private static String lastlocY;
private static String lastlocZ;
private static String lastlocWorld;
private static String columnEmail;
private static String columnLogged;
private static String columnID;
public FlatToSql() {
tableName = Settings.getMySQLTablename;
columnName = Settings.getMySQLColumnName;
columnPassword = Settings.getMySQLColumnPassword;
columnIp = Settings.getMySQLColumnIp;
columnLastLogin = Settings.getMySQLColumnLastLogin;
lastlocX = Settings.getMySQLlastlocX;
lastlocY = Settings.getMySQLlastlocY;
lastlocZ = Settings.getMySQLlastlocZ;
lastlocWorld = Settings.getMySQLlastlocWorld;
columnEmail = Settings.getMySQLColumnEmail;
columnLogged = Settings.getMySQLColumnLogged;
columnID = Settings.getMySQLColumnId;
}
/**
* Method run.
*
* @see java.lang.Runnable#run()
*/
@Override
public void run() {
try {
File source = new File(AuthMe.getInstance().getDataFolder() + File.separator + "auths.db");
source.createNewFile();
File output = new File(AuthMe.getInstance().getDataFolder() + File.separator + "authme.sql");
output.createNewFile();
BufferedReader br = new BufferedReader(new FileReader(source));
BufferedWriter sql = new BufferedWriter(new FileWriter(output));
String createDB = "CREATE TABLE IF NOT EXISTS " + tableName + " (" + columnID + " INTEGER AUTO_INCREMENT," + columnName + " VARCHAR(255) NOT NULL UNIQUE," + columnPassword + " VARCHAR(255) NOT NULL," + columnIp + " VARCHAR(40) NOT NULL DEFAULT '127.0.0.1'," + columnLastLogin + " BIGINT NOT NULL DEFAULT '" + System.currentTimeMillis() + "'," + lastlocX + " DOUBLE NOT NULL DEFAULT '0.0'," + lastlocY + " DOUBLE NOT NULL DEFAULT '0.0'," + lastlocZ + " DOUBLE NOT NULL DEFAULT '0.0'," + lastlocWorld + " VARCHAR(255) DEFAULT 'world'," + columnEmail + " VARCHAR(255) DEFAULT 'your@email.com'," + columnLogged + " SMALLINT NOT NULL DEFAULT '0'," + "CONSTRAINT table_const_prim PRIMARY KEY (" + columnID + "));";
sql.write(createDB);
String line;
String newline;
while ((line = br.readLine()) != null) {
sql.newLine();
String[] args = line.split(":");
if (args.length == 4)
newline = "INSERT INTO " + tableName + "(" + columnName + "," + columnPassword + "," + columnIp + "," + columnLastLogin + "," + lastlocX + "," + lastlocY + "," + lastlocZ + "," + lastlocWorld + "," + columnEmail + "," + columnLogged + ") VALUES ('" + args[0] + "', '" + args[1] + "', '" + args[2] + "', " + args[3] + ", 0.0, 0.0, 0.0, 'world', 'your@email.com', 0);";
else if (args.length == 7)
newline = "INSERT INTO " + tableName + "(" + columnName + "," + columnPassword + "," + columnIp + "," + columnLastLogin + "," + lastlocX + "," + lastlocY + "," + lastlocZ + "," + lastlocWorld + "," + columnEmail + "," + columnLogged + ") VALUES ('" + args[0] + "', '" + args[1] + "', '" + args[2] + "', " + args[3] + ", " + args[4] + ", " + args[5] + ", " + args[6] + ", 'world', 'your@email.com', 0);";
else if (args.length == 8)
newline = "INSERT INTO " + tableName + "(" + columnName + "," + columnPassword + "," + columnIp + "," + columnLastLogin + "," + lastlocX + "," + lastlocY + "," + lastlocZ + "," + lastlocWorld + "," + columnEmail + "," + columnLogged + ") VALUES ('" + args[0] + "', '" + args[1] + "', '" + args[2] + "', " + args[3] + ", " + args[4] + ", " + args[5] + ", " + args[6] + ", '" + args[7] + "', 'your@email.com', 0);";
else if (args.length == 9)
newline = "INSERT INTO " + tableName + "(" + columnName + "," + columnPassword + "," + columnIp + "," + columnLastLogin + "," + lastlocX + "," + lastlocY + "," + lastlocZ + "," + lastlocWorld + "," + columnEmail + "," + columnLogged + ") VALUES ('" + args[0] + "', '" + args[1] + "', '" + args[2] + "', " + args[3] + ", " + args[4] + ", " + args[5] + ", " + args[6] + ", '" + args[7] + "', '" + args[8] + "', 0);";
else newline = "";
if (!newline.equals(""))
sql.write(newline);
}
sql.close();
br.close();
ConsoleLogger.info("The FlatFile has been converted to authme.sql file");
} catch (IOException ex) {
ConsoleLogger.showError(ex.getMessage());
ConsoleLogger.showError("Can't open the flat database file! Does it exist?");
}
}
}
@@ -1,198 +0,0 @@
package fr.xephi.authme.converter;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.settings.Settings;
import org.bukkit.command.CommandSender;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.sql.*;
/**
*/
public class FlatToSqlite implements Converter {
public final CommandSender sender;
private String tableName;
private String columnName;
private String columnPassword;
private String columnIp;
private String columnLastLogin;
private String lastlocX;
private String lastlocY;
private String lastlocZ;
private String lastlocWorld;
private String columnEmail;
private String database;
private String columnID;
private Connection con;
/**
* Constructor for FlatToSqlite.
*
* @param sender CommandSender
*/
public FlatToSqlite(CommandSender sender) {
this.sender = sender;
}
/**
* Method close.
*
* @param o AutoCloseable
*/
private static void close(AutoCloseable o) {
if (o != null) {
try {
o.close();
} catch (Exception ex) {
ConsoleLogger.showError(ex.getMessage());
}
}
}
/**
* Method run.
*
* @see java.lang.Runnable#run()
*/
@Override
public void run() {
database = Settings.getMySQLDatabase;
tableName = Settings.getMySQLTablename;
columnName = Settings.getMySQLColumnName;
columnPassword = Settings.getMySQLColumnPassword;
columnIp = Settings.getMySQLColumnIp;
columnLastLogin = Settings.getMySQLColumnLastLogin;
lastlocX = Settings.getMySQLlastlocX;
lastlocY = Settings.getMySQLlastlocY;
lastlocZ = Settings.getMySQLlastlocZ;
lastlocWorld = Settings.getMySQLlastlocWorld;
columnEmail = Settings.getMySQLColumnEmail;
columnID = Settings.getMySQLColumnId;
File source = new File(Settings.PLUGIN_FOLDER, "auths.db");
if (!source.exists()) {
sender.sendMessage("Source file for FlatFile database not found... Aborting");
return;
}
try {
connect();
setup();
} catch (Exception e) {
sender.sendMessage("Some error appeared while trying to setup and connect to sqlite database... Aborting");
return;
}
try (BufferedReader reader = new BufferedReader(new FileReader(source))) {
String line;
int i = 1;
String newline;
while ((line = reader.readLine()) != null) {
String[] args = line.split(":");
if (args.length == 4)
newline = "INSERT INTO " + tableName + " VALUES (" + i + ", '" + args[0] + "', '" + args[1] + "', '" + args[2] + "', " + args[3] + ", 0, 0, 0, 'world', 'your@email.com');";
else if (args.length == 7)
newline = "INSERT INTO " + tableName + " VALUES (" + i + ", '" + args[0] + "', '" + args[1] + "', '" + args[2] + "', " + args[3] + ", " + args[4] + ", " + args[5] + ", " + args[6] + ", 'world', 'your@email.com');";
else if (args.length == 8)
newline = "INSERT INTO " + tableName + " VALUES (" + i + ", '" + args[0] + "', '" + args[1] + "', '" + args[2] + "', " + args[3] + ", " + args[4] + ", " + args[5] + ", " + args[6] + ", '" + args[7] + "', 'your@email.com');";
else if (args.length == 9)
newline = "INSERT INTO " + tableName + " VALUES (" + i + ", '" + args[0] + "', '" + args[1] + "', '" + args[2] + "', " + args[3] + ", " + args[4] + ", " + args[5] + ", " + args[6] + ", '" + args[7] + "', '" + args[8] + "');";
else newline = "";
if (!newline.equals(""))
saveAuth(newline);
i = i + 1;
}
String resp = "The FlatFile has been converted to " + database + ".db file";
ConsoleLogger.info(resp);
sender.sendMessage(resp);
} catch (IOException ex) {
ConsoleLogger.showError(ex.getMessage());
sender.sendMessage("Can't open the flat database file!");
} finally {
close(con);
}
}
/**
* Method connect.
*
* @throws ClassNotFoundException * @throws SQLException
*/
private synchronized void connect() throws ClassNotFoundException, SQLException {
Class.forName("org.sqlite.JDBC");
con = DriverManager.getConnection("jdbc:sqlite:plugins/AuthMe/" + database + ".db");
}
/**
* Method setup.
*
* @throws SQLException
*/
private synchronized void setup() throws SQLException {
Statement st = null;
ResultSet rs = null;
try {
st = con.createStatement();
st.executeUpdate("CREATE TABLE IF NOT EXISTS " + tableName + " (" + columnID + " INTEGER AUTO_INCREMENT," + columnName + " VARCHAR(255) NOT NULL UNIQUE," + columnPassword + " VARCHAR(255) NOT NULL," + columnIp + " VARCHAR(40) NOT NULL," + columnLastLogin + " BIGINT," + lastlocX + " DOUBLE NOT NULL DEFAULT '0.0'," + lastlocY + " DOUBLE NOT NULL DEFAULT '0.0'," + lastlocZ + " DOUBLE NOT NULL DEFAULT '0.0'," + lastlocWorld + " VARCHAR(255) NOT NULL DEFAULT '" + Settings.defaultWorld + "'," + columnEmail + " VARCHAR(255) DEFAULT 'your@email.com'," + "CONSTRAINT table_const_prim PRIMARY KEY (" + columnID + "));");
rs = con.getMetaData().getColumns(null, null, tableName, columnPassword);
if (!rs.next()) {
st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN " + columnPassword + " VARCHAR(255) NOT NULL;");
}
rs.close();
rs = con.getMetaData().getColumns(null, null, tableName, columnIp);
if (!rs.next()) {
st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN " + columnIp + " VARCHAR(40) NOT NULL;");
}
rs.close();
rs = con.getMetaData().getColumns(null, null, tableName, columnLastLogin);
if (!rs.next()) {
st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN " + columnLastLogin + " BIGINT;");
}
rs.close();
rs = con.getMetaData().getColumns(null, null, tableName, lastlocX);
if (!rs.next()) {
st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN " + lastlocX + " DOUBLE NOT NULL DEFAULT '0.0';");
st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN " + lastlocY + " DOUBLE NOT NULL DEFAULT '0.0';");
st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN " + lastlocZ + " DOUBLE NOT NULL DEFAULT '0.0';");
}
rs.close();
rs = con.getMetaData().getColumns(null, null, tableName, lastlocWorld);
if (!rs.next()) {
st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN " + lastlocWorld + " VARCHAR(255) NOT NULL DEFAULT 'world';");
}
rs.close();
rs = con.getMetaData().getColumns(null, null, tableName, columnEmail);
if (!rs.next()) {
st.executeUpdate("ALTER TABLE " + tableName + " ADD COLUMN " + columnEmail + " VARCHAR(255) DEFAULT 'your@email.com';");
}
} finally {
close(rs);
close(st);
}
}
/**
* Method saveAuth.
*
* @param s String
*
* @return boolean
*/
private synchronized boolean saveAuth(String s) {
PreparedStatement pst = null;
try {
pst = con.prepareStatement(s);
pst.executeUpdate();
} catch (SQLException e) {
ConsoleLogger.showError(e.getMessage());
return false;
} finally {
close(pst);
}
return true;
}
}
@@ -9,7 +9,7 @@ import fr.xephi.authme.settings.Settings;
/**
*/
public class ForceFlatToSqlite implements Converter {
public class ForceFlatToSqlite {
private final DataSource data;
@@ -28,8 +28,7 @@ public class ForceFlatToSqlite implements Converter {
*
* @see java.lang.Runnable#run()
*/
@Override
public void run() {
public DataSource run() {
DataSource sqlite = null;
try {
sqlite = new SQLite();
@@ -40,10 +39,9 @@ public class ForceFlatToSqlite implements Converter {
Settings.setValue("DataSource.backend", "sqlite");
ConsoleLogger.info("Database successfully converted to sqlite !");
} catch (Exception e) {
ConsoleLogger.showError("An error appeared while trying to convert flatfile to sqlite ...");
} finally {
if (sqlite != null)
sqlite.close();
ConsoleLogger.showError("An error occured while trying to convert flatfile to sqlite ...");
return null;
}
return sqlite;
}
}
@@ -1,59 +0,0 @@
package fr.xephi.authme.converter;
import fr.xephi.authme.AuthMe;
import fr.xephi.authme.ConsoleLogger;
import fr.xephi.authme.cache.auth.PlayerAuth;
import fr.xephi.authme.datasource.DataSource;
import fr.xephi.authme.datasource.FlatFile;
import fr.xephi.authme.output.MessageKey;
import org.bukkit.command.CommandSender;
import java.util.List;
/**
*/
public class SqlToFlat implements Converter {
public final AuthMe plugin;
public final DataSource database;
public final CommandSender sender;
/**
* Constructor for SqlToFlat.
*
* @param plugin AuthMe
* @param sender CommandSender
*/
public SqlToFlat(AuthMe plugin, CommandSender sender) {
this.plugin = plugin;
this.database = plugin.database;
this.sender = sender;
}
/**
* Method run.
*
* @see java.lang.Runnable#run()
*/
@Override
public void run() {
try {
FlatFile flat = new FlatFile();
List<PlayerAuth> auths = database.getAllAuths();
int i = 0;
final int size = auths.size();
for (PlayerAuth auth : auths) {
flat.saveAuth(auth);
i++;
if ((i % 100) == 0) {
sender.sendMessage("Conversion Status : " + i + " / " + size);
}
}
sender.sendMessage("Successfully convert from SQL table to file auths.db");
} catch (Exception ex) {
ConsoleLogger.showError(ex.getMessage());
plugin.getMessages().send(sender, MessageKey.ERROR);
}
}
}