Java Code Examples for org.pentaho.di.core.database.DatabaseMeta#setDatabaseInterface()

The following examples show how to use org.pentaho.di.core.database.DatabaseMeta#setDatabaseInterface() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MonetDBBulkLoaderIT.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
@Test
public void testNoInput() {
  String oneStepname = "Monet Bulk Loader";
  MonetDBBulkLoaderMeta meta = new MonetDBBulkLoaderMeta();
  DatabaseMeta database = new DatabaseMeta();
  database.setDatabaseInterface( new MonetDBDatabaseMeta() );
  meta.setDefault();
  meta.setDatabaseMeta( database );
  TransMeta transMeta = TransTestFactory.generateTestTransformation( new Variables(), meta, oneStepname );

  try {
    TransTestFactory.executeTestTransformation( transMeta, oneStepname, new ArrayList<RowMetaAndData>() );
  } catch ( KettleException e ) {
    // The Monet DB Bulk Loader step should finish quietly if no input rows
    fail();
  }
}
 
Example 2
Source File: ValueMetaBaseTest.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetValueFromSQLTypeBinaryMysql() throws Exception {

  final int binaryColumnIndex = 1;
  ValueMetaBase valueMetaBase = new ValueMetaBase();
  DatabaseMeta dbMeta = spy( new DatabaseMeta() );
  DatabaseInterface databaseInterface = new MySQLDatabaseMeta();
  dbMeta.setDatabaseInterface( databaseInterface );

  ResultSetMetaData metaData = mock( ResultSetMetaData.class );

  when( resultSet.getMetaData() ).thenReturn( metaData );
  when( metaData.getColumnType( binaryColumnIndex ) ).thenReturn( Types.LONGVARBINARY );

  ValueMetaInterface binaryValueMeta =
    valueMetaBase.getValueFromSQLType( dbMeta, TEST_NAME, metaData, binaryColumnIndex, false, false );
  assertEquals( ValueMetaInterface.TYPE_BINARY, binaryValueMeta.getType() );
  assertTrue( binaryValueMeta.isBinary() );
}
 
Example 3
Source File: KettleDatabaseRepositoryCreationHelperTest.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
@Test
public void testDefaultDBRepoStringLength() throws Exception {

  KettleEnvironment.init();
  DatabaseMeta databaseMeta = new DatabaseMeta();
  databaseMeta.setDatabaseInterface( new TestDatabaseMeta() );
  repositoryMeta =
      new KettleDatabaseRepositoryMeta( "KettleDatabaseRepository", "TestRepo", "Test Repository", databaseMeta );
  repository = new KettleDatabaseRepository();
  repository.init( repositoryMeta );
  KettleDatabaseRepositoryCreationHelper helper = new KettleDatabaseRepositoryCreationHelper( repository );
  int repoStringLength = helper.getRepoStringLength();
  assertEquals( EXPECTED_DEFAULT_DB_REPO_STRING, repoStringLength );
}
 
Example 4
Source File: DatabaseLookupUTest.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
private DatabaseLookupMeta createDatabaseMeta() throws KettleException {
  MySQLDatabaseMeta mysql = new MySQLDatabaseMeta();
  mysql.setName( "MySQL" );
  DatabaseMeta dbMeta = new DatabaseMeta();
  dbMeta.setDatabaseInterface( mysql );

  DatabaseLookupMeta meta = new DatabaseLookupMeta();
  meta.setDatabaseMeta( dbMeta );
  meta.setTablename( "VirtualTable" );

  meta.setTableKeyField( new String[] { ID_FIELD } );
  meta.setKeyCondition( new String[] { "=" } );

  meta.setReturnValueNewName( new String[] { "returned value" } );
  meta.setReturnValueField( new String[] { BINARY_FIELD } );
  meta.setReturnValueDefaultType( new int[] { ValueMetaInterface.TYPE_BINARY } );

  meta.setStreamKeyField1( new String[ 0 ] );
  meta.setStreamKeyField2( new String[ 0 ] );

  meta.setReturnValueDefault( new String[] { "" } );

  meta = spy( meta );
  doAnswer( new Answer() {
    @Override public Object answer( InvocationOnMock invocation ) throws Throwable {
      RowMetaInterface row = (RowMetaInterface) invocation.getArguments()[ 0 ];
      ValueMetaInterface v = new ValueMetaBinary( BINARY_FIELD );
      row.addValueMeta( v );
      return null;
    }
  } ).when( meta ).getFields(
    any( RowMetaInterface.class ),
    anyString(),
    any( RowMetaInterface[].class ),
    any( StepMeta.class ),
    any( VariableSpace.class ),
    any( Repository.class ),
    any( IMetaStore.class ) );
  return meta;
}
 
Example 5
Source File: MySQLBulkLoaderTest.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
  TransMeta transMeta = new TransMeta();
  transMeta.setName( "MysqlBulkLoader" );

  Map<String, String> vars = new HashMap<String, String>();
  vars.put( "delim", "," );
  vars.put( "enclos", "'" );
  vars.put( "charset", "UTF8" );
  vars.put( "tbl", "sometable" );
  vars.put( "schema", "someschema" );
  transMeta.injectVariables( vars );
  MySQLDatabaseMeta mysql = new MySQLDatabaseMeta();
  mysql.setName( "MySQL" );
  DatabaseMeta dbMeta = new DatabaseMeta();
  dbMeta.setDatabaseInterface( mysql );
  dbMeta.setQuoteAllFields( true );
  lmeta = new MySQLBulkLoaderMeta();
  lmeta.setDelimiter( "${delim}" );
  lmeta.setEnclosure( "${enclos}" );
  lmeta.setEncoding( "${charset}" );
  lmeta.setTableName( "${tbl}" );
  lmeta.setSchemaName( "${schema}" );
  lmeta.setDatabaseMeta( dbMeta );
  ldata = new MySQLBulkLoaderData();
  PluginRegistry plugReg = PluginRegistry.getInstance();
  String mblPid = plugReg.getPluginId( StepPluginType.class, lmeta );
  smeta = new StepMeta( mblPid, "MySqlBulkLoader", lmeta );
  Trans trans = new Trans( transMeta );
  transMeta.addStep( smeta );
  lder = new MySQLBulkLoader( smeta, ldata, 1, transMeta, trans );
  lder.copyVariablesFrom( transMeta );
}
 
Example 6
Source File: AsyncDatabaseActionTest.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
@Before public void before() {
  dbMeta = new DatabaseMeta();
  H2DatabaseMeta h2 = new H2DatabaseMeta();
  dbMeta.setDatabaseInterface( h2 );
  dbMeta.setName( "mem:TEST" );
  dbMeta.setDBName( "mem:TEST" );
  dbMeta.setDatabaseType( "H2" );
  KettleLogStore.getAppender().addLoggingEventListener( errorLogListener );
}
 
Example 7
Source File: SimplePmdDataFactory.java    From pentaho-reporting with GNU Lesser General Public License v2.1 4 votes vote down vote up
private DatabaseMeta getActiveDatabaseMeta( final DatabaseMeta databaseMeta,
                                            final DataRow dataRow ) {
  // retrieve a temporary connection to determine if a dialect change is necessary
  // for generating the MQL Query.
  final String user = computeUsername( dataRow );
  final String password = computePassword( dataRow );

  final Connection connection;
  try {
    connection = getConnectionProvider().createConnection( databaseMeta, user, password );
  } catch ( final ReportDataFactoryException rdfe ) {
    return databaseMeta;
  }

  try {

    // if the connection type is not of the current dialect, regenerate the query
    final DatabaseInterface di = getDatabaseInterface( connection, databaseMeta );

    if ( ( di != null ) && !databaseMeta.getPluginId().equals( di.getPluginId() ) ) {
      // we need to reinitialize our mqlQuery object and reset the query.
      // note that using this di object wipes out connection info
      final DatabaseMeta meta = (DatabaseMeta) databaseMeta.clone();
      final DatabaseInterface di2 = (DatabaseInterface) di.clone();
      di2.setAccessType( databaseMeta.getAccessType() );
      di2.setDatabaseName( databaseMeta.getDatabaseName() );
      meta.setDatabaseInterface( di2 );
      return meta;
    } else {
      return databaseMeta;
    }
  } finally {
    if ( connection != null ) {
      try {
        connection.close();
      } catch ( final SQLException ignored ) {
        // this is just cleanup
        logger.debug( "debug", ignored ); //$NON-NLS-1$
      }
    }
  }

}
 
Example 8
Source File: KettleDatabaseRepositoryDatabaseDelegate.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
/**
 *
 * Load the Database Info
 */
public DatabaseMeta loadDatabaseMeta( ObjectId id_database ) throws KettleException {
  DatabaseMeta databaseMeta = new DatabaseMeta();
  try {
    RowMetaAndData r = getDatabase( id_database );

    if ( r != null ) {
      ObjectId id_database_type =
        new LongObjectId( r.getInteger( KettleDatabaseRepository.FIELD_DATABASE_ID_DATABASE_TYPE, 0 ) ); // con_type
      String dbTypeDesc = getDatabaseTypeCode( id_database_type );
      if ( dbTypeDesc != null ) {
        databaseMeta.setDatabaseInterface( DatabaseMeta.getDatabaseInterface( dbTypeDesc ) );
        databaseMeta.setAttributes( new Properties() ); // new attributes
      }

      databaseMeta.setObjectId( id_database );
      databaseMeta.setName( r.getString( KettleDatabaseRepository.FIELD_DATABASE_NAME, "" ) );

      ObjectId id_database_contype = new LongObjectId(
        r.getInteger( KettleDatabaseRepository.FIELD_DATABASE_ID_DATABASE_CONTYPE, 0 ) ); // con_access
      databaseMeta.setAccessType( DatabaseMeta.getAccessType( getDatabaseConTypeCode( id_database_contype ) ) );

      databaseMeta.setHostname( r.getString( KettleDatabaseRepository.FIELD_DATABASE_HOST_NAME, "" ) );
      databaseMeta.setDBName( r.getString( KettleDatabaseRepository.FIELD_DATABASE_DATABASE_NAME, "" ) );
      databaseMeta.setDBPort( r.getString( KettleDatabaseRepository.FIELD_DATABASE_PORT, "" ) );
      databaseMeta.setUsername( r.getString( KettleDatabaseRepository.FIELD_DATABASE_USERNAME, "" ) );
      databaseMeta.setPassword( Encr.decryptPasswordOptionallyEncrypted( r.getString(
        KettleDatabaseRepository.FIELD_DATABASE_PASSWORD, "" ) ) );
      databaseMeta.setServername( r.getString( KettleDatabaseRepository.FIELD_DATABASE_SERVERNAME, "" ) );
      databaseMeta.setDataTablespace( r.getString( KettleDatabaseRepository.FIELD_DATABASE_DATA_TBS, "" ) );
      databaseMeta.setIndexTablespace( r.getString( KettleDatabaseRepository.FIELD_DATABASE_INDEX_TBS, "" ) );

      // Also, load all the properties we can find...
      final Collection<RowMetaAndData> attrs = repository.connectionDelegate.getDatabaseAttributes( id_database );
      for ( RowMetaAndData row : attrs ) {
        String code = row.getString( KettleDatabaseRepository.FIELD_DATABASE_ATTRIBUTE_CODE, "" );
        String attribute = row.getString( KettleDatabaseRepository.FIELD_DATABASE_ATTRIBUTE_VALUE_STR, "" );
        databaseMeta.getAttributes().put( code, Const.NVL( attribute, "" ) );
      }
    }

    return databaseMeta;
  } catch ( KettleDatabaseException dbe ) {
    throw new KettleException( "Error loading database connection from repository (id_database="
      + id_database + ")", dbe );
  }
}
 
Example 9
Source File: DatabaseLookupUTest.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
@Test
public void testEqualsAndIsNullAreCached() throws Exception {
  when( mockHelper.logChannelInterfaceFactory.create( any(), any( LoggingObjectInterface.class ) ) )
    .thenReturn( mockHelper.logChannelInterface );

  DatabaseLookup look =
    new MockDatabaseLookup( mockHelper.stepMeta, mockHelper.stepDataInterface, 0, mockHelper.transMeta,
      mockHelper.trans );
  DatabaseLookupData lookData = new DatabaseLookupData();
  lookData.cache = DefaultCache.newCache( lookData, 0 );
  lookData.lookupMeta = new RowMeta();

  MySQLDatabaseMeta mysql = new MySQLDatabaseMeta();
  mysql.setName( "MySQL" );
  DatabaseMeta dbMeta = new DatabaseMeta();
  dbMeta.setDatabaseInterface( mysql );

  DatabaseLookupMeta meta = new DatabaseLookupMeta();
  meta.setDatabaseMeta( dbMeta );
  meta.setTablename( "VirtualTable" );

  meta.setTableKeyField( new String[] { "ID1", "ID2" } );
  meta.setKeyCondition( new String[] { "=", "IS NULL" } );

  meta.setReturnValueNewName( new String[] { "val1", "val2" } );
  meta.setReturnValueField( new String[] { BINARY_FIELD, BINARY_FIELD } );
  meta.setReturnValueDefaultType( new int[] { ValueMetaInterface.TYPE_BINARY, ValueMetaInterface.TYPE_BINARY } );

  meta.setStreamKeyField1( new String[ 0 ] );
  meta.setStreamKeyField2( new String[ 0 ] );

  meta.setReturnValueDefault( new String[] { "", "" } );

  meta = spy( meta );
  doAnswer( new Answer() {
    @Override public Object answer( InvocationOnMock invocation ) throws Throwable {
      RowMetaInterface row = (RowMetaInterface) invocation.getArguments()[ 0 ];
      ValueMetaInterface v = new ValueMetaBinary( BINARY_FIELD );
      row.addValueMeta( v );
      return null;
    }
  } ).when( meta ).getFields(
    any( RowMetaInterface.class ),
    anyString(),
    any( RowMetaInterface[].class ),
    any( StepMeta.class ),
    any( VariableSpace.class ),
    any( Repository.class ),
    any( IMetaStore.class ) );


  look.init( meta, lookData );
  assertTrue( lookData.allEquals ); // Test for fix on PDI-15202

}
 
Example 10
Source File: ValueMetaBaseTest.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
@Test
public void testGetBinaryWithLength_WhenBinarySqlTypesOfVertica() throws Exception {
  final int binaryColumnIndex = 1;
  final int varbinaryColumnIndex = 2;
  final int expectedBinarylength = 1;
  final int expectedVarBinarylength = 80;

  ValueMetaBase obj = new ValueMetaBase();
  DatabaseMeta dbMeta = spy( new DatabaseMeta() );
  DatabaseInterface databaseInterface = new Vertica5DatabaseMeta();
  dbMeta.setDatabaseInterface( databaseInterface );

  ResultSetMetaData metaData = mock( ResultSetMetaData.class );

  when( resultSet.getMetaData() ).thenReturn( metaData );
  when( metaData.getColumnType( binaryColumnIndex ) ).thenReturn( Types.BINARY );
  when( metaData.getPrecision( binaryColumnIndex ) ).thenReturn( expectedBinarylength );
  when( metaData.getColumnDisplaySize( binaryColumnIndex ) ).thenReturn( expectedBinarylength * 2 );

  when( metaData.getColumnType( varbinaryColumnIndex ) ).thenReturn( Types.BINARY );
  when( metaData.getPrecision( varbinaryColumnIndex ) ).thenReturn( expectedVarBinarylength );
  when( metaData.getColumnDisplaySize( varbinaryColumnIndex ) ).thenReturn( expectedVarBinarylength * 2 );

  // get value meta for binary type
  ValueMetaInterface binaryValueMeta =
    obj.getValueFromSQLType( dbMeta, TEST_NAME, metaData, binaryColumnIndex, false, false );
  assertNotNull( binaryValueMeta );
  assertTrue( TEST_NAME.equals( binaryValueMeta.getName() ) );
  assertTrue( ValueMetaInterface.TYPE_BINARY == binaryValueMeta.getType() );
  assertTrue( expectedBinarylength == binaryValueMeta.getLength() );
  assertFalse( binaryValueMeta.isLargeTextField() );

  // get value meta for varbinary type
  ValueMetaInterface varbinaryValueMeta =
    obj.getValueFromSQLType( dbMeta, TEST_NAME, metaData, varbinaryColumnIndex, false, false );
  assertNotNull( varbinaryValueMeta );
  assertTrue( TEST_NAME.equals( varbinaryValueMeta.getName() ) );
  assertTrue( ValueMetaInterface.TYPE_BINARY == varbinaryValueMeta.getType() );
  assertTrue( expectedVarBinarylength == varbinaryValueMeta.getLength() );
  assertFalse( varbinaryValueMeta.isLargeTextField() );

}