1
|
|
|
/* |
2
|
|
|
* Copyright 2014, Armenak Grigoryan, and individual contributors as indicated |
3
|
|
|
* by the @authors tag. See the copyright.txt in the distribution for a |
4
|
|
|
* full listing of individual contributors. |
5
|
|
|
* |
6
|
|
|
* This is free software; you can redistribute it and/or modify it |
7
|
|
|
* under the terms of the GNU Lesser General Public License as |
8
|
|
|
* published by the Free Software Foundation; either version 2.1 of |
9
|
|
|
* the License, or (at your option) any later version. |
10
|
|
|
* |
11
|
|
|
* This software is distributed in the hope that it will be useful, |
12
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14
|
|
|
* Lesser General Public License for more details. |
15
|
|
|
*/ |
16
|
|
|
package com.strider.datadefender.database.metadata; |
17
|
|
|
|
18
|
|
|
import java.util.ArrayList; |
19
|
|
|
import java.util.Comparator; |
20
|
|
|
import java.util.List; |
21
|
|
|
import java.util.stream.Collectors; |
22
|
|
|
|
23
|
|
|
import org.apache.commons.lang3.StringUtils; |
24
|
|
|
|
25
|
|
|
import lombok.AccessLevel; |
26
|
|
|
import lombok.Data; |
27
|
|
|
import lombok.Setter; |
28
|
|
|
import lombok.extern.log4j.Log4j2; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Holds metadata for a table. |
32
|
|
|
* |
33
|
|
|
* @author Armenak Grigoryan |
34
|
|
|
*/ |
35
|
|
|
@Data |
36
|
|
|
@Log4j2 |
37
|
|
|
public class TableMetaData implements Comparable<TableMetaData> { |
38
|
|
|
|
39
|
|
|
@Data |
40
|
|
|
public class ColumnMetaData implements Comparable<ColumnMetaData> { |
41
|
|
|
|
42
|
|
|
private final int columnIndex; |
43
|
|
|
private final String columnName; |
44
|
|
|
private final Class columnType; |
45
|
|
|
private final int columnSize; |
46
|
|
|
private final boolean isPrimaryKey; |
47
|
|
|
private final boolean isForeignKey; |
48
|
|
|
private final String foreignKeyReference; |
49
|
|
|
|
50
|
|
|
@Override |
51
|
|
|
public int compareTo(ColumnMetaData t) { |
52
|
|
|
return Comparator |
53
|
|
|
.comparing(ColumnMetaData::getColumnIndex) |
54
|
|
|
.compare(this, t); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
@Override |
58
|
|
|
public String toString() { |
59
|
|
|
return TableMetaData.this.toString() + "." + columnName; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public TableMetaData getTable() { |
63
|
|
|
return TableMetaData.this; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
private final String schemaName; |
68
|
|
|
private final String tableName; |
69
|
|
|
@Setter(AccessLevel.NONE) |
70
|
|
|
private List<ColumnMetaData> columns = new ArrayList<>(); |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Filters and returns a list of columns that are designated as primary |
74
|
|
|
* keys. |
75
|
|
|
* @return |
76
|
|
|
*/ |
77
|
|
|
public List<ColumnMetaData> getPrimaryKeys() { |
78
|
|
|
return columns.stream().filter((c) -> c.isPrimaryKey).collect(Collectors.toList()); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Filters and returns a list of columns that are designated as foreign |
83
|
|
|
* keys. |
84
|
|
|
* @return |
85
|
|
|
*/ |
86
|
|
|
public List<ColumnMetaData> getForeignKeys() { |
87
|
|
|
return columns.stream().filter((c) -> c.isForeignKey).collect(Collectors.toList()); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Adds a ColumnMetaData to the list of columns. |
92
|
|
|
* |
93
|
|
|
* @param columnIndex |
94
|
|
|
* @param columnName |
95
|
|
|
* @param columnType |
96
|
|
|
* @param columnSize |
97
|
|
|
* @param isPrimaryKey |
98
|
|
|
* @param foreignKeyReference |
99
|
|
|
*/ |
100
|
1 |
|
public void addColumn( |
101
|
|
|
int columnIndex, |
102
|
|
|
String columnName, |
103
|
|
|
Class columnType, |
104
|
|
|
int columnSize, |
105
|
|
|
boolean isPrimaryKey, |
106
|
|
|
String foreignKeyReference |
107
|
|
|
) { |
108
|
1 |
|
columns.add(new ColumnMetaData( |
109
|
|
|
columnIndex, |
110
|
|
|
columnName, |
111
|
|
|
columnType, |
112
|
|
|
columnSize, |
113
|
|
|
isPrimaryKey, |
114
|
|
|
StringUtils.isNotBlank(foreignKeyReference), |
115
|
|
|
foreignKeyReference |
116
|
|
|
)); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Returns the column at the specified '1'-based index. |
121
|
|
|
* |
122
|
|
|
* @param index |
123
|
|
|
* @return |
124
|
|
|
*/ |
125
|
1 |
|
public ColumnMetaData getColumn(int index) { |
126
|
1 |
|
return columns.get(index - 1); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Returns the column with the specified name. |
131
|
|
|
* |
132
|
|
|
* @param name |
133
|
|
|
* @return |
134
|
|
|
*/ |
135
|
1 |
|
public ColumnMetaData getColumn(String name) { |
136
|
1 |
|
return columns |
137
|
|
|
.stream() |
138
|
|
|
.filter((c) -> StringUtils.equalsIgnoreCase(name, c.columnName)) |
139
|
|
|
.findAny() |
140
|
|
|
.orElse(null); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
@Override |
144
|
|
|
public int compareTo(TableMetaData t) { |
145
|
|
|
return Comparator |
146
|
|
|
.comparing(TableMetaData::getCanonicalTableName) |
147
|
|
|
.compare(this, t); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* If schemaName is set, returns {schemaName}.{tableName}, otherwise returns |
152
|
|
|
* {tableName} alone. |
153
|
|
|
* |
154
|
|
|
* @return |
155
|
|
|
*/ |
156
|
1 |
|
public String getCanonicalTableName() { |
157
|
2 |
|
if (StringUtils.isNotBlank(schemaName)) { |
158
|
1 |
|
return schemaName + "." + tableName; |
159
|
|
|
} |
160
|
|
|
return tableName; |
161
|
|
|
} |
162
|
|
|
|
163
|
1 |
|
@Override |
164
|
|
|
public String toString() { |
165
|
1 |
|
return getCanonicalTableName(); |
166
|
|
|
} |
167
|
|
|
} |