Search in sources :

Example 1 with SQLStatement

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

the class StagingTransformGenerator method executeSql.

protected void executeSql(TableOutputMeta meta, StepMeta stepMeta, TransMeta transMeta) throws CsvTransformGeneratorException {
    try {
        RowMetaInterface prev = transMeta.getPrevStepFields(TABLE_OUTPUT);
        SQLStatement sqlStatement = meta.getSQLStatements(transMeta, stepMeta, prev, null, false, null);
        if (!sqlStatement.hasError()) {
            if (sqlStatement.hasSQL()) {
                // now we can execute the SQL
                String sqlScript = sqlStatement.getSQL();
                execSqlStatement(sqlScript, meta.getDatabaseMeta(), null);
            } else {
                // No SQL was generated
                // $NON-NLS-1$
                error("No SQL generated");
                // $NON-NLS-1$
                throw new CsvTransformGeneratorException("No SQL generated");
            }
        } else {
            error(sqlStatement.getError());
            throw new CsvTransformGeneratorException(sqlStatement.getError());
        }
    } catch (KettleException ke) {
        // $NON-NLS-1$
        error("Exception encountered", ke);
        throw new CsvTransformGeneratorException("Exception encountered", ke, // $NON-NLS-1$
        getStackTraceAsString(ke));
    }
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) CsvTransformGeneratorException(org.pentaho.platform.dataaccess.datasource.wizard.models.CsvTransformGeneratorException) RowMetaInterface(org.pentaho.di.core.row.RowMetaInterface) SQLStatement(org.pentaho.di.core.SQLStatement)

Example 2 with SQLStatement

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

the class GPBulkLoaderMeta method getSQLStatements.

@Override
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 = new RowMeta();
            // Now change the field names
            for (int i = 0; i < fieldTable.length; i++) {
                ValueMetaInterface v = prev.searchValueMeta(fieldStream[i]);
                if (v != null) {
                    ValueMetaInterface tableField = v.clone();
                    tableField.setName(fieldTable[i]);
                    tableFields.addValueMeta(tableField);
                } else {
                    throw new KettleStepException("Unable to find field [" + fieldStream[i] + "] in the input rows");
                }
            }
            if (!Utils.isEmpty(tableName)) {
                Database db = new Database(loggingObject, databaseMeta);
                db.shareVariablesWith(transMeta);
                try {
                    db.connect();
                    String schemaTable = databaseMeta.getQuotedSchemaTableCombination(transMeta.environmentSubstitute(schemaName), transMeta.environmentSubstitute(tableName));
                    String sql = db.getDDL(schemaTable, tableFields, null, false, null, true);
                    if (sql.length() == 0) {
                        retval.setSQL(null);
                    } else {
                        retval.setSQL(sql);
                    }
                } catch (KettleException e) {
                    retval.setError(BaseMessages.getString(PKG, "GPBulkLoaderMeta.GetSQL.ErrorOccurred") + e.getMessage());
                }
            } else {
                retval.setError(BaseMessages.getString(PKG, "GPBulkLoaderMeta.GetSQL.NoTableDefinedOnConnection"));
            }
        } else {
            retval.setError(BaseMessages.getString(PKG, "GPBulkLoaderMeta.GetSQL.NotReceivingAnyFields"));
        }
    } else {
        retval.setError(BaseMessages.getString(PKG, "GPBulkLoaderMeta.GetSQL.NoConnectionDefined"));
    }
    return retval;
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) KettleStepException(org.pentaho.di.core.exception.KettleStepException) RowMeta(org.pentaho.di.core.row.RowMeta) Database(org.pentaho.di.core.database.Database) RowMetaInterface(org.pentaho.di.core.row.RowMetaInterface) SQLStatement(org.pentaho.di.core.SQLStatement) ValueMetaInterface(org.pentaho.di.core.row.ValueMetaInterface)

Example 3 with SQLStatement

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

the class GPLoadMeta 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 = new RowMeta();
            // Now change the field names
            for (int i = 0; i < fieldTable.length; i++) {
                ValueMetaInterface v = prev.searchValueMeta(fieldStream[i]);
                if (v != null) {
                    ValueMetaInterface tableField = v.clone();
                    tableField.setName(fieldTable[i]);
                    tableFields.addValueMeta(tableField);
                } else {
                    throw new KettleStepException("Unable to find field [" + fieldStream[i] + "] in the input rows");
                }
            }
            if (!Utils.isEmpty(tableName)) {
                Database db = new Database(loggingObject, databaseMeta);
                db.shareVariablesWith(transMeta);
                try {
                    db.connect();
                    String schemaTable = databaseMeta.getQuotedSchemaTableCombination(transMeta.environmentSubstitute(schemaName), transMeta.environmentSubstitute(tableName));
                    String sql = db.getDDL(schemaTable, tableFields, null, false, null, true);
                    if (sql.length() == 0) {
                        retval.setSQL(null);
                    } else {
                        retval.setSQL(sql);
                    }
                } catch (KettleException e) {
                    retval.setError(BaseMessages.getString(PKG, "GPLoadMeta.GetSQL.ErrorOccurred") + e.getMessage());
                }
            } else {
                retval.setError(BaseMessages.getString(PKG, "GPLoadMeta.GetSQL.NoTableDefinedOnConnection"));
            }
        } else {
            retval.setError(BaseMessages.getString(PKG, "GPLoadMeta.GetSQL.NotReceivingAnyFields"));
        }
    } else {
        retval.setError(BaseMessages.getString(PKG, "GPLoadMeta.GetSQL.NoConnectionDefined"));
    }
    return retval;
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) KettleStepException(org.pentaho.di.core.exception.KettleStepException) RowMeta(org.pentaho.di.core.row.RowMeta) Database(org.pentaho.di.core.database.Database) RowMetaInterface(org.pentaho.di.core.row.RowMetaInterface) SQLStatement(org.pentaho.di.core.SQLStatement) ValueMetaInterface(org.pentaho.di.core.row.ValueMetaInterface)

Example 4 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 5 with SQLStatement

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

the class CombinationLookupDialog method create.

/**
 * Generate code for create table. Conversions done by database.
 */
private void create() {
    try {
        // Gather info...
        CombinationLookupMeta info = new CombinationLookupMeta();
        getInfo(info);
        // new name might not yet be linked to other steps!
        String name = stepname;
        StepMeta stepMeta = new StepMeta(BaseMessages.getString(PKG, "CombinationLookupDialog.StepMeta.Title"), name, info);
        RowMetaInterface prev = transMeta.getPrevStepFields(stepname);
        SQLStatement sql = info.getSQLStatements(transMeta, stepMeta, prev, repository, metaStore);
        if (!sql.hasError()) {
            if (sql.hasSQL()) {
                SQLEditor sqledit = new SQLEditor(transMeta, shell, SWT.NONE, info.getDatabaseMeta(), transMeta.getDbCache(), sql.getSQL());
                sqledit.open();
            } else {
                MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ICON_INFORMATION);
                mb.setMessage(BaseMessages.getString(PKG, "CombinationLookupDialog.NoSQLNeeds.DialogMessage"));
                mb.setText(BaseMessages.getString(PKG, "CombinationLookupDialog.NoSQLNeeds.DialogTitle"));
                mb.open();
            }
        } else {
            MessageBox mb = new MessageBox(shell, SWT.OK | SWT.ICON_ERROR);
            mb.setMessage(sql.getError());
            mb.setText(BaseMessages.getString(PKG, "CombinationLookupDialog.SQLError.DialogTitle"));
            mb.open();
        }
    } catch (KettleException ke) {
        new ErrorDialog(shell, BaseMessages.getString(PKG, "CombinationLookupDialog.UnableToCreateSQL.DialogTitle"), BaseMessages.getString(PKG, "CombinationLookupDialog.UnableToCreateSQL.DialogMessage"), ke);
    }
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) SQLEditor(org.pentaho.di.ui.core.database.dialog.SQLEditor) CombinationLookupMeta(org.pentaho.di.trans.steps.combinationlookup.CombinationLookupMeta) ErrorDialog(org.pentaho.di.ui.core.dialog.ErrorDialog) RowMetaInterface(org.pentaho.di.core.row.RowMetaInterface) StepMeta(org.pentaho.di.trans.step.StepMeta) BaseStepMeta(org.pentaho.di.trans.step.BaseStepMeta) SQLStatement(org.pentaho.di.core.SQLStatement) MessageBox(org.eclipse.swt.widgets.MessageBox)

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