Skip to content

Commit

Permalink
- Deprecated classes removal
Browse files Browse the repository at this point in the history
- Code restructuring
- Bug fix
  • Loading branch information
Areeb-Gillani committed Sep 28, 2024
1 parent 1ead8e2 commit de90384
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ private static <T extends AbstractConnection> Object createDatabaseConnection(St
}

public static <T extends AbstractConnection> T getDBConnection(String connectionName, JsonObject config) {
Object con = dbConnectionMap.get(connectionName);
if (con == null)
con = dbConnectionMap.put(connectionName, createDatabaseConnection(connectionName, config));
return (T) con;
return (T) dbConnectionMap.computeIfAbsent(connectionName, key->createDatabaseConnection(key, config));
}
}
21 changes: 6 additions & 15 deletions src/main/java/io/github/areebgillani/db/mssql/MSSQLConnection.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,23 @@
package io.github.areebgillani.db.mssql;

import io.github.areebgillani.db.utils.AbstractConnection;
import io.github.areebgillani.db.utils.AbstractSQLConnection;
import io.github.areebgillani.db.utils.DatabaseConfig;
import io.vertx.core.json.JsonObject;
import io.vertx.mssqlclient.MSSQLConnectOptions;
import io.vertx.mysqlclient.MySQLConnectOptions;
import io.vertx.mysqlclient.MySQLPool;
import io.vertx.sqlclient.PoolOptions;

public class MSSQLConnection extends AbstractConnection<MySQLConnectOptions, MySQLPool> {
public class MSSQLConnection extends AbstractSQLConnection<MSSQLConnectOptions> {
private static MSSQLConnection instance;
private DatabaseConfig config;
public MSSQLConnection(JsonObject config) {
this.config = DatabaseConfig.getInstance(config);
super(config);
this.connectionOptions = getConnectionOption();
this.poolOptions = getPoolOptions();
this.client = getSQLPool();
}

protected MySQLPool getSQLPool() {
return this.client == null ? MySQLPool.pool(vertx, connectionOptions, poolOptions) : this.client;
}

protected PoolOptions getPoolOptions() {
return this.poolOptions == null ? new PoolOptions().setMaxSize(config.DB_POOL_SIZE) : poolOptions;
}

protected MySQLConnectOptions getConnectionOption() {
return this.connectionOptions == null ? new MySQLConnectOptions()
protected MSSQLConnectOptions getConnectionOption() {
return this.connectionOptions == null ? new MSSQLConnectOptions()
.setPort(config.DB_PORT)
.setHost(config.DB_HOST)
.setDatabase(config.DB_NAME)
Expand Down
17 changes: 4 additions & 13 deletions src/main/java/io/github/areebgillani/db/mysql/MySQLConnection.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,19 @@
package io.github.areebgillani.db.mysql;

import io.github.areebgillani.db.utils.AbstractConnection;
import io.github.areebgillani.db.utils.AbstractSQLConnection;
import io.github.areebgillani.db.utils.DatabaseConfig;
import io.vertx.core.json.JsonObject;
import io.vertx.mysqlclient.MySQLConnectOptions;
import io.vertx.mysqlclient.MySQLPool;
import io.vertx.sqlclient.Pool;
import io.vertx.sqlclient.PoolOptions;

public class MySQLConnection extends AbstractConnection<MySQLConnectOptions, MySQLPool> {
public class MySQLConnection extends AbstractSQLConnection<MySQLConnectOptions> {
private static MySQLConnection instance;
private DatabaseConfig config;
public MySQLConnection(JsonObject config) {
this.config = DatabaseConfig.getInstance(config);
super(config);
this.connectionOptions = getConnectionOption();
this.poolOptions = getPoolOptions();
this.client = getSQLPool();
}

protected MySQLPool getSQLPool() {
return this.client == null ? MySQLPool.pool(vertx, connectionOptions, poolOptions) : this.client;
}

protected PoolOptions getPoolOptions() {
return this.poolOptions == null ? new PoolOptions().setMaxSize(config.DB_POOL_SIZE) : poolOptions;
}

protected MySQLConnectOptions getConnectionOption() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,19 @@
package io.github.areebgillani.db.oracle;

import io.github.areebgillani.db.utils.AbstractConnection;
import io.github.areebgillani.db.utils.AbstractSQLConnection;
import io.github.areebgillani.db.utils.DatabaseConfig;
import io.vertx.core.json.JsonObject;
import io.vertx.oracleclient.OracleConnectOptions;
import io.vertx.oracleclient.OraclePool;
import io.vertx.sqlclient.PoolOptions;

public class OracleConnection extends AbstractConnection<OracleConnectOptions, OraclePool> {
public class OracleConnection extends AbstractSQLConnection<OracleConnectOptions> {
private static OracleConnection instance;
private DatabaseConfig config;
public OracleConnection(JsonObject config) {
this.config = DatabaseConfig.getInstance(config);
super(config);
this.connectionOptions = getConnectionOption();
this.poolOptions = getPoolOptions();
this.client = getSQLPool();
}

protected OraclePool getSQLPool() {
return this.client == null ? OraclePool.pool(vertx, connectionOptions, poolOptions) : this.client;
}

protected PoolOptions getPoolOptions() {
return this.poolOptions == null ? new PoolOptions().setMaxSize(config.DB_POOL_SIZE) : poolOptions;
}

protected OracleConnectOptions getConnectionOption() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,19 @@
package io.github.areebgillani.db.postgres;

import io.github.areebgillani.db.utils.AbstractConnection;
import io.github.areebgillani.db.utils.AbstractSQLConnection;
import io.github.areebgillani.db.utils.DatabaseConfig;
import io.vertx.core.json.JsonObject;
import io.vertx.pgclient.PgConnectOptions;
import io.vertx.pgclient.PgPool;
import io.vertx.sqlclient.PoolOptions;

public class PostgresConnection extends AbstractConnection<PgConnectOptions, PgPool> {
public class PostgresConnection extends AbstractSQLConnection<PgConnectOptions> {
private static PostgresConnection instance;
private DatabaseConfig config;
public PostgresConnection(JsonObject config) {
this.config = DatabaseConfig.getInstance(config);
super(config);
this.connectionOptions = getConnectionOption();
this.poolOptions = getPoolOptions();
this.client = getSQLPool();
}

protected PgPool getSQLPool() {
return this.client == null ? PgPool.pool(vertx, connectionOptions, poolOptions) : this.client;
}

protected PoolOptions getPoolOptions() {
return this.poolOptions == null ? new PoolOptions().setMaxSize(config.DB_POOL_SIZE) : poolOptions;
}

protected PgConnectOptions getConnectionOption() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,5 @@ public AbstractConnection(){

protected abstract C getSQLPool();

protected abstract PoolOptions getPoolOptions();

protected abstract T getConnectionOption();
protected abstract PoolOptions getPoolOptions(int poolSize);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.github.areebgillani.db.utils;

import io.vertx.core.json.JsonObject;
import io.vertx.sqlclient.Pool;
import io.vertx.sqlclient.PoolOptions;
import io.vertx.sqlclient.SqlConnectOptions;

public abstract class AbstractSQLConnection <T extends SqlConnectOptions> extends AbstractConnection <T, Pool>{
protected DatabaseConfig config;
public AbstractSQLConnection(JsonObject config) {
this.config = DatabaseConfig.getInstance(config);
this.poolOptions = getPoolOptions(this.config.DB_POOL_SIZE);
this.client = getSQLPool();
}

@Override
protected Pool getSQLPool() {
return this.client == null ? Pool.pool(vertx, connectionOptions, poolOptions) : this.client;
}
@Override
protected PoolOptions getPoolOptions(int poolSize) {
return this.poolOptions == null ? new PoolOptions().setMaxSize(poolSize) : poolOptions;
}

protected abstract T getConnectionOption();
}

0 comments on commit de90384

Please sign in to comment.