/** * Processes the removal of a column from a table. * * @param currentModel The current database schema * @param params The parameters used in the creation of new tables. Note that for existing tables, * the parameters won't be applied * @param change The change object */ public void processChange( Database currentModel, CreationParameters params, RemoveColumnChange change) throws IOException { Table changedTable = findChangedTable(currentModel, change); Column droppedColumn = changedTable.findColumn(change.getChangedColumn(), isDelimitedIdentifierModeOn()); ((InterbaseBuilder) getSqlBuilder()).dropColumn(changedTable, droppedColumn); change.apply(currentModel, isDelimitedIdentifierModeOn()); }
private boolean checkNeedDbForRowMode(Pipeline pipeline, EventData eventData) { // 获取数据表信息 DataMedia dataMedia = ConfigHelper.findDataMedia(pipeline, eventData.getTableId()); DbDialect dbDialect = dbDialectFactory.getDbDialect(pipeline.getId(), (DbMediaSource) dataMedia.getSource()); Table table = dbDialect.findTable(eventData.getSchemaName(), eventData.getTableName()); if (table.getColumnCount() == eventData.getColumns().size() + eventData.getKeys().size()) { return false; } else { return true; } }
/** * Processes the addition of a column to a table. * * @param currentModel The current database schema * @param params The parameters used in the creation of new tables. Note that for existing tables, * the parameters won't be applied * @param change The change object */ public void processChange( Database currentModel, CreationParameters params, AddColumnChange change) throws IOException { Table changedTable = findChangedTable(currentModel, change); Column prevColumn = null; if (change.getPreviousColumn() != null) { prevColumn = changedTable.findColumn(change.getPreviousColumn(), isDelimitedIdentifierModeOn()); } ((InterbaseBuilder) getSqlBuilder()) .insertColumn(currentModel, changedTable, change.getNewColumn(), prevColumn); change.apply(currentModel, isDelimitedIdentifierModeOn()); }
/** 构建数据库主键字段的信息 */ private TableData buildTableData(Table table, List<EventColumn> keys) { Column[] tableColumns = table.getColumns(); TableData data = new TableData(); data.indexs = new int[keys.size()]; data.columnNames = new String[keys.size()]; data.columnTypes = new int[keys.size()]; data.columnValues = new Object[keys.size()]; int i = 0; int index = 0; for (EventColumn keyColumn : keys) { for (Column tableColumn : tableColumns) { if (StringUtils.equalsIgnoreCase(keyColumn.getColumnName(), tableColumn.getName())) { data.indexs[i] = index; data.columnNames[i] = tableColumn.getName(); data.columnTypes[i] = tableColumn.getTypeCode(); data.columnValues[i] = SqlUtils.stringToSqlValue( keyColumn.getColumnValue(), tableColumn.getTypeCode(), tableColumn.isRequired(), false); i++; break; } index++; } } if (i != keys.size()) { throw new ExtractException( "keys is not found in table " + table.toString() + " keys : " + dumpEventColumn(keys)); } return data; }
/** oracle的erosa获取的字段类型,没有转换成jdbc的类型,所以需要手工转一下. */ private int[] getOraclePkTypes(Table table, String[] pkNames) { Column[] columns = table.getColumns(); List<Integer> pkTypes = new ArrayList<Integer>(); for (String pkName : pkNames) { for (Column column : columns) { if (column.getName().equalsIgnoreCase(pkName)) { pkTypes.add(column.getTypeCode()); } } } int[] types = new int[pkTypes.size()]; for (int i = 0; i < types.length; i++) { types[i] = pkTypes.get(i); } return types; }
@SuppressWarnings("unused") private boolean checkNeedDbForRowMode( Table table, List<String> viewColumns, EventData eventData) { if (viewColumns.size() != 0) { // 说明有视图 if (viewColumns.size() != eventData.getColumns().size()) { return true; } // 检查一下当前是否所有字段都在view字段列表里 for (EventColumn column : eventData.getColumns()) { if (!viewColumns.contains(column.getColumnName())) { return true; } } return false; } else { if (table.getColumnCount() == eventData.getColumns().size() + eventData.getKeys().size()) { return false; } else { return true; } } }
private void doPreparedStatement( PreparedStatement ps, DbDialect dbDialect, LobCreator lobCreator, EventData data) throws SQLException { EventType type = data.getEventType(); // 注意insert/update语句对应的字段数序都是将主键排在后面 List<EventColumn> columns = new ArrayList<EventColumn>(); if (type.isInsert()) { columns.addAll(data.getColumns()); // insert为所有字段 columns.addAll(data.getKeys()); } else if (type.isDelete()) { columns.addAll(data.getKeys()); } else if (type.isUpdate()) { boolean existOldKeys = !CollectionUtils.isEmpty(data.getOldKeys()); columns.addAll(data.getUpdatedColumns()); // 只更新带有isUpdate=true的字段 columns.addAll(data.getKeys()); if (existOldKeys) { columns.addAll(data.getOldKeys()); } } for (int i = 0; i < columns.size(); i++) { int paramIndex = i + 1; EventColumn column = columns.get(i); int sqlType = column.getColumnType(); // 获取一下当前字段名的数据是否必填 Table table = dbDialect.findTable(data.getSchemaName(), data.getTableName()); Map<String, Boolean> isRequiredMap = new HashMap<String, Boolean>(); for (Column tableColumn : table.getColumns()) { isRequiredMap.put(StringUtils.lowerCase(tableColumn.getName()), tableColumn.isRequired()); } Boolean isRequired = isRequiredMap.get(StringUtils.lowerCase(column.getColumnName())); if (isRequired == null) { throw new ClaveException( String.format( "column name %s is not found in Table[%s]", column.getColumnName(), table.toString())); } Object param = SqlUtils.stringToSqlValue( column.getColumnValue(), sqlType, isRequired, dbDialect.isEmptyStringNulled()); try { switch (sqlType) { case Types.CLOB: lobCreator.setClobAsString(ps, paramIndex, (String) param); break; case Types.BLOB: lobCreator.setBlobAsBytes(ps, paramIndex, (byte[]) param); break; default: StatementCreatorUtils.setParameterValue(ps, paramIndex, sqlType, null, param); break; } } catch (SQLException ex) { logger.error( "## SetParam error , [table={}, sqltype={}, value={}]", new Object[] {data.getSchemaName() + "." + data.getTableName(), sqlType, param}); throw ex; } } }
/** 构建数据库非主键字段的信息 */ private TableData buildTableData( Table table, List<EventColumn> columns, boolean needAll, List<String> viewColumnNames) { Column[] tableColumns = table.getColumns(); List<Column> noPkcolumns = new ArrayList<Column>(); for (Column tableColumn : tableColumns) { if (!tableColumn.isPrimaryKey()) { noPkcolumns.add(tableColumn); } } TableData data = new TableData(); int size = columns.size(); if (needAll) { size = viewColumnNames.size() != 0 ? viewColumnNames.size() : noPkcolumns.size(); // 如果view不为空就使用view作为反查字段 } data.indexs = new int[size]; data.columnNames = new String[size]; data.columnTypes = new int[size]; data.columnValues = new Object[size]; int i = 0; if (needAll) { int index = 0; if (viewColumnNames.size() != 0) { // 存在视图定义 for (Column tableColumn : tableColumns) { if (viewColumnNames.contains(tableColumn.getName())) { // 只放入在view中定义的 data.indexs[i] = index; // 计算下下标 data.columnNames[i] = tableColumn.getName(); data.columnTypes[i] = tableColumn.getTypeCode(); i++; } index++; } } else { for (Column tableColumn : tableColumns) { if (!tableColumn.isPrimaryKey()) { data.indexs[i] = index; // 计算下下标 data.columnNames[i] = tableColumn.getName(); data.columnTypes[i] = tableColumn.getTypeCode(); i++; } index++; } } } else { for (EventColumn column : columns) { int index = 0; for (Column tableColumn : tableColumns) { if (StringUtils.equalsIgnoreCase(column.getColumnName(), tableColumn.getName())) { data.indexs[i] = index; // 计算下下标 data.columnNames[i] = tableColumn.getName(); data.columnTypes[i] = tableColumn.getTypeCode(); i++; break; } index++; } } if (i != columns.size()) { throw new ExtractException( "columns is not found in table " + table.toString() + " columns : " + dumpEventColumn(columns)); } } return data; }