Search in sources :

Example 11 with DataSet

use of net.sourceforge.sqlexplorer.dataset.DataSet in project tdq-studio-se by Talend.

the class ColumnPriviligesTab method getDataSet.

public DataSet getDataSet() throws Exception {
    INode node = getNode();
    if (node == null) {
        return null;
    }
    if (node instanceof TableNode) {
        TableNode tableNode = (TableNode) node;
        ResultSet resultSet = node.getSession().getMetaData().getColumnPrivileges(tableNode.getTableInfo());
        DataSet dataSet = new DataSet(resultSet, new int[] { 4, 5, 6, 7, 8 });
        resultSet.close();
        return dataSet;
    }
    return null;
}
Also used : INode(net.sourceforge.sqlexplorer.dbstructure.nodes.INode) DataSet(net.sourceforge.sqlexplorer.dataset.DataSet) TableNode(net.sourceforge.sqlexplorer.dbstructure.nodes.TableNode) ResultSet(java.sql.ResultSet)

Example 12 with DataSet

use of net.sourceforge.sqlexplorer.dataset.DataSet in project tdi-studio-se by Talend.

the class ExportCSVAction method run.

/**
     * Main method. Prompt for file name and save table contents to csv file.
     */
public void run() {
    // get filename
    FileDialog fileDialog = new FileDialog(ptable.getShell(), SWT.SAVE);
    //$NON-NLS-1$
    String[] filterExtensions = new String[] { "*.csv" };
    fileDialog.setFilterExtensions(filterExtensions);
    final String fileName = fileDialog.open();
    if (fileName == null || fileName.trim().length() == 0) {
        return;
    }
    // let's show the fancy wait cursor..
    BusyIndicator.showWhile(Display.getCurrent(), new Runnable() {

        public void run() {
            try {
                // create new file
                File file = new File(fileName);
                if (file.exists()) {
                    // overwrite existing files
                    file.delete();
                }
                file.createNewFile();
                BufferedWriter writer = new BufferedWriter(new FileWriter(file));
                //$NON-NLS-1$
                StringBuffer buffer = new StringBuffer("");
                // get column header and separator preferences
                String columnSeparator = SqlBuilderPlugin.getDefault().getPreferenceStore().getString(IConstants.CLIP_EXPORT_SEPARATOR);
                boolean includeColumnNames = SqlBuilderPlugin.getDefault().getPreferenceStore().getBoolean(IConstants.CLIP_EXPORT_COLUMNS);
                // check if there is somethign in our table
                TableItem[] items = ptable.getItems();
                DataSet dataSet = (DataSet) ptable.getData();
                if (items == null || dataSet == null) {
                    return;
                }
                // export column names if we need to 
                if (includeColumnNames) {
                    String[] columnNames = dataSet.getColumnLabels();
                    for (int i = 0; i < columnNames.length; i++) {
                        buffer.append(columnNames[i]);
                        buffer.append(columnSeparator);
                    }
                    writer.write(buffer.toString(), 0, buffer.length());
                    writer.newLine();
                }
                // export column data
                int columnCount = ptable.getColumnCount();
                for (int i = 0; i < items.length; i++) {
                    //$NON-NLS-1$
                    buffer = new StringBuffer("");
                    for (int j = 0; j < columnCount; j++) {
                        buffer.append(items[i].getText(j));
                        buffer.append(columnSeparator);
                    }
                    writer.write(buffer.toString(), 0, buffer.length());
                    writer.newLine();
                }
                writer.close();
            } catch (final Exception e) {
                ptable.getShell().getDisplay().asyncExec(new Runnable() {

                    public void run() {
                        MessageDialog.openError(ptable.getShell(), Messages.getString("SQLResultsView.Error.Export.Title"), //$NON-NLS-1$
                        e.getMessage());
                    }
                });
            }
        }
    });
}
Also used : DataSet(net.sourceforge.sqlexplorer.dataset.DataSet) FileWriter(java.io.FileWriter) FileDialog(org.eclipse.swt.widgets.FileDialog) File(java.io.File) BufferedWriter(java.io.BufferedWriter)

Example 13 with DataSet

use of net.sourceforge.sqlexplorer.dataset.DataSet in project tdi-studio-se by Talend.

the class ExportHTMLAction method run.

/**
     * Copy all table data to clipboard.
     * @see org.eclipse.jface.action.IAction#run()
     */
public void run() {
    FileDialog fileDialog = new FileDialog(ptable.getShell(), SWT.SAVE);
    //$NON-NLS-1$ //$NON-NLS-2$
    String[] filterExtensions = new String[] { "*.htm", "*.html" };
    fileDialog.setFilterExtensions(filterExtensions);
    final String fileName = fileDialog.open();
    if (fileName == null || fileName.trim().length() == 0) {
        return;
    }
    BusyIndicator.showWhile(Display.getCurrent(), new Runnable() {

        public void run() {
            try {
                File file = new File(fileName);
                if (file.exists()) {
                    // overwrite existing files
                    file.delete();
                }
                file.createNewFile();
                BufferedWriter writer = new BufferedWriter(new FileWriter(file));
                //$NON-NLS-1$
                StringBuffer buffer = new StringBuffer("");
                // get preferences
                boolean includeColumnNames = SqlBuilderPlugin.getDefault().getPreferenceStore().getBoolean(IConstants.CLIP_EXPORT_COLUMNS);
                TableItem[] items = ptable.getItems();
                DataSet dataSet = (DataSet) ptable.getData();
                if (items == null || dataSet == null) {
                    return;
                }
                //$NON-NLS-1$
                writer.write("<html>");
                writer.newLine();
                //$NON-NLS-1$
                writer.write("<style>");
                //$NON-NLS-1$
                writer.write("TABLE {border-collapse: collapse;}");
                //$NON-NLS-1$
                writer.write("TH {background-color: rgb(240, 244, 245);}");
                //$NON-NLS-1$
                writer.write("TH, TD {border: 1px solid #D1D6D4;font-size: 10px;font-family: Verdana, Arial, Helvetica, sans-serif;}");
                //$NON-NLS-1$
                writer.write(".right {text-align: right;}");
                //$NON-NLS-1$
                writer.write("</style>");
                //$NON-NLS-1$
                writer.write("</head>");
                //$NON-NLS-1$
                writer.write("<table>");
                writer.newLine();
                // export column names
                if (includeColumnNames) {
                    //$NON-NLS-1$
                    buffer.append("<tr>");
                    String[] columnNames = dataSet.getColumnLabels();
                    for (int i = 0; i < columnNames.length; i++) {
                        //$NON-NLS-1$
                        buffer.append("<th>");
                        buffer.append(columnNames[i]);
                        //$NON-NLS-1$
                        buffer.append("</th>");
                    }
                    //$NON-NLS-1$
                    buffer.append("</tr>");
                    writer.write(buffer.toString());
                    writer.newLine();
                }
                DataSet set = (DataSet) ptable.getData();
                // export column data
                int columnCount = ptable.getColumnCount();
                for (int i = 0; i < items.length; i++) {
                    //$NON-NLS-1$
                    buffer = new StringBuffer("<tr>");
                    for (int j = 0; j < columnCount; j++) {
                        if (set.getColumnTypes()[j] == DataSet.TYPE_DOUBLE || set.getColumnTypes()[j] == DataSet.TYPE_INTEGER) {
                            // right align numbers
                            //$NON-NLS-1$
                            buffer.append("<td class=\"right\">");
                        } else {
                            //$NON-NLS-1$
                            buffer.append("<td>");
                        }
                        buffer.append(items[i].getText(j));
                        //$NON-NLS-1$
                        buffer.append("</td>");
                    }
                    //$NON-NLS-1$
                    buffer.append("</tr>");
                    writer.write(buffer.toString());
                    writer.newLine();
                }
                //$NON-NLS-1$
                writer.write("</table>");
                writer.newLine();
                //$NON-NLS-1$
                writer.write("</html>");
                writer.newLine();
                writer.close();
            } catch (final Exception e) {
                ptable.getShell().getDisplay().asyncExec(new Runnable() {

                    public void run() {
                        MessageDialog.openError(ptable.getShell(), Messages.getString("SQLResultsView.Error.Export.Title"), //$NON-NLS-1$
                        e.getMessage());
                        //$NON-NLS-1$
                        SqlBuilderPlugin.log(Messages.getString("SQLResultsView.Error.Export.Title"), e);
                    }
                });
            }
        }
    });
}
Also used : DataSet(net.sourceforge.sqlexplorer.dataset.DataSet) FileWriter(java.io.FileWriter) FileDialog(org.eclipse.swt.widgets.FileDialog) File(java.io.File) BufferedWriter(java.io.BufferedWriter)

Example 14 with DataSet

use of net.sourceforge.sqlexplorer.dataset.DataSet in project tdq-studio-se by Talend.

the class ExecutionResultImpl method nextDataSet.

public DataSet nextDataSet() throws SQLException {
    // Close the current one
    if (currentResultSet != null) {
        currentResultSet.close();
        currentResultSet = null;
    }
    // Anything more to do?
    if (state == State.CLOSED) {
        return null;
    }
    // Get the first set
    if (state == State.PRIMARY_RESULTS) {
        currentResultSet = stmt.getResultSet();
        state = State.SECONDARY_RESULTS;
        if (currentResultSet != null) {
            return new DataSet(currentResultSet, null, maxRows);
        }
    }
    // getResults())
    while (state == State.SECONDARY_RESULTS) {
        // MOD msjian TDQ-5927, fix the "statement is not executing" error for SQLite.
        if ("org.sqlite.PrepStmt".equals(stmt.getClass().getName())) {
            return null;
        }
        // "updateCountState()"
        if (stmt.getClass().getName().contains(hiveStatementClassName)) {
            updateCountState();
        } else if (stmt.getMoreResults()) {
            currentResultSet = stmt.getResultSet();
        } else {
            updateCountState();
        }
    // ~ xqliu 2014-03-18
    }
    // Got one? Then exit
    if (currentResultSet != null) {
        this.updateCount += stmt.getUpdateCount();
        return new DataSet(currentResultSet, null, maxRows);
    }
    // Look for output parameters which return resultsets
    if (state == State.PARAMETER_RESULTS && parameters != null) {
        CallableStatement stmt = (CallableStatement) this.stmt;
        if (paramIter == null) {
            paramIter = parameters.iterator();
            paramColumnIndex = 1;
        }
        while (paramIter.hasNext()) {
            NamedParameter param = paramIter.next();
            if (param.getDataType() == NamedParameter.DataType.CURSOR) {
                currentResultSet = product.getResultSet(stmt, param, paramColumnIndex);
            }
            paramColumnIndex++;
            if (currentResultSet != null) {
                return new DataSet(Messages.getString("DataSet.Cursor") + ' ' + param.getName(), currentResultSet, null, maxRows);
            }
        }
    }
    // Generate a dataset for output parameters
    state = State.CLOSED;
    if (parameters == null) {
        return null;
    }
    if (!(stmt instanceof CallableStatement)) {
        return null;
    }
    CallableStatement stmt = (CallableStatement) this.stmt;
    TreeMap<NamedParameter, ParamValues> params = new TreeMap<NamedParameter, ParamValues>();
    int columnIndex = 1;
    int numValues = 0;
    for (NamedParameter param : parameters) {
        if (param.getDataType() != NamedParameter.DataType.CURSOR && param.isOutput()) {
            ParamValues pv = params.get(param);
            if (pv == null) {
                params.put(param, new ParamValues(param, columnIndex));
            } else {
                pv.add(columnIndex);
            }
            numValues++;
        }
        columnIndex++;
    }
    if (numValues == 0) {
        return null;
    }
    Comparable[][] rows = new Comparable[numValues][2];
    columnIndex = 1;
    int rowIndex = 0;
    for (ParamValues pv : params.values()) {
        int valueIndex = 1;
        for (Integer index : pv.columnIndexes) {
            Comparable[] row = rows[rowIndex++];
            row[0] = pv.param.getName();
            if (pv.columnIndexes.size() > 1) {
                row[0] = (pv.param.getName() + '[' + valueIndex + ']');
            } else {
                row[0] = pv.param.getName();
            }
            row[1] = stmt.getString(index);
            valueIndex++;
        }
    }
    return new DataSet(Messages.getString("DataSet.Parameters"), new String[] { Messages.getString("SQLExecution.ParameterName"), Messages.getString("SQLExecution.ParameterValue") }, rows);
}
Also used : DataSet(net.sourceforge.sqlexplorer.dataset.DataSet) CallableStatement(java.sql.CallableStatement) NamedParameter(net.sourceforge.sqlexplorer.parsers.NamedParameter) TreeMap(java.util.TreeMap)

Example 15 with DataSet

use of net.sourceforge.sqlexplorer.dataset.DataSet in project tdq-studio-se by Talend.

the class ExportCSVAction method run.

/**
 * Main method. Prompt for file name and save table contents to csv file.
 */
@Override
public void run() {
    final CsvExportOptionsDlg dlg = new CsvExportOptionsDlg(_table.getShell());
    if (dlg.open() != Window.OK) {
        return;
    }
    // let's show the fancy wait cursor..
    BusyIndicator.showWhile(Display.getCurrent(), new Runnable() {

        public void run() {
            try {
                // create new file
                File file = new File(dlg.getFilename());
                if (file.exists()) {
                    // overwrite existing files
                    file.delete();
                }
                file.createNewFile();
                PrintStream writer = new PrintStream(file, dlg.getCharacterSet());
                // $NON-NLS-1$
                StringBuffer buffer = new StringBuffer("");
                // get column header and separator preferences
                String columnSeparator = dlg.getDelimiter();
                boolean includeColumnNames = dlg.includeHeaders();
                boolean rtrim = dlg.trimSpaces();
                boolean quote = dlg.quoteText();
                String nullValue = dlg.getNullValue();
                boolean isExportAll = dlg.isExportAll();
                // check if there is somethign in our table
                DataSet dataSet = (DataSet) _table.getData();
                if (!isExportAll && SqlExplorerTalendDataSet.class.isInstance(dataSet)) {
                    dataSet = ((SqlExplorerTalendDataSet) dataSet).getCurrentPageDataSet();
                }
                if (dataSet == null) {
                    return;
                }
                // export column names if we need to
                if (includeColumnNames) {
                    DataSet.Column[] columns = dataSet.getColumns();
                    for (int i = 0; i < columns.length; i++) {
                        if (i != 0) {
                            buffer.append(columnSeparator);
                        }
                        buffer.append(columns[i].getCaption());
                    }
                    writer.println(buffer.toString());
                }
                // export column data
                int columnCount = _table.getColumnCount();
                for (int i = 0; i < dataSet.getRowCount(); i++) {
                    // $NON-NLS-1$
                    buffer = new StringBuffer("");
                    DataSetRow row = dataSet.getRow(i);
                    for (int j = 0; j < columnCount; j++) {
                        Object o = row.getRawObjectValue(j);
                        String t = o == null ? nullValue : o.toString();
                        if (rtrim) {
                            t = TextUtil.rtrim(t);
                        }
                        if (quote && o instanceof String) {
                            // $NON-NLS-1$
                            buffer.append("\"");
                            buffer.append(t);
                            // $NON-NLS-1$
                            buffer.append("\"");
                        } else {
                            buffer.append(t);
                        }
                        /* don't append separator _after_ last column */
                        if (j < columnCount - 1) {
                            buffer.append(columnSeparator);
                        }
                    }
                    writer.println(buffer.toString());
                }
                writer.close();
            } catch (final Exception e) {
                _table.getShell().getDisplay().asyncExec(new Runnable() {

                    public void run() {
                        MessageDialog.openError(_table.getShell(), Messages.getString("SQLResultsView.Error.Export.Title"), e.getMessage());
                    }
                });
            }
        }
    });
}
Also used : CsvExportOptionsDlg(net.sourceforge.sqlexplorer.dialogs.CsvExportOptionsDlg) PrintStream(java.io.PrintStream) SqlExplorerTalendDataSet(net.sourceforge.sqlexplorer.dataset.mapdb.SqlExplorerTalendDataSet) DataSet(net.sourceforge.sqlexplorer.dataset.DataSet) SqlExplorerTalendDataSet(net.sourceforge.sqlexplorer.dataset.mapdb.SqlExplorerTalendDataSet) File(java.io.File) DataSetRow(net.sourceforge.sqlexplorer.dataset.DataSetRow)

Aggregations

DataSet (net.sourceforge.sqlexplorer.dataset.DataSet)24 INode (net.sourceforge.sqlexplorer.dbstructure.nodes.INode)12 TableNode (net.sourceforge.sqlexplorer.dbstructure.nodes.TableNode)12 ResultSet (java.sql.ResultSet)8 File (java.io.File)7 SQLException (java.sql.SQLException)6 SQLConnection (net.sourceforge.sqlexplorer.dbproduct.SQLConnection)4 BufferedWriter (java.io.BufferedWriter)3 FileWriter (java.io.FileWriter)3 PrintStream (java.io.PrintStream)3 DataSetRow (net.sourceforge.sqlexplorer.dataset.DataSetRow)3 FileDialog (org.eclipse.swt.widgets.FileDialog)3 Statement (java.sql.Statement)2 DatabaseProduct (net.sourceforge.sqlexplorer.dbproduct.DatabaseProduct)2 Query (net.sourceforge.sqlexplorer.parsers.Query)2 Message (net.sourceforge.sqlexplorer.plugin.editors.Message)2 FileReader (java.io.FileReader)1 IOException (java.io.IOException)1 CallableStatement (java.sql.CallableStatement)1 DatabaseMetaData (java.sql.DatabaseMetaData)1