Make itemsManageTable configurable (#13737)

Fixes #9637

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
This commit is contained in:
Jacob Laursen 2022-11-18 17:34:10 +01:00 committed by GitHub
parent 63ecbc264a
commit d075f141d7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 48 additions and 15 deletions

View 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.tablePrimaryKey | `TIMESTAMP` | No | type of `time` column for newly created item tables |
| sqltype.tablePrimaryValue | `NOW()` | No | value of `time` column for newly inserted rows | | sqltype.tablePrimaryValue | `NOW()` | No | value of `time` column for newly inserted rows |
| numberDecimalcount | 3 | No | for Itemtype "Number" default decimal digit count | | 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`. | | 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. | | 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. | | 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 url=jdbc:mysql://192.168.0.1:3306/testMysql
user=test user=test
password=test password=test
itemsManageTable=Items
tableNamePrefix=Item tableNamePrefix=Item
tableUseRealItemNames=false tableUseRealItemNames=false
tableIdDigitCount=0 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. 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. 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 ### Number Precision

View File

@ -58,6 +58,7 @@ public class JdbcConfiguration {
private int numberDecimalcount = 3; private int numberDecimalcount = 3;
private boolean tableUseRealItemNames = false; private boolean tableUseRealItemNames = false;
private boolean tableCaseSensitiveItemNames = false; private boolean tableCaseSensitiveItemNames = false;
private String itemsManageTable = "items";
private String tableNamePrefix = "item"; private String tableNamePrefix = "item";
private int tableIdDigitCount = 4; private int tableIdDigitCount = 4;
private boolean rebuildTableNames = false; private boolean rebuildTableNames = false;
@ -146,6 +147,12 @@ public class JdbcConfiguration {
logger.debug("JDBC::updateConfig: errReconnectThreshold={}", errReconnectThreshold); 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"); String np = (String) configuration.get("tableNamePrefix");
if (np != null && !np.isBlank()) { if (np != null && !np.isBlank()) {
tableNamePrefix = np; tableNamePrefix = np;
@ -350,6 +357,10 @@ public class JdbcConfiguration {
return serviceName; return serviceName;
} }
public String getItemsManageTable() {
return itemsManageTable;
}
public String getTableNamePrefix() { public String getTableNamePrefix() {
return tableNamePrefix; return tableNamePrefix;
} }

View File

@ -95,7 +95,9 @@ public class JdbcMapper {
private boolean ifItemsTableExists() throws JdbcSQLException { private boolean ifItemsTableExists() throws JdbcSQLException {
logger.debug("JDBC::ifItemsTableExists"); logger.debug("JDBC::ifItemsTableExists");
long timerStart = System.currentTimeMillis(); 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()); logTime("doIfTableExists", timerStart, System.currentTimeMillis());
return res; return res;
} }
@ -151,7 +153,9 @@ public class JdbcMapper {
private List<ItemsVO> getItemIDTableNames() throws JdbcSQLException { private List<ItemsVO> getItemIDTableNames() throws JdbcSQLException {
logger.debug("JDBC::getItemIDTableNames"); logger.debug("JDBC::getItemIDTableNames");
long timerStart = System.currentTimeMillis(); 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()); logTime("getItemIDTableNames", timerStart, System.currentTimeMillis());
return vo; return vo;
} }
@ -159,9 +163,10 @@ public class JdbcMapper {
protected List<ItemsVO> getItemTables() throws JdbcSQLException { protected List<ItemsVO> getItemTables() throws JdbcSQLException {
logger.debug("JDBC::getItemTables"); logger.debug("JDBC::getItemTables");
long timerStart = System.currentTimeMillis(); long timerStart = System.currentTimeMillis();
ItemsVO vo = new ItemsVO(); ItemsVO isvo = new ItemsVO();
vo.setJdbcUriDatabaseName(conf.getDbName()); isvo.setJdbcUriDatabaseName(conf.getDbName());
List<ItemsVO> vol = conf.getDBDAO().doGetItemTables(vo); isvo.setItemsManageTable(conf.getItemsManageTable());
List<ItemsVO> vol = conf.getDBDAO().doGetItemTables(isvo);
logTime("getItemTables", timerStart, System.currentTimeMillis()); logTime("getItemTables", timerStart, System.currentTimeMillis());
return vol; return vol;
} }
@ -286,14 +291,17 @@ public class JdbcMapper {
* DATABASE TABLEHANDLING * * DATABASE TABLEHANDLING *
**************************/ **************************/
protected void checkDBSchema() throws JdbcSQLException { protected void checkDBSchema() throws JdbcSQLException {
ItemsVO vo = new ItemsVO();
vo.setItemsManageTable(conf.getItemsManageTable());
if (!conf.getTableUseRealCaseSensitiveItemNames()) { if (!conf.getTableUseRealCaseSensitiveItemNames()) {
createItemsTableIfNot(new ItemsVO()); createItemsTableIfNot(vo);
} }
if (conf.getRebuildTableNames()) { if (conf.getRebuildTableNames()) {
formatTableNames(); formatTableNames();
if (conf.getTableUseRealCaseSensitiveItemNames()) { if (conf.getTableUseRealCaseSensitiveItemNames()) {
dropItemsTableIfExists(new ItemsVO()); dropItemsTableIfExists(vo);
} }
logger.info( logger.info(
"JDBC::checkDBSchema: Rebuild complete, configure the 'rebuildTableNames' setting to 'false' to stop rebuilds on startup"); "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); logger.debug("JDBC::getTable: no table found for item '{}' in itemNameToTableNameMap", itemName);
int itemId = 0; int itemId = 0;
ItemsVO isvo;
ItemVO ivo;
if (!conf.getTableUseRealCaseSensitiveItemNames()) { if (!conf.getTableUseRealCaseSensitiveItemNames()) {
// Create a new entry in items table // Create a new entry in items table
isvo = new ItemsVO(); ItemsVO isvo = new ItemsVO();
isvo.setItemName(itemName); isvo.setItemName(itemName);
isvo.setItemsManageTable(conf.getItemsManageTable());
isvo = createNewEntryInItemsTable(isvo); isvo = createNewEntryInItemsTable(isvo);
itemId = isvo.getItemId(); itemId = isvo.getItemId();
if (itemId == 0) { if (itemId == 0) {
@ -352,7 +359,7 @@ public class JdbcMapper {
// Create table for item // Create table for item
String dataType = conf.getDBDAO().getDataType(item); String dataType = conf.getDBDAO().getDataType(item);
ivo = new ItemVO(tableName, itemName); ItemVO ivo = new ItemVO(tableName, itemName);
ivo.setDbType(dataType); ivo.setDbType(dataType);
ivo = createItemTable(ivo); ivo = createItemTable(ivo);
logger.debug("JDBC::getTable: Table created for item '{}' with dataType {} in SQL database.", itemName, 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) { for (String itemName : itemTables) {
ItemsVO isvo = new ItemsVO(); ItemsVO isvo = new ItemsVO();
isvo.setItemName(itemName); isvo.setItemName(itemName);
isvo.setItemsManageTable(conf.getItemsManageTable());
isvo = createNewEntryInItemsTable(isvo); isvo = createNewEntryInItemsTable(isvo);
int itemId = isvo.getItemId(); int itemId = isvo.getItemId();
if (itemId == 0) { if (itemId == 0) {
@ -395,7 +403,7 @@ public class JdbcMapper {
} }
} }
} else { } else {
String itemsManageTable = new ItemsVO().getItemsManageTable(); String itemsManageTable = conf.getItemsManageTable();
Map<Integer, String> itemIdToItemNameMap = new HashMap<>(); Map<Integer, String> itemIdToItemNameMap = new HashMap<>();
for (ItemsVO vo : itemIdTableNames) { for (ItemsVO vo : itemIdTableNames) {

View File

@ -27,8 +27,8 @@ public class ItemsVO implements Serializable {
private static final String STR_FILTER = "[^a-zA-Z0-9]"; private static final String STR_FILTER = "[^a-zA-Z0-9]";
private String coltype = "VARCHAR(500)"; private String coltype = "VARCHAR(500)";
private String colname = "itemname"; private String colname = "ItemName";
private String itemsManageTable = "items"; private String itemsManageTable;
private int itemId; private int itemId;
private String itemName; private String itemName;
private String tableName; private String tableName;

View File

@ -134,6 +134,10 @@
<!-- <!--
# T A B L E O P E R A T I O N S # 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") # Tablename Prefix String (optional, default: "item")
# for Migration from MYSQL-Bundle set to 'Item'. # for Migration from MYSQL-Bundle set to 'Item'.
#tableNamePrefix=Item #tableNamePrefix=Item
@ -156,6 +160,11 @@
# USE WITH CARE! Deactivate after Renaming is done! # USE WITH CARE! Deactivate after Renaming is done!
#rebuildTableNames=true #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"> <parameter name="tableNamePrefix" type="text">
<label>Tablename Prefix String</label> <label>Tablename Prefix String</label>
<description><![CDATA[Tablename prefix string <br>(optional, default: "item"). <br> <description><![CDATA[Tablename prefix string <br>(optional, default: "item"). <br>