mirror of
https://github.com/openhab/openhab-addons.git
synced 2025-01-25 14:55:55 +01:00
[jdbc] Fix tableCaseSensitiveItemNames
for PostgreSQL/TimescaleDB (#17587)
Signed-off-by: Jonathan van de Giessen <jonathan.vandegiessen@student.hu.nl> Signed-off-by: Ciprian Pascu <contact@ciprianpascu.ro>
This commit is contained in:
parent
5a749d03fe
commit
a21e7d8dff
@ -60,25 +60,28 @@ public class JdbcPostgresqlDAO extends JdbcBaseDAO {
|
|||||||
logger.debug("JDBC::initSqlQueries: '{}'", this.getClass().getSimpleName());
|
logger.debug("JDBC::initSqlQueries: '{}'", this.getClass().getSimpleName());
|
||||||
// System Information Functions: https://www.postgresql.org/docs/9.2/static/functions-info.html
|
// System Information Functions: https://www.postgresql.org/docs/9.2/static/functions-info.html
|
||||||
sqlGetDB = "SELECT CURRENT_DATABASE()";
|
sqlGetDB = "SELECT CURRENT_DATABASE()";
|
||||||
sqlIfTableExists = "SELECT * FROM PG_TABLES WHERE TABLENAME='#searchTable#'";
|
sqlIfTableExists = "SELECT * FROM PG_TABLES WHERE TABLENAME='\"#searchTable#\"'";
|
||||||
sqlCreateItemsTableIfNot = "CREATE TABLE IF NOT EXISTS #itemsManageTable# (itemid SERIAL NOT NULL, #colname# #coltype# NOT NULL, CONSTRAINT #itemsManageTable#_pkey PRIMARY KEY (itemid))";
|
sqlDropTable = "DROP TABLE \"#tableName#\"";
|
||||||
sqlCreateNewEntryInItemsTable = "INSERT INTO items (itemname) SELECT itemname FROM #itemsManageTable# UNION VALUES ('#itemname#') EXCEPT SELECT itemname FROM items";
|
sqlCreateItemsTableIfNot = "CREATE TABLE IF NOT EXISTS \"#itemsManageTable#\" (itemid SERIAL NOT NULL, #colname# #coltype# NOT NULL, CONSTRAINT #itemsManageTable#_pkey PRIMARY KEY (itemid))";
|
||||||
|
sqlCreateNewEntryInItemsTable = "INSERT INTO items (itemname) SELECT itemname FROM \"#itemsManageTable#\" UNION VALUES ('#itemname#') EXCEPT SELECT itemname FROM items";
|
||||||
sqlGetItemTables = """
|
sqlGetItemTables = """
|
||||||
SELECT table_name FROM information_schema.tables WHERE table_type='BASE TABLE' AND table_schema=(SELECT table_schema \
|
SELECT table_name FROM information_schema.tables WHERE table_type='BASE TABLE' AND table_schema=(SELECT table_schema \
|
||||||
FROM information_schema.tables WHERE table_type='BASE TABLE' AND table_name='#itemsManageTable#') AND NOT table_name='#itemsManageTable#'\
|
FROM information_schema.tables WHERE table_type='BASE TABLE' AND table_name='\"#itemsManageTable#\"') AND NOT table_name='\"#itemsManageTable#\"'\
|
||||||
""";
|
""";
|
||||||
// The PostgreSQL equivalent to MySQL columns.column_type is data_type (e.g. "timestamp with time zone") and
|
// The PostgreSQL equivalent to MySQL columns.column_type is data_type (e.g. "timestamp with time zone") and
|
||||||
// udt_name which contains a shorter alias (e.g. "timestamptz"). We alias data_type as "column_type" and
|
// udt_name which contains a shorter alias (e.g. "timestamptz"). We alias data_type as "column_type" and
|
||||||
// udt_name as "column_type_alias" to be compatible with the 'Column' class used in Yank.queryBeanList
|
// udt_name as "column_type_alias" to be compatible with the 'Column' class used in Yank.queryBeanList
|
||||||
sqlGetTableColumnTypes = """
|
sqlGetTableColumnTypes = """
|
||||||
SELECT column_name, data_type as column_type, udt_name as column_type_alias, is_nullable FROM information_schema.columns \
|
SELECT column_name, data_type as column_type, udt_name as column_type_alias, is_nullable FROM information_schema.columns \
|
||||||
WHERE table_name='#tableName#' AND table_catalog='#jdbcUriDatabaseName#' AND table_schema=(SELECT table_schema FROM information_schema.tables WHERE table_type='BASE TABLE' \
|
WHERE table_name='\"#tableName#\"' AND table_catalog='#jdbcUriDatabaseName#' AND table_schema=(SELECT table_schema FROM information_schema.tables WHERE table_type='BASE TABLE' \
|
||||||
AND table_name='#itemsManageTable#')\
|
AND table_name='\"#itemsManageTable#\"')\
|
||||||
""";
|
""";
|
||||||
// NOTICE: on PostgreSql >= 9.5, sqlInsertItemValue query template is modified to do an "upsert" (overwrite
|
// NOTICE: on PostgreSql >= 9.5, sqlInsertItemValue query template is modified to do an "upsert" (overwrite
|
||||||
// existing value). The version check and query change is performed at initAfterFirstDbConnection()
|
// existing value). The version check and query change is performed at initAfterFirstDbConnection()
|
||||||
sqlInsertItemValue = "INSERT INTO #tableName# (TIME, VALUE) VALUES( #tablePrimaryValue#, CAST( ? as #dbType#) )";
|
sqlInsertItemValue = "INSERT INTO \"#tableName#\" (TIME, VALUE) VALUES( #tablePrimaryValue#, CAST( ? as #dbType#) )";
|
||||||
sqlAlterTableColumn = "ALTER TABLE #tableName# ALTER COLUMN #columnName# TYPE #columnType#";
|
sqlCreateItemTable = "CREATE TABLE IF NOT EXISTS \"#tableName#\" (time #tablePrimaryKey# NOT NULL, value #dbType#, PRIMARY KEY(time))";
|
||||||
|
sqlAlterTableColumn = "ALTER TABLE \"#tableName#\" ALTER COLUMN #columnName# TYPE #columnType#";
|
||||||
|
sqlGetRowCount = "SELECT COUNT(*) FROM \"#tableName#\"";
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -92,7 +95,7 @@ public class JdbcPostgresqlDAO extends JdbcBaseDAO {
|
|||||||
if (dbMeta.isDbVersionGreater(9, 4)) {
|
if (dbMeta.isDbVersionGreater(9, 4)) {
|
||||||
logger.debug("JDBC::initAfterFirstDbConnection: Values with the same time will be upserted (Pg >= 9.5)");
|
logger.debug("JDBC::initAfterFirstDbConnection: Values with the same time will be upserted (Pg >= 9.5)");
|
||||||
sqlInsertItemValue = """
|
sqlInsertItemValue = """
|
||||||
INSERT INTO #tableName# (TIME, VALUE) VALUES( #tablePrimaryValue#, CAST( ? as #dbType#) )\
|
INSERT INTO \"#tableName#\" (TIME, VALUE) VALUES( #tablePrimaryValue#, CAST( ? as #dbType#) )\
|
||||||
ON CONFLICT (TIME) DO UPDATE SET VALUE=EXCLUDED.VALUE\
|
ON CONFLICT (TIME) DO UPDATE SET VALUE=EXCLUDED.VALUE\
|
||||||
""";
|
""";
|
||||||
}
|
}
|
||||||
@ -213,7 +216,7 @@ public class JdbcPostgresqlDAO extends JdbcBaseDAO {
|
|||||||
Yank.execute(sql, null);
|
Yank.execute(sql, null);
|
||||||
if (!nullable) {
|
if (!nullable) {
|
||||||
String sql2 = StringUtilsExt.replaceArrayMerge(
|
String sql2 = StringUtilsExt.replaceArrayMerge(
|
||||||
"ALTER TABLE #tableName# ALTER COLUMN #columnName# SET NOT NULL",
|
"ALTER TABLE \"#tableName#\" ALTER COLUMN #columnName# SET NOT NULL",
|
||||||
new String[] { "#tableName#", "#columnName#" }, new String[] { tableName, columnName });
|
new String[] { "#tableName#", "#columnName#" }, new String[] { tableName, columnName });
|
||||||
logger.info("JDBC::doAlterTableColumn sql={}", sql2);
|
logger.info("JDBC::doAlterTableColumn sql={}", sql2);
|
||||||
Yank.execute(sql2, null);
|
Yank.execute(sql2, null);
|
||||||
|
@ -12,12 +12,14 @@
|
|||||||
*/
|
*/
|
||||||
package org.openhab.persistence.jdbc.internal.db;
|
package org.openhab.persistence.jdbc.internal.db;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
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.knowm.yank.exceptions.YankSQLException;
|
import org.knowm.yank.exceptions.YankSQLException;
|
||||||
import org.openhab.persistence.jdbc.internal.dto.ItemVO;
|
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.exceptions.JdbcSQLException;
|
||||||
import org.openhab.persistence.jdbc.internal.utils.StringUtilsExt;
|
import org.openhab.persistence.jdbc.internal.utils.StringUtilsExt;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
@ -34,7 +36,8 @@ import org.slf4j.LoggerFactory;
|
|||||||
public class JdbcTimescaledbDAO extends JdbcPostgresqlDAO {
|
public class JdbcTimescaledbDAO extends JdbcPostgresqlDAO {
|
||||||
private final Logger logger = LoggerFactory.getLogger(JdbcTimescaledbDAO.class);
|
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')";
|
||||||
|
private final String sqlGetItemTables = "SELECT hypertable_name as table_name FROM timescaledb_information.hypertables WHERE hypertable_name != '\"#itemsManageTable#\"'";
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Properties getConnectionProperties() {
|
public Properties getConnectionProperties() {
|
||||||
@ -46,6 +49,10 @@ public class JdbcTimescaledbDAO extends JdbcPostgresqlDAO {
|
|||||||
return properties;
|
return properties;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*************
|
||||||
|
* ITEM DAOs *
|
||||||
|
*************/
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void doCreateItemTable(ItemVO vo) throws JdbcSQLException {
|
public void doCreateItemTable(ItemVO vo) throws JdbcSQLException {
|
||||||
super.doCreateItemTable(vo);
|
super.doCreateItemTable(vo);
|
||||||
@ -58,4 +65,16 @@ public class JdbcTimescaledbDAO extends JdbcPostgresqlDAO {
|
|||||||
throw new JdbcSQLException(e);
|
throw new JdbcSQLException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<ItemsVO> doGetItemTables(ItemsVO vo) throws JdbcSQLException {
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user