2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
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
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.dbquery.internal.domain;
15 import java.util.Collections;
16 import java.util.List;
17 import java.util.StringJoiner;
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20 import org.eclipse.jdt.annotation.Nullable;
25 * @author Joan Pujol - Initial contribution
28 public class QueryResult {
29 public static final QueryResult NO_RESULT = QueryResult.ofIncorrectResult("No result");
31 private final boolean correct;
32 private final @Nullable String errorMessage;
33 private final List<ResultRow> data;
35 private QueryResult(boolean correct, String errorMessage) {
36 this.correct = correct;
37 this.errorMessage = errorMessage;
38 this.data = Collections.emptyList();
41 private QueryResult(List<ResultRow> data) {
43 this.errorMessage = null;
47 public static QueryResult ofIncorrectResult(String errorMessage) {
48 return new QueryResult(false, errorMessage);
51 public static QueryResult of(ResultRow... rows) {
52 return new QueryResult(List.of(rows));
55 public static QueryResult of(List<ResultRow> rows) {
56 return new QueryResult(rows);
59 public static QueryResult ofSingleValue(String columnName, Object value) {
60 return new QueryResult(List.of(new ResultRow(columnName, value)));
63 public boolean isCorrect() {
67 public @Nullable String getErrorMessage() {
71 public List<ResultRow> getData() {
76 public String toString() {
77 return new StringJoiner(", ", QueryResult.class.getSimpleName() + "[", "]").add("correct=" + correct)
78 .add("errorMessage='" + errorMessage + "'").add("data=" + data).toString();