Skip to content
DBBrokerTemplate.java 43.8 KiB
Newer Older
                        ps.executeUpdate();
                    }
                }
                for (String schema : consistencyChecks.getUnexisingSchemas()) {
                    query = String.format("DELETE FROM %s.%s WHERE table_name LIKE ?", tapSchemaNameEscaped, escape(TapSchema.COLUMNS_TABLE));
                    try (PreparedStatement ps = conn.prepareStatement(query)) {
                        ps.setString(1, schema + "%");
                        LOG.debug("Executing query {} [{}%]", query, schema);
                        ps.executeUpdate();
                    }
                }

                // Removing all tables
                for (String table : consistencyChecks.getUnexistingTables()) {
                    query = String.format("DELETE FROM %s.%s WHERE table_name = ?", tapSchemaNameEscaped, escape(TapSchema.TABLES_TABLE));
                    try (PreparedStatement ps = conn.prepareStatement(query)) {
                        ps.setString(1, table);
                        LOG.debug("Executing query {} [{}]", query, table);
                        ps.executeUpdate();
                    }
                }
                for (String schema : consistencyChecks.getUnexisingSchemas()) {
                    query = String.format("DELETE FROM %s.%s WHERE schema_name = ?", tapSchemaNameEscaped, escape(TapSchema.TABLES_TABLE));
                    try (PreparedStatement ps = conn.prepareStatement(query)) {
                        ps.setString(1, schema);
                        LOG.debug("Executing query {} [{}]", query, schema);
                        ps.executeUpdate();
                    }
                }

                // Removing all schemas
                for (String schema : consistencyChecks.getUnexisingSchemas()) {
                    query = String.format("DELETE FROM %s.%s WHERE schema_name = ?", tapSchemaNameEscaped, escape(TapSchema.SCHEMAS_TABLE));
                    try (PreparedStatement ps = conn.prepareStatement(query)) {
                        ps.setString(1, schema);
                        LOG.debug("Executing query {} [{}]", query, schema);
                        ps.executeUpdate();
                    }
                }

                conn.commit();
            } catch (SQLException e) {
                LOG.error("Exception detected. Executing rollback!", e);
                try {
                    conn.rollback();
                    conn.setAutoCommit(true);
                } catch (SQLException er) {
                    LOG.error("Exception during rollback", er);
                    throw er;
                }
            }
        }
    }

    @Override
    public void updateTapSchemaColumnValue(String tapSchemaName, String completeTableName, String columnName, String key, Object value) throws SQLException {

        String query = String.format("UPDATE %s.%s SET %s = ? WHERE table_name = ? and column_name = ?",
                escape(tapSchemaName), escape(TapSchema.COLUMNS_TABLE), escape(key));

        try (Connection conn = dataSource.getConnection();
                PreparedStatement ps = conn.prepareStatement(query)) {

            LOG.debug("Executing query {} [{}, {}, {}]", query, value, completeTableName, columnName);

            ps.setObject(1, value);
            ps.setString(2, completeTableName);
            ps.setString(3, columnName);

            ps.executeUpdate();
        }
    }

    protected String getEnumDefinition(List<String> enumValues) {
        StringBuilder sb = new StringBuilder();

        boolean first = true;
        for (String enumValue : enumValues) {
            if (!first) {
                sb.append(',');
            }
            sb.append(String.format("'%s'", enumValue));
            first = false;
        }

        return sb.toString();
    }

    private String getCheckConstraint(ColumnModel columnModel) {
        if (columnModel.getMinValue() != null && columnModel.getMaxValue() != null) {
            return String.format("CHECK (%s >= %s AND %s <= %s)", columnModel.getName(),
                    columnModel.getMinValue(), columnModel.getName(), columnModel.getMaxValue());
        } else if (columnModel.getMinValue() != null && columnModel.getMaxValue() == null) {
            return String.format("CHECK (%s >= %s)", columnModel.getName(), columnModel.getMinValue());
        } else if (columnModel.getMinValue() == null && columnModel.getMaxValue() != null) {
            return String.format("CHECK (%s <= %s)", columnModel.getName(), columnModel.getMaxValue());
        }
        return null;
    }

    protected void appendCheckConstraints(StringBuilder querySb, TableModel tableModel) {
        for (ColumnModel cm : tableModel.getColumns()) {
            String checkConstraint = getCheckConstraint(cm);
            if (checkConstraint != null) {
                querySb.append(",\n");
                querySb.append(checkConstraint);
            }
        }
    }

    protected String getExposedSchemaName(String schemaName, String realSchemaName, String keySchemaName) {
        if (keySchemaName.equals(realSchemaName)) {
            return schemaName;
        }
        return keySchemaName;
    }

    protected String getArraysize(ADQL adqlType, Integer size) {
        String arraySize = null;

        if (size != null) {
            arraySize = String.valueOf(size);
        }
        if (adqlType != null && ADQL.isVariable(adqlType)) {
            // variable length columns must have a "*" symbol on arraysize
            if (arraySize == null) {
                arraySize = "*";
            } else {
                arraySize += "*";
            }
        }

        return arraySize;
    }