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

The following examples show how to use org.pentaho.di.core.database.Database#shareVariablesWith() . 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: Trans.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
/**
 * Writes step information to a step logging table (if one has been configured).
 *
 * @throws KettleException if any errors occur during logging
 */
protected void writeStepLogInformation() throws KettleException {
  Database db = null;
  StepLogTable stepLogTable = getTransMeta().getStepLogTable();
  try {
    db = createDataBase( stepLogTable.getDatabaseMeta() );
    db.shareVariablesWith( this );
    db.connect();
    db.setCommit( logCommitSize );

    for ( StepMetaDataCombi combi : getSteps() ) {
      db.writeLogRecord( stepLogTable, LogStatus.START, combi, null );
    }

    db.cleanupLogRecords( stepLogTable );
  } catch ( Exception e ) {
    throw new KettleException( BaseMessages.getString( PKG,
      "Trans.Exception.UnableToWriteStepInformationToLogTable" ), e );
  } finally {
    disconnectDb( db );
  }

}
 
Example 3
Source File: TableExistsDialog.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
private void getSchemaNames() {
  if ( wSchemaname.isDisposed() ) {
    return;
  }
  DatabaseMeta databaseMeta = transMeta.findDatabase( wConnection.getText() );
  if ( databaseMeta != null ) {
    Database database = new Database( loggingObject, databaseMeta );
    database.shareVariablesWith( transMeta );
    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, "System.Dialog.AvailableSchemas.Title", wConnection.getText() ),
            BaseMessages.getString( PKG, "System.Dialog.AvailableSchemas.Message" ) );
        String d = dialog.open();
        if ( d != null ) {
          wSchemaname.setText( Const.NVL( d.toString(), "" ) );
        }

      } else {
        MessageBox mb = new MessageBox( shell, SWT.OK | SWT.ICON_ERROR );
        mb.setMessage( BaseMessages.getString( PKG, "System.Dialog.AvailableSchemas.Empty.Message" ) );
        mb.setText( BaseMessages.getString( PKG, "System.Dialog.AvailableSchemas.Empty.Title" ) );
        mb.open();
      }
    } catch ( Exception e ) {
      new ErrorDialog( shell, BaseMessages.getString( PKG, "System.Dialog.Error.Title" ), BaseMessages
        .getString( PKG, "System.Dialog.AvailableSchemas.ConnectionError" ), e );
    } finally {
      if ( database != null ) {
        database.disconnect();
        database = null;
      }
    }
  }
}
 
Example 4
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 5
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 6
Source File: JobEntryTableExistsDialog.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
private void getSchemaNames() {
  if ( wSchemaname.isDisposed() ) {
    return;
  }
  DatabaseMeta databaseMeta = jobMeta.findDatabase( wConnection.getText() );
  if ( databaseMeta != null ) {
    Database database = new Database( loggingObject, databaseMeta );
    database.shareVariablesWith( jobMeta );
    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, "System.Dialog.AvailableSchemas.Title", wConnection.getText() ),
          BaseMessages.getString( PKG, "System.Dialog.AvailableSchemas.Message" ) );
        String d = dialog.open();
        if ( d != null ) {
          wSchemaname.setText( Const.NVL( d.toString(), "" ) );
        }

      } else {
        MessageBox mb = new MessageBox( shell, SWT.OK | SWT.ICON_ERROR );
        mb.setMessage( BaseMessages.getString( PKG, "System.Dialog.AvailableSchemas.Empty.Message" ) );
        mb.setText( BaseMessages.getString( PKG, "System.Dialog.AvailableSchemas.Empty.Title" ) );
        mb.open();
      }
    } catch ( Exception e ) {
      new ErrorDialog( shell, BaseMessages.getString( PKG, "System.Dialog.Error.Title" ),
        BaseMessages.getString( PKG, "System.Dialog.AvailableSchemas.ConnectionError" ), e );
    } finally {
      if ( database != null ) {
        database.disconnect();
        database = null;
      }
    }
  }
}
 
Example 7
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 8
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 9
Source File: Job.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
/**
 * Writes information to Job Log table. Cleans old records, in case job is finished.
 */
protected void writeLogTableInformation( JobLogTable jobLogTable, LogStatus status ) throws KettleJobException,
  KettleDatabaseException {
  boolean cleanLogRecords = status.equals( LogStatus.END );
  String tableName = jobLogTable.getActualTableName();
  DatabaseMeta logcon = jobLogTable.getDatabaseMeta();

  Database ldb = createDataBase( logcon );
  ldb.shareVariablesWith( this );
  try {
    ldb.connect();
    ldb.setCommit( logCommitSize );
    ldb.writeLogRecord( jobLogTable, status, this, null );

    if ( cleanLogRecords ) {
      ldb.cleanupLogRecords( jobLogTable );
    }

  } catch ( KettleDatabaseException dbe ) {
    addErrors( 1 );
    throw new KettleJobException( "Unable to end processing by writing log record to table " + tableName, dbe );
  } finally {
    if ( !ldb.isAutoCommit() ) {
      ldb.commitLog( true, jobLogTable );
    }
    ldb.disconnect();
  }
}
 
Example 10
Source File: JobEntryColumnsExist.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;
  result.setResult( false );
  result.setNrErrors( 1 );

  int nrexistcolums = 0;
  int nrnotexistcolums = 0;

  if ( Utils.isEmpty( tablename ) ) {
    logError( BaseMessages.getString( PKG, "JobEntryColumnsExist.Error.TablenameEmpty" ) );
    return result;
  }
  if ( arguments == null ) {
    logError( BaseMessages.getString( PKG, "JobEntryColumnsExist.Error.ColumnameEmpty" ) );
    return result;
  }
  if ( connection != null ) {
    Database db = getNewDatabaseFromMeta();
    db.shareVariablesWith( this );
    try {
      String realSchemaname = environmentSubstitute( schemaname );
      String realTablename = environmentSubstitute( tablename );

      db.connect( parentJob.getTransactionId(), null );

      if ( db.checkTableExists( realSchemaname, realTablename ) ) {
        if ( log.isDetailed() ) {
          logDetailed( BaseMessages.getString( PKG, "JobEntryColumnsExist.Log.TableExists", realTablename ) );
        }

        for ( int i = 0; i < arguments.length && !parentJob.isStopped(); i++ ) {
          String realColumnname = environmentSubstitute( arguments[i] );

          if ( db.checkColumnExists( realSchemaname, realTablename, realColumnname ) ) {
            if ( log.isDetailed() ) {
              logDetailed( BaseMessages.getString(
                PKG, "JobEntryColumnsExist.Log.ColumnExists", realColumnname, realTablename ) );
            }
            nrexistcolums++;
          } else {
            logError( BaseMessages.getString(
              PKG, "JobEntryColumnsExist.Log.ColumnNotExists", realColumnname, realTablename ) );
            nrnotexistcolums++;
          }
        }
      } else {
        logError( BaseMessages.getString( PKG, "JobEntryColumnsExist.Log.TableNotExists", realTablename ) );
      }
    } catch ( KettleDatabaseException dbe ) {
      logError( BaseMessages.getString( PKG, "JobEntryColumnsExist.Error.UnexpectedError", dbe.getMessage() ) );
    } finally {
      if ( db != null ) {
        try {
          db.disconnect();
        } catch ( Exception e ) { /* Ignore */
        }
      }
    }
  } else {
    logError( BaseMessages.getString( PKG, "JobEntryColumnsExist.Error.NoDbConnection" ) );
  }

  result.setEntryNr( nrnotexistcolums );
  result.setNrLinesWritten( nrexistcolums );
  // result is true only if all columns found (PDI-15801)
  if (  nrexistcolums == arguments.length ) {
    result.setNrErrors( 0 );
    result.setResult( true );
  }
  return result;
}
 
Example 11
Source File: InsertUpdateMeta.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
public SQLStatement getSQLStatements( TransMeta transMeta, StepMeta stepMeta, RowMetaInterface prev,
    Repository repository, IMetaStore metaStore ) throws KettleStepException {
  SQLStatement retval = new SQLStatement( stepMeta.getName(), databaseMeta, null ); // default: nothing to do!

  if ( databaseMeta != null ) {
    if ( prev != null && prev.size() > 0 ) {
      // Copy the row
      RowMetaInterface tableFields = RowMetaUtils.getRowMetaForUpdate( prev, keyLookup,
          keyStream, updateLookup, updateStream );

      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, tableFields, null, false, null, true );

          String cr_index = "";
          String[] idx_fields = null;

          if ( keyLookup != null && keyLookup.length > 0 ) {
            idx_fields = new String[keyLookup.length];
            for ( int i = 0; i < keyLookup.length; i++ ) {
              idx_fields[i] = keyLookup[i];
            }
          } else {
            retval.setError( BaseMessages.getString( PKG, "InsertUpdateMeta.CheckResult.MissingKeyFields" ) );
          }

          // Key lookup dimensions...
          if ( idx_fields != null
              && idx_fields.length > 0 && !db.checkIndexExists( schemaName, tableName, idx_fields ) ) {
            String indexname = "idx_" + tableName + "_lookup";
            cr_index =
              db.getCreateIndexStatement( schemaTable, indexname, idx_fields, false, false, false, true );
          }

          String sql = cr_table + cr_index;
          if ( sql.length() == 0 ) {
            retval.setSQL( null );
          } else {
            retval.setSQL( sql );
          }
        } catch ( KettleException e ) {
          retval.setError( BaseMessages.getString( PKG, "InsertUpdateMeta.ReturnValue.ErrorOccurred" )
              + e.getMessage() );
        }
      } else {
        retval
          .setError( BaseMessages.getString( PKG, "InsertUpdateMeta.ReturnValue.NoTableDefinedOnConnection" ) );
      }
    } else {
      retval.setError( BaseMessages.getString( PKG, "InsertUpdateMeta.ReturnValue.NotReceivingAnyFields" ) );
    }
  } else {
    retval.setError( BaseMessages.getString( PKG, "InsertUpdateMeta.ReturnValue.NoConnectionDefined" ) );
  }

  return retval;
}
 
Example 12
Source File: LucidDBBulkLoaderMeta.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
public SQLStatement getSQLStatements( TransMeta transMeta, StepMeta stepMeta, RowMetaInterface prev,
  Repository repository, IMetaStore metaStore ) throws KettleStepException {
  SQLStatement retval = new SQLStatement( stepMeta.getName(), databaseMeta, null ); // default: nothing to do!

  if ( databaseMeta != null ) {
    if ( prev != null && prev.size() > 0 ) {
      // Copy the row
      RowMetaInterface tableFields = new RowMeta();

      // Now change the field names
      for ( int i = 0; i < fieldTable.length; i++ ) {
        ValueMetaInterface v = prev.searchValueMeta( fieldStream[i] );
        if ( v != null ) {
          ValueMetaInterface tableField = v.clone();
          tableField.setName( fieldTable[i] );
          tableFields.addValueMeta( tableField );
        } else {
          throw new KettleStepException( "Unable to find field [" + fieldStream[i] + "] in the input rows" );
        }
      }

      if ( !Utils.isEmpty( tableName ) ) {
        Database db = new Database( loggingObject, databaseMeta );
        db.shareVariablesWith( transMeta );
        try {
          db.connect();

          String schemaTable =
            databaseMeta.getQuotedSchemaTableCombination(
              transMeta.environmentSubstitute( schemaName ), transMeta.environmentSubstitute( tableName ) );
          String sql = db.getDDL( schemaTable, tableFields, null, false, null, true );

          if ( Utils.isEmpty( sql ) ) {
            retval.setSQL( null );
          } else {
            retval.setSQL( sql );
          }
        } catch ( KettleException e ) {
          retval.setError( BaseMessages.getString( PKG, "LucidDBBulkLoaderMeta.GetSQL.ErrorOccurred" )
            + e.getMessage() );
        }
      } else {
        retval
          .setError( BaseMessages.getString( PKG, "LucidDBBulkLoaderMeta.GetSQL.NoTableDefinedOnConnection" ) );
      }
    } else {
      retval.setError( BaseMessages.getString( PKG, "LucidDBBulkLoaderMeta.GetSQL.NotReceivingAnyFields" ) );
    }
  } else {
    retval.setError( BaseMessages.getString( PKG, "LucidDBBulkLoaderMeta.GetSQL.NoConnectionDefined" ) );
  }

  return retval;
}
 
Example 13
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 14
Source File: PGBulkLoaderMeta.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
public SQLStatement getSQLStatements( TransMeta transMeta, StepMeta stepMeta, RowMetaInterface prev,
  Repository repository, IMetaStore metaStore ) throws KettleStepException {
  SQLStatement retval = new SQLStatement( stepMeta.getName(), databaseMeta, null ); // default: nothing to do!

  if ( databaseMeta != null ) {
    if ( prev != null && prev.size() > 0 ) {
      // Copy the row
      RowMetaInterface tableFields = new RowMeta();

      // Now change the field names
      for ( int i = 0; i < fieldTable.length; i++ ) {
        ValueMetaInterface v = prev.searchValueMeta( fieldStream[i] );
        if ( v != null ) {
          ValueMetaInterface tableField = v.clone();
          tableField.setName( fieldTable[i] );
          tableFields.addValueMeta( tableField );
        } else {
          throw new KettleStepException( "Unable to find field [" + fieldStream[i] + "] in the input rows" );
        }
      }

      if ( !Utils.isEmpty( tableName ) ) {
        Database db = new Database( loggingObject, databaseMeta );
        db.shareVariablesWith( transMeta );
        try {
          db.connect();

          String schemaTable =
            databaseMeta.getQuotedSchemaTableCombination(
              transMeta.environmentSubstitute( schemaName ), transMeta.environmentSubstitute( tableName ) );
          String sql = db.getDDL( schemaTable, tableFields, null, false, null, true );

          if ( sql.length() == 0 ) {
            retval.setSQL( null );
          } else {
            retval.setSQL( sql );
          }
        } catch ( KettleException e ) {
          retval.setError( BaseMessages.getString( PKG, "GPBulkLoaderMeta.GetSQL.ErrorOccurred" )
            + e.getMessage() );
        }
      } else {
        retval.setError( BaseMessages.getString( PKG, "GPBulkLoaderMeta.GetSQL.NoTableDefinedOnConnection" ) );
      }
    } else {
      retval.setError( BaseMessages.getString( PKG, "GPBulkLoaderMeta.GetSQL.NotReceivingAnyFields" ) );
    }
  } else {
    retval.setError( BaseMessages.getString( PKG, "GPBulkLoaderMeta.GetSQL.NoConnectionDefined" ) );
  }

  return retval;
}
 
Example 15
Source File: UpdateMeta.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
@Override
public SQLStatement getSQLStatements( TransMeta transMeta, StepMeta stepMeta, RowMetaInterface prev,
  Repository repository, IMetaStore metaStore ) throws KettleStepException  {
  SQLStatement retval = new SQLStatement( stepMeta.getName(), databaseMeta, null ); // default: nothing to do!

  if ( databaseMeta != null ) {
    if ( prev != null && prev.size() > 0 ) {
      // Copy the row
      RowMetaInterface tableFields = RowMetaUtils.getRowMetaForUpdate( prev, keyLookup, keyStream,
          updateLookup, updateStream );
      if ( !Utils.isEmpty( tableName ) ) {
        String schemaTable = databaseMeta.getQuotedSchemaTableCombination( schemaName, tableName );

        Database db = new Database( loggingObject, databaseMeta );
        db.shareVariablesWith( transMeta );
        try {
          db.connect();

          if ( getIgnoreFlagField() != null && getIgnoreFlagField().length() > 0 ) {
            prev.addValueMeta( new ValueMetaBoolean( getIgnoreFlagField() ) );
          }

          String cr_table = db.getDDL( schemaTable, tableFields, null, false, null, true );

          String cr_index = "";
          String[] idx_fields = null;

          if ( keyLookup != null && keyLookup.length > 0 ) {
            idx_fields = new String[keyLookup.length];
            for ( int i = 0; i < keyLookup.length; i++ ) {
              idx_fields[i] = keyLookup[i];
            }
          } else {
            retval.setError( BaseMessages.getString( PKG, "UpdateMeta.CheckResult.MissingKeyFields" ) );
          }

          // Key lookup dimensions...
          if ( idx_fields != null
            && idx_fields.length > 0 && !db.checkIndexExists( schemaTable, idx_fields ) ) {
            String indexname = "idx_" + tableName + "_lookup";
            cr_index =
              db.getCreateIndexStatement(
                schemaTable, indexname, idx_fields, false, false, false, true );
          }

          String sql = cr_table + cr_index;
          if ( sql.length() == 0 ) {
            retval.setSQL( null );
          } else {
            retval.setSQL( sql );
          }
        } catch ( KettleException e ) {
          retval.setError( BaseMessages.getString( PKG, "UpdateMeta.ReturnValue.ErrorOccurred" )
            + e.getMessage() );
        }
      } else {
        retval.setError( BaseMessages.getString( PKG, "UpdateMeta.ReturnValue.NoTableDefinedOnConnection" ) );
      }
    } else {
      retval.setError( BaseMessages.getString( PKG, "UpdateMeta.ReturnValue.NotReceivingAnyFields" ) );
    }
  } else {
    retval.setError( BaseMessages.getString( PKG, "UpdateMeta.ReturnValue.NoConnectionDefined" ) );
  }

  return retval;
}
 
Example 16
Source File: GPBulkLoaderMeta.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
@Override
public SQLStatement getSQLStatements( TransMeta transMeta, StepMeta stepMeta, RowMetaInterface prev,
  Repository repository, IMetaStore metaStore ) throws KettleStepException {
  SQLStatement retval = new SQLStatement( stepMeta.getName(), databaseMeta, null ); // default: nothing to do!

  if ( databaseMeta != null ) {
    if ( prev != null && prev.size() > 0 ) {
      // Copy the row
      RowMetaInterface tableFields = new RowMeta();

      // Now change the field names
      for ( int i = 0; i < fieldTable.length; i++ ) {
        ValueMetaInterface v = prev.searchValueMeta( fieldStream[i] );
        if ( v != null ) {
          ValueMetaInterface tableField = v.clone();
          tableField.setName( fieldTable[i] );
          tableFields.addValueMeta( tableField );
        } else {
          throw new KettleStepException( "Unable to find field [" + fieldStream[i] + "] in the input rows" );
        }
      }

      if ( !Utils.isEmpty( tableName ) ) {
        Database db = new Database( loggingObject, databaseMeta );
        db.shareVariablesWith( transMeta );
        try {
          db.connect();

          String schemaTable =
            databaseMeta.getQuotedSchemaTableCombination(
              transMeta.environmentSubstitute( schemaName ), transMeta.environmentSubstitute( tableName ) );
          String sql = db.getDDL( schemaTable, tableFields, null, false, null, true );

          if ( sql.length() == 0 ) {
            retval.setSQL( null );
          } else {
            retval.setSQL( sql );
          }
        } catch ( KettleException e ) {
          retval.setError( BaseMessages.getString( PKG, "GPBulkLoaderMeta.GetSQL.ErrorOccurred" )
            + e.getMessage() );
        }
      } else {
        retval.setError( BaseMessages.getString( PKG, "GPBulkLoaderMeta.GetSQL.NoTableDefinedOnConnection" ) );
      }
    } else {
      retval.setError( BaseMessages.getString( PKG, "GPBulkLoaderMeta.GetSQL.NotReceivingAnyFields" ) );
    }
  } else {
    retval.setError( BaseMessages.getString( PKG, "GPBulkLoaderMeta.GetSQL.NoConnectionDefined" ) );
  }

  return retval;
}
 
Example 17
Source File: GPLoadMeta.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
public SQLStatement getSQLStatements( TransMeta transMeta, StepMeta stepMeta, RowMetaInterface prev,
    Repository repository, IMetaStore metaStore ) throws KettleStepException {
  SQLStatement retval = new SQLStatement( stepMeta.getName(), databaseMeta, null ); // default: nothing to do!

  if ( databaseMeta != null ) {
    if ( prev != null && prev.size() > 0 ) {
      // Copy the row
      RowMetaInterface tableFields = new RowMeta();

      // Now change the field names
      for ( int i = 0; i < fieldTable.length; i++ ) {
        ValueMetaInterface v = prev.searchValueMeta( fieldStream[i] );
        if ( v != null ) {
          ValueMetaInterface tableField = v.clone();
          tableField.setName( fieldTable[i] );
          tableFields.addValueMeta( tableField );
        } else {
          throw new KettleStepException( "Unable to find field [" + fieldStream[i] + "] in the input rows" );
        }
      }

      if ( !Utils.isEmpty( tableName ) ) {
        Database db = new Database( loggingObject, databaseMeta );
        db.shareVariablesWith( transMeta );
        try {
          db.connect();

          String schemaTable =
              databaseMeta.getQuotedSchemaTableCombination( transMeta.environmentSubstitute( schemaName ), transMeta
                  .environmentSubstitute( tableName ) );
          String sql = db.getDDL( schemaTable, tableFields, null, false, null, true );

          if ( sql.length() == 0 ) {
            retval.setSQL( null );
          } else {
            retval.setSQL( sql );
          }
        } catch ( KettleException e ) {
          retval.setError( BaseMessages.getString( PKG, "GPLoadMeta.GetSQL.ErrorOccurred" ) + e.getMessage() );
        }
      } else {
        retval.setError( BaseMessages.getString( PKG, "GPLoadMeta.GetSQL.NoTableDefinedOnConnection" ) );
      }
    } else {
      retval.setError( BaseMessages.getString( PKG, "GPLoadMeta.GetSQL.NotReceivingAnyFields" ) );
    }
  } else {
    retval.setError( BaseMessages.getString( PKG, "GPLoadMeta.GetSQL.NoConnectionDefined" ) );
  }

  return retval;
}
 
Example 18
Source File: MySQLBulkLoaderMeta.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
public SQLStatement getSQLStatements( TransMeta transMeta, StepMeta stepMeta, RowMetaInterface prev,
    Repository repository, IMetaStore metaStore ) throws KettleStepException {
  SQLStatement retval = new SQLStatement( stepMeta.getName(), databaseMeta, null ); // default: nothing to do!

  if ( databaseMeta != null ) {
    if ( prev != null && prev.size() > 0 ) {
      // Copy the row
      RowMetaInterface tableFields = new RowMeta();

      // Now change the field names
      for ( int i = 0; i < fieldTable.length; i++ ) {
        ValueMetaInterface v = prev.searchValueMeta( fieldStream[i] );
        if ( v != null ) {
          ValueMetaInterface tableField = v.clone();
          tableField.setName( fieldTable[i] );
          tableFields.addValueMeta( tableField );
        } else {
          throw new KettleStepException( "Unable to find field [" + fieldStream[i] + "] in the input rows" );
        }
      }

      if ( !Utils.isEmpty( tableName ) ) {
        Database db = new Database( loggingObject, databaseMeta );
        db.shareVariablesWith( transMeta );
        try {
          db.connect();

          String schemaTable =
              databaseMeta.getQuotedSchemaTableCombination( transMeta.environmentSubstitute( schemaName ), transMeta
                  .environmentSubstitute( tableName ) );
          String cr_table = db.getDDL( schemaTable, tableFields, null, false, null, true );

          String sql = cr_table;
          if ( sql.length() == 0 ) {
            retval.setSQL( null );
          } else {
            retval.setSQL( sql );
          }
        } catch ( KettleException e ) {
          retval
              .setError( BaseMessages.getString( PKG, "MySQLBulkLoaderMeta.GetSQL.ErrorOccurred" ) + e.getMessage() );
        }
      } else {
        retval.setError( BaseMessages.getString( PKG, "MySQLBulkLoaderMeta.GetSQL.NoTableDefinedOnConnection" ) );
      }
    } else {
      retval.setError( BaseMessages.getString( PKG, "MySQLBulkLoaderMeta.GetSQL.NotReceivingAnyFields" ) );
    }
  } else {
    retval.setError( BaseMessages.getString( PKG, "MySQLBulkLoaderMeta.GetSQL.NoConnectionDefined" ) );
  }

  return retval;
}
 
Example 19
Source File: LucidDBStreamingLoaderMeta.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
@Override
public SQLStatement getSQLStatements( TransMeta transMeta, StepMeta stepMeta, RowMetaInterface prev,
  Repository repository, IMetaStore metaStore ) throws KettleStepException {

  SQLStatement retval = super.getSQLStatements( transMeta, stepMeta, prev, repository, metaStore );

  if ( databaseMeta != null ) {
    if ( prev != null && prev.size() > 0 ) {
      String schemaTable =
        databaseMeta.getQuotedSchemaTableCombination( transMeta.environmentSubstitute( schemaName ), transMeta
          .environmentSubstitute( tableName ) );

      if ( !Utils.isEmpty( schemaTable ) ) {
        Database db = new Database( loggingObject, databaseMeta );
        db.shareVariablesWith( transMeta );
        try {
          db.connect();

          String cr_table = db.getDDL( schemaTable, prev );

          // 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, "LucidDBStreamingLoaderMeta.Error.ErrorConnecting", dbe
            .getMessage() ) );
        } finally {
          db.disconnect();
        }
      } else {
        retval.setError( BaseMessages.getString( PKG, "LucidDBStreamingLoaderMeta.Error.NoTable" ) );
      }
    } else {
      retval.setError( BaseMessages.getString( PKG, "LucidDBStreamingLoaderMeta.Error.NoInput" ) );
    }
  } else {
    retval.setError( BaseMessages.getString( PKG, "LucidDBStreamingLoaderMeta.Error.NoConnection" ) );
  }

  return retval;
}
 
Example 20
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();
      }
    }
  }
}