|
1
|
|
|
/* |
|
2
|
|
|
* |
|
3
|
|
|
* Copyright 2014-2020, Armenak Grigoryan, and individual contributors as indicated |
|
4
|
|
|
* by the @authors tag. See the copyright.txt in the distribution for a |
|
5
|
|
|
* full listing of individual contributors. |
|
6
|
|
|
* |
|
7
|
|
|
* This is free software; you can redistribute it and/or modify it |
|
8
|
|
|
* under the terms of the GNU Lesser General Public License as |
|
9
|
|
|
* published by the Free Software Foundation; either version 2.1 of |
|
10
|
|
|
* the License, or (at your option) any later version. |
|
11
|
|
|
* |
|
12
|
|
|
* This software is distributed in the hope that it will be useful, |
|
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
15
|
|
|
* Lesser General Public License for more details. |
|
16
|
|
|
* |
|
17
|
|
|
*/ |
|
18
|
|
|
package com.strider.datadefender.report; |
|
19
|
|
|
|
|
20
|
|
|
import com.strider.datadefender.DataDefenderException; |
|
21
|
|
|
import com.strider.datadefender.database.IDbFactory; |
|
22
|
|
|
import com.strider.datadefender.database.metadata.TableMetaData.ColumnMetaData; |
|
23
|
|
|
import com.strider.datadefender.database.sqlbuilder.ISqlBuilder; |
|
24
|
|
|
|
|
25
|
|
|
import java.io.IOException; |
|
26
|
|
|
import java.io.InputStream; |
|
27
|
|
|
import java.nio.charset.StandardCharsets; |
|
28
|
|
|
import java.sql.Clob; |
|
29
|
|
|
import java.sql.ResultSet; |
|
30
|
|
|
import java.sql.SQLException; |
|
31
|
|
|
import java.sql.Statement; |
|
32
|
|
|
import java.util.HashSet; |
|
33
|
|
|
import java.util.ArrayList; |
|
34
|
|
|
import java.util.List; |
|
35
|
|
|
|
|
36
|
|
|
import org.apache.commons.io.IOUtils; |
|
37
|
|
|
|
|
38
|
|
|
import lombok.extern.log4j.Log4j2; |
|
39
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* |
|
43
|
|
|
* @author Armenak Grigoryan |
|
44
|
|
|
*/ |
|
45
|
|
|
@Log4j2 |
|
46
|
|
|
public class ReportUtil { |
|
47
|
|
|
|
|
48
|
|
|
public static int rowCount(final IDbFactory factory, final String tableName) throws DataDefenderException { |
|
49
|
|
|
final ISqlBuilder sqlBuilder = factory.createSQLBuilder(); |
|
50
|
|
|
final String table = sqlBuilder.prefixSchema(tableName); |
|
51
|
|
|
|
|
52
|
|
|
// Getting number of records in the table |
|
53
|
|
|
final String queryCount = "SELECT count(*) FROM " + table; |
|
54
|
|
|
log.debug("Executing query against database: " + queryCount); |
|
55
|
|
|
|
|
56
|
|
|
int rowCount = 0; |
|
57
|
|
|
|
|
58
|
|
|
try (Statement stmt = factory.getConnection().createStatement(); |
|
59
|
|
|
ResultSet resultSet = stmt.executeQuery(queryCount);) { |
|
60
|
|
|
resultSet.next(); |
|
61
|
|
|
rowCount = resultSet.getInt(1); |
|
62
|
|
|
} catch (SQLException sqle) { |
|
63
|
|
|
log.error(sqle.toString()); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return rowCount; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public static List<String> sampleData(final IDbFactory factory, final ColumnMetaData metaData) throws IOException, DataDefenderException { |
|
70
|
|
|
final ISqlBuilder sqlBuilder = factory.createSQLBuilder(); |
|
71
|
|
|
String querySample = ""; |
|
72
|
|
|
String select = "SELECT "; |
|
73
|
|
|
|
|
74
|
|
|
|
|
75
|
|
|
|
|
76
|
|
|
if (!metaData.getColumnType().equals("CLOB") && !factory.getVendorName().equals("mssql")) { |
|
77
|
|
|
select = select + "DISTINCT "; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
querySample = sqlBuilder.buildSelectWithLimit( |
|
81
|
|
|
select + metaData.getColumnName() + " FROM " |
|
82
|
|
|
+ sqlBuilder.prefixSchema(metaData.getTable().getTableName()) |
|
83
|
|
|
+ " WHERE " + metaData.getColumnName() + " IS NOT NULL", |
|
84
|
|
|
5 |
|
85
|
|
|
); |
|
86
|
|
|
log.debug("Executing query against database: " + querySample); |
|
87
|
|
|
|
|
88
|
|
|
final List<String> sampleDataList = new ArrayList<>(); |
|
89
|
|
|
|
|
90
|
|
|
try (Statement stmt = factory.getConnection().createStatement(); |
|
91
|
|
|
ResultSet resultSet = stmt.executeQuery(querySample);) { |
|
92
|
|
|
while (resultSet.next()) { |
|
93
|
|
|
String tmp; |
|
94
|
|
|
if (metaData.getColumnType().equals("CLOB")) { |
|
95
|
|
|
Clob clob = resultSet.getClob(1); |
|
96
|
|
|
InputStream is = clob.getAsciiStream(); |
|
97
|
|
|
tmp = IOUtils.toString(is, StandardCharsets.UTF_8.name()); |
|
98
|
|
|
} else { |
|
99
|
|
|
tmp = resultSet.getString(1); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
if (StringUtils.isNotBlank(tmp)) { |
|
103
|
|
|
sampleDataList.add(tmp); |
|
104
|
|
|
tmp = null; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
} catch (SQLException sqle) { |
|
108
|
|
|
log.error(sqle.toString()); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
// Removing duplicates |
|
112
|
|
|
List<String> sampleDataListWithoutDuplicates = |
|
113
|
|
|
new ArrayList<>(new HashSet<>(sampleDataList)); |
|
114
|
|
|
|
|
115
|
|
|
return sampleDataListWithoutDuplicates; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|