use of me.staartvin.statz.database.datatype.Table in project Statz by Staartvin.
the class SQLiteConnector method createTablesStatement.
/**
* This function creates multiple strings in 'SQL style' to create the
* proper tables.
* <br>
* It looks at the tables that are loaded in memory and dynamically creates
* proper SQL statements.
*
* @return SQL statements that will create the necessary tables when run.
*/
public List<String> createTablesStatement() {
// Returns a list of statements that need to be run to create the tables.
final List<String> statements = new ArrayList<String>();
for (final Table table : this.getTables()) {
StringBuilder statement = new StringBuilder("CREATE TABLE IF NOT EXISTS " + table.getTableName() + " (");
// For each column in the table, add it to the table.
for (final Column column : table.getColumns()) {
if (column.getDataType().equals(SQLDataType.INT)) {
statement.append("'" + column.getColumnName() + "' INTEGER");
} else {
statement.append("'" + column.getColumnName() + "' " + column.getDataType().toString());
}
if (column.isPrimaryKey()) {
statement.append(" PRIMARY KEY");
}
if (column.isAutoIncrement()) {
statement.append(" AUTOINCREMENT");
}
if (column.isNotNull()) {
statement.append(" NOT NULL");
}
if (column.isUnique()) {
statement.append(" UNIQUE");
}
statement.append(",");
}
if (!table.getUniqueMatched().isEmpty()) {
statement.append("UNIQUE (");
for (Column matched : table.getUniqueMatched()) {
statement.append(matched.getColumnName() + ",");
}
// Remove last comma
statement = new StringBuilder(statement.substring(0, statement.lastIndexOf(",")) + ")");
} else {
statement = new StringBuilder(statement.substring(0, statement.lastIndexOf(",")));
}
statement.append(");");
statements.add(statement.toString());
plugin.debugMessage(ChatColor.BLUE + "Loaded table '" + table.getTableName() + "'");
}
return statements;
}
use of me.staartvin.statz.database.datatype.Table in project Statz by Staartvin.
the class DataManager method getFreshPlayerInfo.
/**
* Get data of a player for a given statistic. This method will obtain 'fresh' data from the database, meaning
* that it will ignore cached data. Hence, this method will block the thread it is ran on. It is therefore
* advised to run this method asynchronously.
* <br>
* <br>
* It is recommended to use another method to obtain the data of a player when it is not loaded into the cache
* yet: using {@link #loadPlayerData(UUID, PlayerStat)}, the retrieved data will also be stored in the cache, so
* you can retrieve it the next time without making an expensive call to the database.
*
* @param uuid UUID of the player.
* @param statType Type of statistic.
* @return fresh player data in the form of a {@link PlayerInfo} object.
* @throws IllegalArgumentException if the given uuid is null.
*/
public PlayerInfo getFreshPlayerInfo(UUID uuid, PlayerStat statType) throws IllegalArgumentException {
if (uuid == null) {
throw new IllegalArgumentException("UUID cannot be null.");
}
Table table = DatabaseConnector.getTable(statType);
List<Query> databaseRows = plugin.getDatabaseConnector().getObjects(table, new RowRequirement("uuid", uuid.toString()));
PlayerInfo info = new PlayerInfo(uuid);
info.setData(statType, databaseRows);
return info;
}
use of me.staartvin.statz.database.datatype.Table in project Statz by Staartvin.
the class MySQLConnector method createTablesStatement.
/**
* This function creates multiple strings in 'SQL style' to create the
* proper tables. <br>
* It looks at the tables that are loaded in memory and dynamically creates
* proper SQL statements.
*
* @return SQL statements that will create the necessary tables when run.
*/
public List<String> createTablesStatement() {
// Returns a list of statements that need to be run to create the
// tables.
final List<String> statements = new ArrayList<String>();
for (final Table table : this.getTables()) {
StringBuilder statement = new StringBuilder("CREATE TABLE IF NOT EXISTS " + table.getTableName() + " (");
// For each column in the table, add it to the table.
for (final Column column : table.getColumns()) {
if (column.getDataType().equals(SQLDataType.INT)) {
statement.append("" + column.getColumnName() + " BIGINT");
} else if (column.getDataType().equals(SQLDataType.TEXT)) {
statement.append("" + column.getColumnName() + " VARCHAR(100)");
} else if (column.getDataType().equals(SQLDataType.DOUBLE)) {
statement.append("" + column.getColumnName() + " DECIMAL(20,10)");
} else {
statement.append("" + column.getColumnName() + " " + column.getDataType().toString());
}
if (column.isPrimaryKey()) {
statement.append(" PRIMARY KEY");
}
if (column.isAutoIncrement()) {
statement.append(" AUTO_INCREMENT");
}
if (column.isNotNull()) {
statement.append(" NOT NULL");
}
if (column.isUnique()) {
statement.append(" UNIQUE");
}
statement.append(",");
}
if (!table.getUniqueMatched().isEmpty()) {
statement.append("UNIQUE (");
for (Column matched : table.getUniqueMatched()) {
statement.append(matched.getColumnName() + ",");
}
// Remove last comma
statement = new StringBuilder(statement.substring(0, statement.lastIndexOf(",")) + ")");
} else {
statement = new StringBuilder(statement.substring(0, statement.lastIndexOf(",")));
}
statement.append(");");
statements.add(statement.toString());
plugin.debugMessage(ChatColor.BLUE + "Loaded table '" + table.getTableName() + "'");
}
return statements;
}
Aggregations