TeraDataBulkLoaderRoutines { @VisibleForTesting String createUpsertCommand( boolean isPreview ) { StringBuffer updatecmd = new StringBuffer(); StringBuffer insertcmd = new StringBuffer(); updatecmd.append( " UPDATE " + getTargetSchema( isPreview ) + '.' + ( isPreview ? this.meta.getTableName() : parent.environmentSubstitute( this.meta.getTableName() ) ) + " SET\n" ); String[] fieldTable = this.meta.getFieldTable(); String[] fieldStream = this.meta.getFieldStream(); Boolean[] fieldUpdate = this.meta.getFieldUpdate(); Map<String, Boolean> usedAsKey = new HashMap<String, Boolean>(); StringBuffer whereClause = new StringBuffer(); for ( int i = 0; i < this.meta.getKeyStream().length; i++ ) { whereClause.append( " " + ( i == 0 ? "WHERE " : "AND" ) + " " ); whereClause.append( this.meta.getKeyLookup()[i] + " " + this.meta.getKeyCondition()[i] + " :" + this.meta.getKeyStream()[i] + "\n" ); usedAsKey.put( this.meta.getKeyLookup()[i], new Boolean( true ) ); } for ( int i = 0; i < fieldTable.length; i++ ) { if ( !( usedAsKey.containsKey( fieldTable[i] ) || !fieldUpdate[i] ) ) { if ( i > 0 ) { updatecmd.append( "," ); } updatecmd.append( " " + fieldTable[i] + " = :" + fieldStream[i] ); updatecmd.append( "\n" ); } } updatecmd.append( whereClause + ";\n" ); insertcmd.append( createInsertCommand( isPreview ) ); return quote( updatecmd.toString() ) + ",\n" + quote( insertcmd.toString() ); } TeraDataBulkLoaderRoutines( TeraDataBulkLoader parent, TeraDataBulkLoaderMeta meta ); String dropTable( boolean isPreview, String user, String suffix ); String createScriptFile(); void createFromExistingScriptFile(); String createGeneratedScriptFile(); String createGeneratedScriptFile( Map<String, Integer> inputFieldTypes, Map<String, Integer> inputFieldLength );  }
@Test public void testCreateUpsertCommandPreview() { String tableName = UUID.randomUUID().toString(); String schemaName = UUID.randomUUID().toString(); String[] fieldTable = new String[] {}; String[] fieldStream = new String[] {}; Boolean[] fieldUpdate = new Boolean[] {}; when( teraDataBulkLoaderMetaMock.getSchemaName() ).thenReturn( schemaName ); when( teraDataBulkLoaderMetaMock.getTableName() ).thenReturn( tableName ); when( teraDataBulkLoaderMetaMock.getFieldTable() ).thenReturn( fieldTable ); when( teraDataBulkLoaderMetaMock.getFieldStream() ).thenReturn( fieldStream ); when( teraDataBulkLoaderMetaMock.getFieldUpdate() ).thenReturn( fieldUpdate ); when( teraDataBulkLoaderMetaMock.getKeyStream() ).thenReturn( new String[] {} ); String upsertCommand = this.teraDataBulkLoaderRoutines.createUpsertCommand( true ); assertEquals( true, upsertCommand.startsWith( "' UPDATE " + schemaName + "." + tableName + " SET\n" ) ); }