From 31ea05297dbfadf79dd8c1809b9b297cfcf91709 Mon Sep 17 00:00:00 2001 From: "miriam.baglioni" Date: Mon, 4 May 2020 12:22:28 +0200 Subject: [PATCH] moved the DbClient to common and added needed dependency to pom --- dhp-common/pom.xml | 4 ++ .../java/eu/dnetlib/dhp/common/DbClient.java | 62 +++++++++++++++++++ .../dhp/oa/graph/raw/common/DbClient.java | 61 ------------------ 3 files changed, 66 insertions(+), 61 deletions(-) create mode 100644 dhp-common/src/main/java/eu/dnetlib/dhp/common/DbClient.java delete mode 100644 dhp-workflows/dhp-graph-mapper/src/main/java/eu/dnetlib/dhp/oa/graph/raw/common/DbClient.java diff --git a/dhp-common/pom.xml b/dhp-common/pom.xml index 7b073397f..0daee9c8b 100644 --- a/dhp-common/pom.xml +++ b/dhp-common/pom.xml @@ -81,6 +81,10 @@ com.jayway.jsonpath json-path + + org.postgresql + postgresql + diff --git a/dhp-common/src/main/java/eu/dnetlib/dhp/common/DbClient.java b/dhp-common/src/main/java/eu/dnetlib/dhp/common/DbClient.java new file mode 100644 index 000000000..cedc9bd4d --- /dev/null +++ b/dhp-common/src/main/java/eu/dnetlib/dhp/common/DbClient.java @@ -0,0 +1,62 @@ + +package eu.dnetlib.dhp.common; + +import java.io.Closeable; +import java.io.IOException; +import java.sql.*; +import java.util.function.Consumer; + +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +public class DbClient implements Closeable { + + private static final Log log = LogFactory.getLog(DbClient.class); + + private Connection connection; + + public DbClient(final String address, final String login, final String password) { + + try { + Class.forName("org.postgresql.Driver"); + + this.connection = StringUtils.isNoneBlank(login, password) + ? DriverManager.getConnection(address, login, password) + : DriverManager.getConnection(address); + this.connection.setAutoCommit(false); + } catch (final Exception e) { + log.error("Connection to postgresDB failed"); + throw new RuntimeException("Connection to postgresDB failed", e); + } + log.info("Opened database successfully"); + } + + public void processResults(final String sql, final Consumer consumer) { + + try (final Statement stmt = connection.createStatement()) { + stmt.setFetchSize(100); + + try (final ResultSet rs = stmt.executeQuery(sql)) { + while (rs.next()) { + consumer.accept(rs); + } + } catch (final SQLException e) { + log.error("Error executing sql query: " + sql, e); + throw new RuntimeException("Error executing sql query", e); + } + } catch (final SQLException e1) { + log.error("Error preparing sql statement", e1); + throw new RuntimeException("Error preparing sql statement", e1); + } + } + + @Override + public void close() throws IOException { + try { + connection.close(); + } catch (final SQLException e) { + throw new RuntimeException(e); + } + } +} diff --git a/dhp-workflows/dhp-graph-mapper/src/main/java/eu/dnetlib/dhp/oa/graph/raw/common/DbClient.java b/dhp-workflows/dhp-graph-mapper/src/main/java/eu/dnetlib/dhp/oa/graph/raw/common/DbClient.java deleted file mode 100644 index ca7c9fffb..000000000 --- a/dhp-workflows/dhp-graph-mapper/src/main/java/eu/dnetlib/dhp/oa/graph/raw/common/DbClient.java +++ /dev/null @@ -1,61 +0,0 @@ -package eu.dnetlib.dhp.oa.graph.raw.common; - -import java.io.Closeable; -import java.io.IOException; -import java.sql.*; -import java.util.function.Consumer; -import org.apache.commons.lang3.StringUtils; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -public class DbClient implements Closeable { - - private static final Log log = LogFactory.getLog(DbClient.class); - - private Connection connection; - - public DbClient(final String address, final String login, final String password) { - - try { - Class.forName("org.postgresql.Driver"); - - this.connection = - StringUtils.isNoneBlank(login, password) - ? DriverManager.getConnection(address, login, password) - : DriverManager.getConnection(address); - this.connection.setAutoCommit(false); - } catch (final Exception e) { - log.error("Connection to postgresDB failed"); - throw new RuntimeException("Connection to postgresDB failed", e); - } - log.info("Opened database successfully"); - } - - public void processResults(final String sql, final Consumer consumer) { - - try (final Statement stmt = connection.createStatement()) { - stmt.setFetchSize(100); - - try (final ResultSet rs = stmt.executeQuery(sql)) { - while (rs.next()) { - consumer.accept(rs); - } - } catch (final SQLException e) { - log.error("Error executing sql query: " + sql, e); - throw new RuntimeException("Error executing sql query", e); - } - } catch (final SQLException e1) { - log.error("Error preparing sql statement", e1); - throw new RuntimeException("Error preparing sql statement", e1); - } - } - - @Override - public void close() throws IOException { - try { - connection.close(); - } catch (final SQLException e) { - throw new RuntimeException(e); - } - } -}