use of com.cubrid.cubridmanager.core.common.jdbc.DBConnection in project cubrid-manager by CUBRID.
the class MultiQueryThread method run.
/**
* Execute query
*/
public void run() {
// FIXME move this logic to core module
DBConnection connection = new DBConnection(database.getDatabaseInfo());
try {
connection.checkAndConnect();
connection.setAutoClosable(true);
} catch (final SQLException event) {
Display.getDefault().syncExec(new Runnable() {
public void run() {
String errorMsg = Messages.errDbConnect;
if (event.getMessage() != null) {
errorMsg = Messages.bind(com.cubrid.common.ui.common.Messages.errCommonTip, event.getErrorCode(), event.getMessage());
}
LOGGER.error(errorMsg);
CommonUITool.openErrorBox(Display.getDefault().getActiveShell(), errorMsg);
CTabFolder queryResultTabFolder = queryResultComp.getQueryResultTabFolder();
queryResultComp.disposeAllResult();
if (event.getMessage().length() <= 0) {
queryResultComp.makeEmptyResult();
} else {
if (event.getMessage().length() > 0) {
queryResultComp.makeLogResult(queries, event.getMessage());
}
}
if (queryResultTabFolder.getItemCount() > 0) {
queryResultTabFolder.setSelection(queryResultTabFolder.getItemCount() - 1);
}
queryResultComp.setCanDispose(true);
}
});
return;
}
final Vector<String> qVector = QueryUtil.queriesToQuery(queries);
int currentQueryIndex = 0;
int cntResults = 0;
String noSelectSql = "";
StringBuilder log = new StringBuilder(256);
@SuppressWarnings("unused") boolean hasModifyQuery = false;
@SuppressWarnings("unused") boolean isIsolationHigher = false;
long beginTimestamp = 0;
double elapsedTime = 0.0;
NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(3);
result = null;
String multiQuerySql = null;
final Vector<QueryExecuter> curResult = new Vector<QueryExecuter>();
if (database == null) {
database = ((QueryUnit) queryEditor.getEditorInput()).getDatabase();
}
try {
if (qVector.isEmpty()) {
return;
} else {
isIsolationHigher = queryEditor.isIsolationHigherThanRepeatableRead(connection.getConnection(), queryEditor.isActive());
}
ServerInfo serverInfo = null;
if (database != null) {
serverInfo = this.database.getServer().getServerInfo();
}
RecentlyUsedSQLDetailPersistUtils.load(database);
boolean enableSearchUnit = QueryOptions.getEnableSearchUnit(serverInfo);
int unitCount = QueryOptions.getSearchUnitCount(serverInfo);
int sqlTotalCount = qVector.size();
for (int i = 0; i < sqlTotalCount; i++) {
currentQueryIndex = i;
log.delete(0, log.length());
long endTimestamp = 0;
SQLHistoryDetail sqlHistoryDetail = new SQLHistoryDetail();
sqlHistoryDetail.setExecuteTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date()));
sql = qVector.get(i).toString();
if (database.getDatabaseInfo().isShard()) {
if (queryEditor.getDefaultShardQueryType() == DatabaseInfo.SHARD_QUERY_TYPE_ID) {
sql = QueryUtil.wrapShardQueryWithId(sql, queryEditor.getShardId());
} else {
sql = QueryUtil.wrapShardQueryWithVal(sql, queryEditor.getShardVal());
}
}
if (sql != null && sql.trim().lastIndexOf(";") == -1) {
sql += ";";
}
String orignSQL = sql;
if (enableSearchUnit && unitCount > 0) {
multiQuerySql = SqlParser.getPaginatingSqlClause(sql);
}
String order = StringUtil.getOrdinalFromCardinalNumber(i + 1);
if (multiQuerySql == null) {
sql = SqlParser.convertComment(sql);
beginTimestamp = System.currentTimeMillis();
try {
stmt = QueryExecuter.getStatement(connection.getConnection(), sql, false, false);
} catch (final SQLException e) {
throw e;
}
if (stmt.hasResultSet()) {
stmt.setQueryInfo(false);
stmt.setOnlyQueryPlan(false);
try {
stmt.executeQuery();
endTimestamp = System.currentTimeMillis();
rs = (CUBRIDResultSetProxy) stmt.getResultSet();
} catch (final SQLException e) {
throw e;
}
elapsedTime = (endTimestamp - beginTimestamp) * 0.001;
String elapsedTimeStr = nf.format(elapsedTime);
if (elapsedTime < 0.001) {
elapsedTimeStr = "0.000";
}
List<String> columnTableNameList = UIQueryUtil.loadColumnTableNameList(stmt);
result = createQueryExecutor(queryEditor, cntResults, sql, database, connection, orignSQL, columnTableNameList);
result.makeResult(rs);
String queryMsg = Messages.bind(Messages.querySeq, order) + "[ " + elapsedTimeStr + " " + Messages.second + " , " + Messages.totalRows + " : " + result.cntRecord + " ]" + StringUtil.NEWLINE;
result.setQueryMsg(queryMsg);
sqlHistoryDetail.setExecuteInfo(queryMsg);
sqlHistoryDetail.setElapseTime(elapsedTimeStr);
if (stmt.getStatementType() == CUBRIDCommandType.CUBRID_STMT_EVALUATE || stmt.getStatementType() == CUBRIDCommandType.CUBRID_STMT_CALL) {
hasModifyQuery = true;
}
curResult.addElement(result);
queryExecuterMap.put(sql, result);
cntResults++;
} else {
byte execType = stmt.getStatementType();
/*
* the previous version , the variable
* threadExecResult is class field, but why ? is it
* necessary?
*/
int threadExecResult = 0;
try {
threadExecResult = stmt.executeUpdate();
endTimestamp = System.currentTimeMillis();
} catch (final SQLException ee) {
throw ee;
}
elapsedTime = (endTimestamp - beginTimestamp) * 0.001;
int cntModify = threadExecResult;
noSelectSql += sql + StringUtil.NEWLINE;
hasModifyQuery = true;
log.append(Messages.bind(Messages.querySeq, order)).append(" ");
switch(execType) {
case CUBRIDCommandType.CUBRID_STMT_ALTER_CLASS:
case CUBRIDCommandType.CUBRID_STMT_ALTER_SERIAL:
case CUBRIDCommandType.CUBRID_STMT_RENAME_CLASS:
case CUBRIDCommandType.CUBRID_STMT_RENAME_TRIGGER:
log.append(Messages.alterOk);
break;
case CUBRIDCommandType.CUBRID_STMT_CREATE_CLASS:
case CUBRIDCommandType.CUBRID_STMT_CREATE_INDEX:
case CUBRIDCommandType.CUBRID_STMT_CREATE_TRIGGER:
case CUBRIDCommandType.CUBRID_STMT_CREATE_SERIAL:
log.append(Messages.createOk);
break;
case CUBRIDCommandType.CUBRID_STMT_DROP_DATABASE:
case CUBRIDCommandType.CUBRID_STMT_DROP_CLASS:
case CUBRIDCommandType.CUBRID_STMT_DROP_INDEX:
case CUBRIDCommandType.CUBRID_STMT_DROP_LABEL:
case CUBRIDCommandType.CUBRID_STMT_DROP_TRIGGER:
case CUBRIDCommandType.CUBRID_STMT_DROP_SERIAL:
case CUBRIDCommandType.CUBRID_STMT_REMOVE_TRIGGER:
log.append(Messages.dropOk);
break;
case CUBRIDCommandType.CUBRID_STMT_INSERT:
log.append(Messages.bind(Messages.insertOk, cntModify));
break;
case CUBRIDCommandType.CUBRID_STMT_SELECT:
break;
case CUBRIDCommandType.CUBRID_STMT_UPDATE:
log.append(Messages.bind(Messages.updateOk2, cntModify));
break;
case CUBRIDCommandType.CUBRID_STMT_DELETE:
log.append(Messages.bind(Messages.deleteOk, cntModify));
break;
/*
* Under two line works disable button when query's
* last command is commit/rollback
*/
case CUBRIDCommandType.CUBRID_STMT_COMMIT_WORK:
case CUBRIDCommandType.CUBRID_STMT_ROLLBACK_WORK:
hasModifyQuery = false;
default:
log.append(Messages.queryOk);
break;
}
String elapsedTimeStr = nf.format(elapsedTime);
if (elapsedTime < 0.001) {
elapsedTimeStr = "0.000";
}
log.append(" [").append(elapsedTimeStr).append(" ");
log.append(Messages.second).append("]").append(StringUtil.NEWLINE);
logs.append(log);
logs.append(QueryEditorPart.makeSqlLogOnResult(sql));
sqlHistoryDetail.setExecuteInfo(log.toString());
sqlHistoryDetail.setElapseTime(elapsedTimeStr);
}
} else {
result = createQueryExecutor(queryEditor, cntResults, "", database, connection, orignSQL, null);
result.setMultiQuerySql(multiQuerySql);
result.setQueryMsg(Messages.bind(Messages.querySeq, order) + StringUtil.NEWLINE);
result.setSqlDetailHistory(sqlHistoryDetail);
queryExecuterMap.put(sql, result);
try {
result.makeTable(1, false);
} catch (final SQLException ee) {
throw ee;
}
curResult.addElement(result);
cntResults++;
}
QueryUtil.freeQuery(stmt, rs);
stmt = null;
rs = null;
// SQL execution log
sqlHistoryDetail.setSql(sql);
RecentlyUsedSQLDetailPersistUtils.addLog(database, sqlHistoryDetail);
}
// if (editor.isAutocommit()) {
// editor.queryAction(QUERY_ACTION.COMMIT);
// }
} catch (final SQLException event) {
LOGGER.error(event.getMessage(), event);
// }
if (multiQuerySql == null || result == null) {
final String errorSql = (String) qVector.get(currentQueryIndex);
noSelectSql += errorSql;
logs.append(QueryEditorPart.makeSqlErrorOnResult(currentQueryIndex, errorSql, event));
} else {
noSelectSql += result.getQuerySql();
logs.append(result.getQueryMsg());
}
logsMap.put(sql, logs);
} catch (final Exception event) {
LOGGER.error(event.getMessage(), event);
if (multiQuerySql == null || result == null) {
final String errorSql = (String) qVector.get(currentQueryIndex);
noSelectSql += errorSql;
logs.append(QueryEditorPart.makeSqlErrorOnResult(currentQueryIndex, errorSql, event));
} else {
noSelectSql += result.getQuerySql();
logs.append(result.getQueryMsg());
}
logsMap.put(sql, logs);
} finally {
RecentlyUsedSQLDetailPersistUtils.save(database);
QueryUtil.freeQuery(stmt, rs);
stmt = null;
rs = null;
if (connection != null && connection.isAutoClosable()) {
connection.close();
}
}
}
use of com.cubrid.cubridmanager.core.common.jdbc.DBConnection in project cubrid-manager by CUBRID.
the class MultiSQLQueryResultComposite method dispose.
public void dispose() {
for (DBConnection con : connectionList) {
if (con != null) {
con.close();
}
}
for (Map.Entry<String, QueryResultComposite> entry : sqlResultMap.entrySet()) {
QueryResultComposite queryResultComp = entry.getValue();
queryResultComp.disposeAllResult();
queryResultComp.disposeTabResult();
queryResultComp.dispose();
}
super.dispose();
}
use of com.cubrid.cubridmanager.core.common.jdbc.DBConnection in project cubrid-manager by CUBRID.
the class QueryEditorPart method runQuery.
/**
* Execute all the selected SQL script on editor, if not, execute all the
* script on editor
*
* @param isOnlyQueryPlan boolean
* @parma isSqlmapQuery boolean whether or not it should handled as a sqlmap query
*/
private void runQuery(boolean isOnlyQueryPlan, boolean isSqlmapQuery) {
final DBConnection dbConnection = getConnection();
if (dbConnection != null && dbConnection.isAutoCommit() && dbConnection.getConnectionQuietly() != null && dbConnection.isExpiredConnection() && !dbConnection.testConnectionAlived()) {
if (!CommonUITool.openConfirmBox(Messages.msgConnectionTimeOut)) {
return;
}
dbConnection.close();
try {
dbConnection.checkAndConnect();
} catch (SQLException e) {
LOGGER.error(e.getMessage(), e);
String msg = Messages.bind(Messages.msgConnectionError, e.getMessage());
CommonUITool.openErrorBox(msg);
}
}
if (!isOnlyQueryPlan && isAvailableQueryResult()) {
combinedQueryComposite.getQueryResultComp().setSelection();
} else if (isOnlyQueryPlan && isAvailablePlanResult()) {
combinedQueryComposite.getQueryPlanResultComp().setSelection();
}
if (combinedQueryComposite.getSqlEditorComp().isDisposed()) {
return;
}
String queries = combinedQueryComposite.getSqlEditorComp().getSelectedQueries();
runQuery(isOnlyQueryPlan, isSqlmapQuery, queries, null);
}
Aggregations