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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
 * 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.tools.database.contrat.AppColumnDefinition;
import com.megatim.fdxcommons.tools.database.tables.appcolumns.FdxConsultationIndexColumnDefinition;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
 
/**
 *
 * @author ASUS
 */
public class TableDefinitionQueries {
 
    private final String tableName;
    private final Connection connection;
 
    public TableDefinitionQueries(String tableName, Connection connection) {
        this.tableName = tableName;
        this.connection = connection;
    }
 
    public boolean tableExists() throws SQLException {
        Long nb = 0L;
 
        try ( PreparedStatement statement = connection.prepareStatement(tableExistsQuery())) {
            ResultSet resultSet = statement.executeQuery();
 
            if (resultSet.next()) {
                nb = resultSet.getLong(1);
            }
        }
        return nb == 1;
    }
    
    public boolean constraintExists(String constraintName) throws SQLException {
        Long nb = 0L;
 
        try ( PreparedStatement statement = connection.prepareStatement(constraintExistsQuery(constraintName))) {
            ResultSet resultSet = statement.executeQuery();
 
            if (resultSet.next()) {
                nb = resultSet.getLong(1);
            }
        }
        return nb == 1;
    }
 
    public boolean columnExists(String columnName) throws SQLException {
        Long nb = 0L;
 
        try ( PreparedStatement statement = connection.prepareStatement(columnExistsQuery(columnName))) {
            ResultSet resultSet = statement.executeQuery();
 
            if (resultSet.next()) {
                nb = resultSet.getLong(1);
            }
        }
        return nb == 1;
    }
 
    private String columnExistsQuery(String columnName) {
        return "SELECT COUNT(column_name)"
                + " FROM information_schema.columns"
                + " WHERE table_name = '" + tableName + "' AND column_name = '" + columnName + "'";
    }
 
    private String constraintExistsQuery(String constraintName) {
        return "SELECT COUNT(*) FROM  pg_constraint WHERE conname =  '" + constraintName + "'";
    }
 
    private String tableExistsQuery() {
        return "SELECT  COUNT(table_name)"
                + " FROM  information_schema.tables"
                + " WHERE  table_schema LIKE 'public' AND  table_type LIKE 'BASE TABLE' AND table_name = '" + tableName + "'";
    }
 
    public void addAppColumn(AppColumnDefinition appColumn) throws SQLException {
 
        try ( Statement stmt = connection.createStatement()) {
            String query = addAppColumnQuery(appColumn).toString();
            stmt.execute(query);
        }
    }
 
    public void dropAppColumn(AppColumnDefinition appColumn) throws SQLException {
        String query = dropColumnQuery(appColumn);
        
        try ( Statement stmt = connection.createStatement()) {
            stmt.execute(query);
        }
    }
 
    public void dropConstraint(String constraintName) throws SQLException {
        String query = dropConstraintQuery(constraintName);
        
        try ( Statement stmt = connection.createStatement()) {
            stmt.execute(query);
        }
    }
 
    public void addPkConstraint(String constraintName) throws SQLException {
        String query = addPkConstraintQuery(constraintName);
        
        try ( Statement stmt = connection.createStatement()) {
            stmt.execute(query);
        }
    }
 
    private String dropColumnQuery(AppColumnDefinition column) {
        StringBuilder query = new StringBuilder("ALTER TABLE ");
 
        query
                .append(tableName)
                .append(" DROP COLUMN IF EXISTS ")
                .append(column.name())
                .append(";");
 
        return query.toString();
    }
 
    private String dropConstraintQuery(String constraintName) {
        StringBuilder query = new StringBuilder();
 
        query.append("ALTER TABLE ")
                .append(tableName)
                .append(" DROP CONSTRAINT IF EXISTS ")
                .append(constraintName)
                .append(";");
 
        return query.toString();
    }
 
    private String addPkConstraintQuery(String constraintName) {
        StringBuilder query = new StringBuilder();
 
        query.append("ALTER TABLE ")
                .append(tableName)
                .append(" ADD CONSTRAINT ")
                .append(constraintName)
                .append(" PRIMARY KEY(")
                .append(new FdxConsultationIndexColumnDefinition().name())
                .append(");");
 
        return query.toString();
    }
 
    private StringBuilder addAppColumnQuery(AppColumnDefinition column) {
        StringBuilder query = new StringBuilder("ALTER TABLE ");
        query
                .append(tableName)
                .append(" ADD COLUMN IF NOT EXISTS ")
                .append(column.name())
                .append(" ")
                .append(column.type())
                .append(column.isNull() ? " NULL " : " NOT NULL ");
 
        if (column.defaultValue() != null) {
            query.append("DEFAULT ").append(column.defaultValue());
        }
        return query;
    }
}