@Override public void updateColumnDefinition(TableDefinition table, WbConnection conn) { String typeNames = conn.getDbSettings().getProperty("qualifier.typenames", "datetime,interval"); Set<String> types = CollectionUtil.caseInsensitiveSet(); types.addAll(StringUtil.stringToList(typeNames, ",", true, true, false, false)); boolean checkRequired = false; for (ColumnIdentifier col : table.getColumns()) { String plainType = SqlUtil.getPlainTypeName(col.getDbmsType()); if (types.contains(plainType)) { checkRequired = true; } int type = col.getDataType(); String val = col.getDefaultValue(); if (defaultNeedsQuotes(val, type, plainType)) { val = "'" + val + "'"; col.setDefaultValue(val); } } if (checkRequired) { updateDateColumns(table, conn); } }
private void updateDateColumns(TableDefinition table, WbConnection conn) { String catalog = table.getTable().getRawCatalog(); String systemSchema = conn.getDbSettings().getProperty("systemschema", "informix"); TableIdentifier sysTabs = new TableIdentifier(catalog, systemSchema, "systables"); TableIdentifier sysCols = new TableIdentifier(catalog, systemSchema, "syscolumns"); String systables = sysTabs.getFullyQualifiedName(conn); String syscolumns = sysCols.getFullyQualifiedName(conn); String typeValues = conn.getDbSettings().getProperty("qualifier.typevalues", "10,14,266,270"); String sql = "select c.colname, c.collength \n" + "from " + systables + " t \n" + " join " + syscolumns + " c on t.tabid = c.tabid \n" + "where t.tabname = ? \n" + " and t.owner = ? \n" + " and c.coltype in (" + typeValues + ")"; String tablename = table.getTable().getRawTableName(); String schema = table.getTable().getRawSchema(); LogMgr.logDebug( "InformixColumnEnhancer.updateDateColumns()", "Query to retrieve column details:\n" + SqlUtil.replaceParameters(sql, tablename, schema)); PreparedStatement stmt = null; ResultSet rs = null; Map<String, ColumnIdentifier> cols = new TreeMap<>(CaseInsensitiveComparator.INSTANCE); for (ColumnIdentifier col : table.getColumns()) { cols.put(col.getColumnName(), col); } try { stmt = conn.getSqlConnection().prepareStatement(sql); stmt.setString(1, tablename); stmt.setString(2, schema); rs = stmt.executeQuery(); while (rs.next()) { String colname = rs.getString(1); int colLength = rs.getInt(2); ColumnIdentifier col = cols.get(colname); if (col != null) { String typeDesc = getQualifier(colLength); String dbms = SqlUtil.getPlainTypeName(col.getDbmsType()); String newType = dbms + " " + typeDesc; LogMgr.logDebug( "InformixColumnEnhancer.updateDateColumns()", "Column " + tablename + "." + colname + " has collength of: " + colLength + ". Changing type '" + col.getDbmsType() + "' to '" + newType + "'"); col.setDbmsType(newType); } else { LogMgr.logError( "InformixColumnEnhancer.updateDateColumns()", "The query returned a column name (" + colname + ") that was not part of the passed table definition!", null); } } } catch (Exception e) { LogMgr.logError( "InformixColumnEnhancer.updateDateColumns()", "Error retrieving datetime qualifiers using:\n" + SqlUtil.replaceParameters(sql, tablename, schema), e); } finally { SqlUtil.closeAll(rs, stmt); } }