]> git.basschouten.com Git - openhab-addons.git/blob
0280db2e273291080e83e8eee5845e5770b24cc3
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
7  * This program and the accompanying materials are made available under the
8  * terms of the Eclipse Public License 2.0 which is available at
9  * http://www.eclipse.org/legal/epl-2.0
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.dbquery.internal.domain;
14
15 import java.util.Objects;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.openhab.binding.dbquery.internal.config.QueryConfiguration;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 /**
24  * Extracts a result from {@link QueryResult} to a single value to be used in channels
25  * (after being converted that it's not responsability of this class)
26  *
27  * @author Joan Pujol - Initial contribution
28  */
29 @NonNullByDefault
30 public class QueryResultExtractor {
31     private final Logger logger = LoggerFactory.getLogger(QueryResultExtractor.class);
32     private final QueryConfiguration config;
33
34     public QueryResultExtractor(QueryConfiguration config) {
35         this.config = config;
36     }
37
38     public ResultValue extractResult(QueryResult queryResult) {
39         if (queryResult.isCorrect()) {
40             if (config.isScalarResult()) {
41                 return getScalarValue(queryResult);
42             } else {
43                 return ResultValue.of(queryResult);
44             }
45         } else {
46             return ResultValue.incorrect();
47         }
48     }
49
50     private ResultValue getScalarValue(QueryResult queryResult) {
51         if (validateHasScalarValue(queryResult)) {
52             var row = queryResult.getData().get(0);
53             @Nullable
54             Object value;
55             if (config.isScalarColumnDefined()) {
56                 value = row.getValue(Objects.requireNonNull(config.getScalarColumn()));
57             } else {
58                 value = row.getValue(row.getColumnNames().iterator().next());
59             }
60             return ResultValue.of(value);
61         } else {
62             return ResultValue.incorrect();
63         }
64     }
65
66     private boolean validateHasScalarValue(QueryResult queryResult) {
67         boolean valid = false;
68         String baseErrorMessage = "Can't get scalar value for result: ";
69         if (queryResult.isCorrect()) {
70             if (queryResult.getData().size() == 1) {
71                 boolean oneColumn = queryResult.getData().get(0).getColumnsSize() == 1;
72                 if (oneColumn || config.isScalarColumnDefined()) {
73                     valid = true;
74                 } else {
75                     logger.warn("{} Columns size is {} and scalarColumn isn't defined", baseErrorMessage,
76                             queryResult.getData().get(0).getColumnNames().size());
77                 }
78             } else {
79                 logger.warn("{} Rows size is {}", baseErrorMessage, queryResult.getData().size());
80             }
81         } else {
82             logger.debug("{} Incorrect result", baseErrorMessage);
83         }
84         return valid;
85     }
86 }