use of com.cubrid.jdbc.proxy.driver.CUBRIDResultSetMetaDataProxy in project cubrid-manager by CUBRID.
the class ExportToXlsxHandler method exportByQuerying.
public void exportByQuerying(String tableName) throws IOException, SQLException {
if (StringUtil.isEmpty(tableName)) {
return;
}
long totalRecord = exportConfig.getTotalCount(tableName);
if (totalRecord == 0) {
return;
}
// 1048576: limit xlsx row number.
int rowLimit = ImportFileConstants.XLSX_ROW_LIMIT;
// 16384: limit xlsx column number.
int columnLimit = ImportFileConstants.XLSX_COLUMN_LIMIT;
int cellCharacterLimit = ImportFileConstants.XLSX_CELL_CHAR_LIMIT;
boolean hasNextPage = true;
long beginIndex = 1;
String whereCondition = exportConfig.getWhereCondition(tableName);
XlsxWriterHelper xlsxWriterhelper = new XlsxWriterHelper();
//create memory workbook
XSSFWorkbook workbook = new XSSFWorkbook();
Calendar cal = Calendar.getInstance();
int datetimeStyleIndex = ((XSSFCellStyle) xlsxWriterhelper.getStyles(workbook).get("datetime")).getIndex();
int timestampStyleIndex = ((XSSFCellStyle) xlsxWriterhelper.getStyles(workbook).get("timestamp")).getIndex();
int dateStyleIndex = ((XSSFCellStyle) xlsxWriterhelper.getStyles(workbook).get("date")).getIndex();
int timeStyleIndex = ((XSSFCellStyle) xlsxWriterhelper.getStyles(workbook).get("time")).getIndex();
int sheetNum = 0;
int xssfRowNum = 0;
File file = new File(exportConfig.getDataFilePath(tableName));
Map<String, File> fileMap = new HashMap<String, File>();
XlsxWriterHelper.SpreadsheetWriter sheetWriter = null;
Connection conn = null;
CUBRIDPreparedStatementProxy pStmt = null;
CUBRIDResultSetProxy rs = null;
boolean isInitedColumnTitle = false;
List<String> columnTitles = new ArrayList<String>();
try {
conn = getConnection();
String sql = QueryUtil.getSelectSQL(conn, tableName);
isPaginating = isPagination(whereCondition, sql, whereCondition);
int exportedCount = 0;
while (hasNextPage) {
String executeSQL = null;
if (isPaginating) {
long endIndex = beginIndex + RSPAGESIZE;
executeSQL = getExecuteSQL(sql, beginIndex, endIndex, whereCondition);
executeSQL = dbInfo.wrapShardQuery(executeSQL);
beginIndex = endIndex + 1;
} else {
executeSQL = getExecuteSQL(sql, whereCondition);
executeSQL = dbInfo.wrapShardQuery(sql);
beginIndex = totalRecord + 1;
}
pStmt = getStatement(conn, executeSQL, tableName);
rs = (CUBRIDResultSetProxy) pStmt.executeQuery();
CUBRIDResultSetMetaDataProxy rsmt = (CUBRIDResultSetMetaDataProxy) rs.getMetaData();
int colCount = rsmt.getColumnCount();
if (colCount >= columnLimit && !isConfirmColumnLimit) {
isConfirmColumnLimit = true;
Display.getDefault().syncExec(new Runnable() {
public void run() {
if (!CommonUITool.openConfirmBox(Messages.exportColumnCountOverWarnInfo)) {
isExit = true;
}
}
});
if (isExit) {
return;
}
colCount = columnLimit;
}
if (!isInitedColumnTitle) {
columnTitles = getColumnTitleList(rsmt);
isInitedColumnTitle = true;
if (isExit) {
return;
}
if (sheetWriter != null) {
try {
XlsxWriterHelper.writeSheetWriter(sheetWriter);
} catch (IOException e) {
sheetWriter = null;
throw e;
}
}
sheetWriter = createSheetWriter(workbook, xlsxWriterhelper, sheetNum++, fileMap, columnTitles, xssfRowNum);
if (exportConfig.isFirstRowAsColumnName()) {
xssfRowNum++;
}
}
try {
while (rs.next()) {
sheetWriter.insertRow(xssfRowNum);
for (int k = 1; k <= colCount; k++) {
String colType = rsmt.getColumnTypeName(k);
colType = FieldHandlerUtils.amendDataTypeByResult(rs, k, colType);
int precision = rsmt.getPrecision(k);
setIsHasBigValue(colType, precision);
Object cellValue = FieldHandlerUtils.getRsValueForExport(colType, rs, k, exportConfig.getNULLValueTranslation());
// We need judge the CLOB/BLOD data by column type
if (DataType.DATATYPE_BLOB.equals(colType) || DataType.DATATYPE_CLOB.equals(colType)) {
if (DataType.DATATYPE_BLOB.equals(colType)) {
String fileName = exportBlobData(tableName, rs, k);
String dataCellValue = DataType.NULL_EXPORT_FORMAT;
if (StringUtil.isNotEmpty(fileName)) {
dataCellValue = DBAttrTypeFormatter.FILE_URL_PREFIX + tableName + BLOB_FOLDER_POSTFIX + File.separator + fileName;
}
sheetWriter.createCell(k - 1, dataCellValue);
} else {
String fileName = exportClobData(tableName, rs, k);
String dataCellValue = DataType.NULL_EXPORT_FORMAT;
if (StringUtil.isNotEmpty(fileName)) {
dataCellValue = DBAttrTypeFormatter.FILE_URL_PREFIX + tableName + CLOB_FOLDER_POSTFIX + File.separator + fileName;
}
sheetWriter.createCell(k - 1, dataCellValue);
}
} else if (cellValue instanceof Long) {
sheetWriter.createCell(k - 1, ((Long) cellValue).longValue());
} else if (cellValue instanceof Double) {
sheetWriter.createCell(k - 1, ((Double) cellValue).doubleValue());
} else if (cellValue instanceof Date) {
cal.setTime((Date) cellValue);
if (DataType.DATATYPE_DATETIME.equals(colType)) {
sheetWriter.createCell(k - 1, cal, datetimeStyleIndex);
} else if (DataType.DATATYPE_DATE.equals(colType)) {
sheetWriter.createCell(k - 1, cal, dateStyleIndex);
} else if (DataType.DATATYPE_TIME.equals(colType)) {
sheetWriter.createCell(k - 1, cal, timeStyleIndex);
} else {
sheetWriter.createCell(k - 1, cal, timestampStyleIndex);
}
} else {
String cellStr = cellValue.toString().length() > cellCharacterLimit ? cellValue.toString().substring(0, cellCharacterLimit) : cellValue.toString();
sheetWriter.createCell(k - 1, Export.covertXMLString(cellStr));
}
}
sheetWriter.endRow();
xssfRowNum++;
exportedCount++;
if ((xssfRowNum + 1) % rowLimit == 0) {
xssfRowNum = 0;
if (sheetWriter != null) {
try {
XlsxWriterHelper.writeSheetWriter(sheetWriter);
} catch (IOException e) {
sheetWriter = null;
throw e;
}
}
sheetWriter = createSheetWriter(workbook, xlsxWriterhelper, sheetNum, fileMap, columnTitles, xssfRowNum);
sheetNum++;
if (exportConfig.isFirstRowAsColumnName()) {
xssfRowNum++;
}
}
if (exportedCount >= COMMIT_LINES) {
exportDataEventHandler.handleEvent(new ExportDataSuccessEvent(tableName, exportedCount));
exportedCount = 0;
}
if (stop) {
break;
}
}
exportDataEventHandler.handleEvent(new ExportDataSuccessEvent(tableName, exportedCount));
exportedCount = 0;
} catch (Exception e) {
LOGGER.error("", e);
exportDataEventHandler.handleEvent(new ExportDataFailedOneTableEvent(tableName));
} finally {
QueryUtil.freeQuery(pStmt, rs);
}
if (hasNextPage(beginIndex, totalRecord)) {
hasNextPage = true;
} else {
hasNextPage = false;
}
System.gc();
}
} finally {
QueryUtil.freeQuery(conn);
try {
if (sheetWriter != null) {
XlsxWriterHelper.writeSheetWriter(sheetWriter);
}
} catch (IOException e) {
sheetWriter = null;
throw e;
} finally {
XlsxWriterHelper.writeWorkbook(workbook, xlsxWriterhelper, fileMap, file);
}
}
}
use of com.cubrid.jdbc.proxy.driver.CUBRIDResultSetMetaDataProxy in project cubrid-manager by CUBRID.
the class GetInfoDataTask method fillColumnData.
/**
* fill table column data by rs,all data is saved to allColumnList
*
* @param rs CUBRIDResultSetProxy
* @throws SQLException if failed
*/
private void fillColumnData(CUBRIDResultSetProxy rs) throws SQLException {
CUBRIDResultSetMetaDataProxy rsmt = (CUBRIDResultSetMetaDataProxy) rs.getMetaData();
int cntColumn = rsmt.getColumnCount();
if (null == allColumnList) {
return;
}
if (doesGetOidInfo) {
allColumnList.add(0, new ColumnInfo("0", DataType.DATATYPE_OID, DataType.DATATYPE_OID, null, 0, 0));
}
for (int i = 1; i <= cntColumn; i++) {
String columnName = rsmt.getColumnName(i);
String typeName = rsmt.getColumnTypeName(i) == null ? "" : rsmt.getColumnTypeName(i);
int scale = rsmt.getScale(i);
int precision = rsmt.getPrecision(i);
String elementTypeName = rsmt.getElementTypeName(i) == null ? "" : rsmt.getElementTypeName(i);
if (typeName.length() == 0) {
int typeIndex = rsmt.getColumnType(i);
switch(typeIndex) {
case Types.BLOB:
typeName = DataType.DATATYPE_BLOB;
break;
case Types.CLOB:
typeName = DataType.DATATYPE_CLOB;
break;
default:
typeName = "";
}
}
ColumnInfo colInfo = new ColumnInfo(i + "", columnName, typeName.toUpperCase(Locale.getDefault()), elementTypeName.toUpperCase(Locale.getDefault()), precision, scale);
allColumnList.add(colInfo);
}
}
Aggregations