]> git.basschouten.com Git - openhab-addons.git/commitdiff
Make itemsManageTable configurable (#13737)
authorJacob Laursen <jacob-github@vindvejr.dk>
Fri, 18 Nov 2022 16:34:10 +0000 (17:34 +0100)
committerGitHub <noreply@github.com>
Fri, 18 Nov 2022 16:34:10 +0000 (17:34 +0100)
Fixes #9637

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
bundles/org.openhab.persistence.jdbc/README.md
bundles/org.openhab.persistence.jdbc/src/main/java/org/openhab/persistence/jdbc/internal/JdbcConfiguration.java
bundles/org.openhab.persistence.jdbc/src/main/java/org/openhab/persistence/jdbc/internal/JdbcMapper.java
bundles/org.openhab.persistence.jdbc/src/main/java/org/openhab/persistence/jdbc/internal/dto/ItemsVO.java
bundles/org.openhab.persistence.jdbc/src/main/resources/OH-INF/config/config.xml

index ec645362e83905f8b620d66ab102ba46952a65da..0623328a38545aca9da63f01e29b909541189bf0 100644 (file)
@@ -60,6 +60,7 @@ This service can be configured in the file `services/jdbc.cfg`.
 | sqltype.tablePrimaryKey     | `TIMESTAMP`                                                  |    No     | type of `time` column for newly created item tables          |
 | sqltype.tablePrimaryValue   | `NOW()`                                                      |    No     | value of `time` column for newly inserted rows               |
 | numberDecimalcount          | 3                                                            |    No     | for Itemtype "Number" default decimal digit count            |
+| itemsManageTable            | `items`                                                      |    No     | items manage table. For Migration from MySQL Persistence, set to `Items`. |
 | tableNamePrefix             | `item`                                                       |    No     | table name prefix. For Migration from MySQL Persistence, set to `Item`. |
 | tableUseRealItemNames       | `false`                                                      |    No     | table name prefix generation.  When set to `true`, real item names are used for table names and `tableNamePrefix` is ignored.  When set to `false`, the `tableNamePrefix` is used to generate table names with sequential numbers. |
 | tableCaseSensitiveItemNames | `false`                                                      |    No     | table name case. This setting is only applicable when `tableUseRealItemNames` is `true`. When set to `true`, item name case is preserved in table names and no prefix or suffix is added. When set to `false`, table names are lower cased and a numeric suffix is added. Please read [this](#case-sensitive-item-names) before enabling. |
@@ -107,6 +108,7 @@ services/jdbc.cfg
 url=jdbc:mysql://192.168.0.1:3306/testMysql
 user=test
 password=test
+itemsManageTable=Items
 tableNamePrefix=Item
 tableUseRealItemNames=false
 tableIdDigitCount=0
@@ -125,7 +127,10 @@ The item data tables include time and data values.
 The SQL data type used depends on the openHAB item type, and allows the item state to be recovered back into openHAB in the same way it was stored.
 
 With this *per-item* layout, the scalability and easy maintenance of the database is ensured, even if large amounts of data must be managed.
-To rename existing tables, use the parameters `tableUseRealItemNames` and `tableIdDigitCount` in the configuration.
+To rename existing tables, use the parameters `tableNamePrefix`, `tableUseRealItemNames`, `tableIdDigitCount` and `tableCaseSensitiveItemNames` in the configuration.
+
+Please be aware that changing the name of `itemsManageTable` is not supported by the migration.
+If this is changed, the table must be renamed manually according to new configured name.
 
 ### Number Precision
 
index 5ce07056f3a76230ef4376c1e9f2a0980d6b43cf..b066e740ecd33f3adbe03b33a7a0e41ba09f509b 100644 (file)
@@ -58,6 +58,7 @@ public class JdbcConfiguration {
     private int numberDecimalcount = 3;
     private boolean tableUseRealItemNames = false;
     private boolean tableCaseSensitiveItemNames = false;
+    private String itemsManageTable = "items";
     private String tableNamePrefix = "item";
     private int tableIdDigitCount = 4;
     private boolean rebuildTableNames = false;
@@ -146,6 +147,12 @@ public class JdbcConfiguration {
             logger.debug("JDBC::updateConfig: errReconnectThreshold={}", errReconnectThreshold);
         }
 
+        String mt = (String) configuration.get("itemsManageTable");
+        if (mt != null && !mt.isBlank()) {
+            itemsManageTable = mt;
+            logger.debug("JDBC::updateConfig: itemsManageTable={}", itemsManageTable);
+        }
+
         String np = (String) configuration.get("tableNamePrefix");
         if (np != null && !np.isBlank()) {
             tableNamePrefix = np;
@@ -350,6 +357,10 @@ public class JdbcConfiguration {
         return serviceName;
     }
 
+    public String getItemsManageTable() {
+        return itemsManageTable;
+    }
+
     public String getTableNamePrefix() {
         return tableNamePrefix;
     }
index e22e69d647ee61eef81ddd728d09bf0e34767922..f68220d5e8c27aca49a4a016002d9c60550d22b2 100644 (file)
@@ -95,7 +95,9 @@ public class JdbcMapper {
     private boolean ifItemsTableExists() throws JdbcSQLException {
         logger.debug("JDBC::ifItemsTableExists");
         long timerStart = System.currentTimeMillis();
-        boolean res = conf.getDBDAO().doIfTableExists(new ItemsVO());
+        ItemsVO vo = new ItemsVO();
+        vo.setItemsManageTable(conf.getItemsManageTable());
+        boolean res = conf.getDBDAO().doIfTableExists(vo);
         logTime("doIfTableExists", timerStart, System.currentTimeMillis());
         return res;
     }
@@ -151,7 +153,9 @@ public class JdbcMapper {
     private List<ItemsVO> getItemIDTableNames() throws JdbcSQLException {
         logger.debug("JDBC::getItemIDTableNames");
         long timerStart = System.currentTimeMillis();
-        List<ItemsVO> vo = conf.getDBDAO().doGetItemIDTableNames(new ItemsVO());
+        ItemsVO isvo = new ItemsVO();
+        isvo.setItemsManageTable(conf.getItemsManageTable());
+        List<ItemsVO> vo = conf.getDBDAO().doGetItemIDTableNames(isvo);
         logTime("getItemIDTableNames", timerStart, System.currentTimeMillis());
         return vo;
     }
@@ -159,9 +163,10 @@ public class JdbcMapper {
     protected List<ItemsVO> getItemTables() throws JdbcSQLException {
         logger.debug("JDBC::getItemTables");
         long timerStart = System.currentTimeMillis();
-        ItemsVO vo = new ItemsVO();
-        vo.setJdbcUriDatabaseName(conf.getDbName());
-        List<ItemsVO> vol = conf.getDBDAO().doGetItemTables(vo);
+        ItemsVO isvo = new ItemsVO();
+        isvo.setJdbcUriDatabaseName(conf.getDbName());
+        isvo.setItemsManageTable(conf.getItemsManageTable());
+        List<ItemsVO> vol = conf.getDBDAO().doGetItemTables(isvo);
         logTime("getItemTables", timerStart, System.currentTimeMillis());
         return vol;
     }
@@ -286,14 +291,17 @@ public class JdbcMapper {
      * DATABASE TABLEHANDLING *
      **************************/
     protected void checkDBSchema() throws JdbcSQLException {
+        ItemsVO vo = new ItemsVO();
+        vo.setItemsManageTable(conf.getItemsManageTable());
+
         if (!conf.getTableUseRealCaseSensitiveItemNames()) {
-            createItemsTableIfNot(new ItemsVO());
+            createItemsTableIfNot(vo);
         }
         if (conf.getRebuildTableNames()) {
             formatTableNames();
 
             if (conf.getTableUseRealCaseSensitiveItemNames()) {
-                dropItemsTableIfExists(new ItemsVO());
+                dropItemsTableIfExists(vo);
             }
             logger.info(
                     "JDBC::checkDBSchema: Rebuild complete, configure the 'rebuildTableNames' setting to 'false' to stop rebuilds on startup");
@@ -332,13 +340,12 @@ public class JdbcMapper {
         logger.debug("JDBC::getTable: no table found for item '{}' in itemNameToTableNameMap", itemName);
 
         int itemId = 0;
-        ItemsVO isvo;
-        ItemVO ivo;
 
         if (!conf.getTableUseRealCaseSensitiveItemNames()) {
             // Create a new entry in items table
-            isvo = new ItemsVO();
+            ItemsVO isvo = new ItemsVO();
             isvo.setItemName(itemName);
+            isvo.setItemsManageTable(conf.getItemsManageTable());
             isvo = createNewEntryInItemsTable(isvo);
             itemId = isvo.getItemId();
             if (itemId == 0) {
@@ -352,7 +359,7 @@ public class JdbcMapper {
 
         // Create table for item
         String dataType = conf.getDBDAO().getDataType(item);
-        ivo = new ItemVO(tableName, itemName);
+        ItemVO ivo = new ItemVO(tableName, itemName);
         ivo.setDbType(dataType);
         ivo = createItemTable(ivo);
         logger.debug("JDBC::getTable: Table created for item '{}' with dataType {} in SQL database.", itemName,
@@ -384,6 +391,7 @@ public class JdbcMapper {
             for (String itemName : itemTables) {
                 ItemsVO isvo = new ItemsVO();
                 isvo.setItemName(itemName);
+                isvo.setItemsManageTable(conf.getItemsManageTable());
                 isvo = createNewEntryInItemsTable(isvo);
                 int itemId = isvo.getItemId();
                 if (itemId == 0) {
@@ -395,7 +403,7 @@ public class JdbcMapper {
                 }
             }
         } else {
-            String itemsManageTable = new ItemsVO().getItemsManageTable();
+            String itemsManageTable = conf.getItemsManageTable();
             Map<Integer, String> itemIdToItemNameMap = new HashMap<>();
 
             for (ItemsVO vo : itemIdTableNames) {
index 8bc23e5243c6495ce13d1cde9aba28a44694102c..ed0eee7b175f346e96d62a961dd3c56334f6a5a6 100644 (file)
@@ -27,8 +27,8 @@ public class ItemsVO implements Serializable {
     private static final String STR_FILTER = "[^a-zA-Z0-9]";
 
     private String coltype = "VARCHAR(500)";
-    private String colname = "itemname";
-    private String itemsManageTable = "items";
+    private String colname = "ItemName";
+    private String itemsManageTable;
     private int itemId;
     private String itemName;
     private String tableName;
index cc8411376b4f3f522f54322b5ee573cc7ccb50d6..a91244bdd98778fb2cadcfc3a6416f42c4b42c50 100644 (file)
 
                <!--
                        # T A B L E O P E R A T I O N S
+                       # Items Manage Table (optional, default: "items")
+                       # for Migration from MYSQL-Bundle set to 'Items'.
+                       #itemsManageTable=Items
+
                        # Tablename Prefix String (optional, default: "item")
                        # for Migration from MYSQL-Bundle set to 'Item'.
                        #tableNamePrefix=Item
                        # USE WITH CARE! Deactivate after Renaming is done!
                        #rebuildTableNames=true
                -->
+               <parameter name="itemsManageTable" type="text">
+                       <label>Items Manage Table</label>
+                       <description><![CDATA[Items Manage Table <br>(optional, default: "items"). <br>
+                       For migration from MYSQL-Bundle set to 'Items'.]]></description>
+               </parameter>
                <parameter name="tableNamePrefix" type="text">
                        <label>Tablename Prefix String</label>
                        <description><![CDATA[Tablename prefix string <br>(optional, default: "item"). <br>