use of com.cubrid.jdbc.proxy.driver.CUBRIDResultSetMetaDataProxy in project cubrid-manager by CUBRID.
the class ExportToTxtHandler method exportByQuerying.
public void exportByQuerying(String tableName) throws IOException, SQLException {
BufferedWriter fs = null;
Connection conn = null;
CUBRIDPreparedStatementProxy pStmt = null;
CUBRIDResultSetProxy rs = null;
boolean hasNextPage = true;
long totalRecord = exportConfig.getTotalCount(tableName);
long beginIndex = 1;
int exportedCount = 0;
String whereCondition = exportConfig.getWhereCondition(tableName);
boolean isExportedColumnTitles = false;
List<String> columnTitles = new ArrayList<String>();
try {
conn = getConnection();
fs = FileUtil.getBufferedWriter(exportConfig.getDataFilePath(tableName), exportConfig.getFileCharset());
String sql = QueryUtil.getSelectSQL(conn, tableName);
isPaginating = isPagination(tableName, sql, whereCondition);
while (hasNextPage) {
try {
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();
// Init title
if (!isExportedColumnTitles) {
for (int j = 1; j < colCount; j++) {
String columnName = rsmt.getColumnName(j);
String columnType = rsmt.getColumnTypeName(j);
int precision = rsmt.getPrecision(j);
columnType = columnType == null ? "" : columnType;
setIsHasBigValue(columnType, precision);
columnTitles.add(columnName);
}
isExportedColumnTitles = true;
if (exportConfig.isFirstRowAsColumnName()) {
for (int j = 1; j < rsmt.getColumnCount() + 1; j++) {
fs.write(surround + rsmt.getColumnName(j) + surround);
if (j != rsmt.getColumnCount()) {
fs.write(columnSeprator);
}
}
fs.write(rowSeprator);
fs.flush();
}
}
while (rs.next()) {
writeNextLine(tableName, fs, rs, rsmt, columnSeprator, rowSeprator, surround);
fs.write(rowSeprator);
exportedCount++;
if (exportedCount >= COMMIT_LINES) {
fs.flush();
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;
fs.write(rowSeprator);
} else {
hasNextPage = false;
}
System.gc();
}
} finally {
QueryUtil.freeQuery(conn);
Closer.close(fs);
}
}
use of com.cubrid.jdbc.proxy.driver.CUBRIDResultSetMetaDataProxy in project cubrid-manager by CUBRID.
the class ExportToXlsHandler method exportByQuerying.
public void exportByQuerying(String tableName) throws IOException, SQLException {
if (StringUtil.isEmpty(tableName)) {
// FIXME move this logic to core module
return;
}
long totalRecord = exportConfig.getTotalCount(tableName);
if (totalRecord == 0) {
return;
}
Connection conn = null;
CUBRIDPreparedStatementProxy pStmt = null;
CUBRIDResultSetProxy rs = null;
WritableWorkbook workbook = null;
int workbookNum = 0;
String whereCondition = exportConfig.getWhereCondition(tableName);
boolean hasNextPage = true;
long beginIndex = 1;
int cellCharacterLimit = ImportFileConstants.XLSX_CELL_CHAR_LIMIT;
// 65536: limit xls row number.
int rowLimit = ImportFileConstants.XLS_ROW_LIMIT;
boolean isInitedColumnTitles = false;
List<String> columnTitles = new ArrayList<String>();
try {
conn = getConnection();
int sheetNum = 0;
int xlsRecordNum = 0;
workbook = createWorkbook(exportConfig.getDataFilePath(tableName), workbookNum++);
WritableSheet sheet = workbook.createSheet("Sheet " + sheetNum, sheetNum);
sheetNum++;
int exportedCount = 0;
String sql = QueryUtil.getSelectSQL(conn, tableName);
isPaginating = isPagination(whereCondition, sql, whereCondition);
while (hasNextPage) {
try {
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 (!isInitedColumnTitles) {
isInitedColumnTitles = true;
columnTitles = getColumnTitleList(rsmt);
if (isExit) {
return;
}
// first line add column name
if (exportConfig.isFirstRowAsColumnName()) {
writeHeader(sheet, columnTitles);
xlsRecordNum++;
}
}
while (rs.next()) {
//Check memory
if (!CommonUITool.isAvailableMemory(REMAINING_MEMORY_SIZE)) {
closeWorkbook(workbook);
workbook = null;
System.gc();
workbook = createWorkbook(exportConfig.getDataFilePath(tableName), workbookNum++);
sheetNum = 0;
sheet = workbook.createSheet("Sheet " + sheetNum, sheetNum);
sheetNum++;
xlsRecordNum = 0;
// first line add column name
if (exportConfig.isFirstRowAsColumnName()) {
writeHeader(sheet, columnTitles);
xlsRecordNum++;
}
}
for (int j = 1; j <= colCount; j++) {
String colType = rsmt.getColumnTypeName(j);
colType = FieldHandlerUtils.amendDataTypeByResult(rs, j, colType);
int precision = rsmt.getPrecision(j);
setIsHasBigValue(colType, precision);
Object cellValue = FieldHandlerUtils.getRsValueForExport(colType, rs, j, 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, j);
String dataCellValue = DataType.NULL_EXPORT_FORMAT;
if (StringUtil.isNotEmpty(fileName)) {
dataCellValue = DBAttrTypeFormatter.FILE_URL_PREFIX + tableName + BLOB_FOLDER_POSTFIX + File.separator + fileName;
}
sheet.addCell(new Label(j - 1, xlsRecordNum, dataCellValue));
} else {
String fileName = exportClobData(tableName, rs, j);
String dataCellValue = DataType.NULL_EXPORT_FORMAT;
if (StringUtil.isNotEmpty(fileName)) {
dataCellValue = DBAttrTypeFormatter.FILE_URL_PREFIX + tableName + CLOB_FOLDER_POSTFIX + File.separator + fileName;
}
sheet.addCell(new Label(j - 1, xlsRecordNum, dataCellValue));
}
} else if (cellValue instanceof Long) {
sheet.addCell(new Number(j - 1, xlsRecordNum, (Long) cellValue));
} else if (cellValue instanceof Double) {
sheet.addCell(new Number(j - 1, xlsRecordNum, (Double) cellValue));
} else if (cellValue instanceof Timestamp) {
String dataCellValue = FieldHandlerUtils.formatDateTime((Timestamp) cellValue);
sheet.addCell(new Label(j - 1, xlsRecordNum, dataCellValue));
} else if (cellValue instanceof java.sql.Time) {
String dataCellValue = DateUtil.getDatetimeString(((java.sql.Time) cellValue).getTime(), FieldHandlerUtils.FORMAT_TIME);
sheet.addCell(new Label(j - 1, xlsRecordNum, dataCellValue));
} else if (cellValue instanceof java.sql.Date) {
String dataCellValue = DateUtil.getDatetimeString(((java.sql.Date) cellValue).getTime(), FieldHandlerUtils.FORMAT_DATE);
sheet.addCell(new Label(j - 1, xlsRecordNum, dataCellValue));
} else {
sheet.addCell(new Label(j - 1, xlsRecordNum, cellValue.toString().length() > cellCharacterLimit ? cellValue.toString().substring(0, cellCharacterLimit) : cellValue.toString()));
}
}
xlsRecordNum++;
if ((xlsRecordNum + 1) % rowLimit == 0) {
xlsRecordNum = 0;
sheet = workbook.createSheet("Sheet " + sheetNum, sheetNum);
sheetNum++;
// first line add column name
if (exportConfig.isFirstRowAsColumnName()) {
writeHeader(sheet, columnTitles);
xlsRecordNum++;
}
}
exportedCount++;
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();
}
} catch (Exception e) {
LOGGER.error("", e);
} finally {
QueryUtil.freeQuery(conn);
closeWorkbook(workbook);
}
}
use of com.cubrid.jdbc.proxy.driver.CUBRIDResultSetMetaDataProxy in project cubrid-manager by CUBRID.
the class FieldHandlerUtils method getRsValueForExportSQL.
/**
* Get values to export from resultset.
*
* @param colType column data type
* @param rs ResultSet
* @param colNumber column number
* @return the value to be set. String or number.
* @throws SQLException The exception
*/
public static Object getRsValueForExportSQL(String colType, CUBRIDResultSetProxy rs, int colNumber) throws SQLException {
StringBuffer values = new StringBuffer();
Object data = rs.getObject(colNumber);
if (data == null || DataType.isNullValueForExport(colType, data)) {
values.append(DataType.VALUE_NULL);
} else if (DataType.DATATYPE_MONETARY.equals(colType) || DataType.DATATYPE_INTEGER.equals(colType) || DataType.DATATYPE_TINYINT.equals(colType) || DataType.DATATYPE_SMALLINT.equals(colType) || DataType.DATATYPE_BIGINT.equals(colType) || DataType.DATATYPE_DOUBLE.equals(colType) || DataType.DATATYPE_FLOAT.equals(colType) || DataType.DATATYPE_REAL.equals(colType) || DataType.DATATYPE_NUMERIC.equals(colType) || DataType.DATATYPE_DECIMAL.equals(colType)) {
values.append(rs.getString(colNumber));
} else if (DataType.DATATYPE_BIT.equals(colType) || DataType.DATATYPE_BIT_VARYING.equals(colType)) {
byte[] bytes = (byte[]) data;
if (bytes.length > BIT_TYPE_MUCH_VALUE_LENGTH && BIT_TYPE_MUCH_VALUE_LENGTH > 0) {
values.append(DataType.VALUE_NULL);
} else {
String bitString = DBAttrTypeFormatter.getHexString(bytes);
values.append("X'");
values.append(bitString);
values.append("'");
}
} else if (colType.startsWith(DataType.DATATYPE_NCHAR)) {
values.append("N'");
values.append(rs.getString(colNumber).replaceAll("'", "''"));
values.append("'");
} else if (colType.equalsIgnoreCase(DataType.DATATYPE_DATETIME)) {
String datetimeStr = formatDateTime(rs.getTimestamp(colNumber));
String formatValue = DBAttrTypeFormatter.formatValue(DataType.DATATYPE_DATETIME, datetimeStr, true);
values.append(formatValue);
} else if (colType.equalsIgnoreCase(DataType.DATATYPE_TIMESTAMP)) {
String datetime = rs.getString(colNumber);
String formatValue = DBAttrTypeFormatter.formatValue(DataType.DATATYPE_TIMESTAMP, datetime, true);
values.append(formatValue);
} else if (colType.equalsIgnoreCase(DataType.DATATYPE_DATE)) {
String datetime = rs.getString(colNumber);
String formatValue = DBAttrTypeFormatter.formatValue(DataType.DATATYPE_DATE, datetime, true);
values.append(formatValue);
} else if (colType.equalsIgnoreCase(DataType.DATATYPE_TIME)) {
String datetime = rs.getString(colNumber);
String formatValue = DBAttrTypeFormatter.formatValue(DataType.DATATYPE_TIME, datetime, true);
values.append(formatValue);
} else if (colType.startsWith(DataType.DATATYPE_CHAR) || colType.startsWith(DataType.DATATYPE_VARCHAR)) {
values.append("'");
values.append(rs.getString(colNumber).replaceAll("'", "''"));
values.append("'");
} else if (DataType.DATATYPE_CLASS.equals(colType)) {
values.append(DataType.VALUE_NULL);
} else if (DataType.DATATYPE_SET.equals(colType) || DataType.DATATYPE_MULTISET.equals(colType) || DataType.DATATYPE_SEQUENCE.equals(colType)) {
Object[] set = (Object[]) rs.getCollection(colNumber);
StringBuffer value = new StringBuffer("{");
CUBRIDResultSetMetaDataProxy rsmt = (CUBRIDResultSetMetaDataProxy) rs.getMetaData();
String elemType = rsmt.getElementTypeName(colNumber);
int s = rsmt.getScale(colNumber);
int p = rsmt.getPrecision(colNumber);
elemType = DataType.makeType(elemType, null, p, s);
elemType = DataType.getRealType(elemType);
for (int k = 0; k < set.length; k++) {
if (k > 0) {
value.append(", ");
}
Object setK = set[k];
if (setK == null) {
value.append(DataType.VALUE_NULL);
} else if (setK.getClass() == CUBRIDOIDProxy.getCUBRIDOIDClass(rs.getJdbcVersion())) {
value.append(DataType.VALUE_NULL);
} else {
String elem = setK.toString();
if (elemType.equalsIgnoreCase(DataType.DATATYPE_DATETIME)) {
Timestamp datetime = (Timestamp) setK;
elem = formatDateTime(datetime);
}
elem = DBAttrTypeFormatter.formatValue(elemType, elem, true);
value.append(elem);
}
}
value.append("}");
values.append(value);
} else {
values.append(DataType.VALUE_NULL);
}
return values.toString();
}
use of com.cubrid.jdbc.proxy.driver.CUBRIDResultSetMetaDataProxy in project cubrid-manager by CUBRID.
the class ExportTableDataTask method exportXlsx.
/**
*
* Export data as XLSX file format
*
* @param rs CUBRIDResultSetProxy
* @param monitor IProgressMonitor
* @throws IOException The exception
* @throws SQLException The exception
*/
private void exportXlsx(CUBRIDResultSetProxy rs, final IProgressMonitor monitor) throws IOException, SQLException {
// FIXME move this logic to core module
// 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;
CUBRIDResultSetMetaDataProxy rsmt = (CUBRIDResultSetMetaDataProxy) rs.getMetaData();
int colCount = rsmt.getColumnCount();
if (colCount >= columnLimit) {
Display.getDefault().syncExec(new Runnable() {
public void run() {
if (!CommonUITool.openConfirmBox(Messages.exportColumnCountOverWarnInfo)) {
isExit = true;
}
}
});
if (isExit) {
return;
}
colCount = columnLimit;
}
List<String> columnTitles = new ArrayList<String>();
for (int j = 1; j <= colCount; j++) {
String columnName = rsmt.getColumnName(j);
String columnType = rsmt.getColumnTypeName(j);
int precision = rsmt.getPrecision(j);
columnType = columnType == null ? "" : columnType;
setIsHasBigValue(columnType, precision);
//the data length > XLS column character limit
if (precision > cellCharacterLimit) {
final String confirmMSG = Messages.bind(Messages.exportCharacterCountExceedWarnInfo, columnName);
Display.getDefault().syncExec(new Runnable() {
public void run() {
if (!CommonUITool.openConfirmBox(confirmMSG)) {
isExit = true;
}
}
});
if (isExit) {
return;
}
}
if (columnName.length() > cellCharacterLimit) {
columnName = columnName.substring(0, cellCharacterLimit);
}
columnTitles.add(columnName);
}
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 = 1;
int xssfRowNum = 0;
Map<String, File> fileMap = new HashMap<String, File>();
XlsxWriterHelper.SpreadsheetWriter sheetWriter = null;
try {
sheetWriter = createSheetWriter(workbook, xlsxWriterhelper, sheetNum, fileMap, columnTitles, xssfRowNum);
if (isFirstRowAsColumnName && columnTitles != null) {
xssfRowNum++;
}
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, nullValue);
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++;
if (((exportedCount + 1) % rowLimit) == 0) {
sheetNum++;
xssfRowNum -= rowLimit;
try {
XlsxWriterHelper.writeSheetWriter(sheetWriter);
} catch (IOException e) {
sheetWriter = null;
throw e;
}
sheetWriter = createSheetWriter(workbook, xlsxWriterhelper, sheetNum, fileMap, columnTitles, xssfRowNum);
}
exportedCount++;
monitor.worked(10);
monitor.subTask(Messages.bind(Messages.msgExportDataRow, exportedCount));
}
} finally {
try {
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 ExportTableDataTask method exportTxt.
/**
* Export data as TXT file format
*
* @param rs CUBRIDResultSetProxy
* @param monitor IProgressMonitor
* @throws SQLException The exception
* @throws IOException The exception
*/
private void exportTxt(CUBRIDResultSetProxy rs, final IProgressMonitor monitor, String seprator, String surround) throws SQLException, IOException {
// FIXME move this logic to core module
BufferedWriter fs = null;
try {
fs = getBufferedWriter();
CUBRIDResultSetMetaDataProxy rsmt = (CUBRIDResultSetMetaDataProxy) rs.getMetaData();
// first line add column name
if (isFirstRowAsColumnName) {
for (int j = 1; j < rsmt.getColumnCount() + 1; j++) {
fs.write(surround + rsmt.getColumnName(j) + surround);
if (j != rsmt.getColumnCount()) {
fs.write(seprator);
}
}
fs.write('\n');
fs.flush();
}
while (rs.next()) {
writeNextLine(fs, rs, rsmt, seprator, surround);
exportedCount++;
if (exportedCount % COMMIT_LINES == 0) {
fs.flush();
}
monitor.worked(10);
monitor.subTask(Messages.bind(Messages.msgExportDataRow, exportedCount));
}
} finally {
try {
if (fs != null) {
fs.close();
}
} catch (IOException e) {
LOGGER.error("", e);
}
}
}
Aggregations