Search in sources :

Example 1 with SqlScriptStatement

use of org.pentaho.di.core.database.SqlScriptStatement in project pentaho-kettle by pentaho.

the class SQLEditor method exec.

private void exec() {
    DatabaseMeta ci = connection;
    if (ci == null) {
        return;
    }
    StringBuilder message = new StringBuilder();
    Database db = new Database(loggingObject, ci);
    boolean first = true;
    PartitionDatabaseMeta[] partitioningInformation = ci.getPartitioningInformation();
    for (int partitionNr = 0; first || (partitioningInformation != null && partitionNr < partitioningInformation.length); partitionNr++) {
        first = false;
        String partitionId = null;
        if (partitioningInformation != null && partitioningInformation.length > 0) {
            partitionId = partitioningInformation[partitionNr].getPartitionId();
        }
        try {
            db.connect(partitionId);
            String sqlScript = Utils.isEmpty(wScript.getSelectionText()) ? wScript.getText() : wScript.getSelectionText();
            // Multiple statements in the script need to be split into individual
            // executable statements
            statements = ci.getDatabaseInterface().getSqlScriptStatements(sqlScript + Const.CR);
            int nrstats = 0;
            for (SqlScriptStatement sql : statements) {
                if (sql.isQuery()) {
                    // A Query
                    log.logDetailed("launch SELECT statement: " + Const.CR + sql);
                    nrstats++;
                    try {
                        List<Object[]> rows = db.getRows(sql.getStatement(), 1000);
                        RowMetaInterface rowMeta = db.getReturnRowMeta();
                        if (rows.size() > 0) {
                            PreviewRowsDialog prd = new PreviewRowsDialog(shell, ci, SWT.NONE, BaseMessages.getString(PKG, "SQLEditor.ResultRows.Title", Integer.toString(nrstats)), rowMeta, rows);
                            prd.open();
                        } else {
                            MessageBox mb = new MessageBox(shell, SWT.ICON_INFORMATION | SWT.OK);
                            mb.setMessage(BaseMessages.getString(PKG, "SQLEditor.NoRows.Message", sql));
                            mb.setText(BaseMessages.getString(PKG, "SQLEditor.NoRows.Title"));
                            mb.open();
                        }
                    } catch (KettleDatabaseException dbe) {
                        new ErrorDialog(shell, BaseMessages.getString(PKG, "SQLEditor.ErrorExecSQL.Title"), BaseMessages.getString(PKG, "SQLEditor.ErrorExecSQL.Message", sql), dbe);
                    }
                } else {
                    log.logDetailed("launch DDL statement: " + Const.CR + sql);
                    // A DDL statement
                    nrstats++;
                    int startLogLine = KettleLogStore.getLastBufferLineNr();
                    try {
                        log.logDetailed("Executing SQL: " + Const.CR + sql);
                        db.execStatement(sql.getStatement());
                        message.append(BaseMessages.getString(PKG, "SQLEditor.Log.SQLExecuted", sql));
                        message.append(Const.CR);
                        // Clear the database cache, in case we're using one...
                        if (dbcache != null) {
                            dbcache.clear(ci.getName());
                        }
                        // mark the statement in green in the dialog...
                        // 
                        sql.setOk(true);
                    } catch (Exception dbe) {
                        sql.setOk(false);
                        String error = BaseMessages.getString(PKG, "SQLEditor.Log.SQLExecError", sql, dbe.toString());
                        message.append(error).append(Const.CR);
                        ErrorDialog dialog = new ErrorDialog(shell, BaseMessages.getString(PKG, "SQLEditor.ErrorExecSQL.Title"), error, dbe, true);
                        if (dialog.isCancelled()) {
                            break;
                        }
                    } finally {
                        int endLogLine = KettleLogStore.getLastBufferLineNr();
                        sql.setLoggingText(KettleLogStore.getAppender().getLogBufferFromTo(db.getLogChannelId(), true, startLogLine, endLogLine).toString());
                        sql.setComplete(true);
                        refreshExecutionResults();
                    }
                }
            }
            message.append(BaseMessages.getString(PKG, "SQLEditor.Log.StatsExecuted", Integer.toString(nrstats)));
            if (partitionId != null) {
                message.append(BaseMessages.getString(PKG, "SQLEditor.Log.OnPartition", partitionId));
            }
            message.append(Const.CR);
        } catch (KettleDatabaseException dbe) {
            MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ICON_ERROR);
            String error = BaseMessages.getString(PKG, "SQLEditor.Error.CouldNotConnect.Message", (connection == null ? "" : connection.getName()), dbe.getMessage());
            message.append(error).append(Const.CR);
            mb.setMessage(error);
            mb.setText(BaseMessages.getString(PKG, "SQLEditor.Error.CouldNotConnect.Title"));
            mb.open();
        } finally {
            db.disconnect();
            refreshExecutionResults();
        }
    }
    EnterTextDialog dialog = new EnterTextDialog(shell, BaseMessages.getString(PKG, "SQLEditor.Result.Title"), BaseMessages.getString(PKG, "SQLEditor.Result.Message"), message.toString(), true);
    dialog.open();
}
Also used : KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) ErrorDialog(org.pentaho.di.ui.core.dialog.ErrorDialog) RowMetaInterface(org.pentaho.di.core.row.RowMetaInterface) PartitionDatabaseMeta(org.pentaho.di.core.database.PartitionDatabaseMeta) PreviewRowsDialog(org.pentaho.di.ui.core.dialog.PreviewRowsDialog) DatabaseMeta(org.pentaho.di.core.database.DatabaseMeta) PartitionDatabaseMeta(org.pentaho.di.core.database.PartitionDatabaseMeta) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) MessageBox(org.eclipse.swt.widgets.MessageBox) Database(org.pentaho.di.core.database.Database) EnterTextDialog(org.pentaho.di.ui.core.dialog.EnterTextDialog) SqlScriptStatement(org.pentaho.di.core.database.SqlScriptStatement)

Example 2 with SqlScriptStatement

use of org.pentaho.di.core.database.SqlScriptStatement in project pentaho-kettle by pentaho.

the class SQLValuesHighlight method lineGetStyle.

/**
 * Event.detail line start offset (input) Event.text line text (input) LineStyleEvent.styles Enumeration of
 * StyleRanges, need to be in order. (output) LineStyleEvent.background line background color (output)
 */
public void lineGetStyle(LineStyleEvent event) {
    Vector<StyleRange> styles = new Vector<StyleRange>();
    int token;
    StyleRange lastStyle;
    if (inBlockComment(event.lineOffset, event.lineOffset + event.lineText.length())) {
        styles.addElement(new StyleRange(event.lineOffset, event.lineText.length() + 4, colors[1], null));
        event.styles = new StyleRange[styles.size()];
        styles.copyInto(event.styles);
        return;
    }
    scanner.setRange(event.lineText);
    String xs = ((StyledText) event.widget).getText();
    if (xs != null) {
        parseBlockComments(xs);
    }
    token = scanner.nextToken();
    while (token != EOF) {
        if (token != OTHER) {
            if ((token == WHITE) && (!styles.isEmpty())) {
                int start = scanner.getStartOffset() + event.lineOffset;
                lastStyle = styles.lastElement();
                if (lastStyle.fontStyle != SWT.NORMAL) {
                    if (lastStyle.start + lastStyle.length == start) {
                        // have the white space take on the style before it to minimize font style
                        // changes
                        lastStyle.length += scanner.getLength();
                    }
                }
            } else {
                Color color = getColor(token);
                if (color != colors[0]) {
                    // hardcoded default foreground color, black
                    StyleRange style = new StyleRange(scanner.getStartOffset() + event.lineOffset, scanner.getLength(), color, null);
                    // }
                    if (styles.isEmpty()) {
                        styles.addElement(style);
                    } else {
                        lastStyle = styles.lastElement();
                        if (lastStyle.similarTo(style) && (lastStyle.start + lastStyle.length == style.start)) {
                            lastStyle.length += style.length;
                        } else {
                            styles.addElement(style);
                        }
                    }
                }
            }
        }
        token = scanner.nextToken();
    }
    // 
    if (scriptStatements != null) {
        for (SqlScriptStatement statement : scriptStatements) {
            // Leave non-executed statements alone.
            // 
            StyleRange styleRange = new StyleRange();
            styleRange.start = statement.getFromIndex();
            styleRange.length = statement.getToIndex() - statement.getFromIndex();
            if (statement.isComplete()) {
                if (statement.isOk()) {
                    // GUIResource.getInstance().getColor(63, 127, 95), // green
                    // honey dew
                    styleRange.background = GUIResource.getInstance().getColor(244, 238, 224);
                } else {
                    // Antique White
                    styleRange.background = GUIResource.getInstance().getColor(250, 235, 215);
                }
            } else {
                styleRange.background = GUIResource.getInstance().getColorWhite();
            }
            styles.add(styleRange);
        }
    }
    event.styles = new StyleRange[styles.size()];
    styles.copyInto(event.styles);
}
Also used : StyledText(org.eclipse.swt.custom.StyledText) StyleRange(org.eclipse.swt.custom.StyleRange) Color(org.eclipse.swt.graphics.Color) SqlScriptStatement(org.pentaho.di.core.database.SqlScriptStatement) Vector(java.util.Vector)

Aggregations

SqlScriptStatement (org.pentaho.di.core.database.SqlScriptStatement)2 Vector (java.util.Vector)1 StyleRange (org.eclipse.swt.custom.StyleRange)1 StyledText (org.eclipse.swt.custom.StyledText)1 Color (org.eclipse.swt.graphics.Color)1 MessageBox (org.eclipse.swt.widgets.MessageBox)1 Database (org.pentaho.di.core.database.Database)1 DatabaseMeta (org.pentaho.di.core.database.DatabaseMeta)1 PartitionDatabaseMeta (org.pentaho.di.core.database.PartitionDatabaseMeta)1 KettleDatabaseException (org.pentaho.di.core.exception.KettleDatabaseException)1 RowMetaInterface (org.pentaho.di.core.row.RowMetaInterface)1 EnterTextDialog (org.pentaho.di.ui.core.dialog.EnterTextDialog)1 ErrorDialog (org.pentaho.di.ui.core.dialog.ErrorDialog)1 PreviewRowsDialog (org.pentaho.di.ui.core.dialog.PreviewRowsDialog)1