Search in sources :

Example 61 with Database

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

the class JobEntrySQL method execute.

public Result execute(Result result, int nr) {
    if (databaseMeta != null) {
        try (Database db = new Database(this, databaseMeta)) {
            String theSql = sqlFromFile ? buildSqlFromFile() : sql;
            if (Utils.isEmpty(theSql)) {
                return result;
            }
            db.shareVariablesWith(this);
            db.connect(parentJob.getTransactionId(), null);
            // let it run
            if (useVariableSubstitution) {
                theSql = environmentSubstitute(theSql);
            }
            if (isDetailed()) {
                logDetailed(BaseMessages.getString(PKG, "JobSQL.Log.SQlStatement", theSql));
            }
            if (sendOneStatement) {
                db.execStatement(theSql);
            } else {
                db.execStatements(theSql);
            }
        } catch (KettleDatabaseException je) {
            result.setNrErrors(1);
            logError(BaseMessages.getString(PKG, "JobSQL.ErrorRunJobEntry", je.getMessage()));
        }
    } else {
        result.setNrErrors(1);
        logError(BaseMessages.getString(PKG, "JobSQL.NoDatabaseConnection"));
    }
    result.setResult(result.getNrErrors() == 0);
    return result;
}
Also used : KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) Database(org.pentaho.di.core.database.Database)

Example 62 with Database

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

the class JobEntryTableExists method execute.

public Result execute(Result previousResult, int nr) {
    Result result = previousResult;
    result.setResult(false);
    if (connection != null) {
        Database db = new Database(this, connection);
        db.shareVariablesWith(this);
        try {
            db.connect(parentJob.getTransactionId(), null);
            String realTablename = environmentSubstitute(tablename);
            String realSchemaname = environmentSubstitute(schemaname);
            if (db.checkTableExists(realSchemaname, realTablename)) {
                if (log.isDetailed()) {
                    logDetailed(BaseMessages.getString(PKG, "TableExists.Log.TableExists", realTablename));
                }
                result.setResult(true);
            } else {
                if (log.isDetailed()) {
                    logDetailed(BaseMessages.getString(PKG, "TableExists.Log.TableNotExists", realTablename));
                }
            }
        } catch (KettleDatabaseException dbe) {
            result.setNrErrors(1);
            logError(BaseMessages.getString(PKG, "TableExists.Error.RunningJobEntry", dbe.getMessage()));
        } finally {
            if (db != null) {
                try {
                    db.disconnect();
                } catch (Exception e) {
                /* Ignore */
                }
            }
        }
    } else {
        result.setNrErrors(1);
        logError(BaseMessages.getString(PKG, "TableExists.Error.NoConnectionDefined"));
    }
    return result;
}
Also used : KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) Database(org.pentaho.di.core.database.Database) KettleException(org.pentaho.di.core.exception.KettleException) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) KettleXMLException(org.pentaho.di.core.exception.KettleXMLException) Result(org.pentaho.di.core.Result)

Example 63 with Database

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

the class OraBulkLoaderMeta method getRequiredFields.

public RowMetaInterface getRequiredFields(VariableSpace space) throws KettleException {
    String realTableName = space.environmentSubstitute(tableName);
    String realSchemaName = space.environmentSubstitute(schemaName);
    if (databaseMeta != null) {
        Database db = new Database(loggingObject, databaseMeta);
        try {
            db.connect();
            if (!Utils.isEmpty(realTableName)) {
                String schemaTable = databaseMeta.getQuotedSchemaTableCombination(realSchemaName, realTableName);
                // Check if this table exists...
                if (db.checkTableExists(schemaTable)) {
                    return db.getTableFields(schemaTable);
                } else {
                    throw new KettleException(BaseMessages.getString(PKG, "OraBulkLoaderMeta.Exception.TableNotFound"));
                }
            } else {
                throw new KettleException(BaseMessages.getString(PKG, "OraBulkLoaderMeta.Exception.TableNotSpecified"));
            }
        } catch (Exception e) {
            throw new KettleException(BaseMessages.getString(PKG, "OraBulkLoaderMeta.Exception.ErrorGettingFields"), e);
        } finally {
            db.disconnect();
        }
    } else {
        throw new KettleException(BaseMessages.getString(PKG, "OraBulkLoaderMeta.Exception.ConnectionNotDefined"));
    }
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) Database(org.pentaho.di.core.database.Database) KettleException(org.pentaho.di.core.exception.KettleException) KettleXMLException(org.pentaho.di.core.exception.KettleXMLException) KettleStepException(org.pentaho.di.core.exception.KettleStepException)

Example 64 with Database

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

the class PGBulkLoader method getDatabase.

@VisibleForTesting
Database getDatabase(LoggingObjectInterface parentObject, PGBulkLoaderMeta pgBulkLoaderMeta) {
    DatabaseMeta dbMeta = pgBulkLoaderMeta.getDatabaseMeta();
    // If dbNameOverride is present, clone the origin db meta and override the DB name
    String dbNameOverride = environmentSubstitute(pgBulkLoaderMeta.getDbNameOverride());
    if (!Utils.isEmpty(dbNameOverride)) {
        dbMeta = (DatabaseMeta) pgBulkLoaderMeta.getDatabaseMeta().clone();
        dbMeta.setDBName(dbNameOverride.trim());
        logDebug("DB name overridden to the value: " + dbNameOverride);
    }
    return new Database(parentObject, dbMeta);
}
Also used : Database(org.pentaho.di.core.database.Database) DatabaseMeta(org.pentaho.di.core.database.DatabaseMeta) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 65 with Database

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

the class PGBulkLoaderMeta method check.

public void check(List<CheckResultInterface> remarks, TransMeta transMeta, StepMeta stepMeta, RowMetaInterface prev, String[] input, String[] output, RowMetaInterface info, VariableSpace space, Repository repository, IMetaStore metaStore) {
    CheckResult cr;
    String error_message = "";
    if (databaseMeta != null) {
        Database db = new Database(loggingObject, databaseMeta);
        db.shareVariablesWith(transMeta);
        try {
            db.connect();
            if (!Utils.isEmpty(tableName)) {
                cr = new CheckResult(CheckResultInterface.TYPE_RESULT_OK, BaseMessages.getString(PKG, "GPBulkLoaderMeta.CheckResult.TableNameOK"), stepMeta);
                remarks.add(cr);
                boolean first = true;
                boolean error_found = false;
                error_message = "";
                // Check fields in table
                String schemaTable = databaseMeta.getQuotedSchemaTableCombination(transMeta.environmentSubstitute(schemaName), transMeta.environmentSubstitute(tableName));
                RowMetaInterface r = db.getTableFields(schemaTable);
                if (r != null) {
                    cr = new CheckResult(CheckResultInterface.TYPE_RESULT_OK, BaseMessages.getString(PKG, "GPBulkLoaderMeta.CheckResult.TableExists"), stepMeta);
                    remarks.add(cr);
                    // How about the fields to insert/dateMask in the table?
                    first = true;
                    error_found = false;
                    error_message = "";
                    for (int i = 0; i < fieldTable.length; i++) {
                        String field = fieldTable[i];
                        ValueMetaInterface v = r.searchValueMeta(field);
                        if (v == null) {
                            if (first) {
                                first = false;
                                error_message += BaseMessages.getString(PKG, "GPBulkLoaderMeta.CheckResult.MissingFieldsToLoadInTargetTable") + Const.CR;
                            }
                            error_found = true;
                            error_message += "\t\t" + field + Const.CR;
                        }
                    }
                    if (error_found) {
                        cr = new CheckResult(CheckResultInterface.TYPE_RESULT_ERROR, error_message, stepMeta);
                    } else {
                        cr = new CheckResult(CheckResultInterface.TYPE_RESULT_OK, BaseMessages.getString(PKG, "GPBulkLoaderMeta.CheckResult.AllFieldsFoundInTargetTable"), stepMeta);
                    }
                    remarks.add(cr);
                } else {
                    error_message = BaseMessages.getString(PKG, "GPBulkLoaderMeta.CheckResult.CouldNotReadTableInfo");
                    cr = new CheckResult(CheckResultInterface.TYPE_RESULT_ERROR, error_message, stepMeta);
                    remarks.add(cr);
                }
            }
            // Look up fields in the input stream <prev>
            if (prev != null && prev.size() > 0) {
                cr = new CheckResult(CheckResultInterface.TYPE_RESULT_OK, BaseMessages.getString(PKG, "GPBulkLoaderMeta.CheckResult.StepReceivingDatas", prev.size() + ""), stepMeta);
                remarks.add(cr);
                boolean first = true;
                error_message = "";
                boolean error_found = false;
                for (int i = 0; i < fieldStream.length; i++) {
                    ValueMetaInterface v = prev.searchValueMeta(fieldStream[i]);
                    if (v == null) {
                        if (first) {
                            first = false;
                            error_message += BaseMessages.getString(PKG, "GPBulkLoaderMeta.CheckResult.MissingFieldsInInput") + Const.CR;
                        }
                        error_found = true;
                        error_message += "\t\t" + fieldStream[i] + Const.CR;
                    }
                }
                if (error_found) {
                    cr = new CheckResult(CheckResultInterface.TYPE_RESULT_ERROR, error_message, stepMeta);
                } else {
                    cr = new CheckResult(CheckResultInterface.TYPE_RESULT_OK, BaseMessages.getString(PKG, "GPBulkLoaderMeta.CheckResult.AllFieldsFoundInInput"), stepMeta);
                }
                remarks.add(cr);
            } else {
                error_message = BaseMessages.getString(PKG, "GPBulkLoaderMeta.CheckResult.MissingFieldsInInput3") + Const.CR;
                cr = new CheckResult(CheckResultInterface.TYPE_RESULT_ERROR, error_message, stepMeta);
                remarks.add(cr);
            }
        } catch (KettleException e) {
            error_message = BaseMessages.getString(PKG, "GPBulkLoaderMeta.CheckResult.DatabaseErrorOccurred") + e.getMessage();
            cr = new CheckResult(CheckResultInterface.TYPE_RESULT_ERROR, error_message, stepMeta);
            remarks.add(cr);
        } finally {
            db.disconnect();
        }
    } else {
        error_message = BaseMessages.getString(PKG, "GPBulkLoaderMeta.CheckResult.InvalidConnection");
        cr = new CheckResult(CheckResultInterface.TYPE_RESULT_ERROR, error_message, stepMeta);
        remarks.add(cr);
    }
    // See if we have input streams leading to this step!
    if (input.length > 0) {
        cr = new CheckResult(CheckResultInterface.TYPE_RESULT_OK, BaseMessages.getString(PKG, "GPBulkLoaderMeta.CheckResult.StepReceivingInfoFromOtherSteps"), stepMeta);
        remarks.add(cr);
    } else {
        cr = new CheckResult(CheckResultInterface.TYPE_RESULT_ERROR, BaseMessages.getString(PKG, "GPBulkLoaderMeta.CheckResult.NoInputError"), stepMeta);
        remarks.add(cr);
    }
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) CheckResult(org.pentaho.di.core.CheckResult) Database(org.pentaho.di.core.database.Database) RowMetaInterface(org.pentaho.di.core.row.RowMetaInterface) ValueMetaInterface(org.pentaho.di.core.row.ValueMetaInterface)

Aggregations

Database (org.pentaho.di.core.database.Database)238 KettleException (org.pentaho.di.core.exception.KettleException)135 DatabaseMeta (org.pentaho.di.core.database.DatabaseMeta)90 RowMetaInterface (org.pentaho.di.core.row.RowMetaInterface)82 KettleDatabaseException (org.pentaho.di.core.exception.KettleDatabaseException)62 ValueMetaInterface (org.pentaho.di.core.row.ValueMetaInterface)46 ErrorDialog (org.pentaho.di.ui.core.dialog.ErrorDialog)32 KettleStepException (org.pentaho.di.core.exception.KettleStepException)30 MessageBox (org.eclipse.swt.widgets.MessageBox)28 CheckResult (org.pentaho.di.core.CheckResult)25 ValueMetaString (org.pentaho.di.core.row.value.ValueMetaString)25 KettleXMLException (org.pentaho.di.core.exception.KettleXMLException)24 RowMeta (org.pentaho.di.core.row.RowMeta)22 SQLStatement (org.pentaho.di.core.SQLStatement)21 EnterSelectionDialog (org.pentaho.di.ui.core.dialog.EnterSelectionDialog)21 Test (org.junit.Test)20 ArrayList (java.util.ArrayList)18 KettleValueException (org.pentaho.di.core.exception.KettleValueException)17 RowMetaAndData (org.pentaho.di.core.RowMetaAndData)16 ColumnInfo (org.pentaho.di.ui.core.widget.ColumnInfo)15