Search in sources :

Example 6 with SQLStatement

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

the class JobMeta method getSQLStatements.

/**
 * Builds a list of all the SQL statements that this transformation needs in order to work properly.
 *
 * @return An ArrayList of SQLStatement objects.
 */
public List<SQLStatement> getSQLStatements(Repository repository, IMetaStore metaStore, ProgressMonitorListener monitor) throws KettleException {
    if (monitor != null) {
        monitor.beginTask(BaseMessages.getString(PKG, "JobMeta.Monitor.GettingSQLNeededForThisJob"), nrJobEntries() + 1);
    }
    List<SQLStatement> stats = new ArrayList<SQLStatement>();
    for (int i = 0; i < nrJobEntries(); i++) {
        JobEntryCopy copy = getJobEntry(i);
        if (monitor != null) {
            monitor.subTask(BaseMessages.getString(PKG, "JobMeta.Monitor.GettingSQLForJobEntryCopy") + copy + "]");
        }
        stats.addAll(copy.getEntry().getSQLStatements(repository, metaStore, this));
        stats.addAll(compatibleGetEntrySQLStatements(copy.getEntry(), repository));
        stats.addAll(compatibleGetEntrySQLStatements(copy.getEntry(), repository, this));
        if (monitor != null) {
            monitor.worked(1);
        }
    }
    // Also check the sql for the logtable...
    if (monitor != null) {
        monitor.subTask(BaseMessages.getString(PKG, "JobMeta.Monitor.GettingSQLStatementsForJobLogTables"));
    }
    if (jobLogTable.getDatabaseMeta() != null && !Utils.isEmpty(jobLogTable.getTableName())) {
        Database db = new Database(this, jobLogTable.getDatabaseMeta());
        try {
            db.connect();
            RowMetaInterface fields = jobLogTable.getLogRecord(LogStatus.START, null, null).getRowMeta();
            String sql = db.getDDL(jobLogTable.getTableName(), fields);
            if (sql != null && sql.length() > 0) {
                SQLStatement stat = new SQLStatement(BaseMessages.getString(PKG, "JobMeta.SQLFeedback.ThisJob"), jobLogTable.getDatabaseMeta(), sql);
                stats.add(stat);
            }
        } catch (KettleDatabaseException dbe) {
            SQLStatement stat = new SQLStatement(BaseMessages.getString(PKG, "JobMeta.SQLFeedback.ThisJob"), jobLogTable.getDatabaseMeta(), null);
            stat.setError(BaseMessages.getString(PKG, "JobMeta.SQLFeedback.ErrorObtainingJobLogTableInfo") + dbe.getMessage());
            stats.add(stat);
        } finally {
            db.disconnect();
        }
    }
    if (monitor != null) {
        monitor.worked(1);
    }
    if (monitor != null) {
        monitor.done();
    }
    return stats;
}
Also used : JobEntryCopy(org.pentaho.di.job.entry.JobEntryCopy) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) ArrayList(java.util.ArrayList) Database(org.pentaho.di.core.database.Database) RowMetaInterface(org.pentaho.di.core.row.RowMetaInterface) SQLStatement(org.pentaho.di.core.SQLStatement) Point(org.pentaho.di.core.gui.Point) KettleExtensionPoint(org.pentaho.di.core.extension.KettleExtensionPoint)

Example 7 with SQLStatement

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

the class SQLStatementsDialog method exec.

private void exec() {
    int[] idx = wFields.table.getSelectionIndices();
    // None selected: don't waste users time: select them all!
    if (idx.length == 0) {
        idx = new int[stats.size()];
        for (int i = 0; i < stats.size(); i++) {
            idx[i] = i;
        }
    }
    int errors = 0;
    for (int i = 0; i < idx.length; i++) {
        SQLStatement stat = stats.get(idx[i]);
        if (stat.hasError()) {
            errors++;
        }
    }
    if (errors == 0) {
        for (int i = 0; i < idx.length; i++) {
            SQLStatement stat = stats.get(idx[i]);
            DatabaseMeta di = stat.getDatabase();
            if (di != null && !stat.hasError()) {
                Database db = new Database(loggingObject, di);
                try {
                    db.connect();
                    try {
                        db.execStatements(stat.getSQL());
                    } catch (KettleDatabaseException dbe) {
                        errors++;
                        new ErrorDialog(shell, BaseMessages.getString(PKG, "SQLStatementDialog.Error.Title"), BaseMessages.getString(PKG, "SQLStatementDialog.Error.CouldNotExec", stat.getSQL()), dbe);
                    }
                } catch (KettleDatabaseException dbe) {
                    new ErrorDialog(shell, BaseMessages.getString(PKG, "SQLStatementDialog.Error.Title"), BaseMessages.getString(PKG, "SQLStatementDialog.Error.CouldNotConnect", (di == null ? "" : di.getName())), dbe);
                } finally {
                    db.disconnect();
                }
            }
        }
        if (errors == 0) {
            MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ICON_INFORMATION);
            mb.setMessage(BaseMessages.getString(PKG, "SQLStatementDialog.Success.Message", Integer.toString(idx.length)));
            mb.setText(BaseMessages.getString(PKG, "SQLStatementDialog.Success.Title"));
            mb.open();
        }
    } else {
        MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ICON_ERROR);
        mb.setMessage(BaseMessages.getString(PKG, "SQLStatementDialog.Error.Message", Integer.toString(errors)));
        mb.setText(BaseMessages.getString(PKG, "SQLStatementDialog.Error.Title"));
        mb.open();
    }
}
Also used : KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) Database(org.pentaho.di.core.database.Database) SQLStatement(org.pentaho.di.core.SQLStatement) DatabaseMeta(org.pentaho.di.core.database.DatabaseMeta) MessageBox(org.eclipse.swt.widgets.MessageBox)

Example 8 with SQLStatement

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

the class TransMeta method getSQLStatements.

/**
 * Builds a list of all the SQL statements that this transformation needs in order to work properly.
 *
 * @param monitor
 *          a progress monitor listener to be updated as the SQL statements are generated
 * @return An ArrayList of SQLStatement objects.
 * @throws KettleStepException
 *           if any errors occur during SQL statement generation
 */
public List<SQLStatement> getSQLStatements(ProgressMonitorListener monitor) throws KettleStepException {
    if (monitor != null) {
        monitor.beginTask(BaseMessages.getString(PKG, "TransMeta.Monitor.GettingTheSQLForTransformationTask.Title"), nrSteps() + 1);
    }
    List<SQLStatement> stats = new ArrayList<>();
    for (int i = 0; i < nrSteps(); i++) {
        StepMeta stepMeta = getStep(i);
        if (monitor != null) {
            monitor.subTask(BaseMessages.getString(PKG, "TransMeta.Monitor.GettingTheSQLForStepTask.Title", "" + stepMeta));
        }
        RowMetaInterface prev = getPrevStepFields(stepMeta);
        SQLStatement sqlCompat = compatibleStepMetaGetSQLStatements(stepMeta.getStepMetaInterface(), stepMeta, prev);
        if (sqlCompat.getSQL() != null || sqlCompat.hasError()) {
            stats.add(sqlCompat);
        }
        SQLStatement sql = stepMeta.getStepMetaInterface().getSQLStatements(this, stepMeta, prev, repository, metaStore);
        if (sql.getSQL() != null || sql.hasError()) {
            stats.add(sql);
        }
        if (monitor != null) {
            monitor.worked(1);
        }
    }
    // 
    if (monitor != null) {
        monitor.subTask(BaseMessages.getString(PKG, "TransMeta.Monitor.GettingTheSQLForTransformationTask.Title2"));
    }
    if (transLogTable.getDatabaseMeta() != null && (!Utils.isEmpty(transLogTable.getTableName()) || !Utils.isEmpty(performanceLogTable.getTableName()))) {
        try {
            for (LogTableInterface logTable : new LogTableInterface[] { transLogTable, performanceLogTable, channelLogTable, stepLogTable }) {
                if (logTable.getDatabaseMeta() != null && !Utils.isEmpty(logTable.getTableName())) {
                    Database db = null;
                    try {
                        db = new Database(this, transLogTable.getDatabaseMeta());
                        db.shareVariablesWith(this);
                        db.connect();
                        RowMetaInterface fields = logTable.getLogRecord(LogStatus.START, null, null).getRowMeta();
                        String schemaTable = logTable.getDatabaseMeta().getQuotedSchemaTableCombination(logTable.getSchemaName(), logTable.getTableName());
                        String sql = db.getDDL(schemaTable, fields);
                        if (!Utils.isEmpty(sql)) {
                            SQLStatement stat = new SQLStatement("<this transformation>", transLogTable.getDatabaseMeta(), sql);
                            stats.add(stat);
                        }
                    } catch (Exception e) {
                        throw new KettleDatabaseException("Unable to connect to logging database [" + logTable.getDatabaseMeta() + "]", e);
                    } finally {
                        if (db != null) {
                            db.disconnect();
                        }
                    }
                }
            }
        } catch (KettleDatabaseException dbe) {
            SQLStatement stat = new SQLStatement("<this transformation>", transLogTable.getDatabaseMeta(), null);
            stat.setError(BaseMessages.getString(PKG, "TransMeta.SQLStatement.ErrorDesc.ErrorObtainingTransformationLogTableInfo") + dbe.getMessage());
            stats.add(stat);
        }
    }
    if (monitor != null) {
        monitor.worked(1);
    }
    if (monitor != null) {
        monitor.done();
    }
    return stats;
}
Also used : LogTableInterface(org.pentaho.di.core.logging.LogTableInterface) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) ArrayList(java.util.ArrayList) Database(org.pentaho.di.core.database.Database) RowMetaInterface(org.pentaho.di.core.row.RowMetaInterface) SQLStatement(org.pentaho.di.core.SQLStatement) StepMeta(org.pentaho.di.trans.step.StepMeta) Point(org.pentaho.di.core.gui.Point) KettleExtensionPoint(org.pentaho.di.core.extension.KettleExtensionPoint) KettleXMLException(org.pentaho.di.core.exception.KettleXMLException) KettleRowException(org.pentaho.di.core.exception.KettleRowException) FileSystemException(org.apache.commons.vfs2.FileSystemException) KettleStepException(org.pentaho.di.core.exception.KettleStepException) IOException(java.io.IOException) KettleMissingPluginsException(org.pentaho.di.core.exception.KettleMissingPluginsException) KettleFileException(org.pentaho.di.core.exception.KettleFileException) KettleException(org.pentaho.di.core.exception.KettleException) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException)

Example 9 with SQLStatement

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

the class InsertUpdateMeta method getSQLStatements.

public SQLStatement getSQLStatements(TransMeta transMeta, StepMeta stepMeta, RowMetaInterface prev, Repository repository, IMetaStore metaStore) throws KettleStepException {
    // default: nothing to do!
    SQLStatement retval = new SQLStatement(stepMeta.getName(), databaseMeta, null);
    if (databaseMeta != null) {
        if (prev != null && prev.size() > 0) {
            // Copy the row
            RowMetaInterface tableFields = RowMetaUtils.getRowMetaForUpdate(prev, keyLookup, keyStream, updateLookup, updateStream);
            if (!Utils.isEmpty(tableName)) {
                Database db = new Database(loggingObject, databaseMeta);
                db.shareVariablesWith(transMeta);
                try {
                    db.connect();
                    String schemaTable = databaseMeta.getQuotedSchemaTableCombination(schemaName, tableName);
                    String cr_table = db.getDDL(schemaTable, tableFields, null, false, null, true);
                    String cr_index = "";
                    String[] idx_fields = null;
                    if (keyLookup != null && keyLookup.length > 0) {
                        idx_fields = new String[keyLookup.length];
                        for (int i = 0; i < keyLookup.length; i++) {
                            idx_fields[i] = keyLookup[i];
                        }
                    } else {
                        retval.setError(BaseMessages.getString(PKG, "InsertUpdateMeta.CheckResult.MissingKeyFields"));
                    }
                    // Key lookup dimensions...
                    if (idx_fields != null && idx_fields.length > 0 && !db.checkIndexExists(schemaName, tableName, idx_fields)) {
                        String indexname = "idx_" + tableName + "_lookup";
                        cr_index = db.getCreateIndexStatement(schemaTable, indexname, idx_fields, false, false, false, true);
                    }
                    String sql = cr_table + cr_index;
                    if (sql.length() == 0) {
                        retval.setSQL(null);
                    } else {
                        retval.setSQL(sql);
                    }
                } catch (KettleException e) {
                    retval.setError(BaseMessages.getString(PKG, "InsertUpdateMeta.ReturnValue.ErrorOccurred") + e.getMessage());
                }
            } else {
                retval.setError(BaseMessages.getString(PKG, "InsertUpdateMeta.ReturnValue.NoTableDefinedOnConnection"));
            }
        } else {
            retval.setError(BaseMessages.getString(PKG, "InsertUpdateMeta.ReturnValue.NotReceivingAnyFields"));
        }
    } else {
        retval.setError(BaseMessages.getString(PKG, "InsertUpdateMeta.ReturnValue.NoConnectionDefined"));
    }
    return retval;
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) Database(org.pentaho.di.core.database.Database) RowMetaInterface(org.pentaho.di.core.row.RowMetaInterface) SQLStatement(org.pentaho.di.core.SQLStatement)

Example 10 with SQLStatement

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

the class MonetDBBulkLoader method autoAdjustSchema.

public void autoAdjustSchema(MonetDBBulkLoaderMeta meta) throws KettleException {
    if (log.isDetailed()) {
        logDetailed("Attempting to auto adjust table structure");
    }
    drop();
    if (log.isDetailed()) {
        logDetailed("getTransMeta: " + getTransMeta());
    }
    if (log.isDetailed()) {
        logDetailed("getStepname: " + getStepname());
    }
    SQLStatement statement = meta.getTableDdl(getTransMeta(), getStepname(), true, data, true);
    if (log.isDetailed()) {
        logDetailed("Statement: " + statement);
    }
    if (log.isDetailed() && statement != null) {
        logDetailed("Statement has SQL: " + statement.hasSQL());
    }
    if (statement != null && statement.hasSQL()) {
        String cmd = statement.getSQL();
        try {
            executeSql(cmd);
        } catch (Exception e) {
            throw new KettleException("Error while creating table " + data.schemaTable, e);
        }
    }
    if (log.isDetailed()) {
        logDetailed("Successfull");
    }
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) SQLStatement(org.pentaho.di.core.SQLStatement) KettleException(org.pentaho.di.core.exception.KettleException) MonetDBRowLimitException(org.pentaho.di.trans.steps.monetdbagilemart.MonetDBRowLimitException)

Aggregations

SQLStatement (org.pentaho.di.core.SQLStatement)49 RowMetaInterface (org.pentaho.di.core.row.RowMetaInterface)35 KettleException (org.pentaho.di.core.exception.KettleException)33 StepMeta (org.pentaho.di.trans.step.StepMeta)22 Database (org.pentaho.di.core.database.Database)21 MessageBox (org.eclipse.swt.widgets.MessageBox)18 BaseStepMeta (org.pentaho.di.trans.step.BaseStepMeta)16 SQLEditor (org.pentaho.di.ui.core.database.dialog.SQLEditor)16 ErrorDialog (org.pentaho.di.ui.core.dialog.ErrorDialog)16 ValueMetaInterface (org.pentaho.di.core.row.ValueMetaInterface)15 RowMeta (org.pentaho.di.core.row.RowMeta)12 KettleStepException (org.pentaho.di.core.exception.KettleStepException)10 KettleDatabaseException (org.pentaho.di.core.exception.KettleDatabaseException)9 DatabaseMeta (org.pentaho.di.core.database.DatabaseMeta)6 TransMeta (org.pentaho.di.trans.TransMeta)4 KettleExtensionPoint (org.pentaho.di.core.extension.KettleExtensionPoint)3 Point (org.pentaho.di.core.gui.Point)3 ValueMetaInteger (org.pentaho.di.core.row.value.ValueMetaInteger)3 Repository (org.pentaho.di.repository.Repository)3 ArrayList (java.util.ArrayList)2