Java Code Examples for org.pentaho.di.core.row.ValueMetaInterface#clone()

The following examples show how to use org.pentaho.di.core.row.ValueMetaInterface#clone() . 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: MonetDBBulkLoaderMeta.java    From pentaho-kettle with Apache License 2.0 6 votes vote down vote up
public RowMetaInterface updateFields( RowMetaInterface prev, MonetDBBulkLoaderData data ) {
  // update the field table from the fields coming from the previous step
  RowMetaInterface tableFields = new RowMeta();
  List<ValueMetaInterface> fields = prev.getValueMetaList();
  fieldTable = new String[fields.size()];
  fieldStream = new String[fields.size()];
  fieldFormatOk = new boolean[fields.size()];
  int idx = 0;
  for ( ValueMetaInterface field : fields ) {
    ValueMetaInterface tableField = field.clone();
    tableFields.addValueMeta( tableField );
    fieldTable[idx] = field.getName();
    fieldStream[idx] = field.getName();
    fieldFormatOk[idx] = true;
    idx++;
  }

  data.keynrs = new int[getFieldStream().length];
  for ( int i = 0; i < data.keynrs.length; i++ ) {
    data.keynrs[i] = i;
  }
  return tableFields;
}
 
Example 2
Source File: MemoryGroupBy.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
private void initGroupMeta( RowMetaInterface previousRowMeta ) throws KettleValueException {
  data.groupMeta = new RowMeta();
  data.entryMeta = new RowMeta();

  for ( int i = 0; i < data.groupnrs.length; i++ ) {
    ValueMetaInterface valueMeta = previousRowMeta.getValueMeta( data.groupnrs[i] );
    data.groupMeta.addValueMeta( valueMeta );

    ValueMetaInterface normalMeta = valueMeta.clone();
    normalMeta.setStorageType( ValueMetaInterface.STORAGE_TYPE_NORMAL );
  }

  return;
}
 
Example 3
Source File: FlattenerMeta.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
@Override
public void getFields( RowMetaInterface row, String name, RowMetaInterface[] info, StepMeta nextStep,
  VariableSpace space, Repository repository, IMetaStore metaStore ) throws KettleStepException {

  // Remove the key value (there will be different entries for each output row)
  //
  if ( fieldName != null && fieldName.length() > 0 ) {
    int idx = row.indexOfValue( fieldName );
    if ( idx < 0 ) {
      throw new KettleStepException( BaseMessages.getString(
        PKG, "FlattenerMeta.Exception.UnableToLocateFieldInInputFields", fieldName ) );
    }

    ValueMetaInterface v = row.getValueMeta( idx );
    row.removeValueMeta( idx );

    for ( int i = 0; i < targetField.length; i++ ) {
      ValueMetaInterface value = v.clone();
      value.setName( targetField[i] );
      value.setOrigin( name );

      row.addValueMeta( value );
    }
  } else {
    throw new KettleStepException( BaseMessages.getString( PKG, "FlattenerMeta.Exception.FlattenFieldRequired" ) );
  }
}
 
Example 4
Source File: PostgreSQLDatabaseMeta.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
/**
 * Generates the SQL statement to modify a column in the specified table
 *
 * @param tablename
 *          The table to add
 * @param v
 *          The column defined as a value
 * @param tk
 *          the name of the technical key field
 * @param useAutoinc
 *          whether or not this field uses auto increment
 * @param pk
 *          the name of the primary key field
 * @param semicolon
 *          whether or not to add a semi-colon behind the statement.
 * @return the SQL statement to modify a column in the specified table
 */
@Override
public String getModifyColumnStatement( String tablename, ValueMetaInterface v, String tk, boolean useAutoinc,
    String pk, boolean semicolon ) {
  String retval = "";

  ValueMetaInterface tmpColumn = v.clone();

  String tmpName = v.getName();
  boolean isQuoted = tmpName.startsWith( getStartQuote() ) && tmpName.endsWith( getEndQuote() );
  if ( isQuoted ) {
    // remove the quotes first.
    //
    tmpName = tmpName.substring( 1, tmpName.length() - 1 );
  }

  tmpName += "_KTL";

  // put the quotes back if needed.
  //
  if ( isQuoted ) {
    tmpName = getStartQuote() + tmpName + getEndQuote();
  }
  tmpColumn.setName( tmpName );

  // Create a new tmp column
  retval += getAddColumnStatement( tablename, tmpColumn, tk, useAutoinc, pk, semicolon ) + ";" + Const.CR;
  // copy the old data over to the tmp column
  retval += "UPDATE " + tablename + " SET " + tmpColumn.getName() + "=" + v.getName() + ";" + Const.CR;
  // drop the old column
  retval += getDropColumnStatement( tablename, v, tk, useAutoinc, pk, semicolon ) + ";" + Const.CR;
  // rename the temp column to replace the removed column
  retval += "ALTER TABLE " + tablename + " RENAME " + tmpColumn.getName() + " TO " + v.getName() + ";" + Const.CR;
  return retval;
}
 
Example 5
Source File: DatabaseLookup.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
private void useReturnValueTypeFromDatabase( RowMetaInterface fields ) {
  final String[] returnFields = meta.getReturnValueField();
  final int returnFieldsOffset = getInputRowMeta().size();
  for ( int i = 0; i < returnFields.length; i++ ) {
    ValueMetaInterface returnValueMeta = fields.searchValueMeta( returnFields[ i ] );
    if ( returnValueMeta != null ) {
      ValueMetaInterface v = data.outputRowMeta.getValueMeta( returnFieldsOffset + i );
      if ( v.getType() != returnValueMeta.getType() ) {
        ValueMetaInterface clone = returnValueMeta.clone();
        clone.setName( v.getName() );
        data.outputRowMeta.setValueMeta( returnFieldsOffset + i, clone );
      }
    }
  }
}
 
Example 6
Source File: ValueMetaFactory.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
public static ValueMetaInterface cloneValueMeta( ValueMetaInterface source, int targetType ) throws KettlePluginException {
  ValueMetaInterface target = null;

  // If we're Cloneable and not changing types, call clone()
  if ( source.getType() == targetType ) {
    target = source.clone();
  } else {
    target = createValueMeta( source.getName(), targetType, source.getLength(), source.getPrecision() );
  }

  cloneInfo( source, target );

  return target;
}
 
Example 7
Source File: NormaliserMeta.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
@Override
public void getFields( RowMetaInterface row, String name, RowMetaInterface[] info, StepMeta nextStep,
    VariableSpace space, Repository repository, IMetaStore metaStore ) throws KettleStepException {

  // Get a unique list of the occurrences of the type
  //
  List<String> norm_occ = new ArrayList<>();
  List<String> field_occ = new ArrayList<>();
  int maxlen = 0;
  for ( int i = 0; i < normaliserFields.length; i++ ) {
    if ( !norm_occ.contains( normaliserFields[i].getNorm() ) ) {
      norm_occ.add( normaliserFields[i].getNorm() );
      field_occ.add( normaliserFields[i].getName() );
    }

    if ( normaliserFields[i].getValue().length() > maxlen ) {
      maxlen = normaliserFields[i].getValue().length();
    }
  }

  // Then add the type field!
  //
  ValueMetaInterface typefield_value = new ValueMetaString( typeField );
  typefield_value.setOrigin( name );
  typefield_value.setLength( maxlen );
  row.addValueMeta( typefield_value );

  // Loop over the distinct list of fieldNorm[i]
  // Add the new fields that need to be created.
  // Use the same data type as the original fieldname...
  //
  for ( int i = 0; i < norm_occ.size(); i++ ) {
    String normname = norm_occ.get( i );
    String fieldname = field_occ.get( i );
    ValueMetaInterface v = row.searchValueMeta( fieldname );
    if ( v != null ) {
      v = v.clone();
    } else {
      throw new KettleStepException( BaseMessages.getString( PKG, "NormaliserMeta.Exception.UnableToFindField", fieldname ) );
    }
    v.setName( normname );
    v.setOrigin( name );
    row.addValueMeta( v );
  }

  // Now remove all the normalized fields...
  //
  for ( int i = 0; i < normaliserFields.length; i++ ) {
    int idx = row.indexOfValue( normaliserFields[i].getName() );
    if ( idx >= 0 ) {
      row.removeValueMeta( idx );
    }
  }
}
 
Example 8
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 9
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 10
Source File: S3CsvInputMeta.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
@Override
public void getFields( RowMetaInterface rowMeta, String origin, RowMetaInterface[] info, StepMeta nextStep,
    VariableSpace space, Repository repository, IMetaStore metaStore ) throws KettleStepException {
  rowMeta.clear(); // Start with a clean slate, eats the input

  for ( int i = 0; i < inputFields.length; i++ ) {
    TextFileInputField field = inputFields[i];

    ValueMetaInterface valueMeta = new ValueMeta( field.getName(), field.getType() );
    valueMeta.setConversionMask( field.getFormat() );
    valueMeta.setLength( field.getLength() );
    valueMeta.setPrecision( field.getPrecision() );
    valueMeta.setConversionMask( field.getFormat() );
    valueMeta.setDecimalSymbol( field.getDecimalSymbol() );
    valueMeta.setGroupingSymbol( field.getGroupSymbol() );
    valueMeta.setCurrencySymbol( field.getCurrencySymbol() );
    valueMeta.setTrimType( field.getTrimType() );
    if ( lazyConversionActive ) {
      valueMeta.setStorageType( ValueMetaInterface.STORAGE_TYPE_BINARY_STRING );
    }

    // In case we want to convert Strings...
    // Using a copy of the valueMeta object means that the inner and outer representation format is the same.
    // Preview will show the data the same way as we read it.
    // This layout is then taken further down the road by the metadata through the transformation.
    //
    ValueMetaInterface storageMetadata = valueMeta.clone();
    storageMetadata.setType( ValueMetaInterface.TYPE_STRING );
    storageMetadata.setStorageType( ValueMetaInterface.STORAGE_TYPE_NORMAL );
    storageMetadata.setLength( -1, -1 ); // we don't really know the lengths of the strings read in advance.
    valueMeta.setStorageMetadata( storageMetadata );

    valueMeta.setOrigin( origin );

    rowMeta.addValueMeta( valueMeta );
  }

  if ( !Utils.isEmpty( filenameField ) && includingFilename ) {
    ValueMetaInterface filenameMeta = new ValueMetaString( filenameField );
    filenameMeta.setOrigin( origin );
    if ( lazyConversionActive ) {
      filenameMeta.setStorageType( ValueMetaInterface.STORAGE_TYPE_BINARY_STRING );
      filenameMeta.setStorageMetadata( new ValueMetaString( filenameField ) );
    }
    rowMeta.addValueMeta( filenameMeta );
  }

  if ( !Utils.isEmpty( rowNumField ) ) {
    ValueMetaInterface rowNumMeta = new ValueMetaInteger( rowNumField );
    rowNumMeta.setLength( 10 );
    rowNumMeta.setOrigin( origin );
    rowMeta.addValueMeta( rowNumMeta );
  }
}
 
Example 11
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 12
Source File: OracleDatabaseMeta.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
/**
 * Generates the SQL statement to modify a column in the specified table
 *
 * @param tablename
 *          The table to add
 * @param v
 *          The column defined as a value
 * @param tk
 *          the name of the technical key field
 * @param useAutoinc
 *          whether or not this field uses auto increment
 * @param pk
 *          the name of the primary key field
 * @param semicolon
 *          whether or not to add a semi-colon behind the statement.
 * @return the SQL statement to modify a column in the specified table
 */
@Override
public String getModifyColumnStatement( String tablename, ValueMetaInterface v, String tk, boolean useAutoinc,
  String pk, boolean semicolon ) {
  ValueMetaInterface tmpColumn = v.clone();
  String tmpName = v.getName();
  boolean isQuoted = tmpName.startsWith( "\"" ) && tmpName.endsWith( "\"" );
  if ( isQuoted ) {
    // remove the quotes first.
    //
    tmpName = tmpName.substring( 1, tmpName.length() - 1 );
  }

  int threeoh = tmpName.length() >= 30 ? 30 : tmpName.length();
  tmpName = tmpName.substring( 0, threeoh );

  tmpName += "_KTL"; // should always be shorter than 35 positions

  // put the quotes back if needed.
  //
  if ( isQuoted ) {
    tmpName = "\"" + tmpName + "\"";
  }
  tmpColumn.setName( tmpName );

  // Read to go.
  //
  String sql = "";

  // Create a new tmp column
  sql += getAddColumnStatement( tablename, tmpColumn, tk, useAutoinc, pk, semicolon ) + ";" + Const.CR;
  // copy the old data over to the tmp column
  sql += "UPDATE " + tablename + " SET " + tmpColumn.getName() + "=" + v.getName() + ";" + Const.CR;
  // drop the old column
  sql += getDropColumnStatement( tablename, v, tk, useAutoinc, pk, semicolon ) + ";" + Const.CR;
  // create the wanted column
  sql += getAddColumnStatement( tablename, v, tk, useAutoinc, pk, semicolon ) + ";" + Const.CR;
  // copy the data from the tmp column to the wanted column (again)
  // All this to avoid the rename clause as this is not supported on all Oracle versions
  sql += "UPDATE " + tablename + " SET " + v.getName() + "=" + tmpColumn.getName() + ";" + Const.CR;
  // drop the temp column
  sql += getDropColumnStatement( tablename, tmpColumn, tk, useAutoinc, pk, semicolon );

  return sql;
}
 
Example 13
Source File: ValueMetaBase.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
/**
 * Compare 2 values of the same data type
 *
 * @param data1
 *          the first value
 * @param meta2
 *          the second value's metadata
 * @param data2
 *          the second value
 * @return 0 if the values are equal, -1 if data1 is smaller than data2 and +1 if it's larger.
 * @throws KettleValueException
 *           In case we get conversion errors
 */
@Override
public int compare( Object data1, ValueMetaInterface meta2, Object data2 ) throws KettleValueException {
  if ( meta2 == null ) {
    throw new KettleValueException( toStringMeta()
        + " : Second meta data (meta2) is null, please check one of the previous steps." );
  }

  try {
    // Before we can compare data1 to data2 we need to make sure they have the
    // same data type etc.
    //
    if ( getType() == meta2.getType() ) {
      if ( getStorageType() == meta2.getStorageType() ) {
        return compare( data1, data2 );
      }

      // Convert the storage type to compare the data.
      //
      switch ( getStorageType() ) {
        case STORAGE_TYPE_NORMAL:
          return compare( data1, meta2.convertToNormalStorageType( data2 ) );
        case STORAGE_TYPE_BINARY_STRING:
          if ( storageMetadata != null && storageMetadata.getConversionMask() != null && !meta2.isNumber() ) {
            // BACKLOG-18754 - if there is a storage conversion mask, we should use
            // it as the mask for meta2 (meta2 can have specific storage type and type, so
            // it can't be used directly to convert data2 to binary string)
            ValueMetaInterface meta2StorageMask = meta2.clone();
            meta2StorageMask.setConversionMask( storageMetadata.getConversionMask() );
            return compare( data1, meta2StorageMask.convertToBinaryStringStorageType( data2 ) );
          } else {
            return compare( data1, meta2.convertToBinaryStringStorageType( data2 ) );
          }
        case STORAGE_TYPE_INDEXED:
          switch ( meta2.getStorageType() ) {
            case STORAGE_TYPE_INDEXED:
              return compare( data1, data2 ); // not accessible, just to make sure.
            case STORAGE_TYPE_NORMAL:
              return -meta2.compare( data2, convertToNormalStorageType( data1 ) );
            case STORAGE_TYPE_BINARY_STRING:
              return -meta2.compare( data2, convertToBinaryStringStorageType( data1 ) );
            default:
              throw new KettleValueException( meta2.toStringMeta() + " : Unknown storage type : "
                  + meta2.getStorageType() );

          }
        default:
          throw new KettleValueException( toStringMeta() + " : Unknown storage type : " + getStorageType() );
      }
    } else if ( ValueMetaInterface.TYPE_INTEGER == getType() && ValueMetaInterface.TYPE_NUMBER == meta2.getType() ) {
      // BACKLOG-18738
      // compare Double to Integer
      return -meta2.compare( data2, meta2.convertData( this, data1 ) );
    }

    // If the data types are not the same, the first one is the driver...
    // The second data type is converted to the first one.
    //
    return compare( data1, convertData( meta2, data2 ) );
  } catch ( Exception e ) {
    throw new KettleValueException(
        toStringMeta() + " : Unable to compare with value [" + meta2.toStringMeta() + "]", e );
  }
}
 
Example 14
Source File: MySQLBulkLoader.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
@Override
public boolean processRow( StepMetaInterface smi, StepDataInterface sdi ) throws KettleException {
  meta = (MySQLBulkLoaderMeta) smi;
  data = (MySQLBulkLoaderData) sdi;

  try {
    Object[] r = getRow(); // Get row from input rowset & set row busy!
    if ( r == null ) { // no more input to be expected...

      setOutputDone();

      closeOutput();

      return false;
    }

    if ( first ) {
      first = false;

      // Cache field indexes.
      //
      data.keynrs = new int[meta.getFieldStream().length];
      for ( int i = 0; i < data.keynrs.length; i++ ) {
        data.keynrs[i] = getInputRowMeta().indexOfValue( meta.getFieldStream()[i] );
      }

      data.bulkFormatMeta = new ValueMetaInterface[data.keynrs.length];
      for ( int i = 0; i < data.keynrs.length; i++ ) {
        ValueMetaInterface sourceMeta = getInputRowMeta().getValueMeta( data.keynrs[i] );
        if ( sourceMeta.isDate() ) {
          if ( meta.getFieldFormatType()[i] == MySQLBulkLoaderMeta.FIELD_FORMAT_TYPE_DATE ) {
            data.bulkFormatMeta[i] = data.bulkDateMeta.clone();
          } else if ( meta.getFieldFormatType()[i] == MySQLBulkLoaderMeta.FIELD_FORMAT_TYPE_TIMESTAMP ) {
            data.bulkFormatMeta[i] = data.bulkTimestampMeta.clone(); // default to timestamp
          }
        } else if ( sourceMeta.isNumeric()
            && meta.getFieldFormatType()[i] == MySQLBulkLoaderMeta.FIELD_FORMAT_TYPE_NUMBER ) {
          data.bulkFormatMeta[i] = data.bulkNumberMeta.clone();
        }

        if ( data.bulkFormatMeta[i] == null && !sourceMeta.isStorageBinaryString() ) {
          data.bulkFormatMeta[i] = sourceMeta.clone();
        }
      }

      // execute the client statement...
      //
      execute( meta );
    }

    // Every nr of rows we re-start the bulk load process to allow indexes etc to fit into the MySQL server memory
    // Performance could degrade if we don't do this.
    //
    if ( data.bulkSize > 0 && getLinesOutput() > 0 && ( getLinesOutput() % data.bulkSize ) == 0 ) {
      closeOutput();
      executeLoadCommand();
    }

    writeRowToBulk( getInputRowMeta(), r );
    putRow( getInputRowMeta(), r );
    incrementLinesOutput();

    return true;
  } catch ( Exception e ) {
    logError( BaseMessages.getString( PKG, "MySQLBulkLoader.Log.ErrorInStep" ), e );
    setErrors( 1 );
    stopAll();
    setOutputDone(); // signal end to receiver(s)
    return false;
  }
}
 
Example 15
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 16
Source File: OraBulkLoaderMeta.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, "OraBulkLoaderMeta.GetSQL.ErrorOccurred" )
            + e.getMessage() );
        }
      } else {
        retval.setError( BaseMessages.getString( PKG, "OraBulkLoaderMeta.GetSQL.NoTableDefinedOnConnection" ) );
      }
    } else {
      retval.setError( BaseMessages.getString( PKG, "OraBulkLoaderMeta.GetSQL.NotReceivingAnyFields" ) );
    }
  } else {
    retval.setError( BaseMessages.getString( PKG, "OraBulkLoaderMeta.GetSQL.NoConnectionDefined" ) );
  }

  return retval;
}
 
Example 17
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 18
Source File: TableOutput.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
public boolean processRow( StepMetaInterface smi, StepDataInterface sdi ) throws KettleException {
  meta = (TableOutputMeta) smi;
  data = (TableOutputData) sdi;

  Object[] r = getRow(); // this also waits for a previous step to be finished.
  if ( r == null ) { // no more input to be expected...
    // truncate the table if there are no rows at all coming into this step
    if ( first && meta.truncateTable() ) {
      truncateTable();
    }
    return false;
  }

  if ( first ) {
    first = false;
    if ( meta.truncateTable() ) {
      truncateTable();
    }
    data.outputRowMeta = getInputRowMeta().clone();
    meta.getFields( data.outputRowMeta, getStepname(), null, null, this, repository, metaStore );

    if ( !meta.specifyFields() ) {
      // Just take the input row
      data.insertRowMeta = getInputRowMeta().clone();
    } else {

      data.insertRowMeta = new RowMeta();

      //
      // Cache the position of the compare fields in Row row
      //
      data.valuenrs = new int[meta.getFieldDatabase().length];
      for ( int i = 0; i < meta.getFieldDatabase().length; i++ ) {
        data.valuenrs[i] = getInputRowMeta().indexOfValue( meta.getFieldStream()[i] );
        if ( data.valuenrs[i] < 0 ) {
          throw new KettleStepException( BaseMessages.getString(
            PKG, "TableOutput.Exception.FieldRequired", meta.getFieldStream()[i] ) );
        }
      }

      for ( int i = 0; i < meta.getFieldDatabase().length; i++ ) {
        ValueMetaInterface insValue = getInputRowMeta().searchValueMeta( meta.getFieldStream()[i] );
        if ( insValue != null ) {
          ValueMetaInterface insertValue = insValue.clone();
          insertValue.setName( meta.getFieldDatabase()[i] );
          data.insertRowMeta.addValueMeta( insertValue );
        } else {
          throw new KettleStepException( BaseMessages.getString(
            PKG, "TableOutput.Exception.FailedToFindField", meta.getFieldStream()[i] ) );
        }
      }
    }
  }

  try {
    Object[] outputRowData = writeToTable( getInputRowMeta(), r );
    if ( outputRowData != null ) {
      putRow( data.outputRowMeta, outputRowData ); // in case we want it go further...
      incrementLinesOutput();
    }

    if ( checkFeedback( getLinesRead() ) ) {
      if ( log.isBasic() ) {
        logBasic( "linenr " + getLinesRead() );
      }
    }
  } catch ( KettleException e ) {
    logError( "Because of an error, this step can't continue: ", e );
    setErrors( 1 );
    stopAll();
    setOutputDone(); // signal end to receiver(s)
    return false;
  }

  return true;
}