Newer
Older
/*
* _____________________________________________________________________________
*
* 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.
*/
Sonia Zorba
committed
package it.inaf.ia2.tsm.api;
Sonia Zorba
committed
import it.inaf.ia2.tsm.api.contract.DatabaseType;
import it.inaf.ia2.tsm.api.contract.TapSchema;
import it.inaf.ia2.tsm.api.contract.TapSchemaEntity;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Prepares an {@code INSERT} SQL query for a given {@link TapSchemaEntity} and
* a given {@link TapSchema}.
*
* @author Sonia Zorba {@literal <zorba at oats.inaf.it>}
*/
public class InsertQueryBuilder {
private static final Logger log = LoggerFactory.getLogger(InsertQueryBuilder.class);
private final String query;
private final List<EntityPropertyInfo> addedProperties;
private final TapSchemaEntity tapSchemaEntity;
protected InsertQueryBuilder(DatabaseType dbType, TapSchema tapSchema, TapSchemaEntity tapSchemaEntity, String tapSchemaTableName) {
StringBuilder querySb = new StringBuilder("INSERT INTO ");
querySb.append(TSMUtil.escapeName(tapSchema.getName(), dbType));
querySb.append(".");
querySb.append(TSMUtil.escapeName(tapSchemaTableName, dbType));
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
querySb.append(" (");
addedProperties = new ArrayList<>();
this.tapSchemaEntity = tapSchemaEntity;
boolean first = true;
for (EntityPropertyInfo propertyInfo : EntityPropertyInfo.getEntityPropertiesInfo(tapSchemaTableName)) {
if (propertyInfo.acceptVersion(tapSchema.getVersion())) {
if (!first) {
querySb.append(", ");
}
querySb.append(propertyInfo.getPropertyKey());
addedProperties.add(propertyInfo);
first = false;
}
}
querySb.append(") VALUES (");
for (int i = 0; i < addedProperties.size(); i++) {
if (i > 0) {
querySb.append(",");
}
querySb.append("?");
}
querySb.append(")");
query = querySb.toString();
}
protected void executeQuery(Connection connection) throws SQLException {
try (PreparedStatement statement = connection.prepareStatement(query)) {
log.debug("Executing query {}", query);
int i = 1;
for (EntityPropertyInfo property : addedProperties) {
Object value = tapSchemaEntity.getValue(property.getPropertyKey(), property.getPropertyType());
//log.debug(" [{}] {} ({})", i, value, property.getSqlType());
statement.setObject(i, value, property.getSqlType());
i++;
}
statement.executeUpdate();
}
}
}