Skip to content

Commit

Permalink
Merge branch 'feature/bugfix_vocabulary_creation' into 'develop'
Browse files Browse the repository at this point in the history
feature/bugfix_vocabulary_creation

See merge request upm-inesdata/inesdata-connector!32
  • Loading branch information
Samuel Sierra Silva committed Aug 1, 2024
2 parents 8f17da7 + b87d45b commit 5403d0a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,10 @@ public StoreResult<Void> create(Vocabulary vocabulary) {
Objects.requireNonNull(vocabulary);

var vocabularyId = vocabulary.getId();
var connectorId = vocabulary.getConnectorId();
return transactionContext.execute(() -> {
try (var connection = getConnection()) {
if (existsById(vocabularyId, connection)) {
if (existsByIdAndConnectorId(vocabularyId, connectorId, connection)) {
var msg = format(VocabularyIndex.VOCABULARY_EXISTS_TEMPLATE, vocabularyId);
return StoreResult.alreadyExists(msg);
}
Expand Down Expand Up @@ -118,7 +119,8 @@ public StoreResult<Vocabulary> updateVocabulary(Vocabulary vocabulary) {
return transactionContext.execute(() -> {
try (var connection = getConnection()) {
var vocabularyId = vocabulary.getId();
if (existsById(vocabularyId, connection)) {
var connectorId = vocabulary.getConnectorId();
if (existsByIdAndConnectorId(vocabularyId, connectorId, connection)) {
queryExecutor.execute(connection, vocabularyStatements.getUpdateVocabularyTemplate(),
vocabulary.getName(),
toJson(vocabulary.getJsonSchema()),
Expand Down Expand Up @@ -167,9 +169,9 @@ private int mapRowCount(ResultSet resultSet) throws SQLException {
return resultSet.getInt(vocabularyStatements.getCountVariableName());
}

private boolean existsById(String vocabularyId, Connection connection) {
var sql = vocabularyStatements.getCountVocabularyByIdClause();
try (var stream = queryExecutor.query(connection, false, this::mapRowCount, sql, vocabularyId)) {
private boolean existsByIdAndConnectorId(String vocabularyId, String connectorId, Connection connection) {
var sql = vocabularyStatements.getCountVocabularyByIdAndConnectorIdClause();
try (var stream = queryExecutor.query(connection, false, this::mapRowCount, sql, vocabularyId, connectorId)) {
return stream.findFirst().orElse(0) > 0;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,12 @@ public String getUpdateVocabularyTemplate() {
}

@Override
public String getCountVocabularyByIdClause() {
return format("SELECT COUNT(*) AS %s FROM %s WHERE %s = ?",
public String getCountVocabularyByIdAndConnectorIdClause() {
return format("SELECT COUNT(*) AS %s FROM %s WHERE %s = ? AND %s = ?",
getCountVariableName(),
getVocabularyTable(),
getVocabularyIdColumn());
getVocabularyIdColumn(),
getConnectorIdColumn());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ default String getCreatedAtColumn() {
/**
* SELECT COUNT clause for vocabularys.
*/
String getCountVocabularyByIdClause();
String getCountVocabularyByIdAndConnectorIdClause();

/**
* SELECT clause for all vocabularys.
Expand Down

0 comments on commit 5403d0a

Please sign in to comment.