Skip to content

Commit

Permalink
Fix case-sensitive table names for PostgreSQL
Browse files Browse the repository at this point in the history
Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
  • Loading branch information
jlaur committed Oct 20, 2024
1 parent 96fb5d8 commit fd2abfc
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ public boolean doIfTableExists(String tableName) throws JdbcSQLException {
public Long doCreateNewEntryInItemsTable(ItemsVO vo) throws JdbcSQLException {
String sql = StringUtilsExt.replaceArrayMerge(sqlCreateNewEntryInItemsTable,
new String[] { "#itemsManageTable#", "#itemname#" },
new String[] { vo.getItemsManageTable(), vo.getItemName() });
new String[] { formattedIdentifier(vo.getItemsManageTable()), vo.getItemName() });
logger.debug("JDBC::doCreateNewEntryInItemsTable sql={}", sql);
try {
return Yank.insert(sql, null);
Expand All @@ -308,7 +308,7 @@ public Long doCreateNewEntryInItemsTable(ItemsVO vo) throws JdbcSQLException {
public ItemsVO doCreateItemsTableIfNot(ItemsVO vo) throws JdbcSQLException {
String sql = StringUtilsExt.replaceArrayMerge(sqlCreateItemsTableIfNot,
new String[] { "#itemsManageTable#", "#colname#", "#coltype#" },
new String[] { vo.getItemsManageTable(), vo.getColname(), vo.getColtype() });
new String[] { formattedIdentifier(vo.getItemsManageTable()), vo.getColname(), vo.getColtype() });
logger.debug("JDBC::doCreateItemsTableIfNot sql={}", sql);
try {
Yank.execute(sql, null);
Expand All @@ -320,7 +320,7 @@ public ItemsVO doCreateItemsTableIfNot(ItemsVO vo) throws JdbcSQLException {

public ItemsVO doDropItemsTableIfExists(ItemsVO vo) throws JdbcSQLException {
String sql = StringUtilsExt.replaceArrayMerge(sqlDropItemsTableIfExists, new String[] { "#itemsManageTable#" },
new String[] { vo.getItemsManageTable() });
new String[] { formattedIdentifier(vo.getItemsManageTable()) });
logger.debug("JDBC::doDropItemsTableIfExists sql={}", sql);
try {
Yank.execute(sql, null);
Expand All @@ -332,7 +332,7 @@ public ItemsVO doDropItemsTableIfExists(ItemsVO vo) throws JdbcSQLException {

public void doDropTable(String tableName) throws JdbcSQLException {
String sql = StringUtilsExt.replaceArrayMerge(sqlDropTable, new String[] { "#tableName#" },
new String[] { tableName });
new String[] { formattedIdentifier(tableName) });
logger.debug("JDBC::doDropTable sql={}", sql);
try {
Yank.execute(sql, null);
Expand All @@ -344,7 +344,7 @@ public void doDropTable(String tableName) throws JdbcSQLException {
public void doDeleteItemsEntry(ItemsVO vo) throws JdbcSQLException {
String sql = StringUtilsExt.replaceArrayMerge(sqlDeleteItemsEntry,
new String[] { "#itemsManageTable#", "#itemname#" },
new String[] { vo.getItemsManageTable(), vo.getItemName() });
new String[] { formattedIdentifier(vo.getItemsManageTable()), vo.getItemName() });
logger.debug("JDBC::doDeleteItemsEntry sql={}", sql);
try {
Yank.execute(sql, null);
Expand All @@ -355,7 +355,7 @@ public void doDeleteItemsEntry(ItemsVO vo) throws JdbcSQLException {

public List<ItemsVO> doGetItemIDTableNames(ItemsVO vo) throws JdbcSQLException {
String sql = StringUtilsExt.replaceArrayMerge(sqlGetItemIDTableNames, new String[] { "#itemsManageTable#" },
new String[] { vo.getItemsManageTable() });
new String[] { formattedIdentifier(vo.getItemsManageTable()) });
logger.debug("JDBC::doGetItemIDTableNames sql={}", sql);
try {
return Yank.queryBeanList(sql, ItemsVO.class, null);
Expand Down Expand Up @@ -405,8 +405,8 @@ public void doUpdateItemTableNames(List<ItemVO> vol) throws JdbcSQLException {

public void doCreateItemTable(ItemVO vo) throws JdbcSQLException {
String sql = StringUtilsExt.replaceArrayMerge(sqlCreateItemTable,
new String[] { "#tableName#", "#dbType#", "#tablePrimaryKey#" },
new String[] { vo.getTableName(), vo.getDbType(), sqlTypes.get("tablePrimaryKey") });
new String[] { "#tableName#", "#dbType#", "#tablePrimaryKey#" }, new String[] {
formattedIdentifier(vo.getTableName()), vo.getDbType(), sqlTypes.get("tablePrimaryKey") });
logger.debug("JDBC::doCreateItemTable sql={}", sql);
try {
Yank.execute(sql, null);
Expand All @@ -418,8 +418,8 @@ public void doCreateItemTable(ItemVO vo) throws JdbcSQLException {
public void doAlterTableColumn(String tableName, String columnName, String columnType, boolean nullable)
throws JdbcSQLException {
String sql = StringUtilsExt.replaceArrayMerge(sqlAlterTableColumn,
new String[] { "#tableName#", "#columnName#", "#columnType#" },
new String[] { tableName, columnName, nullable ? columnType : columnType + " NOT NULL" });
new String[] { "#tableName#", "#columnName#", "#columnType#" }, new String[] {
formattedIdentifier(tableName), columnName, nullable ? columnType : columnType + " NOT NULL" });
logger.debug("JDBC::doAlterTableColumn sql={}", sql);
try {
Yank.execute(sql, null);
Expand All @@ -432,7 +432,7 @@ public void doStoreItemValue(Item item, State itemState, ItemVO vo) throws JdbcS
ItemVO storedVO = storeItemValueProvider(item, itemState, vo);
String sql = StringUtilsExt.replaceArrayMerge(sqlInsertItemValue,
new String[] { "#tableName#", "#tablePrimaryValue#" },
new String[] { storedVO.getTableName(), sqlTypes.get("tablePrimaryValue") });
new String[] { formattedIdentifier(storedVO.getTableName()), sqlTypes.get("tablePrimaryValue") });
Object[] params = { storedVO.getValue(), storedVO.getValue() };
logger.debug("JDBC::doStoreItemValue sql={} value='{}'", sql, storedVO.getValue());
try {
Expand All @@ -445,7 +445,8 @@ public void doStoreItemValue(Item item, State itemState, ItemVO vo) throws JdbcS
public void doStoreItemValue(Item item, State itemState, ItemVO vo, ZonedDateTime date) throws JdbcSQLException {
ItemVO storedVO = storeItemValueProvider(item, itemState, vo);
String sql = StringUtilsExt.replaceArrayMerge(sqlInsertItemValue,
new String[] { "#tableName#", "#tablePrimaryValue#" }, new String[] { storedVO.getTableName(), "?" });
new String[] { "#tableName#", "#tablePrimaryValue#" },
new String[] { formattedIdentifier(storedVO.getTableName()), "?" });
java.sql.Timestamp timestamp = new java.sql.Timestamp(date.toInstant().toEpochMilli());
Object[] params = { timestamp, storedVO.getValue(), storedVO.getValue() };
logger.debug("JDBC::doStoreItemValue sql={} timestamp={} value='{}'", sql, timestamp, storedVO.getValue());
Expand Down Expand Up @@ -490,7 +491,7 @@ public void doDeleteItemValues(FilterCriteria filter, String table, ZoneId timeZ

public long doGetRowCount(String tableName) throws JdbcSQLException {
final String sql = StringUtilsExt.replaceArrayMerge(sqlGetRowCount, new String[] { "#tableName#" },
new String[] { tableName });
new String[] { formattedIdentifier(tableName) });
logger.debug("JDBC::doGetRowCount sql={}", sql);
try {
final @Nullable Long result = Yank.queryScalar(sql, Long.class, null);
Expand Down Expand Up @@ -519,8 +520,8 @@ protected String histItemFilterQueryProvider(FilterCriteria filter, int numberDe
// SELECT time, ROUND(value,3) FROM number_item_0114 ORDER BY time DESC LIMIT 0,1
// rounding HALF UP
String queryString = "NUMBERITEM".equalsIgnoreCase(simpleName) && numberDecimalcount > -1
? "SELECT time, ROUND(value," + numberDecimalcount + ") FROM " + table
: "SELECT time, value FROM " + table;
? "SELECT time, ROUND(value," + numberDecimalcount + ") FROM " + formattedIdentifier(table)
: "SELECT time, value FROM " + formattedIdentifier(table);
if (!filterString.isEmpty()) {
queryString += filterString;
}
Expand All @@ -532,8 +533,8 @@ protected String histItemFilterDeleteProvider(FilterCriteria filter, String tabl
logger.debug("JDBC::histItemFilterDeleteProvider filter = {}, table = {}", filter, table);

String filterString = resolveTimeFilter(filter, timeZone);
String deleteString = filterString.isEmpty() ? "TRUNCATE TABLE " + table
: "DELETE FROM " + table + filterString;
String deleteString = filterString.isEmpty() ? "TRUNCATE TABLE " + formattedIdentifier(table)
: "DELETE FROM " + formattedIdentifier(table) + filterString;
logger.debug("JDBC::delete deleteString = {}", deleteString);
return deleteString;
}
Expand All @@ -554,7 +555,12 @@ protected String resolveTimeFilter(FilterCriteria filter, ZoneId timeZone) {
}

private String updateItemTableNamesProvider(ItemVO itemTable) {
String queryString = "ALTER TABLE " + itemTable.getTableName() + " RENAME TO " + itemTable.getNewTableName();
String newTableName = itemTable.getNewTableName();
if (newTableName == null) {
throw new IllegalArgumentException("New table name is not provided");
}
String queryString = "ALTER TABLE " + formattedIdentifier(itemTable.getTableName()) + " RENAME TO "
+ formattedIdentifier(newTableName);
logger.debug("JDBC::query queryString = {}", queryString);
return queryString;
}
Expand Down Expand Up @@ -757,7 +763,11 @@ protected String objectAsString(Object v) {
throw new UnsupportedOperationException("String of type '" + v.getClass().getName() + "' is not supported");
}

public String getItemType(Item i) {
protected String formattedIdentifier(String identifier) {
return identifier;
}

private String getItemType(Item i) {
Item item = i;
String def = "STRINGITEM";
if (i instanceof GroupItem groupItem) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,18 +166,6 @@ public ItemsVO doCreateItemsTableIfNot(ItemsVO vo) throws JdbcSQLException {
/*************
* ITEM DAOs *
*************/
@Override
public void doCreateItemTable(ItemVO vo) throws JdbcSQLException {
String sql = StringUtilsExt.replaceArrayMerge(sqlCreateItemTable,
new String[] { "#tableName#", "#dbType#", "#tablePrimaryKey#" },
new String[] { vo.getTableName(), vo.getDbType(), sqlTypes.get("tablePrimaryKey") });
try {
Yank.execute(sql, null);
} catch (YankSQLException e) {
throw new JdbcSQLException(e);
}
}

@Override
public void doStoreItemValue(Item item, State itemState, ItemVO vo) throws JdbcSQLException {
ItemVO storedVO = storeItemValueProvider(item, itemState, vo);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ public void doStoreItemValue(Item item, State itemState, ItemVO vo) throws JdbcS
ItemVO storedVO = storeItemValueProvider(item, itemState, vo);
String sql = StringUtilsExt.replaceArrayMerge(sqlInsertItemValue,
new String[] { "#tableName#", "#dbType#", "#tablePrimaryValue#" },
new String[] { storedVO.getTableName(), storedVO.getDbType(), sqlTypes.get("tablePrimaryValue") });
new String[] { formattedIdentifier(storedVO.getTableName()), storedVO.getDbType(),
sqlTypes.get("tablePrimaryValue") });
Object[] params = { storedVO.getValue() };
logger.debug("JDBC::doStoreItemValue sql={} value='{}'", sql, storedVO.getValue());
try {
Expand All @@ -100,7 +101,7 @@ public void doStoreItemValue(Item item, State itemState, ItemVO vo, ZonedDateTim
ItemVO storedVO = storeItemValueProvider(item, itemState, vo);
String sql = StringUtilsExt.replaceArrayMerge(sqlInsertItemValue,
new String[] { "#tableName#", "#dbType#", "#tablePrimaryValue#" },
new String[] { storedVO.getTableName(), storedVO.getDbType(), "?" });
new String[] { formattedIdentifier(storedVO.getTableName()), storedVO.getDbType(), "?" });
java.sql.Timestamp timestamp = new java.sql.Timestamp(date.toInstant().toEpochMilli());
Object[] params = { timestamp, storedVO.getValue() };
logger.debug("JDBC::doStoreItemValue sql={} timestamp={} value='{}'", sql, timestamp, storedVO.getValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ public void doStoreItemValue(Item item, State itemState, ItemVO vo) throws JdbcS
ItemVO storedVO = storeItemValueProvider(item, itemState, vo);
String sql = StringUtilsExt.replaceArrayMerge(sqlInsertItemValue,
new String[] { "#tableName#", "#dbType#", "#tableName#", "#tablePrimaryValue#" },
new String[] { storedVO.getTableName(), storedVO.getDbType(), storedVO.getTableName(),
sqlTypes.get("tablePrimaryValue") });
new String[] { formattedIdentifier(storedVO.getTableName()), storedVO.getDbType(),
storedVO.getTableName(), sqlTypes.get("tablePrimaryValue") });
Object[] params = { storedVO.getValue() };
logger.debug("JDBC::doStoreItemValue sql={} value='{}'", sql, storedVO.getValue());
try {
Expand All @@ -148,7 +148,8 @@ public void doStoreItemValue(Item item, State itemState, ItemVO vo, ZonedDateTim
ItemVO storedVO = storeItemValueProvider(item, itemState, vo);
String sql = StringUtilsExt.replaceArrayMerge(sqlInsertItemValue,
new String[] { "#tableName#", "#dbType#", "#tableName#", "#tablePrimaryValue#" },
new String[] { storedVO.getTableName(), storedVO.getDbType(), storedVO.getTableName(), "?" });
new String[] { formattedIdentifier(storedVO.getTableName()), storedVO.getDbType(),
storedVO.getTableName(), "?" });
java.sql.Timestamp timestamp = new java.sql.Timestamp(date.toInstant().toEpochMilli());
Object[] params = { timestamp, storedVO.getValue() };
logger.debug("JDBC::doStoreItemValue sql={} timestamp={} value='{}'", sql, timestamp, storedVO.getValue());
Expand Down
Loading

0 comments on commit fd2abfc

Please sign in to comment.