use of com.cubrid.jdbc.proxy.driver.CUBRIDResultSetMetaDataProxy in project cubrid-manager by CUBRID.
the class ExprotToOBSHandler method exportLoad.
/**
* Export data as CUBRID load format
*
* @param stmt Statement
* @param tableName String
* @param monitor IProgressMonitor
* @param fs String
* @param fileName BufferedWriter
* @throws SQLException The exception
* @throws IOException The exception
*/
private void exportLoad(Connection conn, String tableName, BufferedWriter fs, String fileName, String sql) throws SQLException, IOException {
// FIXME move this logic to core module
CUBRIDPreparedStatementProxy pStmt = null;
CUBRIDResultSetProxy rs = null;
boolean hasNextPage = true;
long beginIndex = 1;
int exportedCount = 0;
long totalRecord = exportConfig.getTotalCount(tableName);
String whereCondition = exportConfig.getWhereCondition(tableName);
isPaginating = isPagination(tableName, sql, whereCondition);
boolean isNeedWriteHeader = true;
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 (isNeedWriteHeader) {
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());
isNeedWriteHeader = false;
}
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 (Exception e) {
LOGGER.error("export date write load db error : ", e);
} finally {
fs.flush();
QueryUtil.freeQuery(pStmt, rs);
}
if (hasNextPage(beginIndex, totalRecord)) {
hasNextPage = true;
} else {
hasNextPage = false;
}
System.gc();
}
}
use of com.cubrid.jdbc.proxy.driver.CUBRIDResultSetMetaDataProxy 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.jdbc.proxy.driver.CUBRIDResultSetMetaDataProxy in project cubrid-manager by CUBRID.
the class QueryExecuter 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;
}
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);
allColumnList.add(colInfo);
}
resultSetDataCache.setColumnInfos(new ArrayList<ColumnInfo>(allColumnList));
}
use of com.cubrid.jdbc.proxy.driver.CUBRIDResultSetMetaDataProxy in project cubrid-manager by CUBRID.
the class AnalyseSqlTask method execute.
/**
* Execute the tasks
*/
public void execute() {
try {
if (errorMsg != null && errorMsg.trim().length() > 0) {
return;
}
if (connection == null || connection.isClosed()) {
errorMsg = Messages.error_getConnection;
return;
}
connection.setAutoCommit(false);
if (sqls == null) {
return;
}
StringBuffer sb = new StringBuffer();
for (int i = 0; i < sqls.size(); i++) {
sb.append(sqls.get(i));
if (i != sqls.size() - 1) {
sb.append(" UNION ALL ");
}
}
stmt = connection.createStatement();
rs = stmt.executeQuery(sb.toString());
CUBRIDResultSetMetaDataProxy resultSetMeta = (CUBRIDResultSetMetaDataProxy) rs.getMetaData();
for (int i = 1; i < resultSetMeta.getColumnCount() + 1; i++) {
// "Name", "Data type", "Shared", "Default"
Map<String, String> map = new HashMap<String, String>();
String type = resultSetMeta.getColumnTypeName(i);
int presion = resultSetMeta.getPrecision(i);
int scale = resultSetMeta.getScale(i);
if (type != null && type.equalsIgnoreCase("CLASS")) {
String tableName = resultSetMeta.getTableName(i);
String colName = resultSetMeta.getColumnName(i);
DBAttribute bean = getColAttr(tableName, colName);
if (bean == null || bean.getDomainClassName() == null || bean.getDomainClassName().equals("")) {
type = "OBJECT";
} else {
type = bean.getDomainClassName();
}
} else if (type != null && (type.equalsIgnoreCase("CHAR") || type.equalsIgnoreCase("VARCHAR") || type.equalsIgnoreCase("NCHAR") || type.equalsIgnoreCase("BIT") || type.toUpperCase(Locale.getDefault()).indexOf("VARYING") >= 0)) {
type += "(" + presion + ")";
} else if (type != null && type.equalsIgnoreCase("NUMERIC")) {
type += "(" + presion + "," + scale + ")";
}
map.put("0", resultSetMeta.getColumnName(i));
map.put("1", type);
map.put("2", "");
map.put("3", "");
map.put("4", "");
result.add(map);
}
} catch (SQLException e) {
this.errorMsg = e.getMessage();
} finally {
finish();
}
}
use of com.cubrid.jdbc.proxy.driver.CUBRIDResultSetMetaDataProxy in project cubrid-manager by CUBRID.
the class ExportTableDataTask method exportSql.
/**
* Export data as SQL
*
* @param rs CUBRIDResultSetProxy
* @param tableName String
* @param monitor IProgressMonitor
* @throws NumberFormatException The exception
* @throws ParseException The exception
* @throws SQLException The exception
* @throws IOException The exception
*/
private void exportSql(CUBRIDResultSetProxy rs, String tableName, final IProgressMonitor monitor) throws NumberFormatException, ParseException, SQLException, IOException {
// FIXME move this logic to core module
BufferedWriter fs = null;
try {
fs = getBufferedWriter();
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 == 0) {
fs.flush();
}
monitor.worked(10);
monitor.subTask(Messages.bind(Messages.msgExportDataRow, exportedCount));
}
} finally {
FileUtil.close(fs);
}
}
Aggregations