Skip to content

Commit

Permalink
[jdbc] Fix case-sensitive table names for PostgreSQL (openhab#17597)
Browse files Browse the repository at this point in the history
* Fix case-sensitive table names for PostgreSQL

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
  • Loading branch information
jlaur authored and matchews committed Dec 16, 2024
1 parent a0f171a commit 90e6ac5
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 160 deletions.
8 changes: 4 additions & 4 deletions bundles/org.openhab.persistence.jdbc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ All item- and event-related configuration is done in the file `persistence/jdbc.

To configure this service as the default persistence service for openHAB, add or change the line

```
```ini
org.openhab.core.persistence:default=jdbc
```

Expand All @@ -89,7 +89,7 @@ in the file `services/runtime.cfg`.

services/jdbc.cfg

```
```ini
url=jdbc:postgresql://192.168.0.1:5432/testPostgresql
```

Expand All @@ -102,7 +102,7 @@ To connect to an Oracle Autonomous Database, use the instructions at https://www

Your services/jdbc.cfg should contain the following minimal configuration for connecting to an Oracle Autonomous Database:

```
```ini
url=jdbc:oracle:thin:@dbname?TNS_ADMIN=./dbname_tns_admin_folder
user=openhab
password=openhab_password
Expand Down Expand Up @@ -147,7 +147,7 @@ Here is an example of a configuration for a MySQL database named `testMysql` wit

services/jdbc.cfg

```
```ini
url=jdbc:mysql://192.168.0.1:3306/testMysql
user=test
password=test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ public boolean doIfTableExists(String tableName) throws JdbcSQLException {
public Long doCreateNewEntryInItemsTable(ItemsVO vo) throws JdbcSQLException {
String sql = StringUtilsExt.replaceArrayMerge(sqlCreateNewEntryInItemsTable,
new String[] { "#itemsManageTable#", "#itemname#" },
new String[] { vo.getItemsManageTable(), vo.getItemName() });
new String[] { formattedIdentifier(vo.getItemsManageTable()), vo.getItemName() });
logger.debug("JDBC::doCreateNewEntryInItemsTable sql={}", sql);
try {
return Yank.insert(sql, null);
Expand All @@ -308,7 +308,7 @@ public Long doCreateNewEntryInItemsTable(ItemsVO vo) throws JdbcSQLException {
public ItemsVO doCreateItemsTableIfNot(ItemsVO vo) throws JdbcSQLException {
String sql = StringUtilsExt.replaceArrayMerge(sqlCreateItemsTableIfNot,
new String[] { "#itemsManageTable#", "#colname#", "#coltype#" },
new String[] { vo.getItemsManageTable(), vo.getColname(), vo.getColtype() });
new String[] { formattedIdentifier(vo.getItemsManageTable()), vo.getColname(), vo.getColtype() });
logger.debug("JDBC::doCreateItemsTableIfNot sql={}", sql);
try {
Yank.execute(sql, null);
Expand All @@ -320,7 +320,7 @@ public ItemsVO doCreateItemsTableIfNot(ItemsVO vo) throws JdbcSQLException {

public ItemsVO doDropItemsTableIfExists(ItemsVO vo) throws JdbcSQLException {
String sql = StringUtilsExt.replaceArrayMerge(sqlDropItemsTableIfExists, new String[] { "#itemsManageTable#" },
new String[] { vo.getItemsManageTable() });
new String[] { formattedIdentifier(vo.getItemsManageTable()) });
logger.debug("JDBC::doDropItemsTableIfExists sql={}", sql);
try {
Yank.execute(sql, null);
Expand All @@ -332,7 +332,7 @@ public ItemsVO doDropItemsTableIfExists(ItemsVO vo) throws JdbcSQLException {

public void doDropTable(String tableName) throws JdbcSQLException {
String sql = StringUtilsExt.replaceArrayMerge(sqlDropTable, new String[] { "#tableName#" },
new String[] { tableName });
new String[] { formattedIdentifier(tableName) });
logger.debug("JDBC::doDropTable sql={}", sql);
try {
Yank.execute(sql, null);
Expand All @@ -344,7 +344,7 @@ public void doDropTable(String tableName) throws JdbcSQLException {
public void doDeleteItemsEntry(ItemsVO vo) throws JdbcSQLException {
String sql = StringUtilsExt.replaceArrayMerge(sqlDeleteItemsEntry,
new String[] { "#itemsManageTable#", "#itemname#" },
new String[] { vo.getItemsManageTable(), vo.getItemName() });
new String[] { formattedIdentifier(vo.getItemsManageTable()), vo.getItemName() });
logger.debug("JDBC::doDeleteItemsEntry sql={}", sql);
try {
Yank.execute(sql, null);
Expand All @@ -355,7 +355,7 @@ public void doDeleteItemsEntry(ItemsVO vo) throws JdbcSQLException {

public List<ItemsVO> doGetItemIDTableNames(ItemsVO vo) throws JdbcSQLException {
String sql = StringUtilsExt.replaceArrayMerge(sqlGetItemIDTableNames, new String[] { "#itemsManageTable#" },
new String[] { vo.getItemsManageTable() });
new String[] { formattedIdentifier(vo.getItemsManageTable()) });
logger.debug("JDBC::doGetItemIDTableNames sql={}", sql);
try {
return Yank.queryBeanList(sql, ItemsVO.class, null);
Expand Down Expand Up @@ -405,8 +405,8 @@ public void doUpdateItemTableNames(List<ItemVO> vol) throws JdbcSQLException {

public void doCreateItemTable(ItemVO vo) throws JdbcSQLException {
String sql = StringUtilsExt.replaceArrayMerge(sqlCreateItemTable,
new String[] { "#tableName#", "#dbType#", "#tablePrimaryKey#" },
new String[] { vo.getTableName(), vo.getDbType(), sqlTypes.get("tablePrimaryKey") });
new String[] { "#tableName#", "#dbType#", "#tablePrimaryKey#" }, new String[] {
formattedIdentifier(vo.getTableName()), vo.getDbType(), sqlTypes.get("tablePrimaryKey") });
logger.debug("JDBC::doCreateItemTable sql={}", sql);
try {
Yank.execute(sql, null);
Expand All @@ -418,8 +418,8 @@ public void doCreateItemTable(ItemVO vo) throws JdbcSQLException {
public void doAlterTableColumn(String tableName, String columnName, String columnType, boolean nullable)
throws JdbcSQLException {
String sql = StringUtilsExt.replaceArrayMerge(sqlAlterTableColumn,
new String[] { "#tableName#", "#columnName#", "#columnType#" },
new String[] { tableName, columnName, nullable ? columnType : columnType + " NOT NULL" });
new String[] { "#tableName#", "#columnName#", "#columnType#" }, new String[] {
formattedIdentifier(tableName), columnName, nullable ? columnType : columnType + " NOT NULL" });
logger.debug("JDBC::doAlterTableColumn sql={}", sql);
try {
Yank.execute(sql, null);
Expand All @@ -432,7 +432,7 @@ public void doStoreItemValue(Item item, State itemState, ItemVO vo) throws JdbcS
ItemVO storedVO = storeItemValueProvider(item, itemState, vo);
String sql = StringUtilsExt.replaceArrayMerge(sqlInsertItemValue,
new String[] { "#tableName#", "#tablePrimaryValue#" },
new String[] { storedVO.getTableName(), sqlTypes.get("tablePrimaryValue") });
new String[] { formattedIdentifier(storedVO.getTableName()), sqlTypes.get("tablePrimaryValue") });
Object[] params = { storedVO.getValue(), storedVO.getValue() };
logger.debug("JDBC::doStoreItemValue sql={} value='{}'", sql, storedVO.getValue());
try {
Expand All @@ -445,7 +445,8 @@ public void doStoreItemValue(Item item, State itemState, ItemVO vo) throws JdbcS
public void doStoreItemValue(Item item, State itemState, ItemVO vo, ZonedDateTime date) throws JdbcSQLException {
ItemVO storedVO = storeItemValueProvider(item, itemState, vo);
String sql = StringUtilsExt.replaceArrayMerge(sqlInsertItemValue,
new String[] { "#tableName#", "#tablePrimaryValue#" }, new String[] { storedVO.getTableName(), "?" });
new String[] { "#tableName#", "#tablePrimaryValue#" },
new String[] { formattedIdentifier(storedVO.getTableName()), "?" });
java.sql.Timestamp timestamp = new java.sql.Timestamp(date.toInstant().toEpochMilli());
Object[] params = { timestamp, storedVO.getValue(), storedVO.getValue() };
logger.debug("JDBC::doStoreItemValue sql={} timestamp={} value='{}'", sql, timestamp, storedVO.getValue());
Expand Down Expand Up @@ -490,7 +491,7 @@ public void doDeleteItemValues(FilterCriteria filter, String table, ZoneId timeZ

public long doGetRowCount(String tableName) throws JdbcSQLException {
final String sql = StringUtilsExt.replaceArrayMerge(sqlGetRowCount, new String[] { "#tableName#" },
new String[] { tableName });
new String[] { formattedIdentifier(tableName) });
logger.debug("JDBC::doGetRowCount sql={}", sql);
try {
final @Nullable Long result = Yank.queryScalar(sql, Long.class, null);
Expand Down Expand Up @@ -519,8 +520,8 @@ protected String histItemFilterQueryProvider(FilterCriteria filter, int numberDe
// SELECT time, ROUND(value,3) FROM number_item_0114 ORDER BY time DESC LIMIT 0,1
// rounding HALF UP
String queryString = "NUMBERITEM".equalsIgnoreCase(simpleName) && numberDecimalcount > -1
? "SELECT time, ROUND(value," + numberDecimalcount + ") FROM " + table
: "SELECT time, value FROM " + table;
? "SELECT time, ROUND(value," + numberDecimalcount + ") FROM " + formattedIdentifier(table)
: "SELECT time, value FROM " + formattedIdentifier(table);
if (!filterString.isEmpty()) {
queryString += filterString;
}
Expand All @@ -532,8 +533,8 @@ protected String histItemFilterDeleteProvider(FilterCriteria filter, String tabl
logger.debug("JDBC::histItemFilterDeleteProvider filter = {}, table = {}", filter, table);

String filterString = resolveTimeFilter(filter, timeZone);
String deleteString = filterString.isEmpty() ? "TRUNCATE TABLE " + table
: "DELETE FROM " + table + filterString;
String deleteString = filterString.isEmpty() ? "TRUNCATE TABLE " + formattedIdentifier(table)
: "DELETE FROM " + formattedIdentifier(table) + filterString;
logger.debug("JDBC::delete deleteString = {}", deleteString);
return deleteString;
}
Expand All @@ -554,7 +555,12 @@ protected String resolveTimeFilter(FilterCriteria filter, ZoneId timeZone) {
}

private String updateItemTableNamesProvider(ItemVO itemTable) {
String queryString = "ALTER TABLE " + itemTable.getTableName() + " RENAME TO " + itemTable.getNewTableName();
String newTableName = itemTable.getNewTableName();
if (newTableName == null) {
throw new IllegalArgumentException("New table name is not provided");
}
String queryString = "ALTER TABLE " + formattedIdentifier(itemTable.getTableName()) + " RENAME TO "
+ formattedIdentifier(newTableName);
logger.debug("JDBC::query queryString = {}", queryString);
return queryString;
}
Expand Down Expand Up @@ -757,7 +763,11 @@ protected String objectAsString(Object v) {
throw new UnsupportedOperationException("String of type '" + v.getClass().getName() + "' is not supported");
}

public String getItemType(Item i) {
protected String formattedIdentifier(String identifier) {
return identifier;
}

private String getItemType(Item i) {
Item item = i;
String def = "STRINGITEM";
if (i instanceof GroupItem groupItem) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,18 @@

import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

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.knowm.yank.exceptions.YankSQLException;
import org.openhab.core.items.Item;
import org.openhab.core.library.items.NumberItem;
import org.openhab.core.persistence.FilterCriteria;
import org.openhab.core.persistence.FilterCriteria.Ordering;
import org.openhab.core.persistence.HistoricItem;
import org.openhab.core.types.State;
import org.openhab.persistence.jdbc.internal.dto.ItemVO;
import org.openhab.persistence.jdbc.internal.dto.ItemsVO;
import org.openhab.persistence.jdbc.internal.dto.JdbcHistoricItem;
import org.openhab.persistence.jdbc.internal.exceptions.JdbcSQLException;
import org.openhab.persistence.jdbc.internal.utils.StringUtilsExt;
import org.slf4j.Logger;
Expand Down Expand Up @@ -121,7 +113,7 @@ public void initAfterFirstDbConnection() {
@Override
public boolean doIfTableExists(ItemsVO vo) throws JdbcSQLException {
String sql = StringUtilsExt.replaceArrayMerge(sqlIfTableExists, new String[] { "#searchTable#" },
new String[] { vo.getItemsManageTable().toUpperCase() });
new String[] { formattedIdentifier(vo.getItemsManageTable()) });
logger.debug("JDBC::doIfTableExists sql={}", sql);
try {
final @Nullable String result = Yank.queryScalar(sql, String.class, null);
Expand All @@ -131,26 +123,13 @@ public boolean doIfTableExists(ItemsVO vo) throws JdbcSQLException {
}
}

@Override
public Long doCreateNewEntryInItemsTable(ItemsVO vo) throws JdbcSQLException {
String sql = StringUtilsExt.replaceArrayMerge(sqlCreateNewEntryInItemsTable,
new String[] { "#itemsManageTable#", "#itemname#" },
new String[] { vo.getItemsManageTable().toUpperCase(), vo.getItemName() });
logger.debug("JDBC::doCreateNewEntryInItemsTable sql={}", sql);
try {
return Yank.insert(sql, null);
} catch (YankSQLException e) {
throw new JdbcSQLException(e);
}
}

@Override
public ItemsVO doCreateItemsTableIfNot(ItemsVO vo) throws JdbcSQLException {
boolean tableExists = doIfTableExists(vo);
if (!tableExists) {
String sql = StringUtilsExt.replaceArrayMerge(sqlCreateItemsTableIfNot,
new String[] { "#itemsManageTable#", "#colname#", "#coltype#" },
new String[] { vo.getItemsManageTable().toUpperCase(), vo.getColname(), vo.getColtype() });
new String[] { formattedIdentifier(vo.getItemsManageTable()), vo.getColname(), vo.getColtype() });
logger.debug("JDBC::doCreateItemsTableIfNot tableExists={} therefore sql={}", tableExists, sql);
try {
Yank.execute(sql, null);
Expand All @@ -166,24 +145,12 @@ public ItemsVO doCreateItemsTableIfNot(ItemsVO vo) throws JdbcSQLException {
/*************
* ITEM DAOs *
*************/
@Override
public void doCreateItemTable(ItemVO vo) throws JdbcSQLException {
String sql = StringUtilsExt.replaceArrayMerge(sqlCreateItemTable,
new String[] { "#tableName#", "#dbType#", "#tablePrimaryKey#" },
new String[] { vo.getTableName(), vo.getDbType(), sqlTypes.get("tablePrimaryKey") });
try {
Yank.execute(sql, null);
} catch (YankSQLException e) {
throw new JdbcSQLException(e);
}
}

@Override
public void doStoreItemValue(Item item, State itemState, ItemVO vo) throws JdbcSQLException {
ItemVO storedVO = storeItemValueProvider(item, itemState, vo);
String sql = StringUtilsExt.replaceArrayMerge(sqlInsertItemValue,
new String[] { "#tableName#", "#dbType#", "#tablePrimaryValue#" },
new String[] { storedVO.getTableName().toUpperCase(), storedVO.getDbType(),
new String[] { formattedIdentifier(storedVO.getTableName()), storedVO.getDbType(),
sqlTypes.get("tablePrimaryValue") });
Object[] params = { storedVO.getValue() };
logger.debug("JDBC::doStoreItemValue sql={} value='{}'", sql, storedVO.getValue());
Expand All @@ -199,7 +166,7 @@ public void doStoreItemValue(Item item, State itemState, ItemVO vo, ZonedDateTim
ItemVO storedVO = storeItemValueProvider(item, itemState, vo);
String sql = StringUtilsExt.replaceArrayMerge(sqlInsertItemValue,
new String[] { "#tableName#", "#dbType#", "#tablePrimaryValue#" },
new String[] { storedVO.getTableName().toUpperCase(), storedVO.getDbType(), "?" });
new String[] { formattedIdentifier(storedVO.getTableName()), storedVO.getDbType(), "?" });
java.sql.Timestamp timestamp = new java.sql.Timestamp(date.toInstant().toEpochMilli());
Object[] params = { timestamp, storedVO.getValue() };
logger.debug("JDBC::doStoreItemValue sql={} timestamp={} value='{}'", sql, timestamp, storedVO.getValue());
Expand All @@ -210,26 +177,6 @@ public void doStoreItemValue(Item item, State itemState, ItemVO vo, ZonedDateTim
}
}

@Override
public List<HistoricItem> doGetHistItemFilterQuery(Item item, FilterCriteria filter, int numberDecimalcount,
String table, String name, ZoneId timeZone) throws JdbcSQLException {
String sql = histItemFilterQueryProvider(filter, numberDecimalcount, table, name, timeZone);
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());
// we already retrieve the unit here once as it is a very costly operation
String itemName = item.getName();
Unit<? extends Quantity<?>> unit = item instanceof NumberItem ni ? ni.getUnit() : null;
return m.stream().map(o -> {
logger.debug("JDBC::doGetHistItemFilterQuery 0='{}' 1='{}'", o[0], o[1]);
return new JdbcHistoricItem(itemName, objectAsState(item, unit, o[1]), objectAsZonedDateTime(o[0]));
}).collect(Collectors.<HistoricItem> toList());
}

/****************************
* SQL generation Providers *
****************************/
Expand Down Expand Up @@ -280,7 +227,7 @@ protected String histItemFilterQueryProvider(FilterCriteria filter, int numberDe
queryString += "5 AS DECIMAL(31," + numberDecimalcount + "))"; // 31 is DECIMAL max precision
// https://db.apache.org/derby/docs/10.0/manuals/develop/develop151.html
} else {
queryString += " value FROM " + table.toUpperCase();
queryString += " value FROM " + formattedIdentifier(table);
}

if (!filterString.isEmpty()) {
Expand All @@ -293,6 +240,10 @@ protected String histItemFilterQueryProvider(FilterCriteria filter, int numberDe
/*****************
* H E L P E R S *
*****************/
@Override
protected String formattedIdentifier(String identifier) {
return identifier.toUpperCase();
}

/******************************
* public Getters and Setters *
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ public void doStoreItemValue(Item item, State itemState, ItemVO vo) throws JdbcS
ItemVO storedVO = storeItemValueProvider(item, itemState, vo);
String sql = StringUtilsExt.replaceArrayMerge(sqlInsertItemValue,
new String[] { "#tableName#", "#dbType#", "#tablePrimaryValue#" },
new String[] { storedVO.getTableName(), storedVO.getDbType(), sqlTypes.get("tablePrimaryValue") });
new String[] { formattedIdentifier(storedVO.getTableName()), storedVO.getDbType(),
sqlTypes.get("tablePrimaryValue") });
Object[] params = { storedVO.getValue() };
logger.debug("JDBC::doStoreItemValue sql={} value='{}'", sql, storedVO.getValue());
try {
Expand All @@ -100,7 +101,7 @@ public void doStoreItemValue(Item item, State itemState, ItemVO vo, ZonedDateTim
ItemVO storedVO = storeItemValueProvider(item, itemState, vo);
String sql = StringUtilsExt.replaceArrayMerge(sqlInsertItemValue,
new String[] { "#tableName#", "#dbType#", "#tablePrimaryValue#" },
new String[] { storedVO.getTableName(), storedVO.getDbType(), "?" });
new String[] { formattedIdentifier(storedVO.getTableName()), storedVO.getDbType(), "?" });
java.sql.Timestamp timestamp = new java.sql.Timestamp(date.toInstant().toEpochMilli());
Object[] params = { timestamp, storedVO.getValue() };
logger.debug("JDBC::doStoreItemValue sql={} timestamp={} value='{}'", sql, timestamp, storedVO.getValue());
Expand Down
Loading

0 comments on commit 90e6ac5

Please sign in to comment.