Search in sources :

Example 1 with ColumnNode

use of net.sourceforge.sqlexplorer.dbstructure.nodes.ColumnNode 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 ColumnNode

use of net.sourceforge.sqlexplorer.dbstructure.nodes.ColumnNode in project tdq-studio-se by Talend.

the class GenerateSelectSQLAction method createColumnSelect.

/**
 * @return query string for full table select
 */
private String createColumnSelect() {
    // $NON-NLS-1$
    StringBuffer query = new StringBuffer("select ");
    // $NON-NLS-1$
    String sep = "";
    // $NON-NLS-1$
    String table = "";
    for (INode node : _selectedNodes) {
        if (node instanceof ColumnNode) {
            ColumnNode column = (ColumnNode) node;
            if (table.length() == 0) {
                table = column.getQualifiedParentTableName();
            }
            if (column.getQualifiedParentTableName().equals(table)) {
                query.append(sep);
                query.append(quote(column.getName(), getQuoteString(column)));
                // $NON-NLS-1$
                sep = ", ";
            }
        }
    }
    // $NON-NLS-1$
    query.append(" from ");
    query.append(fixTableName(table));
    return query.toString();
}
Also used : INode(net.sourceforge.sqlexplorer.dbstructure.nodes.INode) ColumnNode(net.sourceforge.sqlexplorer.dbstructure.nodes.ColumnNode)

Aggregations

ColumnNode (net.sourceforge.sqlexplorer.dbstructure.nodes.ColumnNode)2 INode (net.sourceforge.sqlexplorer.dbstructure.nodes.INode)1 TableNode (net.sourceforge.sqlexplorer.dbstructure.nodes.TableNode)1 SQLEditor (net.sourceforge.sqlexplorer.plugin.editors.SQLEditor)1 SQLEditorInput (net.sourceforge.sqlexplorer.plugin.editors.SQLEditorInput)1 IWorkbenchPage (org.eclipse.ui.IWorkbenchPage)1