Skip to content

Commit

Permalink
fix select order
Browse files Browse the repository at this point in the history
  • Loading branch information
aomegax committed Mar 11, 2024
1 parent 88c084f commit 5140374
Showing 1 changed file with 10 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
import javax.persistence.Query;
import org.hibernate.query.internal.NativeQueryImpl;
import org.hibernate.transform.AliasToEntityMapResultTransformer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -22,6 +24,9 @@ public class GenericQueryService {
@Value("${dangerous.sql.keywords}")
private List<String> dangerousKeywordsList;

@Autowired
private JdbcTemplate jdbcTemplate;

@Transactional
public List getQueryResponse(String query) {
String sanitizedQuery = sanitizeQuery(query);
Expand All @@ -31,8 +36,11 @@ public List getQueryResponse(String query) {
nativeQuery.setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE);
if(modifying(query)){
return Collections.singletonList(nativeQuery.executeUpdate());
}else{
return nativeQuery.getResultList();
}
else {
// return nativeQuery.getResultList();
// necessary to maintain the select column order
return jdbcTemplate.queryForList(query);
}
} catch (PersistenceException pExc) {
throw new AppException(AppError.WRONG_QUERY_GRAMMAR);
Expand Down

0 comments on commit 5140374

Please sign in to comment.