organization changed, added sql connection project

Signed-off-by: Quentin Legot <legotquentin@gmail.com>
This commit is contained in:
Quentin Legot 2022-11-07 00:09:52 +01:00
parent ce6d16ef37
commit 396f0cee94
13 changed files with 232 additions and 48 deletions

View File

@ -1,40 +1,10 @@
plugins { subprojects {
id 'java' apply plugin: 'java'
id 'maven-publish' apply plugin: 'maven-publish'
} sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
group "${project.maven_group}" publishing {
version "${project.maven_version}"
repositories {
maven {
name 'altarik-snapshots'
url 'https://repo.altarik.fr/snapshots/'
}
maven {
name 'altarik-releases'
url 'https://repo.altarik.fr/releases/'
}
mavenCentral()
}
dependencies {
testImplementation "org.junit.jupiter:junit-jupiter-api:${project.junit_version}"
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:${project.junit_version}"
}
java {
withSourcesJar()
withJavadocJar()
}
test {
useJUnitPlatform()
}
publishing {
publications { publications {
mavenJava(MavenPublication) { mavenJava(MavenPublication) {
from components.java from components.java
@ -50,4 +20,18 @@ publishing {
} }
} }
} }
}
repositories {
maven {
name 'altarik-snapshots'
url 'https://repo.altarik.fr/snapshots/'
}
maven {
name 'altarik-releases'
url 'https://repo.altarik.fr/releases/'
}
mavenCentral()
}
} }

24
database/build.gradle Normal file
View File

@ -0,0 +1,24 @@
plugins {
id 'java'
id 'maven-publish'
}
group "${project.maven_group}"
version "${project.maven_version}"
dependencies {
implementation 'org.postgresql:postgresql:42.5.0'
testImplementation 'com.google.code.gson:gson:2.10'
testImplementation "org.junit.jupiter:junit-jupiter-api:5.9.0"
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:5.9.0"
}
java {
withSourcesJar()
withJavadocJar()
}
test {
useJUnitPlatform()
}

View File

@ -0,0 +1,40 @@
package fr.altarik.toolbox.database;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public abstract class AbstractSqlConnection implements SqlConnection {
protected final ConnectionConfig config;
protected Connection connection;
protected AbstractSqlConnection(ConnectionConfig config) throws SQLException {
this.config = config;
DriverManager.setLoginTimeout(3);
connect();
}
public void checkConnection() throws SQLException {
if(connection == null || connection.isClosed() || !connection.isValid(1))
connect();
}
@Override
public Connection getConnection() {
return connection;
}
public void closeConnection() {
try {
if(!connection.isClosed()) {
connection.close();
connection = null;
}
} catch(SQLException ignored) {
// no op
}
}
}

View File

@ -0,0 +1,5 @@
package fr.altarik.toolbox.database;
public record ConnectionConfig(String host, int port, String database, String username, String password) {
}

View File

@ -0,0 +1,14 @@
package fr.altarik.toolbox.database;
import java.sql.SQLException;
public class Connections {
/**
* Create a new Connection object for a postgresql database server
* @return
*/
public static SqlConnection newPostgresConnection(ConnectionConfig config) throws SQLException {
return new PostgresConnection(config);
}
}

View File

@ -0,0 +1,21 @@
package fr.altarik.toolbox.database;
import java.sql.DriverManager;
import java.sql.SQLException;
public class PostgresConnection extends AbstractSqlConnection {
PostgresConnection(ConnectionConfig config) throws SQLException {
super(config);
}
@Override
public void connect() throws SQLException {
String host = config.host();
int port = config.port();
String username = config.username();
String password = config.password();
String database = config.database();
this.connection = DriverManager.getConnection("jdbc:postgresql://"+ host + ":" + port + "/" + database + "?autoReconnect=true", username, password);
}
}

View File

@ -0,0 +1,16 @@
package fr.altarik.toolbox.database;
import java.sql.Connection;
import java.sql.SQLException;
public interface SqlConnection {
void connect() throws SQLException;
Connection getConnection();
void checkConnection() throws SQLException;
void closeConnection();
}

View File

@ -0,0 +1,52 @@
package fr.altarik.toolbox.database;
import com.google.gson.Gson;
import org.junit.jupiter.api.Test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.sql.PreparedStatement;
import java.util.Objects;
import java.util.stream.Collectors;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
class ConnectionTest {
@Test
void databaseTest() {
assertDoesNotThrow(() -> {
InputStream configInput = getResource("config.yml");
String configStr = new BufferedReader(new InputStreamReader(Objects.requireNonNull(configInput)))
.lines().collect(Collectors.joining("\n"));
Gson gson = new Gson();
ConnectionConfig config = gson.fromJson(configStr, ConnectionConfig.class);
SqlConnection connection = Connections.newPostgresConnection(config);
try(PreparedStatement statement = connection.getConnection().prepareStatement("CREATE TABLE IF NOT EXISTS toolbox(id SERIAL, PRIMARY KEY (id));")) {
statement.executeUpdate();
}
});
}
private InputStream getResource(String resourcePath) {
try {
URL url = this.getClass().getClassLoader().getResource(resourcePath);
if(url == null)
return null;
URLConnection connection = url.openConnection();
connection.setUseCaches(false);
return connection.getInputStream();
} catch (IOException e){
return null;
}
}
}

View File

@ -0,0 +1,7 @@
{
"host": "127.0.0.1",
"port": 5432,
"database": "postgres",
"username": "postgres",
"password": "root"
}

View File

@ -1,2 +1,2 @@
rootProject.name = 'Toolbox' rootProject.name = 'Toolbox'
include(':tasks', ':database')

21
tasks/build.gradle Normal file
View File

@ -0,0 +1,21 @@
plugins {
id 'java'
id 'maven-publish'
}
group "${project.maven_group}"
version "${project.maven_version}"
dependencies {
testImplementation "org.junit.jupiter:junit-jupiter-api:${project.junit_version}"
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:${project.junit_version}"
}
java {
withSourcesJar()
withJavadocJar()
}
test {
useJUnitPlatform()
}