import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
+import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import javax.measure.Quantity;
import javax.measure.Unit;
+import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.knowm.yank.Yank;
import org.openhab.core.items.GroupItem;
*
* @author Helmut Lehmeyer - Initial contribution
*/
+@NonNullByDefault
public class JdbcBaseDAO {
private final Logger logger = LoggerFactory.getLogger(JdbcBaseDAO.class);
public final Map<String, String> sqlTypes = new HashMap<>();
// Get Database Meta data
- protected DbMetaData dbMeta;
-
- protected String sqlPingDB;
- protected String sqlGetDB;
- protected String sqlIfTableExists;
- protected String sqlCreateNewEntryInItemsTable;
- protected String sqlCreateItemsTableIfNot;
- protected String sqlDeleteItemsEntry;
- protected String sqlGetItemIDTableNames;
- protected String sqlGetItemTables;
- protected String sqlCreateItemTable;
- protected String sqlInsertItemValue;
+ protected @Nullable DbMetaData dbMeta;
+
+ protected String sqlPingDB = "SELECT 1";
+ protected String sqlGetDB = "SELECT DATABASE()";
+ protected String sqlIfTableExists = "SHOW TABLES LIKE '#searchTable#'";
+ protected String sqlCreateNewEntryInItemsTable = "INSERT INTO #itemsManageTable# (ItemName) VALUES ('#itemname#')";
+ protected String sqlCreateItemsTableIfNot = "CREATE TABLE IF NOT EXISTS #itemsManageTable# (ItemId INT NOT NULL AUTO_INCREMENT,#colname# #coltype# NOT NULL,PRIMARY KEY (ItemId))";
+ protected String sqlDeleteItemsEntry = "DELETE FROM items WHERE ItemName=#itemname#";
+ protected String sqlGetItemIDTableNames = "SELECT itemid, itemname FROM #itemsManageTable#";
+ protected String sqlGetItemTables = "SELECT table_name FROM information_schema.tables WHERE table_type='BASE TABLE' AND table_schema='#jdbcUriDatabaseName#' AND NOT table_name='#itemsManageTable#'";
+ protected String sqlCreateItemTable = "CREATE TABLE IF NOT EXISTS #tableName# (time #tablePrimaryKey# NOT NULL, value #dbType#, PRIMARY KEY(time))";
+ protected String sqlInsertItemValue = "INSERT INTO #tableName# (TIME, VALUE) VALUES( #tablePrimaryValue#, ? ) ON DUPLICATE KEY UPDATE VALUE= ?";
/********
* INIT *
public JdbcBaseDAO() {
initSqlTypes();
initDbProps();
- initSqlQueries();
}
/**
*
*/
- private void initSqlQueries() {
- logger.debug("JDBC::initSqlQueries: '{}'", this.getClass().getSimpleName());
- sqlPingDB = "SELECT 1";
- sqlGetDB = "SELECT DATABASE()";
- sqlIfTableExists = "SHOW TABLES LIKE '#searchTable#'";
-
- sqlCreateNewEntryInItemsTable = "INSERT INTO #itemsManageTable# (ItemName) VALUES ('#itemname#')";
- sqlCreateItemsTableIfNot = "CREATE TABLE IF NOT EXISTS #itemsManageTable# (ItemId INT NOT NULL AUTO_INCREMENT,#colname# #coltype# NOT NULL,PRIMARY KEY (ItemId))";
- sqlDeleteItemsEntry = "DELETE FROM items WHERE ItemName=#itemname#";
- sqlGetItemIDTableNames = "SELECT itemid, itemname FROM #itemsManageTable#";
- sqlGetItemTables = "SELECT table_name FROM information_schema.tables WHERE table_type='BASE TABLE' AND table_schema='#jdbcUriDatabaseName#' AND NOT table_name='#itemsManageTable#'";
- sqlCreateItemTable = "CREATE TABLE IF NOT EXISTS #tableName# (time #tablePrimaryKey# NOT NULL, value #dbType#, PRIMARY KEY(time))";
- sqlInsertItemValue = "INSERT INTO #tableName# (TIME, VALUE) VALUES( #tablePrimaryValue#, ? ) ON DUPLICATE KEY UPDATE VALUE= ?";
- }
-
/**
* INFO: http://www.java2s.com/Code/Java/Database-SQL-JDBC/StandardSQLDataTypeswithTheirJavaEquivalents.htm
*/
/**************
* ITEMS DAOs *
**************/
- public Integer doPingDB() {
- return Yank.queryScalar(sqlPingDB, Integer.class, null);
+ public @Nullable Integer doPingDB() {
+ return Yank.queryScalar(sqlPingDB, (Class<@Nullable Integer>) Integer.class, null);
}
- public String doGetDB() {
- return Yank.queryScalar(sqlGetDB, String.class, null);
+ public @Nullable String doGetDB() {
+ return Yank.queryScalar(sqlGetDB, (Class<@Nullable String>) String.class, null);
}
public boolean doIfTableExists(ItemsVO vo) {
String sql = StringUtilsExt.replaceArrayMerge(sqlIfTableExists, new String[] { "#searchTable#" },
new String[] { vo.getItemsManageTable() });
logger.debug("JDBC::doIfTableExists sql={}", sql);
- return Yank.queryScalar(sql, String.class, null) != null;
+ return Yank.queryScalar(sql, (Class<@Nullable String>) String.class, null) != null;
}
public Long doCreateNewEntryInItemsTable(ItemsVO vo) {
}
}
String it = getSqlTypes().get(itemType);
- if (it.toUpperCase().contains("DOUBLE")) {
+ if (it == null) {
+ logger.warn("JDBC::storeItemValueProvider: No SQL type defined for item type {}", itemType);
+ } else if (it.toUpperCase().contains("DOUBLE")) {
vo.setValueTypes(it, java.lang.Double.class);
double value = ((Number) convertedState).doubleValue();
logger.debug("JDBC::storeItemValueProvider: newVal.doubleValue: '{}'", value);
v, unit, v.getClass(), v.getClass().getSimpleName());
if (item instanceof NumberItem) {
String it = getSqlTypes().get("NUMBERITEM");
+ if (it == null) {
+ throw new UnsupportedOperationException("No SQL type defined for item type NUMBERITEM");
+ }
if (it.toUpperCase().contains("DOUBLE")) {
return unit == null ? new DecimalType(((Number) v).doubleValue())
: QuantityType.valueOf(((Number) v).doubleValue(), unit);
} else if (item instanceof ImageItem) {
return RawType.valueOf(objectAsString(v));
} else if (item instanceof ContactItem || item instanceof PlayerItem || item instanceof SwitchItem) {
- return TypeParser.parseState(item.getAcceptedDataTypes(), ((String) v).toString().trim());
+ State state = TypeParser.parseState(item.getAcceptedDataTypes(), ((String) v).toString().trim());
+ if (state == null) {
+ throw new UnsupportedOperationException("Unable to parse state for item " + item.toString());
+ }
+ return state;
} else {
- return TypeParser.parseState(item.getAcceptedDataTypes(), ((String) v).toString());
+ State state = TypeParser.parseState(item.getAcceptedDataTypes(), ((String) v).toString());
+ if (state == null) {
+ throw new UnsupportedOperationException("Unable to parse state for item " + item.toString());
+ }
+ return state;
}
}
logger.debug(
"JDBC::getItemType: Cannot detect ItemType for {} because the GroupItems' base type isn't set in *.items File.",
i.getName());
- item = ((GroupItem) i).getMembers().iterator().next();
- if (item == null) {
+ Iterator<Item> iterator = ((GroupItem) i).getMembers().iterator();
+ if (!iterator.hasNext()) {
logger.debug(
- "JDBC::getItemType: No ItemType found for first Child-Member of GroupItem {}, use ItemType for STRINGITEM as Fallback",
+ "JDBC::getItemType: No Child-Members of GroupItem {}, use ItemType for STRINGITEM as Fallback",
i.getName());
return def;
}
+ item = iterator.next();
}
}
String itemType = item.getClass().getSimpleName().toUpperCase();
}
public String getDataType(Item item) {
- return sqlTypes.get(getItemType(item));
+ String dataType = sqlTypes.get(getItemType(item));
+ if (dataType == null) {
+ throw new UnsupportedOperationException("No data type found for " + getItemType(item));
+ }
+ return dataType;
}
}
import javax.measure.Quantity;
import javax.measure.Unit;
+import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.eclipse.jdt.annotation.Nullable;
import org.knowm.yank.Yank;
import org.openhab.core.items.Item;
import org.openhab.core.library.items.NumberItem;
*
* @author Helmut Lehmeyer - Initial contribution
*/
+@NonNullByDefault
public class JdbcDerbyDAO extends JdbcBaseDAO {
private final Logger logger = LoggerFactory.getLogger(JdbcDerbyDAO.class);
* ITEMS DAOs *
**************/
@Override
- public Integer doPingDB() {
- return Yank.queryScalar(sqlPingDB, Integer.class, null);
+ public @Nullable Integer doPingDB() {
+ return Yank.queryScalar(sqlPingDB, (Class<@Nullable Integer>) Integer.class, null);
}
@Override
String sql = StringUtilsExt.replaceArrayMerge(sqlIfTableExists, new String[] { "#searchTable#" },
new String[] { vo.getItemsManageTable().toUpperCase() });
logger.debug("JDBC::doIfTableExists sql={}", sql);
- return Yank.queryScalar(sql, String.class, null) != null;
+ return Yank.queryScalar(sql, (Class<@Nullable String>) String.class, null) != null;
}
@Override
*/
package org.openhab.persistence.jdbc.db;
+import org.eclipse.jdt.annotation.NonNullByDefault;
import org.knowm.yank.Yank;
import org.openhab.core.items.Item;
import org.openhab.core.types.State;
*
* @author Helmut Lehmeyer - Initial contribution
*/
+@NonNullByDefault
public class JdbcH2DAO extends JdbcBaseDAO {
private final Logger logger = LoggerFactory.getLogger(JdbcH2DAO.class);
*/
package org.openhab.persistence.jdbc.db;
+import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.eclipse.jdt.annotation.Nullable;
import org.knowm.yank.Yank;
import org.openhab.core.items.Item;
import org.openhab.core.types.State;
*
* @author Helmut Lehmeyer - Initial contribution
*/
+@NonNullByDefault
public class JdbcHsqldbDAO extends JdbcBaseDAO {
private final Logger logger = LoggerFactory.getLogger(JdbcHsqldbDAO.class);
* ITEMS DAOs *
**************/
@Override
- public Integer doPingDB() {
- return Yank.queryScalar(sqlPingDB, Integer.class, null);
+ public @Nullable Integer doPingDB() {
+ return Yank.queryScalar(sqlPingDB, (Class<@Nullable Integer>) Integer.class, null);
}
@Override
package org.openhab.persistence.jdbc.db;
import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.eclipse.jdt.annotation.Nullable;
import org.knowm.yank.Yank;
import org.openhab.persistence.jdbc.utils.DbMetaData;
import org.slf4j.Logger;
@Override
public void initAfterFirstDbConnection() {
logger.debug("JDBC::initAfterFirstDbConnection: Initializing step, after db is connected.");
- dbMeta = new DbMetaData();
+ DbMetaData dbMeta = new DbMetaData();
+ this.dbMeta = dbMeta;
// Initialize sqlTypes, depending on DB version for example
if (dbMeta.isDbVersionGreater(5, 1)) {
sqlTypes.put("DATETIMEITEM", "TIMESTAMP(3)");
* ITEMS DAOs *
**************/
@Override
- public Integer doPingDB() {
- return Yank.queryScalar(sqlPingDB, Long.class, null).intValue();
+ public @Nullable Integer doPingDB() {
+ final @Nullable Long result = Yank.queryScalar(sqlPingDB, (Class<@Nullable Long>) Long.class, null);
+ return result != null ? result.intValue() : null;
}
/*************
package org.openhab.persistence.jdbc.db;
import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.eclipse.jdt.annotation.Nullable;
import org.knowm.yank.Yank;
import org.openhab.persistence.jdbc.utils.DbMetaData;
import org.slf4j.Logger;
@Override
public void initAfterFirstDbConnection() {
logger.debug("JDBC::initAfterFirstDbConnection: Initializing step, after db is connected.");
- dbMeta = new DbMetaData();
+ DbMetaData dbMeta = new DbMetaData();
+ this.dbMeta = dbMeta;
// Initialize sqlTypes, depending on DB version for example
if (dbMeta.isDbVersionGreater(5, 5)) {
sqlTypes.put("DATETIMEITEM", "TIMESTAMP(3)");
* ITEMS DAOs *
**************/
@Override
- public Integer doPingDB() {
- return Yank.queryScalar(sqlPingDB, Long.class, null).intValue();
+ public @Nullable Integer doPingDB() {
+ final @Nullable Long result = Yank.queryScalar(sqlPingDB, (Class<@Nullable Long>) Long.class, null);
+ return result != null ? result.intValue() : null;
}
/*************
import java.time.ZoneId;
import java.util.List;
+import org.eclipse.jdt.annotation.NonNullByDefault;
import org.knowm.yank.Yank;
import org.openhab.core.items.Item;
import org.openhab.core.persistence.FilterCriteria;
*
* @author Helmut Lehmeyer - Initial contribution
*/
+@NonNullByDefault
public class JdbcPostgresqlDAO extends JdbcBaseDAO {
private final Logger logger = LoggerFactory.getLogger(JdbcPostgresqlDAO.class);
*/
package org.openhab.persistence.jdbc.db;
+import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.eclipse.jdt.annotation.Nullable;
import org.knowm.yank.Yank;
import org.openhab.core.items.Item;
import org.openhab.core.types.State;
*
* @author Helmut Lehmeyer - Initial contribution
*/
+@NonNullByDefault
public class JdbcSqliteDAO extends JdbcBaseDAO {
private final Logger logger = LoggerFactory.getLogger(JdbcSqliteDAO.class);
**************/
@Override
- public String doGetDB() {
- return Yank.queryColumn(sqlGetDB, "file", String.class, null).get(0);
+ public @Nullable String doGetDB() {
+ return Yank.queryColumn(sqlGetDB, "file", (Class<@Nullable String>) String.class, null).get(0);
}
@Override
import java.util.Properties;
+import org.eclipse.jdt.annotation.NonNullByDefault;
import org.knowm.yank.Yank;
import org.openhab.persistence.jdbc.dto.ItemVO;
import org.openhab.persistence.jdbc.utils.StringUtilsExt;
* @author Riccardo Nimser-Joseph - Initial contribution
* @author Dan Cunningham - Fixes and refactoring
*/
+@NonNullByDefault
public class JdbcTimescaledbDAO extends JdbcPostgresqlDAO {
private final Logger logger = LoggerFactory.getLogger(JdbcTimescaledbDAO.class);
import java.util.regex.Matcher;
import java.util.regex.Pattern;
+import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.eclipse.jdt.annotation.Nullable;
import org.openhab.persistence.jdbc.db.JdbcBaseDAO;
import org.openhab.persistence.jdbc.utils.MovingAverage;
import org.openhab.persistence.jdbc.utils.StringUtilsExt;
*
* @author Helmut Lehmeyer - Initial contribution
*/
+@NonNullByDefault
public class JdbcConfiguration {
private final Logger logger = LoggerFactory.getLogger(JdbcConfiguration.class);
private Map<Object, Object> configuration;
- private JdbcBaseDAO dBDAO = null;
- private String dbName = null;
+ private @NonNullByDefault({}) JdbcBaseDAO dBDAO;
+ private @Nullable String dbName;
boolean dbConnected = false;
boolean driverAvailable = false;
- private String serviceName;
+ private @Nullable String serviceName;
private String name = "jdbc";
public final boolean valid;
public JdbcConfiguration(Map<Object, Object> configuration) {
logger.debug("JDBC::JdbcConfiguration");
- valid = updateConfig(configuration);
+ this.configuration = configuration;
+ valid = updateConfig();
}
- private boolean updateConfig(Map<Object, Object> config) {
- configuration = config;
-
+ private boolean updateConfig() {
logger.debug("JDBC::updateConfig: configuration size = {}", configuration.size());
String user = (String) configuration.get("user");
}
private void setDBDAOClass(String sn) {
- serviceName = "none";
+ String serviceName;
// set database type
- if (sn == null || sn.isBlank() || sn.length() < 2) {
+ if (sn.isBlank() || sn.length() < 2) {
logger.error(
"JDBC::updateConfig: Required database url like 'jdbc:<service>:<host>[:<port>;<attributes>]' - please configure the jdbc:url parameter in openhab.cfg");
+ serviceName = "none";
} else {
serviceName = sn;
}
+ this.serviceName = serviceName;
logger.debug("JDBC::updateConfig: found serviceName = '{}'", serviceName);
// set class for database type
}
String value = (String) configuration.get(key);
logger.debug("JDBC::updateConfig: set sqlTypes: itemType={} value={}", itemType, value);
- dBDAO.sqlTypes.put(itemType, value);
+ if (value != null) {
+ dBDAO.sqlTypes.put(itemType, value);
+ }
}
}
String warn = ""
+ "\n\n\t!!!\n\tTo avoid this error, place an appropriate JDBC driver file for serviceName '{}' in addons directory.\n"
+ "\tCopy missing JDBC-Driver-jar to your openHab/addons Folder.\n\t!!!\n" + "\tDOWNLOAD: \n";
- switch (serviceName) {
- case "derby":
- warn += "\tDerby: version >= 10.11.1.1 from https://mvnrepository.com/artifact/org.apache.derby/derby\n";
- break;
- case "h2":
- warn += "\tH2: version >= 1.4.189 from https://mvnrepository.com/artifact/com.h2database/h2\n";
- break;
- case "hsqldb":
- warn += "\tHSQLDB: version >= 2.3.3 from https://mvnrepository.com/artifact/org.hsqldb/hsqldb\n";
- break;
- case "mariadb":
- warn += "\tMariaDB: version >= 1.2.0 from https://mvnrepository.com/artifact/org.mariadb.jdbc/mariadb-java-client\n";
- break;
- case "mysql":
- warn += "\tMySQL: version >= 5.1.36 from https://mvnrepository.com/artifact/mysql/mysql-connector-java\n";
- break;
- case "postgresql":
- warn += "\tPostgreSQL:version >= 9.4.1208 from https://mvnrepository.com/artifact/org.postgresql/postgresql\n";
- break;
- case "sqlite":
- warn += "\tSQLite: version >= 3.16.1 from https://mvnrepository.com/artifact/org.xerial/sqlite-jdbc\n";
- break;
+ String serviceName = this.serviceName;
+ if (serviceName != null) {
+ switch (serviceName) {
+ case "derby":
+ warn += "\tDerby: version >= 10.11.1.1 from https://mvnrepository.com/artifact/org.apache.derby/derby\n";
+ break;
+ case "h2":
+ warn += "\tH2: version >= 1.4.189 from https://mvnrepository.com/artifact/com.h2database/h2\n";
+ break;
+ case "hsqldb":
+ warn += "\tHSQLDB: version >= 2.3.3 from https://mvnrepository.com/artifact/org.hsqldb/hsqldb\n";
+ break;
+ case "mariadb":
+ warn += "\tMariaDB: version >= 1.2.0 from https://mvnrepository.com/artifact/org.mariadb.jdbc/mariadb-java-client\n";
+ break;
+ case "mysql":
+ warn += "\tMySQL: version >= 5.1.36 from https://mvnrepository.com/artifact/mysql/mysql-connector-java\n";
+ break;
+ case "postgresql":
+ warn += "\tPostgreSQL:version >= 9.4.1208 from https://mvnrepository.com/artifact/org.postgresql/postgresql\n";
+ break;
+ case "sqlite":
+ warn += "\tSQLite: version >= 3.16.1 from https://mvnrepository.com/artifact/org.xerial/sqlite-jdbc\n";
+ break;
+ }
}
logger.warn(warn, serviceName);
}
return name;
}
- public String getServiceName() {
+ public @Nullable String getServiceName() {
return serviceName;
}
return dBDAO;
}
- public String getDbName() {
+ public @Nullable String getDbName() {
return dbName;
}
import java.util.Set;
import java.util.stream.Collectors;
+import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.knowm.yank.Yank;
import org.openhab.core.i18n.TimeZoneProvider;
*
* @author Helmut Lehmeyer - Initial contribution
*/
+@NonNullByDefault
public class JdbcMapper {
private final Logger logger = LoggerFactory.getLogger(JdbcMapper.class);
// Error counter - used to reconnect to database on error
protected int errCnt;
protected boolean initialized = false;
- protected JdbcConfiguration conf = null;
+ protected @NonNullByDefault({}) JdbcConfiguration conf;
protected final Map<String, String> sqlTables = new HashMap<>();
private long afterAccessMin = 10000;
private long afterAccessMax = 0;
logger.debug(
"JDBC::pingDB asking db for name as absolutely first db action, after connection is established.");
String dbName = conf.getDBDAO().doGetDB();
- conf.setDbName(dbName);
- ret = dbName.length() > 0;
+ if (dbName == null) {
+ ret = false;
+ } else {
+ conf.setDbName(dbName);
+ ret = dbName.length() > 0;
+ }
} else {
- ret = conf.getDBDAO().doPingDB() > 0;
+ final @Nullable Integer result = conf.getDBDAO().doPingDB();
+ ret = result != null && result > 0;
}
}
logTime("pingDB", timerStart, System.currentTimeMillis());
logger.debug("JDBC::getDB");
long timerStart = System.currentTimeMillis();
String res = conf.getDBDAO().doGetDB();
- logTime("pingDB", timerStart, System.currentTimeMillis());
- return res;
+ logTime("getDB", timerStart, System.currentTimeMillis());
+ return res != null ? res : "";
}
public ItemsVO createNewEntryInItemsTable(ItemsVO vo) {
public Item storeItemValue(Item item, State itemState, @Nullable ZonedDateTime date) {
logger.debug("JDBC::storeItemValue: item={} state={} date={}", item, itemState, date);
String tableName = getTable(item);
- if (tableName == null) {
- logger.error("JDBC::store: Unable to store item '{}'.", item.getName());
- return item;
- }
long timerStart = System.currentTimeMillis();
if (date == null) {
conf.getDBDAO().doStoreItemValue(item, itemState, new ItemVO(tableName, null));
Item item) {
logger.debug(
"JDBC::getHistItemFilterQuery filter='{}' numberDecimalcount='{}' table='{}' item='{}' itemName='{}'",
- (filter != null), numberDecimalcount, table, item, item.getName());
- if (table != null) {
- long timerStart = System.currentTimeMillis();
- List<HistoricItem> result = conf.getDBDAO().doGetHistItemFilterQuery(item, filter, numberDecimalcount,
- table, item.getName(), timeZoneProvider.getTimeZone());
- logTime("getHistItemFilterQuery", timerStart, System.currentTimeMillis());
- errCnt = 0;
- return result;
- } else {
- logger.error("JDBC::getHistItemFilterQuery: TABLE is NULL; cannot get data from non-existent table.");
- }
- return null;
+ true, numberDecimalcount, table, item, item.getName());
+ long timerStart = System.currentTimeMillis();
+ List<HistoricItem> result = conf.getDBDAO().doGetHistItemFilterQuery(item, filter, numberDecimalcount, table,
+ item.getName(), timeZoneProvider.getTimeZone());
+ logTime("getHistItemFilterQuery", timerStart, System.currentTimeMillis());
+ errCnt = 0;
+ return result;
}
- @SuppressWarnings("null")
public boolean deleteItemValues(FilterCriteria filter, String table) {
- logger.debug("JDBC::deleteItemValues filter='{}' table='{}' itemName='{}'", (filter != null), table,
- filter.getItemName());
- if (table != null) {
- long timerStart = System.currentTimeMillis();
- conf.getDBDAO().doDeleteItemValues(filter, table, timeZoneProvider.getTimeZone());
- logTime("deleteItemValues", timerStart, System.currentTimeMillis());
- errCnt = 0;
- return true;
- } else {
- logger.error("JDBC::deleteItemValues: TABLE is NULL; cannot delete data from non-existent table.");
- return false;
- }
+ logger.debug("JDBC::deleteItemValues filter='{}' table='{}' itemName='{}'", true, table, filter.getItemName());
+ long timerStart = System.currentTimeMillis();
+ conf.getDBDAO().doDeleteItemValues(filter, table, timeZoneProvider.getTimeZone());
+ logTime("deleteItemValues", timerStart, System.currentTimeMillis());
+ errCnt = 0;
+ return true;
}
/***********************
* DATABASE CONNECTION *
***********************/
- @SuppressWarnings("null")
protected boolean openConnection() {
logger.debug("JDBC::openConnection isDriverAvailable: {}", conf.isDriverAvailable());
if (conf.isDriverAvailable() && !conf.isDbConnected()) {
conf.setDbConnected(true);
return true;
} catch (PoolInitializationException e) {
- if (e.getCause() instanceof SQLInvalidAuthorizationSpecException) {
- logger.warn("JDBC::openConnection: failed to open connection: {}", e.getCause().getMessage());
+ Throwable cause = e.getCause();
+ if (cause instanceof SQLInvalidAuthorizationSpecException) {
+ logger.warn("JDBC::openConnection: failed to open connection: {}", cause.getMessage());
} else {
logger.warn("JDBC::openConnection: failed to open connection: {}", e.getMessage());
}
logger.debug("JDBC::getTable: getTableName with rowId={} itemName={}", rowId, itemName);
tableName = getTableName(rowId, itemName);
- // An error occurred adding the item name into the index list!
- if (tableName == null) {
- logger.error("JDBC::getTable: tableName was null; could not create a table for item '{}'", itemName);
- return null;
- }
-
// Create table for item
String dataType = conf.getDBDAO().getDataType(item);
ivo = new ItemVO(tableName, itemName);
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
+import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.eclipse.jdt.annotation.Nullable;
import org.knowm.yank.Yank;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
*
* @author Helmut Lehmeyer - Initial contribution
*/
+@NonNullByDefault
public class DbMetaData {
private final Logger logger = LoggerFactory.getLogger(DbMetaData.class);
private int dbMinorVersion;
private int driverMajorVersion;
private int driverMinorVersion;
- private String dbProductName;
- private String dbProductVersion;
+ private @Nullable String dbProductName;
+ private @Nullable String dbProductVersion;
public DbMetaData() {
HikariDataSource h = Yank.getDefaultConnectionPool();
return false;
}
- public String getDbProductName() {
+ public @Nullable String getDbProductName() {
return dbProductName;
}
- public String getDbProductVersion() {
+ public @Nullable String getDbProductVersion() {
return dbProductVersion;
}
}
props = new Properties(def);
}
- if (url == null || url.length() < 9) {
+ if (url.length() < 9) {
return props;
}
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import java.time.LocalDateTime;
public void testObjectAsStateReturnsValidState() {
State decimalType = jdbcBaseDAO.objectAsState(new NumberItem("testNumberItem"), null, 7.3);
assertInstanceOf(DecimalType.class, decimalType);
- assertThat(decimalType, is(DecimalType.valueOf("7.3")));
+ assertEquals(DecimalType.valueOf("7.3"), decimalType);
State quantityType = jdbcBaseDAO.objectAsState(new NumberItem("testNumberItem"), SIUnits.CELSIUS, 7.3);
assertInstanceOf(QuantityType.class, quantityType);
- assertThat(quantityType, is(QuantityType.valueOf("7.3 °C")));
+ assertEquals(QuantityType.valueOf("7.3 °C"), quantityType);
State dateTimeType = jdbcBaseDAO.objectAsState(new DateTimeItem("testDateTimeItem"), null,
java.sql.Timestamp.valueOf("2021-02-01 23:30:02.049"));
assertInstanceOf(DateTimeType.class, dateTimeType);
- assertThat(dateTimeType, is(DateTimeType.valueOf("2021-02-01T23:30:02.049")));
+ assertEquals(DateTimeType.valueOf("2021-02-01T23:30:02.049"), dateTimeType);
dateTimeType = jdbcBaseDAO.objectAsState(new DateTimeItem("testDateTimeItem"), null,
LocalDateTime.parse("2021-02-01T23:30:02.049"));
assertInstanceOf(DateTimeType.class, dateTimeType);
- assertThat(dateTimeType, is(DateTimeType.valueOf("2021-02-01T23:30:02.049")));
+ assertEquals(DateTimeType.valueOf("2021-02-01T23:30:02.049"), dateTimeType);
State hsbType = jdbcBaseDAO.objectAsState(new ColorItem("testColorItem"), null, "184,100,52");
assertInstanceOf(HSBType.class, hsbType);
- assertThat(hsbType, is(HSBType.valueOf("184,100,52")));
+ assertEquals(HSBType.valueOf("184,100,52"), hsbType);
State percentType = jdbcBaseDAO.objectAsState(new DimmerItem("testDimmerItem"), null, 52);
assertInstanceOf(PercentType.class, percentType);
- assertThat(percentType, is(PercentType.valueOf("52")));
+ assertEquals(PercentType.valueOf("52"), percentType);
percentType = jdbcBaseDAO.objectAsState(new RollershutterItem("testRollershutterItem"), null, 39);
assertInstanceOf(PercentType.class, percentType);
- assertThat(percentType, is(PercentType.valueOf("39")));
+ assertEquals(PercentType.valueOf("39"), percentType);
State openClosedType = jdbcBaseDAO.objectAsState(new ContactItem("testContactItem"), null, "OPEN");
assertInstanceOf(OpenClosedType.class, openClosedType);
State stringListType = jdbcBaseDAO.objectAsState(new CallItem("testCallItem"), null, "0699222222,0179999998");
assertInstanceOf(StringListType.class, stringListType);
- assertThat(stringListType, is(StringListType.valueOf("0699222222,0179999998")));
+ assertEquals(StringListType.valueOf("0699222222,0179999998"), stringListType);
State expectedRawType = new RawType(new byte[0], "application/octet-stream");
State rawType = jdbcBaseDAO.objectAsState(new ImageItem("testImageItem"), null, expectedRawType.toFullString());
State pointType = jdbcBaseDAO.objectAsState(new LocationItem("testLocationItem"), null, "1,2,3");
assertInstanceOf(PointType.class, pointType);
- assertThat(pointType, is(PointType.valueOf("1,2,3")));
+ assertEquals(PointType.valueOf("1,2,3"), pointType);
State stringType = jdbcBaseDAO.objectAsState(new StringItem("testStringItem"), null, "String");
assertInstanceOf(StringType.class, stringType);
- assertThat(stringType, is(StringType.valueOf("String")));
+ assertEquals(StringType.valueOf("String"), stringType);
}
@Test