]> git.basschouten.com Git - openhab-addons.git/blob
18396b5912c24b00c32ff5da9e72358fe6085ee5
[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.Collections;
16 import java.util.List;
17 import java.util.StringJoiner;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
21
22 /**
23  * Query result
24  * 
25  * @author Joan Pujol - Initial contribution
26  */
27 @NonNullByDefault
28 public class QueryResult {
29     public static final QueryResult NO_RESULT = QueryResult.ofIncorrectResult("No result");
30
31     private final boolean correct;
32     private final @Nullable String errorMessage;
33     private final List<ResultRow> data;
34
35     private QueryResult(boolean correct, String errorMessage) {
36         this.correct = correct;
37         this.errorMessage = errorMessage;
38         this.data = Collections.emptyList();
39     }
40
41     private QueryResult(List<ResultRow> data) {
42         this.correct = true;
43         this.errorMessage = null;
44         this.data = data;
45     }
46
47     public static QueryResult ofIncorrectResult(String errorMessage) {
48         return new QueryResult(false, errorMessage);
49     }
50
51     public static QueryResult of(ResultRow... rows) {
52         return new QueryResult(List.of(rows));
53     }
54
55     public static QueryResult of(List<ResultRow> rows) {
56         return new QueryResult(rows);
57     }
58
59     public static QueryResult ofSingleValue(String columnName, Object value) {
60         return new QueryResult(List.of(new ResultRow(columnName, value)));
61     }
62
63     public boolean isCorrect() {
64         return correct;
65     }
66
67     public @Nullable String getErrorMessage() {
68         return errorMessage;
69     }
70
71     public List<ResultRow> getData() {
72         return data;
73     }
74
75     @Override
76     public String toString() {
77         return new StringJoiner(", ", QueryResult.class.getSimpleName() + "[", "]").add("correct=" + correct)
78                 .add("errorMessage='" + errorMessage + "'").add("data=" + data).toString();
79     }
80 }