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;
}
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;
}
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"));
}
}
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);
}
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);
}
}
Aggregations