Skip to content
Snippets Groups Projects
Commit 1fe7ef31 authored by Moritz Staudinger's avatar Moritz Staudinger
Browse files

Added save functionality to query, fixed bug in reexecution

Former-commit-id: 8f86a0e4
parent bd8e47f7
No related branches found
No related tags found
1 merge request!42Fixed the query service tests
package at.tuwien.endpoint;
import at.tuwien.api.database.query.ExecuteQueryDto;
import at.tuwien.api.database.query.QueryDto;
import at.tuwien.api.database.query.QueryResultDto;
import at.tuwien.entities.database.query.Query;
import at.tuwien.exception.*;
......@@ -50,6 +51,21 @@ public class QueryEndpoint {
return ResponseEntity.ok(response);
}
@Transactional
@PutMapping("/save")
@ApiOperation(value = "saves a query without execution")
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Executed the query, Saved it and return the results"),
@ApiResponse(code = 404, message = "The database does not exist."),
@ApiResponse(code = 405, message = "The container is not running."),
@ApiResponse(code = 409, message = "The container image is not supported."),})
public ResponseEntity<QueryResultDto> save(@PathVariable Long id, @RequestBody ExecuteQueryDto dto)
throws DatabaseNotFoundException, ImageNotSupportedException, SQLException,
JSQLParserException, QueryMalformedException, QueryStoreException {
final QueryResultDto response = queryService.save(id, queryMapper.queryDTOtoQuery(dto));
return ResponseEntity.ok(response);
}
@Transactional
@GetMapping("/query/{queryId}")
@ApiOperation(value = "re-executes a query")
......
......@@ -191,6 +191,7 @@ public class QueryService extends JdbcConnector {
query.append("' WHERE");
query.append(split[1]);
query.append(") as tab");
}
} else {
query.append(q);
......@@ -221,4 +222,33 @@ public class QueryService extends JdbcConnector {
return queryResultDto;
}
@Transactional
public QueryResultDto save(Long id, Query query) throws SQLException, ImageNotSupportedException, DatabaseNotFoundException, QueryStoreException, JSQLParserException {
Database database = findDatabase(id);
if(database.getContainer().getImage().getDialect().equals("MARIADB")){
if(!queryStoreService.exists(database)) {
queryStoreService.create(id);
}
}
DSLContext context = open(database);
StringBuilder parsedQuery = new StringBuilder();
String q = parse(query, database).getQuery();
if(q.charAt(q.length()-1) == ';') {
parsedQuery.append(q.substring(0, q.length()-2));
} else {
parsedQuery.append(q);
}
parsedQuery.append(";");
ResultQuery<Record> resultQuery = context.resultQuery(parsedQuery.toString());
Result<Record> result = resultQuery.fetch();
QueryResultDto queryResultDto = queryMapper.recordListToQueryResultDto(result);
log.debug("Result of the query is: \n {}", result.toString());
// Save the query in the store
boolean b = queryStoreService.saveQuery(database, query, queryResultDto);
log.debug("Save query returned code {}", b);
QueryResultDto savedQuery = queryStoreService.findLast(database.getId());
return savedQuery;
}
}
......@@ -66,6 +66,17 @@ public class QueryStoreService extends JdbcConnector {
.fetch());
}
public QueryResultDto findLast(Long databaseId) throws DatabaseNotFoundException, SQLException, ImageNotSupportedException {
Database database = findDatabase(databaseId);
DSLContext context = open(database);
StringBuilder sb = new StringBuilder();
sb.append("SELECT * FROM ");
sb.append(QUERYSTORENAME);
sb.append(" ORDER BY id desc LIMIT 1;");
return queryMapper.recordListToQueryResultDto(context
.fetch(sb.toString()));
}
/**
* Creates the querystore for a given database
* @param databaseId
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment