Java Code Examples for org.pentaho.di.core.row.ValueMetaInterface

The following examples show how to use org.pentaho.di.core.row.ValueMetaInterface. These examples are extracted from open source projects. 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 Project: pentaho-kettle   Source File: MappingInputFieldsTest.java    License: Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void setUpBeforeClass() throws Exception {
  KettleEnvironment.init();

  // PluginRegistry.addPluginType(ValueMetaPluginType.getInstance());
  PluginRegistry.getInstance().registerPluginType( ValueMetaPluginType.class );

  Map<Class<?>, String> classes = new HashMap<Class<?>, String>();
  classes.put( ValueMetaInterface.class, "org.pentaho.di.core.row.value.ValueMetaString" );
  p1 =
      new Plugin( new String[] { "2" }, ValueMetaPluginType.class, ValueMetaInterface.class, "", "", "", "", false,
      true, classes, null, null, null );

  classes = new HashMap<Class<?>, String>();
  classes.put( ValueMetaInterface.class, "org.pentaho.di.core.row.value.ValueMetaInteger" );
  p2 =
      new Plugin( new String[] { "5" }, ValueMetaPluginType.class, ValueMetaInterface.class, "", "", "", "", false,
      true, classes, null, null, null );

  PluginRegistry.getInstance().registerPlugin( ValueMetaPluginType.class, p1 );
  PluginRegistry.getInstance().registerPlugin( ValueMetaPluginType.class, p2 );
}
 
Example 2
Source Project: pentaho-kettle   Source File: JsonInputTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testJsonInputPathResolution() throws KettleException {
  JsonInputField inputField = new JsonInputField( "value" );
  final String PATH = "$[*].name";
  inputField.setPath( PATH );
  inputField.setType( ValueMetaInterface.TYPE_STRING );
  JsonInputMeta inputMeta = createSimpleMeta( "json", inputField );
  VariableSpace variables = new Variables();
  JsonInput jsonInput = null;
  try {
    jsonInput =
      createJsonInput( "json", inputMeta, variables, new Object[] { getSampleJson() } );

    JsonInputData data = (JsonInputData) Whitebox.getInternalState( jsonInput, "data" );
    FastJsonReader reader = (FastJsonReader) Whitebox.getInternalState( data, "reader" );
    RowSet rowset = reader.parse( new ByteArrayInputStream( getSampleJson().getBytes() ) );
    List results = (List) Whitebox.getInternalState( rowset, "results" );
    JSONArray jsonResult = (JSONArray) results.get( 0 );

    assertEquals( 1, jsonResult.size() );
    assertEquals( "United States of America", jsonResult.get( 0 ) );

  } catch ( InvalidPathException pathException ) {
    assertNull( jsonInput );
  }
}
 
Example 3
Source Project: pentaho-mongodb-plugin   Source File: MongoDbInputMeta.java    License: Apache License 2.0 6 votes vote down vote up
@SuppressWarnings( "deprecation" )
@Override
public void getFields( RowMetaInterface rowMeta, String origin, RowMetaInterface[] info, StepMeta nextStep,
    VariableSpace space ) throws KettleStepException {

  if ( m_outputJson || m_fields == null || m_fields.size() == 0 ) {
    ValueMetaInterface jsonValueMeta = new ValueMeta( jsonFieldName, ValueMetaInterface.TYPE_STRING );
    jsonValueMeta.setOrigin( origin );
    rowMeta.addValueMeta( jsonValueMeta );
  } else {
    for ( MongoField f : m_fields ) {
      ValueMetaInterface vm = new ValueMeta();
      vm.setName( f.m_fieldName );
      vm.setOrigin( origin );
      vm.setType( ValueMeta.getType( f.m_kettleType ) );
      if ( f.m_indexedVals != null ) {
        vm.setIndex( f.m_indexedVals.toArray() ); // indexed values
      }
      rowMeta.addValueMeta( vm );
    }
  }
}
 
Example 4
Source Project: pentaho-kettle   Source File: PGBulkLoaderMeta.java    License: Apache License 2.0 6 votes vote down vote up
public void analyseImpact( List<DatabaseImpact> impact, TransMeta transMeta, StepMeta stepMeta,
  RowMetaInterface prev, String[] input, String[] output, RowMetaInterface info, Repository repository,
  IMetaStore metaStore ) throws KettleStepException {
  if ( prev != null ) {
    /* DEBUG CHECK THIS */
    // Insert dateMask fields : read/write
    for ( int i = 0; i < fieldTable.length; i++ ) {
      ValueMetaInterface v = prev.searchValueMeta( fieldStream[i] );

      DatabaseImpact ii =
        new DatabaseImpact(
          DatabaseImpact.TYPE_IMPACT_READ_WRITE, transMeta.getName(), stepMeta.getName(), databaseMeta
            .getDatabaseName(), transMeta.environmentSubstitute( tableName ), fieldTable[i],
          fieldStream[i], v != null ? v.getOrigin() : "?", "", "Type = " + v.toStringMeta() );
      impact.add( ii );
    }
  }
}
 
Example 5
Source Project: pentaho-kettle   Source File: TeraFastDialog.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * ...
 */
public void getUpdate() {
  try {
    final RowMetaInterface row = this.transMeta.getPrevStepFields( this.stepname );
    if ( row != null ) {
      TableItemInsertListener listener = new TableItemInsertListener() {
        public boolean tableItemInserted( final TableItem tableItem, final ValueMetaInterface value ) {
          // possible to check format of input fields
          return true;
        }
      };
      BaseStepDialog.getFieldsFromPrevious(
        row, this.wReturn, 1, new int[] { 1, 2 }, new int[] {}, -1, -1, listener );
    }
  } catch ( KettleException ke ) {
    new ErrorDialog(
      this.shell, BaseMessages.getString( PKG, "TeraFastDialog.FailedToGetFields.DialogTitle" ), BaseMessages
        .getString( PKG, "TeraFastDialog.FailedToGetFields.DialogMessage" ), ke );
  }
}
 
Example 6
@Before
public void setUp() throws Exception {
  data = new GroupByData();
  data.subjectnrs = new int[] { 0 };
  GroupByMeta meta = new GroupByMeta();
  meta.setAggregateType( new int[] { 5 } );
  ValueMetaInterface vmi = new ValueMetaInteger();
  when( mockHelper.stepMeta.getStepMetaInterface() ).thenReturn( meta );
  RowMetaInterface rmi = Mockito.mock( RowMetaInterface.class );
  data.inputRowMeta = rmi;
  data.outputRowMeta = rmi;
  when( rmi.getValueMeta( Mockito.anyInt() ) ).thenReturn( vmi );
  data.aggMeta = rmi;
  data.agg = new Object[] { def };
  step = new GroupBy( mockHelper.stepMeta, data, 0, mockHelper.transMeta, mockHelper.trans );
}
 
Example 7
Source Project: pentaho-hadoop-shims   Source File: CommonHadoopShim.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Class<? extends Writable> getHadoopWritableCompatibleClass( ValueMetaInterface kettleType ) {
  if ( kettleType == null ) {
    return NullWritable.class;
  }
  switch ( kettleType.getType() ) {
    case ValueMetaInterface.TYPE_STRING:
    case ValueMetaInterface.TYPE_BIGNUMBER:
    case ValueMetaInterface.TYPE_DATE:
      return Text.class;
    case ValueMetaInterface.TYPE_INTEGER:
      return LongWritable.class;
    case ValueMetaInterface.TYPE_NUMBER:
      return DoubleWritable.class;
    case ValueMetaInterface.TYPE_BOOLEAN:
      return BooleanWritable.class;
    case ValueMetaInterface.TYPE_BINARY:
      return BytesWritable.class;
    default:
      return Text.class;
  }
}
 
Example 8
Source Project: pentaho-kettle   Source File: BaseFileInputStep.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Prepare to process. Executed only first time row processing. It can't be possible to prepare to process in the
 * init() phrase, because files can be in fields from previous step.
 */
protected void prepareToRowProcessing() throws KettleException {
  data.outputRowMeta = new RowMeta();
  RowMetaInterface[] infoStep = null;

  if ( meta.inputFiles.acceptingFilenames ) {
    // input files from previous step
    infoStep = filesFromPreviousStep();
  }

  // get the metadata populated. Simple and easy.
  meta.getFields( data.outputRowMeta, getStepname(), infoStep, null, this, repository, metaStore );
  // Create convert meta-data objects that will contain Date & Number formatters
  //
  data.convertRowMeta = data.outputRowMeta.cloneToType( ValueMetaInterface.TYPE_STRING );

  BaseFileInputStepUtils.handleMissingFiles( data.files, log, meta.errorHandling.errorIgnored,
      data.dataErrorLineHandler );

  // Count the number of repeat fields...
  for ( int i = 0; i < meta.inputFields.length; i++ ) {
    if ( meta.inputFields[i].isRepeated() ) {
      data.nr_repeats++;
    }
  }
}
 
Example 9
Source Project: pentaho-kettle   Source File: RegexEvalMetaTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testGetFieldsAddsFieldIfDoesntExist() throws KettleStepException {
  RegexEvalMeta regexEvalMeta = new RegexEvalMeta();
  String name = "TEST_NAME";
  regexEvalMeta.allocate( 1 );
  String fieldName = "fieldname";
  regexEvalMeta.getFieldName()[0] = fieldName;
  when( mockInputRowMeta.indexOfValue( fieldName ) ).thenReturn( -1 );
  ValueMetaInterface mockValueMeta = mock( ValueMetaInterface.class );
  String mockName = "MOCK_NAME";
  when( mockVariableSpace.environmentSubstitute( fieldName ) ).thenReturn( mockName );
  when( mockInputRowMeta.getValueMeta( 0 ) ).thenReturn( mockValueMeta );
  regexEvalMeta.setReplacefields( true );
  regexEvalMeta.setAllowCaptureGroupsFlag( true );
  regexEvalMeta.getFields( mockInputRowMeta, name, null, null, mockVariableSpace, null, null );
  ArgumentCaptor<ValueMetaInterface> captor = ArgumentCaptor.forClass( ValueMetaInterface.class );
  verify( mockInputRowMeta ).addValueMeta( captor.capture() );
  assertEquals( fieldName, captor.getValue().getName() );
}
 
Example 10
Source Project: pentaho-kettle   Source File: MonetDBBulkLoaderMeta.java    License: 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 11
Source Project: pentaho-kettle   Source File: ConstantMeta.java    License: Apache License 2.0 6 votes vote down vote up
public void getFields( RowMetaInterface rowMeta, String name, RowMetaInterface[] info, StepMeta nextStep,
  VariableSpace space, Repository repository, IMetaStore metaStore ) throws KettleStepException {
  for ( int i = 0; i < fieldName.length; i++ ) {
    if ( fieldName[i] != null && fieldName[i].length() != 0 ) {
      int type = ValueMetaFactory.getIdForValueMeta( fieldType[i] );
      if ( type == ValueMetaInterface.TYPE_NONE ) {
        type = ValueMetaInterface.TYPE_STRING;
      }
      try {
        ValueMetaInterface v = ValueMetaFactory.createValueMeta( fieldName[i], type );
        v.setLength( fieldLength[i] );
        v.setPrecision( fieldPrecision[i] );
        v.setOrigin( name );
        v.setConversionMask( fieldFormat[i] );
        rowMeta.addValueMeta( v );
      } catch ( Exception e ) {
        throw new KettleStepException( e );
      }
    }
  }
}
 
Example 12
Source Project: pentaho-kettle   Source File: ValueMetaBaseTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testVerticaTimeType() throws Exception {
  // PDI-12244
  ResultSetMetaData metaData = mock( ResultSetMetaData.class );
  ValueMetaInterface valueMetaInterface = mock( ValueMetaInternetAddress.class );

  when( resultSet.getMetaData() ).thenReturn( metaData );
  when( metaData.getColumnType( 1 ) ).thenReturn( Types.TIME );
  when( resultSet.getTime( 1 ) ).thenReturn( new Time( 0 ) );
  when( valueMetaInterface.getOriginalColumnType() ).thenReturn( Types.TIME );
  when( valueMetaInterface.getType() ).thenReturn( ValueMetaInterface.TYPE_DATE );

  DatabaseInterface databaseInterface = new Vertica5DatabaseMeta();
  Object ret = databaseInterface.getValueFromResultSet( resultSet, valueMetaInterface, 0 );
  assertEquals( new Time( 0 ), ret );
}
 
Example 13
Source Project: pentaho-kettle   Source File: StringCutIT.java    License: Apache License 2.0 6 votes vote down vote up
public RowMetaInterface createResultRowMetaInterface2() {
  RowMetaInterface rm = new RowMeta();

  ValueMetaInterface[] valuesMeta =
  {
    new ValueMetaString( "field1" ), new ValueMetaString( "field2" ),
    new ValueMetaString( "outf3" ), new ValueMetaString( "outf4" ),

  };

  for ( int i = 0; i < valuesMeta.length; i++ ) {
    rm.addValueMeta( valuesMeta[i] );
  }

  return rm;
}
 
Example 14
Source Project: pentaho-kettle   Source File: BaseFileField.java    License: Apache License 2.0 6 votes vote down vote up
public void guessTrimType() {
  boolean spaces_before = false;
  boolean spaces_after = false;

  for ( int i = 0; i < samples.length; i++ ) {
    spaces_before |= Const.nrSpacesBefore( samples[i] ) > 0;
    spaces_after |= Const.nrSpacesAfter( samples[i] ) > 0;
    samples[i] = Const.trim( samples[i] );
  }

  trimtype = ValueMetaInterface.TRIM_TYPE_NONE;

  if ( spaces_before ) {
    trimtype |= ValueMetaInterface.TRIM_TYPE_LEFT;
  }
  if ( spaces_after ) {
    trimtype |= ValueMetaInterface.TRIM_TYPE_RIGHT;
  }
}
 
Example 15
@Override public boolean validateTestObject( RowMetaInterface testObject, Object other ) {
  if ( other == null || !( other instanceof RowMetaInterface ) ) {
    return false;
  }
  RowMetaInterface otherRow = (RowMetaInterface) other;
  if ( testObject.size() != otherRow.size() ) {
    return false;
  }
  for ( int i = 0; i < testObject.size(); i++ ) {
    ValueMetaInterface testVmi = testObject.getValueMeta( i );
    ValueMetaInterface otherVmi = otherRow.getValueMeta( i );
    if ( !testVmi.getName().equals( otherVmi.getName() ) ) {
      return false;
    }
    if ( !testVmi.getTypeDesc().equals( otherVmi.getTypeDesc() ) ) {
      return false;
    }
  }
  return true;
}
 
Example 16
Source Project: pentaho-kettle   Source File: JsonInputTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testNoFilesInListError() throws Exception {
  ByteArrayOutputStream err = new ByteArrayOutputStream();
  helper.redirectLog( err, LogLevel.ERROR );

  JsonInputMeta meta = createFileListMeta( Collections.<FileObject>emptyList() );
  meta.setDoNotFailIfNoFile( false );
  JsonInputField price = new JsonInputField();
  price.setName( "price" );
  price.setType( ValueMetaInterface.TYPE_NUMBER );
  price.setPath( "$..book[*].price" );
  meta.setInputFields( new JsonInputField[] { price } );

  try ( LocaleChange enUS = new LocaleChange( Locale.US ) ) {
    JsonInput jsonInput = createJsonInput( meta );
    processRows( jsonInput, 1 );
  }
  String errMsgs = err.toString();
  assertTrue( errMsgs, errMsgs.contains( "No file(s) specified!" ) );
}
 
Example 17
Source Project: pentaho-kettle   Source File: MySQLBulkLoaderDialog.java    License: Apache License 2.0 6 votes vote down vote up
private void getUpdate() {
  try {
    RowMetaInterface r = transMeta.getPrevStepFields( stepname );
    if ( r != null ) {
      TableItemInsertListener listener = new TableItemInsertListener() {
        public boolean tableItemInserted( TableItem tableItem, ValueMetaInterface v ) {
          if ( v.getType() == ValueMetaInterface.TYPE_DATE ) {
            // The default is : format is OK for dates, see if this sticks later on...
            //
            tableItem.setText( 3, "Y" );
          } else {
            tableItem.setText( 3, "Y" ); // default is OK too...
          }
          return true;
        }
      };
      BaseStepDialog.getFieldsFromPrevious( r, wReturn, 1, new int[] { 1, 2 }, new int[] {}, -1, -1, listener );
    }
  } catch ( KettleException ke ) {
    new ErrorDialog(
      shell, BaseMessages.getString( PKG, "MySQLBulkLoaderDialog.FailedToGetFields.DialogTitle" ),
      BaseMessages.getString( PKG, "MySQLBulkLoaderDialog.FailedToGetFields.DialogMessage" ), ke );
  }
}
 
Example 18
Source Project: pentaho-kettle   Source File: ValueMetaTimestamp.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Convert the specified data to the data type specified in this object.
 *
 * @param meta2 the metadata of the object to be converted
 * @param data2 the data of the object to be converted
 * @return the object in the data type of this value metadata object
 * @throws KettleValueException in case there is a data conversion error
 */
@Override
public Object convertData( ValueMetaInterface meta2, Object data2 ) throws KettleValueException {
  switch ( meta2.getType() ) {
    case TYPE_TIMESTAMP:
      return ( (ValueMetaTimestamp) meta2 ).getTimestamp( data2 );
    case TYPE_STRING:
      return convertStringToTimestamp( meta2.getString( data2 ) );
    case TYPE_INTEGER:
      return convertIntegerToTimestamp( meta2.getInteger( data2 ) );
    case TYPE_NUMBER:
      return convertNumberToTimestamp( meta2.getNumber( data2 ) );
    case TYPE_DATE:
      return convertDateToTimestamp( meta2.getDate( data2 ) );
    case TYPE_BIGNUMBER:
      return convertBigNumberToTimestamp( meta2.getBigNumber( data2 ) );
    default:
      throw new KettleValueException( meta2.toStringMeta() + " : can't be converted to a timestamp" );
  }
}
 
Example 19
Source Project: pentaho-kettle   Source File: RulesAccumulatorMeta.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void loadXML( Node stepnode, List<DatabaseMeta> _databases, IMetaStore metaStore ) throws KettleXMLException {
  try {
    Node fields = XMLHandler.getSubNode( stepnode, StorageKeys.NODE_FIELDS.toString() );
    int nrfields = XMLHandler.countNodes( fields, StorageKeys.SUBNODE_FIELD.toString() );

    ValueMetaInterface vm = null;
    for ( int i = 0; i < nrfields; i++ ) {
      Node fnode = XMLHandler.getSubNodeByNr( fields, StorageKeys.SUBNODE_FIELD.toString(), i );

      String name = XMLHandler.getTagValue( fnode, StorageKeys.COLUMN_NAME.toString() );
      int type = ValueMeta.getType( XMLHandler.getTagValue( fnode, StorageKeys.COLUMN_TYPE.toString() ) );
      vm = ValueMetaFactory.createValueMeta( name, type );

      getRuleResultColumns().add( vm );
    }

    setRuleFile( XMLHandler.getTagValue( stepnode, StorageKeys.RULE_FILE.toString() ) );
    setRuleDefinition( XMLHandler.getTagValue( stepnode, StorageKeys.RULE_DEFINITION.toString() ) );
  } catch ( Exception e ) {
    throw new KettleXMLException( BaseMessages.getString( PKG, "RulesMeta.Error.LoadFromXML" ), e );
  }
}
 
Example 20
private static DataType getDataType( ValueMetaInterface v ) {
  switch ( v.getType() ) {
    case ValueMetaInterface.TYPE_BIGNUMBER:
    case ValueMetaInterface.TYPE_INTEGER:
    case ValueMetaInterface.TYPE_NUMBER:
      return DataType.NUMERIC;
    case ValueMetaInterface.TYPE_BINARY:
      return DataType.BINARY;
    case ValueMetaInterface.TYPE_BOOLEAN:
      return DataType.BOOLEAN;
    case ValueMetaInterface.TYPE_DATE:
    case ValueMetaInterface.TYPE_TIMESTAMP:
      return DataType.DATE;
    case ValueMetaInterface.TYPE_STRING:
      return DataType.STRING;
    case ValueMetaInterface.TYPE_NONE:
    default:
      return DataType.UNKNOWN;
  }
  // the enum data type no longer supports length and precision
  // dataTypeSettings.setLength(v.getLength());
  // dataTypeSettings.setPrecision(v.getPrecision());

}
 
Example 21
Source Project: pentaho-kettle   Source File: JoinRowsTest.java    License: Apache License 2.0 5 votes vote down vote up
BlockingRowSet getRowSetWithData( int size, String dataPrefix, boolean isMainStep ) {
  BlockingRowSet blockingRowSet = new BlockingRowSet( size );
  RowMeta rowMeta = new RowMeta();

  ValueMetaInterface valueMetaString = new ValueMetaString( dataPrefix + " first value name" );
  ValueMetaInterface valueMetaInteger = new ValueMetaString( dataPrefix + " second value name" );
  ValueMetaInterface valueMetaBoolean = new ValueMetaString( dataPrefix + " third value name" );

  rowMeta.addValueMeta( valueMetaString );
  rowMeta.addValueMeta( valueMetaInteger );
  rowMeta.addValueMeta( valueMetaBoolean );

  blockingRowSet.setRowMeta( rowMeta );

  for ( int i = 0; i < size; i++ ) {
    Object[] rowData = new Object[3];
    rowData[0] = dataPrefix + " row[" + i + "]-first value";
    rowData[1] = dataPrefix + " row[" + i + "]-second value";
    rowData[2] = dataPrefix + " row[" + i + "]-third value";
    blockingRowSet.putRow( rowMeta, rowData );
  }

  if ( isMainStep ) {
    blockingRowSet.setThreadNameFromToCopy( "main step name", 0, null, 0 );
  } else {
    blockingRowSet.setThreadNameFromToCopy( "secondary step name", 0, null, 0 );
  }

  blockingRowSet.setDone();

  return blockingRowSet;
}
 
Example 22
Source Project: pentaho-kettle   Source File: DatabaseMeta.java    License: Apache License 2.0 5 votes vote down vote up
public String getAddColumnStatement( String tablename, ValueMetaInterface v, String tk, boolean use_autoinc,
  String pk, boolean semicolon ) {
  String retval = databaseInterface.getAddColumnStatement( tablename, v, tk, use_autoinc, pk, semicolon );
  retval += Const.CR;
  if ( semicolon ) {
    retval += ";" + Const.CR;
  }
  return retval;
}
 
Example 23
Source Project: pentaho-kettle   Source File: DatabaseJoinMeta.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public RowMetaInterface getTableFields() {
  // Build a dummy parameter row...
  //
  RowMetaInterface param = new RowMeta();
  for ( int i = 0; i < parameterField.length; i++ ) {
    ValueMetaInterface v;
    try {
      v = ValueMetaFactory.createValueMeta( parameterField[i], parameterType[i] );
    } catch ( KettlePluginException e ) {
      v = new ValueMetaNone( parameterField[i] );
    }
    param.addValueMeta( v );
  }

  RowMetaInterface fields = null;
  if ( databaseMeta != null ) {
    Database db = new Database( loggingObject, databaseMeta );
    databases = new Database[] { db }; // Keep track of this one for cancelQuery

    try {
      db.connect();
      fields =
        db.getQueryFields( databaseMeta.environmentSubstitute( sql ), true, param, new Object[param.size()] );
    } catch ( KettleDatabaseException dbe ) {
      logError( BaseMessages.getString( PKG, "DatabaseJoinMeta.Log.DatabaseErrorOccurred" ) + dbe.getMessage() );
    } finally {
      db.disconnect();
    }
  }
  return fields;
}
 
Example 24
Source Project: pentaho-kettle   Source File: UpdateMetaTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testUseDefaultSchemaName() throws Exception {
  String schemaName = "";
  String tableName = "tableName";
  String schemaTable = "default.tableName";

  DatabaseMeta databaseMeta = spy( new DatabaseMeta( databaseXML ) );
  doReturn( "someValue" ).when( databaseMeta )
    .getFieldDefinition( any( ValueMetaInterface.class ), anyString(), anyString(), anyBoolean() );
  doReturn( schemaTable ).when( databaseMeta ).getQuotedSchemaTableCombination( schemaName, tableName );

  ValueMetaInterface valueMeta = mock( ValueMetaInterface.class );
  when( valueMeta.clone() ).thenReturn( mock( ValueMetaInterface.class ) );
  RowMetaInterface rowMetaInterface = mock( RowMetaInterface.class );
  when( rowMetaInterface.size() ).thenReturn( 1 );
  when( rowMetaInterface.searchValueMeta( anyString() ) ).thenReturn( valueMeta );

  UpdateMeta updateMeta = new UpdateMeta();
  updateMeta.setDatabaseMeta( databaseMeta );
  updateMeta.setTableName( tableName );
  updateMeta.setSchemaName( schemaName );
  updateMeta.setKeyLookup( new String[] { "KeyLookup1", "KeyLookup2" } );
  updateMeta.setKeyStream( new String[] { "KeyStream1", "KeyStream2" } );
  updateMeta.setUpdateLookup( new String[] { "updateLookup1", "updateLookup2" } );
  updateMeta.setUpdateStream( new String[] { "UpdateStream1", "UpdateStream2" } );

  SQLStatement sqlStatement =
      updateMeta.getSQLStatements( new TransMeta(), mock( StepMeta.class ), rowMetaInterface,
          mock( Repository.class ), mock( IMetaStore.class ) );
  String sql = sqlStatement.getSQL();

  assertTrue( StringUtils.countMatches( sql, schemaTable ) == 2 );
}
 
Example 25
Source Project: pentaho-kettle   Source File: TableAgileMart.java    License: Apache License 2.0 5 votes vote down vote up
protected Object[] writeToTable( RowMetaInterface rowMeta, Object[] r ) throws KettleException {
  // see if we need to truncate any string fields
  try {
    int index = 0;
    List<ValueMetaInterface> valueMetas = rowMeta.getValueMetaList();
    for ( ValueMetaInterface valueMeta : valueMetas ) {
      Object valueData = r[index];
      if ( valueData != null ) {
        if ( valueMeta.getType() == ValueMetaInterface.TYPE_STRING ) {
          String str = valueMeta.getString( valueData );
          int len = valueMeta.getLength();
          if ( len < 1 ) {
            len = MonetDBDatabaseMeta.DEFAULT_VARCHAR_LENGTH;
          }
          if ( str.length() > len ) {
            // TODO log this event
            str = str.substring( 0, len );
          }
          r[index] = str;
        }
      }
      index++;
    }
  } catch ( Exception e ) {
    throw new KettleException( "Error serializing rows of data to the psql command", e );
  }
  return super.writeToTable( rowMeta, r );
}
 
Example 26
Source Project: pentaho-kettle   Source File: ValueMetaConverter.java    License: Apache License 2.0 5 votes vote down vote up
protected Object convertFromDateMetaInterface( int targetValueMetaType, Object value )
  throws ValueMetaConversionException {

  if ( value == null ) {
    return null;
  }

  // value is expected to be of type Date
  if ( !( value instanceof Date ) ) {
    handleConversionError(
      "Error.  Expecting value of type Date.    actual value type = '" + value.getClass() + "'.    value = '"
        + value + "'." );
  }

  try {
    Date dateValue = (Date) value;
    switch ( targetValueMetaType ) {
      case ValueMetaInterface.TYPE_INTEGER:
        return dateValue.getTime();
      case ValueMetaInterface.TYPE_STRING:
        return datePattern.format( dateValue );
      case ValueMetaInterface.TYPE_TIMESTAMP:
        return new Timestamp( dateValue.getTime() );
      case ValueMetaInterface.TYPE_DATE:
        return new Date( dateValue.getTime() );
      default:
        throwBadConversionCombination( ValueMetaInterface.TYPE_DATE, targetValueMetaType, value );
    }
  } catch ( Exception e ) {
    throwErroredConversion( ValueMetaInterface.TYPE_DATE, targetValueMetaType, value, e );
  }
  return null;
}
 
Example 27
Source Project: pentaho-kettle   Source File: GetXMLDataTest.java    License: Apache License 2.0 5 votes vote down vote up
public RowMetaInterface createResultRowMetaInterface() {
  RowMetaInterface rm = new RowMeta();

  ValueMetaInterface[] valuesMeta =
    { new ValueMeta( "field1", ValueMeta.TYPE_STRING ), new ValueMeta( "objectid", ValueMeta.TYPE_STRING ),
      new ValueMeta( "sapident", ValueMeta.TYPE_STRING ), new ValueMeta( "quantity", ValueMeta.TYPE_STRING ),
      new ValueMeta( "merkmalname", ValueMeta.TYPE_STRING ), new ValueMeta( "merkmalswert", ValueMeta.TYPE_STRING ) };

  for ( int i = 0; i < valuesMeta.length; i++ ) {
    rm.addValueMeta( valuesMeta[i] );
  }

  return rm;
}
 
Example 28
Source Project: pentaho-kettle   Source File: ValueMetaTimestamp.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public ValueMetaInterface getMetadataPreview( DatabaseMeta databaseMeta, ResultSet rs )
  throws KettleDatabaseException {

  try {
    if ( java.sql.Types.TIMESTAMP == rs.getInt( "COLUMN_TYPE" ) ) {
      ValueMetaInterface vmi = super.getMetadataPreview( databaseMeta, rs );
      ValueMetaInterface valueMeta;
      if ( databaseMeta.supportsTimestampDataType() ) {
        valueMeta = new ValueMetaTimestamp( name );
      } else {
        valueMeta = new ValueMetaDate( name );
      }
      valueMeta.setLength( vmi.getLength() );
      valueMeta.setOriginalColumnType( vmi.getOriginalColumnType() );
      valueMeta.setOriginalColumnTypeName( vmi.getOriginalColumnTypeName() );
      valueMeta.setOriginalNullable( vmi.getOriginalNullable() );
      valueMeta.setOriginalPrecision( vmi.getOriginalPrecision() );
      valueMeta.setOriginalScale( vmi.getOriginalScale() );
      valueMeta.setOriginalSigned( vmi.getOriginalSigned() );
      return valueMeta;
    }
  } catch ( SQLException e ) {
    throw new KettleDatabaseException( e );
  }
  return null;
}
 
Example 29
public void getFields(RowMetaInterface r, String origin, RowMetaInterface[] info, StepMeta nextStep, VariableSpace space) {
    super.getFields(r, origin, info, nextStep, space);

    if (civiCrmResultField != null && !civiCrmResultField.equals("")) {
        ValueMetaInterface v = new ValueMeta(civiCrmResultField, ValueMetaInterface.TYPE_STRING);
        v.setOrigin(origin);
        r.addValueMeta(v);
    }
}
 
Example 30
@Test
public void testConvertedTimestampIgnoreTZ() throws Exception {

  System.setProperty( Const.KETTLE_COMPATIBILITY_DB_IGNORE_TIMEZONE, "Y" );

  ValueMetaBase valueMeta = new ValueMetaDate( "", -1, -1 );
  valueMeta.setPreparedStatementValue( dbMeta, ps, 1, date );
  valueMeta.setStorageType( ValueMetaInterface.STORAGE_TYPE_NORMAL );

  verify( ps ).setTimestamp( eq( 1 ), any( Timestamp.class ) );
}