Search in sources :

Example 11 with QueryUnit

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);
    }
}
Also used : IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) QueryEditorPart(com.cubrid.common.ui.query.editor.QueryEditorPart) QueryUnit(com.cubrid.common.ui.query.editor.QueryUnit) ISchemaNode(com.cubrid.common.ui.spi.model.ISchemaNode) IStructuredSelection(org.eclipse.jface.viewers.IStructuredSelection) ICubridNode(com.cubrid.common.ui.spi.model.ICubridNode) IEditorPart(org.eclipse.ui.IEditorPart) DefaultSchemaNode(com.cubrid.common.ui.spi.model.DefaultSchemaNode) PartInitException(org.eclipse.ui.PartInitException)

Example 12 with QueryUnit

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();
        }
    }
}
Also used : IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) QueryEditorPart(com.cubrid.common.ui.query.editor.QueryEditorPart) QueryUnit(com.cubrid.common.ui.query.editor.QueryUnit) LoginQueryEditorDialog(com.cubrid.cubridquery.ui.common.dialog.LoginQueryEditorDialog) DatabaseInfo(com.cubrid.cubridmanager.core.cubrid.database.model.DatabaseInfo) ShardIdSelectionDialog(com.cubrid.common.ui.common.dialog.ShardIdSelectionDialog) IEditorPart(org.eclipse.ui.IEditorPart) CubridDatabase(com.cubrid.common.ui.spi.model.CubridDatabase)

Example 13 with QueryUnit

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);
    }
}
Also used : IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) QueryEditorPart(com.cubrid.common.ui.query.editor.QueryEditorPart) QueryUnit(com.cubrid.common.ui.query.editor.QueryUnit) LoginQueryEditorDialog(com.cubrid.cubridquery.ui.common.dialog.LoginQueryEditorDialog) DatabaseInfo(com.cubrid.cubridmanager.core.cubrid.database.model.DatabaseInfo) ShardIdSelectionDialog(com.cubrid.common.ui.common.dialog.ShardIdSelectionDialog) IEditorPart(org.eclipse.ui.IEditorPart) CubridDatabase(com.cubrid.common.ui.spi.model.CubridDatabase)

Example 14 with QueryUnit

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());
    }
}
Also used : IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) QueryEditorPart(com.cubrid.common.ui.query.editor.QueryEditorPart) QueryUnit(com.cubrid.common.ui.query.editor.QueryUnit) ISchemaNode(com.cubrid.common.ui.spi.model.ISchemaNode) PartInitException(org.eclipse.ui.PartInitException)

Example 15 with QueryUnit

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);
            }
        }
    }
}
Also used : IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) XLSXImportFileHandler(com.cubrid.common.ui.cubrid.table.importhandler.handler.XLSXImportFileHandler) XLSImportFileHandler(com.cubrid.common.ui.cubrid.table.importhandler.handler.XLSImportFileHandler) ImportFileHandler(com.cubrid.common.ui.cubrid.table.importhandler.ImportFileHandler) XLSXImportFileHandler(com.cubrid.common.ui.cubrid.table.importhandler.handler.XLSXImportFileHandler) ArrayList(java.util.ArrayList) IEditorPart(org.eclipse.ui.IEditorPart) PartInitException(org.eclipse.ui.PartInitException) InvocationTargetException(java.lang.reflect.InvocationTargetException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) QueryEditorPart(com.cubrid.common.ui.query.editor.QueryEditorPart) QueryUnit(com.cubrid.common.ui.query.editor.QueryUnit) List(java.util.List) ArrayList(java.util.ArrayList) XLSImportFileHandler(com.cubrid.common.ui.cubrid.table.importhandler.handler.XLSImportFileHandler) PartInitException(org.eclipse.ui.PartInitException)

Aggregations

QueryUnit (com.cubrid.common.ui.query.editor.QueryUnit)22 QueryEditorPart (com.cubrid.common.ui.query.editor.QueryEditorPart)21 IWorkbenchWindow (org.eclipse.ui.IWorkbenchWindow)19 IEditorPart (org.eclipse.ui.IEditorPart)18 PartInitException (org.eclipse.ui.PartInitException)17 CubridDatabase (com.cubrid.common.ui.spi.model.CubridDatabase)7 ISchemaNode (com.cubrid.common.ui.spi.model.ISchemaNode)5 ShardIdSelectionDialog (com.cubrid.common.ui.common.dialog.ShardIdSelectionDialog)4 DatabaseInfo (com.cubrid.cubridmanager.core.cubrid.database.model.DatabaseInfo)4 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)3 IEditorInput (org.eclipse.ui.IEditorInput)3 IWorkbenchPage (org.eclipse.ui.IWorkbenchPage)3 SetFileEncodingDialog (com.cubrid.common.ui.query.dialog.SetFileEncodingDialog)2 DefaultSchemaNode (com.cubrid.common.ui.spi.model.DefaultSchemaNode)2 ICubridNode (com.cubrid.common.ui.spi.model.ICubridNode)2 LoginQueryEditDialog (com.cubrid.cubridmanager.ui.common.dialog.LoginQueryEditDialog)2 LoginQueryEditorDialog (com.cubrid.cubridquery.ui.common.dialog.LoginQueryEditorDialog)2 BufferedReader (java.io.BufferedReader)2 File (java.io.File)2