use of com.cubrid.jdbc.proxy.driver.CUBRIDResultSetProxy in project cubrid-manager by CUBRID.
the class DataCompareEditorPart method fetchHashedCompareData.
private List<HashedCompareData> fetchHashedCompareData(Connection conn, SchemaInfo schemaInfo, long start, int rows) {
// FIXME logic code move to core module
Statement stmt = null;
ResultSet rs = null;
StringBuilder pkColumns = new StringBuilder();
Constraint constraint = schemaInfo.getPK();
if (constraint != null) {
for (String column : constraint.getAttributes()) {
if (pkColumns.length() > 0) {
pkColumns.append(",");
}
pkColumns.append(QuerySyntax.escapeKeyword(column));
}
}
StringBuilder extraColumns = new StringBuilder();
StringBuilder sql = new StringBuilder();
sql.append(" SELECT ").append(pkColumns).append(", ");
sql.append(" MD5(CONCAT(");
StringBuilder cols = new StringBuilder();
List<DBAttribute> attrs = schemaInfo.getAttributes();
// TODO Collections.sort
for (int i = 0, len = attrs.size(); i < len; i++) {
DBAttribute attr = attrs.get(i);
if (cols.length() > 0) {
cols.append(",");
}
makeColumnsClause(extraColumns, cols, attr);
}
sql.append(cols);
sql.append(")) AS _record_hash_ ");
if (extraColumns.length() > 0) {
sql.append(",");
sql.append(extraColumns);
}
String escapedTableName = QuerySyntax.escapeKeyword(schemaInfo.getClassname());
sql.append(" FROM ").append(escapedTableName);
sql.append(" ORDER BY ").append(pkColumns);
sql.append(" FOR ORDERBY_NUM() BETWEEN ").append(start + 1);
sql.append(" AND ").append(start + rows);
List<HashedCompareData> result = new ArrayList<HashedCompareData>();
try {
StringBuilder extra = new StringBuilder();
stmt = conn.createStatement();
rs = stmt.executeQuery(sql.toString());
ResultSetMetaData meta = rs.getMetaData();
while (rs.next()) {
String hash = null;
boolean isPk = true;
extra.delete(0, extra.length());
List<String> pkColumnList = new ArrayList<String>();
List<String> pkValueList = new ArrayList<String>();
List<String> pkTypeList = new ArrayList<String>();
for (int j = 1, len = meta.getColumnCount(); j <= len; j++) {
String columnName = meta.getColumnName(j);
String columnType = DataType.isNumberType(meta.getColumnTypeName(j)) ? HashedCompareData.NUMBER_TYPE : HashedCompareData.STRING_TYPE;
if (columnName.equalsIgnoreCase("_record_hash_")) {
isPk = false;
hash = rs.getString(j);
} else if (isPk) {
pkColumnList.add(columnName);
pkValueList.add(rs.getString(j));
pkTypeList.add(columnType);
} else {
String val = null;
if (DataType.isSetDataType(meta.getColumnTypeName(j))) {
String colType = FieldHandlerUtils.amendDataTypeByResult(rs, j, meta.getColumnTypeName(j));
val = (String) FieldHandlerUtils.getRsValueForExport(colType, (CUBRIDResultSetProxy) rs, j, "{NULL}");
} else {
val = rs.getString(j);
}
if (val == null) {
val = "{NULL}";
}
extra.append(val);
}
}
HashedCompareData data = new HashedCompareData(pkColumnList, pkValueList, pkTypeList, hash);
result.add(data);
}
} catch (Exception e) {
printToConsoleOnWorkThread(StringUtil.NEWLINE, false);
printToConsoleOnWorkThread(e.getMessage(), true);
LOGGER.error(sql.toString(), e);
} finally {
QueryUtil.freeQuery(stmt, rs);
}
return result;
}
use of com.cubrid.jdbc.proxy.driver.CUBRIDResultSetProxy 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.CUBRIDResultSetProxy 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.CUBRIDResultSetProxy in project cubrid-manager by CUBRID.
the class QueryExecuter method reloadQuery.
public void reloadQuery() {
CUBRIDPreparedStatementProxy pstmt = null;
CUBRIDResultSetProxy prs = null;
try {
pstmt = QueryExecuter.getStatement(connection.checkAndConnectQuietly(), query, false, false);
if (parameterList != null) {
for (PstmtParameter pstmtParameter : parameterList) {
FieldHandlerUtils.setPreparedStatementValue(pstmtParameter, pstmt, charset);
}
}
if (pstmt.hasResultSet()) {
pstmt.setQueryInfo(false);
pstmt.setOnlyQueryPlan(false);
pstmt.executeQuery();
prs = (CUBRIDResultSetProxy) pstmt.getResultSet();
if (allColumnList != null) {
allColumnList.clear();
}
if (allDataList != null) {
allDataList.clear();
}
int page = queryInfo.getCurrentPage();
makeResult(prs);
queryInfo.setCurrentPage(page);
makeItem();
updateActions();
}
} catch (final Exception ee) {
LOGGER.error("execute SQL failed sql at query editor : " + query + " error message: " + ee);
ee.printStackTrace();
} finally {
QueryUtil.freeQuery(pstmt, prs);
pstmt = null;
prs = null;
}
}
use of com.cubrid.jdbc.proxy.driver.CUBRIDResultSetProxy 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);
}
}
Aggregations