Java Code Examples for org.pentaho.di.core.database.Database#disconnect()

The following examples show how to use org.pentaho.di.core.database.Database#disconnect() . 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: DimensionLookupMeta.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 = createDatabaseObject();
    try {
      db.connect();
      fields = db.getTableFieldsMeta( schemaName, tableName );
    } catch ( KettleDatabaseException dbe ) {
      logError( BaseMessages.getString( PKG, "DimensionLookupMeta.Log.DatabaseErrorOccurred" ) + dbe.getMessage() );
    } finally {
      db.disconnect();
    }
  }
  return fields;
}
 
Example 2
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 3
Source File: InsertUpdateMeta.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
public RowMetaInterface getRequiredFields( VariableSpace space ) throws KettleException {
  String realSchemaName = space.environmentSubstitute( schemaName );
  String realTableName = space.environmentSubstitute( tableName );

  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, "InsertUpdateMeta.Exception.TableNotFound" ) );
        }
      } else {
        throw new KettleException( BaseMessages.getString( PKG, "InsertUpdateMeta.Exception.TableNotSpecified" ) );
      }
    } catch ( Exception e ) {
      throw new KettleException(
        BaseMessages.getString( PKG, "InsertUpdateMeta.Exception.ErrorGettingFields" ), e );
    } finally {
      db.disconnect();
    }
  } else {
    throw new KettleException( BaseMessages.getString( PKG, "InsertUpdateMeta.Exception.ConnectionNotDefined" ) );
  }

}
 
Example 4
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 5
Source File: PGBulkLoaderMeta.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, "GPBulkLoaderMeta.Exception.TableNotFound" ) );
        }
      } else {
        throw new KettleException( BaseMessages.getString( PKG, "GPBulkLoaderMeta.Exception.TableNotSpecified" ) );
      }
    } catch ( Exception e ) {
      throw new KettleException(
        BaseMessages.getString( PKG, "GPBulkLoaderMeta.Exception.ErrorGettingFields" ), e );
    } finally {
      db.disconnect();
    }
  } else {
    throw new KettleException( BaseMessages.getString( PKG, "GPBulkLoaderMeta.Exception.ConnectionNotDefined" ) );
  }

}
 
Example 6
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 7
Source File: LucidDBBulkLoaderMeta.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, "LucidDBBulkLoaderMeta.Exception.TableNotFound" ) );
        }
      } else {
        throw new KettleException( BaseMessages.getString(
          PKG, "LucidDBBulkLoaderMeta.Exception.TableNotSpecified" ) );
      }
    } catch ( Exception e ) {
      throw new KettleException( BaseMessages.getString(
        PKG, "LucidDBBulkLoaderMeta.Exception.ErrorGettingFields" ), e );
    } finally {
      db.disconnect();
    }
  } else {
    throw new KettleException( BaseMessages.getString(
      PKG, "LucidDBBulkLoaderMeta.Exception.ConnectionNotDefined" ) );
  }

}
 
Example 8
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;
}
 
Example 9
Source File: UpdateDialog.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, "UpdateDialog.AvailableSchemas.Title", wConnection.getText() ),
          BaseMessages.getString( PKG, "UpdateDialog.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, "UpdateDialog.NoSchema.Error" ) );
        mb.setText( BaseMessages.getString( PKG, "UpdateDialog.GetSchemas.Error" ) );
        mb.open();
      }
    } catch ( Exception e ) {
      new ErrorDialog( shell,
        BaseMessages.getString( PKG, "System.Dialog.Error.Title" ),
        BaseMessages.getString( PKG, "UpdateDialog.ErrorGettingSchemas" ), e );
    } finally {
      database.disconnect();
    }
  }
}
 
Example 10
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 11
Source File: SynchronizeAfterMergeMeta.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, "SynchronizeAfterMergeMeta.Exception.TableNotFound" ) );
        }
      } else {
        throw new KettleException( BaseMessages.getString(
          PKG, "SynchronizeAfterMergeMeta.Exception.TableNotSpecified" ) );
      }
    } catch ( Exception e ) {
      throw new KettleException( BaseMessages.getString(
        PKG, "SynchronizeAfterMergeMeta.Exception.ErrorGettingFields" ), e );
    } finally {
      db.disconnect();
    }
  } else {
    throw new KettleException( BaseMessages.getString(
      PKG, "SynchronizeAfterMergeMeta.Exception.ConnectionNotDefined" ) );
  }

}
 
Example 12
Source File: JobEntryColumnsExistDialog.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
/**
 * Get a list of columns
 */
private void getListColumns() {
  if ( !Utils.isEmpty( wTablename.getText() ) ) {
    DatabaseMeta databaseMeta = jobMeta.findDatabase( wConnection.getText() );
    if ( databaseMeta != null ) {
      Database database = new Database( loggingObject, databaseMeta );
      database.shareVariablesWith( jobMeta );
      try {
        database.connect();
        RowMetaInterface row =
          database.getTableFieldsMeta(
            jobMeta.environmentSubstitute( wSchemaname.getText() ),
            jobMeta.environmentSubstitute( wTablename.getText() ) );
        if ( row != null ) {
          String[] available = row.getFieldNames();

          wFields.removeAll();
          for ( int i = 0; i < available.length; i++ ) {
            wFields.add( available[i] );
          }
          wFields.removeEmptyRows();
          wFields.setRowNums();
        } else {
          MessageBox mb = new MessageBox( shell, SWT.OK | SWT.ICON_ERROR );
          mb.setMessage( BaseMessages.getString( PKG, "JobEntryColumnsExist.GetListColumsNoRow.DialogMessage" ) );
          mb.setText( BaseMessages.getString( PKG, "System.Dialog.Error.Title" ) );
          mb.open();
        }
      } catch ( Exception e ) {
        new ErrorDialog( shell, BaseMessages.getString( PKG, "System.Dialog.Error.Title" ), BaseMessages
          .getString( PKG, "JobEntryColumnsExist.ConnectionError2.DialogMessage", wTablename.getText() ), e );
      } finally {
        database.disconnect();
      }
    }
  }
}
 
Example 13
Source File: TableOutputMeta.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
public SQLStatement getSQLStatements( TransMeta transMeta, StepMeta stepMeta, RowMetaInterface prev, String tk,
                                      boolean use_autoinc, String pk ) {
  SQLStatement retval = new SQLStatement( stepMeta.getName(), databaseMeta, null ); // default: nothing to do!

  if ( databaseMeta != null ) {
    if ( prev != null && prev.size() > 0 ) {
      if ( !Utils.isEmpty( tableName ) ) {
        Database db = new Database( loggingObject, databaseMeta );
        db.shareVariablesWith( transMeta );
        try {
          db.connect();

          String schemaTable = databaseMeta.getQuotedSchemaTableCombination( schemaName, tableName );
          String cr_table = db.getDDL( schemaTable, prev, tk, use_autoinc, pk );

          // Empty string means: nothing to do: set it to null...
          if ( cr_table == null || cr_table.length() == 0 ) {
            cr_table = null;
          }

          retval.setSQL( cr_table );
        } catch ( KettleDatabaseException dbe ) {
          retval.setError( BaseMessages.getString( PKG, "TableOutputMeta.Error.ErrorConnecting", dbe
            .getMessage() ) );
        } finally {
          db.disconnect();
        }
      } else {
        retval.setError( BaseMessages.getString( PKG, "TableOutputMeta.Error.NoTable" ) );
      }
    } else {
      retval.setError( BaseMessages.getString( PKG, "TableOutputMeta.Error.NoInput" ) );
    }
  } else {
    retval.setError( BaseMessages.getString( PKG, "TableOutputMeta.Error.NoConnection" ) );
  }

  return retval;
}
 
Example 14
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 15
Source File: DeleteDialog.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, "DeleteDialog.AvailableSchemas.Title", wConnection.getText() ),
            BaseMessages.getString( PKG, "DeleteDialog.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, "DeleteDialog.NoSchema.Error" ) );
        mb.setText( BaseMessages.getString( PKG, "DeleteDialog.GetSchemas.Error" ) );
        mb.open();
      }
    } catch ( Exception e ) {
      new ErrorDialog( shell, BaseMessages.getString( PKG, "System.Dialog.Error.Title" ), BaseMessages
        .getString( PKG, "DeleteDialog.ErrorGettingSchemas" ), e );
    } finally {
      database.disconnect();
    }
  }
}
 
Example 16
Source File: JobEntryTruncateTables.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
public Result execute( Result previousResult, int nr ) {
  Result result = previousResult;
  List<RowMetaAndData> rows = result.getRows();
  RowMetaAndData resultRow = null;

  result.setResult( true );
  nrErrors = 0;
  continueProcess = true;
  nrSuccess = 0;

  if ( argFromPrevious ) {
    if ( log.isDetailed() ) {
      logDetailed( BaseMessages.getString( PKG, "JobEntryTruncateTables.FoundPreviousRows", String
        .valueOf( ( rows != null ? rows.size() : 0 ) ) ) );
    }
    if ( rows.size() == 0 ) {
      return result;
    }
  }
  if ( connection != null ) {
    Database db = new Database( this, connection );
    db.shareVariablesWith( this );
    try {
      db.connect( parentJob.getTransactionId(), null );
      if ( argFromPrevious && rows != null ) { // Copy the input row to the (command line) arguments

        for ( int iteration = 0; iteration < rows.size() && !parentJob.isStopped() && continueProcess; iteration++ ) {
          resultRow = rows.get( iteration );

          // Get values from previous result
          String tablename_previous = resultRow.getString( 0, null );
          String schemaname_previous = resultRow.getString( 1, null );

          if ( !Utils.isEmpty( tablename_previous ) ) {
            if ( log.isDetailed() ) {
              logDetailed( BaseMessages.getString(
                PKG, "JobEntryTruncateTables.ProcessingRow", tablename_previous, schemaname_previous ) );
            }

            // let's truncate table
            if ( truncateTables( tablename_previous, schemaname_previous, db ) ) {
              updateSuccess();
            } else {
              updateErrors();
            }
          } else {
            logError( BaseMessages.getString( PKG, "JobEntryTruncateTables.RowEmpty" ) );
          }
        }

      } else if ( arguments != null ) {
        for ( int i = 0; i < arguments.length && !parentJob.isStopped() && continueProcess; i++ ) {
          String realTablename = environmentSubstitute( arguments[i] );
          String realSchemaname = environmentSubstitute( schemaname[i] );
          if ( !Utils.isEmpty( realTablename ) ) {
            if ( log.isDetailed() ) {
              logDetailed( BaseMessages.getString(
                PKG, "JobEntryTruncateTables.ProcessingArg", arguments[i], schemaname[i] ) );
            }

            // let's truncate table
            if ( truncateTables( realTablename, realSchemaname, db ) ) {
              updateSuccess();
            } else {
              updateErrors();
            }
          } else {
            logError( BaseMessages.getString(
              PKG, "JobEntryTruncateTables.ArgEmpty", arguments[i], schemaname[i] ) );
          }
        }
      }
    } catch ( Exception dbe ) {
      result.setNrErrors( 1 );
      logError( BaseMessages.getString( PKG, "JobEntryTruncateTables.Error.RunningEntry", dbe.getMessage() ) );
    } finally {
      if ( db != null ) {
        db.disconnect();
      }
    }
  } else {
    result.setNrErrors( 1 );
    logError( BaseMessages.getString( PKG, "JobEntryTruncateTables.NoDbConnection" ) );
  }

  result.setNrErrors( nrErrors );
  result.setNrLinesDeleted( nrSuccess );
  result.setResult( nrErrors == 0 );
  return result;
}
 
Example 17
Source File: DatabaseLookup.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
private void loadAllTableDataIntoTheCache() throws KettleException {
  DatabaseMeta dbMeta = meta.getDatabaseMeta();

  Database db = getDatabase( dbMeta );
  connectDatabase( db );

  try {
    // We only want to get the used table fields...
    //
    String sql = "SELECT ";

    for ( int i = 0; i < meta.getStreamKeyField1().length; i++ ) {
      if ( i > 0 ) {
        sql += ", ";
      }
      sql += dbMeta.quoteField( meta.getTableKeyField()[ i ] );
    }

    // Also grab the return field...
    //
    for ( int i = 0; i < meta.getReturnValueField().length; i++ ) {
      sql += ", " + dbMeta.quoteField( meta.getReturnValueField()[ i ] );
    }
    // The schema/table
    //
    sql += " FROM "
      + dbMeta.getQuotedSchemaTableCombination(
        environmentSubstitute( meta.getSchemaName() ),
        environmentSubstitute( meta.getTablename() ) );

    // order by?
    if ( meta.getOrderByClause() != null && meta.getOrderByClause().length() != 0 ) {
      sql += " ORDER BY " + meta.getOrderByClause();
    }

    // Now that we have the SQL constructed, let's store the rows...
    //
    List<Object[]> rows = db.getRows( sql, 0 );
    if ( rows != null && rows.size() > 0 ) {
      if ( data.allEquals ) {
        putToDefaultCache( db, rows );
      } else {
        putToReadOnlyCache( db, rows );
      }
    }
  } catch ( Exception e ) {
    throw new KettleException( e );
  } finally {
    if ( db != null ) {
      db.disconnect();
    }
  }
}
 
Example 18
Source File: AutoModeler.java    From pentaho-metadata with GNU Lesser General Public License v2.1 4 votes vote down vote up
public Domain generateDomain( final ImportStrategy importStrategy ) throws PentahoMetadataException {
  Domain domain = new Domain();
  domain.setId( modelName );

  List<LocaleType> locales = new ArrayList<LocaleType>();
  locales.add( new LocaleType( "en_US", "English (US)" ) ); //$NON-NLS-1$ //$NON-NLS-2$
  domain.setLocales( locales );

  SqlPhysicalModel physicalModel = new SqlPhysicalModel();
  physicalModel.setId( databaseMeta.getName() );
  physicalModel.setDatasource( ThinModelConverter.convertFromLegacy( databaseMeta ) );

  Database database = database();

  try {
    // Add the database connection to the empty schema...
    //
    domain.addPhysicalModel( physicalModel );

    // Also add a model with the same name as the model name...
    //
    String bmID = Util.getLogicalModelIdPrefix() + "_" + modelName.replaceAll( " ", "_" ).toUpperCase(); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    LogicalModel logicalModel = new LogicalModel();
    logicalModel.setId( bmID );
    domain.addLogicalModel( logicalModel );

    // Connect to the database...
    //
    database.connect();

    // clear the cache
    DBCache.getInstance().clear( databaseMeta.getName() );

    for ( int i = 0; i < tableNames.length; i++ ) {
      SchemaTable schemaTable = tableNames[i];

      // Import the specified tables and turn them into PhysicalTable
      // objects...
      //
      SqlPhysicalTable physicalTable =
          PhysicalTableImporter.importTableDefinition( database, schemaTable.getSchemaName(), schemaTable
          .getTableName(), locale, importStrategy );
      physicalModel.addPhysicalTable( physicalTable );

      // At the same time, we will create a business table and add that to the
      // business model...
      //
      LogicalTable businessTable = createBusinessTable( physicalTable, locale );
      logicalModel.addLogicalTable( businessTable );
    }
  } catch ( Exception e ) {
    // For the unexpected stuff, just throw the exception upstairs.
    //
    throw new PentahoMetadataException( e );
  } finally {
    // Make sure to close the connection
    //
    database.disconnect();
  }

  return domain;
}
 
Example 19
Source File: ScriptValuesAddedFunctions.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings( "unused" )
public static Object fireToDB( Context actualContext, Scriptable actualObject, Object[] ArgList,
  Function FunctionContext ) {

  Object oRC = new Object();
  if ( ArgList.length == 2 ) {
    try {
      Object scmO = actualObject.get( "_step_", actualObject );
      ScriptValuesMod scm = (ScriptValuesMod) Context.jsToJava( scmO, ScriptValuesMod.class );
      String strDBName = Context.toString( ArgList[0] );
      String strSQL = Context.toString( ArgList[1] );
      DatabaseMeta ci = DatabaseMeta.findDatabase( scm.getTransMeta().getDatabases(), strDBName );
      if ( ci == null ) {
        throw Context.reportRuntimeError( "Database connection not found: " + strDBName );
      }
      ci.shareVariablesWith( scm );

      Database db = new Database( scm, ci );
      db.setQueryLimit( 0 );
      try {
        if ( scm.getTransMeta().isUsingUniqueConnections() ) {
          synchronized ( scm.getTrans() ) {
            db.connect( scm.getTrans().getTransactionId(), scm.getPartitionID() );
          }
        } else {
          db.connect( scm.getPartitionID() );
        }

        ResultSet rs = db.openQuery( strSQL );
        ResultSetMetaData resultSetMetaData = rs.getMetaData();
        int columnCount = resultSetMetaData.getColumnCount();
        if ( rs != null ) {
          List<Object[]> list = new ArrayList<Object[]>();
          while ( rs.next() ) {
            Object[] objRow = new Object[columnCount];
            for ( int i = 0; i < columnCount; i++ ) {
              objRow[i] = rs.getObject( i + 1 );
            }
            list.add( objRow );
          }
          Object[][] resultArr = new Object[list.size()][];
          list.toArray( resultArr );
          db.disconnect();
          return resultArr;
        }
      } catch ( Exception er ) {
        throw Context.reportRuntimeError( er.toString() );
      }
    } catch ( Exception e ) {
      throw Context.reportRuntimeError( e.toString() );
    }
  } else {
    throw Context.reportRuntimeError( "The function call fireToDB requires 2 arguments." );
  }
  return oRC;
}
 
Example 20
Source File: InfobrightLoaderData.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
void databaseSetup( InfobrightLoaderMeta meta, InfobrightLoader step ) throws KettleException {

    db = new Database( step, meta.getDatabaseMeta() );
    db.connect();

    // FIXME: This will fail if the first row of the table contains a value that
    // cannot be read by Java. For example, a DATE field that contains the value
    // '0000-00-00'. In this case, the Kettle error message will misleadingly say
    // that the table doesn't exist. There doesn't seem to be any workaround.
    // See Pentaho JIRA: PDI-2117.
    //
    requiredRowMeta = meta.getRequiredFields( step );
    requiredFields = requiredRowMeta.getFieldNames();

    try {
      // once the loader is built, this db connection cannot be used by this thread anymore.
      // the loader is using it and any other uses of the connection will block.
      if ( meta.getInfobrightProductType() == null ) {
        meta.setDataFormat( DataFormat.TXT_VARIABLE ); // default for ICE
      }
      DataFormat dataFormat = DataFormat.valueForDisplayName( meta.getInfobrightProductType() );
      int agentPort = meta.getAgentPort();
      Charset charset = meta.getCharset();
      Connection conn = db.getConnection();
      String tableName =
        meta
          .getDatabaseMeta().getQuotedSchemaTableCombination(
            step.environmentSubstitute( meta.getSchemaName() ),
            step.environmentSubstitute( meta.getTableName() ) );
      EtlLogger logger = new KettleEtlLogger( step );
      loader = new InfobrightNamedPipeLoader( tableName, conn, logger, dataFormat, charset, agentPort );
      loader.setTimeout( 30 );
      String debugFile = meta.getDebugFile();
      if ( debugFile != null ) {
        OutputStream debugOutputStream = new FileOutputStream( debugFile );
        loader.setDebugOutputStream( debugOutputStream );
      }
      record = loader.createRecord( false ); // TODO set to true to support error path
      loader.start();

    } catch ( Exception e ) {
      db.disconnect();
      db = null;
      if ( loader != null ) {
        try {
          loader.killQuery();
        } catch ( SQLException e1 ) {
          throw new KettleDatabaseException( e1 );
        }
      }
      throw new KettleDatabaseException( e );
    }
  }