use of com.cubrid.common.ui.cubrid.table.event.ExportDataFailedOneTableEvent in project cubrid-manager by CUBRID.
the class ExprotToOBSHandler method handle.
public void handle(String tableName) throws IOException, SQLException {
// FIXME move this logic to core module
String schemaFile = exportConfig.getSchemaFilePath();
String indexFile = exportConfig.getIndexFilePath();
Set<String> tableSet = new HashSet<String>();
tableSet.addAll(exportConfig.getTableNameList());
try {
try {
if (exportConfig.isExportSchema()) {
exportDataEventHandler.handleEvent(new ExportDataBeginOneTableEvent(schemaFile));
}
if (exportConfig.isExportIndex()) {
exportDataEventHandler.handleEvent(new ExportDataBeginOneTableEvent(indexFile));
}
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));
}
if (exportConfig.isExportData()) {
//data
long totalRecord = exportConfig.getTotalCount(tableName);
if (totalRecord == 0) {
return;
}
String path = exportConfig.getDataFilePath(tableName);
BufferedWriter fs = null;
Connection conn = null;
try {
fs = FileUtil.getBufferedWriter(path, exportConfig.getFileCharset());
conn = getConnection();
if (exportConfig.isExportData()) {
exportDataEventHandler.handleEvent(new ExportDataBeginOneTableEvent(path));
String sql = QueryUtil.getSelectSQL(conn, tableName);
// [TOOLS-2425]Support shard broker
sql = DatabaseInfo.wrapShardQuery(dbInfo, sql);
exportLoad(conn, tableName, fs, path, sql);
exportDataEventHandler.handleEvent(new ExportDataFinishOneTableEvent(path));
}
} finally {
QueryUtil.freeQuery(conn);
try {
if (fs != null) {
fs.close();
}
} catch (IOException e) {
LOGGER.error("", e);
}
}
}
} catch (Exception e) {
LOGGER.error("create schema index error : ", e);
exportDataEventHandler.handleEvent(new ExportDataFailedOneTableEvent(tableName));
}
} catch (Exception e) {
LOGGER.error("", e);
}
}
use of com.cubrid.common.ui.cubrid.table.event.ExportDataFailedOneTableEvent in project cubrid-manager by CUBRID.
the class ExprotToSqlHandler method exportByQuerying.
public void exportByQuerying(String tableName) throws IOException, SQLException {
if (StringUtil.isEmpty(tableName)) {
return;
}
long totalRecord = exportConfig.getTotalCount(tableName);
if (totalRecord == 0) {
return;
}
BufferedWriter fs = null;
String whereCondition = exportConfig.getWhereCondition(tableName);
boolean hasNextPage = true;
long beginIndex = 1;
int exportedCount = 0;
Connection conn = null;
CUBRIDPreparedStatementProxy pStmt = null;
CUBRIDResultSetProxy rs = null;
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();
StringBuffer insert = new StringBuffer("INSERT INTO ");
insert.append(QuerySyntax.escapeKeyword(tableName));
insert.append(" (");
for (int i = 1; i < rsmt.getColumnCount() + 1; i++) {
if (i > 1) {
insert.append(", ");
}
insert.append(QuerySyntax.escapeKeyword(rsmt.getColumnName(i)));
}
insert.append(") ");
while (rs.next()) {
StringBuffer values = new StringBuffer("VALUES (");
for (int j = 1; j < rsmt.getColumnCount() + 1; j++) {
if (j > 1) {
values.append(", ");
}
String columnType = rsmt.getColumnTypeName(j);
int precision = rsmt.getPrecision(j);
columnType = FieldHandlerUtils.amendDataTypeByResult(rs, j, columnType);
setIsHasBigValue(columnType, precision);
values.append(FieldHandlerUtils.getRsValueForExportSQL(columnType, rs, j).toString());
}
values.append(");\n");
fs.write(insert.toString());
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 (Exception e) {
LOGGER.error(e.getMessage(), 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);
FileUtil.close(fs);
}
}
use of com.cubrid.common.ui.cubrid.table.event.ExportDataFailedOneTableEvent in project cubrid-manager by CUBRID.
the class ExprotToSqlHandler method exportFromCache.
public void exportFromCache(String tableName) throws IOException {
if (StringUtil.isEmpty(tableName)) {
return;
}
BufferedWriter fs = null;
int exportedCount = 0;
ResultSetDataCache resultSetDataCache = exportConfig.getResultSetDataCache();
try {
fs = FileUtil.getBufferedWriter(exportConfig.getDataFilePath(tableName), exportConfig.getFileCharset());
try {
List<ColumnInfo> columnInfos = resultSetDataCache.getColumnInfos();
int colCount = columnInfos.size();
StringBuffer insert = new StringBuffer("INSERT INTO ");
insert.append(QuerySyntax.escapeKeyword(tableName));
insert.append(" (");
for (int i = 0; i < colCount; i++) {
if (i > 0) {
insert.append(", ");
}
insert.append(QuerySyntax.escapeKeyword(columnInfos.get(i).getName()));
}
insert.append(") ");
List<ArrayList<Object>> datas = resultSetDataCache.getDatas();
for (ArrayList<Object> rowData : datas) {
StringBuffer values = new StringBuffer("VALUES (");
for (int j = 0; j < colCount; j++) {
if (j > 0) {
values.append(", ");
}
int precision = columnInfos.get(j).getPrecision();
String columnType = columnInfos.get(j).getType();
setIsHasBigValue(columnType, precision);
Object value = rowData.get(j);
if (DataType.DATATYPE_BLOB.equals(columnType) || DataType.DATATYPE_CLOB.equals(columnType)) {
value = DataType.VALUE_NULL;
}
values.append(value.toString());
}
values.append(");\n");
fs.write(insert.toString());
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 (Exception e) {
LOGGER.error(e.getMessage(), e);
exportDataEventHandler.handleEvent(new ExportDataFailedOneTableEvent(tableName));
}
System.gc();
} finally {
FileUtil.close(fs);
}
}
use of com.cubrid.common.ui.cubrid.table.event.ExportDataFailedOneTableEvent 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.common.ui.cubrid.table.event.ExportDataFailedOneTableEvent in project cubrid-manager by CUBRID.
the class ExportToTxtHandler method exportFromCache.
public void exportFromCache(String tableName) throws IOException {
BufferedWriter fs = null;
int exportedCount = 0;
ResultSetDataCache resultSetDataCache = exportConfig.getResultSetDataCache();
try {
fs = FileUtil.getBufferedWriter(exportConfig.getDataFilePath(tableName), exportConfig.getFileCharset());
try {
List<ColumnInfo> columnInfos = resultSetDataCache.getColumnInfos();
int colCount = columnInfos.size();
for (int j = 0; j < colCount; j++) {
fs.write(surround + columnInfos.get(j).getName() + surround);
if (j != colCount - 1) {
fs.write(columnSeprator);
}
}
fs.write(rowSeprator);
fs.flush();
List<ArrayList<Object>> datas = resultSetDataCache.getDatas();
for (ArrayList<Object> rowData : datas) {
writeNextLine(tableName, fs, columnInfos, rowData, 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));
}
System.gc();
} finally {
Closer.close(fs);
}
}
Aggregations