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