Added new TPS Table columns

Added Worlds Table
Added WorldTimes Table
DB Version bumped to 7

Added averages for deaths, mobkills & playerkills
This commit is contained in:
Rsl1122 2017-07-27 10:32:45 +03:00
parent 65d0e09ace
commit 53cbe88a2c
14 changed files with 472 additions and 48 deletions

View File

@ -0,0 +1,13 @@
<component name="libraryTable">
<library name="Maven: com.destroystokyo.paper:paper:1.12-R0.1-SNAPSHOT">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/com/destroystokyo/paper/paper/1.12-R0.1-SNAPSHOT/paper-1.12-R0.1-20170725.202533-1.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$MAVEN_REPOSITORY$/com/destroystokyo/paper/paper/1.12-R0.1-SNAPSHOT/paper-1.12-R0.1-20170725.202533-1-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/com/destroystokyo/paper/paper/1.12-R0.1-SNAPSHOT/paper-1.12-R0.1-20170725.202533-1-sources.jar!/" />
</SOURCES>
</library>
</component>

View File

@ -0,0 +1,14 @@
<component name="libraryTable">
<library name="Maven: com.destroystokyo.paper:paper-spigot:1.12">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/com/destroystokyo/paper/paper-spigot/1.12/paper-spigot-1.12.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$MAVEN_REPOSITORY$/com/destroystokyo/paper/paper-spigot/1.12/paper-spigot-1.12-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://D:/Downloads/PaperSpigot-latest.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/com/destroystokyo/paper/paper-spigot/1.12/paper-spigot-1.12-sources.jar!/" />
</SOURCES>
</library>
</component>

View File

@ -0,0 +1,13 @@
<component name="libraryTable">
<library name="Maven: com.djrapitops:abstract-plugin-framework:2.0.0">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/com/djrapitops/abstract-plugin-framework/2.0.0/abstract-plugin-framework-2.0.0.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$MAVEN_REPOSITORY$/com/djrapitops/abstract-plugin-framework/2.0.0/abstract-plugin-framework-2.0.0-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/com/djrapitops/abstract-plugin-framework/2.0.0/abstract-plugin-framework-2.0.0-sources.jar!/" />
</SOURCES>
</library>
</component>

124
Plan/.idea/uiDesigner.xml Normal file
View File

@ -0,0 +1,124 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Palette2">
<group name="Swing">
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
</item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item>
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.png" removable="false" auto-create-binding="false" can-attach-label="true">
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
</item>
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
<initial-values>
<property name="text" value="Button" />
</initial-values>
</item>
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="RadioButton" />
</initial-values>
</item>
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="CheckBox" />
</initial-values>
</item>
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
<initial-values>
<property name="text" value="Label" />
</initial-values>
</item>
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
</item>
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
</item>
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
<preferred-size width="-1" height="20" />
</default-constraints>
</item>
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
</item>
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
</item>
</group>
</component>
</project>

View File

@ -3,6 +3,7 @@ package main.java.com.djrapitops.plan.data.analysis;
import com.djrapitops.plugin.utilities.Verify;
import main.java.com.djrapitops.plan.data.KillData;
import main.java.com.djrapitops.plan.utilities.MiscUtils;
import main.java.com.djrapitops.plan.utilities.analysis.MathUtils;
import java.util.HashMap;
import java.util.List;
@ -16,14 +17,14 @@ import java.util.UUID;
* <p>
* Placeholder values can be retrieved using the get method.
* <p>
* Contains following place-holders: deaths, mobkills, playerkilss
* Contains following place-holders: deaths, mobkills, playerkills, avgdeaths, avgmobkills, avgplayerkills
*
* @author Rsl1122
* @since 3.5.2
*/
public class KillPart extends RawData<KillPart> {
private final PlayerCountPart playerCount; // TODO Averages
private final PlayerCountPart playerCount;
private final Map<UUID, List<KillData>> playerKills;
private long mobKills;
private long deaths;
@ -39,7 +40,12 @@ public class KillPart extends RawData<KillPart> {
public void analyse() {
addValue("deaths", deaths);
addValue("mobkills", mobKills);
addValue("playerkills", getAllPlayerKills().size());
int playerKillAmount = getAllPlayerKills().size();
addValue("playerkills", playerKillAmount);
int playerCount = this.playerCount.getPlayerCount();
addValue("avgdeaths", MathUtils.averageLong(deaths, playerCount));
addValue("avgmobkills", MathUtils.averageLong(mobKills, playerCount));
addValue("avgplayerkills", MathUtils.averageLong(playerKillAmount, playerCount));
}
public void addKills(UUID uuid, List<KillData> kills) throws IllegalArgumentException {

View File

@ -220,8 +220,6 @@ public class DataCacheHandler extends SessionCache {
/**
* Saves all UserData in the cache to Database.
* <p>
* ATTENTION: TODO - Doesn't save the Locations in the locationCache.
* <p>
* Should only be called from Async thread
*/
public void saveCachedUserData() {

View File

@ -78,8 +78,27 @@ public abstract class Database {
*/
protected VersionTable versionTable;
/**
* Table representing plan_security in the database.
*
* @since 3.5.2
*/
protected SecurityTable securityTable;
/**
* Table representing plan_worlds in the database.
*
* @since 3.6.0
*/
protected WorldTable worldTable;
/**
* Table representing plan_world_times in the database.
*
* @since 3.6.0
*/
protected WorldTimesTable worldTimesTable;
/**
* Super constructor.
*
@ -343,7 +362,30 @@ public abstract class Database {
return tpsTable;
}
/**
* Used to get the security table.
*
* @return Table representing plan_security
*/
public SecurityTable getSecurityTable() {
return securityTable;
}
/**
* Used to get the worlds table.
*
* @return Table representing plan_worlds
*/
public WorldTable getWorldTable() {
return worldTable;
}
/**
* Used to get the world times table.
*
* @return Table representing plan_world_times
*/
public WorldTimesTable getWorldTimesTable() {
return worldTimesTable;
}
}

View File

@ -48,6 +48,8 @@ public abstract class SQLDB extends Database {
versionTable = new VersionTable(this, usingMySQL);
tpsTable = new TPSTable(this, usingMySQL);
securityTable = new SecurityTable(this, usingMySQL);
worldTable = new WorldTable(this, usingMySQL);
worldTimesTable = new WorldTimesTable(this, usingMySQL);
startConnectionPingTask();
}
@ -118,7 +120,7 @@ public abstract class SQLDB extends Database {
}
if (newDatabase) {
Log.info("New Database created.");
setVersion(6);
setVersion(7);
}
Benchmark.start("Database: Create tables");
for (Table table : getAllTables()) {
@ -132,8 +134,8 @@ public abstract class SQLDB extends Database {
return false;
}
Benchmark.stop("Database: Create tables");
if (!newDatabase && getVersion() < 6) {
setVersion(6);
if (!newDatabase && getVersion() < 7) {
setVersion(7);
}
}
return true;
@ -177,14 +179,22 @@ public abstract class SQLDB extends Database {
* @return
*/
public Table[] getAllTables() {
return new Table[]{usersTable, gmTimesTable, ipsTable, nicknamesTable, sessionsTable, killsTable, commandUseTable, tpsTable};
return new Table[]{
usersTable, gmTimesTable, ipsTable,
nicknamesTable, sessionsTable, killsTable,
commandUseTable, tpsTable, worldTable,
worldTimesTable};
}
/**
* @return
*/
public Table[] getAllTablesInRemoveOrder() {
return new Table[]{locationsTable, gmTimesTable, ipsTable, nicknamesTable, sessionsTable, killsTable, usersTable, commandUseTable, tpsTable};
return new Table[]{
locationsTable, gmTimesTable, ipsTable,
nicknamesTable, sessionsTable, killsTable,
worldTimesTable, worldTable, usersTable,
commandUseTable, tpsTable};
}
/**

View File

@ -26,6 +26,9 @@ public class TPSTable extends Table {
private final String columnTPS;
private final String columnPlayers;
private final String columnCPUUsage;
private final String columnRAMUsage;
private final String columnEntities;
private final String columnChunksLoaded;
/**
* @param db
@ -37,7 +40,9 @@ public class TPSTable extends Table {
columnTPS = "tps";
columnPlayers = "players_online";
columnCPUUsage = "cpu_usage";
//TODO add new columns
columnRAMUsage = "ram_usage";
columnEntities = "entities";
columnChunksLoaded = "chunks_loaded";
}
@Override
@ -47,14 +52,18 @@ public class TPSTable extends Table {
+ columnDate + " bigint NOT NULL, "
+ columnTPS + " double NOT NULL, "
+ columnPlayers + " integer NOT NULL, "
+ columnCPUUsage + " double NOT NULL"
+ columnCPUUsage + " double NOT NULL, "
+ columnEntities + " integer NOT NULL, "
+ columnChunksLoaded + " integer NOT NULL"
+ ")"
//TODO add new columns
);
int version = getVersion();
if (version < 6) {
alterTablesV6();
}
if (version < 7) {
alterTablesV7();
}
return true;
} catch (SQLException ex) {
Log.toLog(this.getClass().getName(), ex);
@ -70,11 +79,26 @@ public class TPSTable extends Table {
execute("ALTER TABLE " + tableName + " ADD COLUMN " + columnCPUUsage + " double NOT NULL DEFAULT 0");
}
} catch (SQLException e) {
}
}
//TODO alterTablesV7
private void alterTablesV7() {
String[] sql;
if (usingMySQL) {
sql = new String[]{
"ALTER TABLE " + tableName + " ADD " + columnRAMUsage + " bigint NOT NULL DEFAULT 0",
"ALTER TABLE " + tableName + " ADD " + columnEntities + " integer NOT NULL DEFAULT 0",
"ALTER TABLE " + tableName + " ADD " + columnChunksLoaded + " integer NOT NULL DEFAULT 0"
};
} else {
sql = new String[]{
"ALTER TABLE " + tableName + " ADD COLUMN " + columnRAMUsage + " bigint NOT NULL DEFAULT 0",
"ALTER TABLE " + tableName + " ADD COLUMN " + columnEntities + " integer NOT NULL DEFAULT 0",
"ALTER TABLE " + tableName + " ADD COLUMN " + columnChunksLoaded + " integer NOT NULL DEFAULT 0"
};
}
executeUnsafe(sql);
}
/**
* @return @throws SQLException
@ -92,8 +116,10 @@ public class TPSTable extends Table {
double tps = set.getDouble(columnTPS);
int players = set.getInt(columnPlayers);
double cpuUsage = set.getDouble(columnCPUUsage);
//TODO add new data
data.add(new TPS(date, tps, players, cpuUsage, 0, 0, 0));
long ramUsage = set.getLong(columnRAMUsage);
int entities = set.getInt(columnEntities);
int chunksLoaded = set.getInt(columnChunksLoaded);
data.add(new TPS(date, tps, players, cpuUsage, ramUsage, entities, chunksLoaded));
}
return data;
} finally {
@ -121,8 +147,11 @@ public class TPSTable extends Table {
+ columnDate + ", "
+ columnTPS + ", "
+ columnPlayers + ", "
+ columnCPUUsage
+ ") VALUES (?, ?, ?, ?)");
+ columnCPUUsage + ", "
+ columnRAMUsage + ", "
+ columnEntities + ", "
+ columnChunksLoaded
+ ") VALUES (?, ?, ?, ?, ?, ?, ?)");
boolean commitRequired = false;
int i = 0;
@ -131,6 +160,9 @@ public class TPSTable extends Table {
statement.setDouble(2, tps.getTps());
statement.setInt(3, tps.getPlayers());
statement.setDouble(4, tps.getCPUUsage());
statement.setLong(5, tps.getUsedMemory());
statement.setDouble(6, tps.getEntityCount());
statement.setDouble(7, tps.getChunksLoaded());
statement.addBatch();
commitRequired = true;
i++;

View File

@ -1,5 +1,6 @@
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.Container;
import main.java.com.djrapitops.plan.database.DBUtils;
@ -67,13 +68,28 @@ public abstract class Table {
}
/**
* @param sql
* @param statement
* @return
* @throws SQLException
*/
protected boolean execute(String sql) throws SQLException {
protected boolean execute(String statement) throws SQLException {
Connection connection = getConnection();
return connection.createStatement().execute(sql);
return connection.createStatement().execute(statement);
}
/**
* Used to execute queries while possible SQLExceptions are suppressed.
*
* @param statements SQL statements to execute
*/
protected void executeUnsafe(String... statements) {
Verify.nullCheck(statements);
for (String statement : statements) {
try {
execute(statement);
} catch (SQLException e) {
}
}
}
/**

View File

@ -123,9 +123,9 @@ public class UsersTable extends Table {
}
private void alterTablesV4() {
String[] queries;
String[] statements;
if (usingMySQL) {
queries = new String[]{
statements = new String[]{
"ALTER TABLE " + tableName + " ADD " + columnContainsBukkitData + " boolean NOT NULL DEFAULT 0",
"ALTER TABLE " + tableName + " ADD " + columnOP + " boolean NOT NULL DEFAULT 0",
"ALTER TABLE " + tableName + " ADD " + columnBanned + " boolean NOT NULL DEFAULT 0",
@ -133,7 +133,7 @@ public class UsersTable extends Table {
"ALTER TABLE " + tableName + " ADD " + columnRegistered + " bigint NOT NULL DEFAULT 0"
};
} else {
queries = new String[]{
statements = new String[]{
"ALTER TABLE " + tableName + " ADD COLUMN " + columnContainsBukkitData + " boolean NOT NULL DEFAULT 0",
"ALTER TABLE " + tableName + " ADD COLUMN " + columnOP + " boolean NOT NULL DEFAULT 0",
"ALTER TABLE " + tableName + " ADD COLUMN " + columnBanned + " boolean NOT NULL DEFAULT 0",
@ -141,34 +141,24 @@ public class UsersTable extends Table {
"ALTER TABLE " + tableName + " ADD COLUMN " + columnRegistered + " bigint NOT NULL DEFAULT 0"
};
}
for (String query : queries) {
try {
execute(query);
} catch (Exception e) {
}
}
executeUnsafe(statements);
}
private void alterTablesV3() {
String[] queries;
String[] statements;
if (usingMySQL) {
queries = new String[]{
statements = new String[]{
"ALTER TABLE " + tableName + " ADD " + columnDeaths + " integer NOT NULL DEFAULT 0",
"ALTER TABLE " + tableName + " ADD " + columnMobKills + " integer NOT NULL DEFAULT 0",
"ALTER TABLE " + tableName + " DROP INDEX " + columnPlayerKills
};
} else {
queries = new String[]{
statements = new String[]{
"ALTER TABLE " + tableName + " ADD COLUMN " + columnDeaths + " integer NOT NULL DEFAULT 0",
"ALTER TABLE " + tableName + " ADD COLUMN " + columnMobKills + " integer NOT NULL DEFAULT 0"
};
}
for (String query : queries) {
try {
execute(query);
} catch (Exception e) {
}
}
executeUnsafe(statements);
}
/**
@ -870,12 +860,4 @@ public class UsersTable extends Table {
close(statement);
}
}
/**
* @param uuids
* @return
*/
public Map<Integer, Long> getLoginTimes(Collection<UUID> uuids) {
throw new UnsupportedOperationException("Not supported yet."); // TODO
}
}

View File

@ -0,0 +1,116 @@
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 java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
* Table class representing database table plan_worlds.
* <p>
* Used for storing id references to world names.
*
* @author Rsl1122
* @since 3.6.0 / Database version 7
*/
public class WorldTable extends Table {
private final String columnWorldId;
private final String columnWorldName;
/**
* Constructor.
*
* @param db Database this table is a part of.
* @param usingMySQL Database is a MySQL database.
*/
public WorldTable(SQLDB db, boolean usingMySQL) {
super("plan_worlds", db, usingMySQL);
columnWorldId = "world_id";
columnWorldName = "world_name";
}
@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 + ")" : "")
+ ")"
);
return true;
} catch (SQLException ex) {
Log.toLog(this.getClass().getName(), ex);
return false;
}
}
/**
* Used to get the available world names.
*
* @return List of all world names in the database.
* @throws SQLException Database error occurs.
*/
public List<String> getWorlds() throws SQLException {
PreparedStatement statement = null;
ResultSet set = null;
try {
statement = prepareStatement("SELECT * FROM " + tableName);
set = statement.executeQuery();
List<String> worldNames = new ArrayList<>();
while (set.next()) {
String worldName = set.getString(columnWorldName);
worldNames.add(worldName);
}
return worldNames;
} finally {
close(set, statement);
}
}
/**
* Used to save a list of world names.
* <p>
* Already saved names will not be saved.
*
* @param worlds List of world names.
* @throws SQLException Database error occurs.
*/
public void saveWorlds(List<String> worlds) throws SQLException {
Verify.nullCheck(worlds);
List<String> saved = getWorlds();
worlds.removeAll(saved);
if (Verify.isEmpty(worlds)) {
return;
}
PreparedStatement statement = null;
try {
statement = prepareStatement("INSERT INTO " + tableName + " ("
+ columnWorldName
+ ") VALUES (?)");
boolean commitRequired = false;
for (String world : worlds) {
statement.setString(1, world);
statement.addBatch();
commitRequired = true;
}
if (commitRequired) {
statement.executeBatch();
}
} finally {
close(statement);
}
}
public String getColumnID() {
return columnWorldId;
}
}

View File

@ -0,0 +1,54 @@
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 java.sql.SQLException;
/**
* Table class representing database table plan_world_times.
*
* @author Rsl1122
* @since 3.6.0 / Database version 7
*/
public class WorldTimesTable extends Table {
private final WorldTable worldTable;
private final String columnWorldId;
private final String columnUserId;
private final String columnPlaytime;
/**
* Constructor.
*
* @param db Database this table is a part of.
* @param usingMySQL Database is a MySQL database.
*/
public WorldTimesTable(SQLDB db, boolean usingMySQL) {
super("plan_world_times", db, usingMySQL);
worldTable = db.getWorldTable();
columnWorldId = "world_id";
columnUserId = "user_id";
columnPlaytime = "playtime";
}
@Override
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() + ")"
+ ")"
);
return true;
} catch (SQLException ex) {
Log.toLog(this.getClass().getName(), ex);
return false;
}
}
}

View File

@ -0,0 +1,4 @@
<changelist name="Fix__198" date="1501136598251" recycled="false">
<option name="PATH" value="$PROJECT_DIR$/.idea/shelf/Fix__198/shelved.patch" />
<option name="DESCRIPTION" value="Fix #198" />
</changelist>