Search in sources :

Example 26 with Database

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;
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) RowMeta(org.pentaho.di.core.row.RowMeta) Database(org.pentaho.di.core.database.Database) KettleException(org.pentaho.di.core.exception.KettleException) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) KettleValueException(org.pentaho.di.core.exception.KettleValueException) KettleStepException(org.pentaho.di.core.exception.KettleStepException)

Example 27 with Database

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);
    }
}
Also used : InfobrightNamedPipeLoader(com.infobright.io.InfobrightNamedPipeLoader) SQLException(java.sql.SQLException) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) Connection(java.sql.Connection) Charset(java.nio.charset.Charset) KettleException(org.pentaho.di.core.exception.KettleException) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) SQLException(java.sql.SQLException) EtlLogger(com.infobright.logging.EtlLogger) FileOutputStream(java.io.FileOutputStream) Database(org.pentaho.di.core.database.Database) DataFormat(com.infobright.etl.model.DataFormat)

Example 28 with Database

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);
}
Also used : Database(org.pentaho.di.core.database.Database) Connection(java.sql.Connection) RowMetaInterface(org.pentaho.di.core.row.RowMetaInterface) DatabaseMeta(org.pentaho.di.core.database.DatabaseMeta) Test(org.junit.Test)

Example 29 with Database

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);
    }
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) CheckResult(org.pentaho.di.core.CheckResult) Database(org.pentaho.di.core.database.Database)

Example 30 with Database

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;
}
Also used : KettleException(org.pentaho.di.core.exception.KettleException) Database(org.pentaho.di.core.database.Database) SQLStatement(org.pentaho.di.core.SQLStatement)

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