Search in sources :

Example 36 with QueryEditorPart

use of com.cubrid.common.ui.query.editor.QueryEditorPart 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 37 with QueryEditorPart

use of com.cubrid.common.ui.query.editor.QueryEditorPart in project cubrid-manager by CUBRID.

the class InformationWindow method updateLocation.

/**
	 * Update the location
	 */
public void updateLocation() {
    int x = 0, y = 0;
    IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
    if (window == null || window.getActivePage() == null) {
        return;
    }
    Shell windowShell = this.getShell();
    if (windowShell == null) {
        InfoWindowManager.dispose();
        return;
    }
    IEditorPart editPart = window.getActivePage().getActiveEditor();
    if (editPart != null && editPart instanceof QueryEditorPart) {
        QueryEditorPart queryEditorPart = (QueryEditorPart) editPart;
        CombinedQueryEditorComposite combinedComposite = queryEditorPart.getCombinedQueryComposite();
        if (combinedComposite != null && !combinedComposite.isDisposed()) {
            Rectangle compositeBounds = combinedComposite.getBounds();
            if (compositeBounds.width < width + horizonalMargin || compositeBounds.height < height + verticalMargin) {
                windowShell.setVisible(false);
                return;
            }
            Point size = infoText.computeSize(SWT.DEFAULT, SWT.DEFAULT);
            width = size.x;
            height = size.y;
            Point endLocation = Display.getCurrent().map(combinedComposite, null, new Point(compositeBounds.width, compositeBounds.height));
            x = endLocation.x - width - horizonalMargin;
            y = endLocation.y - height - verticalMargin;
            windowShell.setBounds(x, y, width, height);
            if (StringUtil.isEmpty(infoText.getText())) {
                windowShell.setVisible(false);
            } else {
                windowShell.setVisible(true);
            }
        }
    }
}
Also used : IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) Shell(org.eclipse.swt.widgets.Shell) QueryEditorPart(com.cubrid.common.ui.query.editor.QueryEditorPart) CombinedQueryEditorComposite(com.cubrid.common.ui.query.control.CombinedQueryEditorComposite) Rectangle(org.eclipse.swt.graphics.Rectangle) IEditorPart(org.eclipse.ui.IEditorPart) Point(org.eclipse.swt.graphics.Point) Point(org.eclipse.swt.graphics.Point)

Example 38 with QueryEditorPart

use of com.cubrid.common.ui.query.editor.QueryEditorPart 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)

Example 39 with QueryEditorPart

use of com.cubrid.common.ui.query.editor.QueryEditorPart in project cubrid-manager by CUBRID.

the class FavoriteQueryNavigatorView method saveFile.

/**
	 * Save query editor's sql into locally managed file.
	 */
private void saveFile() {
    IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
    if (window == null || window.getActivePage() == null) {
        CommonUITool.openErrorBox(Messages.errCanNotFindQueryEditor);
        return;
    }
    IEditorPart editor = window.getActivePage().getActiveEditor();
    if (editor == null || !(editor instanceof QueryEditorPart)) {
        CommonUITool.openErrorBox(Messages.errCanNotFindQueryEditor);
        return;
    }
    QueryEditorPart queryEditor = (QueryEditorPart) editor;
    String query = queryEditor.getCurrentQuery();
    addFavoriteQuery(query);
}
Also used : IWorkbenchWindow(org.eclipse.ui.IWorkbenchWindow) QueryEditorPart(com.cubrid.common.ui.query.editor.QueryEditorPart) IEditorPart(org.eclipse.ui.IEditorPart)

Example 40 with QueryEditorPart

use of com.cubrid.common.ui.query.editor.QueryEditorPart in project cubrid-manager by CUBRID.

the class FavoriteQueryNavigatorView method pasteToEditor.

private void pasteToEditor(String filename, String filepath, String charset, boolean isOpenFileMode) {
    IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
    if (window == null || window.getActivePage() == null) {
        return;
    }
    IEditorPart editor = window.getActivePage().getActiveEditor();
    try {
        if (editor == null || !(editor instanceof QueryEditorPart)) {
            editor = window.getActivePage().openEditor(new QueryUnit(), QueryEditorPart.ID);
        }
    } catch (PartInitException e) {
        editor = null;
    }
    if (editor == null) {
        return;
    }
    String fullpath = filepath + File.separator + filename;
    if (StringUtil.isEmpty(charset)) {
        charset = StringUtil.getDefaultCharset();
    }
    try {
        QueryEditorPart queryEditor = (QueryEditorPart) editor;
        StringBuilder sb = new StringBuilder();
        if (!isOpenFileMode) {
            sb.append("/* SQL Filename: ").append(filename).append(" */").append(StringUtil.NEWLINE);
        }
        BufferedReader in = null;
        try {
            File file = new File(fullpath);
            in = new BufferedReader(new InputStreamReader(new FileInputStream(file), charset));
            String line = in.readLine();
            while (line != null) {
                sb.append(line + StringUtil.NEWLINE);
                line = in.readLine();
            }
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (IOException e) {
            }
        }
        if (!StringUtil.isEmpty(queryEditor.getCurrentQuery())) {
            queryEditor.addEditorTab();
        }
        if (isOpenFileMode) {
            queryEditor.getCombinedQueryComposite().getSqlEditorComp().open(fullpath, charset);
        } else {
            queryEditor.setQuery(sb.toString(), false, false, false);
        }
        queryEditor.setFocus();
    } catch (IOException e) {
        CommonUITool.openErrorBox(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) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) IEditorPart(org.eclipse.ui.IEditorPart) PartInitException(org.eclipse.ui.PartInitException) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Aggregations

QueryEditorPart (com.cubrid.common.ui.query.editor.QueryEditorPart)51 IEditorPart (org.eclipse.ui.IEditorPart)41 IWorkbenchWindow (org.eclipse.ui.IWorkbenchWindow)39 QueryUnit (com.cubrid.common.ui.query.editor.QueryUnit)21 PartInitException (org.eclipse.ui.PartInitException)19 CubridDatabase (com.cubrid.common.ui.spi.model.CubridDatabase)13 IWorkbenchPage (org.eclipse.ui.IWorkbenchPage)8 ArrayList (java.util.ArrayList)7 DatabaseInfo (com.cubrid.cubridmanager.core.cubrid.database.model.DatabaseInfo)6 ISchemaNode (com.cubrid.common.ui.spi.model.ISchemaNode)5 List (java.util.List)5 StyledText (org.eclipse.swt.custom.StyledText)5 ShardIdSelectionDialog (com.cubrid.common.ui.common.dialog.ShardIdSelectionDialog)4 CombinedQueryEditorComposite (com.cubrid.common.ui.query.control.CombinedQueryEditorComposite)4 IOException (java.io.IOException)4 IEditorReference (org.eclipse.ui.IEditorReference)4 CubridServer (com.cubrid.common.ui.spi.model.CubridServer)3 DefaultSchemaNode (com.cubrid.common.ui.spi.model.DefaultSchemaNode)3 ICubridNode (com.cubrid.common.ui.spi.model.ICubridNode)3 Control (org.eclipse.swt.widgets.Control)3