<commit_before> /* Copyright (c) 2007 Pentaho Corporation.  All rights reserved. 
 * This software was developed by Pentaho Corporation and is provided under the terms 
 * of the GNU Lesser General Public License, Version 2.1. You may not use 
 * this file except in compliance with the license. If you need a copy of the license, 
 * please go to http://www.gnu.org/licenses/lgpl-2.1.txt. The Original Code is Pentaho 
 * Data Integration.  The Initial Developer is Pentaho Corporation.
 *
 * Software distributed under the GNU Lesser Public License is distributed on an "AS IS" 
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or  implied. Please refer to 
 * the license for the specific language governing your rights and limitations.*/
package org.pentaho.di.trans.steps.update;

import java.sql.SQLException;

import org.pentaho.di.core.Const;
import org.pentaho.di.core.database.Database;
import org.pentaho.di.core.database.DatabaseMeta;
import org.pentaho.di.core.exception.KettleDatabaseException;
import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.core.exception.KettleStepException;
import org.pentaho.di.core.row.RowMeta;
import org.pentaho.di.core.row.RowMetaInterface;
import org.pentaho.di.core.row.ValueMetaInterface;
import org.pentaho.di.trans.Trans;
import org.pentaho.di.trans.TransMeta;
import org.pentaho.di.trans.step.BaseStep;
import org.pentaho.di.trans.step.StepDataInterface;
import org.pentaho.di.trans.step.StepInterface;
import org.pentaho.di.trans.step.StepMeta;
import org.pentaho.di.trans.step.StepMetaInterface;

/**
 * Update data in a database table, does NOT ever perform an insert.
 * 
 * @author Matt
 * @since 26-apr-2003
 */
public class Update extends BaseStep implements StepInterface
{
	private UpdateMeta meta;
	private UpdateData data;
	
	public Update(StepMeta stepMeta, StepDataInterface stepDataInterface, int copyNr, TransMeta transMeta, Trans trans)
	{
		super(stepMeta, stepDataInterface, copyNr, transMeta, trans);
	}
	
	private synchronized Object[] lookupValues(RowMetaInterface rowMeta, Object[] row) throws KettleException
	{
        Object[] outputRow=row;
        
        // Create the output row and copy the input values
        if (!Const.isEmpty( meta.getIgnoreFlagField())) // add flag field!
        {
    		outputRow = new Object[data.outputRowMeta.size()];
            for (int i=0;i<rowMeta.size();i++)
            {
                outputRow[i] = row[i];
            }
        }
        
        // OK, now do the lookup.
        // We need the lookupvalues for that.
        Object[] lookupRow = new Object[data.lookupParameterRowMeta.size()];
        int lookupIndex = 0;
        
		for (int i=0;i<meta.getKeyStream().length;i++)
		{
			if (data.keynrs[i]>=0)
			{
				lookupRow[lookupIndex] = row[ data.keynrs[i] ];
                lookupIndex++;
			}
			if (data.keynrs2[i]>=0)
			{
                lookupRow[lookupIndex] = row[ data.keynrs2[i] ];
                lookupIndex++;
			}
		}
		
		data.db.setValues(data.lookupParameterRowMeta, lookupRow, data.prepStatementLookup);
		
		if (log.isDebug()) logDebug(Messages.getString("Update.Log.ValuesSetForLookup", data.lookupParameterRowMeta.getString(lookupRow), rowMeta.getString(row))); //$NON-NLS-1$ //$NON-NLS-2$
		Object[] add = data.db.getLookup(data.prepStatementLookup);  // Got back the complete row!
		linesInput++;
		
		if (add==null) 
		{
			/* nothing was found: throw error!
			 */
            if (!meta.isErrorIgnored())
            {
                if (getStepMeta().isDoingErrorHandling())
                {
                    outputRow=null;
                    if (data.stringErrorKeyNotFound==null)
                    {
                        data.stringErrorKeyNotFound=Messages.getString("Update.Exception.KeyCouldNotFound")+data.lookupParameterRowMeta.getString(lookupRow);
                        data.stringFieldnames="";
                        for (int i=0;i<data.lookupParameterRowMeta.size();i++) 
                        {
                            if (i>0) data.stringFieldnames+=", ";
                            data.stringFieldnames+=data.lookupParameterRowMeta.getValueMeta(i).getName();
                        }
                    }
                    putError(rowMeta, row, 1L, data.stringErrorKeyNotFound, data.stringFieldnames, "UPD001");
                }
                else
                {
                    throw new KettleDatabaseException(Messages.getString("Update.Exception.KeyCouldNotFound")+data.lookupParameterRowMeta.getString(lookupRow)); //$NON-NLS-1$
                }
            }
            else
            {
                if (log.isDetailed()) log.logDetailed(toString(), Messages.getString("Update.Log.KeyCouldNotFound")+data.lookupParameterRowMeta.getString(lookupRow)); //$NON-NLS-1$
                if (!Const.isEmpty( meta.getIgnoreFlagField())) // set flag field!
                {
                    outputRow[rowMeta.size()] = Boolean.FALSE;
                }
            }
		}
		else
		{
			if (log.isRowLevel()) logRowlevel(Messages.getString("Update.Log.FoundRow")+add.toString()); //$NON-NLS-1$
			/* Row was found:
			 *  
			 * UPDATE row or do nothing?
			 *
			 */
			boolean update = false;
			for (int i=0;i<data.valuenrs.length;i++)
			{
                ValueMetaInterface valueMeta = rowMeta.getValueMeta( data.valuenrs[i] );
				Object rowvalue = row[ data.valuenrs[i] ];
				Object retvalue = add[ i ];
                
                if ( valueMeta.compare(rowvalue, retvalue)!=0 )
				{
					update=true;
				}
			}
			if (update)
			{
                // Create the update row...
                Object[] updateRow = new Object[data.updateParameterRowMeta.size()];
                for (int i=0;i<data.valuenrs.length;i++)
                {
                    updateRow[i] = row[ data.valuenrs[i] ]; // the setters
                }
                // add the where clause parameters, they are exactly the same for lookup and update
                for (int i=0;i<lookupRow.length;i++)<commit_msg>PDI-1071 : Metadata mismatch - Step Update (fix)<commit_after> /* Copyright (c) 2007 Pentaho Corporation.  All rights reserved. 
 * This software was developed by Pentaho Corporation and is provided under the terms 
 * of the GNU Lesser General Public License, Version 2.1. You may not use 
 * this file except in compliance with the license. If you need a copy of the license, 
 * please go to http://www.gnu.org/licenses/lgpl-2.1.txt. The Original Code is Pentaho 
 * Data Integration.  The Initial Developer is Pentaho Corporation.
 *
 * Software distributed under the GNU Lesser Public License is distributed on an "AS IS" 
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or  implied. Please refer to 
 * the license for the specific language governing your rights and limitations.*/
package org.pentaho.di.trans.steps.update;

import java.sql.SQLException;

import org.pentaho.di.core.Const;
import org.pentaho.di.core.database.Database;
import org.pentaho.di.core.database.DatabaseMeta;
import org.pentaho.di.core.exception.KettleDatabaseException;
import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.core.exception.KettleStepException;
import org.pentaho.di.core.row.RowMeta;
import org.pentaho.di.core.row.RowMetaInterface;
import org.pentaho.di.core.row.ValueMetaInterface;
import org.pentaho.di.trans.Trans;
import org.pentaho.di.trans.TransMeta;
import org.pentaho.di.trans.step.BaseStep;
import org.pentaho.di.trans.step.StepDataInterface;
import org.pentaho.di.trans.step.StepInterface;
import org.pentaho.di.trans.step.StepMeta;
import org.pentaho.di.trans.step.StepMetaInterface;

/**
 * Update data in a database table, does NOT ever perform an insert.
 * 
 * @author Matt
 * @since 26-apr-2003
 */
public class Update extends BaseStep implements StepInterface
{
	private UpdateMeta meta;
	private UpdateData data;
	
	public Update(StepMeta stepMeta, StepDataInterface stepDataInterface, int copyNr, TransMeta transMeta, Trans trans)
	{
		super(stepMeta, stepDataInterface, copyNr, transMeta, trans);
	}
	
	private synchronized Object[] lookupValues(RowMetaInterface rowMeta, Object[] row) throws KettleException
	{
        Object[] outputRow=row;
        
        // Create the output row and copy the input values
        if (!Const.isEmpty( meta.getIgnoreFlagField())) // add flag field!
        {
    		outputRow = new Object[data.outputRowMeta.size()];
            for (int i=0;i<rowMeta.size();i++)
            {
                outputRow[i] = row[i];
            }
        }
        
        // OK, now do the lookup.
        // We need the lookupvalues for that.
        Object[] lookupRow = new Object[data.lookupParameterRowMeta.size()];
        int lookupIndex = 0;
        
		for (int i=0;i<meta.getKeyStream().length;i++)
		{
			if (data.keynrs[i]>=0)
			{
				lookupRow[lookupIndex] = row[ data.keynrs[i] ];
                lookupIndex++;
			}
			if (data.keynrs2[i]>=0)
			{
                lookupRow[lookupIndex] = row[ data.keynrs2[i] ];
                lookupIndex++;
			}
		}
		
		data.db.setValues(data.lookupParameterRowMeta, lookupRow, data.prepStatementLookup);
		
		if (log.isDebug()) logDebug(Messages.getString("Update.Log.ValuesSetForLookup", data.lookupParameterRowMeta.getString(lookupRow), rowMeta.getString(row))); //$NON-NLS-1$ //$NON-NLS-2$
		Object[] add = data.db.getLookup(data.prepStatementLookup);  // Got back the complete row!
		RowMetaInterface returnRowMeta = data.db.getReturnRowMeta();
		linesInput++;
		
		if (add==null) 
		{
			/* nothing was found: throw error!
			 */
            if (!meta.isErrorIgnored())
            {
                if (getStepMeta().isDoingErrorHandling())
                {
                    outputRow=null;
                    if (data.stringErrorKeyNotFound==null)
                    {
                        data.stringErrorKeyNotFound=Messages.getString("Update.Exception.KeyCouldNotFound")+data.lookupParameterRowMeta.getString(lookupRow);
                        data.stringFieldnames="";
                        for (int i=0;i<data.lookupParameterRowMeta.size();i++) 
                        {
                            if (i>0) data.stringFieldnames+=", ";
                            data.stringFieldnames+=data.lookupParameterRowMeta.getValueMeta(i).getName();
                        }
                    }
                    putError(rowMeta, row, 1L, data.stringErrorKeyNotFound, data.stringFieldnames, "UPD001");
                }
                else
                {
                    throw new KettleDatabaseException(Messages.getString("Update.Exception.KeyCouldNotFound")+data.lookupParameterRowMeta.getString(lookupRow)); //$NON-NLS-1$
                }
            }
            else
            {
                if (log.isDetailed()) log.logDetailed(toString(), Messages.getString("Update.Log.KeyCouldNotFound")+data.lookupParameterRowMeta.getString(lookupRow)); //$NON-NLS-1$
                if (!Const.isEmpty( meta.getIgnoreFlagField())) // set flag field!
                {
                    outputRow[rowMeta.size()] = Boolean.FALSE;
                }
            }
		}
		else
		{
			if (log.isRowLevel()) logRowlevel(Messages.getString("Update.Log.FoundRow")+add.toString()); //$NON-NLS-1$
			/* Row was found:
			 *  
			 * UPDATE row or do nothing?
			 *
			 */
			boolean update = false;
			for (int i=0;i<data.valuenrs.length;i++)
			{
                ValueMetaInterface valueMeta = rowMeta.getValueMeta( data.valuenrs[i] );
				Object rowvalue = row[ data.valuenrs[i] ];
				ValueMetaInterface returnValueMeta = returnRowMeta.getValueMeta( i );
				Object retvalue = add[ i ];
                
                if ( valueMeta.compare(rowvalue, returnValueMeta, retvalue)!=0 )
				{
					update=true;
				}
			}
			if (update)
			{
                // Create the update row...
                Object[] updateRow = new Object[data.updateParameterRowMeta.size()];
                for (int i=0;i<data.valuenrs.length;i++)
                {
                    updateRow[i] = row[ data.valuenrs[i] ]; // the setters
                }
                // add the where clause parameters, they are exactly the same for lookup and update
                for (int i=0;i<lookupRow.length;i++)
{'fraction_non_alphanumeric': 0.11259247215673522, 'fraction_numerical': 0.006178359482968864, 'mean_word_length': 2.3483941208492105, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3219963', 'n_tokens_mistral': 3770, 'n_tokens_neox': 3447, 'n_words': 794}