Newer
Older
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
/*
* _____________________________________________________________________________
*
* INAF - OATS National Institute for Astrophysics - Astronomical Observatory of
* Trieste INAF - IA2 Italian Center for Astronomical Archives
* _____________________________________________________________________________
*
* Copyright (C) 2016 Istituto Nazionale di Astrofisica
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License Version 3 as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package it.inaf.ia2.tsm;
import java.util.Objects;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Sonia Zorba {@literal <zorba at oats.inaf.it>}
*/
public class Column extends ChildEntity<Table> {
public final static String TABLE_NAME_KEY = "table_name";
public final static String COLUMN_NAME_KEY = "column_name";
public final static String DATATYPE_KEY = "datatype";
public final static String SIZE_KEY = "size";
public final static String ARRAYSIZE_KEY = "arraysize";
public final static String INDEXED_KEY = "indexed";
public final static String PRIMARY_KEY = "primary_key";
private static final long serialVersionUID = 9175956487892235521L;
private static final Logger LOG = LoggerFactory.getLogger(Column.class);
private boolean foreignKeySearched;
private Key foreignKey;
private Table parentTable;
private Column() {
// for serialization
super();
}
protected Column(TapSchema tapSchema, Table table, String columnName) {
super(tapSchema, tapSchema.getTableModel(TapSchema.COLUMNS_TABLE), table.getColumnMetadata(columnName));
parentTable = table;
setStatus(Status.LOADED);
}
public Key getForeignKey() {
if (!foreignKeySearched) { // lazy loading (but the foreignKey value can be null, so we use this boolean)
String fromSchemaName = parentTable.getParent().getName();
String fromTableName = parentTable.getName();
String fromColumnName = getName();
ext:
for (Key key : tapSchema.getAllKeys()) {
if (key.getFromSchemaName().equals(fromSchemaName) && key.getFromTableSimpleName().equals(fromTableName)) {
for (KeyColumn keyColumn : key.getKeyColumns()) {
if (keyColumn.getFromColumn().equals(fromColumnName)) {
foreignKey = key;
break ext;
}
}
}
}
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
}
return foreignKey;
}
public String getTableCompleteName() {
return getValue(TABLE_NAME_KEY, String.class);
}
@Override
public String getName() {
return getValue(COLUMN_NAME_KEY, String.class);
}
public boolean getIndexed() {
return getValue(INDEXED_KEY, Boolean.class);
}
/**
* This information is not stored into the TAP_SCHEMA, so it will be
* reloaded from the source schema each time.
*/
public boolean isPrimaryKey() {
return (boolean) getMetadata(PRIMARY_KEY);
}
@Override
public int hashCode() {
int hash = 7;
hash = 29 * hash + Objects.hashCode(getTableCompleteName());
hash = 29 * hash + Objects.hashCode(getName());
return hash;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Column other = (Column) obj;
if (!Objects.equals(this.getTableCompleteName(), other.getTableCompleteName())) {
return false;
}
return Objects.equals(this.getName(), other.getName());
}
/**
* {@inheritDoc}
*/
@Override
public Table getParent() {
return parentTable;
}
}