Skip to content

Commit

Permalink
Connecting to the database has been improved
Browse files Browse the repository at this point in the history
From now SQLite is a default database
  • Loading branch information
timsixth committed Jun 25, 2024
1 parent d0d706f commit d58cfc4
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 34 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# Project exclude paths
/out/
/target/
/T-DataBasesAPI.iml
/T-DataBasesAPI.iml
/database.db
/.idea/
/javadocs/
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>pl.timsixth</groupId>
<artifactId>T-DataBasesAPI</artifactId>
<version>1.9.2</version>
<version>1.10.0</version>

<repositories>
<repository>
Expand Down
45 changes: 15 additions & 30 deletions src/main/java/pl/timsixth/databasesapi/DatabasesApiPlugin.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package pl.timsixth.databasesapi;

import lombok.SneakyThrows;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.plugin.PluginDescriptionFile;
Expand All @@ -9,14 +8,15 @@
import pl.timsixth.databasesapi.api.IDataBasesApi;
import pl.timsixth.databasesapi.config.ConfigFileSpigot;
import pl.timsixth.databasesapi.config.IConfigFile;
import pl.timsixth.databasesapi.database.DatabaseConnector;
import pl.timsixth.databasesapi.database.DatabaseFactory;
import pl.timsixth.databasesapi.database.ISQLDataBase;
import pl.timsixth.databasesapi.database.ISQLite;
import pl.timsixth.databasesapi.database.migration.DataBaseMigrations;
import pl.timsixth.databasesapi.database.migration.Migrations;
import pl.timsixth.databasesapi.database.type.MySQL;
import pl.timsixth.databasesapi.database.type.SQLite;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;

public final class DatabasesApiPlugin extends JavaPlugin {
ISQLDataBase currentSQLDataBase;
Expand All @@ -32,7 +32,7 @@ public DatabasesApiPlugin(JavaPluginLoader loader, PluginDescriptionFile descrip
super(loader, description, dataFolder, file);
}

@SneakyThrows

@Override
public void onEnable() {
configFile = new ConfigFileSpigot(this);
Expand All @@ -43,32 +43,17 @@ public void onEnable() {
getConfig().options().copyDefaults(true);
saveConfig();

switch (configFile.getDataBaseType()) {
case MYSQL:
ISQLDataBase mysql = new MySQL();
mysql.setDataBase(getConfig().getString("database"));
mysql.setHostname(getConfig().getString("hostname"));
mysql.setPassword(getConfig().getString("password"));
mysql.setPort(getConfig().getInt("port"));
mysql.setUsername(getConfig().getString("username"));
mysql.openConnection();
currentSQLDataBase = mysql;
Bukkit.getConsoleSender().sendMessage(ChatColor.GREEN + "Successful connected to MySQL");
break;
case SQLITE:
ISQLite sqlite = new SQLite(this);
sqlite.setDataBase(getConfig().getString("database"));
File database = sqlite.createDataBase(sqlite.getDataBase() + ".db");
sqlite.openConnection(database);
currentSQLDataBase = sqlite;
Bukkit.getConsoleSender().sendMessage(ChatColor.GREEN + "Successful connected to SQLite");
break;
default:
Bukkit.getConsoleSender().sendMessage(ChatColor.RED + "This database doesn't exists");
}
try {
currentSQLDataBase = (ISQLDataBase) DatabaseFactory.createDatabase(configFile.getDataBaseType(), this);
DatabaseConnector.connect(currentSQLDataBase);

dataBaseMigrations.createMigrationsTable();
dataBaseMigrations.checkMigrations();
Bukkit.getConsoleSender().sendMessage(ChatColor.GREEN + "Successful connected to " + configFile.getDataBaseType());

dataBaseMigrations.createMigrationsTable();
dataBaseMigrations.checkMigrations();
} catch (SQLException | IOException | ClassNotFoundException ex) {
Bukkit.getLogger().severe(ex.getMessage());
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package pl.timsixth.databasesapi.database;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;

public final class DatabaseConnector {

/**
* Connects to database
*
* @param dataBase database to connect
* @throws SQLException when can not connect to database
* @throws IOException when can not create file
* @throws ClassNotFoundException when can not find database driver
*/
public static void connect(IDataBase dataBase) throws SQLException, IOException, ClassNotFoundException {
if (dataBase instanceof ISQLite) {
ISQLite sqlLite = (ISQLite) dataBase;

File database = sqlLite.createDataBase(dataBase.getDataBase() + ".db");
sqlLite.openConnection(database);
return;
}

dataBase.openConnection();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package pl.timsixth.databasesapi.database;

import org.bukkit.configuration.file.FileConfiguration;
import pl.timsixth.databasesapi.DatabasesApiPlugin;
import pl.timsixth.databasesapi.database.type.MySQL;
import pl.timsixth.databasesapi.database.type.SQLite;

public final class DatabaseFactory {

/**
* Creates database instance
*
* @param dataBaseType type from config.yml
* @param databasesApiPlugin database api plugin, to get config
* @return created database instance
*/
public static IDataBase createDatabase(DataBaseType dataBaseType, DatabasesApiPlugin databasesApiPlugin) {
FileConfiguration fileConfiguration = databasesApiPlugin.getConfig();

if (dataBaseType == DataBaseType.MYSQL) {
ISQLDataBase mysql = new MySQL();
mysql.setDataBase(fileConfiguration.getString("database"));
mysql.setHostname(fileConfiguration.getString("hostname"));
mysql.setPassword(fileConfiguration.getString("password"));
mysql.setPort(fileConfiguration.getInt("port"));
mysql.setUsername(fileConfiguration.getString("username"));

return mysql;
}

ISQLite sqlite = new SQLite(databasesApiPlugin);
sqlite.setDataBase(fileConfiguration.getString("database"));

return sqlite;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ public ITable createColumn(String columnName, IDataType dataType, boolean nullab
return this;
}

@Override
public ITable createColumn(String columnName, IDataType type) {
return createColumn(columnName, type, false);
}

@Override
public boolean createTable(String name) {
if (columns.size() < 2) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,13 @@ public interface ITable {
* @return ITable
*/
ITable id();

/**
* Creates new column, nullable is set to false be default
*
* @param columnName column which will be set primaryKey
* @param type DataType which will be set to column @{@link IDataType}
* @return ITable
*/
ITable createColumn(String columnName, IDataType type);
}
2 changes: 1 addition & 1 deletion src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ username: 'root'
password: ''
database: 'servertestowy'
port: 3306
type: MYSQL
type: SQLITE
#Avaiable types of databases MYSQL,SQLITE.
#Please type this databases uppercase.
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: T-DatabasesApi
main: pl.timsixth.databasesapi.DatabasesApiPlugin
version: '1.9.2'
version: '1.10.0'
author: timsixth

0 comments on commit d58cfc4

Please sign in to comment.