[jdbc] Improve error handling safety (#13726)

* Wrap YankSQLException into checked exception for all Yank calls

* Move files into internal

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
This commit is contained in:
Jacob Laursen 2022-11-16 09:28:46 +01:00 committed by GitHub
parent dff4d3b4fb
commit 0873dd3ddf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
27 changed files with 435 additions and 190 deletions

View File

@ -10,7 +10,7 @@
* *
* SPDX-License-Identifier: EPL-2.0 * SPDX-License-Identifier: EPL-2.0
*/ */
package org.openhab.persistence.jdbc; package org.openhab.persistence.jdbc.internal;
import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.NonNullByDefault;

View File

@ -10,7 +10,7 @@
* *
* SPDX-License-Identifier: EPL-2.0 * SPDX-License-Identifier: EPL-2.0
*/ */
package org.openhab.persistence.jdbc; package org.openhab.persistence.jdbc.internal;
import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.NonNullByDefault;

View File

@ -23,9 +23,9 @@ import java.util.regex.Pattern;
import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jdt.annotation.Nullable;
import org.openhab.persistence.jdbc.db.JdbcBaseDAO; import org.openhab.persistence.jdbc.internal.db.JdbcBaseDAO;
import org.openhab.persistence.jdbc.utils.MovingAverage; import org.openhab.persistence.jdbc.internal.utils.MovingAverage;
import org.openhab.persistence.jdbc.utils.StringUtilsExt; import org.openhab.persistence.jdbc.internal.utils.StringUtilsExt;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -39,7 +39,7 @@ public class JdbcConfiguration {
private final Logger logger = LoggerFactory.getLogger(JdbcConfiguration.class); private final Logger logger = LoggerFactory.getLogger(JdbcConfiguration.class);
private static final Pattern EXTRACT_CONFIG_PATTERN = Pattern.compile("^(.*?)\\.([0-9.a-zA-Z]+)$"); private static final Pattern EXTRACT_CONFIG_PATTERN = Pattern.compile("^(.*?)\\.([0-9.a-zA-Z]+)$");
private static final String DB_DAO_PACKAGE = "org.openhab.persistence.jdbc.db.Jdbc"; private static final String DB_DAO_PACKAGE = "org.openhab.persistence.jdbc.internal.db.Jdbc";
private Map<Object, Object> configuration; private Map<Object, Object> configuration;

View File

@ -25,16 +25,16 @@ import java.util.stream.Collectors;
import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jdt.annotation.Nullable;
import org.knowm.yank.Yank; import org.knowm.yank.Yank;
import org.knowm.yank.exceptions.YankSQLException;
import org.openhab.core.i18n.TimeZoneProvider; import org.openhab.core.i18n.TimeZoneProvider;
import org.openhab.core.items.Item; import org.openhab.core.items.Item;
import org.openhab.core.persistence.FilterCriteria; import org.openhab.core.persistence.FilterCriteria;
import org.openhab.core.persistence.HistoricItem; import org.openhab.core.persistence.HistoricItem;
import org.openhab.core.persistence.PersistenceItemInfo; import org.openhab.core.persistence.PersistenceItemInfo;
import org.openhab.core.types.State; import org.openhab.core.types.State;
import org.openhab.persistence.jdbc.dto.ItemVO; import org.openhab.persistence.jdbc.internal.dto.ItemVO;
import org.openhab.persistence.jdbc.dto.ItemsVO; import org.openhab.persistence.jdbc.internal.dto.ItemsVO;
import org.openhab.persistence.jdbc.dto.JdbcPersistenceItemInfo; import org.openhab.persistence.jdbc.internal.dto.JdbcPersistenceItemInfo;
import org.openhab.persistence.jdbc.internal.exceptions.JdbcSQLException;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -64,10 +64,10 @@ public class JdbcMapper {
this.timeZoneProvider = timeZoneProvider; this.timeZoneProvider = timeZoneProvider;
} }
/***************** /****************
* MAPPER ITEMS * * MAPPER ITEMS *
*****************/ ****************/
private boolean pingDB() { private boolean pingDB() throws JdbcSQLException {
logger.debug("JDBC::pingDB"); logger.debug("JDBC::pingDB");
boolean ret = false; boolean ret = false;
long timerStart = System.currentTimeMillis(); long timerStart = System.currentTimeMillis();
@ -91,7 +91,7 @@ public class JdbcMapper {
return ret; return ret;
} }
private boolean ifItemsTableExists() { private boolean ifItemsTableExists() throws JdbcSQLException {
logger.debug("JDBC::ifItemsTableExists"); logger.debug("JDBC::ifItemsTableExists");
long timerStart = System.currentTimeMillis(); long timerStart = System.currentTimeMillis();
boolean res = conf.getDBDAO().doIfTableExists(new ItemsVO()); boolean res = conf.getDBDAO().doIfTableExists(new ItemsVO());
@ -99,7 +99,7 @@ public class JdbcMapper {
return res; return res;
} }
protected boolean ifTableExists(String tableName) { protected boolean ifTableExists(String tableName) throws JdbcSQLException {
logger.debug("JDBC::ifTableExists"); logger.debug("JDBC::ifTableExists");
long timerStart = System.currentTimeMillis(); long timerStart = System.currentTimeMillis();
boolean res = conf.getDBDAO().doIfTableExists(tableName); boolean res = conf.getDBDAO().doIfTableExists(tableName);
@ -107,7 +107,7 @@ public class JdbcMapper {
return res; return res;
} }
private ItemsVO createNewEntryInItemsTable(ItemsVO vo) { private ItemsVO createNewEntryInItemsTable(ItemsVO vo) throws JdbcSQLException {
logger.debug("JDBC::createNewEntryInItemsTable"); logger.debug("JDBC::createNewEntryInItemsTable");
long timerStart = System.currentTimeMillis(); long timerStart = System.currentTimeMillis();
Long i = conf.getDBDAO().doCreateNewEntryInItemsTable(vo); Long i = conf.getDBDAO().doCreateNewEntryInItemsTable(vo);
@ -116,7 +116,7 @@ public class JdbcMapper {
return vo; return vo;
} }
private boolean createItemsTableIfNot(ItemsVO vo) { private boolean createItemsTableIfNot(ItemsVO vo) throws JdbcSQLException {
logger.debug("JDBC::createItemsTableIfNot"); logger.debug("JDBC::createItemsTableIfNot");
long timerStart = System.currentTimeMillis(); long timerStart = System.currentTimeMillis();
conf.getDBDAO().doCreateItemsTableIfNot(vo); conf.getDBDAO().doCreateItemsTableIfNot(vo);
@ -124,7 +124,7 @@ public class JdbcMapper {
return true; return true;
} }
private boolean dropItemsTableIfExists(ItemsVO vo) { private boolean dropItemsTableIfExists(ItemsVO vo) throws JdbcSQLException {
logger.debug("JDBC::dropItemsTableIfExists"); logger.debug("JDBC::dropItemsTableIfExists");
long timerStart = System.currentTimeMillis(); long timerStart = System.currentTimeMillis();
conf.getDBDAO().doDropItemsTableIfExists(vo); conf.getDBDAO().doDropItemsTableIfExists(vo);
@ -132,14 +132,14 @@ public class JdbcMapper {
return true; return true;
} }
protected void dropTable(String tableName) { protected void dropTable(String tableName) throws JdbcSQLException {
logger.debug("JDBC::dropTable"); logger.debug("JDBC::dropTable");
long timerStart = System.currentTimeMillis(); long timerStart = System.currentTimeMillis();
conf.getDBDAO().doDropTable(tableName); conf.getDBDAO().doDropTable(tableName);
logTime("doDropTable", timerStart, System.currentTimeMillis()); logTime("doDropTable", timerStart, System.currentTimeMillis());
} }
protected ItemsVO deleteItemsEntry(ItemsVO vo) { protected ItemsVO deleteItemsEntry(ItemsVO vo) throws JdbcSQLException {
logger.debug("JDBC::deleteItemsEntry"); logger.debug("JDBC::deleteItemsEntry");
long timerStart = System.currentTimeMillis(); long timerStart = System.currentTimeMillis();
conf.getDBDAO().doDeleteItemsEntry(vo); conf.getDBDAO().doDeleteItemsEntry(vo);
@ -147,7 +147,7 @@ public class JdbcMapper {
return vo; return vo;
} }
private List<ItemsVO> getItemIDTableNames() { private List<ItemsVO> getItemIDTableNames() throws JdbcSQLException {
logger.debug("JDBC::getItemIDTableNames"); logger.debug("JDBC::getItemIDTableNames");
long timerStart = System.currentTimeMillis(); long timerStart = System.currentTimeMillis();
List<ItemsVO> vo = conf.getDBDAO().doGetItemIDTableNames(new ItemsVO()); List<ItemsVO> vo = conf.getDBDAO().doGetItemIDTableNames(new ItemsVO());
@ -155,7 +155,7 @@ public class JdbcMapper {
return vo; return vo;
} }
protected List<ItemsVO> getItemTables() { protected List<ItemsVO> getItemTables() throws JdbcSQLException {
logger.debug("JDBC::getItemTables"); logger.debug("JDBC::getItemTables");
long timerStart = System.currentTimeMillis(); long timerStart = System.currentTimeMillis();
ItemsVO vo = new ItemsVO(); ItemsVO vo = new ItemsVO();
@ -168,14 +168,14 @@ public class JdbcMapper {
/**************** /****************
* MAPPERS ITEM * * MAPPERS ITEM *
****************/ ****************/
private void updateItemTableNames(List<ItemVO> vol) { private void updateItemTableNames(List<ItemVO> vol) throws JdbcSQLException {
logger.debug("JDBC::updateItemTableNames"); logger.debug("JDBC::updateItemTableNames");
long timerStart = System.currentTimeMillis(); long timerStart = System.currentTimeMillis();
conf.getDBDAO().doUpdateItemTableNames(vol); conf.getDBDAO().doUpdateItemTableNames(vol);
logTime("updateItemTableNames", timerStart, System.currentTimeMillis()); logTime("updateItemTableNames", timerStart, System.currentTimeMillis());
} }
private ItemVO createItemTable(ItemVO vo) { private ItemVO createItemTable(ItemVO vo) throws JdbcSQLException {
logger.debug("JDBC::createItemTable"); logger.debug("JDBC::createItemTable");
long timerStart = System.currentTimeMillis(); long timerStart = System.currentTimeMillis();
conf.getDBDAO().doCreateItemTable(vo); conf.getDBDAO().doCreateItemTable(vo);
@ -183,7 +183,7 @@ public class JdbcMapper {
return vo; return vo;
} }
protected Item storeItemValue(Item item, State itemState, @Nullable ZonedDateTime date) { protected void storeItemValue(Item item, State itemState, @Nullable ZonedDateTime date) throws JdbcSQLException {
logger.debug("JDBC::storeItemValue: item={} state={} date={}", item, itemState, date); logger.debug("JDBC::storeItemValue: item={} state={} date={}", item, itemState, date);
String tableName = getTable(item); String tableName = getTable(item);
long timerStart = System.currentTimeMillis(); long timerStart = System.currentTimeMillis();
@ -194,15 +194,14 @@ public class JdbcMapper {
} }
logTime("storeItemValue", timerStart, System.currentTimeMillis()); logTime("storeItemValue", timerStart, System.currentTimeMillis());
errCnt = 0; errCnt = 0;
return item;
} }
public long getRowCount(String tableName) { public long getRowCount(String tableName) throws JdbcSQLException {
return conf.getDBDAO().doGetRowCount(tableName); return conf.getDBDAO().doGetRowCount(tableName);
} }
protected List<HistoricItem> getHistItemFilterQuery(FilterCriteria filter, int numberDecimalcount, String table, protected List<HistoricItem> getHistItemFilterQuery(FilterCriteria filter, int numberDecimalcount, String table,
Item item) { Item item) throws JdbcSQLException {
logger.debug( logger.debug(
"JDBC::getHistItemFilterQuery filter='{}' numberDecimalcount='{}' table='{}' item='{}' itemName='{}'", "JDBC::getHistItemFilterQuery filter='{}' numberDecimalcount='{}' table='{}' item='{}' itemName='{}'",
true, numberDecimalcount, table, item, item.getName()); true, numberDecimalcount, table, item, item.getName());
@ -214,7 +213,7 @@ public class JdbcMapper {
return result; return result;
} }
protected void deleteItemValues(FilterCriteria filter, String table) { protected void deleteItemValues(FilterCriteria filter, String table) throws JdbcSQLException {
logger.debug("JDBC::deleteItemValues filter='{}' table='{}' itemName='{}'", true, table, filter.getItemName()); logger.debug("JDBC::deleteItemValues filter='{}' table='{}' itemName='{}'", true, table, filter.getItemName());
long timerStart = System.currentTimeMillis(); long timerStart = System.currentTimeMillis();
conf.getDBDAO().doDeleteItemValues(filter, table, timeZoneProvider.getTimeZone()); conf.getDBDAO().doDeleteItemValues(filter, table, timeZoneProvider.getTimeZone());
@ -276,7 +275,7 @@ public class JdbcMapper {
logger.debug("JDBC::checkDBAcessability, second try connection: {}", p); logger.debug("JDBC::checkDBAcessability, second try connection: {}", p);
return (p && !(conf.getErrReconnectThreshold() > 0 && errCnt <= conf.getErrReconnectThreshold())); return (p && !(conf.getErrReconnectThreshold() > 0 && errCnt <= conf.getErrReconnectThreshold()));
} }
} catch (YankSQLException e) { } catch (JdbcSQLException e) {
logger.warn("Unable to ping database", e); logger.warn("Unable to ping database", e);
return false; return false;
} }
@ -285,7 +284,7 @@ public class JdbcMapper {
/************************** /**************************
* DATABASE TABLEHANDLING * * DATABASE TABLEHANDLING *
**************************/ **************************/
protected void checkDBSchema() { protected void checkDBSchema() throws JdbcSQLException {
if (!conf.getTableUseRealCaseSensitiveItemNames()) { if (!conf.getTableUseRealCaseSensitiveItemNames()) {
createItemsTableIfNot(new ItemsVO()); createItemsTableIfNot(new ItemsVO());
} }
@ -303,7 +302,7 @@ public class JdbcMapper {
populateItemNameToTableNameMap(); populateItemNameToTableNameMap();
} }
private void populateItemNameToTableNameMap() { private void populateItemNameToTableNameMap() throws JdbcSQLException {
itemNameToTableNameMap.clear(); itemNameToTableNameMap.clear();
if (conf.getTableUseRealCaseSensitiveItemNames()) { if (conf.getTableUseRealCaseSensitiveItemNames()) {
for (String itemName : getItemTables().stream().map(t -> t.getTableName()).collect(Collectors.toList())) { for (String itemName : getItemTables().stream().map(t -> t.getTableName()).collect(Collectors.toList())) {
@ -317,7 +316,7 @@ public class JdbcMapper {
} }
} }
protected String getTable(Item item) { protected String getTable(Item item) throws JdbcSQLException {
int itemId = 0; int itemId = 0;
ItemsVO isvo; ItemsVO isvo;
ItemVO ivo; ItemVO ivo;
@ -360,7 +359,7 @@ public class JdbcMapper {
return tableName; return tableName;
} }
private void formatTableNames() { private void formatTableNames() throws JdbcSQLException {
boolean tmpinit = initialized; boolean tmpinit = initialized;
if (tmpinit) { if (tmpinit) {
initialized = false; initialized = false;

View File

@ -25,7 +25,6 @@ import java.util.stream.Collectors;
import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jdt.annotation.Nullable;
import org.knowm.yank.exceptions.YankSQLException;
import org.openhab.core.config.core.ConfigurableService; import org.openhab.core.config.core.ConfigurableService;
import org.openhab.core.i18n.TimeZoneProvider; import org.openhab.core.i18n.TimeZoneProvider;
import org.openhab.core.items.GroupItem; import org.openhab.core.items.GroupItem;
@ -41,9 +40,8 @@ import org.openhab.core.persistence.QueryablePersistenceService;
import org.openhab.core.persistence.strategy.PersistenceStrategy; import org.openhab.core.persistence.strategy.PersistenceStrategy;
import org.openhab.core.types.State; import org.openhab.core.types.State;
import org.openhab.core.types.UnDefType; import org.openhab.core.types.UnDefType;
import org.openhab.persistence.jdbc.ItemTableCheckEntry; import org.openhab.persistence.jdbc.internal.dto.ItemsVO;
import org.openhab.persistence.jdbc.ItemTableCheckEntryStatus; import org.openhab.persistence.jdbc.internal.exceptions.JdbcSQLException;
import org.openhab.persistence.jdbc.dto.ItemsVO;
import org.osgi.framework.BundleContext; import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants; import org.osgi.framework.Constants;
import org.osgi.service.component.annotations.Activate; import org.osgi.service.component.annotations.Activate;
@ -163,7 +161,7 @@ public class JdbcPersistenceService extends JdbcMapper implements ModifiablePers
logger.debug("JDBC: Stored item '{}' as '{}' in SQL database at {} in {} ms.", item.getName(), state, logger.debug("JDBC: Stored item '{}' as '{}' in SQL database at {} in {} ms.", item.getName(), state,
new Date(), System.currentTimeMillis() - timerStart); new Date(), System.currentTimeMillis() - timerStart);
} }
} catch (YankSQLException e) { } catch (JdbcSQLException e) {
logger.warn("JDBC::store: Unable to store item", e); logger.warn("JDBC::store: Unable to store item", e);
} }
} }
@ -230,7 +228,7 @@ public class JdbcPersistenceService extends JdbcMapper implements ModifiablePers
// Success // Success
errCnt = 0; errCnt = 0;
return items; return items;
} catch (YankSQLException e) { } catch (JdbcSQLException e) {
logger.warn("JDBC::query: Unable to query item", e); logger.warn("JDBC::query: Unable to query item", e);
return List.of(); return List.of();
} }
@ -246,7 +244,7 @@ public class JdbcPersistenceService extends JdbcMapper implements ModifiablePers
checkDBSchema(); checkDBSchema();
// connection has been established ... initialization completed! // connection has been established ... initialization completed!
initialized = true; initialized = true;
} catch (YankSQLException e) { } catch (JdbcSQLException e) {
logger.error("Failed to check database schema", e); logger.error("Failed to check database schema", e);
initialized = false; initialized = false;
} }
@ -291,7 +289,7 @@ public class JdbcPersistenceService extends JdbcMapper implements ModifiablePers
System.currentTimeMillis() - timerStart); System.currentTimeMillis() - timerStart);
} }
return true; return true;
} catch (YankSQLException e) { } catch (JdbcSQLException e) {
logger.debug("JDBC::remove: Unable to remove values for item", e); logger.debug("JDBC::remove: Unable to remove values for item", e);
return false; return false;
} }
@ -310,7 +308,7 @@ public class JdbcPersistenceService extends JdbcMapper implements ModifiablePers
* *
* @return list of {@link ItemTableCheckEntry} * @return list of {@link ItemTableCheckEntry}
*/ */
public List<ItemTableCheckEntry> getCheckedEntries() { public List<ItemTableCheckEntry> getCheckedEntries() throws JdbcSQLException {
List<ItemTableCheckEntry> entries = new ArrayList<>(); List<ItemTableCheckEntry> entries = new ArrayList<>();
if (!checkDBAccessability()) { if (!checkDBAccessability()) {
@ -362,8 +360,9 @@ public class JdbcPersistenceService extends JdbcMapper implements ModifiablePers
* @param itemName Name of item to clean * @param itemName Name of item to clean
* @param force If true, non-empty tables will be dropped too * @param force If true, non-empty tables will be dropped too
* @return true if item was cleaned up * @return true if item was cleaned up
* @throws JdbcSQLException
*/ */
public boolean cleanupItem(String itemName, boolean force) { public boolean cleanupItem(String itemName, boolean force) throws JdbcSQLException {
String tableName = itemNameToTableNameMap.get(itemName); String tableName = itemNameToTableNameMap.get(itemName);
if (tableName == null) { if (tableName == null) {
return false; return false;
@ -378,12 +377,13 @@ public class JdbcPersistenceService extends JdbcMapper implements ModifiablePers
* *
* @param entry * @param entry
* @return true if item was cleaned up * @return true if item was cleaned up
* @throws JdbcSQLException
*/ */
public boolean cleanupItem(ItemTableCheckEntry entry) { public boolean cleanupItem(ItemTableCheckEntry entry) throws JdbcSQLException {
return cleanupItem(entry, false); return cleanupItem(entry, false);
} }
private boolean cleanupItem(ItemTableCheckEntry entry, boolean force) { private boolean cleanupItem(ItemTableCheckEntry entry, boolean force) throws JdbcSQLException {
if (!checkDBAccessability()) { if (!checkDBAccessability()) {
logger.warn("JDBC::cleanupItem: database not connected"); logger.warn("JDBC::cleanupItem: database not connected");
return false; return false;

View File

@ -21,7 +21,7 @@ import java.util.Objects;
import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.core.items.ItemUtil; import org.openhab.core.items.ItemUtil;
import org.openhab.persistence.jdbc.dto.ItemVO; import org.openhab.persistence.jdbc.internal.dto.ItemVO;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;

View File

@ -10,7 +10,7 @@
* *
* SPDX-License-Identifier: EPL-2.0 * SPDX-License-Identifier: EPL-2.0
*/ */
package org.openhab.persistence.jdbc.console; package org.openhab.persistence.jdbc.internal.console;
import java.util.Arrays; import java.util.Arrays;
import java.util.Comparator; import java.util.Comparator;
@ -19,7 +19,6 @@ import java.util.stream.Stream;
import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jdt.annotation.Nullable;
import org.knowm.yank.exceptions.YankSQLException;
import org.openhab.core.io.console.Console; import org.openhab.core.io.console.Console;
import org.openhab.core.io.console.ConsoleCommandCompleter; import org.openhab.core.io.console.ConsoleCommandCompleter;
import org.openhab.core.io.console.StringsCompleter; import org.openhab.core.io.console.StringsCompleter;
@ -27,10 +26,11 @@ import org.openhab.core.io.console.extensions.AbstractConsoleCommandExtension;
import org.openhab.core.io.console.extensions.ConsoleCommandExtension; import org.openhab.core.io.console.extensions.ConsoleCommandExtension;
import org.openhab.core.persistence.PersistenceService; import org.openhab.core.persistence.PersistenceService;
import org.openhab.core.persistence.PersistenceServiceRegistry; import org.openhab.core.persistence.PersistenceServiceRegistry;
import org.openhab.persistence.jdbc.ItemTableCheckEntry; import org.openhab.persistence.jdbc.internal.ItemTableCheckEntry;
import org.openhab.persistence.jdbc.ItemTableCheckEntryStatus; import org.openhab.persistence.jdbc.internal.ItemTableCheckEntryStatus;
import org.openhab.persistence.jdbc.internal.JdbcPersistenceService; import org.openhab.persistence.jdbc.internal.JdbcPersistenceService;
import org.openhab.persistence.jdbc.internal.JdbcPersistenceServiceConstants; import org.openhab.persistence.jdbc.internal.JdbcPersistenceServiceConstants;
import org.openhab.persistence.jdbc.internal.exceptions.JdbcSQLException;
import org.osgi.service.component.annotations.Activate; import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component; import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference; import org.osgi.service.component.annotations.Reference;
@ -76,7 +76,7 @@ public class JdbcCommandExtension extends AbstractConsoleCommandExtension implem
printUsage(console); printUsage(console);
return; return;
} }
} catch (YankSQLException e) { } catch (JdbcSQLException e) {
console.println(e.toString()); console.println(e.toString());
} }
} }
@ -90,7 +90,8 @@ public class JdbcCommandExtension extends AbstractConsoleCommandExtension implem
return null; return null;
} }
private boolean execute(JdbcPersistenceService persistenceService, String[] args, Console console) { private boolean execute(JdbcPersistenceService persistenceService, String[] args, Console console)
throws JdbcSQLException {
if (SUBCMD_TABLES_LIST.equalsIgnoreCase(args[1])) { if (SUBCMD_TABLES_LIST.equalsIgnoreCase(args[1])) {
listTables(persistenceService, console, args.length == 3 && PARAMETER_ALL.equalsIgnoreCase(args[2])); listTables(persistenceService, console, args.length == 3 && PARAMETER_ALL.equalsIgnoreCase(args[2]));
return true; return true;
@ -109,7 +110,8 @@ public class JdbcCommandExtension extends AbstractConsoleCommandExtension implem
return false; return false;
} }
private void listTables(JdbcPersistenceService persistenceService, Console console, Boolean all) { private void listTables(JdbcPersistenceService persistenceService, Console console, Boolean all)
throws JdbcSQLException {
List<ItemTableCheckEntry> entries = persistenceService.getCheckedEntries(); List<ItemTableCheckEntry> entries = persistenceService.getCheckedEntries();
if (!all) { if (!all) {
entries.removeIf(t -> t.getStatus() == ItemTableCheckEntryStatus.VALID); entries.removeIf(t -> t.getStatus() == ItemTableCheckEntryStatus.VALID);
@ -138,7 +140,7 @@ public class JdbcCommandExtension extends AbstractConsoleCommandExtension implem
} }
} }
private void cleanupTables(JdbcPersistenceService persistenceService, Console console) { private void cleanupTables(JdbcPersistenceService persistenceService, Console console) throws JdbcSQLException {
console.println("Cleaning up all inconsistent items..."); console.println("Cleaning up all inconsistent items...");
List<ItemTableCheckEntry> entries = persistenceService.getCheckedEntries(); List<ItemTableCheckEntry> entries = persistenceService.getCheckedEntries();
entries.removeIf(t -> t.getStatus() == ItemTableCheckEntryStatus.VALID || t.getItemName().isEmpty()); entries.removeIf(t -> t.getStatus() == ItemTableCheckEntryStatus.VALID || t.getItemName().isEmpty());
@ -152,8 +154,8 @@ public class JdbcCommandExtension extends AbstractConsoleCommandExtension implem
} }
} }
private void cleanupItem(JdbcPersistenceService persistenceService, Console console, String itemName, private void cleanupItem(JdbcPersistenceService persistenceService, Console console, String itemName, boolean force)
boolean force) { throws JdbcSQLException {
console.print("Cleaning up item " + itemName + "... "); console.print("Cleaning up item " + itemName + "... ");
if (persistenceService.cleanupItem(itemName, force)) { if (persistenceService.cleanupItem(itemName, force)) {
console.println("done."); console.println("done.");

View File

@ -10,7 +10,7 @@
* *
* SPDX-License-Identifier: EPL-2.0 * SPDX-License-Identifier: EPL-2.0
*/ */
package org.openhab.persistence.jdbc.db; package org.openhab.persistence.jdbc.internal.db;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.time.Instant; import java.time.Instant;
@ -32,6 +32,7 @@ import javax.measure.Unit;
import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jdt.annotation.Nullable;
import org.knowm.yank.Yank; import org.knowm.yank.Yank;
import org.knowm.yank.exceptions.YankSQLException;
import org.openhab.core.items.GroupItem; import org.openhab.core.items.GroupItem;
import org.openhab.core.items.Item; import org.openhab.core.items.Item;
import org.openhab.core.library.items.ColorItem; import org.openhab.core.library.items.ColorItem;
@ -55,11 +56,12 @@ import org.openhab.core.persistence.FilterCriteria.Ordering;
import org.openhab.core.persistence.HistoricItem; import org.openhab.core.persistence.HistoricItem;
import org.openhab.core.types.State; import org.openhab.core.types.State;
import org.openhab.core.types.TypeParser; import org.openhab.core.types.TypeParser;
import org.openhab.persistence.jdbc.dto.ItemVO; import org.openhab.persistence.jdbc.internal.dto.ItemVO;
import org.openhab.persistence.jdbc.dto.ItemsVO; import org.openhab.persistence.jdbc.internal.dto.ItemsVO;
import org.openhab.persistence.jdbc.dto.JdbcHistoricItem; import org.openhab.persistence.jdbc.internal.dto.JdbcHistoricItem;
import org.openhab.persistence.jdbc.utils.DbMetaData; import org.openhab.persistence.jdbc.internal.exceptions.JdbcSQLException;
import org.openhab.persistence.jdbc.utils.StringUtilsExt; import org.openhab.persistence.jdbc.internal.utils.DbMetaData;
import org.openhab.persistence.jdbc.internal.utils.StringUtilsExt;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -248,131 +250,196 @@ public class JdbcBaseDAO {
/************** /**************
* ITEMS DAOs * * ITEMS DAOs *
**************/ **************/
public @Nullable Integer doPingDB() { public @Nullable Integer doPingDB() throws JdbcSQLException {
final @Nullable Integer result = Yank.queryScalar(sqlPingDB, Integer.class, null); try {
return result; final @Nullable Integer result = Yank.queryScalar(sqlPingDB, Integer.class, null);
return result;
} catch (YankSQLException e) {
throw new JdbcSQLException(e);
}
} }
public @Nullable String doGetDB() { public @Nullable String doGetDB() throws JdbcSQLException {
final @Nullable String result = Yank.queryScalar(sqlGetDB, String.class, null); try {
return result; final @Nullable String result = Yank.queryScalar(sqlGetDB, String.class, null);
return result;
} catch (YankSQLException e) {
throw new JdbcSQLException(e);
}
} }
public boolean doIfTableExists(ItemsVO vo) { public boolean doIfTableExists(ItemsVO vo) throws JdbcSQLException {
String sql = StringUtilsExt.replaceArrayMerge(sqlIfTableExists, new String[] { "#searchTable#" }, String sql = StringUtilsExt.replaceArrayMerge(sqlIfTableExists, new String[] { "#searchTable#" },
new String[] { vo.getItemsManageTable() }); new String[] { vo.getItemsManageTable() });
logger.debug("JDBC::doIfTableExists sql={}", sql); logger.debug("JDBC::doIfTableExists sql={}", sql);
final @Nullable String result = Yank.queryScalar(sql, String.class, null); try {
return Objects.nonNull(result); final @Nullable String result = Yank.queryScalar(sql, String.class, null);
return Objects.nonNull(result);
} catch (YankSQLException e) {
throw new JdbcSQLException(e);
}
} }
public boolean doIfTableExists(String tableName) { public boolean doIfTableExists(String tableName) throws JdbcSQLException {
String sql = StringUtilsExt.replaceArrayMerge(sqlIfTableExists, new String[] { "#searchTable#" }, String sql = StringUtilsExt.replaceArrayMerge(sqlIfTableExists, new String[] { "#searchTable#" },
new String[] { tableName }); new String[] { tableName });
logger.debug("JDBC::doIfTableExists sql={}", sql); logger.debug("JDBC::doIfTableExists sql={}", sql);
final @Nullable String result = Yank.queryScalar(sql, String.class, null); try {
return Objects.nonNull(result); final @Nullable String result = Yank.queryScalar(sql, String.class, null);
return Objects.nonNull(result);
} catch (YankSQLException e) {
throw new JdbcSQLException(e);
}
} }
public Long doCreateNewEntryInItemsTable(ItemsVO vo) { public Long doCreateNewEntryInItemsTable(ItemsVO vo) throws JdbcSQLException {
String sql = StringUtilsExt.replaceArrayMerge(sqlCreateNewEntryInItemsTable, String sql = StringUtilsExt.replaceArrayMerge(sqlCreateNewEntryInItemsTable,
new String[] { "#itemsManageTable#", "#itemname#" }, new String[] { "#itemsManageTable#", "#itemname#" },
new String[] { vo.getItemsManageTable(), vo.getItemName() }); new String[] { vo.getItemsManageTable(), vo.getItemName() });
logger.debug("JDBC::doCreateNewEntryInItemsTable sql={}", sql); logger.debug("JDBC::doCreateNewEntryInItemsTable sql={}", sql);
return Yank.insert(sql, null); try {
return Yank.insert(sql, null);
} catch (YankSQLException e) {
throw new JdbcSQLException(e);
}
} }
public ItemsVO doCreateItemsTableIfNot(ItemsVO vo) { public ItemsVO doCreateItemsTableIfNot(ItemsVO vo) throws JdbcSQLException {
String sql = StringUtilsExt.replaceArrayMerge(sqlCreateItemsTableIfNot, String sql = StringUtilsExt.replaceArrayMerge(sqlCreateItemsTableIfNot,
new String[] { "#itemsManageTable#", "#colname#", "#coltype#" }, new String[] { "#itemsManageTable#", "#colname#", "#coltype#" },
new String[] { vo.getItemsManageTable(), vo.getColname(), vo.getColtype() }); new String[] { vo.getItemsManageTable(), vo.getColname(), vo.getColtype() });
logger.debug("JDBC::doCreateItemsTableIfNot sql={}", sql); logger.debug("JDBC::doCreateItemsTableIfNot sql={}", sql);
Yank.execute(sql, null); try {
Yank.execute(sql, null);
} catch (YankSQLException e) {
throw new JdbcSQLException(e);
}
return vo; return vo;
} }
public ItemsVO doDropItemsTableIfExists(ItemsVO vo) { public ItemsVO doDropItemsTableIfExists(ItemsVO vo) throws JdbcSQLException {
String sql = StringUtilsExt.replaceArrayMerge(sqlDropItemsTableIfExists, new String[] { "#itemsManageTable#" }, String sql = StringUtilsExt.replaceArrayMerge(sqlDropItemsTableIfExists, new String[] { "#itemsManageTable#" },
new String[] { vo.getItemsManageTable() }); new String[] { vo.getItemsManageTable() });
logger.debug("JDBC::doDropItemsTableIfExists sql={}", sql); logger.debug("JDBC::doDropItemsTableIfExists sql={}", sql);
Yank.execute(sql, null); try {
Yank.execute(sql, null);
} catch (YankSQLException e) {
throw new JdbcSQLException(e);
}
return vo; return vo;
} }
public void doDropTable(String tableName) { public void doDropTable(String tableName) throws JdbcSQLException {
String sql = StringUtilsExt.replaceArrayMerge(sqlDropTable, new String[] { "#tableName#" }, String sql = StringUtilsExt.replaceArrayMerge(sqlDropTable, new String[] { "#tableName#" },
new String[] { tableName }); new String[] { tableName });
logger.debug("JDBC::doDropTable sql={}", sql); logger.debug("JDBC::doDropTable sql={}", sql);
Yank.execute(sql, null); try {
Yank.execute(sql, null);
} catch (YankSQLException e) {
throw new JdbcSQLException(e);
}
} }
public void doDeleteItemsEntry(ItemsVO vo) { public void doDeleteItemsEntry(ItemsVO vo) throws JdbcSQLException {
String sql = StringUtilsExt.replaceArrayMerge(sqlDeleteItemsEntry, String sql = StringUtilsExt.replaceArrayMerge(sqlDeleteItemsEntry,
new String[] { "#itemsManageTable#", "#itemname#" }, new String[] { "#itemsManageTable#", "#itemname#" },
new String[] { vo.getItemsManageTable(), vo.getItemName() }); new String[] { vo.getItemsManageTable(), vo.getItemName() });
logger.debug("JDBC::doDeleteItemsEntry sql={}", sql); logger.debug("JDBC::doDeleteItemsEntry sql={}", sql);
Yank.execute(sql, null); try {
Yank.execute(sql, null);
} catch (YankSQLException e) {
throw new JdbcSQLException(e);
}
} }
public List<ItemsVO> doGetItemIDTableNames(ItemsVO vo) { public List<ItemsVO> doGetItemIDTableNames(ItemsVO vo) throws JdbcSQLException {
String sql = StringUtilsExt.replaceArrayMerge(sqlGetItemIDTableNames, new String[] { "#itemsManageTable#" }, String sql = StringUtilsExt.replaceArrayMerge(sqlGetItemIDTableNames, new String[] { "#itemsManageTable#" },
new String[] { vo.getItemsManageTable() }); new String[] { vo.getItemsManageTable() });
logger.debug("JDBC::doGetItemIDTableNames sql={}", sql); logger.debug("JDBC::doGetItemIDTableNames sql={}", sql);
return Yank.queryBeanList(sql, ItemsVO.class, null); try {
return Yank.queryBeanList(sql, ItemsVO.class, null);
} catch (YankSQLException e) {
throw new JdbcSQLException(e);
}
} }
public List<ItemsVO> doGetItemTables(ItemsVO vo) { public List<ItemsVO> doGetItemTables(ItemsVO vo) throws JdbcSQLException {
String sql = StringUtilsExt.replaceArrayMerge(sqlGetItemTables, String sql = StringUtilsExt.replaceArrayMerge(sqlGetItemTables,
new String[] { "#jdbcUriDatabaseName#", "#itemsManageTable#" }, new String[] { "#jdbcUriDatabaseName#", "#itemsManageTable#" },
new String[] { vo.getJdbcUriDatabaseName(), vo.getItemsManageTable() }); new String[] { vo.getJdbcUriDatabaseName(), vo.getItemsManageTable() });
logger.debug("JDBC::doGetItemTables sql={}", sql); logger.debug("JDBC::doGetItemTables sql={}", sql);
return Yank.queryBeanList(sql, ItemsVO.class, null); try {
return Yank.queryBeanList(sql, ItemsVO.class, null);
} catch (YankSQLException e) {
throw new JdbcSQLException(e);
}
} }
/************* /*************
* ITEM DAOs * * ITEM DAOs *
*************/ *************/
public void doUpdateItemTableNames(List<ItemVO> vol) { public void doUpdateItemTableNames(List<ItemVO> vol) throws JdbcSQLException {
logger.debug("JDBC::doUpdateItemTableNames vol.size = {}", vol.size()); logger.debug("JDBC::doUpdateItemTableNames vol.size = {}", vol.size());
for (ItemVO itemTable : vol) { for (ItemVO itemTable : vol) {
String sql = updateItemTableNamesProvider(itemTable); String sql = updateItemTableNamesProvider(itemTable);
Yank.execute(sql, null); try {
Yank.execute(sql, null);
} catch (YankSQLException e) {
throw new JdbcSQLException(e);
}
} }
} }
public void doCreateItemTable(ItemVO vo) { public void doCreateItemTable(ItemVO vo) throws JdbcSQLException {
String sql = StringUtilsExt.replaceArrayMerge(sqlCreateItemTable, String sql = StringUtilsExt.replaceArrayMerge(sqlCreateItemTable,
new String[] { "#tableName#", "#dbType#", "#tablePrimaryKey#" }, new String[] { "#tableName#", "#dbType#", "#tablePrimaryKey#" },
new String[] { vo.getTableName(), vo.getDbType(), sqlTypes.get("tablePrimaryKey") }); new String[] { vo.getTableName(), vo.getDbType(), sqlTypes.get("tablePrimaryKey") });
logger.debug("JDBC::doCreateItemTable sql={}", sql); logger.debug("JDBC::doCreateItemTable sql={}", sql);
Yank.execute(sql, null); try {
Yank.execute(sql, null);
} catch (YankSQLException e) {
throw new JdbcSQLException(e);
}
} }
public void doStoreItemValue(Item item, State itemState, ItemVO vo) { public void doStoreItemValue(Item item, State itemState, ItemVO vo) throws JdbcSQLException {
ItemVO storedVO = storeItemValueProvider(item, itemState, vo); ItemVO storedVO = storeItemValueProvider(item, itemState, vo);
String sql = StringUtilsExt.replaceArrayMerge(sqlInsertItemValue, String sql = StringUtilsExt.replaceArrayMerge(sqlInsertItemValue,
new String[] { "#tableName#", "#tablePrimaryValue#" }, new String[] { "#tableName#", "#tablePrimaryValue#" },
new String[] { storedVO.getTableName(), sqlTypes.get("tablePrimaryValue") }); new String[] { storedVO.getTableName(), sqlTypes.get("tablePrimaryValue") });
Object[] params = { storedVO.getValue(), storedVO.getValue() }; Object[] params = { storedVO.getValue(), storedVO.getValue() };
logger.debug("JDBC::doStoreItemValue sql={} value='{}'", sql, storedVO.getValue()); logger.debug("JDBC::doStoreItemValue sql={} value='{}'", sql, storedVO.getValue());
Yank.execute(sql, params); try {
Yank.execute(sql, params);
} catch (YankSQLException e) {
throw new JdbcSQLException(e);
}
} }
public void doStoreItemValue(Item item, State itemState, ItemVO vo, ZonedDateTime date) { public void doStoreItemValue(Item item, State itemState, ItemVO vo, ZonedDateTime date) throws JdbcSQLException {
ItemVO storedVO = storeItemValueProvider(item, itemState, vo); ItemVO storedVO = storeItemValueProvider(item, itemState, vo);
String sql = StringUtilsExt.replaceArrayMerge(sqlInsertItemValue, String sql = StringUtilsExt.replaceArrayMerge(sqlInsertItemValue,
new String[] { "#tableName#", "#tablePrimaryValue#" }, new String[] { storedVO.getTableName(), "?" }); new String[] { "#tableName#", "#tablePrimaryValue#" }, new String[] { storedVO.getTableName(), "?" });
java.sql.Timestamp timestamp = new java.sql.Timestamp(date.toInstant().toEpochMilli()); java.sql.Timestamp timestamp = new java.sql.Timestamp(date.toInstant().toEpochMilli());
Object[] params = { timestamp, storedVO.getValue(), storedVO.getValue() }; Object[] params = { timestamp, storedVO.getValue(), storedVO.getValue() };
logger.debug("JDBC::doStoreItemValue sql={} timestamp={} value='{}'", sql, timestamp, storedVO.getValue()); logger.debug("JDBC::doStoreItemValue sql={} timestamp={} value='{}'", sql, timestamp, storedVO.getValue());
Yank.execute(sql, params); try {
Yank.execute(sql, params);
} catch (YankSQLException e) {
throw new JdbcSQLException(e);
}
} }
public List<HistoricItem> doGetHistItemFilterQuery(Item item, FilterCriteria filter, int numberDecimalcount, public List<HistoricItem> doGetHistItemFilterQuery(Item item, FilterCriteria filter, int numberDecimalcount,
String table, String name, ZoneId timeZone) { String table, String name, ZoneId timeZone) throws JdbcSQLException {
String sql = histItemFilterQueryProvider(filter, numberDecimalcount, table, name, timeZone); String sql = histItemFilterQueryProvider(filter, numberDecimalcount, table, name, timeZone);
logger.debug("JDBC::doGetHistItemFilterQuery sql={}", sql); logger.debug("JDBC::doGetHistItemFilterQuery sql={}", sql);
List<Object[]> m = Yank.queryObjectArrays(sql, null); List<Object[]> m;
try {
m = Yank.queryObjectArrays(sql, null);
} catch (YankSQLException e) {
throw new JdbcSQLException(e);
}
if (m == null) { if (m == null) {
logger.debug("JDBC::doGetHistItemFilterQuery Query failed. Returning an empty list."); logger.debug("JDBC::doGetHistItemFilterQuery Query failed. Returning an empty list.");
return List.of(); return List.of();
@ -385,18 +452,26 @@ public class JdbcBaseDAO {
.collect(Collectors.<HistoricItem> toList()); .collect(Collectors.<HistoricItem> toList());
} }
public void doDeleteItemValues(FilterCriteria filter, String table, ZoneId timeZone) { public void doDeleteItemValues(FilterCriteria filter, String table, ZoneId timeZone) throws JdbcSQLException {
String sql = histItemFilterDeleteProvider(filter, table, timeZone); String sql = histItemFilterDeleteProvider(filter, table, timeZone);
logger.debug("JDBC::doDeleteItemValues sql={}", sql); logger.debug("JDBC::doDeleteItemValues sql={}", sql);
Yank.execute(sql, null); try {
Yank.execute(sql, null);
} catch (YankSQLException e) {
throw new JdbcSQLException(e);
}
} }
public long doGetRowCount(String tableName) { public long doGetRowCount(String tableName) throws JdbcSQLException {
final String sql = StringUtilsExt.replaceArrayMerge(sqlGetRowCount, new String[] { "#tableName#" }, final String sql = StringUtilsExt.replaceArrayMerge(sqlGetRowCount, new String[] { "#tableName#" },
new String[] { tableName }); new String[] { tableName });
logger.debug("JDBC::doGetRowCount sql={}", sql); logger.debug("JDBC::doGetRowCount sql={}", sql);
final @Nullable Long result = Yank.queryScalar(sql, Long.class, null); try {
return Objects.requireNonNullElse(result, 0L); final @Nullable Long result = Yank.queryScalar(sql, Long.class, null);
return Objects.requireNonNullElse(result, 0L);
} catch (YankSQLException e) {
throw new JdbcSQLException(e);
}
} }
/************* /*************

View File

@ -10,7 +10,7 @@
* *
* SPDX-License-Identifier: EPL-2.0 * SPDX-License-Identifier: EPL-2.0
*/ */
package org.openhab.persistence.jdbc.db; package org.openhab.persistence.jdbc.internal.db;
import java.time.ZoneId; import java.time.ZoneId;
import java.util.List; import java.util.List;
@ -23,16 +23,18 @@ import javax.measure.Unit;
import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jdt.annotation.Nullable;
import org.knowm.yank.Yank; import org.knowm.yank.Yank;
import org.knowm.yank.exceptions.YankSQLException;
import org.openhab.core.items.Item; import org.openhab.core.items.Item;
import org.openhab.core.library.items.NumberItem; import org.openhab.core.library.items.NumberItem;
import org.openhab.core.persistence.FilterCriteria; import org.openhab.core.persistence.FilterCriteria;
import org.openhab.core.persistence.FilterCriteria.Ordering; import org.openhab.core.persistence.FilterCriteria.Ordering;
import org.openhab.core.persistence.HistoricItem; import org.openhab.core.persistence.HistoricItem;
import org.openhab.core.types.State; import org.openhab.core.types.State;
import org.openhab.persistence.jdbc.dto.ItemVO; import org.openhab.persistence.jdbc.internal.dto.ItemVO;
import org.openhab.persistence.jdbc.dto.ItemsVO; import org.openhab.persistence.jdbc.internal.dto.ItemsVO;
import org.openhab.persistence.jdbc.dto.JdbcHistoricItem; import org.openhab.persistence.jdbc.internal.dto.JdbcHistoricItem;
import org.openhab.persistence.jdbc.utils.StringUtilsExt; import org.openhab.persistence.jdbc.internal.exceptions.JdbcSQLException;
import org.openhab.persistence.jdbc.internal.utils.StringUtilsExt;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -106,39 +108,53 @@ public class JdbcDerbyDAO extends JdbcBaseDAO {
* ITEMS DAOs * * ITEMS DAOs *
**************/ **************/
@Override @Override
public @Nullable Integer doPingDB() { public @Nullable Integer doPingDB() throws JdbcSQLException {
return Yank.queryScalar(sqlPingDB, Integer.class, null); try {
return Yank.queryScalar(sqlPingDB, Integer.class, null);
} catch (YankSQLException e) {
throw new JdbcSQLException(e);
}
} }
@Override @Override
public boolean doIfTableExists(ItemsVO vo) { public boolean doIfTableExists(ItemsVO vo) throws JdbcSQLException {
String sql = StringUtilsExt.replaceArrayMerge(sqlIfTableExists, new String[] { "#searchTable#" }, String sql = StringUtilsExt.replaceArrayMerge(sqlIfTableExists, new String[] { "#searchTable#" },
new String[] { vo.getItemsManageTable().toUpperCase() }); new String[] { vo.getItemsManageTable().toUpperCase() });
logger.debug("JDBC::doIfTableExists sql={}", sql); logger.debug("JDBC::doIfTableExists sql={}", sql);
final @Nullable String result = Yank.queryScalar(sql, String.class, null); try {
return Objects.nonNull(result); final @Nullable String result = Yank.queryScalar(sql, String.class, null);
return Objects.nonNull(result);
} catch (YankSQLException e) {
throw new JdbcSQLException(e);
}
} }
@Override @Override
public Long doCreateNewEntryInItemsTable(ItemsVO vo) { public Long doCreateNewEntryInItemsTable(ItemsVO vo) throws JdbcSQLException {
String sql = StringUtilsExt.replaceArrayMerge(sqlCreateNewEntryInItemsTable, String sql = StringUtilsExt.replaceArrayMerge(sqlCreateNewEntryInItemsTable,
new String[] { "#itemsManageTable#", "#itemname#" }, new String[] { "#itemsManageTable#", "#itemname#" },
new String[] { vo.getItemsManageTable().toUpperCase(), vo.getItemName() }); new String[] { vo.getItemsManageTable().toUpperCase(), vo.getItemName() });
logger.debug("JDBC::doCreateNewEntryInItemsTable sql={}", sql); logger.debug("JDBC::doCreateNewEntryInItemsTable sql={}", sql);
return Yank.insert(sql, null); try {
return Yank.insert(sql, null);
} catch (YankSQLException e) {
throw new JdbcSQLException(e);
}
} }
@Override @Override
public ItemsVO doCreateItemsTableIfNot(ItemsVO vo) { public ItemsVO doCreateItemsTableIfNot(ItemsVO vo) throws JdbcSQLException {
// boolean tableExists = Yank.queryScalar(SQL_IF_TABLE_EXISTS.replace("#searchTable#",
// vo.getItemsManageTable().toUpperCase()), String.class, null) == null;
boolean tableExists = doIfTableExists(vo); boolean tableExists = doIfTableExists(vo);
if (!tableExists) { if (!tableExists) {
String sql = StringUtilsExt.replaceArrayMerge(sqlCreateItemsTableIfNot, String sql = StringUtilsExt.replaceArrayMerge(sqlCreateItemsTableIfNot,
new String[] { "#itemsManageTable#", "#colname#", "#coltype#" }, new String[] { "#itemsManageTable#", "#colname#", "#coltype#" },
new String[] { vo.getItemsManageTable().toUpperCase(), vo.getColname(), vo.getColtype() }); new String[] { vo.getItemsManageTable().toUpperCase(), vo.getColname(), vo.getColtype() });
logger.debug("JDBC::doCreateItemsTableIfNot tableExists={} therefore sql={}", tableExists, sql); logger.debug("JDBC::doCreateItemsTableIfNot tableExists={} therefore sql={}", tableExists, sql);
Yank.execute(sql, null); try {
Yank.execute(sql, null);
} catch (YankSQLException e) {
throw new JdbcSQLException(e);
}
} else { } else {
logger.debug("JDBC::doCreateItemsTableIfNot tableExists={}, did not CREATE TABLE", tableExists); logger.debug("JDBC::doCreateItemsTableIfNot tableExists={}, did not CREATE TABLE", tableExists);
} }
@ -149,15 +165,19 @@ public class JdbcDerbyDAO extends JdbcBaseDAO {
* ITEM DAOs * * ITEM DAOs *
*************/ *************/
@Override @Override
public void doCreateItemTable(ItemVO vo) { public void doCreateItemTable(ItemVO vo) throws JdbcSQLException {
String sql = StringUtilsExt.replaceArrayMerge(sqlCreateItemTable, String sql = StringUtilsExt.replaceArrayMerge(sqlCreateItemTable,
new String[] { "#tableName#", "#dbType#", "#tablePrimaryKey#" }, new String[] { "#tableName#", "#dbType#", "#tablePrimaryKey#" },
new String[] { vo.getTableName(), vo.getDbType(), sqlTypes.get("tablePrimaryKey") }); new String[] { vo.getTableName(), vo.getDbType(), sqlTypes.get("tablePrimaryKey") });
Yank.execute(sql, null); try {
Yank.execute(sql, null);
} catch (YankSQLException e) {
throw new JdbcSQLException(e);
}
} }
@Override @Override
public void doStoreItemValue(Item item, State itemState, ItemVO vo) { public void doStoreItemValue(Item item, State itemState, ItemVO vo) throws JdbcSQLException {
ItemVO storedVO = storeItemValueProvider(item, itemState, vo); ItemVO storedVO = storeItemValueProvider(item, itemState, vo);
String sql = StringUtilsExt.replaceArrayMerge(sqlInsertItemValue, String sql = StringUtilsExt.replaceArrayMerge(sqlInsertItemValue,
new String[] { "#tableName#", "#dbType#", "#tablePrimaryValue#" }, new String[] { "#tableName#", "#dbType#", "#tablePrimaryValue#" },
@ -165,14 +185,23 @@ public class JdbcDerbyDAO extends JdbcBaseDAO {
sqlTypes.get("tablePrimaryValue") }); sqlTypes.get("tablePrimaryValue") });
Object[] params = { storedVO.getValue() }; Object[] params = { storedVO.getValue() };
logger.debug("JDBC::doStoreItemValue sql={} value='{}'", sql, storedVO.getValue()); logger.debug("JDBC::doStoreItemValue sql={} value='{}'", sql, storedVO.getValue());
Yank.execute(sql, params); try {
Yank.execute(sql, params);
} catch (YankSQLException e) {
throw new JdbcSQLException(e);
}
} }
@Override @Override
public List<HistoricItem> doGetHistItemFilterQuery(Item item, FilterCriteria filter, int numberDecimalcount, public List<HistoricItem> doGetHistItemFilterQuery(Item item, FilterCriteria filter, int numberDecimalcount,
String table, String name, ZoneId timeZone) { String table, String name, ZoneId timeZone) throws JdbcSQLException {
String sql = histItemFilterQueryProvider(filter, numberDecimalcount, table, name, timeZone); String sql = histItemFilterQueryProvider(filter, numberDecimalcount, table, name, timeZone);
List<Object[]> m = Yank.queryObjectArrays(sql, null); List<Object[]> m;
try {
m = Yank.queryObjectArrays(sql, null);
} catch (YankSQLException e) {
throw new JdbcSQLException(e);
}
logger.debug("JDBC::doGetHistItemFilterQuery got Array length={}", m.size()); logger.debug("JDBC::doGetHistItemFilterQuery got Array length={}", m.size());
// we already retrieve the unit here once as it is a very costly operation // we already retrieve the unit here once as it is a very costly operation
String itemName = item.getName(); String itemName = item.getName();

View File

@ -10,14 +10,16 @@
* *
* SPDX-License-Identifier: EPL-2.0 * SPDX-License-Identifier: EPL-2.0
*/ */
package org.openhab.persistence.jdbc.db; package org.openhab.persistence.jdbc.internal.db;
import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.NonNullByDefault;
import org.knowm.yank.Yank; import org.knowm.yank.Yank;
import org.knowm.yank.exceptions.YankSQLException;
import org.openhab.core.items.Item; import org.openhab.core.items.Item;
import org.openhab.core.types.State; import org.openhab.core.types.State;
import org.openhab.persistence.jdbc.dto.ItemVO; import org.openhab.persistence.jdbc.internal.dto.ItemVO;
import org.openhab.persistence.jdbc.utils.StringUtilsExt; import org.openhab.persistence.jdbc.internal.exceptions.JdbcSQLException;
import org.openhab.persistence.jdbc.internal.utils.StringUtilsExt;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -77,14 +79,18 @@ public class JdbcH2DAO extends JdbcBaseDAO {
* ITEM DAOs * * ITEM DAOs *
*************/ *************/
@Override @Override
public void doStoreItemValue(Item item, State itemState, ItemVO vo) { public void doStoreItemValue(Item item, State itemState, ItemVO vo) throws JdbcSQLException {
ItemVO storedVO = storeItemValueProvider(item, itemState, vo); ItemVO storedVO = storeItemValueProvider(item, itemState, vo);
String sql = StringUtilsExt.replaceArrayMerge(sqlInsertItemValue, String sql = StringUtilsExt.replaceArrayMerge(sqlInsertItemValue,
new String[] { "#tableName#", "#dbType#", "#tablePrimaryValue#" }, new String[] { "#tableName#", "#dbType#", "#tablePrimaryValue#" },
new String[] { storedVO.getTableName(), storedVO.getDbType(), sqlTypes.get("tablePrimaryValue") }); new String[] { storedVO.getTableName(), storedVO.getDbType(), sqlTypes.get("tablePrimaryValue") });
Object[] params = { storedVO.getValue() }; Object[] params = { storedVO.getValue() };
logger.debug("JDBC::doStoreItemValue sql={} value='{}'", sql, storedVO.getValue()); logger.debug("JDBC::doStoreItemValue sql={} value='{}'", sql, storedVO.getValue());
Yank.execute(sql, params); try {
Yank.execute(sql, params);
} catch (YankSQLException e) {
throw new JdbcSQLException(e);
}
} }
/**************************** /****************************

View File

@ -10,16 +10,18 @@
* *
* SPDX-License-Identifier: EPL-2.0 * SPDX-License-Identifier: EPL-2.0
*/ */
package org.openhab.persistence.jdbc.db; package org.openhab.persistence.jdbc.internal.db;
import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jdt.annotation.Nullable;
import org.knowm.yank.Yank; import org.knowm.yank.Yank;
import org.knowm.yank.exceptions.YankSQLException;
import org.openhab.core.items.Item; import org.openhab.core.items.Item;
import org.openhab.core.types.State; import org.openhab.core.types.State;
import org.openhab.persistence.jdbc.dto.ItemVO; import org.openhab.persistence.jdbc.internal.dto.ItemVO;
import org.openhab.persistence.jdbc.dto.ItemsVO; import org.openhab.persistence.jdbc.internal.dto.ItemsVO;
import org.openhab.persistence.jdbc.utils.StringUtilsExt; import org.openhab.persistence.jdbc.internal.exceptions.JdbcSQLException;
import org.openhab.persistence.jdbc.internal.utils.StringUtilsExt;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -83,34 +85,46 @@ public class JdbcHsqldbDAO extends JdbcBaseDAO {
* ITEMS DAOs * * ITEMS DAOs *
**************/ **************/
@Override @Override
public @Nullable Integer doPingDB() { public @Nullable Integer doPingDB() throws JdbcSQLException {
return Yank.queryScalar(sqlPingDB, Integer.class, null); try {
return Yank.queryScalar(sqlPingDB, Integer.class, null);
} catch (YankSQLException e) {
throw new JdbcSQLException(e);
}
} }
@Override @Override
public ItemsVO doCreateItemsTableIfNot(ItemsVO vo) { public ItemsVO doCreateItemsTableIfNot(ItemsVO vo) throws JdbcSQLException {
String sql = StringUtilsExt.replaceArrayMerge(sqlCreateItemsTableIfNot, String sql = StringUtilsExt.replaceArrayMerge(sqlCreateItemsTableIfNot,
new String[] { "#itemsManageTable#", "#colname#", "#coltype#", "#itemsManageTable#" }, new String[] { "#itemsManageTable#", "#colname#", "#coltype#", "#itemsManageTable#" },
new String[] { vo.getItemsManageTable(), vo.getColname(), vo.getColtype(), vo.getItemsManageTable() }); new String[] { vo.getItemsManageTable(), vo.getColname(), vo.getColtype(), vo.getItemsManageTable() });
logger.debug("JDBC::doCreateItemsTableIfNot sql={}", sql); logger.debug("JDBC::doCreateItemsTableIfNot sql={}", sql);
Yank.execute(sql, null); try {
Yank.execute(sql, null);
} catch (YankSQLException e) {
throw new JdbcSQLException(e);
}
return vo; return vo;
} }
@Override @Override
public Long doCreateNewEntryInItemsTable(ItemsVO vo) { public Long doCreateNewEntryInItemsTable(ItemsVO vo) throws JdbcSQLException {
String sql = StringUtilsExt.replaceArrayMerge(sqlCreateNewEntryInItemsTable, String sql = StringUtilsExt.replaceArrayMerge(sqlCreateNewEntryInItemsTable,
new String[] { "#itemsManageTable#", "#itemname#" }, new String[] { "#itemsManageTable#", "#itemname#" },
new String[] { vo.getItemsManageTable(), vo.getItemName() }); new String[] { vo.getItemsManageTable(), vo.getItemName() });
logger.debug("JDBC::doCreateNewEntryInItemsTable sql={}", sql); logger.debug("JDBC::doCreateNewEntryInItemsTable sql={}", sql);
return Yank.insert(sql, null); try {
return Yank.insert(sql, null);
} catch (YankSQLException e) {
throw new JdbcSQLException(e);
}
} }
/************* /*************
* ITEM DAOs * * ITEM DAOs *
*************/ *************/
@Override @Override
public void doStoreItemValue(Item item, State itemState, ItemVO vo) { public void doStoreItemValue(Item item, State itemState, ItemVO vo) throws JdbcSQLException {
ItemVO storedVO = storeItemValueProvider(item, itemState, vo); ItemVO storedVO = storeItemValueProvider(item, itemState, vo);
String sql = StringUtilsExt.replaceArrayMerge(sqlInsertItemValue, String sql = StringUtilsExt.replaceArrayMerge(sqlInsertItemValue,
new String[] { "#tableName#", "#dbType#", "#tableName#", "#tablePrimaryValue#" }, new String[] { "#tableName#", "#dbType#", "#tableName#", "#tablePrimaryValue#" },
@ -118,7 +132,11 @@ public class JdbcHsqldbDAO extends JdbcBaseDAO {
sqlTypes.get("tablePrimaryValue") }); sqlTypes.get("tablePrimaryValue") });
Object[] params = { storedVO.getValue() }; Object[] params = { storedVO.getValue() };
logger.debug("JDBC::doStoreItemValue sql={} value='{}'", sql, storedVO.getValue()); logger.debug("JDBC::doStoreItemValue sql={} value='{}'", sql, storedVO.getValue());
Yank.execute(sql, params); try {
Yank.execute(sql, params);
} catch (YankSQLException e) {
throw new JdbcSQLException(e);
}
} }
/**************************** /****************************

View File

@ -10,14 +10,16 @@
* *
* SPDX-License-Identifier: EPL-2.0 * SPDX-License-Identifier: EPL-2.0
*/ */
package org.openhab.persistence.jdbc.db; package org.openhab.persistence.jdbc.internal.db;
import java.util.Objects; import java.util.Objects;
import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jdt.annotation.Nullable;
import org.knowm.yank.Yank; import org.knowm.yank.Yank;
import org.openhab.persistence.jdbc.utils.DbMetaData; import org.knowm.yank.exceptions.YankSQLException;
import org.openhab.persistence.jdbc.internal.exceptions.JdbcSQLException;
import org.openhab.persistence.jdbc.internal.utils.DbMetaData;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -95,9 +97,13 @@ public class JdbcMariadbDAO extends JdbcBaseDAO {
* ITEMS DAOs * * ITEMS DAOs *
**************/ **************/
@Override @Override
public @Nullable Integer doPingDB() { public @Nullable Integer doPingDB() throws JdbcSQLException {
final @Nullable Long result = Yank.queryScalar(sqlPingDB, Long.class, null); try {
return Objects.nonNull(result) ? result.intValue() : null; final @Nullable Long result = Yank.queryScalar(sqlPingDB, Long.class, null);
return Objects.nonNull(result) ? result.intValue() : null;
} catch (YankSQLException e) {
throw new JdbcSQLException(e);
}
} }
/************* /*************

View File

@ -10,14 +10,16 @@
* *
* SPDX-License-Identifier: EPL-2.0 * SPDX-License-Identifier: EPL-2.0
*/ */
package org.openhab.persistence.jdbc.db; package org.openhab.persistence.jdbc.internal.db;
import java.util.Objects; import java.util.Objects;
import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jdt.annotation.Nullable;
import org.knowm.yank.Yank; import org.knowm.yank.Yank;
import org.openhab.persistence.jdbc.utils.DbMetaData; import org.knowm.yank.exceptions.YankSQLException;
import org.openhab.persistence.jdbc.internal.exceptions.JdbcSQLException;
import org.openhab.persistence.jdbc.internal.utils.DbMetaData;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -98,9 +100,13 @@ public class JdbcMysqlDAO extends JdbcBaseDAO {
* ITEMS DAOs * * ITEMS DAOs *
**************/ **************/
@Override @Override
public @Nullable Integer doPingDB() { public @Nullable Integer doPingDB() throws JdbcSQLException {
final @Nullable Long result = Yank.queryScalar(sqlPingDB, Long.class, null); try {
return Objects.nonNull(result) ? result.intValue() : null; final @Nullable Long result = Yank.queryScalar(sqlPingDB, Long.class, null);
return Objects.nonNull(result) ? result.intValue() : null;
} catch (YankSQLException e) {
throw new JdbcSQLException(e);
}
} }
/************* /*************

View File

@ -10,20 +10,22 @@
* *
* SPDX-License-Identifier: EPL-2.0 * SPDX-License-Identifier: EPL-2.0
*/ */
package org.openhab.persistence.jdbc.db; package org.openhab.persistence.jdbc.internal.db;
import java.time.ZoneId; import java.time.ZoneId;
import java.util.List; import java.util.List;
import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.NonNullByDefault;
import org.knowm.yank.Yank; import org.knowm.yank.Yank;
import org.knowm.yank.exceptions.YankSQLException;
import org.openhab.core.items.Item; import org.openhab.core.items.Item;
import org.openhab.core.persistence.FilterCriteria; import org.openhab.core.persistence.FilterCriteria;
import org.openhab.core.persistence.FilterCriteria.Ordering; import org.openhab.core.persistence.FilterCriteria.Ordering;
import org.openhab.core.types.State; import org.openhab.core.types.State;
import org.openhab.persistence.jdbc.dto.ItemVO; import org.openhab.persistence.jdbc.internal.dto.ItemVO;
import org.openhab.persistence.jdbc.dto.ItemsVO; import org.openhab.persistence.jdbc.internal.dto.ItemsVO;
import org.openhab.persistence.jdbc.utils.StringUtilsExt; import org.openhab.persistence.jdbc.internal.exceptions.JdbcSQLException;
import org.openhab.persistence.jdbc.internal.utils.StringUtilsExt;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -108,45 +110,61 @@ public class JdbcPostgresqlDAO extends JdbcBaseDAO {
* ITEMS DAOs * * ITEMS DAOs *
**************/ **************/
@Override @Override
public ItemsVO doCreateItemsTableIfNot(ItemsVO vo) { public ItemsVO doCreateItemsTableIfNot(ItemsVO vo) throws JdbcSQLException {
String sql = StringUtilsExt.replaceArrayMerge(sqlCreateItemsTableIfNot, String sql = StringUtilsExt.replaceArrayMerge(sqlCreateItemsTableIfNot,
new String[] { "#itemsManageTable#", "#colname#", "#coltype#", "#itemsManageTable#" }, new String[] { "#itemsManageTable#", "#colname#", "#coltype#", "#itemsManageTable#" },
new String[] { vo.getItemsManageTable(), vo.getColname(), vo.getColtype(), vo.getItemsManageTable() }); new String[] { vo.getItemsManageTable(), vo.getColname(), vo.getColtype(), vo.getItemsManageTable() });
logger.debug("JDBC::doCreateItemsTableIfNot sql={}", sql); logger.debug("JDBC::doCreateItemsTableIfNot sql={}", sql);
Yank.execute(sql, null); try {
Yank.execute(sql, null);
} catch (YankSQLException e) {
throw new JdbcSQLException(e);
}
return vo; return vo;
} }
@Override @Override
public Long doCreateNewEntryInItemsTable(ItemsVO vo) { public Long doCreateNewEntryInItemsTable(ItemsVO vo) throws JdbcSQLException {
String sql = StringUtilsExt.replaceArrayMerge(sqlCreateNewEntryInItemsTable, String sql = StringUtilsExt.replaceArrayMerge(sqlCreateNewEntryInItemsTable,
new String[] { "#itemsManageTable#", "#itemname#" }, new String[] { "#itemsManageTable#", "#itemname#" },
new String[] { vo.getItemsManageTable(), vo.getItemName() }); new String[] { vo.getItemsManageTable(), vo.getItemName() });
logger.debug("JDBC::doCreateNewEntryInItemsTable sql={}", sql); logger.debug("JDBC::doCreateNewEntryInItemsTable sql={}", sql);
return Yank.insert(sql, null); try {
return Yank.insert(sql, null);
} catch (YankSQLException e) {
throw new JdbcSQLException(e);
}
} }
@Override @Override
public List<ItemsVO> doGetItemTables(ItemsVO vo) { public List<ItemsVO> doGetItemTables(ItemsVO vo) throws JdbcSQLException {
String sql = StringUtilsExt.replaceArrayMerge(this.sqlGetItemTables, String sql = StringUtilsExt.replaceArrayMerge(this.sqlGetItemTables,
new String[] { "#itemsManageTable#", "#itemsManageTable#" }, new String[] { "#itemsManageTable#", "#itemsManageTable#" },
new String[] { vo.getItemsManageTable(), vo.getItemsManageTable() }); new String[] { vo.getItemsManageTable(), vo.getItemsManageTable() });
this.logger.debug("JDBC::doGetItemTables sql={}", sql); this.logger.debug("JDBC::doGetItemTables sql={}", sql);
return Yank.queryBeanList(sql, ItemsVO.class, null); try {
return Yank.queryBeanList(sql, ItemsVO.class, null);
} catch (YankSQLException e) {
throw new JdbcSQLException(e);
}
} }
/************* /*************
* ITEM DAOs * * ITEM DAOs *
*************/ *************/
@Override @Override
public void doStoreItemValue(Item item, State itemState, ItemVO vo) { public void doStoreItemValue(Item item, State itemState, ItemVO vo) throws JdbcSQLException {
ItemVO storedVO = storeItemValueProvider(item, itemState, vo); ItemVO storedVO = storeItemValueProvider(item, itemState, vo);
String sql = StringUtilsExt.replaceArrayMerge(sqlInsertItemValue, String sql = StringUtilsExt.replaceArrayMerge(sqlInsertItemValue,
new String[] { "#tableName#", "#dbType#", "#tablePrimaryValue#" }, new String[] { "#tableName#", "#dbType#", "#tablePrimaryValue#" },
new String[] { storedVO.getTableName(), storedVO.getDbType(), sqlTypes.get("tablePrimaryValue") }); new String[] { storedVO.getTableName(), storedVO.getDbType(), sqlTypes.get("tablePrimaryValue") });
Object[] params = { storedVO.getValue() }; Object[] params = { storedVO.getValue() };
logger.debug("JDBC::doStoreItemValue sql={} value='{}'", sql, storedVO.getValue()); logger.debug("JDBC::doStoreItemValue sql={} value='{}'", sql, storedVO.getValue());
Yank.execute(sql, params); try {
Yank.execute(sql, params);
} catch (YankSQLException e) {
throw new JdbcSQLException(e);
}
} }
/**************************** /****************************

View File

@ -10,16 +10,18 @@
* *
* SPDX-License-Identifier: EPL-2.0 * SPDX-License-Identifier: EPL-2.0
*/ */
package org.openhab.persistence.jdbc.db; package org.openhab.persistence.jdbc.internal.db;
import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable; import org.eclipse.jdt.annotation.Nullable;
import org.knowm.yank.Yank; import org.knowm.yank.Yank;
import org.knowm.yank.exceptions.YankSQLException;
import org.openhab.core.items.Item; import org.openhab.core.items.Item;
import org.openhab.core.types.State; import org.openhab.core.types.State;
import org.openhab.persistence.jdbc.dto.ItemVO; import org.openhab.persistence.jdbc.internal.dto.ItemVO;
import org.openhab.persistence.jdbc.dto.ItemsVO; import org.openhab.persistence.jdbc.internal.dto.ItemsVO;
import org.openhab.persistence.jdbc.utils.StringUtilsExt; import org.openhab.persistence.jdbc.internal.exceptions.JdbcSQLException;
import org.openhab.persistence.jdbc.internal.utils.StringUtilsExt;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -79,17 +81,25 @@ public class JdbcSqliteDAO extends JdbcBaseDAO {
**************/ **************/
@Override @Override
public @Nullable String doGetDB() { public @Nullable String doGetDB() throws JdbcSQLException {
return Yank.queryColumn(sqlGetDB, "file", String.class, null).get(0); try {
return Yank.queryColumn(sqlGetDB, "file", String.class, null).get(0);
} catch (YankSQLException e) {
throw new JdbcSQLException(e);
}
} }
@Override @Override
public ItemsVO doCreateItemsTableIfNot(ItemsVO vo) { public ItemsVO doCreateItemsTableIfNot(ItemsVO vo) throws JdbcSQLException {
String sql = StringUtilsExt.replaceArrayMerge(sqlCreateItemsTableIfNot, String sql = StringUtilsExt.replaceArrayMerge(sqlCreateItemsTableIfNot,
new String[] { "#itemsManageTable#", "#colname#", "#coltype#" }, new String[] { "#itemsManageTable#", "#colname#", "#coltype#" },
new String[] { vo.getItemsManageTable(), vo.getColname(), vo.getColtype() }); new String[] { vo.getItemsManageTable(), vo.getColname(), vo.getColtype() });
logger.debug("JDBC::doCreateItemsTableIfNot sql={}", sql); logger.debug("JDBC::doCreateItemsTableIfNot sql={}", sql);
Yank.execute(sql, null); try {
Yank.execute(sql, null);
} catch (YankSQLException e) {
throw new JdbcSQLException(e);
}
return vo; return vo;
} }
@ -97,14 +107,18 @@ public class JdbcSqliteDAO extends JdbcBaseDAO {
* ITEM DAOs * * ITEM DAOs *
*************/ *************/
@Override @Override
public void doStoreItemValue(Item item, State itemState, ItemVO vo) { public void doStoreItemValue(Item item, State itemState, ItemVO vo) throws JdbcSQLException {
ItemVO storedVO = storeItemValueProvider(item, itemState, vo); ItemVO storedVO = storeItemValueProvider(item, itemState, vo);
String sql = StringUtilsExt.replaceArrayMerge(sqlInsertItemValue, String sql = StringUtilsExt.replaceArrayMerge(sqlInsertItemValue,
new String[] { "#tableName#", "#dbType#", "#tablePrimaryValue#" }, new String[] { "#tableName#", "#dbType#", "#tablePrimaryValue#" },
new String[] { storedVO.getTableName(), storedVO.getDbType(), sqlTypes.get("tablePrimaryValue") }); new String[] { storedVO.getTableName(), storedVO.getDbType(), sqlTypes.get("tablePrimaryValue") });
Object[] params = { storedVO.getValue() }; Object[] params = { storedVO.getValue() };
logger.debug("JDBC::doStoreItemValue sql={} value='{}'", sql, storedVO.getValue()); logger.debug("JDBC::doStoreItemValue sql={} value='{}'", sql, storedVO.getValue());
Yank.execute(sql, params); try {
Yank.execute(sql, params);
} catch (YankSQLException e) {
throw new JdbcSQLException(e);
}
} }
/**************************** /****************************

View File

@ -10,14 +10,16 @@
* *
* SPDX-License-Identifier: EPL-2.0 * SPDX-License-Identifier: EPL-2.0
*/ */
package org.openhab.persistence.jdbc.db; package org.openhab.persistence.jdbc.internal.db;
import java.util.Properties; import java.util.Properties;
import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.NonNullByDefault;
import org.knowm.yank.Yank; import org.knowm.yank.Yank;
import org.openhab.persistence.jdbc.dto.ItemVO; import org.knowm.yank.exceptions.YankSQLException;
import org.openhab.persistence.jdbc.utils.StringUtilsExt; import org.openhab.persistence.jdbc.internal.dto.ItemVO;
import org.openhab.persistence.jdbc.internal.exceptions.JdbcSQLException;
import org.openhab.persistence.jdbc.internal.utils.StringUtilsExt;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -45,11 +47,15 @@ public class JdbcTimescaledbDAO extends JdbcPostgresqlDAO {
} }
@Override @Override
public void doCreateItemTable(ItemVO vo) { public void doCreateItemTable(ItemVO vo) throws JdbcSQLException {
super.doCreateItemTable(vo); super.doCreateItemTable(vo);
String sql = StringUtilsExt.replaceArrayMerge(this.sqlCreateHypertable, new String[] { "#tableName#" }, String sql = StringUtilsExt.replaceArrayMerge(this.sqlCreateHypertable, new String[] { "#tableName#" },
new String[] { vo.getTableName() }); new String[] { vo.getTableName() });
this.logger.debug("JDBC::doCreateItemTable sql={}", sql); this.logger.debug("JDBC::doCreateItemTable sql={}", sql);
Yank.queryScalar(sql, Boolean.class, null); try {
Yank.queryScalar(sql, Boolean.class, null);
} catch (YankSQLException e) {
throw new JdbcSQLException(e);
}
} }
} }

View File

@ -10,7 +10,7 @@
* *
* SPDX-License-Identifier: EPL-2.0 * SPDX-License-Identifier: EPL-2.0
*/ */
package org.openhab.persistence.jdbc.dto; package org.openhab.persistence.jdbc.internal.dto;
import java.io.Serializable; import java.io.Serializable;
import java.util.Date; import java.util.Date;

View File

@ -10,7 +10,7 @@
* *
* SPDX-License-Identifier: EPL-2.0 * SPDX-License-Identifier: EPL-2.0
*/ */
package org.openhab.persistence.jdbc.dto; package org.openhab.persistence.jdbc.internal.dto;
import java.io.Serializable; import java.io.Serializable;
import java.util.Objects; import java.util.Objects;

View File

@ -10,7 +10,7 @@
* *
* SPDX-License-Identifier: EPL-2.0 * SPDX-License-Identifier: EPL-2.0
*/ */
package org.openhab.persistence.jdbc.dto; package org.openhab.persistence.jdbc.internal.dto;
import java.time.ZonedDateTime; import java.time.ZonedDateTime;

View File

@ -10,7 +10,7 @@
* *
* SPDX-License-Identifier: EPL-2.0 * SPDX-License-Identifier: EPL-2.0
*/ */
package org.openhab.persistence.jdbc.dto; package org.openhab.persistence.jdbc.internal.dto;
import java.util.Date; import java.util.Date;

View File

@ -0,0 +1,34 @@
/**
* Copyright (c) 2010-2022 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.persistence.jdbc.internal.exceptions;
import org.eclipse.jdt.annotation.NonNullByDefault;
/**
* Base class for JDBC exceptions.
*
* @author Jacob Laursen - Initial contribution
*/
@NonNullByDefault
public class JdbcException extends Exception {
private static final long serialVersionUID = 1911437557128995424L;
public JdbcException(String message) {
super(message);
}
public JdbcException(String message, Throwable cause) {
super(message, cause);
}
}

View File

@ -0,0 +1,33 @@
/**
* Copyright (c) 2010-2022 Contributors to the openHAB project
*
* See the NOTICE file(s) distributed with this work for additional
* information.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.openhab.persistence.jdbc.internal.exceptions;
import java.util.Objects;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.knowm.yank.exceptions.YankSQLException;
/**
* This exception wraps a {@link YankSQLException}.
*
* @author Jacob Laursen - Initial contribution
*/
@NonNullByDefault
public class JdbcSQLException extends JdbcException {
private static final long serialVersionUID = 4562191548585905000L;
public JdbcSQLException(YankSQLException sqlException) {
super(Objects.requireNonNull(sqlException.getMessage()));
}
}

View File

@ -10,7 +10,7 @@
* *
* SPDX-License-Identifier: EPL-2.0 * SPDX-License-Identifier: EPL-2.0
*/ */
package org.openhab.persistence.jdbc.utils; package org.openhab.persistence.jdbc.internal.utils;
import java.sql.DatabaseMetaData; import java.sql.DatabaseMetaData;
import java.sql.SQLException; import java.sql.SQLException;
@ -42,7 +42,6 @@ public class DbMetaData {
public DbMetaData() { public DbMetaData() {
HikariDataSource h = Yank.getDefaultConnectionPool(); HikariDataSource h = Yank.getDefaultConnectionPool();
// HikariDataSource h = Yank.getDataSource();
DatabaseMetaData meta; DatabaseMetaData meta;
try { try {

View File

@ -10,7 +10,7 @@
* *
* SPDX-License-Identifier: EPL-2.0 * SPDX-License-Identifier: EPL-2.0
*/ */
package org.openhab.persistence.jdbc.utils; package org.openhab.persistence.jdbc.internal.utils;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.RoundingMode; import java.math.RoundingMode;

View File

@ -10,7 +10,7 @@
* *
* SPDX-License-Identifier: EPL-2.0 * SPDX-License-Identifier: EPL-2.0
*/ */
package org.openhab.persistence.jdbc.utils; package org.openhab.persistence.jdbc.internal.utils;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;

View File

@ -30,7 +30,7 @@ import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension; import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings; import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness; import org.mockito.quality.Strictness;
import org.openhab.persistence.jdbc.dto.ItemVO; import org.openhab.persistence.jdbc.internal.dto.ItemVO;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import ch.qos.logback.classic.Level; import ch.qos.logback.classic.Level;

View File

@ -10,7 +10,7 @@
* *
* SPDX-License-Identifier: EPL-2.0 * SPDX-License-Identifier: EPL-2.0
*/ */
package org.openhab.persistence.jdbc.db; package org.openhab.persistence.jdbc.internal.db;
import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;