Search in sources :

Example 1 with SQLEditor

use of net.sourceforge.sqlexplorer.plugin.editors.SQLEditor in project tdq-studio-se by Talend.

the class GenerateSelectSQLAction method run.

/**
 * Generate select statement
 *
 * @see org.eclipse.jface.action.IAction#run()
 */
@Override
public void run() {
    try {
        String query = null;
        if (_selectedNodes[0] instanceof ColumnNode) {
            query = createColumnSelect();
        }
        if (_selectedNodes[0] instanceof TableNode) {
            query = createTableSelect();
        }
        if (query == null) {
            return;
        }
        SQLEditorInput input = new SQLEditorInput(// $NON-NLS-1$
        "SQL Editor (" + SQLExplorerPlugin.getDefault().getEditorSerialNo() + // $NON-NLS-1$
        ").sql");
        input.setUser(_selectedNodes[0].getSession().getUser());
        IWorkbenchPage page = SQLExplorerPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage();
        SQLEditor editorPart = (SQLEditor) page.openEditor(input, SQLEditor.class.getName());
        editorPart.setText(query);
    } catch (Throwable e) {
        SQLExplorerPlugin.error("Could generate sql.", e);
    }
}
Also used : SQLEditorInput(net.sourceforge.sqlexplorer.plugin.editors.SQLEditorInput) SQLEditor(net.sourceforge.sqlexplorer.plugin.editors.SQLEditor) ColumnNode(net.sourceforge.sqlexplorer.dbstructure.nodes.ColumnNode) TableNode(net.sourceforge.sqlexplorer.dbstructure.nodes.TableNode) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage)

Example 2 with SQLEditor

use of net.sourceforge.sqlexplorer.plugin.editors.SQLEditor in project tdq-studio-se by Talend.

the class AppendToEditorAction method run.

public void run() {
    try {
        TableItem[] ti = _table.getSelection();
        if (ti == null || ti.length == 0) {
            return;
        }
        String queryDelimiter = SQLExplorerPlugin.getDefault().getPluginPreferences().getString(IConstants.SQL_QRY_DELIMITER);
        StringBuffer copiedText = new StringBuffer();
        for (int i = 0; i < ti.length; i++) {
            SQLHistoryElement el = (SQLHistoryElement) ti[i].getData();
            copiedText.append(el.getRawSQLString());
            copiedText.append(queryDelimiter);
            if (ti.length > 1) {
                copiedText.append("\n");
            }
        }
        IWorkbenchPage page = SQLExplorerPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage();
        if (page == null) {
            return;
        }
        SQLEditor editorPart = (SQLEditor) page.getActiveEditor();
        editorPart.setText(editorPart.getSQLToBeExecuted() + "\n" + copiedText.toString());
    } catch (Throwable e) {
        SQLExplorerPlugin.error("Error creating sql editor", e);
    }
}
Also used : SQLEditor(net.sourceforge.sqlexplorer.plugin.editors.SQLEditor) SQLHistoryElement(net.sourceforge.sqlexplorer.history.SQLHistoryElement) TableItem(org.eclipse.swt.widgets.TableItem) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage)

Example 3 with SQLEditor

use of net.sourceforge.sqlexplorer.plugin.editors.SQLEditor in project tdq-studio-se by Talend.

the class CreateTableScriptAction method run.

/**
 * Create table script for selected node.
 *
 * @see org.eclipse.jface.action.IAction#run()
 */
public void run() {
    TableNode tableNode = (TableNode) _selectedNodes[0];
    ITableInfo info = tableNode.getTableInfo();
    StringBuffer buf = new StringBuffer(4 * 1024);
    String sep = System.getProperty("line.separator");
    try {
        SQLDatabaseMetaData metaData = tableNode.getSession().getMetaData();
        ArrayList<String> pks = new ArrayList<String>();
        PrimaryKeyInfo[] pksInfo = metaData.getPrimaryKey(info);
        for (PrimaryKeyInfo pkInfo : pksInfo) pks.add(pkInfo.getColumnName());
        TableColumnInfo[] columnsInfo = metaData.getColumnInfo(info);
        String tableName = _selectedNodes[0].getQualifiedName();
        buf.append("CREATE TABLE ");
        buf.append(tableName);
        buf.append("(");
        for (TableColumnInfo col : columnsInfo) {
            // String columnName = resultSet.getString(4);
            // String typeName = resultSet.getString(6);
            // String columnSize = resultSet.getString(7);
            // String decimalDigits = resultSet.getString(9);
            // String defaultValue = resultSet.getString(13);
            boolean notNull = "NO".equalsIgnoreCase(col.isNullable());
            String sLower = col.getColumnName().toLowerCase();
            buf.append(sep);
            buf.append(col.getColumnName() + " ");
            buf.append(col.getTypeName());
            boolean bNumeric = false;
            if (sLower.equals("numeric") || sLower.equals("number") || sLower.equals("decimal"))
                bNumeric = true;
            if (sLower.indexOf("char") != -1 || sLower.indexOf("int") != -1) {
                buf.append("(");
                buf.append(col.getColumnSize());
                buf.append(")");
            } else if (bNumeric) {
                buf.append("(");
                buf.append(col.getColumnSize());
                if (col.getDecimalDigits() > 0)
                    buf.append(col.getDecimalDigits());
                buf.append(")");
            }
            if (pks.size() == 1 && pks.get(0).equals(col.getColumnName())) {
                buf.append(" PRIMARY KEY");
            }
            String defaultValue = col.getDefaultValue();
            if (defaultValue != null && !defaultValue.equals("")) {
                buf.append(" default ");
                boolean isSystemValue = bNumeric;
                if (defaultValue.equalsIgnoreCase("CURRENT_TIMESTAMP")) {
                    isSystemValue = true;
                }
                if (!isSystemValue)
                    buf.append("'");
                buf.append(defaultValue);
                if (!isSystemValue)
                    buf.append("'");
            }
            if (notNull) {
                buf.append(" not null");
            }
            buf.append(",");
        }
        buf.deleteCharAt(buf.length() - 1);
        buf.append(")" + sep);
        SQLEditorInput input = new SQLEditorInput("SQL Editor (" + SQLExplorerPlugin.getDefault().getEditorSerialNo() + ").sql");
        input.setUser(_selectedNodes[0].getSession().getUser());
        IWorkbenchPage page = SQLExplorerPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage();
        SQLEditor editorPart = (SQLEditor) page.openEditor((IEditorInput) input, "net.sourceforge.sqlexplorer.plugin.editors.SQLEditor");
        editorPart.setText(buf.toString());
    } catch (SQLException e) {
        SQLExplorerPlugin.error("Error creating export script", e);
    } catch (PartInitException e) {
        SQLExplorerPlugin.error("Error creating export script", e);
    }
}
Also used : SQLEditor(net.sourceforge.sqlexplorer.plugin.editors.SQLEditor) SQLDatabaseMetaData(net.sourceforge.squirrel_sql.fw.sql.SQLDatabaseMetaData) ITableInfo(net.sourceforge.squirrel_sql.fw.sql.ITableInfo) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) PrimaryKeyInfo(net.sourceforge.squirrel_sql.fw.sql.PrimaryKeyInfo) SQLEditorInput(net.sourceforge.sqlexplorer.plugin.editors.SQLEditorInput) TableNode(net.sourceforge.sqlexplorer.dbstructure.nodes.TableNode) TableColumnInfo(net.sourceforge.squirrel_sql.fw.sql.TableColumnInfo) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) PartInitException(org.eclipse.ui.PartInitException) IEditorInput(org.eclipse.ui.IEditorInput)

Example 4 with SQLEditor

use of net.sourceforge.sqlexplorer.plugin.editors.SQLEditor in project tdq-studio-se by Talend.

the class SqlexplorerService method runInDQViewer.

/**
 * open the sql editor and run it.
 *
 * @param alias
 * @param databaseConnection
 * @param lEditorName
 * @param query
 */
private void runInDQViewer(Alias alias, DatabaseConnection databaseConnection, String lEditorName, String query) {
    String url = JavaSqlFactory.getURL(databaseConnection);
    String username = JavaSqlFactory.getUsername(databaseConnection);
    String password = JavaSqlFactory.getPassword(databaseConnection);
    // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    SQLEditorInput input = new SQLEditorInput("SQL Editor (" + alias.getName() + "." + lEditorName + ").sql");
    User user = alias.getUser(username);
    if (PluginConstant.EMPTY_STRING.equals(username)) {
        // get the user both the dbtype and username are the same.
        if (!alias.getUrl().equals(url)) {
            user = new net.sourceforge.sqlexplorer.dbproduct.User(username, password);
            user.setAlias(alias);
            alias.addUser(user);
        }
    } else {
        if (user == null) {
            user = alias.getDefaultUser();
        }
    }
    alias.setDefaultUser(user);
    // create the hive connection
    if (databaseConnection != null) {
        user.setDatabaseConnection(databaseConnection);
        // if ManagedDriver class is not Loaded,check if it lack jars then update the realted jar.
        updateDriverIfClassNotLoad(databaseConnection);
    }
    input.setUser(user);
    // TDQ-9533 append a "limit X" in sql query for vertica database.
    if (EDatabaseTypeName.VERTICA.getProduct().equals(databaseConnection.getProductId())) {
        String maxPref = SQLExplorerPlugin.getDefault().getPreferenceStore().getString(IConstants.MAX_SQL_ROWS);
        int maxNum = maxPref == null ? 100 : Integer.parseInt(maxPref);
        query = query + " limit " + maxNum;
    }
    IWorkbenchPage page = SQLExplorerPlugin.getDefault().getActivePage();
    try {
        SQLEditor editorPart = (SQLEditor) page.openEditor(input, SQLEditor.class.getName());
        editorPart.setText(query);
        new ExecSQLAction(editorPart).run();
    } catch (PartInitException e) {
        log.error(e, e);
    }
}
Also used : SQLEditorInput(net.sourceforge.sqlexplorer.plugin.editors.SQLEditorInput) SQLEditor(net.sourceforge.sqlexplorer.plugin.editors.SQLEditor) User(net.sourceforge.sqlexplorer.dbproduct.User) ExecSQLAction(net.sourceforge.sqlexplorer.sqleditor.actions.ExecSQLAction) User(net.sourceforge.sqlexplorer.dbproduct.User) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage) PartInitException(org.eclipse.ui.PartInitException)

Example 5 with SQLEditor

use of net.sourceforge.sqlexplorer.plugin.editors.SQLEditor in project tdq-studio-se by Talend.

the class OpenInEditorAction method run.

public void run() {
    try {
        TableItem[] ti = _table.getSelection();
        if (ti == null || ti.length == 0) {
            return;
        }
        String queryDelimiter = SQLExplorerPlugin.getDefault().getPluginPreferences().getString(IConstants.SQL_QRY_DELIMITER);
        StringBuffer copiedText = new StringBuffer();
        for (int i = 0; i < ti.length; i++) {
            SQLHistoryElement el = (SQLHistoryElement) ti[i].getData();
            copiedText.append(el.getRawSQLString());
            if (ti.length > 0) {
                copiedText.append(queryDelimiter);
                copiedText.append("\n");
            }
        }
        SQLHistoryElement sqlHistoryElement = (SQLHistoryElement) ti[0].getData();
        User user = sqlHistoryElement.getUser();
        Alias alias;
        if (user != null)
            alias = user.getAlias();
        else {
            alias = sqlHistoryElement.getAlias();
            if (alias != null)
                user = alias.getDefaultUser();
            if (user == null) {
                ConnectionsView view = SQLExplorerPlugin.getDefault().getConnectionsView();
                if (view != null)
                    user = view.getDefaultUser();
            }
        }
        if (user != null && !user.hasAuthenticated()) {
            boolean okToOpen = MessageDialog.openConfirm(_table.getShell(), Messages.getString("SQLHistoryView.OpenInEditor.Confirm.Title"), Messages.getString("SQLHistoryView.OpenInEditor.Confirm.Message.Prefix") + " " + user.getDescription() + Messages.getString("SQLHistoryView.OpenInEditor.Confirm.Message.Postfix"));
            if (okToOpen) {
                OpenPasswordConnectDialogAction openDlgAction = new OpenPasswordConnectDialogAction(alias, user, false);
                openDlgAction.run();
            }
        }
        SQLEditorInput input = new SQLEditorInput("SQL Editor (" + SQLExplorerPlugin.getDefault().getEditorSerialNo() + ").sql");
        input.setUser(user);
        IWorkbenchPage page = SQLExplorerPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage();
        if (page == null)
            return;
        SQLEditor editorPart = (SQLEditor) page.openEditor(input, SQLEditor.class.getName());
        editorPart.setText(copiedText.toString());
    } catch (Throwable e) {
        SQLExplorerPlugin.error("Error creating sql editor", e);
    }
}
Also used : SQLEditor(net.sourceforge.sqlexplorer.plugin.editors.SQLEditor) User(net.sourceforge.sqlexplorer.dbproduct.User) ConnectionsView(net.sourceforge.sqlexplorer.connections.ConnectionsView) TableItem(org.eclipse.swt.widgets.TableItem) OpenPasswordConnectDialogAction(net.sourceforge.sqlexplorer.plugin.actions.OpenPasswordConnectDialogAction) SQLEditorInput(net.sourceforge.sqlexplorer.plugin.editors.SQLEditorInput) SQLHistoryElement(net.sourceforge.sqlexplorer.history.SQLHistoryElement) Alias(net.sourceforge.sqlexplorer.dbproduct.Alias) IWorkbenchPage(org.eclipse.ui.IWorkbenchPage)

Aggregations

SQLEditor (net.sourceforge.sqlexplorer.plugin.editors.SQLEditor)6 IWorkbenchPage (org.eclipse.ui.IWorkbenchPage)6 SQLEditorInput (net.sourceforge.sqlexplorer.plugin.editors.SQLEditorInput)4 TableItem (org.eclipse.swt.widgets.TableItem)3 User (net.sourceforge.sqlexplorer.dbproduct.User)2 TableNode (net.sourceforge.sqlexplorer.dbstructure.nodes.TableNode)2 SQLHistoryElement (net.sourceforge.sqlexplorer.history.SQLHistoryElement)2 PartInitException (org.eclipse.ui.PartInitException)2 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 ConnectionsView (net.sourceforge.sqlexplorer.connections.ConnectionsView)1 Alias (net.sourceforge.sqlexplorer.dbproduct.Alias)1 ColumnNode (net.sourceforge.sqlexplorer.dbstructure.nodes.ColumnNode)1 OpenPasswordConnectDialogAction (net.sourceforge.sqlexplorer.plugin.actions.OpenPasswordConnectDialogAction)1 ExecSQLAction (net.sourceforge.sqlexplorer.sqleditor.actions.ExecSQLAction)1 ITableInfo (net.sourceforge.squirrel_sql.fw.sql.ITableInfo)1 PrimaryKeyInfo (net.sourceforge.squirrel_sql.fw.sql.PrimaryKeyInfo)1 SQLDatabaseMetaData (net.sourceforge.squirrel_sql.fw.sql.SQLDatabaseMetaData)1 TableColumnInfo (net.sourceforge.squirrel_sql.fw.sql.TableColumnInfo)1 IEditorInput (org.eclipse.ui.IEditorInput)1