use of org.pentaho.di.core.database.Database in project pentaho-kettle by pentaho.
the class GetTableNames method init.
public boolean init(StepMetaInterface smi, StepDataInterface sdi) {
meta = (GetTableNamesMeta) smi;
data = (GetTableNamesData) sdi;
if (super.init(smi, sdi)) {
if (Utils.isEmpty(meta.getTablenameFieldName())) {
logError(BaseMessages.getString(PKG, "GetTableNames.Error.TablenameFieldNameMissing"));
return false;
}
String realSchemaName = environmentSubstitute(meta.getSchemaName());
if (!Utils.isEmpty(realSchemaName)) {
data.realSchemaName = realSchemaName;
}
data.realTableNameFieldName = environmentSubstitute(meta.getTablenameFieldName());
data.realObjectTypeFieldName = environmentSubstitute(meta.getObjectTypeFieldName());
data.realIsSystemObjectFieldName = environmentSubstitute(meta.isSystemObjectFieldName());
data.realSQLCreationFieldName = environmentSubstitute(meta.getSQLCreationFieldName());
if (!meta.isIncludeCatalog() && !meta.isIncludeSchema() && !meta.isIncludeTable() && !meta.isIncludeView() && !meta.isIncludeProcedure() && !meta.isIncludeSynonym()) {
logError(BaseMessages.getString(PKG, "GetTableNames.Error.includeAtLeastOneType"));
return false;
}
try {
// Create the output row meta-data
data.outputRowMeta = new RowMeta();
// get the
meta.getFields(data.outputRowMeta, getStepname(), null, null, this, repository, metaStore);
// metadata
// populated
} catch (Exception e) {
logError("Error initializing step: " + e.toString());
logError(Const.getStackTracker(e));
return false;
}
data.db = new Database(this, meta.getDatabase());
data.db.shareVariablesWith(this);
try {
if (getTransMeta().isUsingUniqueConnections()) {
synchronized (getTrans()) {
data.db.connect(getTrans().getTransactionId(), getPartitionID());
}
} else {
data.db.connect(getPartitionID());
}
if (log.isDetailed()) {
logDetailed(BaseMessages.getString(PKG, "GetTableNames.Log.ConnectedToDB"));
}
return true;
} catch (KettleException e) {
logError(BaseMessages.getString(PKG, "GetTableNames.Log.DBException") + e.getMessage());
if (data.db != null) {
data.db.disconnect();
}
}
}
return false;
}
use of org.pentaho.di.core.database.Database in project pentaho-kettle by pentaho.
the class InfobrightLoaderData method databaseSetup.
void databaseSetup(InfobrightLoaderMeta meta, InfobrightLoader step) throws KettleException {
db = new Database(step, meta.getDatabaseMeta());
db.connect();
// FIXME: This will fail if the first row of the table contains a value that
// cannot be read by Java. For example, a DATE field that contains the value
// '0000-00-00'. In this case, the Kettle error message will misleadingly say
// that the table doesn't exist. There doesn't seem to be any workaround.
// See Pentaho JIRA: PDI-2117.
//
requiredRowMeta = meta.getRequiredFields(step);
requiredFields = requiredRowMeta.getFieldNames();
try {
// the loader is using it and any other uses of the connection will block.
if (meta.getInfobrightProductType() == null) {
// default for ICE
meta.setDataFormat(DataFormat.TXT_VARIABLE);
}
DataFormat dataFormat = DataFormat.valueForDisplayName(meta.getInfobrightProductType());
int agentPort = meta.getAgentPort();
Charset charset = meta.getCharset();
Connection conn = db.getConnection();
String tableName = meta.getDatabaseMeta().getQuotedSchemaTableCombination(step.environmentSubstitute(meta.getSchemaName()), step.environmentSubstitute(meta.getTableName()));
EtlLogger logger = new KettleEtlLogger(step);
loader = new InfobrightNamedPipeLoader(tableName, conn, logger, dataFormat, charset, agentPort);
loader.setTimeout(30);
String debugFile = meta.getDebugFile();
if (debugFile != null) {
OutputStream debugOutputStream = new FileOutputStream(debugFile);
loader.setDebugOutputStream(debugOutputStream);
}
// TODO set to true to support error path
record = loader.createRecord(false);
loader.start();
} catch (Exception e) {
db.disconnect();
db = null;
if (loader != null) {
try {
loader.killQuery();
} catch (SQLException e1) {
throw new KettleDatabaseException(e1);
}
}
throw new KettleDatabaseException(e);
}
}
use of org.pentaho.di.core.database.Database in project pentaho-kettle by pentaho.
the class InsertUpdateMetaTest method keyStream2ProcessRow.
// PDI-16349
@Test
public void keyStream2ProcessRow() throws KettleException {
InsertUpdate insertUpdateStep = new InsertUpdate(mockHelper.stepMeta, mockHelper.stepDataInterface, 0, mockHelper.transMeta, mockHelper.trans);
insertUpdateStep.setInputRowMeta(Mockito.mock(RowMetaInterface.class));
insertUpdateStep = Mockito.spy(insertUpdateStep);
InsertUpdateMeta insertUpdateMeta = new InsertUpdateMeta();
insertUpdateMeta.setKeyStream(new String[] { "test_field" });
insertUpdateMeta.setKeyCondition(new String[] { "test_condition" });
insertUpdateMeta.setKeyStream2(new String[] {});
insertUpdateMeta.setUpdateLookup(new String[] {});
insertUpdateMeta.setKeyLookup(new String[] {});
insertUpdateMeta.setUpdateBypassed(true);
insertUpdateMeta.setDatabaseMeta(Mockito.mock(DatabaseMeta.class));
Database database = Mockito.mock(Database.class);
mockHelper.processRowsStepDataInterface.db = database;
Mockito.doReturn(Mockito.mock(Connection.class)).when(database).getConnection();
Mockito.doNothing().when(insertUpdateStep).lookupValues(Mockito.any(), Mockito.any());
Mockito.doNothing().when(insertUpdateStep).putRow(Mockito.any(), Mockito.any());
Mockito.doReturn(new Object[] {}).when(insertUpdateStep).getRow();
insertUpdateStep.first = true;
insertUpdateMeta.afterInjectionSynchronization();
// run without a exception
insertUpdateStep.processRow(insertUpdateMeta, mockHelper.processRowsStepDataInterface);
Assert.assertEquals(insertUpdateMeta.getKeyStream().length, insertUpdateMeta.getKeyStream2().length);
}
use of org.pentaho.di.core.database.Database in project pentaho-kettle by pentaho.
the class AddSequenceMeta method check.
@Override
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;
if (useDatabase) {
Database db = new Database(loggingObject, database);
db.shareVariablesWith(transMeta);
try {
db.connect();
if (db.checkSequenceExists(transMeta.environmentSubstitute(schemaName), transMeta.environmentSubstitute(sequenceName))) {
cr = new CheckResult(CheckResultInterface.TYPE_RESULT_OK, BaseMessages.getString(PKG, "AddSequenceMeta.CheckResult.SequenceExists.Title"), stepMeta);
} else {
cr = new CheckResult(CheckResultInterface.TYPE_RESULT_ERROR, BaseMessages.getString(PKG, "AddSequenceMeta.CheckResult.SequenceCouldNotBeFound.Title", sequenceName), stepMeta);
}
} catch (KettleException e) {
cr = new CheckResult(CheckResultInterface.TYPE_RESULT_ERROR, BaseMessages.getString(PKG, "AddSequenceMeta.CheckResult.UnableToConnectDB.Title") + Const.CR + e.getMessage(), stepMeta);
} finally {
db.disconnect();
}
remarks.add(cr);
}
if (input.length > 0) {
cr = new CheckResult(CheckResultInterface.TYPE_RESULT_OK, BaseMessages.getString(PKG, "AddSequenceMeta.CheckResult.StepIsReceving.Title"), stepMeta);
remarks.add(cr);
} else {
cr = new CheckResult(CheckResultInterface.TYPE_RESULT_ERROR, BaseMessages.getString(PKG, "AddSequenceMeta.CheckResult.NoInputReceived.Title"), stepMeta);
remarks.add(cr);
}
}
use of org.pentaho.di.core.database.Database in project pentaho-kettle by pentaho.
the class AddSequenceMeta method getSQLStatements.
@Override
public SQLStatement getSQLStatements(TransMeta transMeta, StepMeta stepMeta, RowMetaInterface prev, Repository repository, IMetaStore metaStore) {
// default: nothing to do!
SQLStatement retval = new SQLStatement(stepMeta.getName(), database, null);
if (useDatabase) {
// Otherwise, don't bother!
if (database != null) {
Database db = new Database(loggingObject, database);
db.shareVariablesWith(transMeta);
try {
db.connect();
if (!db.checkSequenceExists(schemaName, sequenceName)) {
String cr_table = db.getCreateSequenceStatement(sequenceName, startAt, incrementBy, maxValue, true);
retval.setSQL(cr_table);
} else {
// Empty string means: nothing to do: set it to null...
retval.setSQL(null);
}
} catch (KettleException e) {
retval.setError(BaseMessages.getString(PKG, "AddSequenceMeta.ErrorMessage.UnableToConnectDB") + Const.CR + e.getMessage());
} finally {
db.disconnect();
}
} else {
retval.setError(BaseMessages.getString(PKG, "AddSequenceMeta.ErrorMessage.NoConnectionDefined"));
}
}
return retval;
}
Aggregations