organization changed, added sql connection project
Signed-off-by: Quentin Legot <legotquentin@gmail.com>
This commit is contained in:
parent
ce6d16ef37
commit
396f0cee94
54
build.gradle
54
build.gradle
@ -1,38 +1,8 @@
|
||||
plugins {
|
||||
id 'java'
|
||||
id 'maven-publish'
|
||||
}
|
||||
|
||||
group "${project.maven_group}"
|
||||
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()
|
||||
}
|
||||
subprojects {
|
||||
apply plugin: 'java'
|
||||
apply plugin: 'maven-publish'
|
||||
sourceCompatibility = JavaVersion.VERSION_17
|
||||
targetCompatibility = JavaVersion.VERSION_17
|
||||
|
||||
publishing {
|
||||
publications {
|
||||
@ -51,3 +21,17 @@ 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
24
database/build.gradle
Normal 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()
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
package fr.altarik.toolbox.database;
|
||||
|
||||
public record ConnectionConfig(String host, int port, String database, String username, String password) {
|
||||
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
@ -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();
|
||||
|
||||
}
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
7
database/src/test/resources/config.yml
Normal file
7
database/src/test/resources/config.yml
Normal file
@ -0,0 +1,7 @@
|
||||
{
|
||||
"host": "127.0.0.1",
|
||||
"port": 5432,
|
||||
"database": "postgres",
|
||||
"username": "postgres",
|
||||
"password": "root"
|
||||
}
|
@ -1,2 +1,2 @@
|
||||
rootProject.name = 'Toolbox'
|
||||
|
||||
include(':tasks', ':database')
|
||||
|
21
tasks/build.gradle
Normal file
21
tasks/build.gradle
Normal 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()
|
||||
}
|
Loading…
Reference in New Issue
Block a user