Skip to content

Commit cecbacf

Browse files
author
Yuriy Lytvynchuk
committed
Query: add SQL string with parameters
1 parent 1a4cf21 commit cecbacf

File tree

2 files changed

+52
-23
lines changed

2 files changed

+52
-23
lines changed

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,7 @@ public void logQuery(QueryContext ctx, String query, long duration) {
4242
if (logSqlQueries && duration > logQueriesThreshold) {
4343

4444
String sqlToUse = substituteParametersInSqlString(query, ctx);
45-
46-
log.info("QUERY: {} took {} ms", query, duration);
47-
log.info("QUERY SQL TO USE: {} took {} ms", sqlToUse, duration);
48-
45+
log.warn("SLOW QUERY took {} ms: {}", sqlToUse, duration);
4946
Arrays.asList(ctx.getParameterNames()).forEach(param -> log.info("QUERY PARAM: {} -> {}", param, ctx.getValue(param)));
5047
}
5148
}

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

Lines changed: 51 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,83 @@
1-
1+
/**
2+
* Copyright © 2016-2022 The Thingsboard Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
216
package org.thingsboard.server.dao.sql.query;
317

4-
import com.datastax.oss.driver.api.core.uuid.Uuids;
518
import org.junit.Before;
619
import org.junit.Test;
20+
import org.junit.jupiter.api.extension.ExtendWith;
721
import org.junit.runner.RunWith;
8-
import org.springframework.beans.factory.annotation.Autowired;
9-
import org.springframework.boot.test.context.SpringBootTest;
22+
import org.mockito.BDDMockito;
23+
import org.mockito.Mockito;
24+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
25+
import org.springframework.boot.test.mock.mockito.SpyBean;
26+
import org.springframework.test.context.ContextConfiguration;
27+
import org.springframework.test.context.TestPropertySource;
28+
import org.springframework.test.context.junit.jupiter.SpringExtension;
1029
import org.springframework.test.context.junit4.SpringRunner;
1130
import org.thingsboard.server.common.data.EntityType;
1231
import org.thingsboard.server.common.data.id.TenantId;
13-
import java.util.ArrayList;
32+
1433
import java.util.List;
1534
import java.util.UUID;
1635

1736
import static org.junit.Assert.assertEquals;
37+
import static org.mockito.Mockito.times;
1838

1939
@RunWith(SpringRunner.class)
20-
@SpringBootTest(classes = DefaultQueryLogComponent.class)
40+
@ExtendWith(SpringExtension.class)
41+
@ContextConfiguration(classes = DefaultQueryLogComponent.class)
42+
@EnableConfigurationProperties
43+
@TestPropertySource(properties = {
44+
"sql.log_queries=true",
45+
"sql.log_queries_threshold:2999"
46+
})
47+
2148
public class DefaultQueryLogComponentTest {
2249

2350
private TenantId tenantId;
2451
private QueryContext ctx;
2552

26-
@Autowired
53+
@SpyBean
2754
private DefaultQueryLogComponent queryLog;
2855

2956
@Before
3057
public void setUp() {
31-
tenantId = new TenantId(Uuids.timeBased());
58+
tenantId = new TenantId(UUID.fromString("97275c1c-9cf2-4d25-a68d-933031158f84"));
3259
ctx = new QueryContext(new QuerySecurityContext(tenantId, null, EntityType.ALARM));
3360
}
3461

62+
@Test
63+
public void logQuery() {
64+
65+
BDDMockito.willCallRealMethod().given(queryLog).logQuery(ctx, "", 3000);
66+
BDDMockito.willReturn("").given(queryLog).substituteParametersInSqlString("", ctx);
67+
queryLog.logQuery(ctx, "", 3000);
68+
69+
Mockito.verify(queryLog, times(1)).substituteParametersInSqlString("", ctx);
70+
71+
}
72+
3573
@Test
3674
public void substituteParametersInSqlString_StringType() {
3775

38-
String Name = "Mery's";
39-
String id = "ID_1";
4076
String sql = "Select * from Table Where name = :name AND id = :id";
4177
String sqlToUse = "Select * from Table Where name = 'Mery''s' AND id = 'ID_1'";
4278

43-
ctx.addStringParameter("name", Name);
44-
ctx.addStringParameter("id", id);
79+
ctx.addStringParameter("name", "Mery's");
80+
ctx.addStringParameter("id", "ID_1");
4581

4682
String sqlToUseResult = queryLog.substituteParametersInSqlString(sql, ctx);
4783
assertEquals(sqlToUse, sqlToUseResult);
@@ -65,11 +101,10 @@ public void substituteParametersInSqlString_DoubleLongType() {
65101
@Test
66102
public void substituteParametersInSqlString_BooleanType() {
67103

68-
boolean check = true;
69104
String sql = "Select * from Table Where check = :check AND mark = :mark";
70105
String sqlToUse = "Select * from Table Where check = true AND mark = false";
71106

72-
ctx.addBooleanParameter("check", check);
107+
ctx.addBooleanParameter("check", true);
73108
ctx.addBooleanParameter("mark", false);
74109

75110
String sqlToUseResult = queryLog.substituteParametersInSqlString(sql, ctx);
@@ -79,7 +114,7 @@ public void substituteParametersInSqlString_BooleanType() {
79114
@Test
80115
public void substituteParametersInSqlString_UuidType() {
81116

82-
UUID guid = Uuids.timeBased();
117+
UUID guid = UUID.randomUUID();
83118
String sql = "Select * from Table Where guid = :guid";
84119
String sqlToUse = "Select * from Table Where guid = '" + guid + "'";
85120

@@ -106,10 +141,7 @@ public void substituteParametersInSqlString_StringListType() {
106141
@Test
107142
public void substituteParametersInSqlString_UuidListType() {
108143

109-
List<UUID> guids = new ArrayList<>();
110-
guids.add(UUID.fromString("634a8d03-6871-4e01-94d0-876bf3e67dff"));
111-
guids.add(UUID.fromString("3adbb5b8-4dc6-4faf-80dc-681a7b518b5e"));
112-
guids.add(UUID.fromString("63a50f0c-2058-4d1d-8f15-812eb7f84412"));
144+
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"));
113145

114146
String sql = "Select * from Table Where guid IN (:guids)";
115147
String sqlToUse = "Select * from Table Where guid IN ('634a8d03-6871-4e01-94d0-876bf3e67dff', '3adbb5b8-4dc6-4faf-80dc-681a7b518b5e', '63a50f0c-2058-4d1d-8f15-812eb7f84412')";

0 commit comments

Comments
 (0)