Kenmegne
2025-12-10 e9d80d486b912144b59ebd5939d4837105b37b99
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
/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Project/Maven2/JavaApp/src/main/java/${packagePath}/${mainClassName}.java to edit this template
 */
package com.megatim.queryadhoc;
 
import com.megatim.queryadhoc.enums.SgbdType;
import com.megatim.queryadhoc.enums.TypeDonnee;
import com.megatim.queryadhoc.exceptions.ConnectionException;
import com.megatim.queryadhoc.exceptions.ConnectionParameterException;
import com.megatim.queryadhoc.exceptions.IllegalFormatDateException;
import com.megatim.queryadhoc.exceptions.NotSupportedDataBaseException;
import com.megatim.queryadhoc.model.ConnectionParameter;
import com.megatim.queryadhoc.model.DbColumnDescription;
import com.megatim.queryadhoc.query.insert.impl.PostgresInsertQuery;
import com.megatim.queryadhoc.query.select.impl.PostgresSelectQuery;
import com.megatim.queryadhoc.table.DataBaseTable;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
 
/**
 *
 * @author ASUS
 */
public class App {
 
    private static final String TABLE_NAME = "users";
    private static final String DATABASE_NAME = "test_query";
    private static final String OUTPUT_DIR = "C:\\Users\\ASUS\\Documents\\DATAS\\Export";
 
    public static void main(String[] args) {
//        tableExists();
//        isDatabaseAvailable();
//        insert();
        select();
    }
 
    private static ConnectionParameter mySqlConnectionParameters() {
        return new ConnectionParameter(3306, DATABASE_NAME, "mela", "127.0.0.1", "mela");
    }
 
    private static ConnectionParameter postgresConnectionParameters() {
        return new ConnectionParameter(5432, DATABASE_NAME, "postgres", "127.0.0.1", "mela");
    }
 
    private static ConnectionParameter sqlServerConnectionParameters() {
        return new ConnectionParameter(1433, DATABASE_NAME, "sa", "192.168.1.210", "Mgt@9090");
    }
 
    private static void isDatabaseAvailable() {
        try {
            boolean bool = new DataBaseTable(SgbdType.POSTGRESQL, postgresConnectionParameters())
                    .isDatabaseAvailable();
            System.out.println("Result : " + bool);
        } catch (NotSupportedDataBaseException | ConnectionParameterException ex) {
            Logger.getLogger(App.class.getName()).log(Level.SEVERE, ex.getMessage(), ex);
        }
    }
 
    private static void tableExists() {
        try {
            boolean bool = new DataBaseTable(SgbdType.POSTGRESQL, postgresConnectionParameters())
                    .tableExists("users1");
            System.out.println("Result : " + bool);
        } catch (NotSupportedDataBaseException | ConnectionParameterException | ConnectionException ex) {
            Logger.getLogger(App.class.getName()).log(Level.SEVERE, ex.getMessage(), ex);
        }
    }
 
    private static void select() {
        try {
//            List<Map<String, Object>> data = new DataBaseTable()
//                    .select(SgbdType.POSTGRESQL, postgresConnectionParameters())
//                    .execute(TABLE_NAME, Arrays.asList("userName", "dateaction"));
//
//            data.stream().forEach(l -> {
//                l.forEach((k, v) -> {
//                    System.out.println(k + ", " + v);
//                });
//            });
            new DataBaseTable(SgbdType.POSTGRESQL, postgresConnectionParameters())
                    .select()
                    .execute(TABLE_NAME, columns(), OUTPUT_DIR, TABLE_NAME, false);
        } catch (NotSupportedDataBaseException ex) {
            Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
 
    private static void insert() {
        try {
            File dataFile = new File("C:\\Users\\ASUS\\Documents\\DATAS\\Export\\USERS20250129102052.txt");
            new DataBaseTable(SgbdType.POSTGRESQL, postgresConnectionParameters())
                    .insert()
                    .execute(TABLE_NAME, columns(), dataFile);
        } catch (NotSupportedDataBaseException ex) {
            Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
 
    private static void postgresSelectQuery() {
        try {
//            List<Map<String, Object>> data = new PostgresSelectQuery(connectionParameters()).execute("ref000018_caaadm", Arrays.asList());
//System.out.println("***************** size " + data.size());
//            System.out.println("" + data.get(0));
            new PostgresSelectQuery(postgresConnectionParameters()).execute("users", columns(), TABLE_NAME, OUTPUT_DIR, true);
 
        } catch (Exception ex) {
            Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
 
    private static void postgresInsertQuery() {
        File dataFile = new File("C:\\Users\\ASUS\\Documents\\DATAS\\Export\\USERS20250127160445.txt");
        try {
            new PostgresInsertQuery(postgresConnectionParameters()).execute(TABLE_NAME, columns(), data());
        } catch (Exception ex) {
            Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
 
    private static List<DbColumnDescription> columns() {
        try {
            List<DbColumnDescription> cols = new ArrayList<>();
//        cols.add(new DbColumnDescription("code_adnt", TypeDonnee.ALPHANUMERIQUE, 10, 1));
//        cols.add(new DbColumnDescription("valeur", TypeDonnee.ALPHANUMERIQUE, 20, 2));
//        cols.add(new DbColumnDescription("code_val", TypeDonnee.ALPHANUMERIQUE, 30, 3));
//        cols.add(new DbColumnDescription("qua_ti_col", TypeDonnee.ALPHANUMERIQUE, 30, 4));
//        cols.add(new DbColumnDescription("te_cpt_cons", TypeDonnee.ALPHANUMERIQUE, 50, 5));
 
            cols.add(new DbColumnDescription("username", TypeDonnee.ALPHANUMERIQUE, 4, 1, null, null, 0));
            cols.add(new DbColumnDescription("montant", TypeDonnee.NUMERIQUE, 10, 2, null, null, 0));
 
            DbColumnDescription desc = new DbColumnDescription("dateaction", TypeDonnee.DATE, 10, 3, "yyyyMMdd", "-", 0);
            cols.add(desc);
 
            return cols;
        } catch (IllegalFormatDateException ex) {
            Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }
 
    private static List<Map<String, Object>> data() {
        List<Map<String, Object>> data = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
 
        map.put("username", "Samory");
        map.put("montant", "000025");
        map.put("dateaction", "2025-02-06");
        data.add(map);
 
        return data;
    }
}