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.action;
15 import java.time.Duration;
16 import java.util.HashMap;
18 import java.util.stream.Collectors;
20 import org.eclipse.jdt.annotation.NonNullByDefault;
21 import org.eclipse.jdt.annotation.Nullable;
22 import org.openhab.binding.dbquery.internal.DatabaseBridgeHandler;
23 import org.openhab.binding.dbquery.internal.QueryHandler;
24 import org.openhab.binding.dbquery.internal.domain.ExecuteNonConfiguredQuery;
25 import org.openhab.binding.dbquery.internal.domain.QueryResult;
26 import org.openhab.binding.dbquery.internal.domain.ResultRow;
27 import org.openhab.binding.dbquery.internal.error.UnnexpectedCondition;
28 import org.openhab.core.automation.annotation.ActionInput;
29 import org.openhab.core.automation.annotation.RuleAction;
30 import org.openhab.core.thing.binding.ThingActions;
31 import org.openhab.core.thing.binding.ThingActionsScope;
32 import org.openhab.core.thing.binding.ThingHandler;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * @author Joan Pujol - Initial contribution
39 @ThingActionsScope(name = "dbquery")
41 public class DBQueryActions implements IDBQueryActions, ThingActions {
42 private final Logger logger = LoggerFactory.getLogger(DBQueryActions.class);
44 private @Nullable QueryHandler queryHandler;
45 private @Nullable DatabaseBridgeHandler databaseBridgeHandler;
48 @RuleAction(label = "Execute query", description = "Execute query synchronously (use with care)")
49 public ActionQueryResult executeQuery(String query, Map<String, @Nullable Object> parameters,
50 int timeoutInSeconds) {
51 logger.debug("executeQuery from action {} params={}", query, parameters);
52 var currentDatabaseBridgeHandler = databaseBridgeHandler;
53 if (currentDatabaseBridgeHandler != null) {
54 QueryResult queryResult = new ExecuteNonConfiguredQuery(currentDatabaseBridgeHandler.getDatabase())
55 .executeSynchronously(query, parameters, Duration.ofSeconds(timeoutInSeconds));
56 logger.debug("executeQuery from action result {}", queryResult);
57 return queryResult2ActionQueryResult(queryResult);
59 logger.warn("Execute queried ignored as databaseBridgeHandler is null");
60 return new ActionQueryResult(false, null);
64 private ActionQueryResult queryResult2ActionQueryResult(QueryResult queryResult) {
65 return new ActionQueryResult(queryResult.isCorrect(),
66 queryResult.getData().stream().map(DBQueryActions::resultRow2Map).collect(Collectors.toList()));
69 private static Map<String, @Nullable Object> resultRow2Map(ResultRow resultRow) {
70 Map<String, @Nullable Object> map = new HashMap<>();
71 for (String column : resultRow.getColumnNames()) {
72 map.put(column, resultRow.getValue(column));
78 @RuleAction(label = "Set query parameters", description = "Set query parameters for a query")
79 public void setQueryParameters(@ActionInput(name = "parameters") Map<String, @Nullable Object> parameters) {
80 logger.debug("setQueryParameters {}", parameters);
81 var queryHandler = getThingHandler();
82 if (queryHandler instanceof QueryHandler) {
83 ((QueryHandler) queryHandler).setParameters(parameters);
85 logger.warn("setQueryParameters called on wrong Thing, it must be a Query Thing");
90 @RuleAction(label = "Get last query result", description = "Get last result from a query")
91 public ActionQueryResult getLastQueryResult() {
92 var currentQueryHandler = queryHandler;
93 if (currentQueryHandler != null) {
94 return queryResult2ActionQueryResult(queryHandler.getLastQueryResult());
96 logger.warn("getLastQueryResult ignored as queryHandler is null");
97 return new ActionQueryResult(false, null);
102 public void setThingHandler(ThingHandler thingHandler) {
103 if (thingHandler instanceof QueryHandler) {
104 this.queryHandler = ((QueryHandler) thingHandler);
105 } else if (thingHandler instanceof DatabaseBridgeHandler) {
106 this.databaseBridgeHandler = ((DatabaseBridgeHandler) thingHandler);
108 throw new UnnexpectedCondition("Not expected thing handler " + thingHandler);
113 public @Nullable ThingHandler getThingHandler() {
114 return queryHandler != null ? queryHandler : databaseBridgeHandler;