/** * 导出Excel(多工作表) * * @param title 文件名 * @param creator 创建人 * @param tableDataLst 各工作格数据(注意:每个tableData要设置sheet名称,否则按默认呈现) * @return void <style name="dataset"> case SYSROWNUM%2==0?#row0:#row1; fontsize:9px; </style> * <style name="row0"> import(parent); bgcolor:#FFFFFF; </style> <style name="row1"> * import(parent); bgcolor:#CAEAFE; </style> */ public void exportToExcel( String title, String creator, List<TableData> tableDataLst, String subTitleName) throws Exception { HSSFWorkbook wb = new HSSFWorkbook(); // 创建新的Excel 工作簿 HashMap<String, HSSFCellStyle> styles = initStyles(wb); // 初始化表头样式 int i = 1; for (TableData tableData : tableDataLst) { String sheetTitle = tableData.getSheetTitle(); sheetTitle = sheetTitle == null || sheetTitle.equals("") ? "sheet" + i : sheetTitle; wb = writeSheet( wb, tableData.getSheetTitle(), styles, creator, tableData, subTitleName); // 写入工作表 i++; } String sFileName = title + ".xls"; response.setHeader( "Content-Disposition", "attachment;filename=".concat(String.valueOf(URLEncoder.encode(sFileName, "UTF-8")))); response.setHeader("Connection", "close"); response.setHeader("Content-Type", "application/vnd.ms-excel"); wb.write(response.getOutputStream()); }
/** * 向浏览器输出JSON数据 * * @param * @return void */ public void outDataToBrowser(TableData tableData) { StringBuffer outData = new StringBuffer(); // 向前台输出数据 outData.append("{pageInfo: {totalRowNum: " + tableData.getTotalRows() + "},"); outData.append("data: ["); boolean isFirst = true; TableHeaderMetaData headerMetaData = tableData.getTableHeader(); List<TableDataRow> dataRows = tableData.getRows(); try { for (TableDataRow dataRow : dataRows) { List<TableDataCell> dataCells = dataRow.getCells(); int size = dataCells.size(); if (!isFirst) { outData.append(",{"); for (int i = 0; i < size; i++) { outData.append( headerMetaData.getColumnAt(i).getId() + ": '" + dataCells.get(i).getValue() + "',"); } int index = outData.lastIndexOf(","); outData.deleteCharAt(index); outData.append("}"); } else { outData.append("{"); for (int i = 0; i < size; i++) { outData.append( headerMetaData.getColumnAt(i).getId() + ": '" + dataCells.get(i).getValue() + "',"); } int index = outData.lastIndexOf(","); outData.deleteCharAt(index); outData.append("}"); isFirst = false; } } } catch (Exception e) { e.printStackTrace(); } outData.append("]"); outData.append("}"); try { out.print(outData.toString()); out.flush(); out.close(); } catch (IOException e) { e.printStackTrace(); } }
/** 构建数据库主键字段的信息 */ 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; }
public Table( int totalCount, TableHeader header, boolean requireRadioButton, boolean fireEventForFirstRow, final DoubleClickListner doubleClickListner) { super(); initWidget(BINDER.createAndBindUi(this)); this.totalCount = totalCount; this.fireEventForFirstRow = fireEventForFirstRow; tableData = new TableData(); tableData.setHeader(header); this.requireRadioButton = requireRadioButton; navBar = new NavBar(this); mouseOn = false; if (doubleClickListner != null && totalCount != 0) { focusPanel.addMouseOutHandler( new MouseOutHandler() { @Override public void onMouseOut(MouseOutEvent arg0) { mouseOn = false; } }); focusPanel.addMouseOverHandler( new MouseOverHandler() { @Override public void onMouseOver(MouseOverEvent arg0) { mouseOn = true; } }); addDoubleClickHandler( new DoubleClickHandler() { @Override public void onDoubleClick(DoubleClickEvent arg0) { if (mouseOn) { doubleClickListner.onDoubleClickTable(); } } }); } }
public String getSql2() { return tableData2.getQuery(); }
public String getSql1() { return tableData1.getQuery(); }
public int getObjectCount2() { return tableData2.getKeys().size(); }
@Override public void paintComponent(final Graphics g) { super.paintComponent(g); // skip if view is unavailable if (tdata.rows == null) return; gui.painting = true; g.setFont(GUIConstants.font); final int w = getWidth() - scroll.getWidth(); final int h = getHeight(); final int fsz = gui.gprop.num(GUIProp.FONTSIZE); final Context context = tdata.context; final Data data = context.data(); final int focus = gui.context.focused; final int rfocus = tdata.getRoot(data, focus); int mpos = 0; final int nCols = tdata.cols.length; final int nRows = tdata.rows.size(); final int rowH = tdata.rowH; final TableIterator ti = new TableIterator(data, tdata); final TokenBuilder[] tb = new TokenBuilder[nCols]; for (int i = 0; i < nCols; ++i) tb[i] = new TokenBuilder(); focusedString = null; final Nodes marked = context.marked; int l = scroll.pos() / rowH - 1; int posY = -scroll.pos() + l * rowH; while (++l < nRows && marked != null) { // skip when all visible rows have been painted or if data has changed if (posY > h || l >= tdata.rows.size()) break; posY += rowH; final int pre = tdata.rows.get(l); while (mpos < marked.size() && marked.list[mpos] < pre) ++mpos; // draw line g.setColor(GUIConstants.color2); g.drawLine(0, posY + rowH - 1, w, posY + rowH - 1); g.setColor(Color.white); g.drawLine(0, posY + rowH, w, posY + rowH); // verify if current node is marked or focused final boolean rm = mpos < marked.size() && marked.list[mpos] == pre; final boolean rf = pre == rfocus; final int col = rm ? rf ? 5 : 4 : 3; if (rm || rf) { g.setColor(GUIConstants.color(col)); g.fillRect(0, posY - 1, w, rowH); g.setColor(GUIConstants.color(col + 4)); g.drawLine(0, posY - 1, w, posY - 1); } g.setColor(Color.black); // skip drawing of text during animation if (rowH < fsz) continue; // find all row contents ti.init(pre); int fcol = -1; while (ti.more()) { final int c = ti.col; if (ti.pre == focus || data.parent(ti.pre, data.kind(ti.pre)) == focus) fcol = c; // add content to column (skip too long contents)... if (tb[c].size() < 100) { if (tb[c].size() != 0) tb[c].add("; "); tb[c].add(data.text(ti.pre, ti.text)); } } // add dots if content is too long for (final TokenBuilder t : tb) if (t.size() > 100) t.add(DOTS); // draw row contents byte[] focusStr = null; int fx = -1; double x = 1; for (int c = 0; c < nCols; ++c) { // draw single column final double cw = w * tdata.cols[c].width; final double ce = x + cw; if (ce != 0) { final byte[] str = tb[c].size() != 0 ? tb[c].finish() : null; if (str != null) { if (tdata.mouseX > x && tdata.mouseX < ce || fcol == c) { fx = (int) x; focusStr = str; } BaseXLayout.chopString(g, str, (int) x + 1, posY + 2, (int) cw - 4, fsz); tb[c].reset(); } } x = ce; } // highlight focused entry if (rf || fcol != -1) { if (focusStr != null) { final int sw = BaseXLayout.width(g, focusStr) + 8; if (fx > w - sw - 2) fx = w - sw - 2; g.setColor(GUIConstants.color(col + 2)); g.fillRect(fx - 2, posY, sw, rowH - 1); g.setColor(Color.black); BaseXLayout.chopString(g, focusStr, fx + 1, posY + 2, sw, fsz); // cache focused string focusedString = string(focusStr); final int i = focusedString.indexOf("; "); if (i != -1) focusedString = focusedString.substring(0, i); } } } gui.painting = false; }
/** * 写入工作表 * * @param wb Excel工作簿 * @param title Sheet工作表名称 * @param styles 表头样式 * @param creator 创建人 * @param tableData 表格数据 * @throws Exception */ public HSSFWorkbook writeSheet( HSSFWorkbook wb, HashMap<String, HSSFCellStyle> styles, String creator, List<TableData> tableDataLst) throws Exception { SimpleDateFormat formater = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分"); String create_time = formater.format(new Date()); int cnt = 1; for (TableData tableData : tableDataLst) { String sheetTitle = tableData.getSheetTitle(); sheetTitle = sheetTitle == null || sheetTitle.equals("") ? "sheet" + cnt : sheetTitle; cnt++; TableHeaderMetaData headerMetaData = tableData.getTableHeader(); // 获得HTML的表头元素 HSSFSheet sheet = wb.createSheet(sheetTitle); // 在Excel工作簿中建一工作表 sheet.setDisplayGridlines(false); // 设置表标题是否有表格边框 wb.cloneSheet(0); // 创建标题 HSSFRow row = sheet.createRow(0); // 创建新行 HSSFCell cell = row.createCell(0); // 创建新列 int rownum = 0; cell.setCellValue(new HSSFRichTextString(sheetTitle)); HSSFCellStyle style = styles.get("TITLE"); // 设置标题样式 if (style != null) cell.setCellStyle(style); sheet.addMergedRegion( new CellRangeAddress( 0, 0, 0, headerMetaData.getColumnCount() - 1)); // 合并标题行:起始行号,终止行号, 起始列号,终止列号 // 创建副标题 row = sheet.createRow(1); cell = row.createCell(0); cell.setCellValue(new HSSFRichTextString("创建人:")); style = styles.get("SUB_TITLE"); if (style != null) cell.setCellStyle(style); cell = row.createCell(1); cell.setCellValue(new HSSFRichTextString(creator)); style = styles.get("SUB_TITLE2"); if (style != null) cell.setCellStyle(style); cell = row.createCell(2); cell.setCellValue(new HSSFRichTextString("创建时间:")); style = styles.get("SUB_TITLE"); if (style != null) cell.setCellStyle(style); cell = row.createCell(3); style = styles.get("SUB_TITLE2"); cell.setCellValue(new HSSFRichTextString(create_time)); if (style != null) cell.setCellStyle(style); rownum = 3; // 如果rownum = 1,则去掉创建人、创建时间等副标题;如果rownum = 0, 则把标题也去掉 HSSFCellStyle headerstyle = styles.get("TABLE_HEADER"); int colnum = 0; for (int i = 0; i < headerMetaData.getOriginColumns().size(); i++) { TableColumn tc = headerMetaData.getOriginColumns().get(i); if (i != 0) { colnum += headerMetaData.getOriginColumns().get(i - 1).getLength(); } generateColumn(sheet, tc, headerMetaData.maxlevel, rownum, colnum, headerstyle); } rownum += headerMetaData.maxlevel; List<TableDataRow> dataRows = tableData.getRows(); int index = 0; for (TableDataRow dataRow : dataRows) { row = sheet.createRow(rownum); List<TableDataCell> dataCells = dataRow.getCells(); int size = headerMetaData.getColumns().size(); index = -1; for (int i = 0; i < size; i++) { TableColumn tc = headerMetaData.getColumns().get(i); if (!tc.isVisible()) continue; index++; createCell(row, tc, dataCells, i, index, styles); } rownum++; } // 设置前两列根据数据自动列宽 for (int c = 0; c < headerMetaData.getColumns().size(); c++) { sheet.autoSizeColumn((short) c); String t = headerMetaData.getColumns().get(c).getDisplay(); if (sheet.getColumnWidth(c) < t.length() * 256 * 3) sheet.setColumnWidth(c, t.length() * 256 * 3); } sheet.setGridsPrinted(true); } return wb; }
/** * 写入工作表 * * @param wb Excel工作簿 * @param title Sheet工作表名称 * @param styles 表头样式 * @param creator 创建人 * @param tableData 表格数据 * @throws Exception */ public HSSFWorkbook writeSheet( HSSFWorkbook wb, String title, HashMap<String, HSSFCellStyle> styles, String creator, TableData tableData, String subTitleName) throws Exception { TableHeaderMetaData headerMetaData = tableData.getTableHeader(); // 获得HTML的表头元素 SimpleDateFormat formater = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分"); String create_time = formater.format(new Date()); HSSFSheet sheet = wb.createSheet(title); // 在Excel工作簿中建一工作表 sheet.setDisplayGridlines(false); // 设置表标题是否有表格边框 // 创建标题 HSSFRow row = sheet.createRow(0); // 创建新行 HSSFCell cell = row.createCell(0); // 创建新列 int rownum = 0; cell.setCellValue(new HSSFRichTextString(title)); HSSFCellStyle style = styles.get("TITLE"); // 设置标题样式 if (style != null) cell.setCellStyle(style); sheet.addMergedRegion( new CellRangeAddress( 0, 0, 0, headerMetaData.getColumnCount() - 1)); // 合并标题行:起始行号,终止行号, 起始列号,终止列号 // 创建副标题 row = sheet.createRow(1); cell = row.createCell(0); cell.setCellValue(new HSSFRichTextString(subTitleName)); // updated by zdwang 2014-02-21 style = styles.get("SUB_TITLE"); if (style != null) cell.setCellStyle(style); cell = row.createCell(1); cell.setCellValue(new HSSFRichTextString(creator)); style = styles.get("SUB_TITLE2"); if (style != null) cell.setCellStyle(style); cell = row.createCell(2); cell.setCellValue(new HSSFRichTextString("创建时间:")); style = styles.get("SUB_TITLE"); if (style != null) cell.setCellStyle(style); cell = row.createCell(3); style = styles.get("SUB_TITLE2"); cell.setCellValue(new HSSFRichTextString(create_time)); if (style != null) cell.setCellStyle(style); rownum = 3; // 如果rownum = 1,则去掉创建人、创建时间等副标题;如果rownum = 0, 则把标题也去掉 HSSFCellStyle headerstyle = styles.get("TABLE_HEADER"); int colnum = 0; for (int i = 0; i < headerMetaData.getOriginColumns().size(); i++) { TableColumn tc = headerMetaData.getOriginColumns().get(i); if (i != 0) { colnum += headerMetaData.getOriginColumns().get(i - 1).getLength(); } generateColumn(sheet, tc, headerMetaData.maxlevel, rownum, colnum, headerstyle); } rownum += headerMetaData.maxlevel; List<TableDataRow> dataRows = tableData.getRows(); HashMap<Integer, Integer> counter = new HashMap<Integer, Integer>(); HashMap<Integer, String> word = new HashMap<Integer, String>(); int index = 0; for (TableDataRow dataRow : dataRows) { row = sheet.createRow(rownum); List<TableDataCell> dataCells = dataRow.getCells(); int size = headerMetaData.getColumns().size(); index = -1; for (int i = 0; i < size; i++) { TableColumn tc = headerMetaData.getColumns().get(i); if (!tc.isVisible()) continue; index++; String value = dataCells.get(i).getValue(); if (tc.isGrouped()) { String w = word.get(index); if (w == null) { word.put(index, value); counter.put(index, 1); createCell(row, tc, dataCells, i, index, styles); } else { if (w.equals(value)) { counter.put(index, counter.get(index) + 1); } else { stopGrouping(sheet, word, counter, index, size, rownum, styles.get("STRING")); word.put(index, value); counter.put(index, 1); createCell(row, tc, dataCells, i, index, styles); } } } else { createCell(row, tc, dataCells, i, index, styles); } } rownum++; } stopGrouping(sheet, word, counter, 0, index, rownum, styles.get("STRING")); // 设置前两列根据数据自动列宽 for (int c = 0; c < headerMetaData.getColumns().size(); c++) { sheet.autoSizeColumn((short) c); String t = headerMetaData.getColumns().get(c).getDisplay(); if (sheet.getColumnWidth(c) < t.length() * 256 * 3) sheet.setColumnWidth(c, t.length() * 256 * 3); } sheet.setGridsPrinted(true); return wb; }
public int getTableRecordCount() { return tableData.getRecordList().size(); }
private void update(boolean isRadioButton, int startIndex, int selectedIndexlocal) { selectedRowId = null; flexTable.removeAllRows(); navBarTable.removeAllRows(); createTableHeader(isRadioButton); int count = totalCount; int max = startIndex + visibleRecodrCount; if (max > count) { max = count; } navBar.update(startIndex, count, max); setNavigationBar(); TableHeader header = tableData.getHeader(); HeaderColumn[] columns = header.getHeaderColumns(); String width = null; int rowCounter = 0; String radioName = String.valueOf(new Date().getTime()); final List<Record> recordList = tableData.getRecordList(); if (recordList != null) { if (!recordList.isEmpty()) { for (final Record record : recordList) { int colCounter = 0; for (; colCounter < columns.length; colCounter++) { width = columns[colCounter].getWidth() + "%"; flexTable.getCellFormatter().setWidth(rowCounter, colCounter, width); flexTable.setWidget(rowCounter, colCounter, record.getWidget(columns[colCounter])); flexTable .getCellFormatter() .setHorizontalAlignment(rowCounter, colCounter, HasHorizontalAlignment.ALIGN_LEFT); flexTable.getCellFormatter().setWordWrap(rowCounter, colCounter, true); flexTable.getCellFormatter().addStyleName(rowCounter, colCounter, "wordWrap"); } if (isRadioButton) { final RadioButton radioButton = new RadioButton(radioName); if (rowCounter == selectedIndexlocal) { radioButton.setValue(true); selectedRowId = record.getIdentifier(); selectedIndex = rowCounter; if (null != listner && fireEventForFirstRow) { listner.onRowSelected(selectedRowId); } scrollIntoView( flexTable.getWidget(selectedIndexlocal, FIRST_COLUMN_INDEX).getElement()); } radioButton.addClickHandler( new ClickHandler() { @Override public void onClick(ClickEvent arg0) { clearRadioButtons(); radioButton.setValue(true); selectedRowId = record.getIdentifier(); selectedIndex = recordList.indexOf(record); } }); flexTable.setWidget(rowCounter, 0, radioButton); RadioButtonContainer radioButtonContainer = new RadioButtonContainer(radioButton, record.getIdentifier()); radioButtons.put(rowCounter, radioButtonContainer); flexTable .getCellFormatter() .setHorizontalAlignment(rowCounter, 0, HasHorizontalAlignment.ALIGN_CENTER); radioButton.addFocusHandler( new FocusHandler() { @Override public void onFocus(FocusEvent arg0) { removeSelectedRowStyleFromTable(); for (Integer rowId : radioButtons.keySet()) { if (radioButtons.get(rowId).getRadioButton().equals(radioButton)) { selectedIndex = recordList.indexOf(record); flexTable .getRowFormatter() .addStyleName(rowId, selectionStyle.rowHighlighted()); } } } }); } else { RadioButtonContainer radioButtonContainer = new RadioButtonContainer(null, record.getIdentifier()); radioButtons.put(rowCounter, radioButtonContainer); } if (rowCounter % 2 == 0) { flexTable.getRowFormatter().setStyleName(rowCounter, selectionStyle.oddRow()); } else { flexTable.getRowFormatter().setStyleName(rowCounter, selectionStyle.evenRow()); } rowCounter++; flexTable .getRowFormatter() .addStyleName(selectedIndexlocal, selectionStyle.rowHighlighted()); } } else { Label label = new Label(); label.setWidth("100%"); label.setText("No record found."); flexTable.getCellFormatter().setWidth(1, 0, "100%"); flexTable.getFlexCellFormatter().setColSpan(1, 0, 3); // Record record = new Record("1"); // tableData.getRecordList().add(record); flexTable.setWidget(1, 0, label); } } }
private void createTableHeader(boolean isRadioButton) { Images images = GWT.create(Images.class); final TableHeader header = tableData.getHeader(); final List<HeaderColumn> columns = header.getHeaderColumns(isRadioButton); String width = null; int counter = 0; for (final HeaderColumn column : columns) { width = column.getWidth() + "%"; headerTable.getCellFormatter().setWidth(0, counter, width); headerTable.getCellFormatter().addStyleName(0, counter, "wordWrap"); HorizontalPanel headerPanel = new HorizontalPanel(); Label name = new Label(column.getName()); headerPanel.add(name); final Label sortImage = new Label(); sortImage.setWidth("5px"); sortImage.setStyleName("alignMiddle"); if (order != null && column.getDomainProperty() != null && order .getSortProperty() .getProperty() .equals(column.getDomainProperty().getProperty())) { if (column.isPrimaryAsc()) { DOM.setInnerHTML( sortImage.getElement(), AbstractImagePrototype.create(images.sortUp()).getHTML()); } else { DOM.setInnerHTML( sortImage.getElement(), AbstractImagePrototype.create(images.sortDown()).getHTML()); } } headerPanel.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE); headerPanel.add(sortImage); if (counter == 0 && isRadioButton) { name.setText(""); } headerTable.setWidget(0, counter, headerPanel); if (column.isSortable()) { name.addStyleName("cursorHand"); name.addClickHandler( new ClickHandler() { @Override public void onClick(ClickEvent arg0) { order = new Order(column.getDomainProperty(), !column.isPrimaryAsc()); navBar.setOrder(order); column.setPrimaryAsc(!column.isPrimaryAsc()); navBar.getListner().onPagination(navBar.getStartIndex(), visibleRecodrCount, order); } }); } headerTable .getFlexCellFormatter() .setVerticalAlignment(0, counter, HasVerticalAlignment.ALIGN_TOP); headerTable .getCellFormatter() .setHorizontalAlignment(0, counter, HasHorizontalAlignment.ALIGN_LEFT); counter++; } headerTable.getRowFormatter().setStyleName(0, selectionStyle.header()); }
/** 构建数据库非主键字段的信息 */ 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; }
public void run() { try { MDC.put(OtterConstants.splitPipelineLogFileKey, String.valueOf(pipeline.getId())); Thread.currentThread() .setName(String.format(WORKER_NAME_FORMAT, pipeline.getId(), pipeline.getName())); // 获取数据表信息 DataMedia dataMedia = ConfigHelper.findDataMedia(pipeline, eventData.getTableId()); DbDialect dbDialect = dbDialectFactory.getDbDialect(pipeline.getId(), (DbMediaSource) dataMedia.getSource()); Table table = dbDialect.findTable(eventData.getSchemaName(), eventData.getTableName()); TableData keyTableData = buildTableData(table, eventData.getKeys()); // oracle类型特殊处理下 if (dbDialect instanceof OracleDialect) { keyTableData.columnTypes = getOraclePkTypes(table, keyTableData.columnNames); } boolean needAll = pipeline.getParameters().getSyncMode().isRow() || (eventData.getSyncMode() != null && eventData.getSyncMode().isRow()); // 增加一种case, 针对oracle erosa有时侯结果记录只有主键,没有变更字段,需要做一次反查,获取所有字段 needAll |= CollectionUtils.isEmpty(eventData.getUpdatedColumns()) && dataMedia.getSource().getType().isOracle(); List<DataMediaPair> mediaParis = ConfigHelper.findDataMediaPairByMediaId(pipeline, dataMedia.getId()); List<String> viewColumnNames = buildMaxColumnsFromColumnPairs(mediaParis, eventData.getKeys()); // TODO 后续版本测试下 // if (needAll) { // boolean needDb = checkNeedDbForRowMode(table, // viewColumnNames, eventData); // if (needAll && !needDb) {// 不需要进行反查 // item.setFilter(false); // return; // } // } // modified by ljh at 2012-11-04 // 反查数据时只反查带update=true标识的数据,因为update=false的记录可能只是进行filter需要用到的数据,不需要反查 TableData columnTableData = buildTableData(table, eventData.getUpdatedColumns(), needAll, viewColumnNames); if (columnTableData.columnNames.length == 0) { // 全主键,不需要进行反查 } else { List<String> newColumnValues = select( dbDialect, eventData.getSchemaName(), eventData.getTableName(), keyTableData, columnTableData); if (newColumnValues == null) { // miss from db // 设置为filter=true,可能存在丢数据的风险. // 比如针对源库发生主备切换,otter反查的是备库,查询不到对应的记录 // item.setFilter(true); // 针对需要自定义反查数据库的,允许忽略 // a. 自由门触发的数据,不存在时可以忽略 // b. 回环补救算法触发的数据,不存在时可以忽略 boolean needFilter = eventData.isRemedy() || pipeline.getParameters().getSkipNoRow(); item.setFilter(needFilter); // 判断主键是否有变更,如果变更了,就原样返回item int index = 0; for (EventColumn oldKey : eventData.getOldKeys()) { if (!oldKey.equals(eventData.getKeys().get(index))) { item.setFilter(false); break; } } } else { // 构造反查的返回结果 List<EventColumn> newEventColumns = new ArrayList<EventColumn>(); for (int i = 0; i < newColumnValues.size(); i++) { EventColumn column = new EventColumn(); column.setIndex(columnTableData.indexs[i]); column.setColumnName(columnTableData.columnNames[i]); column.setColumnType(columnTableData.columnTypes[i]); column.setNull(newColumnValues.get(i) == null); column.setColumnValue(newColumnValues.get(i)); column.setUpdate(true); newEventColumns.add(column); } // 处理下columns中不在反查字段内的字段列表 for (EventColumn column : eventData.getColumns()) { boolean override = false; for (EventColumn newEventColumn : newEventColumns) { if (StringUtils.equalsIgnoreCase( newEventColumn.getColumnName(), column.getColumnName())) { override = true; break; } } if (!override) { // 针对newcolumns不存在的记录进行添加 newEventColumns.add(column); } } Collections.sort(newEventColumns, new EventColumnIndexComparable()); // 重新排个序 eventData.setColumns(newEventColumns); } } } catch (InterruptedException e) { // ignore } finally { Thread.currentThread().setName(WORKER_NAME); MDC.remove(OtterConstants.splitPipelineLogFileKey); } }