/* * 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 indexes(Long productionId, Connection connection) throws SQLException { String query = "SELECT indexes FROM productionMetaData_indexes WHERE productionId = ? ORDER BY indexes"; List 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; } }