Search in sources :

Example 71 with DatabaseMeta

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

the class SynchronizeAfterMergeTest method initWithCommitSizeVariable.

@Test
public void initWithCommitSizeVariable() {
    StepMeta stepMeta = mock(StepMeta.class);
    doReturn(STEP_NAME).when(stepMeta).getName();
    doReturn(1).when(stepMeta).getCopies();
    SynchronizeAfterMergeMeta smi = mock(SynchronizeAfterMergeMeta.class);
    SynchronizeAfterMergeData sdi = mock(SynchronizeAfterMergeData.class);
    DatabaseMeta dbMeta = mock(DatabaseMeta.class);
    doReturn(mock(MySQLDatabaseMeta.class)).when(dbMeta).getDatabaseInterface();
    doReturn(dbMeta).when(smi).getDatabaseMeta();
    doReturn("${commit.size}").when(smi).getCommitSize();
    TransMeta transMeta = mock(TransMeta.class);
    doReturn("1").when(transMeta).getVariable(Const.INTERNAL_VARIABLE_SLAVE_SERVER_NUMBER);
    doReturn("2").when(transMeta).getVariable(Const.INTERNAL_VARIABLE_CLUSTER_SIZE);
    doReturn("Y").when(transMeta).getVariable(Const.INTERNAL_VARIABLE_CLUSTER_MASTER);
    doReturn(stepMeta).when(transMeta).findStep(STEP_NAME);
    SynchronizeAfterMerge step = mock(SynchronizeAfterMerge.class);
    doCallRealMethod().when(step).setTransMeta(any(TransMeta.class));
    doCallRealMethod().when(step).setStepMeta(any(StepMeta.class));
    doCallRealMethod().when(step).init(any(StepMetaInterface.class), any(StepDataInterface.class));
    doReturn(stepMeta).when(step).getStepMeta();
    doReturn(transMeta).when(step).getTransMeta();
    doReturn("120").when(step).environmentSubstitute("${commit.size}");
    step.setTransMeta(transMeta);
    step.setStepMeta(stepMeta);
    step.init(smi, sdi);
    assertEquals(120, sdi.commitSize);
}
Also used : TransMeta(org.pentaho.di.trans.TransMeta) StepMetaInterface(org.pentaho.di.trans.step.StepMetaInterface) StepMeta(org.pentaho.di.trans.step.StepMeta) DatabaseMeta(org.pentaho.di.core.database.DatabaseMeta) MySQLDatabaseMeta(org.pentaho.di.core.database.MySQLDatabaseMeta) StepDataInterface(org.pentaho.di.trans.step.StepDataInterface) MySQLDatabaseMeta(org.pentaho.di.core.database.MySQLDatabaseMeta) Test(org.junit.Test)

Example 72 with DatabaseMeta

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

the class TableInputMetaTest method testGetFields.

@Test
public void testGetFields() throws Exception {
    TableInputMetaHandler meta = new TableInputMetaHandler();
    meta.setLazyConversionActive(true);
    DatabaseMeta dbMeta = mock(DatabaseMeta.class);
    meta.setDatabaseMeta(dbMeta);
    Database mockDB = meta.getDatabase();
    when(mockDB.getQueryFields(anyString(), anyBoolean())).thenReturn(createMockFields());
    RowMetaInterface expectedRowMeta = new RowMeta();
    ValueMetaInterface valueMeta = new ValueMetaString("field1");
    valueMeta.setStorageMetadata(new ValueMetaString("field1"));
    valueMeta.setStorageType(ValueMetaInterface.STORAGE_TYPE_BINARY_STRING);
    expectedRowMeta.addValueMeta(valueMeta);
    VariableSpace space = mock(VariableSpace.class);
    RowMetaInterface rowMetaInterface = new RowMeta();
    meta.getFields(rowMetaInterface, "TABLE_INPUT_META", null, null, space, null, null);
    assertEquals(expectedRowMeta.toString(), rowMetaInterface.toString());
}
Also used : ValueMetaString(org.pentaho.di.core.row.value.ValueMetaString) RowMeta(org.pentaho.di.core.row.RowMeta) VariableSpace(org.pentaho.di.core.variables.VariableSpace) Database(org.pentaho.di.core.database.Database) RowMetaInterface(org.pentaho.di.core.row.RowMetaInterface) DatabaseMeta(org.pentaho.di.core.database.DatabaseMeta) ValueMetaInterface(org.pentaho.di.core.row.ValueMetaInterface) Test(org.junit.Test)

Example 73 with DatabaseMeta

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

the class TableOutputMetaTest method testSupportsErrorHandling.

@Test
public void testSupportsErrorHandling() throws Exception {
    TableOutputMeta tableOutputMeta = new TableOutputMeta();
    DatabaseMeta dbMeta = mock(DatabaseMeta.class);
    tableOutputMeta.setDatabaseMeta(dbMeta);
    DatabaseInterface databaseInterface = mock(DatabaseInterface.class);
    when(dbMeta.getDatabaseInterface()).thenReturn(databaseInterface);
    when(databaseInterface.supportsErrorHandling()).thenReturn(true, false);
    assertTrue(tableOutputMeta.supportsErrorHandling());
    assertFalse(tableOutputMeta.supportsErrorHandling());
    tableOutputMeta.setDatabaseMeta(null);
    assertTrue(tableOutputMeta.supportsErrorHandling());
}
Also used : DatabaseInterface(org.pentaho.di.core.database.DatabaseInterface) DatabaseMeta(org.pentaho.di.core.database.DatabaseMeta) Test(org.junit.Test)

Example 74 with DatabaseMeta

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

the class TableOutputMetaTest method testIsReturningGeneratedKeys.

@Test
public void testIsReturningGeneratedKeys() throws Exception {
    TableOutputMeta tableOutputMeta = new TableOutputMeta(), tableOutputMetaSpy = spy(tableOutputMeta);
    DatabaseMeta databaseMeta = mock(DatabaseMeta.class);
    doReturn(true).when(databaseMeta).supportsAutoGeneratedKeys();
    doReturn(databaseMeta).when(tableOutputMetaSpy).getDatabaseMeta();
    tableOutputMetaSpy.setReturningGeneratedKeys(true);
    assertTrue(tableOutputMetaSpy.isReturningGeneratedKeys());
    doReturn(false).when(databaseMeta).supportsAutoGeneratedKeys();
    assertFalse(tableOutputMetaSpy.isReturningGeneratedKeys());
    tableOutputMetaSpy.setReturningGeneratedKeys(true);
    assertFalse(tableOutputMetaSpy.isReturningGeneratedKeys());
    tableOutputMetaSpy.setReturningGeneratedKeys(false);
    assertFalse(tableOutputMetaSpy.isReturningGeneratedKeys());
}
Also used : DatabaseMeta(org.pentaho.di.core.database.DatabaseMeta) Test(org.junit.Test)

Example 75 with DatabaseMeta

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

the class DimensionLookup method setDimLookup.

/**
 * table: dimension table keys[]: which dim-fields do we use to look up key? retval: name of the key to return
 * datefield: do we have a datefield? datefrom, dateto: date-range, if any.
 */
private void setDimLookup(RowMetaInterface rowMeta) throws KettleDatabaseException {
    DatabaseMeta databaseMeta = meta.getDatabaseMeta();
    data.lookupRowMeta = new RowMeta();
    /*
     * DEFAULT, SYSDATE, START_TRANS, COLUMN_VALUE :
     *
     * SELECT <tk>, <version>, ... , FROM <table> WHERE key1=keys[1] AND key2=keys[2] ... AND <datefrom> <= <datefield>
     * AND <dateto> > <datefield> ;
     *
     * NULL :
     *
     * SELECT <tk>, <version>, ... , FROM <table> WHERE key1=keys[1] AND key2=keys[2] ... AND ( <datefrom> is null OR
     * <datefrom> <= <datefield> ) AND <dateto> >= <datefield>
     */
    String sql = "SELECT " + databaseMeta.quoteField(meta.getKeyField()) + ", " + databaseMeta.quoteField(meta.getVersionField());
    if (!Utils.isEmpty(meta.getFieldLookup())) {
        for (int i = 0; i < meta.getFieldLookup().length; i++) {
            // Don't retrieve the fields without input
            if (!Utils.isEmpty(meta.getFieldLookup()[i]) && !DimensionLookupMeta.isUpdateTypeWithoutArgument(meta.isUpdate(), meta.getFieldUpdate()[i])) {
                sql += ", " + databaseMeta.quoteField(meta.getFieldLookup()[i]);
                if (!Utils.isEmpty(meta.getFieldStream()[i]) && !meta.getFieldLookup()[i].equals(meta.getFieldStream()[i])) {
                    sql += " AS " + databaseMeta.quoteField(meta.getFieldStream()[i]);
                }
            }
        }
    }
    if (meta.getCacheSize() >= 0) {
        sql += ", " + databaseMeta.quoteField(meta.getDateFrom()) + ", " + databaseMeta.quoteField(meta.getDateTo());
    }
    sql += " FROM " + data.schemaTable + " WHERE ";
    for (int i = 0; i < meta.getKeyLookup().length; i++) {
        if (i != 0) {
            sql += " AND ";
        }
        sql += databaseMeta.quoteField(meta.getKeyLookup()[i]) + " = ? ";
        data.lookupRowMeta.addValueMeta(rowMeta.getValueMeta(data.keynrs[i]));
    }
    String dateFromField = databaseMeta.quoteField(meta.getDateFrom());
    String dateToField = databaseMeta.quoteField(meta.getDateTo());
    if (meta.isUsingStartDateAlternative() && (meta.getStartDateAlternative() == DimensionLookupMeta.START_DATE_ALTERNATIVE_NULL) || (meta.getStartDateAlternative() == DimensionLookupMeta.START_DATE_ALTERNATIVE_COLUMN_VALUE)) {
        // Null as a start date is possible...
        // 
        sql += " AND ( " + dateFromField + " IS NULL OR " + dateFromField + " <= ? )" + Const.CR;
        sql += " AND " + dateToField + " > ?" + Const.CR;
        data.lookupRowMeta.addValueMeta(new ValueMetaDate(meta.getDateFrom()));
        data.lookupRowMeta.addValueMeta(new ValueMetaDate(meta.getDateTo()));
    } else {
        // Null as a start date is NOT possible
        // 
        sql += " AND ? >= " + dateFromField + Const.CR;
        sql += " AND ? < " + dateToField + Const.CR;
        data.lookupRowMeta.addValueMeta(new ValueMetaDate(meta.getDateFrom()));
        data.lookupRowMeta.addValueMeta(new ValueMetaDate(meta.getDateTo()));
    }
    try {
        logDetailed("Dimension Lookup setting preparedStatement to [" + sql + "]");
        data.prepStatementLookup = data.db.getConnection().prepareStatement(databaseMeta.stripCR(sql));
        if (databaseMeta.supportsSetMaxRows()) {
            // alywas get only 1 line back!
            data.prepStatementLookup.setMaxRows(1);
        }
        if (databaseMeta.getDatabaseInterface().isMySQLVariant()) {
            // Make sure to DISABLE Streaming Result sets
            data.prepStatementLookup.setFetchSize(0);
        }
        logDetailed("Finished preparing dimension lookup statement.");
    } catch (SQLException ex) {
        throw new KettleDatabaseException("Unable to prepare dimension lookup", ex);
    }
}
Also used : RowMeta(org.pentaho.di.core.row.RowMeta) SQLException(java.sql.SQLException) KettleDatabaseException(org.pentaho.di.core.exception.KettleDatabaseException) DatabaseMeta(org.pentaho.di.core.database.DatabaseMeta) ValueMetaDate(org.pentaho.di.core.row.value.ValueMetaDate)

Aggregations

DatabaseMeta (org.pentaho.di.core.database.DatabaseMeta)522 Test (org.junit.Test)133 KettleException (org.pentaho.di.core.exception.KettleException)131 Database (org.pentaho.di.core.database.Database)88 MessageBox (org.eclipse.swt.widgets.MessageBox)66 ErrorDialog (org.pentaho.di.ui.core.dialog.ErrorDialog)63 TransMeta (org.pentaho.di.trans.TransMeta)57 StepMeta (org.pentaho.di.trans.step.StepMeta)54 ArrayList (java.util.ArrayList)53 KettleDatabaseException (org.pentaho.di.core.exception.KettleDatabaseException)48 RowMetaInterface (org.pentaho.di.core.row.RowMetaInterface)44 ValueMetaString (org.pentaho.di.core.row.value.ValueMetaString)42 SlaveServer (org.pentaho.di.cluster.SlaveServer)33 IMetaStore (org.pentaho.metastore.api.IMetaStore)30 ObjectId (org.pentaho.di.repository.ObjectId)29 DatabaseExplorerDialog (org.pentaho.di.ui.core.database.dialog.DatabaseExplorerDialog)29 JobMeta (org.pentaho.di.job.JobMeta)26 TransHopMeta (org.pentaho.di.trans.TransHopMeta)26 RowMetaAndData (org.pentaho.di.core.RowMetaAndData)24 PluginRegistry (org.pentaho.di.core.plugins.PluginRegistry)24