/** * Returns the Maximum sequence number assigned to a column of the table with name 'tableName' in * the MetaData DB. Works off the cache only. * * <p>Returns -ve value if table is not found in the MetaData. */ public static int getMaxColSeqNum(SysDatabase database, String tableName) { int maxSeq = -1000; SysTable sysTab = database.getSysTable(tableName); if (sysTab != null) { for (Iterator it = sysTab.getColumns().iterator(); it.hasNext(); ) { SysColumn aSysColumn = (SysColumn) it.next(); if (aSysColumn.getColSeq() > maxSeq) { maxSeq = aSysColumn.getColSeq(); } } } return maxSeq; // will be -ve if tableName not found }
/** * Utility function to modify column definitions to the MetatData DB tables * * <p>This is used from the ALTER TABLE MODIFY */ public static void modifyTableColumn(SqlCreateTableColumn columnDefinition, SysTable targetTable) throws Exception { StringBuffer sbStatement = new StringBuffer("UPDATE xsyscolumns SET "); SysColumn aSysCol = targetTable.getSysColumn(columnDefinition.columnName); sbStatement.append("coltype = ").append(columnDefinition.getColumnType()).append(", "); sbStatement.append("collength = "); int colLength = columnDefinition.getColumnLength(); if (colLength > -1) { sbStatement.append(colLength).append(", "); } else { sbStatement.append("null, "); } sbStatement.append("colscale = "); int colScale = columnDefinition.getColumnScale(); if (colScale > -1) { sbStatement.append(colScale).append(", "); } else { sbStatement.append("null, "); } sbStatement.append("colprecision = "); int colPrecision = columnDefinition.getColumnPrecision(); if (colPrecision > -1) { sbStatement.append(colPrecision).append(", "); } else { sbStatement.append("null, "); } sbStatement.append("isnullable = ").append(columnDefinition.isnullable).append(", "); sbStatement.append("isserial = ").append(columnDefinition.isSerial() ? "1" : "0").append(", "); sbStatement.append("defaultexpr = "); String defaultExpr = columnDefinition.getDefaultValue(); if (defaultExpr == null) { sbStatement.append("null, "); } else { sbStatement.append("'").append(defaultExpr.replaceAll("'", "''")).append("', "); } sbStatement .append("nativecoldef = '") .append(columnDefinition.rebuildString().replaceAll("'", "''")) .append("' "); sbStatement.append("WHERE colid = ").append(aSysCol.getColID()); MetaData.getMetaData().executeUpdate(sbStatement.toString()); }