void checkClientEncoding() throws Exception { Connection connection = data.db.getConnection(); Statement statement = connection.createStatement(); try { try ( ResultSet rs = statement.executeQuery( "show client_encoding" ) ) { if ( !rs.next() || rs.getMetaData().getColumnCount() != 1 ) { logBasic( "Cannot detect client_encoding, using system default encoding" ); return; } String clientEncodingStr = rs.getString( 1 ); logBasic( "Detect client_encoding: " + clientEncodingStr ); clientEncoding = Charset.forName( clientEncodingStr ); } } catch ( SQLException | IllegalArgumentException ex ) { logError( "Cannot detect PostgreSQL client_encoding, using system default encoding", ex ); } finally { statement.close(); } }
void processTruncate() throws Exception { Connection connection = data.db.getConnection(); String loadAction = environmentSubstitute( meta.getLoadAction() ); if ( loadAction.equalsIgnoreCase( "truncate" ) ) { DatabaseMeta dm = meta.getDatabaseMeta(); String tableName = dm.getQuotedSchemaTableCombination( environmentSubstitute( meta.getSchemaName() ), environmentSubstitute( meta.getTableName() ) ); logBasic( "Launching command: " + "TRUNCATE " + tableName ); Statement statement = connection.createStatement(); try { statement.executeUpdate( "TRUNCATE " + tableName ); } catch ( Exception ex ) { throw new KettleException( "Error while truncating " + tableName, ex ); } finally { statement.close(); } } }
@Before public void setUp() throws Exception { databaseMeta = mock( DatabaseMeta.class ); doReturn( "" ).when( databaseMeta ).quoteField( anyString() ); tableOutputMeta = mock( TableOutputMeta.class ); doReturn( databaseMeta ).when( tableOutputMeta ).getDatabaseMeta(); stepMeta = mock( StepMeta.class ); doReturn( "step" ).when( stepMeta ).getName(); doReturn( mock( StepPartitioningMeta.class ) ).when( stepMeta ).getTargetStepPartitioningMeta(); doReturn( tableOutputMeta ).when( stepMeta ).getStepMetaInterface(); db = mock( Database.class ); doReturn( mock( Connection.class ) ).when( db ).getConnection(); tableOutputData = mock( TableOutputData.class ); tableOutputData.db = db; tableOutputData.tableName = "sas"; tableOutputData.preparedStatements = mock( Map.class ); tableOutputData.commitCounterMap = mock( Map.class ); TransMeta transMeta = mock( TransMeta.class ); doReturn( stepMeta ).when( transMeta ).findStep( anyString() ); tableOutput = new TableOutput( stepMeta, tableOutputData, 1, transMeta, mock( Trans.class ) ); tableOutput.setData( tableOutputData ); tableOutput.setMeta( tableOutputMeta ); tableOutputSpy = spy( tableOutput ); doReturn( stepMeta ).when( tableOutputSpy ).getStepMeta(); doReturn( false ).when( tableOutputSpy ).isRowLevel(); doReturn( false ).when( tableOutputSpy ).isDebug(); doNothing().when( tableOutputSpy ).logDetailed( anyString() ); }
doReturn( mock( Connection.class ) ).when( db ).getConnection();
private void do_copy( PGBulkLoaderMeta meta, boolean wait ) throws KettleException { data.db = getDatabase( this, meta ); String copyCmd = getCopyCommand(); try { connect(); checkClientEncoding(); processTruncate(); logBasic( "Launching command: " + copyCmd ); pgCopyOut = new PGCopyOutputStream( (PGConnection) data.db.getConnection(), copyCmd ); } catch ( Exception ex ) { throw new KettleException( "Error while preparing the COPY " + copyCmd, ex ); } }
private void finishStep() { if ( data.db != null && data.db.getConnection() != null ) { try { if ( !data.db.getConnection().isClosed() ) { for ( String schemaTable : data.preparedStatements.keySet() ) {
@Test public void keyStream2ProcessRow() throws KettleException { InsertUpdate insertUpdateStep = new InsertUpdate( mockHelper.stepMeta, mockHelper.stepDataInterface, 0, mockHelper.transMeta, mockHelper.trans ); insertUpdateStep.setInputRowMeta( Mockito.mock( RowMetaInterface.class ) ); insertUpdateStep = Mockito.spy( insertUpdateStep ); InsertUpdateMeta insertUpdateMeta = new InsertUpdateMeta(); insertUpdateMeta.setKeyStream( new String[] { "test_field" } ); insertUpdateMeta.setKeyCondition( new String[] { "test_condition" } ); insertUpdateMeta.setKeyStream2( new String[] {} ); insertUpdateMeta.setUpdateLookup( new String[] {} ); insertUpdateMeta.setKeyLookup( new String[] {} ); insertUpdateMeta.setUpdateBypassed( true ); insertUpdateMeta.setDatabaseMeta( Mockito.mock( DatabaseMeta.class ) ); Database database = Mockito.mock( Database.class ); mockHelper.processRowsStepDataInterface.db = database; Mockito.doReturn( Mockito.mock( Connection.class ) ).when( database ).getConnection(); Mockito.doNothing().when( insertUpdateStep ).lookupValues( Mockito.any(), Mockito.any() ); Mockito.doNothing().when( insertUpdateStep ).putRow( Mockito.any(), Mockito.any() ); Mockito.doReturn( new Object[] {} ).when( insertUpdateStep ).getRow(); insertUpdateStep.first = true; insertUpdateMeta.afterInjectionSynchronization(); //run without a exception insertUpdateStep.processRow( insertUpdateMeta, mockHelper.processRowsStepDataInterface ); Assert.assertEquals( insertUpdateMeta.getKeyStream().length, insertUpdateMeta.getKeyStream2().length ); } }
logDetailed( "Setting delete preparedStatement to [" + sql + "]" ); data.prepStatementDelete = data.db.getConnection().prepareStatement( databaseMeta.stripCR( sql ) ); } catch ( SQLException ex ) { throw new KettleDatabaseException( "Unable to prepare statement for SQL statement [" + sql + "]", ex );
if ( getConnection().getAutoCommit() != isAutoCommit() ) { setAutoCommit( isAutoCommit() );
logDetailed( "Setting update preparedStatement to [" + sql + "]" ); data.prepStatementUpdate = data.db.getConnection().prepareStatement( databaseMeta.stripCR( sql ) ); } catch ( SQLException ex ) { throw new KettleDatabaseException( "Unable to prepare statement for SQL statement [" + sql + "]", ex );
logDetailed( "Setting preparedStatement to [" + sql + "]" ); data.prepStatementLookup = data.db.getConnection().prepareStatement( databaseMeta.stripCR( sql ) ); } catch ( SQLException ex ) { throw new KettleDatabaseException( "Unable to prepare statement for SQL statement [" + sql + "]", ex );
logDetailed( "Setting update preparedStatement to [" + sql + "]" ); data.prepStatementUpdate = data.db.getConnection().prepareStatement( databaseMeta.stripCR( sql ) ); } catch ( SQLException ex ) { throw new KettleDatabaseException( "Unable to prepare statement for SQL statement [" + sql + "]", ex );
logDetailed( "Setting preparedStatement to [" + sql + "]" ); data.prepStatementLookup = data.db.getConnection().prepareStatement( databaseMeta.stripCR( sql ) ); } catch ( SQLException ex ) { throw new KettleDatabaseException( "Unable to prepare statement for SQL statement [" + sql + "]", ex );
logDetailed( "SQL with return keys: " + sqlStatement ); data.prepStatementInsert = data.db.getConnection().prepareStatement( databaseMeta.stripCR( sqlStatement ), Statement.RETURN_GENERATED_KEYS ); } else { logDetailed( "SQL without return keys: " + sqlStatement ); data.prepStatementInsert = data.db.getConnection().prepareStatement( databaseMeta.stripCR( sqlStatement ) );
data.db.getConnection().prepareStatement( meta.getDatabaseMeta().stripCR( sql_upd ) ); } catch ( SQLException ex ) { throw new KettleDatabaseException( "Unable to prepare dimension punchThrough update statement : "
int agentPort = meta.getAgentPort(); Charset charset = meta.getCharset(); Connection conn = db.getConnection(); String tableName = meta
logDebug( "preparing combi-lookup statement:" + Const.CR + sql ); data.prepStatementLookup = data.db.getConnection().prepareStatement( databaseMeta.stripCR( sql ) ); if ( databaseMeta.supportsSetMaxRows() ) { data.prepStatementLookup.setMaxRows( 1 ); // alywas get only 1 line back!
logDetailed( "SQL w/ return keys=[" + sql + "]" ); data.prepStatementInsert = data.db.getConnection().prepareStatement( databaseMeta.stripCR( sql ), Statement.RETURN_GENERATED_KEYS ); } else { logDetailed( "SQL=[" + sql + "]" ); data.prepStatementInsert = data.db.getConnection().prepareStatement( databaseMeta.stripCR( sql ) ); data.prepStatementUpdate = data.db.getConnection().prepareStatement( databaseMeta.stripCR( sql_upd ) ); } catch ( SQLException ex ) { throw new KettleDatabaseException( "Unable to prepare dimension update :" + Const.CR + sql_upd, ex );
data.db.getConnection().prepareStatement( meta.getDatabaseMeta().stripCR( sql ) ); } catch ( SQLException ex ) { throw new KettleDatabaseException( "Couldn't prepare statement :" + Const.CR + sql, ex );
data.prepStatementLookup = data.db.getConnection().prepareStatement( databaseMeta.stripCR( sql ) ); if ( databaseMeta.supportsSetMaxRows() ) { data.prepStatementLookup.setMaxRows( 1 ); // alywas get only 1 line back!