Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[jdbc] Fix tableCaseSensitiveItemNames for PostgreSQL/TimescaleDB #17587

Merged
merged 12 commits into from
Oct 19, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public List<ItemsVO> doGetItemTables(ItemsVO vo) throws JdbcSQLException {
public List<Column> doGetTableColumns(ItemsVO vo) throws JdbcSQLException {
String sql = StringUtilsExt.replaceArrayMerge(sqlGetTableColumnTypes,
new String[] { "#jdbcUriDatabaseName#", "#tableName#", "#itemsManageTable#" },
new String[] { vo.getJdbcUriDatabaseName(), vo.getTableName(), vo.getItemsManageTable() });
new String[] { vo.getJdbcUriDatabaseName(), vo.getQuotedTableName(), vo.getItemsManageTable() });
jlaur marked this conversation as resolved.
Show resolved Hide resolved
logger.debug("JDBC::doGetTableColumns sql={}", sql);
try {
return Yank.queryBeanList(sql, Column.class, null);
Expand Down Expand Up @@ -227,8 +227,8 @@ public void doAlterTableColumn(String tableName, String columnName, String colum
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(), storedVO.getDbType(), sqlTypes.get("tablePrimaryValue") });
new String[] { "#tableName#", "#dbType#", "#tablePrimaryValue#" }, new String[] {
storedVO.getQuotedTableName(), storedVO.getDbType(), sqlTypes.get("tablePrimaryValue") });
jlaur marked this conversation as resolved.
Show resolved Hide resolved
Object[] params = { storedVO.getValue() };
logger.debug("JDBC::doStoreItemValue sql={} value='{}'", sql, storedVO.getValue());
try {
Expand All @@ -243,7 +243,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[] { storedVO.getQuotedTableName(), storedVO.getDbType(), "?" });
jlaur marked this conversation as resolved.
Show resolved Hide resolved
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,22 @@
*/
package org.openhab.persistence.jdbc.internal.db;

import java.util.List;
import java.util.Properties;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.knowm.yank.Yank;
import org.knowm.yank.exceptions.YankSQLException;
import org.openhab.persistence.jdbc.internal.dto.ItemVO;
import org.openhab.persistence.jdbc.internal.dto.ItemsVO;
import org.openhab.persistence.jdbc.internal.exceptions.JdbcSQLException;
import org.openhab.persistence.jdbc.internal.utils.StringUtilsExt;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Extended Database Configuration class. Class represents the extended database-specific configuration. Overrides and
* Extended Database Configuration class. Class represents the extended
* database-specific configuration. Overrides and
* supplements the default settings from JdbcBaseDAO and JdbcPostgresqlDAO.
*
* @author Riccardo Nimser-Joseph - Initial contribution
Expand All @@ -34,28 +37,46 @@
public class JdbcTimescaledbDAO extends JdbcPostgresqlDAO {
private final Logger logger = LoggerFactory.getLogger(JdbcTimescaledbDAO.class);

private final String sqlCreateHypertable = "SELECT created from create_hypertable('#tableName#', 'time')";
private final String sqlCreateHypertable = "SELECT created FROM create_hypertable('#tableName#', 'time')";
jlaur marked this conversation as resolved.
Show resolved Hide resolved
private final String sqlGetItemTables = "SELECT hypertable_name as table_name FROM timescaledb_information.hypertables WHERE hypertable_name != '#itemsManageTable#'";
jlaur marked this conversation as resolved.
Show resolved Hide resolved

@Override
public Properties getConnectionProperties() {
Properties properties = (Properties) this.databaseProps.clone();
// Adjust the jdbc url since the service name 'timescaledb' is only used to differentiate the DAOs
// Adjust the jdbc url since the service name 'timescaledb' is only used to
// differentiate the DAOs
if (properties.containsKey("jdbcUrl")) {
properties.put("jdbcUrl", properties.getProperty("jdbcUrl").replace("jdbc:timescaledb", "jdbc:postgresql"));
}
return properties;
}

/*************
* ITEM DAOs *
*************/

@Override
public void doCreateItemTable(ItemVO vo) throws JdbcSQLException {
super.doCreateItemTable(vo);
String sql = StringUtilsExt.replaceArrayMerge(this.sqlCreateHypertable, new String[] { "#tableName#" },
new String[] { vo.getTableName() });
new String[] { vo.getQuotedTableName() });
jlaur marked this conversation as resolved.
Show resolved Hide resolved
this.logger.debug("JDBC::doCreateItemTable sql={}", sql);
try {
Yank.queryScalar(sql, Boolean.class, null);
} catch (YankSQLException e) {
throw new JdbcSQLException(e);
}
}

@Override
public List<ItemsVO> doGetItemTables(ItemsVO vo) throws JdbcSQLException {
String sql = StringUtilsExt.replaceArrayMerge(this.sqlGetItemTables, new String[] { "#itemsManageTable#" },
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
String sql = StringUtilsExt.replaceArrayMerge(this.sqlGetItemTables, new String[] { "#itemsManageTable#" },
String sql = StringUtilsExt.replaceArrayMerge(sqlGetItemTables, new String[] { "#itemsManageTable#" },

new String[] { vo.getItemsManageTable() });
this.logger.debug("JDBC::doGetItemTables sql={}", sql);
try {
return Yank.queryBeanList(sql, ItemsVO.class, null);
} catch (YankSQLException e) {
throw new JdbcSQLException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ public String getTableName() {
return tableName;
}

public String getQuotedTableName() {
return "\"" + tableName + "\"";
}

public void setTableName(String tableName) {
this.tableName = tableName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ public String getTableName() {
return tableName;
}

public String getQuotedTableName() {
return "\"" + tableName + "\"";
}

public void setTableName(String tableName) {
this.tableName = tableName;
}
Expand Down