Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Fixing constructors of database types. #15

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 28 additions & 29 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,49 +10,48 @@ allprojects {
plugins.apply("java")
plugins.apply("maven-publish")

tasks.withType<JavaCompile> {
options.encoding = "UTF-8"

sourceCompatibility = "8"
targetCompatibility = "8"
}

repositories {
mavenCentral()
mavenLocal()
}

configurations.all {
resolutionStrategy.cacheChangingModulesFor(0, TimeUnit.SECONDS)
}

dependencies {
// Lombok
rootProject.libs.lombok.let {
compileOnly(it)
annotationProcessor(it)
testAnnotationProcessor(it)
testCompileOnly(it)
}

// jetbrains
rootProject.libs.jetbrains.let {
compileOnly(it)
testCompileOnly(it)
}
}

publishing {
publications {
create<MavenPublication>("maven") {
create<MavenPublication>("${rootProject.name}-${this@allprojects.name}") {
from(components["java"])

groupId = rootProject.group.toString()
version = rootProject.version.toString()
}
}
}

dependencies {
val lombokVersion = "1.18.22"
val annotationsVersion = "21.0.1"

val hikariVersion = "4.0.3"
val slf4jVersion = "1.7.32"
val sqliteVersion = "3.45.1.0"
val mysqlVersion = "8.0.33"
val mariaDBVersion = "3.3.2";

// Lombok
annotationProcessor("org.projectlombok:lombok:$lombokVersion")
compileOnly("org.projectlombok:lombok:$lombokVersion")

// Jetbrains Annotations
compileOnly("org.jetbrains:annotations-java5:$annotationsVersion")

// HikariCP
implementation("com.zaxxer:HikariCP:$hikariVersion")
implementation("org.slf4j:slf4j-api:$slf4jVersion")
implementation("org.slf4j:slf4j-simple:$slf4jVersion")

// Databases
implementation("org.xerial:sqlite-jdbc:$sqliteVersion")
implementation("mysql:mysql-connector-java:$mysqlVersion")
implementation("org.mariadb.jdbc:mariadb-java-client:$mariaDBVersion")
}
}

tasks.withType<Javadoc> {
Expand Down
11 changes: 10 additions & 1 deletion connector/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
version = rootProject.version
version = rootProject.version

dependencies {
rootProject.libs.let {
implementation(it.hikari)
implementation(it.sqlite)
implementation(it.mysql)
implementation(it.mariadb)
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.jaoow.sql.connector.type;

import com.jaoow.sql.connector.SQLConnector;
import com.jaoow.sql.connector.type.impl.MySQLDatabaseType;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.experimental.SuperBuilder;

import java.sql.SQLException;

@Getter
@SuperBuilder
@RequiredArgsConstructor
public abstract class SQLDatabaseType {

Expand All @@ -15,4 +18,18 @@ public abstract class SQLDatabaseType {

public abstract SQLConnector connect() throws SQLException;

public static abstract class SQLDatabaseTypeBuilder<C extends SQLDatabaseType, B extends SQLDatabaseTypeBuilder<C, B>> {

protected B driverClassName(String driverClassName) {
this.driverClassName = driverClassName;
return self();
}

protected B jdbcUrl(String jdbcUrl) {
this.jdbcUrl = jdbcUrl;
return self();
}

}

}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.jaoow.sql.connector.type.impl;

import org.jetbrains.annotations.NotNull;
import lombok.NonNull;
import lombok.experimental.SuperBuilder;

@SuperBuilder
public class MariaDatabaseType extends MySQLDatabaseType {

public MariaDatabaseType(@NotNull String address, @NotNull String username,
@NotNull String password, @NotNull String database) {

public MariaDatabaseType(@NonNull String address, @NonNull String username, @NonNull String password, @NonNull String database) {
super(address, username, password, database);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import com.jaoow.sql.connector.SQLConnector;
import com.jaoow.sql.connector.type.SQLDatabaseType;
import com.zaxxer.hikari.HikariDataSource;
import lombok.Builder;
import lombok.NonNull;
import lombok.experimental.SuperBuilder;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;

Expand All @@ -13,10 +14,9 @@
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;

@SuperBuilder
public class MySQLDatabaseType extends SQLDatabaseType {

private final HikariDataSource dataSource = new HikariDataSource();

// https://github.com/lucko/helper/blob/master/helper-sql/src/main/java/me/lucko/helper/sql/plugin/HelperSql.java
private static final int MAXIMUM_POOL_SIZE = (Runtime.getRuntime().availableProcessors() * 2) + 1;
private static final int MINIMUM_IDLE = Math.min(MAXIMUM_POOL_SIZE, 10);
Expand All @@ -25,10 +25,65 @@ public class MySQLDatabaseType extends SQLDatabaseType {
private static final long CONNECTION_TIMEOUT = TimeUnit.SECONDS.toMillis(10);
private static final long LEAK_DETECTION_THRESHOLD = TimeUnit.SECONDS.toMillis(10);

@Builder
public MySQLDatabaseType(@NotNull String address, @NotNull String username, @NotNull String password, @NotNull String database) {
super("com.mysql.jdbc.Driver", "jdbc:mysql://%s/%s");
@NonNull private final String address;
@NonNull private final String username;
@NonNull private final String password;
@NonNull private final String database;

private HikariDataSource dataSource;

public MySQLDatabaseType(@NonNull String address, @NonNull String username, @NonNull String password, @NonNull String database) {
super("com.mysql.cj.jdbc.Driver", "jdbc:mysql://%s/%s");

this.address = address;
this.username = username;
this.password = password;
this.database = database;
}

@Override
public String getJdbcUrl() {
return "jdbc:mysql://%s/%s";
}

@Override
public String getDriverClassName() {
try {
return Class.forName("com.mysql.cj.jdbc.Driver").getName();
} catch (ClassNotFoundException exception) {
return "com.mysql.jdbc.Driver";
}
}

@Deprecated
@Contract("_ -> this")
public SQLDatabaseType configureDataSource(@NotNull Consumer<HikariDataSource> consumer) {
consumer.accept(dataSource);
return this;
}

@NotNull
@Override
public SQLConnector connect() throws SQLException {
if (this.dataSource == null) {
this.initDataSource();
}

// Test if connection was established.
dataSource.getConnection().close();

return consumer -> {
try (Connection connection = dataSource.getConnection()) {
consumer.execute(connection);
} catch (SQLException exception) {
throw new ConnectorException(exception);
}
};

}

private void initDataSource() {
dataSource = new HikariDataSource();
dataSource.setJdbcUrl(String.format(this.getJdbcUrl(), address, database));
dataSource.setDriverClassName(this.getDriverClassName());

Expand Down Expand Up @@ -61,34 +116,14 @@ public MySQLDatabaseType(@NotNull String address, @NotNull String username, @Not
dataSource.addDataSourceProperty("socketTimeout", String.valueOf(TimeUnit.SECONDS.toMillis(30)));
}

@Override
public String getDriverClassName() {
try {
return Class.forName("com.mysql.cj.jdbc.Driver").getName();
} catch (ClassNotFoundException exception) {
return "com.mysql.jdbc.Driver";
@SuppressWarnings("unused")
public static abstract class MySQLDatabaseTypeBuilder<C extends MySQLDatabaseType, B extends MySQLDatabaseTypeBuilder<C, B>> extends SQLDatabaseTypeBuilder<C, B> {

protected B dataSource(HikariDataSource dataSource) {
this.self().dataSource(dataSource);
return self();
}
}

@Contract("_ -> this")
public SQLDatabaseType configureDataSource(@NotNull Consumer<HikariDataSource> consumer) {
consumer.accept(dataSource);
return this;
}

@NotNull
@Override
public SQLConnector connect() throws SQLException {
// Test if connection was established.
dataSource.getConnection().close();

return consumer -> {
try (Connection connection = dataSource.getConnection()) {
consumer.execute(connection);
} catch (SQLException exception) {
throw new ConnectorException(exception);
}
};

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import com.jaoow.sql.connector.exception.ConnectorException;
import com.jaoow.sql.connector.SQLConnector;
import com.jaoow.sql.connector.type.SQLDatabaseType;
import lombok.Builder;
import lombok.Getter;
import lombok.experimental.SuperBuilder;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.sqlite.SQLiteConfig;
import org.sqlite.SQLiteDataSource;
Expand All @@ -15,57 +16,53 @@
import java.sql.SQLException;

@Getter
@SuperBuilder
public final class SQLiteDatabaseType extends SQLDatabaseType {

private final SQLiteDataSource source;
private final File file;

private SQLiteDataSource source;

public SQLiteDatabaseType(@NotNull String driverClassName, @NotNull String jdbcUrl, @NotNull File file) {
super(driverClassName, jdbcUrl);
this.file = file;

SQLiteConfig config = new SQLiteConfig();
config.setSynchronous(SQLiteConfig.SynchronousMode.NORMAL);
config.setTempStore(SQLiteConfig.TempStore.MEMORY);
config.setPageSize(32768);

source = new SQLiteDataSource(config);
source.setUrl("jdbc:sqlite:" + file);
}

@Builder
public SQLiteDatabaseType(@NotNull File file) {
this(
"org.sqlite.JDBC",
"jdbc:sqlite:" + file,
file
);

try {
File parent = file.getParentFile();
if (!parent.exists() && !parent.mkdirs()) {
throw new IOException("The database folder cannot be created.");
}
@NotNull
@Override
@Contract(pure = true)
public String getJdbcUrl() {
return "jdbc:sqlite:%s";
}

if (!file.exists() && !file.createNewFile()) {
throw new IOException("The database file cannot be created.");
}
@NotNull
@Override
@Contract(pure = true)
public String getDriverClassName() {
return "org.sqlite.JDBC";
}

} catch (IOException e) {
e.printStackTrace();
}
public static SQLiteDatabaseTypeBuilder<?,?> builder(@NotNull File file) {
return new SQLiteDatabaseTypeBuilderImpl().file(file);
}

@Override
@NotNull
@Override
public SQLConnector connect() throws SQLException {

try {
Class.forName(this.getDriverClassName());
} catch (ClassNotFoundException e) {
throw new SQLException("Driver not found.");
}

if (this.source == null) {
try {
this.initDataSource();
} catch (IOException e) {
throw new RuntimeException("Error while initializing the database file.", e);
}
}

return consumer -> {
try (Connection connection = source.getConnection()) {
consumer.execute(connection);
Expand All @@ -74,4 +71,24 @@ public SQLConnector connect() throws SQLException {
}
};
}

private void initDataSource() throws IOException {
File parent = file.getParentFile();
if (!parent.exists() && !parent.mkdirs()) {
throw new IOException("The database folder cannot be created.");
}

if (!file.exists() && !file.createNewFile()) {
throw new IOException("The database file cannot be created.");
}

SQLiteConfig config = new SQLiteConfig();
config.setSynchronous(SQLiteConfig.SynchronousMode.NORMAL);
config.setTempStore(SQLiteConfig.TempStore.MEMORY);
config.setPageSize(32768);

source = new SQLiteDataSource(config);
source.setUrl(String.format(getJdbcUrl(), file));
}

}
Loading
Loading