<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.pgbulkloader;

//
// The "designer" notes of the PostgreSQL bulkloader:
// ----------------------------------------------
//
// Let's see how fast we can push data down the tube with the use of COPY FROM STDIN 
//
// 

import java.io.IOException;
import java.math.BigDecimal;

import org.apache.commons.vfs.FileObject;
import org.pentaho.di.core.Const;
import org.pentaho.di.core.database.DatabaseMeta;
import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.core.row.RowMetaInterface;
import org.pentaho.di.core.row.ValueMetaInterface;
import org.pentaho.di.core.util.StreamLogger;
import org.pentaho.di.core.vfs.KettleVFS;
import org.pentaho.di.i18n.BaseMessages;
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;


/**
 * Performs a bulk load to a postgres table.
 *
 * Based on (copied from) <NAME>'s Oracle Bulk Loader step
 * 
 * @author matt
 * @since  28-mar-2008
 */
public class PGBulkLoader extends BaseStep implements StepInterface
{
	private static Class<?> PKG = PGBulkLoaderMeta.class; // for i18n purposes, needed by Translator2!!   $NON-NLS-1$

	private PGBulkLoaderMeta meta;
	private PGBulkLoaderData data;	
	
	public PGBulkLoader(StepMeta stepMeta, StepDataInterface stepDataInterface, int copyNr, TransMeta transMeta, Trans trans)
	{
		super(stepMeta, stepDataInterface, copyNr, transMeta, trans);
	}

	/**
	 * Get the contents of the control file as specified in the meta object
	 * 
	 * @param meta the meta object to model the control file after
	 * 
	 * @return a string containing the control file contents
	 */
	public String getCopyCommand(RowMetaInterface rm, Object[] r) throws KettleException
	{
		DatabaseMeta dm = meta.getDatabaseMeta();

		String loadAction = meta.getLoadAction();

		StringBuffer contents = new StringBuffer(500);

        String tableName = dm.getQuotedSchemaTableCombination(
            environmentSubstitute(meta.getSchemaName()),
            environmentSubstitute(meta.getTableName()));
        
        // Set the date style...
        //
        // contents.append("SET DATESTYLE ISO;"); // This is the default but we set it anyway...
        // contents.append(Const.CR);

        // Create a Postgres / Greenplum COPY string for use with a psql client
        if (loadAction.equalsIgnoreCase("truncate")) {
            contents.append("TRUNCATE TABLE ");
            contents.append(tableName + ";");
            contents.append(Const.CR);
        }
        contents.append("COPY ");
        // Table name

        contents.append(tableName);

        // Names of columns

        contents.append(" ( ");

		String streamFields[] = meta.getFieldStream();
		String tableFields[] = meta.getFieldTable();

		if ( streamFields == null || streamFields.length == 0 )
		{
			throw new KettleException("No fields defined to load to database");
		}

		for (int i = 0; i < streamFields.length; i++)
		{
			if ( i!=0 ) contents.append(", ");
			contents.append(dm.quoteField(tableFields[i]));
        }

        contents.append(" ) ");

        // The "FROM" filename
        contents.append(" FROM STDIN"); // FIFO file

        // The "FORMAT" clause
        contents.append(" WITH CSV DELIMITER AS '").append(meta.getDelimiter()).append("' QUOTE AS '").append(meta.getEnclosure()).append("'");
        contents.append(";").append(Const.CR);

		return contents.toString();
	}
		
	/**
	 * Create the command line for a psql process depending on the meta
	 * information supplied.
	 * 
	 * @param meta The meta data to create the command line from
	 * @param password Use the real password or not
	 * 
	 * @return The string to execute.
	 * 
	 * @throws KettleException Upon any exception
	 */
	public String createCommandLine(PGBulkLoaderMeta meta, boolean password) throws KettleException
	{
	   StringBuffer sb = new StringBuffer(300);
	   
	   if ( meta.getPsqlpath() != null )
	   {
		   try
		   {
	           FileObject fileObject = KettleVFS.getFileObject(environmentSubstitute(meta.getPsqlpath()));
  	      	   String psqlexec = Const.optionallyQuoteStringByOS(KettleVFS.getFilename(fileObject));
		       sb.append(psqlexec);
  	       }
	       catch ( IOException ex )
	       {
	           throw new KettleException("Error retrieving sqlldr string", ex);
	       }		       
	   }
	   else
	   {
		   if(log.isDetailed()) log.logDetailed(toString(), "psql defaults to system path");
		   sb.append("psql");
	   }

       DatabaseMeta dm = meta.getDatabaseMeta();
       if ( dm != null )
       {

		   // Note: Passwords are not supported directly, try configuring your connection for trusted access using pg_hba.conf
           //

           // The username
           // 
    	   // String user = Const.NVL(dm.getUsername(), "");
           // sb.append(" -U ").append(environmentSubstitute(user));

           // Hostname and portname
           //
           String hostname = environmentSubstitute(Const.NVL(dm.getHostname(), ""));
           String portnum = environmentSubstitute(Const.NVL(dm.getDatabasePortNumberString(), ""));
           sb.append(" -h ");
           sb.append(hostname);
           sb.append(" -p ");
           sb.append(portnum);
           
           // Database Name
           // 
           String dns  = environmentSubstitute(Const.NVL(dm.getDatabaseName(), ""));
           sb.append(" ");
           
           String overrideName = meta.getDbNameOverride();
           if ( Const.isEmpty(Const.rtrim(overrideName)) )
           {
               sb.append(environmentSubstitute(dns));
           }
           else
           {
        	   // if the database name override is filled in, do that one.
        	   sb.append(environmentSubstitute(overrideName));
           }
       }
	   else
	   {<commit_msg>PDI-1901: Patch for Postgres Dump (PostgreSQL Bulk Loader: user name)<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.pgbulkloader;

//
// The "designer" notes of the PostgreSQL bulkloader:
// ----------------------------------------------
//
// Let's see how fast we can push data down the tube with the use of COPY FROM STDIN 
//
// 

import java.io.IOException;
import java.math.BigDecimal;

import org.apache.commons.vfs.FileObject;
import org.pentaho.di.core.Const;
import org.pentaho.di.core.database.DatabaseMeta;
import org.pentaho.di.core.exception.KettleException;
import org.pentaho.di.core.row.RowMetaInterface;
import org.pentaho.di.core.row.ValueMetaInterface;
import org.pentaho.di.core.util.StreamLogger;
import org.pentaho.di.core.vfs.KettleVFS;
import org.pentaho.di.i18n.BaseMessages;
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;


/**
 * Performs a bulk load to a postgres table.
 *
 * Based on (copied from) <NAME>'s Oracle Bulk Loader step
 * 
 * @author matt
 * @since  28-mar-2008
 */
public class PGBulkLoader extends BaseStep implements StepInterface
{
	private static Class<?> PKG = PGBulkLoaderMeta.class; // for i18n purposes, needed by Translator2!!   $NON-NLS-1$

	private PGBulkLoaderMeta meta;
	private PGBulkLoaderData data;	
	
	public PGBulkLoader(StepMeta stepMeta, StepDataInterface stepDataInterface, int copyNr, TransMeta transMeta, Trans trans)
	{
		super(stepMeta, stepDataInterface, copyNr, transMeta, trans);
	}

	/**
	 * Get the contents of the control file as specified in the meta object
	 * 
	 * @param meta the meta object to model the control file after
	 * 
	 * @return a string containing the control file contents
	 */
	public String getCopyCommand(RowMetaInterface rm, Object[] r) throws KettleException
	{
		DatabaseMeta dm = meta.getDatabaseMeta();

		String loadAction = meta.getLoadAction();

		StringBuffer contents = new StringBuffer(500);

        String tableName = dm.getQuotedSchemaTableCombination(
            environmentSubstitute(meta.getSchemaName()),
            environmentSubstitute(meta.getTableName()));
        
        // Set the date style...
        //
        // contents.append("SET DATESTYLE ISO;"); // This is the default but we set it anyway...
        // contents.append(Const.CR);

        // Create a Postgres / Greenplum COPY string for use with a psql client
        if (loadAction.equalsIgnoreCase("truncate")) {
            contents.append("TRUNCATE TABLE ");
            contents.append(tableName + ";");
            contents.append(Const.CR);
        }
        contents.append("COPY ");
        // Table name

        contents.append(tableName);

        // Names of columns

        contents.append(" ( ");

		String streamFields[] = meta.getFieldStream();
		String tableFields[] = meta.getFieldTable();

		if ( streamFields == null || streamFields.length == 0 )
		{
			throw new KettleException("No fields defined to load to database");
		}

		for (int i = 0; i < streamFields.length; i++)
		{
			if ( i!=0 ) contents.append(", ");
			contents.append(dm.quoteField(tableFields[i]));
        }

        contents.append(" ) ");

        // The "FROM" filename
        contents.append(" FROM STDIN"); // FIFO file

        // The "FORMAT" clause
        contents.append(" WITH CSV DELIMITER AS '").append(meta.getDelimiter()).append("' QUOTE AS '").append(meta.getEnclosure()).append("'");
        contents.append(";").append(Const.CR);

		return contents.toString();
	}
		
	/**
	 * Create the command line for a psql process depending on the meta
	 * information supplied.
	 * 
	 * @param meta The meta data to create the command line from
	 * @param password Use the real password or not
	 * 
	 * @return The string to execute.
	 * 
	 * @throws KettleException Upon any exception
	 */
	public String createCommandLine(PGBulkLoaderMeta meta, boolean password) throws KettleException
	{
	   StringBuffer sb = new StringBuffer(300);
	   
	   if ( meta.getPsqlpath() != null )
	   {
		   try
		   {
	           FileObject fileObject = KettleVFS.getFileObject(environmentSubstitute(meta.getPsqlpath()));
  	      	   String psqlexec = Const.optionallyQuoteStringByOS(KettleVFS.getFilename(fileObject));
		       sb.append(psqlexec);
  	       }
	       catch ( IOException ex )
	       {
	           throw new KettleException("Error retrieving sqlldr string", ex);
	       }		       
	   }
	   else
	   {
		   if(log.isDetailed()) log.logDetailed(toString(), "psql defaults to system path");
		   sb.append("psql");
	   }

       DatabaseMeta dm = meta.getDatabaseMeta();
       if ( dm != null )
       {

		   // Note: Passwords are not supported directly, try configuring your connection for trusted access using pg_hba.conf
           //

           // The username
           // 
    	   String user = Const.NVL(dm.getUsername(), "");
           sb.append(" -U ").append(environmentSubstitute(user));

           // Hostname and portname
           //
           String hostname = environmentSubstitute(Const.NVL(dm.getHostname(), ""));
           String portnum = environmentSubstitute(Const.NVL(dm.getDatabasePortNumberString(), ""));
           sb.append(" -h ");
           sb.append(hostname);
           sb.append(" -p ");
           sb.append(portnum);
           
           // Database Name
           // 
           String dns  = environmentSubstitute(Const.NVL(dm.getDatabaseName(), ""));
           sb.append(" ");
           
           String overrideName = meta.getDbNameOverride();
           if ( Const.isEmpty(Const.rtrim(overrideName)) )
           {
               sb.append(environmentSubstitute(dns));
           }
           else
           {
        	   // if the database name override is filled in, do that one.
        	   sb.append(environmentSubstitute(overrideName));
           }
       }
	   else
	   {
{'fraction_non_alphanumeric': 0.11415760070567481, 'fraction_numerical': 0.00455748309320788, 'mean_word_length': 2.6270327912556652, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, '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': '3749144', 'n_tokens_mistral': 4079, 'n_tokens_neox': 3764, 'n_words': 1087}