@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 List<String> getIdentityColumns(WbConnection dbConnection, TableIdentifier table) { List<String> result = new ArrayList<>(1); try { List<ColumnIdentifier> columns = dbConnection.getMetadata().getTableColumns(table, false); for (ColumnIdentifier col : columns) { if (col.isAutoincrement()) { result.add(col.getColumnName()); } } } catch (SQLException ex) { LogMgr.logError( "FirebirdSequenceAdjuster.getIdentityColumns()", "Could not read sequence columns", ex); } return result; }
private void updateComputedColumns(TableDefinition table, WbConnection conn) { PreparedStatement stmt = null; ResultSet rs = null; String tablename = table.getTable().getRawTableName(); String schema = table.getTable().getRawSchema(); String sql = "select column_name, \n" + " generation_type \n" + "from sys.table_columns \n" + "where table_name = ? \n" + "and schema_name = ? \n" + "and generation_type is not null"; Map<String, String> expressions = new HashMap<>(); 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); String generated = rs.getString(2); if (StringUtil.isNonEmpty(generated)) { expressions.put(colname, "GENERATED " + generated); } } } catch (Exception e) { LogMgr.logError("HanaColumnEnhancer.updateComputedColumns()", "Error retrieving remarks", e); } finally { SqlUtil.closeAll(rs, stmt); } for (ColumnIdentifier col : table.getColumns()) { String expr = expressions.get(col.getColumnName()); if (StringUtil.isNonBlank(expr)) { col.setDefaultValue(null); col.setComputedColumnExpression(expr); col.setIsAutoincrement(true); } } }
private void resetInfo(ResultInfo info) { for (ColumnIdentifier col : info.getColumns()) { col.setIsPkColumn(false); col.setIsNullable(true); } }
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); } }