use of com.cubrid.jdbc.proxy.driver.CUBRIDPreparedStatementProxy 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.CUBRIDPreparedStatementProxy 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.CUBRIDPreparedStatementProxy 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.CUBRIDPreparedStatementProxy in project cubrid-manager by CUBRID.
the class GetInfoDataTask method initDemoDataTable.
/**
* Initial demo data table
*
* @throws SQLException
*/
private void initDemoDataTable() throws SQLException {
String sql = getStmtSQL();
stmt = null;
rs = null;
try {
stmt = getStatement(connection, sql, doesGetOidInfo, false);
CUBRIDPreparedStatementProxy cubridStmt = (CUBRIDPreparedStatementProxy) stmt;
cubridStmt.setQueryInfo(false);
cubridStmt.setOnlyQueryPlan(false);
cubridStmt.executeQuery();
rs = (CUBRIDResultSetProxy) stmt.getResultSet();
makeTableData((CUBRIDResultSetProxy) rs);
} catch (SQLException event) {
errorMsg += Messages.runError + event.getErrorCode() + StringUtil.NEWLINE + Messages.errorHead + event.getMessage() + StringUtil.NEWLINE;
throw event;
} finally {
QueryUtil.freeQuery(connection, stmt, rs);
rs = null;
stmt = null;
connection = null;
}
Display.getDefault().syncExec(new Runnable() {
public void run() {
diaplayDemoDataTable();
}
});
}
use of com.cubrid.jdbc.proxy.driver.CUBRIDPreparedStatementProxy 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