Search in sources :

Example 1 with Table

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;
}
Also used : Table(me.staartvin.statz.database.datatype.Table) SQLiteTable(me.staartvin.statz.database.datatype.sqlite.SQLiteTable) Column(me.staartvin.statz.database.datatype.Column) ArrayList(java.util.ArrayList)

Example 2 with Table

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;
}
Also used : Table(me.staartvin.statz.database.datatype.Table) Query(me.staartvin.statz.database.datatype.Query) PlayerInfo(me.staartvin.statz.datamanager.player.PlayerInfo) RowRequirement(me.staartvin.statz.database.datatype.RowRequirement)

Example 3 with Table

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;
}
Also used : Table(me.staartvin.statz.database.datatype.Table) MySQLTable(me.staartvin.statz.database.datatype.mysql.MySQLTable) Column(me.staartvin.statz.database.datatype.Column) ArrayList(java.util.ArrayList)

Aggregations

Table (me.staartvin.statz.database.datatype.Table)3 ArrayList (java.util.ArrayList)2 Column (me.staartvin.statz.database.datatype.Column)2 Query (me.staartvin.statz.database.datatype.Query)1 RowRequirement (me.staartvin.statz.database.datatype.RowRequirement)1 MySQLTable (me.staartvin.statz.database.datatype.mysql.MySQLTable)1 SQLiteTable (me.staartvin.statz.database.datatype.sqlite.SQLiteTable)1 PlayerInfo (me.staartvin.statz.datamanager.player.PlayerInfo)1