Search in sources :

Example 1 with TablesColumn

use of mom.trd.opentheso.core.exports.privatesdatas.TablesColumn in project opentheso by miledrousset.

the class ExportPrivatesDatas method getDatasOfTable.

public ArrayList<Table> getDatasOfTable(HikariDataSource ds, String tableName) {
    ArrayList<Table> tableList = new ArrayList<>();
    if (!isTableToIgnore(tableName)) {
        ArrayList<TablesColumn> tablesColumns = new ArrayList<>();
        Connection conn;
        Statement stmt;
        ResultSet resultSet;
        String colomneNameTemp;
        try {
            conn = ds.getConnection();
            try {
                stmt = conn.createStatement();
                try {
                    // récupération des noms des colonnes de la table
                    // et sont type de données
                    String query = "SELECT COLUMN_NAME," + " data_type FROM INFORMATION_SCHEMA.COLUMNS" + " where TABLE_NAME='" + tableName + "'";
                    resultSet = stmt.executeQuery(query);
                    while (resultSet.next()) {
                        colomneNameTemp = resultSet.getString("COLUMN_NAME");
                        if (!isToIgnore(colomneNameTemp)) {
                            TablesColumn tablesColumn = new TablesColumn();
                            tablesColumn.setColumnName(colomneNameTemp);
                            tablesColumn.setColumnType(resultSet.getString("data_type"));
                            tablesColumns.add(tablesColumn);
                        }
                    }
                    // récupération des données de la table
                    query = "SELECT * FROM " + tableName;
                    resultSet = stmt.executeQuery(query);
                    while (resultSet.next()) {
                        Table table = new Table();
                        ArrayList<LineOfData> lineOfDatas = new ArrayList<>();
                        for (TablesColumn tablesColumn : tablesColumns) {
                            LineOfData lineOfData = new LineOfData();
                            lineOfData.setColomne(tablesColumn.getColumnName());
                            if (tablesColumn.getColumnType().equalsIgnoreCase("integer")) {
                                lineOfData.setValue("" + resultSet.getInt(tablesColumn.getColumnName()));
                            }
                            if (tablesColumn.getColumnType().equalsIgnoreCase("character varying")) {
                                lineOfData.setValue(resultSet.getString(tablesColumn.getColumnName()));
                            }
                            if (tablesColumn.getColumnType().equalsIgnoreCase("character")) {
                                lineOfData.setValue(resultSet.getString(tablesColumn.getColumnName()));
                            }
                            if (tablesColumn.getColumnType().equalsIgnoreCase("text")) {
                                lineOfData.setValue(resultSet.getString(tablesColumn.getColumnName()));
                            }
                            if (tablesColumn.getColumnType().equalsIgnoreCase("timestamp with time zone")) {
                                lineOfData.setValue("" + resultSet.getDate(tablesColumn.getColumnName()));
                            }
                            if (tablesColumn.getColumnType().equalsIgnoreCase("timestamp without time zone")) {
                                lineOfData.setValue("" + resultSet.getDate(tablesColumn.getColumnName()));
                            }
                            if (tablesColumn.getColumnType().equalsIgnoreCase("boolean")) {
                                lineOfData.setValue("" + resultSet.getBoolean(tablesColumn.getColumnName()));
                            }
                            if (tablesColumn.getColumnType().equalsIgnoreCase("USER-DEFINED")) {
                                lineOfData.setValue(resultSet.getString(tablesColumn.getColumnName()));
                            }
                            lineOfDatas.add(lineOfData);
                        }
                        table.setLineOfDatas(lineOfDatas);
                        tableList.add(table);
                    }
                } finally {
                    stmt.close();
                }
            } finally {
                conn.close();
            }
        } catch (SQLException ex) {
            Logger.getLogger(Table.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    return tableList;
}
Also used : Table(mom.trd.opentheso.core.exports.privatesdatas.tables.Table) SQLException(java.sql.SQLException) Statement(java.sql.Statement) LineOfData(mom.trd.opentheso.core.exports.privatesdatas.LineOfData) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) TablesColumn(mom.trd.opentheso.core.exports.privatesdatas.TablesColumn)

Aggregations

Connection (java.sql.Connection)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Statement (java.sql.Statement)1 ArrayList (java.util.ArrayList)1 LineOfData (mom.trd.opentheso.core.exports.privatesdatas.LineOfData)1 TablesColumn (mom.trd.opentheso.core.exports.privatesdatas.TablesColumn)1 Table (mom.trd.opentheso.core.exports.privatesdatas.tables.Table)1