Kenmegne
7 days ago 23a46b4be35277e06ec89f48730eeb694e686be8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package com.megatim.fdxcommons.tools.database.queries;
 
import com.megatim.fdxcommons.model.dataproduction.CommonDataProduction;
import com.megatim.fdxcommons.tools.database.tables.appcolumns.DataProductionIdColumnDefinition;
import com.megatim.fdxcommons.tools.database.tables.appcolumns.IndexColumnDefinition;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
 
/**
 *
 * @author ASUS
 */
public class CustomQueries {
 
    public Long selectMinIndexOfDataProduction(Long dataProdcutionId, String tableName, Connection connection) throws SQLException {
        String query = "SELECT MIN(" + new IndexColumnDefinition().name() + ")"
                + " FROM " + tableName
                + " WHERE " + new DataProductionIdColumnDefinition().name() + " = " + dataProdcutionId;
        Long min = null;
 
        try ( PreparedStatement statement = connection.prepareStatement(query);  ResultSet resultSet = statement.executeQuery()) {
            while (resultSet.next()) {
                min = resultSet.getLong(1);
            }
        }
        return min;
    }
 
    public boolean fdxIndexExists(Long index, String tableName, Connection connection) throws SQLException {
        String query = "SELECT * FROM " + tableName + " WHERE " + new IndexColumnDefinition().name() + " = ?";
 
        boolean exists = false;
 
        try ( PreparedStatement statement = connection.prepareStatement(query);) {
            statement.setLong(1, index);
            ResultSet resultSet = statement.executeQuery();
 
            if (resultSet.next()) {
                exists = true;
            }
        }
        return exists;
    }
 
    public void deleteWhereIndexLowerThan(Long fdx_index, String tableName, Connection connection) throws SQLException {
        String query = "DELETE FROM " + tableName + " WHERE " + new IndexColumnDefinition().name() + " < ?";
 
        try ( PreparedStatement statement = connection.prepareStatement(query);) {
            statement.setLong(1, fdx_index);
 
            statement.executeUpdate();
        }
    }
 
    public Long countDataproductionElements(String token, Connection connection) throws SQLException {
        String query = "SELECT COUNT(*) FROM " + CommonDataProduction.class.getSimpleName() + " WHERE token = ?";
 
        try ( PreparedStatement statement = connection.prepareStatement(query);) {
            statement.setString(1, token);
            ResultSet result = statement.executeQuery();
 
            result.next();
            return result.getLong(1);
        }
    }
 
    public List<Long> indexes(Long productionId, Connection connection) throws SQLException {
        String query = "SELECT indexes FROM  productionMetaData_indexes WHERE productionId = ? ORDER BY indexes";
        List<Long> indexes = new ArrayList<>();
 
        try ( PreparedStatement statement = connection.prepareStatement(query);) {
            statement.setLong(1, productionId);
            ResultSet resultSet = statement.executeQuery();
 
            while (resultSet.next()) {
                indexes.add(resultSet.getLong(1));
            }
        }
        return indexes;
    }
}