Projekt

Obecné

Profil

Stáhnout (4.41 KB) Statistiky
| Větev: | Revize:
1
package vldc.aswi.database;
2

    
3
import org.springframework.dao.DataAccessException;
4
import org.springframework.jdbc.core.JdbcTemplate;
5
import org.springframework.jdbc.core.ResultSetExtractor;
6
import org.springframework.stereotype.Component;
7
import vldc.aswi.model.table.TableColumn;
8

    
9
import java.sql.ResultSet;
10
import java.sql.ResultSetMetaData;
11
import java.sql.SQLException;
12
import java.util.ArrayList;
13
import java.util.HashMap;
14
import java.util.List;
15
import java.util.Map;
16

    
17
/**
18
 * Class for manipulating (selecting, updating, creating, ...) database data.
19
 */
20
@Component
21
public class DatabaseInterface {
22

    
23
    /** JdbcTemplate for executing queries. */
24
    private JdbcTemplate jdbcTemplate = null;
25

    
26
    /**
27
     * Constructor.
28
     * @param jdbcTemplate - JdbcTemplate for executing queries.
29
     */
30
    public DatabaseInterface(JdbcTemplate jdbcTemplate) {
31
        this.jdbcTemplate = jdbcTemplate;
32
    }
33

    
34
    /**
35
     * Get list of table column names from SQL query.
36
     * @param sqlQuery - SQL query.
37
     * @return List of table column names. If null is returned, then it means that SQL query is not valid.
38
     */
39
    public List<String> getNameOfColumnsFromQuery(String sqlQuery) {
40
        try
41
        {
42
            // Try extract columns from database using SQL query.
43
            return jdbcTemplate.query(
44
                sqlQuery,
45
                new ResultSetExtractor<List<String>>() {
46
                    @Override
47
                    public List<String> extractData(ResultSet resultSet) throws SQLException {
48
                        List<String> tableColumnNames = new ArrayList<>();
49

    
50
                        ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
51
                        int columnCount = resultSetMetaData.getColumnCount();
52

    
53
                        // The column count starts from 1.
54
                        for (int i = 1; i <= columnCount; i++ ) {
55
                            String columnName = resultSetMetaData.getColumnName(i);
56
                            tableColumnNames.add(columnName.toLowerCase());
57
                        }
58

    
59
                        return tableColumnNames;
60
                    }
61
                }
62
            );
63

    
64
        }
65
        catch (DataAccessException e)
66
        {
67
            // If SQL query is not valid, then JdbcTemplate returned DataAccess exception.
68
            return null;
69
        }
70
    }
71

    
72
    /**
73
     * Validate given SQLQuery.
74
     * @param sqlQuery - Tested SQL Query.
75
     * @return true if SQL query is valid, false if not.
76
     */
77
    public boolean validateSQLQuery(String sqlQuery) {
78

    
79
        try
80
        {
81
            // Try validate SQL query.
82
            jdbcTemplate.execute("EXPLAIN PLAN FOR " + sqlQuery);
83
            return true;
84
        }
85
        catch (DataAccessException e)
86
        {
87
            // If SQL query is not valid, then JdbcTemplate returned DataAccess exception.
88
            return false;
89
        }
90
    }
91

    
92
    /**
93
     * Execute given SQL Query and return results as list of TableColumn class.
94
     * @param sqlQuery - SQL query.
95
     * @return List of results saved as TableColumn.
96
     */
97
    public Map<String, TableColumn> executeQueryAndReturnTableColumns(String sqlQuery) {
98
        return jdbcTemplate.query(
99
                sqlQuery,
100
                resultSet -> {
101
                    List<TableColumn> tableColumnsList = new ArrayList<>();
102

    
103
                    ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
104
                    int columnCount = resultSetMetaData.getColumnCount();
105

    
106
                    // The column count starts from 1.
107
                    for (int i = 1; i <= columnCount; i++ ) {
108
                        String columnName = resultSetMetaData.getColumnName(i);
109
                        tableColumnsList.add(new TableColumn(columnName));
110
                    }
111

    
112
                    // Add values from row to every column.
113
                    while(resultSet.next()) {
114
                        for (int i = 1; i <= columnCount; i++) {
115
                            tableColumnsList.get(i - 1).addValue(resultSet.getString(i));
116
                        }
117
                    }
118

    
119
                    Map<String, TableColumn> tableColumnMap = new HashMap<>();
120

    
121
                    // Create map, where key is tablecolumn name.
122
                    for (TableColumn column : tableColumnsList) {
123
                        tableColumnMap.put(column.getName(), column);
124
                    }
125

    
126
                    return tableColumnMap;
127
                }
128
        );
129
    }
130
}
    (1-1/1)