org.pentaho.di.core.database.Database Java Examples

The following examples show how to use org.pentaho.di.core.database.Database. 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: InsertUpdateMetaTest.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
@Test
public void testErrorProcessRow() throws KettleException {
  Mockito.when( mockHelper.logChannelInterfaceFactory.create( Mockito.any(), Mockito.any( LoggingObjectInterface.class ) ) )
    .thenReturn(
      mockHelper.logChannelInterface );
  Mockito.when( mockHelper.stepMeta.getStepMetaInterface() ).thenReturn( new InsertUpdateMeta() );

  InsertUpdate insertUpdateStep =
    new InsertUpdate( mockHelper.stepMeta, mockHelper.stepDataInterface, 0, mockHelper.transMeta, mockHelper.trans );
  insertUpdateStep = Mockito.spy( insertUpdateStep );

  Mockito.doReturn( new Object[] {} ).when( insertUpdateStep ).getRow();
  insertUpdateStep.first = false;
  mockHelper.processRowsStepDataInterface.lookupParameterRowMeta = Mockito.mock( RowMetaInterface.class );
  mockHelper.processRowsStepDataInterface.keynrs = new int[] {};
  mockHelper.processRowsStepDataInterface.db = Mockito.mock( Database.class );
  mockHelper.processRowsStepDataInterface.valuenrs = new int[] {};
  Mockito.doThrow( new KettleStepException( "Test exception" ) ).when( insertUpdateStep ).putRow( Mockito.any(), Mockito.any() );

  boolean result =
    insertUpdateStep.processRow( mockHelper.processRowsStepMetaInterface, mockHelper.processRowsStepDataInterface );
  Assert.assertFalse( result );
}
 
Example #2
Source File: TableInputTest.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() {

  StepMeta mockStepMeta = mock( StepMeta.class );
  TransMeta mockTransMeta = mock( TransMeta.class );
  Trans mockTrans = mock( Trans.class );
  StepPartitioningMeta mockStepPartitioningMeta = mock( StepPartitioningMeta.class );

  when( mockStepMeta.getName() ).thenReturn( "MockStep" );
  when( mockTransMeta.findStep( anyString() ) ).thenReturn( mockStepMeta );
  when( mockStepMeta.getTargetStepPartitioningMeta() ).thenReturn( mockStepPartitioningMeta );

  mockStepMetaInterface = mock( TableInputMeta.class, withSettings().extraInterfaces( StepMetaInterface.class ) );
  mockStepDataInterface = mock( TableInputData.class, withSettings().extraInterfaces( StepMetaInterface.class ) );
  mockStepDataInterface.db = mock( Database.class );
  mockTableInput = spy( new TableInput( mockStepMeta, mockStepDataInterface, 1, mockTransMeta, mockTrans ) );
}
 
Example #3
Source File: DatabaseJoinTest.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() {

  StepMeta mockStepMeta = mock( StepMeta.class );
  TransMeta mockTransMeta = mock( TransMeta.class );
  Trans mockTrans = mock( Trans.class );
  StepPartitioningMeta mockStepPartitioningMeta = mock( StepPartitioningMeta.class );

  when( mockStepMeta.getName() ).thenReturn( "MockStep" );
  when( mockTransMeta.findStep( anyString() ) ).thenReturn( mockStepMeta );
  when( mockStepMeta.getTargetStepPartitioningMeta() ).thenReturn( mockStepPartitioningMeta );

  mockStepMetaInterface = mock( DatabaseJoinMeta.class, withSettings().extraInterfaces( StepMetaInterface.class ) );
  mockStepDataInterface = mock( DatabaseJoinData.class, withSettings().extraInterfaces( StepMetaInterface.class ) );
  mockStepDataInterface.db = mock( Database.class );
  mockStepDataInterface.pstmt = mock( PreparedStatement.class );
  mockDatabaseJoin = spy( new DatabaseJoin( mockStepMeta, mockStepDataInterface, 1, mockTransMeta, mockTrans ) );
}
 
Example #4
Source File: TeraFast.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
/**
 * Execute fastload.
 *
 * @throws KettleException
 *           ...
 */
public void execute() throws KettleException {
  if ( this.meta.getTruncateTable().getValue() ) {
    Database db = new Database( this, this.meta.getDbMeta() );
    db.connect();
    db.truncateTable( this.meta.getTargetTable().getValue() );
    db.commit();
    db.disconnect();
  }
  startFastLoad();

  if ( this.meta.getUseControlFile().getValue() ) {
    this.invokeLoadingControlFile();
  } else {
    this.invokeLoadingCommand();
  }
}
 
Example #5
Source File: DimensionLookupIT.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
public void prepareMocksForInsertTest() {
  mockDimensionLookupData.schemaTable = "testSchemaTable";
  ValueMetaInterface mockKeyValueMeta = mock( ValueMetaInterface.class );
  when( mockDimensionLookupMeta.getDatabaseMeta() ).thenReturn( mockDatabaseMeta );
  when( mockDatabaseMeta.quoteField( anyString() ) ).thenAnswer( new Answer<String>() {
    public String answer( InvocationOnMock invocation ) throws Throwable {
      return "\"" + invocation.getArguments()[0] + "\"";
    }
  } );
  String keyField = "testKeyField";
  when( mockDimensionLookupMeta.getKeyField() ).thenReturn( keyField );
  when( mockDimensionLookupMeta.getVersionField() ).thenReturn( "testVersionField" );
  when( mockDimensionLookupMeta.getDateFrom() ).thenReturn( "1900-01-01" );
  when( mockDimensionLookupMeta.getDateTo() ).thenReturn( "1901-01-01" );
  when( mockDimensionLookupMeta.getKeyLookup() ).thenReturn( new String[] {} );
  when( mockDimensionLookupMeta.getFieldLookup() ).thenReturn( new String[] {} );
  when( mockDimensionLookupMeta.getFieldUpdate() ).thenReturn( new int[] {} );
  mockDimensionLookupData.keynrs = new int[] {};
  mockDimensionLookupData.fieldnrs = new int[] {};
  Database mockDatabase = mock( Database.class );
  when( mockDatabase.getConnection() ).thenReturn( mockConnection );
  mockDimensionLookupData.db = mockDatabase;
  when( mockKeyValueMeta.getName() ).thenReturn( "testKey" );
  when( mockOutputRowMeta.getValueMeta( 0 ) ).thenReturn( mockKeyValueMeta );
}
 
Example #6
Source File: ExecSQLRowIT.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void createDatabase() throws Exception {
  KettleEnvironment.init();

  //
  // Create a new transformation...
  //
  TransMeta transMeta = new TransMeta();
  transMeta.setName( "transname" );

  // Add the database connections
  for ( int i = 0; i < databasesXML.length; i++ ) {
    DatabaseMeta databaseMeta = new DatabaseMeta( databasesXML[i] );
    transMeta.addDatabase( databaseMeta );
  }
  DatabaseMeta dbInfo = transMeta.findDatabase( "db" );

  // Execute our setup SQLs in the database.
  database = new Database( loggingObject, dbInfo );
  database.connect();
  createTables( database );
  createData( database );
}
 
Example #7
Source File: PDI5436Test.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
@Test
public void testCacheAllTable() throws KettleException {
  DatabaseLookup stepSpy =
      spy( new DatabaseLookup( smh.stepMeta, smh.stepDataInterface, 0, smh.transMeta, smh.trans ) );

  Database database = mockDatabase();
  doReturn( database ).when( stepSpy ).getDatabase( any( DatabaseMeta.class ) );

  stepSpy.addRowSetToInputRowSets( mockInputRowSet() );
  stepSpy.setInputRowMeta( mockInputRowMeta() );
  RowSet outputRowSet = new QueueRowSet();
  stepSpy.addRowSetToOutputRowSets( outputRowSet );

  StepMetaInterface meta = mockStepMeta();
  StepDataInterface data = smh.initStepDataInterface;

  Assert.assertTrue( "Step init failed", stepSpy.init( meta, data ) );
  Assert.assertTrue( "Error processing row", stepSpy.processRow( meta, data ) );
  Assert.assertEquals( "Cache lookup failed", "value", outputRowSet.getRow()[2] );
}
 
Example #8
Source File: DatabaseLookupMeta.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
@Override
public RowMetaInterface getTableFields() {
  RowMetaInterface fields = null;
  if ( databaseMeta != null ) {
    Database db = new Database( loggingObject, databaseMeta );
    databases = new Database[] { db }; // Keep track of this one for cancelQuery

    try {
      db.connect();
      String tableName = databaseMeta.environmentSubstitute( tablename );
      String schemaTable = databaseMeta.getQuotedSchemaTableCombination( schemaName, tableName );
      fields = db.getTableFields( schemaTable );

    } catch ( KettleDatabaseException dbe ) {
      logError( BaseMessages.getString( PKG, "DatabaseLookupMeta.ERROR0004.ErrorGettingTableFields" )
          + dbe.getMessage() );
    } finally {
      db.disconnect();
    }
  }
  return fields;
}
 
Example #9
Source File: JobEntryTruncateTables.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
private boolean truncateTables( String tablename, String schemaname, Database db ) {
  boolean retval = false;
  try {
    // check if table exists!
    if ( db.checkTableExists( schemaname, tablename ) ) {
      if ( !Utils.isEmpty( schemaname ) ) {
        db.truncateTable( schemaname, tablename );
      } else {
        db.truncateTable( tablename );
      }

      if ( log.isDetailed() ) {
        logDetailed( BaseMessages.getString( PKG, "JobEntryTruncateTables.Log.TableTruncated", tablename ) );
      }

      retval = true;
    } else {
      logError( BaseMessages.getString( PKG, "JobEntryTruncateTables.Error.CanNotFindTable", tablename ) );
    }
  } catch ( Exception e ) {
    logError( BaseMessages.getString( PKG, "JobEntryTruncateTables.Error.CanNotTruncateTables", tablename, e
      .toString() ) );
  }
  return retval;
}
 
Example #10
Source File: Job.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
/**
 * Write job entry log information.
 *
 * @throws KettleException
 *           the kettle exception
 */
protected void writeJobEntryLogInformation() throws KettleException {
  Database db = null;
  JobEntryLogTable jobEntryLogTable = getJobMeta().getJobEntryLogTable();
  try {
    db = createDataBase( jobEntryLogTable.getDatabaseMeta() );
    db.shareVariablesWith( this );
    db.connect();
    db.setCommit( logCommitSize );

    for ( JobEntryCopy copy : getJobMeta().getJobCopies() ) {
      db.writeLogRecord( jobEntryLogTable, LogStatus.START, copy, this );
    }

    db.cleanupLogRecords( jobEntryLogTable );
  } catch ( Exception e ) {
    throw new KettleException( BaseMessages.getString( PKG, "Job.Exception.UnableToJobEntryInformationToLogTable" ),
        e );
  } finally {
    if ( !db.isAutoCommit() ) {
      db.commitLog( true, jobEntryLogTable );
    }
    db.disconnect();
  }
}
 
Example #11
Source File: XulDatabaseExplorerController.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
public void getDDL() {
  if ( model.getTable() == null ) {
    return;
  }
  Database db = new Database( null, this.model.getDatabaseMeta() );
  try {
    db.connect();
    String tableName = getSchemaAndTable( this.model );
    RowMetaInterface r = db.getTableFields( tableName );
    String sql = db.getCreateTableStatement( tableName, r, null, false, null, true );
    SQLEditor se =
      new SQLEditor( this.getDatabaseMeta(), this.dbExplorerDialog.getShell(), SWT.NONE, this.model
        .getDatabaseMeta(), this.dbcache, sql );
    se.open();
  } catch ( KettleDatabaseException dbe ) {
    new ErrorDialog(
      this.dbExplorerDialog.getShell(), BaseMessages.getString( PKG, "Dialog.Error.Header" ), BaseMessages
        .getString( PKG, "DatabaseExplorerDialog.Error.RetrieveLayout" ), dbe );
  } finally {
    db.disconnect();
  }
}
 
Example #12
Source File: MySQLBulkLoaderMeta.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
public RowMetaInterface getRequiredFields( VariableSpace space ) throws KettleException {
  String realTableName = space.environmentSubstitute( tableName );
  String realSchemaName = space.environmentSubstitute( schemaName );

  if ( databaseMeta != null ) {
    Database db = new Database( loggingObject, databaseMeta );
    try {
      db.connect();

      if ( !Utils.isEmpty( realTableName ) ) {
        String schemaTable = databaseMeta.getQuotedSchemaTableCombination( realSchemaName, realTableName );

        // Check if this table exists...
        if ( db.checkTableExists( schemaTable ) ) {
          return db.getTableFields( schemaTable );
        } else {
          throw new KettleException( BaseMessages.getString( PKG, "MySQLBulkLoaderMeta.Exception.TableNotFound" ) );
        }
      } else {
        throw new KettleException( BaseMessages.getString( PKG, "MySQLBulkLoaderMeta.Exception.TableNotSpecified" ) );
      }
    } catch ( Exception e ) {
      throw new KettleException( BaseMessages.getString( PKG, "MySQLBulkLoaderMeta.Exception.ErrorGettingFields" ), e );
    } finally {
      db.disconnect();
    }
  } else {
    throw new KettleException( BaseMessages.getString( PKG, "MySQLBulkLoaderMeta.Exception.ConnectionNotDefined" ) );
  }

}
 
Example #13
Source File: JobHistoryDelegate.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
/**
 * User requested to clear the log table.<br>
 * Better ask confirmation
 */
private void clearLogTable( int index ) {
  JobHistoryLogTab model = models[index];
  LogTableInterface logTable = model.logTable;

  if ( logTable.isDefined() ) {
    DatabaseMeta databaseMeta = logTable.getDatabaseMeta();

    MessageBox mb = new MessageBox( jobGraph.getShell(), SWT.YES | SWT.NO | SWT.ICON_QUESTION );
    mb.setMessage( BaseMessages.getString( PKG, "JobGraph.Dialog.AreYouSureYouWantToRemoveAllLogEntries.Message",
      logTable.getQuotedSchemaTableCombination() ) );
    mb.setText( BaseMessages.getString( PKG, "JobGraph.Dialog.AreYouSureYouWantToRemoveAllLogEntries.Title" ) );
    if ( mb.open() == SWT.YES ) {
      Database database = new Database( loggingObject, databaseMeta );
      try {
        database.connect();
        database.truncateTable( logTable.getSchemaName(), logTable.getTableName() );
      } catch ( Exception e ) {
        new ErrorDialog( jobGraph.getShell(),
          BaseMessages.getString( PKG, "JobGraph.Dialog.ErrorClearningLoggingTable.Title" ),
          BaseMessages.getString( PKG, "JobGraph.Dialog.AreYouSureYouWantToRemoveAllLogEntries.Message" ), e );
      } finally {
        database.disconnect();

        refreshHistory();
        if ( model.logDisplayText != null ) {
          model.logDisplayText.setText( "" );
        }
      }
    }
  }
}
 
Example #14
Source File: AddSequenceDialog.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
private void getSchemaNames() {
  if ( wSchema.isDisposed() ) {
    return;
  }
  DatabaseMeta databaseMeta = transMeta.findDatabase( wConnection.getText() );
  if ( databaseMeta != null ) {
    Database database = new Database( loggingObject, databaseMeta );
    try {
      database.connect();
      String[] schemas = database.getSchemas();

      if ( null != schemas && schemas.length > 0 ) {
        schemas = Const.sortStrings( schemas );
        EnterSelectionDialog dialog =
          new EnterSelectionDialog( shell, schemas,
            BaseMessages.getString( PKG, "AddSequenceDialog.SelectSequence.Title", wConnection.getText() ),
            BaseMessages.getString( PKG, "AddSequenceDialog.SelectSequence.Message" ) );
        String d = dialog.open();
        if ( d != null ) {
          wSchema.setText( Const.NVL( d.toString(), "" ) );
        }

      } else {
        MessageBox mb = new MessageBox( shell, SWT.OK | SWT.ICON_ERROR );
        mb.setMessage( BaseMessages.getString( PKG, "AddSequenceDialog.NoSchema.Message" ) );
        mb.setText( BaseMessages.getString( PKG, "AddSequenceDialog.NoSchema.Title" ) );
        mb.open();
      }
    } catch ( Exception e ) {
      new ErrorDialog( shell, BaseMessages.getString( PKG, "System.Dialog.Error.Title" ), BaseMessages
        .getString( PKG, "AddSequenceDialog.ErrorGettingSchemas" ), e );
    } finally {
      if ( database != null ) {
        database.disconnect();
        database = null;
      }
    }
  }
}
 
Example #15
Source File: DimensionLookupDialog.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
/**
 * Get the fields from the table in the database and use them as lookup keys. Only get the the fields which are not
 * yet in use as key, or in the field table. Also ignore technical key, version, fromdate, todate.
 */
private void getLookup() {
  DatabaseMeta databaseMeta = transMeta.findDatabase( wConnection.getText() );
  if ( databaseMeta != null ) {
    Database db = new Database( loggingObject, databaseMeta );
    db.shareVariablesWith( transMeta );
    try {
      db.connect();
      RowMetaInterface r = db.getTableFieldsMeta( wSchema.getText(), wTable.getText() );
      if ( r != null && !r.isEmpty() ) {
        BaseStepDialog.getFieldsFromPrevious(
          r, wUpIns, 2, new int[] { 1, 2 }, new int[] { 3 }, -1, -1, new TableItemInsertListener() {
            public boolean tableItemInserted( TableItem tableItem, ValueMetaInterface v ) {
              int idx = wKey.indexOfString( v.getName(), 2 );
              return idx < 0
                && !v.getName().equalsIgnoreCase( wTk.getText() )
                && !v.getName().equalsIgnoreCase( wVersion.getText() )
                && !v.getName().equalsIgnoreCase( wFromdate.getText() )
                && !v.getName().equalsIgnoreCase( wTodate.getText() );
            }
          } );
      }
    } catch ( KettleException e ) {
      MessageBox mb = new MessageBox( shell, SWT.OK | SWT.ICON_ERROR );
      mb.setText( BaseMessages.getString( PKG, "DimensionLookupDialog.ErrorOccurred.DialogTitle" ) );
      mb.setMessage( BaseMessages.getString( PKG, "DimensionLookupDialog.ErrorOccurred.DialogMessage" )
        + Const.CR + e.getMessage() );
      mb.open();
    } finally {
      db.disconnect();
    }
  }
}
 
Example #16
Source File: JobEntrySQL.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
public Result execute( Result result, int nr ) {

    if ( databaseMeta != null ) {
      try ( Database db = new Database( this, databaseMeta ) ) {
        String theSql = sqlFromFile ? buildSqlFromFile() : sql;
        if ( Utils.isEmpty( theSql ) ) {
          return result;
        }
        db.shareVariablesWith( this );
        db.connect( parentJob.getTransactionId(), null );
        // let it run
        if ( useVariableSubstitution ) {
          theSql = environmentSubstitute( theSql );
        }
        if ( isDetailed() ) {
          logDetailed( BaseMessages.getString( PKG, "JobSQL.Log.SQlStatement", theSql ) );
        }
        if ( sendOneStatement ) {
          db.execStatement( theSql );
        } else {
          db.execStatements( theSql );
        }
      } catch ( KettleDatabaseException je ) {
        result.setNrErrors( 1 );
        logError( BaseMessages.getString( PKG, "JobSQL.ErrorRunJobEntry", je.getMessage() ) );
      }
    } else {
      result.setNrErrors( 1 );
      logError( BaseMessages.getString( PKG, "JobSQL.NoDatabaseConnection" ) );
    }

    result.setResult( result.getNrErrors() == 0 );
    return result;
  }
 
Example #17
Source File: DimensionLookupDialog.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
private void getSchemaNames() {
  DatabaseMeta databaseMeta = transMeta.findDatabase( wConnection.getText() );
  if ( databaseMeta != null ) {
    Database database = new Database( loggingObject, databaseMeta );
    try {
      database.connect();
      String[] schemas = database.getSchemas();

      if ( null != schemas && schemas.length > 0 ) {
        schemas = Const.sortStrings( schemas );
        EnterSelectionDialog dialog =
          new EnterSelectionDialog( shell, schemas, BaseMessages.getString(
            PKG, "DimensionLookupDialog.AvailableSchemas.Title", wConnection.getText() ), BaseMessages
            .getString( PKG, "DimensionLookupDialog.AvailableSchemas.Message", wConnection.getText() ) );
        String d = dialog.open();
        if ( d != null ) {
          wSchema.setText( Const.NVL( d, "" ) );
          setTableFieldCombo();
        }

      } else {
        MessageBox mb = new MessageBox( shell, SWT.OK | SWT.ICON_ERROR );
        mb.setMessage( BaseMessages.getString( PKG, "DimensionLookupDialog.NoSchema.Error" ) );
        mb.setText( BaseMessages.getString( PKG, "DimensionLookupDialog.GetSchemas.Error" ) );
        mb.open();
      }
    } catch ( Exception e ) {
      new ErrorDialog( shell, BaseMessages.getString( PKG, "System.Dialog.Error.Title" ), BaseMessages
        .getString( PKG, "DimensionLookupDialog.ErrorGettingSchemas" ), e );
    } finally {
      database.disconnect();
    }
  }
}
 
Example #18
Source File: DatabaseLookup.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
private void putToDefaultCache( Database db, List<Object[]> rows ) {
  final int keysAmount = meta.getStreamKeyField1().length;
  RowMetaInterface prototype = copyValueMetasFrom( db.getReturnRowMeta(), keysAmount );

  // Copy the data into 2 parts: key and value...
  //
  for ( Object[] row : rows ) {
    int index = 0;
    // not sure it is efficient to re-create the same on every row,
    // but this was done earlier, so I'm keeping this behaviour
    RowMetaInterface keyMeta = prototype.clone();
    Object[] keyData = new Object[ keysAmount ];
    for ( int i = 0; i < keysAmount; i++ ) {
      keyData[ i ] = row[ index++ ];
    }
    // RowMeta valueMeta = new RowMeta();
    Object[] valueData = new Object[ data.returnMeta.size() ];
    for ( int i = 0; i < data.returnMeta.size(); i++ ) {
      valueData[ i ] = row[ index++ ];
      // valueMeta.addValueMeta(returnRowMeta.getValueMeta(index++));
    }
    // Store the data...
    //
    data.cache.storeRowInCache( meta, keyMeta, keyData, valueData );
    incrementLinesInput();
  }
}
 
Example #19
Source File: PGBulkLoaderTest.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
@Test
public void testDBNameOverridden_IfDbNameOverrideSetUp() throws Exception {
  // Db Name Override is set up
  PGBulkLoaderMeta pgBulkLoaderMock = getPgBulkLoaderMock( DB_NAME_OVVERRIDE );
  Database database = pgBulkLoader.getDatabase( pgBulkLoader, pgBulkLoaderMock );
  assertNotNull( database );
  // Verify DB name is overridden
  assertEquals( DB_NAME_OVVERRIDE, database.getDatabaseMeta().getDatabaseName() );
  // Check additionally other connection information
  assertEquals( CONNECTION_NAME, database.getDatabaseMeta().getName() );
  assertEquals( CONNECTION_DB_HOST, database.getDatabaseMeta().getHostname() );
  assertEquals( CONNECTION_DB_PORT, database.getDatabaseMeta().getDatabasePortNumberString() );
  assertEquals( CONNECTION_DB_USERNAME, database.getDatabaseMeta().getUsername() );
  assertEquals( CONNECTION_DB_PASSWORD, database.getDatabaseMeta().getPassword() );
}
 
Example #20
Source File: CombinationLookupDialog.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
private void getSchemaNames() {
  DatabaseMeta databaseMeta = transMeta.findDatabase( wConnection.getText() );
  if ( databaseMeta != null ) {
    Database database = new Database( loggingObject, databaseMeta );
    try {
      database.connect();
      String[] schemas = database.getSchemas();

      if ( null != schemas && schemas.length > 0 ) {
        schemas = Const.sortStrings( schemas );
        EnterSelectionDialog dialog =
          new EnterSelectionDialog( shell, schemas, BaseMessages.getString(
            PKG, "CombinationLookupDialog.AvailableSchemas.Title", wConnection.getText() ), BaseMessages
            .getString( PKG, "CombinationLookupDialog.AvailableSchemas.Message", wConnection.getText() ) );
        String d = dialog.open();
        if ( d != null ) {
          wSchema.setText( Const.NVL( d, "" ) );
          setTableFieldCombo();
        }

      } else {
        MessageBox mb = new MessageBox( shell, SWT.OK | SWT.ICON_ERROR );
        mb.setMessage( BaseMessages.getString( PKG, "CombinationLookupDialog.NoSchema.Error" ) );
        mb.setText( BaseMessages.getString( PKG, "CombinationLookupDialog.GetSchemas.Error" ) );
        mb.open();
      }
    } catch ( Exception e ) {
      new ErrorDialog( shell, BaseMessages.getString( PKG, "System.Dialog.Error.Title" ), BaseMessages
        .getString( PKG, "CombinationLookupDialog.ErrorGettingSchemas" ), e );
    } finally {
      database.disconnect();
    }
  }
}
 
Example #21
Source File: MonetDBBulkLoaderMeta.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
public RowMetaInterface getRequiredFields( VariableSpace space ) throws KettleException {
  String realTableName = space.environmentSubstitute( tableName );
  String realSchemaName = space.environmentSubstitute( schemaName );

  if ( databaseMeta != null ) {
    Database db = new Database( loggingObject, databaseMeta );
    try {
      db.connect();

      if ( !Utils.isEmpty( realTableName ) ) {
        String schemaTable = databaseMeta.getQuotedSchemaTableCombination( realSchemaName, realTableName );

        // Check if this table exists...
        if ( db.checkTableExists( schemaTable ) ) {
          return db.getTableFields( schemaTable );
        } else {
          throw new KettleException( BaseMessages.getString(
              PKG, "MonetDBBulkLoaderMeta.Exception.TableNotFound" ) );
        }
      } else {
        throw new KettleException( BaseMessages.getString(
            PKG, "MonetDBBulkLoaderMeta.Exception.TableNotSpecified" ) );
      }
    } catch ( Exception e ) {
      throw new KettleException( BaseMessages.getString(
          PKG, "MonetDBBulkLoaderMeta.Exception.ErrorGettingFields" ), e );
    } finally {
      db.disconnect();
    }
  } else {
    throw new KettleException( BaseMessages.getString(
        PKG, "MonetDBBulkLoaderMeta.Exception.ConnectionNotDefined" ) );
  }

}
 
Example #22
Source File: TableOutputMeta.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
public RowMetaInterface getRequiredFields( VariableSpace space ) throws KettleException {
  String realTableName = space.environmentSubstitute( tableName );
  String realSchemaName = space.environmentSubstitute( schemaName );

  if ( databaseMeta != null ) {
    Database db = new Database( loggingObject, databaseMeta );
    try {
      db.connect();

      if ( !Utils.isEmpty( realTableName ) ) {
        // Check if this table exists...
        if ( db.checkTableExists( realSchemaName, realTableName ) ) {
          return db.getTableFieldsMeta( realSchemaName, realTableName );
        } else {
          throw new KettleException( BaseMessages.getString( PKG, "TableOutputMeta.Exception.TableNotFound" ) );
        }
      } else {
        throw new KettleException( BaseMessages.getString( PKG, "TableOutputMeta.Exception.TableNotSpecified" ) );
      }
    } catch ( Exception e ) {
      throw new KettleException(
        BaseMessages.getString( PKG, "TableOutputMeta.Exception.ErrorGettingFields" ), e );
    } finally {
      db.disconnect();
    }
  } else {
    throw new KettleException( BaseMessages.getString( PKG, "TableOutputMeta.Exception.ConnectionNotDefined" ) );
  }

}
 
Example #23
Source File: DatabaseLookupUTest.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
private DatabaseLookupData getCreatedData( boolean allEquals ) throws Exception {
  Database db = mock( Database.class );
  when( db.getRows( anyString(), anyInt() ) )
    .thenReturn( Collections.singletonList( new Object[] { 1L } ) );

  RowMeta returnRowMeta = new RowMeta();
  returnRowMeta.addValueMeta( new ValueMetaInteger() );
  when( db.getReturnRowMeta() ).thenReturn( returnRowMeta );

  DatabaseLookupMeta meta = createTestMeta();
  DatabaseLookupData data = new DatabaseLookupData();

  DatabaseLookup step = createSpiedStep( db, mockHelper, meta );
  step.init( meta, data );


  data.db = db;
  data.keytypes = new int[] { ValueMetaInterface.TYPE_INTEGER };
  if ( allEquals ) {
    data.allEquals = true;
    data.conditions = new int[] { DatabaseLookupMeta.CONDITION_EQ };
  } else {
    data.allEquals = false;
    data.conditions = new int[] { DatabaseLookupMeta.CONDITION_LT };
  }
  step.processRow( meta, data );

  return data;
}
 
Example #24
Source File: TeraFastMeta.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
/**
 * @return the database.
 * @throws KettleException
 *           if an error occurs.
 */
public Database connectToDatabase() throws KettleException {
  if ( this.getDbMeta() != null ) {
    Database db = new Database( loggingObject, this.getDbMeta() );
    db.connect();
    return db;
  }
  throw new KettleException( MESSAGES.getString( "TeraFastMeta.Exception.ConnectionNotDefined" ) );
}
 
Example #25
Source File: TransProfileFactory.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
private RowMetaInterface getTableFields( LoggingObjectInterface parentLoggingObject ) throws KettleDatabaseException {
  Database database = new Database( parentLoggingObject, databaseMeta );
  try {
    database.connect();
    return database.getTableFields( schemaTable );
  } finally {
    database.disconnect();
  }

}
 
Example #26
Source File: DataSetDatabaseGroup.java    From pentaho-pdi-dataset with Apache License 2.0 5 votes vote down vote up
public static final void writeDataSetData( LoggingObjectInterface loggingObject, DataSetGroup dataSetGroup, String tableName,
                                           RowMetaInterface rowMeta, List<Object[]> rows ) throws KettleException {

  Database database = new Database( loggingObject, dataSetGroup.getDatabaseMeta() );
  try {
    database.connect();

    String schemaTable = dataSetGroup.getDatabaseMeta().getQuotedSchemaTableCombination( dataSetGroup.getSchemaName(), tableName );

    String sql;
    if ( database.checkTableExists( schemaTable ) ) {
      // Clean out old junk, allow for rollback
      //
      database.truncateTable( schemaTable );
      sql = database.getAlterTableStatement( schemaTable, rowMeta, null, false, null, true );
    } else {
      sql = database.getCreateTableStatement( schemaTable, rowMeta, null, false, null, true );
    }
    if ( !StringUtil.isEmpty( sql ) ) {
      database.execStatements( sql );
    }
    database.prepareInsert( rowMeta, schemaTable );
    for ( Object[] row : rows ) {
      database.setValuesInsert( new RowMetaAndData( rowMeta, row ) );
      database.insertRow();
    }

    database.commit();
  } finally {
    database.disconnect();
  }

}
 
Example #27
Source File: LucidDBStreamingLoaderMeta.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
public RowMetaInterface getRequiredFields( VariableSpace space ) throws KettleException {
  String realTableName = space.environmentSubstitute( tableName );
  String realSchemaName = space.environmentSubstitute( schemaName );

  if ( databaseMeta != null ) {
    Database db = new Database( loggingObject, databaseMeta );
    try {
      db.connect();

      if ( !Utils.isEmpty( realTableName ) ) {
        String schemaTable = databaseMeta.getQuotedSchemaTableCombination( realSchemaName, realTableName );

        // Check if this table exists...
        if ( db.checkTableExists( schemaTable ) ) {
          return db.getTableFields( schemaTable );
        } else {
          throw new KettleException( BaseMessages.getString(
            PKG, "LucidDBStreamingLoaderMeta.Exception.TableNotFound" ) );
        }
      } else {
        throw new KettleException( BaseMessages.getString(
          PKG, "LucidDBStreamingLoaderMeta.Exception.TableNotSpecified" ) );
      }
    } catch ( Exception e ) {
      throw new KettleException( BaseMessages.getString(
        PKG, "LucidDBStreamingLoaderMeta.Exception.ErrorGettingFields" ), e );
    } finally {
      db.disconnect();
    }
  } else {
    throw new KettleException( BaseMessages.getString(
      PKG, "LucidDBStreamingLoaderMeta.Exception.ConnectionNotDefined" ) );
  }

}
 
Example #28
Source File: InsertUpdateMetaTest.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
@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 );
}
 
Example #29
Source File: DatabaseLookupUTest.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
@Test
public void mySqlVariantDbIsLazyConverted() throws Exception {
  DatabaseLookupMeta meta = createDatabaseMeta();
  DatabaseLookupData data = createDatabaseData();
  Database db = createVirtualDb( meta.getDatabaseMeta() );

  DatabaseLookup lookup = spyLookup( mockHelper, db, meta.getDatabaseMeta() );

  lookup.init( meta, data );
  lookup.processRow( meta, data );

  verify( db ).getLookup( any( PreparedStatement.class ), anyBoolean(), eq( false ) );
}
 
Example #30
Source File: JobEntryTableExists.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
public Result execute( Result previousResult, int nr ) {
  Result result = previousResult;
  result.setResult( false );

  if ( connection != null ) {
    Database db = new Database( this, connection );
    db.shareVariablesWith( this );
    try {
      db.connect( parentJob.getTransactionId(), null );
      String realTablename = environmentSubstitute( tablename );
      String realSchemaname = environmentSubstitute( schemaname );

      if ( db.checkTableExists( realSchemaname, realTablename ) ) {
        if ( log.isDetailed() ) {
          logDetailed( BaseMessages.getString( PKG, "TableExists.Log.TableExists", realTablename ) );
        }
        result.setResult( true );
      } else {
        if ( log.isDetailed() ) {
          logDetailed( BaseMessages.getString( PKG, "TableExists.Log.TableNotExists", realTablename ) );
        }
      }
    } catch ( KettleDatabaseException dbe ) {
      result.setNrErrors( 1 );
      logError( BaseMessages.getString( PKG, "TableExists.Error.RunningJobEntry", dbe.getMessage() ) );
    } finally {
      if ( db != null ) {
        try {
          db.disconnect();
        } catch ( Exception e ) { /* Ignore */
        }
      }
    }
  } else {
    result.setNrErrors( 1 );
    logError( BaseMessages.getString( PKG, "TableExists.Error.NoConnectionDefined" ) );
  }

  return result;
}