|
|
|
@ -5,7 +5,7 @@ |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
* You may obtain a copy of the License at |
|
|
|
* |
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
* |
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
|
|
@ -18,6 +18,7 @@ package org.thingsboard.server.dao.sql.query; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
|
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; |
|
|
|
import org.springframework.stereotype.Repository; |
|
|
|
import org.springframework.transaction.TransactionStatus; |
|
|
|
@ -223,6 +224,9 @@ public class DefaultEntityQueryRepository implements EntityQueryRepository { |
|
|
|
private final NamedParameterJdbcTemplate jdbcTemplate; |
|
|
|
private final TransactionTemplate transactionTemplate; |
|
|
|
|
|
|
|
@Value("${sql.log_entity_queries:false}") |
|
|
|
private boolean logSqlQueries; |
|
|
|
|
|
|
|
public DefaultEntityQueryRepository(NamedParameterJdbcTemplate jdbcTemplate, TransactionTemplate transactionTemplate) { |
|
|
|
this.jdbcTemplate = jdbcTemplate; |
|
|
|
this.transactionTemplate = transactionTemplate; |
|
|
|
@ -236,8 +240,10 @@ public class DefaultEntityQueryRepository implements EntityQueryRepository { |
|
|
|
ctx.append(addEntityTableQuery(ctx, query.getEntityFilter())); |
|
|
|
ctx.append(" e where "); |
|
|
|
ctx.append(buildEntityWhere(ctx, query.getEntityFilter(), Collections.emptyList())); |
|
|
|
// log.error("QUERY: {}", ctx.getQuery());
|
|
|
|
// Arrays.asList(ctx.getParameterNames()).forEach(param -> log.error("QUERY PARAM: {}->{}", param, ctx.getValue(param)));
|
|
|
|
if (logSqlQueries) { |
|
|
|
log.info("QUERY: {}", ctx.getQuery()); |
|
|
|
Arrays.asList(ctx.getParameterNames()).forEach(param -> log.info("QUERY PARAM: {}->{}", param, ctx.getValue(param))); |
|
|
|
} |
|
|
|
return transactionTemplate.execute(status -> jdbcTemplate.queryForObject(ctx.getQuery(), ctx, Long.class)); |
|
|
|
} |
|
|
|
|
|
|
|
@ -331,8 +337,10 @@ public class DefaultEntityQueryRepository implements EntityQueryRepository { |
|
|
|
if (pageLink.getPageSize() > 0) { |
|
|
|
dataQuery = String.format("%s limit %s offset %s", dataQuery, pageLink.getPageSize(), startIndex); |
|
|
|
} |
|
|
|
// log.error("QUERY: {}", dataQuery);
|
|
|
|
// Arrays.asList(ctx.getParameterNames()).forEach(param -> log.error("QUERY PARAM: {}->{}", param, ctx.getValue(param)));
|
|
|
|
if (logSqlQueries) { |
|
|
|
log.error("QUERY: {}", dataQuery); |
|
|
|
Arrays.asList(ctx.getParameterNames()).forEach(param -> log.error("QUERY PARAM: {}->{}", param, ctx.getValue(param))); |
|
|
|
} |
|
|
|
List<Map<String, Object>> rows = jdbcTemplate.queryForList(dataQuery, ctx); |
|
|
|
return EntityDataAdapter.createEntityData(pageLink, selectionMapping, rows, totalElements); |
|
|
|
}); |
|
|
|
|