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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
* _____________________________________________________________________________
*
* 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 it.inaf.ia2.tsm.datalayer.DatabaseType;
import it.inaf.ia2.tsm.datalayer.DBWrapper;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Utility class that contains static methods for managing {@link Column}s into
* the database.
*
* @author Sonia Zorba {@literal <zorba at oats.inaf.it>}
*/
public class DaoColumn {
//
// private static final Logger log = LoggerFactory.getLogger(DaoColumn.class);
//
// private static boolean equalsOneOf(String string, String... values) {
// for (String value : values) {
// if (string.equals(value)) {
// return true;
// }
// }
// return false;
// }
//
// /**
// * Returns the list of all the columns names given a schema name and a table
// * name, also if these objects have never been added into the TAP_SCHEMA.
// * This can be useful to retrieve the source database structure.
// */
// public static List<String> getAllColumnsNames(DBWrapper dbWrapper, TapSchema tapSchema, String schemaName, String tableSimpleName) throws SQLException {
// final List<String> allColumns = new ArrayList<>();
//
// DataSource dataSource = TSMUtil.getSchemaDataSource(dbWrapper, tapSchema, schemaName);
// DatabaseType dbType = TSMUtil.getSchemaDatabaseType(dbWrapper, tapSchema, schemaName);
//
// String query;
// if (dbType == DatabaseType.MYSQL) {
// query = String.format("SHOW COLUMNS FROM `%s`.`%s`", schemaName, tableSimpleName);
// } else if (dbType == DatabaseType.POSTGRES) {
// query = "SELECT column_name FROM information_schema.columns WHERE table_schema = '" + schemaName + "' AND table_name = '" + tableSimpleName + "'";
// } else {
// throw new UnsupportedOperationException("Database type " + dbType + " not supported");
// }
//
// log.debug("Executing query {}", query);
//
// try (Connection connection = dataSource.getConnection();
// Statement statement = connection.createStatement();
// ResultSet resultSet = statement.executeQuery(query)) {
//
// while (resultSet.next()) {
//
// String columnName;
// if (dbType == DatabaseType.MYSQL) {
// columnName = resultSet.getString("Field");
// } else if (dbType == DatabaseType.POSTGRES) {
// columnName = resultSet.getString("column_name");
// } else {
// throw new UnsupportedOperationException("Database type " + dbType + " not supported");
// }
//
// allColumns.add(columnName);
// }
// }
//
// return allColumns;
// }
//
// /**
// * For performance reasons all columns of a {@link Table} are loaded
// * together using this method. Columns {@link Status} is at first set as
// * {@code LOADED}.
// */
// protected static List<Column> loadAllTableColumns(DBWrapper dbWrapper, TapSchema tapSchema, String schemaName, Table table) throws SQLException {
// String tableSimpleName = table.getName();
// final List<Column> allColumns = new ArrayList<>();
//
// DataSource dataSource = TSMUtil.getSchemaDataSource(dbWrapper, tapSchema, schemaName);
// DatabaseType dbType = TSMUtil.getSchemaDatabaseType(dbWrapper, tapSchema, schemaName);
//
// String query;
// if (dbType == DatabaseType.MYSQL) {
// query = String.format("SHOW COLUMNS FROM `%s`.`%s`", schemaName, tableSimpleName);
// } else if (dbType == DatabaseType.POSTGRES) {
// query = "SELECT c.column_name, c.data_type, r.contype AS column_type, c.character_maximum_length, c.numeric_precision\n" //, c.numeric_precision_radix
// + "FROM information_schema.columns c\n"
// + "JOIN pg_catalog.pg_tables t ON c.table_schema = t.schemaname AND c.table_name = t.tablename\n"
// + "LEFT JOIN pg_catalog.pg_constraint r ON c.ordinal_position = ANY(r.conkey) AND r.conrelid = (t.schemaname || '.' || t.tablename)::regclass::oid\n"
// + "WHERE t.schemaname = '" + schemaName + "' AND t.tablename = '" + tableSimpleName + "'";
// } else {
// throw new UnsupportedOperationException("Database type " + dbType + " not supported");
// }
//
// log.debug("Executing query {}", query);
//
// try (Connection connection = dataSource.getConnection();
// Statement statement = connection.createStatement();
// ResultSet resultSet = statement.executeQuery(query)) {
//
// while (resultSet.next()) {
//
// String columnName;
// if (dbType == DatabaseType.MYSQL) {
// columnName = resultSet.getString("Field");
// } else if (dbType == DatabaseType.POSTGRES) {
// columnName = resultSet.getString("column_name");
// } else {
// throw new UnsupportedOperationException("Database type " + dbType + " not supported");
// }
//
// boolean indexed = false, primaryKey = false;
//
// // Key type
// if (dbType == DatabaseType.MYSQL) {
// String key = resultSet.getString("Key");
// primaryKey = key.equals("PRI");
// indexed = equalsOneOf(key, "PRI", "UNI", "MUL");
// } else if (dbType == DatabaseType.POSTGRES) {
// String columnType = resultSet.getString("column_type");
// if (columnType != null) {
// primaryKey = "p".equals(columnType);
// indexed = equalsOneOf(columnType, "p", "f", "u");
// }
// } else {
// throw new UnsupportedOperationException("Database type " + dbType + " not supported");
// }
//
// // Datatype and Size
// int size = 0;
// String datatype;
//
// if (dbType == DatabaseType.MYSQL) {
// String type = resultSet.getString("Type").toLowerCase();
//
// if (type.startsWith("int")) {
// datatype = "adql:INTEGER";
// } else if (type.startsWith("smallint")) {
// datatype = "adql:SMALLINT";
// } else if (type.startsWith("bigint")) {
// datatype = "adql:BIGINT";
// } else if (type.startsWith("float")) {
// datatype = "adql:REAL";
// } else if (type.startsWith("char")) {
// int beginIndex = type.indexOf('(');
// int endIndex = type.indexOf(')');
// size = Integer.parseInt(type.substring(beginIndex + 1, endIndex));
// datatype = "adql:CHAR";
// } else if (type.startsWith("varchar")) {
// int beginIndex = type.indexOf('(');
// int endIndex = type.indexOf(')');
// size = Integer.parseInt(type.substring(beginIndex + 1, endIndex));
// datatype = "adql:VARCHAR";
// } else if (type.contains("timestamp")) {
// datatype = "adql:TIMESTAMP";
// } else {
// datatype = "adql:" + type.toUpperCase();
// }
// } else if (dbType == DatabaseType.POSTGRES) {
// String type = resultSet.getString("data_type");
//
// if (type.startsWith("int")) {
// datatype = "adql:INTEGER";
// } else if (type.startsWith("smallint")) {
// datatype = "adql:SMALLINT";
// } else if (type.startsWith("bigint")) {
// datatype = "adql:BIGINT";
// } else if (type.startsWith("double") || type.startsWith("real")) {
// datatype = "adql:REAL";
// } else if (type.startsWith("character varying")) {
// datatype = "adql:VARCHAR";
// size = resultSet.getInt("character_maximum_length");
// } else if (type.startsWith("char")) {
// datatype = "adql:CHAR";
// size = resultSet.getInt("character_maximum_length");
// } else if (type.contains("timestamp")) {
// datatype = "adql:TIMESTAMP";
// } else {
// datatype = "adql:" + type.toUpperCase();
// }
// } else {
// throw new UnsupportedOperationException("Database type " + dbType + " not supported");
// }
//
// Integer arraySize = null; // TODO (v 1.1)
//
// Column column = new Column(dbWrapper, tapSchema, table, columnName, indexed, primaryKey, datatype, size, arraySize);
//
// allColumns.add(column);
// }
// }
//
// return allColumns;
// }
//
// /**
// * Retrieves saved {@code Column}s from the database and add them into the
// * specified {@code TapSchema}.
// */
// protected static void fillSavedColumns(DBWrapper dbWrapper, final TapSchema tapSchema) throws SQLException {
//
// log.debug("fillSavedColumns");
//
// SelectQueryBuilder selectQueryBuilder = new SelectQueryBuilder(dbWrapper.getTapSchemaDatabaseType(), tapSchema, TapSchema.COLUMNS_TABLE) {
//
// @Override
// protected TapSchemaEntity getEntity(ResultSet rs) throws SQLException {
// String tableCompleteName = rs.getString("table_name");
// String columnName = rs.getString("column_name");
//
// String[] tableNameSplit = tableCompleteName.split(Pattern.quote("."));
// String schemaName = tableNameSplit[0];
// String tableSimpleName = tableNameSplit[1];
//
// Schema schema = tapSchema.getChild(schemaName);
// if (schema == null) {
// return null;
// }
// Table table = schema.getChild(tableSimpleName);
// if (table == null) {
// return null;
// }
// Column column = table.addChild(columnName);
// if (column == null) {
// return null;
// }
// column.setStatus(Status.ADDED_PERSISTED);
// return column;
// }
// };
//
// selectQueryBuilder.executeQuery(dbWrapper.getTapSchemaConnection());
// }
//
// /**
// * Save a new {@code Column} into the TAP_SCHEMA schema.
// */
// protected static void insertNewColumn(DatabaseType dbType, Connection connection, TapSchema tapSchema, Column column) throws SQLException {
//
// log.debug("insertNewColumn");
//
// InsertQueryBuilder insertQueryBuilder = new InsertQueryBuilder(dbType, tapSchema, column, TapSchema.COLUMNS_TABLE);
// insertQueryBuilder.executeQuery(connection);
// }
//
// /**
// * Updates an existing {@code Column}.
// */
// protected static void updateColumn(DatabaseType dbType, Connection connection, TapSchema tapSchema, Column column) throws SQLException {
//
// UpdateQueryBuilder updateQueryBuilder = new UpdateQueryBuilder(dbType, tapSchema, column, TapSchema.COLUMNS_TABLE, "column_name = ? AND table_name = ?");
//
// String query = updateQueryBuilder.getQuery();
//
// try (PreparedStatement statement = connection.prepareStatement(query)) {
//
// log.debug("Executing query {}", query);
//
// int i = updateQueryBuilder.addStatementValues(statement);
// statement.setString(i, column.getName());
// statement.setString(i + 1, column.getTableCompleteName());
//
// statement.executeUpdate();
// }
// }
}