use of com.cubrid.jdbc.proxy.driver.CUBRIDResultSetMetaDataProxy in project cubrid-manager by CUBRID.
the class ExportTableDataTask method exportXls.
/**
* Export data as XLS file format
*
* @param rs CUBRIDResultSetProxy
* @param monitor IProgressMonitor
*/
private void exportXls(CUBRIDResultSetProxy rs, final IProgressMonitor monitor) {
// FIXME move this logic to core module
WritableWorkbook workbook = null;
try {
int sheetNum = 0;
workbook = Workbook.createWorkbook(file);
if (fileCharset == null || fileCharset.trim().length() == 0) {
workbook = Workbook.createWorkbook(file);
} else {
WorkbookSettings workbookSettings = new WorkbookSettings();
workbookSettings.setEncoding(fileCharset);
workbook = Workbook.createWorkbook(file, workbookSettings);
}
WritableSheet sheet = workbook.createSheet("Sheet " + sheetNum, sheetNum);
// 65536: limit xls row number.
int rowLimit = ImportFileConstants.XLS_ROW_LIMIT;
// it set 257. Because Tbl's first column is oid value that doesn't export
// 256: limit xls column number.
int columnLimit = ImportFileConstants.XLS_COLUMN_LIMIT + 1;
int cellCharacterLimit = ImportFileConstants.XLSX_CELL_CHAR_LIMIT;
CUBRIDResultSetMetaDataProxy rsmt = (CUBRIDResultSetMetaDataProxy) rs.getMetaData();
int colCount = rsmt.getColumnCount() + 1;
isExit = false;
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;
}
}
// first line add column name
if (isFirstRowAsColumnName) {
WritableFont wf = new WritableFont(WritableFont.ARIAL, 12, WritableFont.BOLD, false);
WritableCellFormat wcf = new WritableCellFormat(wf);
if (columnName.length() > cellCharacterLimit) {
Label label = new Label(j - 1, 0, columnName.substring(0, cellCharacterLimit));
sheet.addCell(label);
label = null;
} else {
Label label = new Label(j - 1, 0, columnName.toString(), wcf);
sheet.addCell(label);
label = null;
}
wcf = null;
wf = null;
}
}
if (colCount > columnLimit) {
Display.getDefault().syncExec(new Runnable() {
public void run() {
if (!CommonUITool.openConfirmBox(Messages.exportColumnCountOverWarnInfo)) {
isExit = true;
}
}
});
if (isExit) {
return;
}
colCount = columnLimit;
}
int xlsRecordNum = 0;
if (isFirstRowAsColumnName) {
xlsRecordNum = 1;
}
while (rs.next()) {
if (!CommonUITool.isAvailableMemory(REMAINING_MEMORY_SIZE)) {
throw new OutOfMemoryError();
}
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, nullValue);
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 (((exportedCount + 1) % rowLimit) == 0) {
sheetNum++;
xlsRecordNum -= rowLimit;
sheet = workbook.createSheet("Sheet " + sheetNum, sheetNum);
}
exportedCount++;
monitor.worked(10);
monitor.subTask(Messages.bind(Messages.msgExportDataRow, exportedCount));
}
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
try {
if (workbook != null) {
workbook.write();
}
} catch (Exception ignored) {
}
try {
if (workbook != null) {
workbook.close();
}
} catch (Exception ignored) {
}
}
}
use of com.cubrid.jdbc.proxy.driver.CUBRIDResultSetMetaDataProxy in project cubrid-manager by CUBRID.
the class ExportTableDataTask method exportLoad.
/**
* Export data as CUBRID load format
*
* @param rs CUBRIDResultSetProxy
* @param tableName String
* @param monitor IProgressMonitor
* @throws SQLException The exception
* @throws IOException The exception
*/
private void exportLoad(CUBRIDResultSetProxy rs, String tableName, final IProgressMonitor monitor) throws SQLException, IOException {
// FIXME move this logic to core module
BufferedWriter fs = null;
try {
fs = getBufferedWriter();
CUBRIDResultSetMetaDataProxy rsmt = (CUBRIDResultSetMetaDataProxy) rs.getMetaData();
// loaddb header
StringBuffer header = new StringBuffer("%class ");
header.append(QuerySyntax.escapeKeyword(tableName));
header.append(" (");
for (int i = 1; i < rsmt.getColumnCount() + 1; i++) {
if (i > 1) {
header.append(" ");
}
header.append(QuerySyntax.escapeKeyword(rsmt.getColumnName(i)));
}
header.append(")\n");
fs.write(header.toString());
// loaddb body
while (rs.next()) {
StringBuffer values = new StringBuffer();
values.append(++exportedCount);
values.append(":");
for (int j = 1; j < rsmt.getColumnCount() + 1; j++) {
String columnType = rsmt.getColumnTypeName(j);
int precision = rsmt.getPrecision(j);
columnType = FieldHandlerUtils.amendDataTypeByResult(rs, j, columnType);
setIsHasBigValue(columnType, precision);
values.append(FieldHandlerUtils.getRsValueForExportOBS(columnType, rs, j).toString());
}
values.append("\n");
fs.write(values.toString());
if (exportedCount % COMMIT_LINES == 0) {
fs.flush();
}
monitor.worked(10);
monitor.subTask(Messages.bind(Messages.msgExportDataRow, exportedCount));
}
} finally {
FileUtil.close(fs);
}
}
use of com.cubrid.jdbc.proxy.driver.CUBRIDResultSetMetaDataProxy in project cubrid-manager by CUBRID.
the class QueryTunerJob method getColumnData.
/**
* Init table column data
* @param rs
* @return
* @throws SQLException
*/
private List<ColumnInfo> getColumnData(CUBRIDResultSetProxy rs) throws SQLException {
// FIXME move this logic to core module
List<ColumnInfo> columnInfoList = new ArrayList<ColumnInfo>();
CUBRIDResultSetMetaDataProxy rsmt = (CUBRIDResultSetMetaDataProxy) rs.getMetaData();
int cntColumn = rsmt.getColumnCount();
for (int i = 1; i <= cntColumn; i++) {
String columnName = rsmt.getColumnName(i);
String typeName = StringUtil.nvl(rsmt.getColumnTypeName(i));
int scale = rsmt.getScale(i);
int precision = rsmt.getPrecision(i);
String elementTypeName = StringUtil.nvl(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 = "";
}
}
String columnType = typeName.toUpperCase(Locale.getDefault());
String elementType = elementTypeName.toUpperCase(Locale.getDefault());
ColumnInfo colInfo = new ColumnInfo(String.valueOf(i), columnName, columnType, elementType, precision, scale);
columnInfoList.add(colInfo);
}
return columnInfoList;
}
use of com.cubrid.jdbc.proxy.driver.CUBRIDResultSetMetaDataProxy in project cubrid-manager by CUBRID.
the class FieldHandlerUtils method getRsValueForExport.
/**
* 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 getRsValueForExport(String colType, CUBRIDResultSetProxy rs, int colNumber, String nullValue) throws SQLException {
Object result = null;
Object data = rs.getObject(colNumber);
if (data == null || DataType.isNullValueForExport(colType, data)) {
result = nullValue;
} else if (DataType.DATATYPE_INTEGER.equals(colType) || DataType.DATATYPE_TINYINT.equals(colType) || DataType.DATATYPE_SMALLINT.equals(colType) || DataType.DATATYPE_BIGINT.equals(colType)) {
result = rs.getLong(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) {
result = DataType.BIT_EXPORT_FORMAT;
} else {
result = "X'" + DBAttrTypeFormatter.getHexString(bytes) + "'";
}
} else if (DataType.DATATYPE_DOUBLE.equals(colType) || DataType.DATATYPE_FLOAT.equals(colType) || DataType.DATATYPE_REAL.equals(colType)) {
result = rs.getDouble(colNumber);
} else if (DataType.DATATYPE_NUMERIC.equals(colType) || DataType.DATATYPE_DECIMAL.equals(colType) || DataType.DATATYPE_MONETARY.equals(colType)) {
result = rs.getBigDecimal(colNumber).toString();
} else if (DataType.DATATYPE_DATETIME.equals(colType)) {
result = rs.getTimestamp(colNumber);
} else if (DataType.DATATYPE_TIMESTAMP.equals(colType)) {
result = rs.getTimestamp(colNumber);
} else if (DataType.DATATYPE_TIME.equals(colType)) {
result = rs.getTime(colNumber);
} else if (DataType.DATATYPE_DATE.equals(colType)) {
result = rs.getDate(colNumber);
} else if (DataType.DATATYPE_CLASS.equals(colType)) {
result = DataType.DATATYPE_CLASS;
} 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("{");
for (int k = 0; k < set.length; k++) {
CUBRIDResultSetMetaDataProxy rsmt = (CUBRIDResultSetMetaDataProxy) rs.getMetaData();
String elemType = rsmt.getElementTypeName(colNumber);
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 {
if (DataType.DATATYPE_DATETIME.equals(elemType)) {
Timestamp datetime = (Timestamp) setk;
String datetimeStr = formatDateTime(datetime);
value.append(getCVSValueInSet(elemType, datetimeStr));
} else {
value.append(getCVSValueInSet(elemType, setk.toString()));
}
}
}
value.append("}");
result = value.toString();
} else if (DataType.DATATYPE_BLOB.equals(colType)) {
result = DataType.BLOB_EXPORT_FORMAT;
} else if (DataType.DATATYPE_CLOB.equals(colType)) {
result = DataType.CLOB_EXPORT_FORMAT;
} else {
result = rs.getString(colNumber);
}
return result;
}
use of com.cubrid.jdbc.proxy.driver.CUBRIDResultSetMetaDataProxy in project cubrid-manager by CUBRID.
the class ExportLoadDBHandler method handle.
public void handle(String nullValue) throws IOException, SQLException {
// FIXME move this logic to core module
Connection conn = null;
CUBRIDPreparedStatementProxy pStmt = null;
CUBRIDResultSetProxy rs = null;
BufferedWriter fs = null;
String schemaFile = exportConfig.getDataFilePath(ExportConfig.LOADDB_SCHEMAFILEKEY);
String indexFile = exportConfig.getDataFilePath(ExportConfig.LOADDB_INDEXFILEKEY);
String dataTablesName = exportConfig.getDataFilePath(ExportConfig.LOADDB_DATAFILEKEY);
// Get connection
try {
conn = getConnection();
} catch (SQLException e) {
LOGGER.error(e.getMessage(), e);
if (exportConfig.isExportSchema()) {
exportDataEventHandler.handleEvent(new ExportDataFailedOneTableEvent(schemaFile));
}
if (exportConfig.isExportIndex()) {
exportDataEventHandler.handleEvent(new ExportDataFailedOneTableEvent(indexFile));
}
exportDataEventHandler.handleEvent(new ExportDataFailedOneTableEvent(dataTablesName));
QueryUtil.freeQuery(conn);
throw e;
}
// Export schema
boolean isExportSchemaSuccess = true;
try {
if (exportConfig.isExportSchema()) {
exportDataEventHandler.handleEvent(new ExportDataBeginOneTableEvent(schemaFile));
}
if (exportConfig.isExportIndex()) {
exportDataEventHandler.handleEvent(new ExportDataBeginOneTableEvent(indexFile));
}
Set<String> tableSet = new HashSet<String>();
tableSet.addAll(exportConfig.getTableNameList());
exportSchemaToOBSFile(dbInfo, exportDataEventHandler, tableSet, schemaFile, indexFile, exportConfig.getFileCharset(), exportConfig.isExportSerialStartValue());
if (exportConfig.isExportSchema()) {
exportDataEventHandler.handleEvent(new ExportDataSuccessEvent(schemaFile));
exportDataEventHandler.handleEvent(new ExportDataFinishOneTableEvent(schemaFile));
}
if (exportConfig.isExportIndex()) {
exportDataEventHandler.handleEvent(new ExportDataSuccessEvent(indexFile));
exportDataEventHandler.handleEvent(new ExportDataFinishOneTableEvent(indexFile));
}
} catch (IOException e) {
LOGGER.error(e.getMessage(), e);
isExportSchemaSuccess = false;
} catch (SQLException e) {
LOGGER.error(e.getMessage(), e);
isExportSchemaSuccess = false;
} finally {
if (!isExportSchemaSuccess) {
if (exportConfig.isExportSchema()) {
exportDataEventHandler.handleEvent(new ExportDataFailedOneTableEvent(schemaFile));
}
if (exportConfig.isExportIndex()) {
exportDataEventHandler.handleEvent(new ExportDataFailedOneTableEvent(indexFile));
}
}
}
// Export data
try {
exportDataEventHandler.handleEvent(new ExportDataBeginOneTableEvent(dataTablesName));
fs = FileUtil.getBufferedWriter(exportConfig.getDataFilePath(ExportConfig.LOADDB_DATAFILEKEY), exportConfig.getFileCharset());
for (String tableName : exportConfig.getTableNameList()) {
String whereCondition = exportConfig.getWhereCondition(tableName);
long totalRecord = exportConfig.getTotalCount(tableName);
if (totalRecord == 0) {
continue;
}
boolean hasNextPage = true;
int exportedCount = 0;
long beginIndex = 1;
String sql = QueryUtil.getSelectSQL(conn, tableName);
isPaginating = isPagination(tableName, sql, whereCondition);
boolean isExportedColumnTitles = false;
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();
if (!isExportedColumnTitles) {
StringBuilder header = new StringBuilder("%class \"");
header.append(tableName);
header.append("\" (");
for (int i = 1; i < rsmt.getColumnCount() + 1; i++) {
if (i > 1) {
header.append(" ");
}
header.append("\"");
header.append(rsmt.getColumnName(i));
header.append("\"");
}
header.append(")\n");
fs.write(header.toString());
isExportedColumnTitles = true;
}
while (rs.next()) {
StringBuffer values = new StringBuffer();
for (int j = 1; j < rsmt.getColumnCount() + 1; j++) {
String columnType = rsmt.getColumnTypeName(j);
int precision = rsmt.getPrecision(j);
columnType = FieldHandlerUtils.amendDataTypeByResult(rs, j, columnType);
setIsHasBigValue(columnType, precision);
values.append(FieldHandlerUtils.getRsValueForExportOBS(columnType, rs, j).toString());
}
values.append("\n");
fs.write(values.toString());
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 (SQLException e) {
LOGGER.error(e.getMessage(), e);
exportDataEventHandler.handleEvent(new ExportDataFailedOneTableEvent(tableName));
} finally {
QueryUtil.freeQuery(rs);
}
if (hasNextPage(beginIndex, totalRecord)) {
hasNextPage = true;
fs.write(StringUtil.NEWLINE);
} else {
hasNextPage = false;
}
System.gc();
}
}
} catch (IOException e) {
LOGGER.error(e.getMessage(), e);
throw e;
} finally {
try {
if (fs != null) {
fs.flush();
fs.close();
fs = null;
}
} catch (IOException e) {
LOGGER.error(e.getMessage(), e);
}
}
}
Aggregations