From cf6729adefaaedec2440567393d06abfbff4e501 Mon Sep 17 00:00:00 2001 From: nimric <55734294+nimric@users.noreply.github.com> Date: Sat, 24 Jul 2021 21:29:15 +0200 Subject: [PATCH] [jdbc] Select the proper schema when retrieving the item tables (#11023) Signed-off-by: Riccardo Nimser-Joseph --- .../openhab/persistence/jdbc/db/JdbcPostgresqlDAO.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/bundles/org.openhab.persistence.jdbc/src/main/java/org/openhab/persistence/jdbc/db/JdbcPostgresqlDAO.java b/bundles/org.openhab.persistence.jdbc/src/main/java/org/openhab/persistence/jdbc/db/JdbcPostgresqlDAO.java index 45e50b84ace8d..883c85d44f791 100644 --- a/bundles/org.openhab.persistence.jdbc/src/main/java/org/openhab/persistence/jdbc/db/JdbcPostgresqlDAO.java +++ b/bundles/org.openhab.persistence.jdbc/src/main/java/org/openhab/persistence/jdbc/db/JdbcPostgresqlDAO.java @@ -52,7 +52,8 @@ private void initSqlQueries() { 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))"; sqlCreateNewEntryInItemsTable = "INSERT INTO items (itemname) SELECT itemname FROM #itemsManageTable# UNION VALUES ('#itemname#') EXCEPT SELECT itemname FROM items"; - sqlGetItemTables = "SELECT table_name FROM information_schema.tables WHERE table_type='BASE TABLE' AND table_schema='public' AND NOT table_name='#itemsManageTable#'"; + sqlGetItemTables = "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#'"; // http://stackoverflow.com/questions/17267417/how-do-i-do-an-upsert-merge-insert-on-duplicate-update-in-postgresql // for later use, PostgreSql > 9.5 to prevent PRIMARY key violation use: // SQL_INSERT_ITEM_VALUE = "INSERT INTO #tableName# (TIME, VALUE) VALUES( NOW(), CAST( ? as #dbType#) ) ON @@ -121,9 +122,10 @@ public Long doCreateNewEntryInItemsTable(ItemsVO vo) { @Override public List doGetItemTables(ItemsVO vo) { - String sql = StringUtilsExt.replaceArrayMerge(sqlGetItemTables, new String[] { "#itemsManageTable#" }, - new String[] { vo.getItemsManageTable() }); - logger.debug("JDBC::doGetItemTables sql={}", sql); + String sql = StringUtilsExt.replaceArrayMerge(this.sqlGetItemTables, + new String[] { "#itemsManageTable#", "#itemsManageTable#" }, + new String[] { vo.getItemsManageTable(), vo.getItemsManageTable() }); + this.logger.debug("JDBC::doGetItemTables sql={}", sql); return Yank.queryBeanList(sql, ItemsVO.class, null); }