2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.persistence.jdbc.internal;
15 import java.util.ArrayList;
16 import java.util.HashMap;
17 import java.util.List;
19 import java.util.Map.Entry;
20 import java.util.Objects;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.openhab.core.items.ItemUtil;
24 import org.openhab.persistence.jdbc.internal.dto.ItemVO;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
29 * This class manages strategy for table names.
31 * @author Jacob Laursen - Initial contribution
34 public class NamingStrategy {
36 private final Logger logger = LoggerFactory.getLogger(NamingStrategy.class);
38 private JdbcConfiguration configuration;
40 public NamingStrategy(JdbcConfiguration configuration) {
41 this.configuration = configuration;
44 public String getTableName(int itemId, String itemName) {
45 if (!ItemUtil.isValidItemName(itemName)) {
46 throw new IllegalArgumentException(itemName + " is not a valid item name");
48 if (configuration.getTableUseRealItemNames()) {
49 return formatTableName(itemName, itemId);
51 return configuration.getTableNamePrefix() + getSuffix(itemId);
55 private String formatTableName(String itemName, int itemId) {
56 if (configuration.getTableCaseSensitiveItemNames()) {
59 return itemName.toLowerCase() + "_" + getSuffix(itemId);
63 private String getSuffix(int itemId) {
64 int digits = configuration.getTableIdDigitCount();
66 return String.format("%0" + configuration.getTableIdDigitCount() + "d", itemId);
68 return String.valueOf(itemId);
72 public List<ItemVO> prepareMigration(List<String> itemTables, Map<Integer, String> itemIdToItemNameMap,
73 String itemsManageTable) {
74 List<ItemVO> oldNewTableNames = new ArrayList<>();
75 Map<String, Integer> tableNameToItemIdMap = new HashMap<>();
77 for (Entry<Integer, String> entry : itemIdToItemNameMap.entrySet()) {
78 String itemName = entry.getValue();
79 tableNameToItemIdMap.put(itemName, entry.getKey());
82 for (String oldName : itemTables) {
83 Integer itemIdBoxed = tableNameToItemIdMap.get(oldName);
86 if (Objects.nonNull(itemIdBoxed)) {
88 logger.info("JDBC::formatTableNames: found by name; table name= {} id= {}", oldName, itemId);
91 itemId = Integer.parseInt(oldName.replaceFirst("^.*\\D", ""));
92 logger.info("JDBC::formatTableNames: found by id; table name= {} id= {}", oldName, itemId);
93 } catch (NumberFormatException e) {
98 String itemName = itemIdToItemNameMap.get(itemId);
100 if (!Objects.isNull(itemName)) {
101 String newName = getTableName(itemId, itemName);
102 if (newName.equalsIgnoreCase(itemsManageTable)) {
104 "JDBC::formatTableNames: Table '{}' could NOT be renamed to '{}' since it conflicts with manage table",
106 } else if (!oldName.equals(newName)) {
107 oldNewTableNames.add(new ItemVO(oldName, newName));
108 logger.info("JDBC::formatTableNames: Table '{}' will be renamed to '{}'", oldName, newName);
110 logger.info("JDBC::formatTableNames: Table oldName='{}' newName='{}' nothing to rename", oldName,
114 logger.error("JDBC::formatTableNames: Table '{}' could NOT be renamed for id '{}'", oldName, itemId);
118 return oldNewTableNames;