Skip to content

Commit

Permalink
Merge pull request #4 from kaktus-1/orm-implementation
Browse files Browse the repository at this point in the history
Implemented ORMLite
  • Loading branch information
NightDev701 committed Oct 11, 2023
2 parents 2bfd299 + 095a4cd commit 9322324
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 70 deletions.
13 changes: 12 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,22 @@ compileJava {
group 'pl.nightdev701'

repositories {
maven {
url 'https://jitpack.io'
}

mavenCentral()
}

dependencies {
implementation files("API/mysql-connector-j-8.1.0.jar")
// https://mvnrepository.com/artifact/com.mysql/mysql-connector-j
implementation 'com.mysql:mysql-connector-j:8.1.0'

// https://mvnrepository.com/artifact/org.postgresql/postgresql
implementation 'org.postgresql:postgresql:42.6.0'

// https://mvnrepository.com/artifact/com.j256.ormlite/ormlite-jdbc
implementation 'com.j256.ormlite:ormlite-jdbc:6.1'
}

java {
Expand Down
139 changes: 72 additions & 67 deletions src/main/java/pl/nightdev701/database/DatabaseConnector.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,42 +10,71 @@
*/

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.dao.DaoManager;
import com.j256.ormlite.jdbc.JdbcPooledConnectionSource;
import com.j256.ormlite.table.TableUtils;
import pl.nightdev701.database.formular.DatabaseFormular;
import pl.nightdev701.database.type.DatabaseType;
import pl.nightdev701.logger.AbstractLogger;
import pl.nightdev701.logger.standard.DefaultLogger;

import java.sql.*;
import java.sql.SQLException;
import java.util.logging.Level;

public class DatabaseConnector {

private static int port;
private final String ip;
private final String user;
private final String password;
private final String database;
private final DatabaseType databaseType;
private final AbstractLogger logger;
private Connection connection;
private final String connectionURL;
private JdbcPooledConnectionSource connectionSource;

/**
* Creates a new instance of the DatabaseConnector
*
* @param formular
* @param databaseType
*/
public DatabaseConnector(DatabaseFormular formular, DatabaseType databaseType) {
this.ip = formular.ip();
this.user = formular.user();
this.password = formular.password();
this.database = formular.database();
this.databaseType = databaseType;

this.logger = new DefaultLogger();
this(formular, databaseType, new DefaultLogger());
}

if (databaseType == DatabaseType.MYSQL) {
setPort(3306);
} else if (databaseType == DatabaseType.POSTGRESQL) {
setPort(5432);
}
/**
* Creates a new instance of the DatabaseConnector with a custom port
*
* @param formular
* @param databaseType
* @param port
*/
public DatabaseConnector(DatabaseFormular formular, DatabaseType databaseType, int port) {
this(formular, databaseType, port, new DefaultLogger());
}


/**
* Creates a new instance of the DatabaseConnector with a custom logger
*
* @param formular
* @param databaseType
* @param logger
*/
public DatabaseConnector(DatabaseFormular formular, DatabaseType databaseType, AbstractLogger logger) {
this(formular, databaseType, databaseType.getDefaultPort(), logger);
}

/**
* Creates a new instance of the DatabaseConnector with a custom port and logger
*
* @param formular
* @param databaseType
* @param port
* @param logger
*/
public DatabaseConnector(DatabaseFormular formular, DatabaseType databaseType, int port, AbstractLogger logger) {
this.ip = formular.ip();
this.user = formular.user();
this.password = formular.password();
Expand All @@ -55,81 +84,57 @@ public DatabaseConnector(DatabaseFormular formular, DatabaseType databaseType, A
this.logger = logger;

if (databaseType == DatabaseType.MYSQL) {
setPort(3306);
} else if (databaseType == DatabaseType.POSTGRESQL) {
setPort(5432);
}
}

/**
* connect to database
*/
public void connect() throws SQLException {
logger.log(Level.INFO, "Connect to database, with the type \"" + databaseType.name() + "\"!");
if (databaseType == DatabaseType.MYSQL) {
connection = DriverManager.getConnection("jdbc:mysql://" +
connectionURL = "jdbc:mysql://" +
ip + ":" + port + "/" + database + "?autoReconnect=true" +
"&characterEncoding=utf8&useUnicode=true" +
"&sessionVariables=storage_engine%3DInnoDB" +
"&interactiveClient=true&dontTrackOpenResources=true", user, password);
"&interactiveClient=true&dontTrackOpenResources=true";
} else if (databaseType == DatabaseType.POSTGRESQL) {
connection = DriverManager.getConnection("jdbc:postgresql://" + ip + ":" + port + "/" + database +
connectionURL = "jdbc:postgresql://" + ip + ":" + port + "/" + database +
"?reWriteBatchedInserts=true" +
"&charSet=utf-8", user, password);
"&charSet=utf-8";
} else {
connectionURL = null;
}
logger.log(Level.INFO, "Connected to database!");
}

/**
* close connection
* initialize connection
*/
public void close() throws SQLException {
if (isConnected()) {
connection.close();
logger.log(Level.INFO, "Connection closed");
}
}
public void initialize() throws SQLException {
logger.log(Level.INFO, "Initializing database connection, with the type \"" + databaseType.name() + "\"!");

/**
* read value from database
*/
public Object getDatabaseStatement(String command, String data) throws SQLException {
PreparedStatement statement = connection.prepareStatement(command);
ResultSet result = statement.executeQuery();
result.next();
return result.getObject(data);
connectionSource = new JdbcPooledConnectionSource(connectionURL);
connectionSource.setUsername(user);
connectionSource.setPassword(password);
}

/**
* execute action to database
*/
public void executeDatabaseStatement(String command) throws SQLException {
PreparedStatement statement = connection.prepareStatement(command);
statement.executeUpdate();
}

/**
* check if connected
* close connection
*/
public boolean isConnected() throws SQLException {
if (connection == null) {
return false;
}
return !connection.isClosed();
public void close() throws Exception {
connectionSource.close();
logger.log(Level.INFO, "Connection closed");
}

/**
* get port
* Creates a table for a DatabaseTable class if no table already exists with the same name
*
* @param dataClass your DatabaseTable class
*/
public int getPort() {
return port;
public <T> int createTableIfNotExists(Class<T> dataClass) throws SQLException {
return TableUtils.createTableIfNotExists(connectionSource, dataClass);
}

/**
* set port
* Creates a new Dao with which you can work
*
* @param clazz your DatabaseTable class
* @return new Dao
* @throws SQLException
*/
public void setPort(int port) {
this.port = port;
public <D extends Dao<T, ?>, T> D createDao(Class<T> clazz) throws SQLException {
return DaoManager.createDao(connectionSource, clazz);
}

}
18 changes: 16 additions & 2 deletions src/main/java/pl/nightdev701/database/type/DatabaseType.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,21 @@
/* database types */
public enum DatabaseType {

MYSQL,
POSTGRESQL,
MYSQL(3306),
POSTGRESQL(5432),
;

private final int defaultPort;

DatabaseType(int defaultPort) {
this.defaultPort = defaultPort;
}

/**
* Gets the default port for a database type
* @return defaultPort
*/
public int getDefaultPort() {
return defaultPort;
}
}

0 comments on commit 9322324

Please sign in to comment.