org.pentaho.di.core.database.Database类的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(12.2k)|赞(0)|评价(0)|浏览(528)

本文整理了Java中org.pentaho.di.core.database.Database类的一些代码示例,展示了Database类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Database类的具体详情如下:
包路径:org.pentaho.di.core.database.Database
类名称:Database

Database介绍

[英]Database handles the process of connecting to, reading from, writing to and updating databases. The database specific parameters are defined in DatabaseInfo.
[中]数据库处理连接、读取、写入和更新数据库的过程。数据库特定参数在DatabaseInfo中定义。

代码示例

代码示例来源:origin: pentaho/pentaho-kettle

  1. protected void checkConnection() throws KettleDatabaseException {
  2. // check connection
  3. // connect and disconnect
  4. Database dbchecked = null;
  5. try {
  6. dbchecked = new Database( this, connection );
  7. dbchecked.shareVariablesWith( this );
  8. dbchecked.connect( parentJob.getTransactionId(), null );
  9. } finally {
  10. if ( dbchecked != null ) {
  11. dbchecked.disconnect();
  12. }
  13. }
  14. }

代码示例来源:origin: pentaho/pentaho-kettle

  1. private RowMetaInterface getTableFields( LoggingObjectInterface parentLoggingObject ) throws KettleDatabaseException {
  2. Database database = new Database( parentLoggingObject, databaseMeta );
  3. try {
  4. database.connect();
  5. return database.getTableFields( schemaTable );
  6. } finally {
  7. database.disconnect();
  8. }
  9. }

代码示例来源:origin: pentaho/pentaho-kettle

  1. public String getDDL( String tableName, RowMetaInterface fields, String tk, boolean use_autoinc, String pk,
  2. boolean semicolon ) throws KettleDatabaseException {
  3. String retval;
  4. // First, check for reserved SQL in the input row r...
  5. databaseMeta.quoteReservedWords( fields );
  6. String quotedTk = tk != null ? databaseMeta.quoteField( tk ) : null;
  7. if ( checkTableExists( tableName ) ) {
  8. retval = getAlterTableStatement( tableName, fields, quotedTk, use_autoinc, pk, semicolon );
  9. } else {
  10. retval = getCreateTableStatement( tableName, fields, quotedTk, use_autoinc, pk, semicolon );
  11. }
  12. return retval;
  13. }

代码示例来源:origin: pentaho/pentaho-kettle

  1. private void disconnectDb( Database db ) throws KettleDatabaseException {
  2. if ( db == null ) {
  3. return;
  4. }
  5. if ( !db.isAutoCommit() ) {
  6. db.commit( true );
  7. }
  8. db.disconnect();
  9. }

代码示例来源:origin: pentaho/pentaho-kettle

  1. protected RowMetaInterface getDatabaseTableFields( Database db, String schemaName, String tableName )
  2. throws KettleDatabaseException {
  3. // First try without connecting to the database... (can be S L O W)
  4. RowMetaInterface extraFields = db.getTableFieldsMeta( schemaName, tableName );
  5. if ( extraFields == null ) { // now we need to connect
  6. db.connect();
  7. extraFields = db.getTableFieldsMeta( schemaName, tableName );
  8. }
  9. return extraFields;
  10. }

代码示例来源:origin: pentaho/pentaho-kettle

  1. if ( prev != null && prev.size() > 0 ) {
  2. if ( !Utils.isEmpty( tableName ) ) {
  3. Database db = new Database( loggingObject, databaseMeta );
  4. db.shareVariablesWith( transMeta );
  5. try {
  6. db.connect();
  7. String cr_table = db.getDDL( schemaTable, prev, null, false, null, true );
  8. if ( idx_fields != null && idx_fields.length > 0 && !db.checkIndexExists( schemaTable, idx_fields ) ) {
  9. String indexname = "idx_" + tableName + "_lookup";
  10. cr_index =
  11. db.getCreateIndexStatement(
  12. schemaName, tableName, indexname, idx_fields, false, false, false, true );

代码示例来源:origin: pentaho/pentaho-kettle

  1. db = new Database( jobMeta, logTable.getDatabaseMeta() );
  2. db.shareVariablesWith( jobMeta );
  3. db.connect();
  4. String tableName = db.environmentSubstitute( logTable.getTableName() );
  5. String schemaTable =
  6. logTable.getDatabaseMeta().getQuotedSchemaTableCombination(
  7. db.environmentSubstitute( logTable.getSchemaName() ),
  8. db.environmentSubstitute( logTable.getTableName() ) );
  9. String createTable = db.getDDL( schemaTable, fields );
  10. if ( !index.isEmpty() ) {
  11. String createIndex =
  12. db.getCreateIndexStatement( schemaTable, "IDX_" + tableName + "_" + ( i + 1 ), index
  13. .getFieldNames(), false, false, false, true );
  14. if ( !Utils.isEmpty( createIndex ) ) {
  15. db.disconnect();

代码示例来源:origin: pentaho/pentaho-kettle

  1. public RowMetaInterface getRequiredFields( VariableSpace space ) throws KettleException {
  2. String realTableName = space.environmentSubstitute( tablename );
  3. String realSchemaName = space.environmentSubstitute( schemaName );
  4. if ( databaseMeta != null ) {
  5. Database db = new Database( loggingObject, databaseMeta );
  6. try {
  7. db.connect();
  8. if ( !Utils.isEmpty( realTableName ) ) {
  9. // Check if this table exists...
  10. if ( db.checkTableExists( realSchemaName, realTableName ) ) {
  11. return db.getTableFieldsMeta( realSchemaName, realTableName );
  12. } else {
  13. throw new KettleException( BaseMessages.getString( PKG, "SQLFileOutputMeta.Exception.TableNotFound" ) );
  14. }
  15. } else {
  16. throw new KettleException( BaseMessages.getString( PKG, "SQLFileOutputMeta.Exception.TableNotSpecified" ) );
  17. }
  18. } catch ( Exception e ) {
  19. throw new KettleException(
  20. BaseMessages.getString( PKG, "SQLFileOutputMeta.Exception.ErrorGettingFields" ), e );
  21. } finally {
  22. db.disconnect();
  23. }
  24. } else {
  25. throw new KettleException( BaseMessages.getString( PKG, "SQLFileOutputMeta.Exception.ConnectionNotDefined" ) );
  26. }
  27. }

代码示例来源:origin: pentaho/pentaho-kettle

  1. if ( !Utils.isEmpty( tablename ) ) {
  2. String schemaTable = databaseMeta.getQuotedSchemaTableCombination( schemaName, tablename );
  3. Database db = new Database( loggingObject, databaseMeta );
  4. try {
  5. boolean doHash = false;
  6. String cr_table = null;
  7. db.connect();
  8. if ( !db.checkTableExists( schemaTable ) ) {
  9. RowMetaInterface tabFields = db.getTableFields( schemaTable );
  10. db.getDDL(
  11. schemaTable, fields, ( CREATION_METHOD_SEQUENCE.equals( getTechKeyCreation() )
  12. && sequenceFrom != null && sequenceFrom.length() != 0 ) ? null : technicalKeyField,
  13. if ( !db.checkIndexExists( schemaTable, techKeyArr ) ) {
  14. String indexname = "idx_" + tablename + "_pk";
  15. cr_uniq_index =
  16. db.getCreateIndexStatement(
  17. schemaTable, indexname, techKeyArr, true, true, false, true );
  18. cr_uniq_index += Const.CR;
  19. if ( !Utils.isEmpty( idx_fields ) && !db.checkIndexExists( schemaTable, idx_fields ) ) {
  20. String indexname = "idx_" + tablename + "_lookup";
  21. cr_index =
  22. db.getCreateIndexStatement(
  23. schemaTable, indexname, idx_fields, false, false, false, true );

代码示例来源:origin: pentaho/pentaho-kettle

  1. public boolean init( StepMetaInterface smi, StepDataInterface sdi ) {
  2. meta = (InsertUpdateMeta) smi;
  3. data = (InsertUpdateData) sdi;
  4. if ( super.init( smi, sdi ) ) {
  5. try {
  6. if ( meta.getDatabaseMeta() == null ) {
  7. logError( BaseMessages.getString( PKG, "InsertUpdate.Init.ConnectionMissing", getStepname() ) );
  8. return false;
  9. }
  10. data.db = new Database( this, meta.getDatabaseMeta() );
  11. data.db.shareVariablesWith( this );
  12. if ( getTransMeta().isUsingUniqueConnections() ) {
  13. synchronized ( getTrans() ) {
  14. data.db.connect( getTrans().getTransactionId(), getPartitionID() );
  15. }
  16. } else {
  17. data.db.connect( getPartitionID() );
  18. }
  19. data.db.setCommit( meta.getCommitSize( this ) );
  20. return true;
  21. } catch ( KettleException ke ) {
  22. logError( BaseMessages.getString( PKG, "InsertUpdate.Log.ErrorOccurredDuringStepInitialize" )
  23. + ke.getMessage() );
  24. }
  25. }
  26. return false;
  27. }

代码示例来源:origin: pentaho/pentaho-kettle

  1. /**
  2. * Writes information to Job Log table. Cleans old records, in case job is finished.
  3. */
  4. protected void writeLogTableInformation( JobLogTable jobLogTable, LogStatus status ) throws KettleJobException,
  5. KettleDatabaseException {
  6. boolean cleanLogRecords = status.equals( LogStatus.END );
  7. String tableName = jobLogTable.getActualTableName();
  8. DatabaseMeta logcon = jobLogTable.getDatabaseMeta();
  9. Database ldb = createDataBase( logcon );
  10. ldb.shareVariablesWith( this );
  11. try {
  12. ldb.connect();
  13. ldb.setCommit( logCommitSize );
  14. ldb.writeLogRecord( jobLogTable, status, this, null );
  15. if ( cleanLogRecords ) {
  16. ldb.cleanupLogRecords( jobLogTable );
  17. }
  18. } catch ( KettleDatabaseException dbe ) {
  19. addErrors( 1 );
  20. throw new KettleJobException( "Unable to end processing by writing log record to table " + tableName, dbe );
  21. } finally {
  22. if ( !ldb.isAutoCommit() ) {
  23. ldb.commitLog( true, jobLogTable );
  24. }
  25. ldb.disconnect();
  26. }
  27. }

代码示例来源:origin: pentaho/pentaho-kettle

  1. public void run( IProgressMonitor monitor ) throws InvocationTargetException, InterruptedException {
  2. db = new Database( Spoon.loggingObject, dbMeta );
  3. try {
  4. db.connect();
  5. if ( limit > 0 ) {
  6. db.setQueryLimit( limit );
  7. }
  8. rows = db.getFirstRows( tableName, limit, new ProgressMonitorAdapter( monitor ) );
  9. rowMeta = db.getReturnRowMeta();
  10. } catch ( KettleException e ) {
  11. throw new InvocationTargetException( e, "Couldn't find any rows because of an error :" + e.toString() );
  12. } finally {
  13. db.disconnect();
  14. }
  15. }
  16. };

代码示例来源:origin: pentaho/pentaho-kettle

  1. @Override
  2. public SQLStatement getSQLStatements( TransMeta transMeta, StepMeta stepMeta, RowMetaInterface prev,
  3. Repository repository, IMetaStore metaStore ) {
  4. SQLStatement retval = new SQLStatement( stepMeta.getName(), database, null ); // default: nothing to do!
  5. if ( useDatabase ) {
  6. // Otherwise, don't bother!
  7. if ( database != null ) {
  8. Database db = new Database( loggingObject, database );
  9. db.shareVariablesWith( transMeta );
  10. try {
  11. db.connect();
  12. if ( !db.checkSequenceExists( schemaName, sequenceName ) ) {
  13. String cr_table = db.getCreateSequenceStatement( sequenceName, startAt, incrementBy, maxValue, true );
  14. retval.setSQL( cr_table );
  15. } else {
  16. retval.setSQL( null ); // Empty string means: nothing to do: set it to null...
  17. }
  18. } catch ( KettleException e ) {
  19. retval.setError( BaseMessages.getString( PKG, "AddSequenceMeta.ErrorMessage.UnableToConnectDB" )
  20. + Const.CR + e.getMessage() );
  21. } finally {
  22. db.disconnect();
  23. }
  24. } else {
  25. retval.setError( BaseMessages.getString( PKG, "AddSequenceMeta.ErrorMessage.NoConnectionDefined" ) );
  26. }
  27. }
  28. return retval;
  29. }

代码示例来源:origin: pentaho/pentaho-kettle

  1. if ( !Utils.isEmpty( schemaTable ) ) {
  2. Database db = createDatabaseObject();
  3. db.shareVariablesWith( transMeta );
  4. try {
  5. db.connect();
  6. db.getDDL( schemaTable, fields, ( sequenceName != null && sequenceName.length() != 0 ) ? null
  7. : keyField, autoIncrement, null, true );
  8. if ( !Utils.isEmpty( idx_fields ) && !db.checkIndexExists( schemaTable, idx_fields ) ) {
  9. String indexname = "idx_" + tableName + "_lookup";
  10. sql += db.getCreateIndexStatement( schemaTable, indexname, idx_fields, false, false, false, true );
  11. if ( !db.checkIndexExists( schemaTable, idx_fields ) ) {
  12. String indexname = "idx_" + tableName + "_tk";
  13. sql += db.getCreateIndexStatement( schemaTable, indexname, idx_fields, true, false, true, true );
  14. if ( !db.checkSequenceExists( schemaName, sequenceName ) ) {
  15. sql += db.getCreateSequenceStatement( schemaName, sequenceName, 1L, 1L, -1L, true );
  16. .getMessage() );
  17. } finally {
  18. db.disconnect();

代码示例来源:origin: pentaho/pentaho-kettle

  1. KettleDatabaseRepository.REP_STRING_LENGTH, 0 ) );
  2. sql =
  3. database.getDDL(
  4. schemaTable, table, null, false, KettleDatabaseRepository.FIELD_REPOSITORY_LOG_ID_REPOSITORY_LOG,
  5. false );
  6. database.execStatements( sql );
  7. if ( log.isDetailed() ) {
  8. log.logDetailed( "Created/altered table " + schemaTable );
  9. sql =
  10. database
  11. .getDDL( schemaTable, table, null, false, KettleDatabaseRepository.FIELD_VERSION_ID_VERSION, false );
  12. boolean create = false;
  13. if ( !Utils.isEmpty( sql ) ) {
  14. database.execStatements( sql );
  15. if ( log.isDetailed() ) {
  16. log.logDetailed( "Created/altered table " + schemaTable );
  17. Boolean.valueOf( upgrade ), };
  18. if ( dryrun ) {
  19. sql = database.getSQLOutput( null, KettleDatabaseRepository.TABLE_R_VERSION, table, data, null );
  20. statements.add( sql );
  21. } else {
  22. database.execStatement( "INSERT INTO "
  23. + databaseMeta.getQuotedSchemaTableCombination( null, KettleDatabaseRepository.TABLE_R_VERSION )
  24. + " VALUES(?, ?, ?, ?, ?)", table, data );

代码示例来源:origin: pentaho/pentaho-kettle

  1. @Test
  2. public void jobFail_columnNotExist() throws KettleException {
  3. doReturn( db ).when( jobEntry ).getNewDatabaseFromMeta();
  4. doNothing().when( db ).connect( anyString(), anyString() );
  5. doReturn( true ).when( db ).checkTableExists( anyString(), anyString() );
  6. doReturn( false ).when( db ).checkColumnExists( anyString(), anyString(), anyString() );
  7. final Result result = jobEntry.execute( new Result(), 0 );
  8. assertEquals( "Should be some errors", 1, result.getNrErrors() );
  9. assertFalse( "Result should be false", result.getResult() );
  10. verify( db, atLeastOnce() ).disconnect();
  11. }

代码示例来源:origin: pentaho/pentaho-kettle

  1. public void run( IProgressMonitor monitor ) throws InvocationTargetException, InterruptedException {
  2. db = new Database( Spoon.loggingObject, dbMeta );
  3. try {
  4. db.connect();
  5. result = db.getQueryFields( sql, false );
  6. if ( monitor.isCanceled() ) {
  7. throw new InvocationTargetException( new Exception( "This operation was cancelled!" ) );
  8. }
  9. } catch ( Exception e ) {
  10. throw new InvocationTargetException( e, "Problem encountered determining query fields: " + e.toString() );
  11. } finally {
  12. db.disconnect();
  13. }
  14. }
  15. };

代码示例来源:origin: pentaho/pentaho-kettle

  1. protected Database getDatabase() {
  2. // Added for test purposes
  3. return new Database( loggingObject, databaseMeta );
  4. }

代码示例来源:origin: pentaho/pentaho-kettle

  1. DatabaseMeta ci = transMeta.findDatabase( connectionName );
  2. if ( ci != null ) {
  3. Database db = new Database( loggingObject, ci );
  4. try {
  5. db.connect();
  6. RowMetaInterface r = db.getTableFields( schemaTable );
  7. if ( null != r ) {
  8. String[] fieldNames = r.getFieldNames();

代码示例来源:origin: pentaho/pentaho-kettle

  1. @Test
  2. public void jobFail_tableNotExist() throws KettleException {
  3. when( jobEntry.getNewDatabaseFromMeta() ).thenReturn( db );
  4. doNothing().when( db ).connect( anyString(), any() );
  5. doReturn( false ).when( db ).checkTableExists( anyString(), anyString() );
  6. final Result result = jobEntry.execute( new Result(), 0 );
  7. assertEquals( "Should be error", 1, result.getNrErrors() );
  8. assertFalse( "Result should be false", result.getResult() );
  9. verify( db, atLeastOnce() ).disconnect();
  10. }

相关文章