use of com.cubrid.common.ui.query.editor.QueryUnit in project cubrid-manager by CUBRID.
the class CubridWorkbenchContrItem method openSelectQuery.
public void openSelectQuery(ISelection selection) {
final Object obj = ((IStructuredSelection) selection).getFirstElement();
if (!(obj instanceof ICubridNode)) {
return;
}
ISchemaNode table = (ISchemaNode) obj;
IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
if (window == null) {
return;
}
boolean existsEditor = false;
QueryEditorPart editor = null;
QueryUnit input = new QueryUnit();
input.setDatabase(table.getDatabase());
try {
IEditorPart editorPart = window.getActivePage().getActiveEditor();
if (editorPart != null && editorPart instanceof QueryEditorPart) {
QueryEditorPart queryEditorPart = (QueryEditorPart) editorPart;
if (queryEditorPart.getSelectedDatabase() == input.getDatabase()) {
editor = (QueryEditorPart) editorPart;
existsEditor = true;
}
}
if (editor == null) {
editor = (QueryEditorPart) window.getActivePage().openEditor(input, QueryEditorPart.ID);
editor.connect(table.getDatabase());
}
DefaultSchemaNode tableNode = (DefaultSchemaNode) obj;
String sql = getStmtSQL(tableNode) + StringUtil.NEWLINE + StringUtil.NEWLINE;
if (existsEditor) {
editor.newQueryTab(sql, true);
} else {
editor.setQuery(sql, false, true, false);
}
} catch (Exception e) {
LOGGER.error("", e);
}
}
use of com.cubrid.common.ui.query.editor.QueryUnit in project cubrid-manager by CUBRID.
the class QueryNewAction method openQueryEditor.
/**
* Open new query editor.
*
* @param database of query editor.
* @throws PartInitException when open editor error.
*/
private void openQueryEditor(CubridDatabase database) throws PartInitException {
IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
if (window == null || window.getActivePage() == null) {
return;
}
if (database == null) {
window.getActivePage().openEditor(new QueryUnit(), QueryEditorPart.ID);
return;
}
CubridDatabase cdb = database;
if (database == null || !database.isLogined()) {
LoginQueryEditorDialog dialog = new LoginQueryEditorDialog(getShell());
dialog.setSelectedConnName(getInitConnectionName(database));
if (dialog.open() != IDialogConstants.OK_ID) {
return;
}
cdb = DatabaseNavigatorMenu.SELF_DATABASE;
}
// [TOOLS-2425]Support shard broker
boolean isShrd = false;
int shardId = 0;
int shardVal = 0;
int shardQueryType = DatabaseInfo.SHARD_QUERY_TYPE_ID;
if (cdb != null) {
DatabaseInfo dbInfo = cdb.getDatabaseInfo();
if (dbInfo != null && dbInfo.isShard()) {
isShrd = true;
ShardIdSelectionDialog dialog = new ShardIdSelectionDialog(getShell());
dialog.setDatabaseInfo(dbInfo);
dialog.setShardId(0);
dialog.setShardVal(0);
dialog.setShardQueryType(shardQueryType);
if (dialog.open() == IDialogConstants.OK_ID) {
shardId = dialog.getShardId();
shardVal = dialog.getShardVal();
shardQueryType = dialog.getShardQueryType();
}
}
}
QueryUnit input = new QueryUnit();
input.setDatabase(cdb);
IEditorPart editor = window.getActivePage().openEditor(input, QueryEditorPart.ID);
if (editor != null) {
QueryEditorPart editorPart = (QueryEditorPart) editor;
editorPart.connect(cdb);
// [TOOLS-2425]Support shard broker
if (isShrd) {
editorPart.setShardId(shardId);
editorPart.setShardVal(shardVal);
editorPart.setShardQueryType(shardQueryType);
editorPart.changeQueryEditorPartNameWithShard();
}
}
}
use of com.cubrid.common.ui.query.editor.QueryUnit in project cubrid-manager by CUBRID.
the class QueryNewCustomAction method openQueryEditor.
/**
* Open new query editor.
*
* @param database of query editor.
* @throws PartInitException when open editor error.
*/
private void openQueryEditor(CubridDatabase database) throws PartInitException {
IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
if (window == null || window.getActivePage() == null) {
return;
}
CubridDatabase cdb = DatabaseNavigatorMenu.NULL_DATABASE;
LoginQueryEditorDialog loginDialog = new LoginQueryEditorDialog(getShell());
loginDialog.setSelectedConnName(getInitConnectionName(database));
if (loginDialog.open() == IDialogConstants.OK_ID) {
cdb = DatabaseNavigatorMenu.SELF_DATABASE;
} else {
return;
}
// [TOOLS-2425]Support shard broker
if (cdb != null) {
DatabaseInfo dbInfo = cdb.getDatabaseInfo();
if (dbInfo != null && dbInfo.isShard()) {
ShardIdSelectionDialog dialog = new ShardIdSelectionDialog(getShell());
dialog.setDatabaseInfo(dbInfo);
dialog.setShardId(0);
dialog.setShardVal(0);
dialog.setShardQueryType(dbInfo.getShardQueryType());
if (dialog.open() == IDialogConstants.OK_ID) {
dbInfo.setCurrentShardId(dialog.getShardId());
}
}
}
QueryUnit input = new QueryUnit();
input.setDatabase(cdb);
IEditorPart editor = window.getActivePage().openEditor(input, QueryEditorPart.ID);
if (editor != null) {
((QueryEditorPart) editor).connect(cdb);
}
}
use of com.cubrid.common.ui.query.editor.QueryUnit in project cubrid-manager by CUBRID.
the class ColumnSelectSqlAction method run.
public void run() {
Object[] objs = this.getSelectedObj();
if (!isSupported(objs)) {
setEnabled(false);
return;
}
ISchemaNode table = (ISchemaNode) ((ISchemaNode) objs[0]).getParent().getParent();
StringBuilder buf = new StringBuilder();
for (int i = 0; i < objs.length; i++) {
ISchemaNode columnNode = (ISchemaNode) objs[i];
String column = columnNode.getName().split(",")[0];
buf.append(QuerySyntax.escapeKeyword(column));
if (i != objs.length - 1) {
buf.append(',');
}
}
String cols = buf.toString();
if (StringUtil.isEmpty(cols)) {
return;
}
IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
if (window == null) {
return;
}
QueryUnit input = new QueryUnit();
input.setDatabase(table.getDatabase());
try {
QueryEditorPart editor = (QueryEditorPart) window.getActivePage().openEditor(input, QueryEditorPart.ID);
editor.connect(table.getDatabase());
// FIXME move this logic to core module
String escapedTableName = QuerySyntax.escapeKeyword(table.getName());
String sql = "SELECT " + cols + " FROM " + escapedTableName + ";";
editor.setQuery(sql, false, true, false);
} catch (PartInitException e) {
LOGGER.error(e.getMessage());
}
}
use of com.cubrid.common.ui.query.editor.QueryUnit in project cubrid-manager by CUBRID.
the class ImportDataFromFileDialog method showResultSet.
/**
*
* Show the result set by query editor
*
* @param parameterList List<PstmtParameter>
*/
private void showResultSet(List<PstmtParameter> parameterList) {
// FIXME move this logic to core module
IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
if (window == null) {
return;
}
String showedSql = sqlTxt.getText();
String executedSql = showedSql;
while (executedSql.endsWith(";")) {
executedSql = executedSql.substring(0, executedSql.length() - 1);
}
executedSql += ";";
int threadCount = threadCountSpinner.getSelection();
String fileName = fileNameTxt.getText();
String charset = fileCharsetCombo.getText();
int totalLine = Integer.parseInt(totalLinesText.getText());
int commitedLineOnce = commitLineSpinner.getSelection();
ImportFileHandler handler = ImportFileHandlerFactory.getHandler(fileName, charset);
PstmtDataTask task = new PstmtDataTask(sqlTxt.getText(), database, fileName, parameterList, 0, totalLine, commitedLineOnce, charset, firstRowAsColumnBtn.getSelection(), false, null, handler);
List<List<PstmtParameter>> rowParameterList = null;
try {
rowParameterList = task.getRowParameterList();
} catch (RuntimeException ex) {
String errorMsg = null;
if (ex.getCause() == null) {
errorMsg = ex.getMessage();
} else {
if (ex.getCause() instanceof OutOfMemoryError) {
errorMsg = Messages.errNoAvailableMemory;
} else {
errorMsg = ex.getCause().getMessage();
}
}
errorMsg = errorMsg == null || errorMsg.trim().length() == 0 ? "Unknown error." : errorMsg;
CommonUITool.openErrorBox(getShell(), errorMsg);
return;
} catch (Exception ex) {
String errorMsg = ex.getMessage();
errorMsg = errorMsg == null || errorMsg.trim().length() == 0 ? "Unknown error." : errorMsg;
CommonUITool.openErrorBox(getShell(), errorMsg);
return;
} finally {
if (importFileHandler instanceof XLSImportFileHandler) {
XLSImportFileHandler xlsHandler = (XLSImportFileHandler) importFileHandler;
xlsHandler.dispose();
} else if (importFileHandler instanceof XLSXImportFileHandler) {
XLSXImportFileHandler xlsHandler = (XLSXImportFileHandler) importFileHandler;
xlsHandler.dispose();
}
}
close();
int rows = rowParameterList == null ? 0 : rowParameterList.size();
int rowsOfThread = rows;
if (threadCount > 1) {
rowsOfThread = rows / threadCount;
}
int rowsOfLastThread = rowsOfThread + (rows % threadCount == 0 ? 0 : rows % threadCount);
int currentRow = 0;
for (int i = 0; i < threadCount; i++) {
QueryUnit editorInput = new QueryUnit();
IEditorPart editor = null;
try {
editor = window.getActivePage().openEditor(editorInput, QueryEditorPart.ID);
} catch (PartInitException e) {
editor = null;
}
if (editor != null) {
QueryEditorPart queryEditor = ((QueryEditorPart) editor);
int endRow = currentRow + rowsOfThread;
if (i == threadCount - 1) {
endRow = currentRow + rowsOfLastThread;
}
List<List<PstmtParameter>> paraList = new ArrayList<List<PstmtParameter>>();
StringBuffer showedSqlBuffer = new StringBuffer();
StringBuffer executeSqlBuffer = new StringBuffer();
for (int j = currentRow; j < endRow; j++) {
showedSqlBuffer.append(getCommentSqlValue(rowParameterList.get(j)));
paraList.add(rowParameterList.get(j));
executeSqlBuffer.append(executedSql);
}
showedSqlBuffer.append(showedSql);
currentRow = endRow;
if (!queryEditor.isConnected() && database != null) {
queryEditor.connect(database);
}
if (queryEditor.isConnected()) {
queryEditor.setQuery(showedSqlBuffer.toString(), executedSql, rowParameterList, true, true, false);
} else {
queryEditor.setQuery(showedSqlBuffer.toString(), true, false, false);
}
}
}
}
Aggregations