Browse Source

Query: add SQL string with parameters

pull/6392/head
Yuriy Lytvynchuk 4 years ago
parent
commit
cecbacfad4
  1. 5
      dao/src/main/java/org/thingsboard/server/dao/sql/query/DefaultQueryLogComponent.java
  2. 70
      dao/src/test/java/org/thingsboard/server/dao/sql/query/DefaultQueryLogComponentTest.java

5
dao/src/main/java/org/thingsboard/server/dao/sql/query/DefaultQueryLogComponent.java

@ -42,10 +42,7 @@ public class DefaultQueryLogComponent implements QueryLogComponent {
if (logSqlQueries && duration > logQueriesThreshold) {
String sqlToUse = substituteParametersInSqlString(query, ctx);
log.info("QUERY: {} took {} ms", query, duration);
log.info("QUERY SQL TO USE: {} took {} ms", sqlToUse, duration);
log.warn("SLOW QUERY took {} ms: {}", sqlToUse, duration);
Arrays.asList(ctx.getParameterNames()).forEach(param -> log.info("QUERY PARAM: {} -> {}", param, ctx.getValue(param)));
}
}

70
dao/src/test/java/org/thingsboard/server/dao/sql/query/DefaultQueryLogComponentTest.java

@ -1,47 +1,83 @@
/**
* Copyright © 2016-2022 The Thingsboard Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* 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
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.thingsboard.server.dao.sql.query;
import com.datastax.oss.driver.api.core.uuid.Uuids;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.mockito.BDDMockito;
import org.mockito.Mockito;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.context.junit4.SpringRunner;
import org.thingsboard.server.common.data.EntityType;
import org.thingsboard.server.common.data.id.TenantId;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.times;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = DefaultQueryLogComponent.class)
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = DefaultQueryLogComponent.class)
@EnableConfigurationProperties
@TestPropertySource(properties = {
"sql.log_queries=true",
"sql.log_queries_threshold:2999"
})
public class DefaultQueryLogComponentTest {
private TenantId tenantId;
private QueryContext ctx;
@Autowired
@SpyBean
private DefaultQueryLogComponent queryLog;
@Before
public void setUp() {
tenantId = new TenantId(Uuids.timeBased());
tenantId = new TenantId(UUID.fromString("97275c1c-9cf2-4d25-a68d-933031158f84"));
ctx = new QueryContext(new QuerySecurityContext(tenantId, null, EntityType.ALARM));
}
@Test
public void logQuery() {
BDDMockito.willCallRealMethod().given(queryLog).logQuery(ctx, "", 3000);
BDDMockito.willReturn("").given(queryLog).substituteParametersInSqlString("", ctx);
queryLog.logQuery(ctx, "", 3000);
Mockito.verify(queryLog, times(1)).substituteParametersInSqlString("", ctx);
}
@Test
public void substituteParametersInSqlString_StringType() {
String Name = "Mery's";
String id = "ID_1";
String sql = "Select * from Table Where name = :name AND id = :id";
String sqlToUse = "Select * from Table Where name = 'Mery''s' AND id = 'ID_1'";
ctx.addStringParameter("name", Name);
ctx.addStringParameter("id", id);
ctx.addStringParameter("name", "Mery's");
ctx.addStringParameter("id", "ID_1");
String sqlToUseResult = queryLog.substituteParametersInSqlString(sql, ctx);
assertEquals(sqlToUse, sqlToUseResult);
@ -65,11 +101,10 @@ public class DefaultQueryLogComponentTest {
@Test
public void substituteParametersInSqlString_BooleanType() {
boolean check = true;
String sql = "Select * from Table Where check = :check AND mark = :mark";
String sqlToUse = "Select * from Table Where check = true AND mark = false";
ctx.addBooleanParameter("check", check);
ctx.addBooleanParameter("check", true);
ctx.addBooleanParameter("mark", false);
String sqlToUseResult = queryLog.substituteParametersInSqlString(sql, ctx);
@ -79,7 +114,7 @@ public class DefaultQueryLogComponentTest {
@Test
public void substituteParametersInSqlString_UuidType() {
UUID guid = Uuids.timeBased();
UUID guid = UUID.randomUUID();
String sql = "Select * from Table Where guid = :guid";
String sqlToUse = "Select * from Table Where guid = '" + guid + "'";
@ -106,10 +141,7 @@ public class DefaultQueryLogComponentTest {
@Test
public void substituteParametersInSqlString_UuidListType() {
List<UUID> guids = new ArrayList<>();
guids.add(UUID.fromString("634a8d03-6871-4e01-94d0-876bf3e67dff"));
guids.add(UUID.fromString("3adbb5b8-4dc6-4faf-80dc-681a7b518b5e"));
guids.add(UUID.fromString("63a50f0c-2058-4d1d-8f15-812eb7f84412"));
List<UUID> guids = List.of(UUID.fromString("634a8d03-6871-4e01-94d0-876bf3e67dff"), UUID.fromString("3adbb5b8-4dc6-4faf-80dc-681a7b518b5e"), UUID.fromString("63a50f0c-2058-4d1d-8f15-812eb7f84412"));
String sql = "Select * from Table Where guid IN (:guids)";
String sqlToUse = "Select * from Table Where guid IN ('634a8d03-6871-4e01-94d0-876bf3e67dff', '3adbb5b8-4dc6-4faf-80dc-681a7b518b5e', '63a50f0c-2058-4d1d-8f15-812eb7f84412')";

Loading…
Cancel
Save