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

The following examples show how to use org.pentaho.di.core.database.Database#getTableFieldsMeta() . 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: TeraFastMeta.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * @see org.pentaho.di.trans.step.BaseStepMeta#getRequiredFields(org.pentaho.di.core.variables.VariableSpace)
 */
@Override
public RowMetaInterface getRequiredFields( final VariableSpace space ) throws KettleException {
  if ( !this.useControlFile.getValue() ) {
    final Database database = connectToDatabase();
    database.shareVariablesWith( space );

    RowMetaInterface fields =
      database.getTableFieldsMeta(
        StringUtils.EMPTY,
        space.environmentSubstitute( this.targetTable.getValue() ) );
    database.disconnect();
    if ( fields == null ) {
      throw new KettleException( MESSAGES.getString( "TeraFastMeta.Exception.TableNotFound" ) );
    }
    return fields;
  }
  return null;
}
 
Example 2
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 3
Source File: DimensionLookupMeta.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
protected RowMetaInterface getDatabaseTableFields( Database db, String schemaName, String tableName )
  throws KettleDatabaseException {
  // First try without connecting to the database... (can be S L O W)
  RowMetaInterface extraFields = db.getTableFieldsMeta( schemaName, tableName );
  if ( extraFields == null ) { // now we need to connect
    db.connect();
    extraFields = db.getTableFieldsMeta( schemaName, tableName );
  }
  return extraFields;
}
 
Example 4
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 5
Source File: SQLFileOutputMeta.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, "SQLFileOutputMeta.Exception.TableNotFound" ) );
        }
      } else {
        throw new KettleException( BaseMessages.getString( PKG, "SQLFileOutputMeta.Exception.TableNotSpecified" ) );
      }
    } catch ( Exception e ) {
      throw new KettleException(
        BaseMessages.getString( PKG, "SQLFileOutputMeta.Exception.ErrorGettingFields" ), e );
    } finally {
      db.disconnect();
    }
  } else {
    throw new KettleException( BaseMessages.getString( PKG, "SQLFileOutputMeta.Exception.ConnectionNotDefined" ) );
  }
}
 
Example 6
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 7
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 8
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 9
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 10
Source File: JobEntryMysqlBulkFileDialog.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
/**
 * Get a list of columns, comma separated, allow the user to select from it.
 *
 */
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( wSchemaname.getText(), wTablename.getText() );
        String[] available = row.getFieldNames();

        String[] source = wListColumn.getText().split( "," );
        for ( int i = 0; i < source.length; i++ ) {
          source[i] = Const.trim( source[i] );
        }
        int[] idxSource = Const.indexsOfStrings( source, available );
        EnterSelectionDialog dialog = new EnterSelectionDialog( shell, available,
          BaseMessages.getString( PKG, "JobMysqlBulkFile.SelectColumns.Title" ),
          BaseMessages.getString( PKG, "JobMysqlBulkFile.SelectColumns.Message" ) );
        dialog.setMulti( true );
        dialog.setAvoidQuickSearch();
        dialog.setSelectedNrs( idxSource );
        if ( dialog.open() != null ) {
          String columns = "";
          int[] idx = dialog.getSelectionIndeces();
          for ( int i = 0; i < idx.length; i++ ) {
            if ( i > 0 ) {
              columns += ", ";
            }
            columns += available[idx[i]];
          }
          wListColumn.setText( columns );
        }
      } catch ( KettleDatabaseException e ) {
        new ErrorDialog( shell, BaseMessages.getString( PKG, "System.Dialog.Error.Title" ), BaseMessages
          .getString( PKG, "JobMysqlBulkFile.ConnectionError2.DialogMessage" ), e );
      } finally {
        database.disconnect();
      }
    }
  }
}
 
Example 11
Source File: JobEntryMssqlBulkLoadDialog.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
/**
 * Get a list of columns, comma separated, allow the user to select from it.
 */
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( wSchemaname.getText(), wTablename.getText() );
        String[] available = row.getFieldNames();

        String[] source = wOrderBy.getText().split( "," );
        for ( int i = 0; i < source.length; i++ ) {
          source[i] = Const.trim( source[i] );
        }
        int[] idxSource = Const.indexsOfStrings( source, available );
        EnterSelectionDialog dialog = new EnterSelectionDialog( shell, available,
          BaseMessages.getString( PKG, "JobMssqlBulkLoad.SelectColumns.Title" ),
          BaseMessages.getString( PKG, "JobMssqlBulkLoad.SelectColumns.Message" ) );
        dialog.setMulti( true );
        dialog.setAvoidQuickSearch();
        dialog.setSelectedNrs( idxSource );
        if ( dialog.open() != null ) {
          String columns = "";
          int[] idx = dialog.getSelectionIndeces();
          for ( int i = 0; i < idx.length; i++ ) {
            if ( i > 0 ) {
              columns += ", ";
            }
            columns += available[idx[i]];
          }
          wOrderBy.setText( columns );
        }
      } catch ( KettleDatabaseException e ) {
        new ErrorDialog( shell, BaseMessages.getString( PKG, "System.Dialog.Error.Title" ), BaseMessages
          .getString( PKG, "JobMssqlBulkLoad.ConnectionError2.DialogMessage" ), e );
      } finally {
        database.disconnect();
      }
    }
  }
}
 
Example 12
Source File: DimensionLookupDialog.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
private void getFieldsFromTable() {
  if ( !gotTableFields ) {
    if ( !Utils.isEmpty( wTable.getText() ) ) {
      DatabaseMeta ci = transMeta.findDatabase( wConnection.getText() );
      if ( ci != null ) {
        Database db = new Database( loggingObject, ci );
        try {
          db.connect();
          RowMetaInterface r =
            db.getTableFieldsMeta(
              transMeta.environmentSubstitute( wSchema.getText() ),
              transMeta.environmentSubstitute( wTable.getText() ) );
          if ( null != r ) {
            String[] fieldNames = r.getFieldNames();
            if ( null != fieldNames ) {
              // Version
              String version = wVersion.getText();
              wVersion.setItems( fieldNames );
              if ( version != null ) {
                wVersion.setText( version );
              }
              // from date
              String fromdate = wFromdate.getText();
              wFromdate.setItems( fieldNames );
              if ( fromdate != null ) {
                wFromdate.setText( fromdate );
              }
              // to date
              String todate = wTodate.getText();
              wTodate.setItems( fieldNames );
              if ( todate != null ) {
                wTodate.setText( todate );
              }
              // tk
              String tk = wTk.getText();
              wTk.setItems( fieldNames );
              if ( tk != null ) {
                wTk.setText( tk );
              }
              // AltStartDateField
              String sd = wAltStartDateField.getText();
              wAltStartDateField.setItems( fieldNames );
              if ( sd != null ) {
                wAltStartDateField.setText( sd );
              }
            }
          }
        } catch ( Exception e ) {

          // ignore any errors here. drop downs will not be
          // filled, but no problem for the user
        }
      }
    }
    gotTableFields = true;
  }
}