Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package pro.aloginov.revoluttest;
- import javax.inject.Inject;
- import javax.inject.Named;
- import javax.inject.Singleton;
- import javax.ws.rs.Path;
- import javax.ws.rs.Produces;
- import javax.ws.rs.core.MediaType;
- import java.sql.*;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Optional;
- import java.util.concurrent.CompletableFuture;
- import java.util.concurrent.ExecutorService;
- @Singleton
- @Path("transfer")
- @Produces(MediaType.APPLICATION_JSON)
- public class DatabaseService {
- Connection connection;
- private final ExecutorService ex;
- @Inject
- public DatabaseService(@Named("ex") ExecutorService ex) {
- this.ex = ex;
- }
- public Connection creAteConnection() {
- try {
- connection = DriverManager.getConnection("jdbc:postgresql://localhost:5432/postgres", "postgres",
- "1988");
- } catch (SQLException e) {
- throw new RuntimeException(e);
- }
- return connection;
- }
- private static void mapParams(PreparedStatement ps, Object... args) throws SQLException {
- int i = 1;
- for (Object arg : args) {
- if (arg instanceof Date) {
- ps.setTimestamp(i++, new Timestamp(((Date) arg).getTime()));
- } else if (arg instanceof Integer) {
- System.out.print(i);
- ps.setInt(i++, (Integer) arg);
- System.out.print(i);
- } else if (arg instanceof Long) {
- ps.setLong(i++, (Long) arg);
- } else if (arg instanceof Double) {
- ps.setDouble(i++, (Double) arg);
- } else if (arg instanceof Float) {
- ps.setFloat(i++, (Float) arg);
- } else {
- ps.setString(i++, (String) arg);
- }
- }
- }
- public <T> CompletableFuture<List<T>> loadAsync(String sql, RowMapper<T> rowMapper, Object... params) {
- return CompletableFuture.supplyAsync(() -> loadMany(sql, rowMapper, params), ex);
- }
- public <T> CompletableFuture<T> loadsingle(String sql, RowMapper<T> rowMapper, Object... params) {
- return CompletableFuture.supplyAsync(() -> loadOne(sql, rowMapper, params), ex);
- }
- public <T> T loadOne(String sql, RowMapper<T> rowMapper, Object... params) {
- try {
- PreparedStatement ps = connection.prepareStatement(sql);
- mapParams(ps, params);
- return loadOptional(ps, rowMapper);
- } catch (Exception e) {
- System.out.print(sql);
- throw new RuntimeException(e);
- }
- }
- public <T> List<T> loadMany(String sql, RowMapper<T> rowMapper, Object... params) {
- try {
- PreparedStatement ps = connection.prepareStatement(sql);
- mapParams(ps, params);
- return load(ps, rowMapper);
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- }
- private <T> List<T> load(PreparedStatement ps, RowMapper<T> rowMapper) throws SQLException {
- ResultSet rs = ps.executeQuery();
- List<T> result = new ArrayList<>();
- while (rs.next()) {
- T element = rowMapper.get(rs);
- result.add(element);
- }
- return result;
- }
- public interface RowMapper<T> {
- T get(ResultSet rs) throws SQLException;
- }
- private <T> T loadOptional(PreparedStatement ps, RowMapper<T> rowMapper) throws SQLException {
- ResultSet rs = ps.executeQuery();
- Optional<T> element = null;
- while (rs.next()) {
- element = Optional.of(rowMapper.get(rs));
- }
- return element.get();
- }
- }
- /* @Inject
- public DatabaseService() {
- try {
- connection = DriverManager.getConnection("jdbc:postgresql://localhost:5432/postgres", "postgres",
- "1988");
- } catch (SQLException ex) {
- // log an exception. fro example:
- System.out.println("Failed to create the database connection.");
- }
- }
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement