Search in sources :

Example 21 with DataSet

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

the class ExportedKeysTab 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().getExportedKeys(tableNode.getTableInfo());
        DataSet dataSet = new DataSet(resultSet, new int[] { 4, 7, 8, 9, 10, 11, 12, 13, 14 });
        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 22 with DataSet

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

the class ColumnInfoTab method getDataSet.

@Override
public DataSet getDataSet() {
    INode node = getNode();
    if (node == null) {
        return null;
    }
    if (node instanceof TableNode) {
        TableNode tableNode = (TableNode) node;
        DataSet dataSet = null;
        // something(.e.g,ResultSetColumnReader.getLong(16))
        try {
            TableColumnInfo[] cols = node.getSession().getMetaData().getColumnInfo(tableNode.getTableInfo());
            Comparable[][] dataRows = new Comparable[cols.length][];
            int index = 0;
            for (TableColumnInfo col : cols) {
                Comparable[] row = new Comparable[COLUMN_LABELS.length];
                dataRows[index++] = row;
                int i = 0;
                row[i++] = col.getColumnName();
                row[i++] = col.getDataType();
                row[i++] = col.getTypeName();
                row[i++] = col.getColumnSize();
                row[i++] = col.getDecimalDigits();
                row[i++] = col.getRadix();
                row[i++] = col.isNullAllowed();
                row[i++] = col.getRemarks();
                row[i++] = col.getDefaultValue();
                row[i++] = col.getOctetLength();
                row[i++] = col.getOrdinalPosition();
                row[i++] = col.isNullable();
                if (i != COLUMN_LABELS.length) {
                    throw new RuntimeException(Messages.getString("ColumnInfoTab.runtimeException"));
                }
            }
            dataSet = new DataSet(COLUMN_LABELS, dataRows);
        } catch (Exception e) {
            SQLExplorerPlugin.error(Messages.getString("AbstractDataSetTab.error"), e);
            boolean isODBCTeradata = false;
            try {
                isODBCTeradata = ConnectionUtils.isOdbcTeradata(node.getSession().getMetaData().getJDBCMetaData()) ? true : false;
            } catch (SQLException e1) {
                SQLExplorerPlugin.error("Failed to get the type of Database", e);
            }
            if (isODBCTeradata) {
                MessageDialog.openError(Display.getDefault().getActiveShell(), "unsupported", "This operation is unsupported by ODBC Teradata in SQLExplorer!");
            }
        }
        return dataSet;
    }
    return null;
}
Also used : INode(net.sourceforge.sqlexplorer.dbstructure.nodes.INode) DataSet(net.sourceforge.sqlexplorer.dataset.DataSet) SQLException(java.sql.SQLException) TableNode(net.sourceforge.sqlexplorer.dbstructure.nodes.TableNode) TableColumnInfo(net.sourceforge.squirrel_sql.fw.sql.TableColumnInfo) SQLException(java.sql.SQLException)

Example 23 with DataSet

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

the class SQLExecution method doExecution.

protected void doExecution(IProgressMonitor monitor) throws Exception {
    int numErrors = 0;
    SQLException lastSQLException = null;
    try {
        long overallUpdateCount = 0;
        long overallStartTime = System.currentTimeMillis();
        for (Query query : getQueryParser()) {
            if (monitor.isCanceled())
                break;
            if (getEditor().isClosed())
                break;
            // Get the next bit of SQL to run and store it as "current"
            if (query == null)
                break;
            String querySQL = query.getQuerySql().toString();
            if (querySQL == null)
                continue;
            // Initialise
            setProgressMessage(Messages.getString("SQLResultsView.Executing"));
            final long startTime = System.currentTimeMillis();
            // Run it
            DatabaseProduct.ExecutionResults results = null;
            try {
                DatabaseProduct product = getEditor().getSession().getDatabaseProduct();
                try {
                    results = product.executeQuery(_connection, query, _maxRows);
                } catch (RuntimeException e) {
                    throw new SQLException(e.getMessage());
                }
                final long endTime = System.currentTimeMillis();
                DataSet dataSet;
                boolean checkedForMessages = false;
                while ((dataSet = results.nextDataSet()) != null) {
                    // update sql result
                    SQLResult sqlResult = new SQLResult();
                    sqlResult.setQuery(query);
                    sqlResult.setDataSet(dataSet);
                    sqlResult.setExecutionTimeMillis(endTime - startTime);
                    // Save successfull query
                    SQLExplorerPlugin.getDefault().getSQLHistory().addSQL(querySQL, _session);
                    if (monitor.isCanceled())
                        return;
                    checkForMessages(query);
                    checkedForMessages = true;
                    // show results..
                    displayResults(sqlResult);
                }
                overallUpdateCount += results.getUpdateCount();
                if (!checkedForMessages)
                    checkForMessages(query);
                debugLogQuery(query, null);
            } catch (final SQLException e) {
                debugLogQuery(query, e);
                boolean stopOnError = SQLExplorerPlugin.getDefault().getPreferenceStore().getBoolean(IConstants.STOP_ON_ERROR);
                logException(e, query, stopOnError);
                closeStatement();
                hasMessages = true;
                if (stopOnError) {
                    errorDialog(Messages.getString("SQLResultsView.Error.Title"), e.getMessage());
                    return;
                }
                numErrors++;
                lastSQLException = e;
            } finally {
                try {
                    if (results != null) {
                        results.close();
                        results = null;
                    }
                } catch (SQLException e) {
                // Nothing
                }
            }
        }
        if (!hasMessages || SQLExplorerPlugin.getDefault().getPreferenceStore().getBoolean(IConstants.LOG_SUCCESS_MESSAGES)) {
            long overallTime = System.currentTimeMillis() - overallStartTime;
            String message = Long.toString(overallUpdateCount) + " " + Messages.getString("SQLEditor.Update.Prefix") + " " + Long.toString(overallTime) + " " + Messages.getString("SQLEditor.Update.Postfix");
            addMessage(new Message(Message.Status.STATUS, getQueryParser().adjustLineNo(1), 0, "", message));
        }
    } catch (Exception e) {
        closeStatement();
        throw e;
    }
    if (numErrors == 1)
        throw lastSQLException;
    else if (numErrors > 1 && SQLExplorerPlugin.getDefault().getPreferenceStore().getBoolean(IConstants.CONFIRM_BOOL_SHOW_DIALOG_ON_QUERY_ERROR))
        getEditor().getSite().getShell().getDisplay().asyncExec(new Runnable() {

            public void run() {
                MessageDialogWithToggle dialog = MessageDialogWithToggle.openInformation(getEditor().getSite().getShell(), Messages.getString("SQLExecution.Error.Title"), Messages.getString("SQLExecution.Error.Message"), Messages.getString("SQLExecution.Error.Toggle"), false, null, null);
                if (dialog.getToggleState() && dialog.getReturnCode() == IDialogConstants.OK_ID)
                    SQLExplorerPlugin.getDefault().getPluginPreferences().setValue(IConstants.CONFIRM_BOOL_SHOW_DIALOG_ON_QUERY_ERROR, false);
            }
        });
}
Also used : Query(net.sourceforge.sqlexplorer.parsers.Query) Message(net.sourceforge.sqlexplorer.plugin.editors.Message) SQLException(java.sql.SQLException) DataSet(net.sourceforge.sqlexplorer.dataset.DataSet) SQLException(java.sql.SQLException) DatabaseProduct(net.sourceforge.sqlexplorer.dbproduct.DatabaseProduct) MessageDialogWithToggle(org.eclipse.jface.dialogs.MessageDialogWithToggle)

Example 24 with DataSet

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

the class BatchJob method run.

@Override
protected IStatus run(IProgressMonitor monitor) {
    monitor.beginTask(Messages.getString("BatchJob.ExecutingScripts"), files.size());
    DatabaseProduct product = user.getAlias().getDriver().getDatabaseProduct();
    SQLConnection connection = null;
    try {
        if (session == null)
            session = user.createSession();
        connection = session.grabConnection();
        int index = 0;
        for (File file : files) {
            if (monitor.isCanceled())
                break;
            monitor.worked(index++);
            monitor.subTask(file.getName());
            _logger.fatal(file.getAbsolutePath());
            String sql = null;
            try {
                char[] buffer = new char[(int) file.length() + 10];
                FileReader reader = new FileReader(file);
                int length = reader.read(buffer);
                reader.close();
                if (length < 0 || length >= buffer.length) {
                    SQLExplorerPlugin.error("Cannot read from file " + file.getAbsolutePath());
                    continue;
                }
                // Normalise this to have standard \n in strings.  \r confuses Oracle and
                // isn't normally needed internally anyway
                StringBuffer sb = new StringBuffer(new String(buffer, 0, length));
                buffer = null;
                for (int i = 0; i < sb.length(); i++) {
                    if (sb.charAt(i) == '\r') {
                        sb.deleteCharAt(i);
                        i--;
                    }
                }
                sql = sb.toString();
                sb = null;
            } catch (IOException e) {
                SQLExplorerPlugin.error("Cannot read from file " + file.getAbsolutePath(), e);
                continue;
            }
            QueryParser parser = product.getQueryParser(sql, 1);
            parser.parse();
            for (Query query : parser) {
                DatabaseProduct.ExecutionResults results = null;
                try {
                    results = product.executeQuery(connection, query, -1);
                    DataSet dataSet;
                    while ((dataSet = results.nextDataSet()) != null) {
                        LinkedList<Message> messages = new LinkedList<Message>();
                        Collection<Message> messagesTmp = session.getDatabaseProduct().getErrorMessages(connection, query);
                        if (messagesTmp != null)
                            messages.addAll(messagesTmp);
                        messagesTmp = session.getDatabaseProduct().getServerMessages(connection);
                        if (messagesTmp != null)
                            messages.addAll(messagesTmp);
                        for (Message msg : messages) msg.setLineNo(parser.adjustLineNo(msg.getLineNo()));
                        for (Message message : messages) {
                            _logger.fatal(message.getSql());
                        }
                    }
                } catch (SQLException e) {
                    _logger.fatal(e.getMessage());
                } finally {
                    try {
                        if (results != null) {
                            results.close();
                            results = null;
                        }
                    } catch (SQLException e) {
                    // Nothing
                    }
                }
            }
        }
        monitor.done();
    } catch (SQLException e) {
        SQLExplorerPlugin.error(e);
    } catch (ParserException e) {
        SQLExplorerPlugin.error(e);
    } finally {
        if (connection != null)
            session.releaseConnection(connection);
    }
    return new Status(IStatus.OK, getClass().getName(), IStatus.OK, Messages.getString("BatchJob.Success"), null);
}
Also used : Status(org.eclipse.core.runtime.Status) IStatus(org.eclipse.core.runtime.IStatus) ParserException(net.sourceforge.sqlexplorer.parsers.ParserException) Query(net.sourceforge.sqlexplorer.parsers.Query) Message(net.sourceforge.sqlexplorer.plugin.editors.Message) DataSet(net.sourceforge.sqlexplorer.dataset.DataSet) SQLException(java.sql.SQLException) SQLConnection(net.sourceforge.sqlexplorer.dbproduct.SQLConnection) IOException(java.io.IOException) LinkedList(java.util.LinkedList) DatabaseProduct(net.sourceforge.sqlexplorer.dbproduct.DatabaseProduct) QueryParser(net.sourceforge.sqlexplorer.parsers.QueryParser) FileReader(java.io.FileReader) File(java.io.File)

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