This commit is contained in:
Fuzzlemann 2017-08-16 17:14:51 +02:00
commit c615d9f9a2
15 changed files with 258 additions and 85 deletions

View File

@ -0,0 +1,12 @@
package main.java.com.djrapitops.plan.database.sql;
public class Sql {
public static final String INT = "integer";
public static final String DOUBLE = "double";
public static final String LONG = "bigint";
public static final String BOOL = "boolean";
public static String VARCHAR(int length) {
return "varchar("+length+")";
}
}

View File

@ -0,0 +1,35 @@
package main.java.com.djrapitops.plan.database.sql;
/**
* Class for parsing different SQL strings.
*
* @author Rsl1122
* @since 3.7.0
*/
public class SqlParser {
private StringBuilder s;
public SqlParser() {
s = new StringBuilder();
}
public SqlParser(String start) {
s = new StringBuilder(start);
}
public SqlParser addSpace() {
s.append(" ");
return this;
}
public SqlParser append(String string) {
s.append(string);
return this;
}
@Override
public String toString() {
return s.toString();
}
}

View File

@ -0,0 +1,101 @@
package main.java.com.djrapitops.plan.database.sql;
/**
* SqlParser Class for parsing table creation, removal & modification statements.
*
* @author Rsl1122
* @since 3.7.0
*/
public class TableSqlParser extends SqlParser {
private int columns = 0;
public TableSqlParser(String start) {
super(start);
}
public static TableSqlParser createTable(String tableName) {
return new TableSqlParser("CREATE TABLE IF NOT EXISTS " + tableName + " (");
}
public static String dropTable(String tableName) {
return "DROP TABLE IF EXISTS " + tableName;
}
public TableSqlParser column(String column, String type) {
if (columns > 0) {
append(", ");
}
append(column).addSpace();
append(type);
columns++;
return this;
}
public TableSqlParser foreignKey(String column, String refrencedTable, String referencedColumn) {
if (columns > 0) {
append(", ");
}
append("FOREIGN KEY(")
.append(column)
.append(") REFERENCES ")
.append(refrencedTable)
.append("(")
.append(referencedColumn)
.append(")");
columns++;
return this;
}
public TableSqlParser notNull() {
addSpace();
append("NOT NULL");
return this;
}
public TableSqlParser unique() {
addSpace();
append("UNIQUE");
return this;
}
public TableSqlParser defaultValue(boolean value) {
return defaultValue(value ? "1" : "0");
}
public TableSqlParser defaultValue(String value) {
addSpace();
append("DEFAULT ").append(value);
return this;
}
public TableSqlParser primaryKeyIDColumn(boolean mySQL, String column, String type) {
if (columns > 0) {
append(", ");
}
append(column).addSpace();
append(type).addSpace();
append((mySQL) ? "NOT NULL AUTO_INCREMENT" : "PRIMARY KEY");
columns++;
return this;
}
public TableSqlParser primaryKey(boolean mySQL, String column) {
if (mySQL) {
if (columns > 0) {
append(", ");
}
append("PRIMARY KEY (").append(column).append(")");
columns++;
}
return this;
}
@Override
public String toString() {
append(")");
return super.toString();
}
}

View File

@ -3,6 +3,8 @@ package main.java.com.djrapitops.plan.database.tables;
import com.djrapitops.plugin.utilities.Verify;
import main.java.com.djrapitops.plan.Log;
import main.java.com.djrapitops.plan.database.databases.SQLDB;
import main.java.com.djrapitops.plan.database.sql.Sql;
import main.java.com.djrapitops.plan.database.sql.TableSqlParser;
import main.java.com.djrapitops.plan.utilities.Benchmark;
import java.sql.PreparedStatement;
@ -35,10 +37,10 @@ public class CommandUseTable extends Table {
@Override
public boolean createTable() {
try {
execute("CREATE TABLE IF NOT EXISTS " + tableName + " ("
+ columnCommand + " varchar(20) NOT NULL, "
+ columnTimesUsed + " integer NOT NULL"
+ ")"
execute(TableSqlParser.createTable(tableName)
.column(columnCommand, Sql.VARCHAR(20)).notNull()
.column(columnTimesUsed, Sql.INT).notNull()
.toString()
);
return true;
} catch (SQLException ex) {

View File

@ -6,6 +6,8 @@ import main.java.com.djrapitops.plan.data.time.GMTimes;
import main.java.com.djrapitops.plan.database.Container;
import main.java.com.djrapitops.plan.database.DBUtils;
import main.java.com.djrapitops.plan.database.databases.SQLDB;
import main.java.com.djrapitops.plan.database.sql.Sql;
import main.java.com.djrapitops.plan.database.sql.TableSqlParser;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
@ -52,14 +54,14 @@ public class GMTimesTable extends UserIDTable {
public boolean createTable() {
UsersTable usersTable = db.getUsersTable();
try {
execute("CREATE TABLE IF NOT EXISTS " + tableName + " ("
+ columnUserID + " integer NOT NULL, "
+ columnSurvivalTime + " bigint NOT NULL, "
+ columnCreativeTime + " bigint NOT NULL, "
+ columnAdventureTime + " bigint NOT NULL, "
+ columnSpectatorTime + " bigint NOT NULL, "
+ "FOREIGN KEY(" + columnUserID + ") REFERENCES " + usersTable.getTableName() + "(" + usersTable.getColumnID() + ")"
+ ")"
execute(TableSqlParser.createTable(tableName)
.column(columnUserID, Sql.INT).notNull()
.column(columnSurvivalTime, Sql.LONG).notNull()
.column(columnCreativeTime, Sql.LONG).notNull()
.column(columnAdventureTime, Sql.LONG).notNull()
.column(columnSpectatorTime, Sql.LONG).notNull()
.foreignKey(columnUserID, usersTable.getTableName(), usersTable.getColumnID())
.toString()
);
return true;
} catch (SQLException ex) {

View File

@ -3,6 +3,8 @@ package main.java.com.djrapitops.plan.database.tables;
import com.djrapitops.plugin.utilities.Verify;
import main.java.com.djrapitops.plan.Log;
import main.java.com.djrapitops.plan.database.databases.SQLDB;
import main.java.com.djrapitops.plan.database.sql.Sql;
import main.java.com.djrapitops.plan.database.sql.TableSqlParser;
import main.java.com.djrapitops.plan.utilities.Benchmark;
import java.net.InetAddress;
@ -36,11 +38,11 @@ public class IPsTable extends UserIDTable {
public boolean createTable() {
UsersTable usersTable = db.getUsersTable();
try {
execute("CREATE TABLE IF NOT EXISTS " + tableName + " ("
+ columnUserID + " integer NOT NULL, "
+ columnIP + " varchar(20) NOT NULL, "
+ "FOREIGN KEY(" + columnUserID + ") REFERENCES " + usersTable.getTableName() + "(" + usersTable.getColumnID() + ")"
+ ")"
execute(TableSqlParser.createTable(tableName)
.column(columnUserID, Sql.INT).notNull()
.column(columnIP, Sql.VARCHAR(20)).notNull()
.foreignKey(columnUserID, usersTable.getTableName(), usersTable.getColumnID())
.toString()
);
return true;
} catch (SQLException ex) {

View File

@ -4,6 +4,8 @@ import com.djrapitops.plugin.utilities.Verify;
import main.java.com.djrapitops.plan.Log;
import main.java.com.djrapitops.plan.data.KillData;
import main.java.com.djrapitops.plan.database.databases.SQLDB;
import main.java.com.djrapitops.plan.database.sql.Sql;
import main.java.com.djrapitops.plan.database.sql.TableSqlParser;
import main.java.com.djrapitops.plan.utilities.Benchmark;
import java.sql.PreparedStatement;
@ -40,14 +42,14 @@ public class KillsTable extends Table {
public boolean createTable() {
UsersTable usersTable = db.getUsersTable();
try {
execute("CREATE TABLE IF NOT EXISTS " + tableName + " ("
+ columnKillerUserID + " integer NOT NULL, "
+ columnVictimUserID + " integer NOT NULL, "
+ columnWeapon + " varchar(30) NOT NULL, "
+ columnDate + " bigint NOT NULL, "
+ "FOREIGN KEY(" + columnKillerUserID + ") REFERENCES " + usersTable.getTableName() + "(" + usersTable.getColumnID() + "), "
+ "FOREIGN KEY(" + columnVictimUserID + ") REFERENCES " + usersTable.getTableName() + "(" + usersTable.getColumnID() + ")"
+ ")"
execute(TableSqlParser.createTable(tableName)
.column(columnKillerUserID, Sql.INT).notNull()
.column(columnVictimUserID, Sql.INT).notNull()
.column(columnWeapon, Sql.VARCHAR(30)).notNull()
.column(columnDate, Sql.LONG).notNull()
.foreignKey(columnKillerUserID, usersTable.getTableName(), usersTable.getColumnID())
.foreignKey(columnVictimUserID, usersTable.getTableName(), usersTable.getColumnID())
.toString()
);
return true;
} catch (SQLException ex) {

View File

@ -3,6 +3,8 @@ package main.java.com.djrapitops.plan.database.tables;
import com.djrapitops.plugin.utilities.Verify;
import main.java.com.djrapitops.plan.Log;
import main.java.com.djrapitops.plan.database.databases.SQLDB;
import main.java.com.djrapitops.plan.database.sql.Sql;
import main.java.com.djrapitops.plan.database.sql.TableSqlParser;
import main.java.com.djrapitops.plan.utilities.Benchmark;
import java.sql.PreparedStatement;
@ -36,12 +38,12 @@ public class NicknamesTable extends UserIDTable {
public boolean createTable() {
UsersTable usersTable = db.getUsersTable();
try {
execute("CREATE TABLE IF NOT EXISTS " + tableName + " ("
+ columnUserID + " integer NOT NULL, "
+ columnNick + " varchar(75) NOT NULL, "
+ columnCurrent + " boolean NOT NULL DEFAULT 0, "
+ "FOREIGN KEY(" + columnUserID + ") REFERENCES " + usersTable.getTableName() + "(" + usersTable.getColumnID() + ")"
+ ")"
execute(TableSqlParser.createTable(tableName)
.column(columnUserID, Sql.INT).notNull()
.column(columnNick, Sql.VARCHAR(75)).notNull()
.column(columnCurrent, Sql.BOOL).notNull().defaultValue(false)
.foreignKey(columnUserID, usersTable.getTableName(), usersTable.getColumnID())
.toString()
);
if (getVersion() < 3) {

View File

@ -8,6 +8,8 @@ package main.java.com.djrapitops.plan.database.tables;
import main.java.com.djrapitops.plan.Log;
import main.java.com.djrapitops.plan.data.WebUser;
import main.java.com.djrapitops.plan.database.databases.SQLDB;
import main.java.com.djrapitops.plan.database.sql.Sql;
import main.java.com.djrapitops.plan.database.sql.TableSqlParser;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
@ -34,10 +36,11 @@ public class SecurityTable extends Table {
@Override
public boolean createTable() {
try {
execute("CREATE TABLE IF NOT EXISTS " + tableName + " ("
+ columnUser + " varchar(100) NOT NULL UNIQUE, "
+ columnSaltedHash + " varchar(100) NOT NULL UNIQUE, "
+ columnPermLevel + " integer NOT NULL)"
execute(TableSqlParser.createTable(tableName)
.column(columnUser, Sql.VARCHAR(100)).notNull().unique()
.column(columnSaltedHash, Sql.VARCHAR(100)).notNull().unique()
.column(columnPermLevel, Sql.INT).notNull()
.toString()
);
return true;
} catch (SQLException ex) {

View File

@ -4,6 +4,8 @@ import main.java.com.djrapitops.plan.Log;
import main.java.com.djrapitops.plan.data.SessionData;
import main.java.com.djrapitops.plan.database.Container;
import main.java.com.djrapitops.plan.database.databases.SQLDB;
import main.java.com.djrapitops.plan.database.sql.Sql;
import main.java.com.djrapitops.plan.database.sql.TableSqlParser;
import main.java.com.djrapitops.plan.utilities.Benchmark;
import java.sql.PreparedStatement;
@ -37,12 +39,12 @@ public class SessionsTable extends UserIDTable {
public boolean createTable() {
try {
UsersTable usersTable = db.getUsersTable();
execute("CREATE TABLE IF NOT EXISTS " + tableName + " ("
+ columnUserID + " integer NOT NULL, "
+ columnSessionStart + " bigint NOT NULL, "
+ columnSessionEnd + " bigint NOT NULL, "
+ "FOREIGN KEY(" + columnUserID + ") REFERENCES " + usersTable.getTableName() + "(" + usersTable.getColumnID() + ")"
+ ")"
execute(TableSqlParser.createTable(tableName)
.column(columnUserID, Sql.INT).notNull()
.column(columnSessionStart, Sql.LONG).notNull()
.column(columnSessionEnd, Sql.LONG).notNull()
.foreignKey(columnUserID, usersTable.getTableName(), usersTable.getColumnID())
.toString()
);
return true;
} catch (SQLException ex) {

View File

@ -5,6 +5,8 @@ import main.java.com.djrapitops.plan.Log;
import main.java.com.djrapitops.plan.data.TPS;
import main.java.com.djrapitops.plan.database.DBUtils;
import main.java.com.djrapitops.plan.database.databases.SQLDB;
import main.java.com.djrapitops.plan.database.sql.Sql;
import main.java.com.djrapitops.plan.database.sql.TableSqlParser;
import main.java.com.djrapitops.plan.utilities.Benchmark;
import main.java.com.djrapitops.plan.utilities.MiscUtils;
@ -48,15 +50,15 @@ public class TPSTable extends Table {
@Override
public boolean createTable() {
try {
execute("CREATE TABLE IF NOT EXISTS " + tableName + " ("
+ columnDate + " bigint NOT NULL, "
+ columnTPS + " double NOT NULL, "
+ columnPlayers + " integer NOT NULL, "
+ columnCPUUsage + " double NOT NULL, "
+ columnRAMUsage + " bigint NOT NULL, "
+ columnEntities + " integer NOT NULL, "
+ columnChunksLoaded + " integer NOT NULL"
+ ")"
execute(TableSqlParser.createTable(tableName)
.column(columnDate, Sql.LONG).notNull()
.column(columnTPS, Sql.DOUBLE).notNull()
.column(columnPlayers, Sql.INT).notNull()
.column(columnCPUUsage, Sql.DOUBLE).notNull()
.column(columnRAMUsage, Sql.LONG).notNull()
.column(columnEntities, Sql.INT).notNull()
.column(columnChunksLoaded, Sql.INT).notNull()
.toString()
);
int version = getVersion();
if (version < 6) {

View File

@ -8,6 +8,8 @@ import main.java.com.djrapitops.plan.data.time.GMTimes;
import main.java.com.djrapitops.plan.data.time.WorldTimes;
import main.java.com.djrapitops.plan.database.DBUtils;
import main.java.com.djrapitops.plan.database.databases.SQLDB;
import main.java.com.djrapitops.plan.database.sql.Sql;
import main.java.com.djrapitops.plan.database.sql.TableSqlParser;
import main.java.com.djrapitops.plan.utilities.Benchmark;
import main.java.com.djrapitops.plan.utilities.uuid.UUIDUtility;
@ -95,26 +97,26 @@ public class UsersTable extends Table {
@Override
public boolean createTable() {
try {
execute("CREATE TABLE IF NOT EXISTS " + tableName + " ("
+ columnID + " integer " + ((usingMySQL) ? "NOT NULL AUTO_INCREMENT" : "PRIMARY KEY") + ", "
+ columnUUID + " varchar(36) NOT NULL UNIQUE, "
+ columnGeolocation + " varchar(50) NOT NULL, "
+ columnLastGM + " varchar(15) NOT NULL, "
+ columnLastGMSwapTime + " bigint NOT NULL, "
+ columnPlayTime + " bigint NOT NULL, "
+ columnLoginTimes + " integer NOT NULL, "
+ columnLastPlayed + " bigint NOT NULL, "
+ columnDeaths + " int NOT NULL, "
+ columnMobKills + " int NOT NULL, "
+ columnRegistered + " bigint NOT NULL, "
+ columnOP + " boolean NOT NULL DEFAULT 0, "
+ columnName + " varchar(16) NOT NULL, "
+ columnBanned + " boolean NOT NULL DEFAULT 0, "
+ columnContainsBukkitData + " boolean NOT NULL DEFAULT 0, "
+ columnLastWorld + " varchar(255) NOT NULL, "
+ columnLastWorldSwapTime + " bigint NOT NULL"
+ (usingMySQL ? ", PRIMARY KEY (" + columnID + ")" : "")
+ ")"
execute(TableSqlParser.createTable(tableName)
.primaryKeyIDColumn(usingMySQL, columnID, Sql.INT)
.column(columnUUID, Sql.VARCHAR(36)).notNull().unique()
.column(columnGeolocation, Sql.VARCHAR(50)).notNull()
.column(columnLastGM, Sql.VARCHAR(15)).notNull()
.column(columnLastGMSwapTime, Sql.LONG).notNull()
.column(columnPlayTime, Sql.LONG).notNull()
.column(columnLoginTimes, Sql.INT).notNull()
.column(columnLastPlayed, Sql.LONG).notNull()
.column(columnDeaths, Sql.INT).notNull()
.column(columnMobKills, Sql.INT).notNull()
.column(columnRegistered, Sql.LONG).notNull()
.column(columnOP, Sql.BOOL).notNull().defaultValue(false)
.column(columnName, Sql.VARCHAR(16)).notNull()
.column(columnBanned, Sql.BOOL).notNull().defaultValue(false)
.column(columnContainsBukkitData, Sql.BOOL).notNull().defaultValue(false)
.column(columnLastWorld, Sql.VARCHAR(255)).notNull()
.column(columnLastWorldSwapTime, Sql.LONG).notNull()
.primaryKey(usingMySQL, columnID)
.toString()
);
int version = getVersion();
if (version < 3) {

View File

@ -2,6 +2,8 @@ package main.java.com.djrapitops.plan.database.tables;
import main.java.com.djrapitops.plan.Log;
import main.java.com.djrapitops.plan.database.databases.SQLDB;
import main.java.com.djrapitops.plan.database.sql.Sql;
import main.java.com.djrapitops.plan.database.sql.TableSqlParser;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
@ -26,9 +28,9 @@ public class VersionTable extends Table {
@Override
public boolean createTable() {
try {
execute("CREATE TABLE IF NOT EXISTS " + tableName + " ("
+ "version integer NOT NULL"
+ ")"
execute(TableSqlParser.createTable(tableName)
.column("version", Sql.INT).notNull()
.toString()
);
return true;
} catch (SQLException ex) {

View File

@ -3,6 +3,8 @@ package main.java.com.djrapitops.plan.database.tables;
import com.djrapitops.plugin.utilities.Verify;
import main.java.com.djrapitops.plan.Log;
import main.java.com.djrapitops.plan.database.databases.SQLDB;
import main.java.com.djrapitops.plan.database.sql.Sql;
import main.java.com.djrapitops.plan.database.sql.TableSqlParser;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
@ -39,11 +41,11 @@ public class WorldTable extends Table {
@Override
public boolean createTable() {
try {
execute("CREATE TABLE IF NOT EXISTS " + tableName + " ("
+ columnWorldId + " integer " + ((usingMySQL) ? "NOT NULL AUTO_INCREMENT" : "PRIMARY KEY") + ", "
+ columnWorldName + " varchar(100) NOT NULL"
+ (usingMySQL ? ", PRIMARY KEY (" + columnWorldId + ")" : "")
+ ")"
execute(TableSqlParser.createTable(tableName)
.primaryKeyIDColumn(usingMySQL, columnWorldId, Sql.INT)
.column(columnWorldName, Sql.VARCHAR(100)).notNull()
.primaryKey(usingMySQL, columnWorldId)
.toString()
);
return true;
} catch (SQLException ex) {

View File

@ -3,6 +3,8 @@ package main.java.com.djrapitops.plan.database.tables;
import com.djrapitops.plugin.utilities.Verify;
import main.java.com.djrapitops.plan.Log;
import main.java.com.djrapitops.plan.database.databases.SQLDB;
import main.java.com.djrapitops.plan.database.sql.Sql;
import main.java.com.djrapitops.plan.database.sql.TableSqlParser;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
@ -50,13 +52,13 @@ public class WorldTimesTable extends UserIDTable {
public boolean createTable() {
UsersTable usersTable = db.getUsersTable();
try {
execute("CREATE TABLE IF NOT EXISTS " + tableName + " ("
+ columnUserID + " integer NOT NULL, "
+ columnWorldId + " integer NOT NULL, "
+ columnPlaytime + " bigint NOT NULL, "
+ "FOREIGN KEY(" + columnUserID + ") REFERENCES " + usersTable.getTableName() + "(" + usersTable.getColumnID() + "), "
+ "FOREIGN KEY(" + columnWorldId + ") REFERENCES " + worldTable.getTableName() + "(" + worldTable.getColumnID() + ")"
+ ")"
execute(TableSqlParser.createTable(tableName)
.column(columnUserID, Sql.INT).notNull()
.column(columnWorldId, Sql.INT).notNull()
.column(columnPlaytime, Sql.LONG).notNull()
.foreignKey(columnUserID, usersTable.getTableName(), usersTable.getColumnID())
.foreignKey(columnWorldId, worldTable.getTableName(), worldTable.getColumnID())
.toString()
);
return true;
} catch (SQLException ex) {