@Test public void testParametrizeAndQuery() throws SQLException { DataRow parameters = mock( DataRow.class ); String[] preparedParameterNames = new String[] {}; Connection con = mock( Connection.class ); PreparedStatement statement = mock( PreparedStatement.class ); ResultSet res = mock( ResultSet.class ); ResultSetMetaData rsmd = mock( ResultSetMetaData.class ); doReturn( 10 ).when( parameters ).get( DataFactory.QUERY_LIMIT ); doReturn( 20 ).when( parameters ).get( DataFactory.QUERY_TIMEOUT ); doReturn( con ).when( factory ).getConnection( parameters ); doReturn( ResultSet.TYPE_FORWARD_ONLY ).when( factory ).getBestResultSetType( parameters ); doReturn( statement ).when( con ).createStatement( anyInt(), anyInt() ); doReturn( res ).when( statement ).executeQuery( QUERY ); doReturn( res ).when( statement ).executeQuery(); doReturn( rsmd ).when( res ).getMetaData(); doReturn( 1 ).when( rsmd ).getColumnCount(); doReturn( "test_column_label" ).when( rsmd ).getColumnLabel( 1 ); doReturn( "test_column_name" ).when( rsmd ).getColumnName( 1 ); doReturn( true ).doReturn( false ).when( res ).next(); doReturn( "test_val" ).when( res ).getObject( 1 ); TableModel result = factory.parametrizeAndQuery( parameters, QUERY, preparedParameterNames ); verify( con ).createStatement( ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY ); verify( statement ).setMaxRows( 10 ); verify( statement ).setQueryTimeout( 20 ); verify( statement ).executeQuery( QUERY ); assertThat( result, is( notNullValue() ) ); assertThat( result.getRowCount(), is( equalTo( 1 ) ) ); assertThat( result.getColumnCount(), is( equalTo( 1 ) ) ); assertThat( result.getColumnName( 0 ), is( equalTo( "test_column_label" ) ) ); assertThat( (String) result.getValueAt( 0, 0 ), is( equalTo( "test_val" ) ) ); }
protected TableModel parametrizeAndQuery( final DataRow parameters, final String translatedQuery, final String[] preparedParameterNames ) throws SQLException { final boolean callableStatementQuery = isCallableStatementQuery( translatedQuery ); final boolean callableStatementUsed = callableStatementQuery || isCallableStatement( translatedQuery ); final Statement statement; if ( preparedParameterNames.length == 0 ) { statement = getConnection( parameters ).createStatement( getBestResultSetType( parameters ), ResultSet.CONCUR_READ_ONLY ); } else { if ( callableStatementUsed ) { final CallableStatement pstmt = getConnection( parameters ).prepareCall( translatedQuery, getBestResultSetType( parameters ), ResultSet.CONCUR_READ_ONLY ); if ( isCallableStatementQuery( translatedQuery ) ) { pstmt.registerOutParameter( 1, Types.OTHER ); parametrize( parameters, preparedParameterNames, pstmt, false, 1 ); } else { parametrize( parameters, preparedParameterNames, pstmt, false, 0 ); } statement = pstmt; } else { final PreparedStatement pstmt = getConnection( parameters ).prepareStatement( translatedQuery, getBestResultSetType( parameters ), ResultSet.CONCUR_READ_ONLY ); parametrize( parameters, preparedParameterNames, pstmt, isExpandArrays(), 0 ); statement = pstmt; } } final Object queryLimit = parameters.get( DataFactory.QUERY_LIMIT ); try { if ( queryLimit instanceof Number ) { final Number i = (Number) queryLimit; final int max = i.intValue(); if ( max > 0 ) { statement.setMaxRows( max ); } } } catch ( SQLException sqle ) { logger.warn( "Driver indicated error: Failed to set query-limit: " + queryLimit, sqle ); } final Object queryTimeout = parameters.get( DataFactory.QUERY_TIMEOUT ); try { if ( queryTimeout instanceof Number ) { final Number i = (Number) queryTimeout; final int seconds = i.intValue(); if ( seconds > 0 ) { statement.setQueryTimeout( seconds ); } } } catch ( SQLException sqle ) { logger.warn( "Driver indicated error: Failed to set query-timeout: " + queryTimeout, sqle ); } final ResultSet res; try { currentRunningStatement = statement; res = performQuery( statement, translatedQuery, preparedParameterNames ); } finally { currentRunningStatement = null; } boolean simpleMode = true; if ( globalConfig != null ) { simpleMode = "simple".equalsIgnoreCase( globalConfig.getConfigProperty( ResultSetTableModelFactory.RESULTSET_FACTORY_MODE ) ); } if ( simpleMode ) { return ResultSetTableModelFactory.getInstance().generateDefaultTableModel( res, columnNameMapping ); } return ResultSetTableModelFactory.getInstance().createTableModel( res, columnNameMapping, true ); }
SimpleSQLReportDataFactory extends AbstractDataFactory { protected TableModel parametrizeAndQuery( final DataRow parameters, final String translatedQuery, final String[] preparedParameterNames ) throws SQLException { final boolean callableStatementQuery = isCallableStatementQuery( translatedQuery ); final boolean callableStatementUsed = callableStatementQuery || isCallableStatement( translatedQuery ); final Statement statement; if ( preparedParameterNames.length == 0 ) { statement = getConnection( parameters ).createStatement( getBestResultSetType( parameters ), ResultSet.CONCUR_READ_ONLY ); } else { if ( callableStatementUsed ) { final CallableStatement pstmt = getConnection( parameters ).prepareCall( translatedQuery, getBestResultSetType( parameters ), ResultSet.CONCUR_READ_ONLY ); if ( isCallableStatementQuery( translatedQuery ) ) { pstmt.registerOutParameter( 1, Types.OTHER ); parametrize( parameters, preparedParameterNames, pstmt, false, 1 ); } else { parametrize( parameters, preparedParameterNames, pstmt, false, 0 ); } statement = pstmt; } else { final PreparedStatement pstmt = getConnection( parameters ).prepareStatement( translatedQuery, getBestResultSetType( parameters ), ResultSet.CONCUR_READ_ONLY ); parametrize( parameters, preparedParameterNames, pstmt, isExpandArrays(), 0 ); statement = pstmt; } } final Object queryLimit = parameters.get( DataFactory.QUERY_LIMIT ); try { if ( queryLimit instanceof Number ) { final Number i = (Number) queryLimit; final int max = i.intValue(); if ( max > 0 ) { statement.setMaxRows( max ); } } } catch ( SQLException sqle ) { logger.warn( "Driver indicated error: Failed to set query-limit: " + queryLimit, sqle ); } final Object queryTimeout = parameters.get( DataFactory.QUERY_TIMEOUT ); try { if ( queryTimeout instanceof Number ) { final Number i = (Number) queryTimeout; final int seconds = i.intValue(); if ( seconds > 0 ) { statement.setQueryTimeout( seconds ); } } } catch ( SQLException sqle ) { logger.warn( "Driver indicated error: Failed to set query-timeout: " + queryTimeout, sqle ); } final ResultSet res; try { currentRunningStatement = statement; res = performQuery( statement, translatedQuery, preparedParameterNames ); } finally { currentRunningStatement = null; } boolean simpleMode = true; if ( globalConfig != null ) { simpleMode = "simple".equalsIgnoreCase( globalConfig.getConfigProperty( ResultSetTableModelFactory.RESULTSET_FACTORY_MODE ) ); } if ( simpleMode ) { return ResultSetTableModelFactory.getInstance().generateDefaultTableModel( res, columnNameMapping ); } return ResultSetTableModelFactory.getInstance().createTableModel( res, columnNameMapping, true ); } }
SimpleSQLReportDataFactory extends AbstractDataFactory { protected TableModel parametrizeAndQuery( final DataRow parameters, final String translatedQuery, final String[] preparedParameterNames ) throws SQLException { final boolean callableStatementQuery = isCallableStatementQuery( translatedQuery ); final boolean callableStatementUsed = callableStatementQuery || isCallableStatement( translatedQuery ); final Statement statement; if ( preparedParameterNames.length == 0 ) { statement = getConnection( parameters ).createStatement( getBestResultSetType( parameters ), ResultSet.CONCUR_READ_ONLY ); } else { if ( callableStatementUsed ) { final CallableStatement pstmt = getConnection( parameters ).prepareCall( translatedQuery, getBestResultSetType( parameters ), ResultSet.CONCUR_READ_ONLY ); if ( isCallableStatementQuery( translatedQuery ) ) { pstmt.registerOutParameter( 1, Types.OTHER ); parametrize( parameters, preparedParameterNames, pstmt, false, 1 ); } else { parametrize( parameters, preparedParameterNames, pstmt, false, 0 ); } statement = pstmt; } else { final PreparedStatement pstmt = getConnection( parameters ).prepareStatement( translatedQuery, getBestResultSetType( parameters ), ResultSet.CONCUR_READ_ONLY ); parametrize( parameters, preparedParameterNames, pstmt, isExpandArrays(), 0 ); statement = pstmt; } } final Object queryLimit = parameters.get( DataFactory.QUERY_LIMIT ); try { if ( queryLimit instanceof Number ) { final Number i = (Number) queryLimit; final int max = i.intValue(); if ( max > 0 ) { statement.setMaxRows( max ); } } } catch ( SQLException sqle ) { logger.warn( "Driver indicated error: Failed to set query-limit: " + queryLimit, sqle ); } final Object queryTimeout = parameters.get( DataFactory.QUERY_TIMEOUT ); try { if ( queryTimeout instanceof Number ) { final Number i = (Number) queryTimeout; final int seconds = i.intValue(); if ( seconds > 0 ) { statement.setQueryTimeout( seconds ); } } } catch ( SQLException sqle ) { logger.warn( "Driver indicated error: Failed to set query-timeout: " + queryTimeout, sqle ); } final ResultSet res; try { currentRunningStatement = statement; res = performQuery( statement, translatedQuery, preparedParameterNames ); } finally { currentRunningStatement = null; } boolean simpleMode = true; if ( globalConfig != null ) { simpleMode = "simple".equalsIgnoreCase( globalConfig.getConfigProperty( ResultSetTableModelFactory.RESULTSET_FACTORY_MODE ) ); } if ( simpleMode ) { return ResultSetTableModelFactory.getInstance().generateDefaultTableModel( res, columnNameMapping ); } return ResultSetTableModelFactory.getInstance().createTableModel( res, columnNameMapping, true ); } SimpleSQLReportDataFactory();  SimpleSQLReportDataFactory( final Connection connection );  SimpleSQLReportDataFactory( final ConnectionProvider connectionProvider ); }
SimpleSQLReportDataFactory extends AbstractDataFactory { protected TableModel parametrizeAndQuery( final DataRow parameters, final String translatedQuery, final String[] preparedParameterNames ) throws SQLException { final boolean callableStatementQuery = isCallableStatementQuery( translatedQuery ); final boolean callableStatementUsed = callableStatementQuery || isCallableStatement( translatedQuery ); final Statement statement; if ( preparedParameterNames.length == 0 ) { statement = getConnection( parameters ).createStatement( getBestResultSetType( parameters ), ResultSet.CONCUR_READ_ONLY ); } else { if ( callableStatementUsed ) { final CallableStatement pstmt = getConnection( parameters ).prepareCall( translatedQuery, getBestResultSetType( parameters ), ResultSet.CONCUR_READ_ONLY ); if ( isCallableStatementQuery( translatedQuery ) ) { pstmt.registerOutParameter( 1, Types.OTHER ); parametrize( parameters, preparedParameterNames, pstmt, false, 1 ); } else { parametrize( parameters, preparedParameterNames, pstmt, false, 0 ); } statement = pstmt; } else { final PreparedStatement pstmt = getConnection( parameters ).prepareStatement( translatedQuery, getBestResultSetType( parameters ), ResultSet.CONCUR_READ_ONLY ); parametrize( parameters, preparedParameterNames, pstmt, isExpandArrays(), 0 ); statement = pstmt; } } final Object queryLimit = parameters.get( DataFactory.QUERY_LIMIT ); try { if ( queryLimit instanceof Number ) { final Number i = (Number) queryLimit; final int max = i.intValue(); if ( max > 0 ) { statement.setMaxRows( max ); } } } catch ( SQLException sqle ) { logger.warn( "Driver indicated error: Failed to set query-limit: " + queryLimit, sqle ); } final Object queryTimeout = parameters.get( DataFactory.QUERY_TIMEOUT ); try { if ( queryTimeout instanceof Number ) { final Number i = (Number) queryTimeout; final int seconds = i.intValue(); if ( seconds > 0 ) { statement.setQueryTimeout( seconds ); } } } catch ( SQLException sqle ) { logger.warn( "Driver indicated error: Failed to set query-timeout: " + queryTimeout, sqle ); } final ResultSet res; try { currentRunningStatement = statement; res = performQuery( statement, translatedQuery, preparedParameterNames ); } finally { currentRunningStatement = null; } boolean simpleMode = true; if ( globalConfig != null ) { simpleMode = "simple".equalsIgnoreCase( globalConfig.getConfigProperty( ResultSetTableModelFactory.RESULTSET_FACTORY_MODE ) ); } if ( simpleMode ) { return ResultSetTableModelFactory.getInstance().generateDefaultTableModel( res, columnNameMapping ); } return ResultSetTableModelFactory.getInstance().createTableModel( res, columnNameMapping, true ); } SimpleSQLReportDataFactory();  SimpleSQLReportDataFactory( final Connection connection );  SimpleSQLReportDataFactory( final ConnectionProvider connectionProvider ); String getUserField(); void setUserField( final String userField ); String getPasswordField(); void setPasswordField( final String passwordField ); int getBestResultSetType( final DataRow dataRow ); synchronized TableModel queryData( final String query, final DataRow parameters ); String[] getReferencedFields( final String query, final DataRow parameters ); static boolean isExpandArrayParameterNeeded( final String query ); ResultSet performQuery( Statement statement, final String translatedQuery, final String[] preparedParameterNames ); void cancelRunningQuery(); synchronized void close(); SimpleSQLReportDataFactory clone(); void setConnectionProvider( final ConnectionProvider connectionProvider ); ConnectionProvider getConnectionProvider(); boolean isQueryExecutable( final String query, final DataRow parameters ); String[] getQueryNames(); ArrayList<Object> getQueryHash( final String queryName, final DataRow parameter ); }
SimpleSQLReportDataFactory extends AbstractDataFactory { protected TableModel parametrizeAndQuery( final DataRow parameters, final String translatedQuery, final String[] preparedParameterNames ) throws SQLException { final boolean callableStatementQuery = isCallableStatementQuery( translatedQuery ); final boolean callableStatementUsed = callableStatementQuery || isCallableStatement( translatedQuery ); final Statement statement; if ( preparedParameterNames.length == 0 ) { statement = getConnection( parameters ).createStatement( getBestResultSetType( parameters ), ResultSet.CONCUR_READ_ONLY ); } else { if ( callableStatementUsed ) { final CallableStatement pstmt = getConnection( parameters ).prepareCall( translatedQuery, getBestResultSetType( parameters ), ResultSet.CONCUR_READ_ONLY ); if ( isCallableStatementQuery( translatedQuery ) ) { pstmt.registerOutParameter( 1, Types.OTHER ); parametrize( parameters, preparedParameterNames, pstmt, false, 1 ); } else { parametrize( parameters, preparedParameterNames, pstmt, false, 0 ); } statement = pstmt; } else { final PreparedStatement pstmt = getConnection( parameters ).prepareStatement( translatedQuery, getBestResultSetType( parameters ), ResultSet.CONCUR_READ_ONLY ); parametrize( parameters, preparedParameterNames, pstmt, isExpandArrays(), 0 ); statement = pstmt; } } final Object queryLimit = parameters.get( DataFactory.QUERY_LIMIT ); try { if ( queryLimit instanceof Number ) { final Number i = (Number) queryLimit; final int max = i.intValue(); if ( max > 0 ) { statement.setMaxRows( max ); } } } catch ( SQLException sqle ) { logger.warn( "Driver indicated error: Failed to set query-limit: " + queryLimit, sqle ); } final Object queryTimeout = parameters.get( DataFactory.QUERY_TIMEOUT ); try { if ( queryTimeout instanceof Number ) { final Number i = (Number) queryTimeout; final int seconds = i.intValue(); if ( seconds > 0 ) { statement.setQueryTimeout( seconds ); } } } catch ( SQLException sqle ) { logger.warn( "Driver indicated error: Failed to set query-timeout: " + queryTimeout, sqle ); } final ResultSet res; try { currentRunningStatement = statement; res = performQuery( statement, translatedQuery, preparedParameterNames ); } finally { currentRunningStatement = null; } boolean simpleMode = true; if ( globalConfig != null ) { simpleMode = "simple".equalsIgnoreCase( globalConfig.getConfigProperty( ResultSetTableModelFactory.RESULTSET_FACTORY_MODE ) ); } if ( simpleMode ) { return ResultSetTableModelFactory.getInstance().generateDefaultTableModel( res, columnNameMapping ); } return ResultSetTableModelFactory.getInstance().createTableModel( res, columnNameMapping, true ); } SimpleSQLReportDataFactory();  SimpleSQLReportDataFactory( final Connection connection );  SimpleSQLReportDataFactory( final ConnectionProvider connectionProvider ); String getUserField(); void setUserField( final String userField ); String getPasswordField(); void setPasswordField( final String passwordField ); int getBestResultSetType( final DataRow dataRow ); synchronized TableModel queryData( final String query, final DataRow parameters ); String[] getReferencedFields( final String query, final DataRow parameters ); static boolean isExpandArrayParameterNeeded( final String query ); ResultSet performQuery( Statement statement, final String translatedQuery, final String[] preparedParameterNames ); void cancelRunningQuery(); synchronized void close(); SimpleSQLReportDataFactory clone(); void setConnectionProvider( final ConnectionProvider connectionProvider ); ConnectionProvider getConnectionProvider(); boolean isQueryExecutable( final String query, final DataRow parameters ); String[] getQueryNames(); ArrayList<Object> getQueryHash( final String queryName, final DataRow parameter ); static Configuration globalConfig; }