Package org.qi4j.library.sql.common

Examples of org.qi4j.library.sql.common.Databases$StatementVisitor


    }

    public void init( @Service @IdentifiedBy( "testds" ) DataSource dataSource, @Service @IdentifiedBy( "testds2" ) ServiceReference<DataSource> dataSource2 )
            throws SQLException, PropertyVetoException
    {
        Databases databases = new Databases( dataSource );

        // Insert some data and print it out
        databases.update( "insert into test values ('id1','foo')" );
        databases.query( "select * from test" ).transferTo( Outputs.withReceiver( new Receiver<ResultSet, SQLException>()
        {
            @Override
            public void receive( ResultSet item )
                    throws SQLException
            {
                System.out.println( item.getString( "id" ) );
            }

        } ) );

        Databases databases2 = new Databases( dataSource2.get() );

        // Insert some data and print it out
        databases2.update( "insert into test values ('id2','bar')" );
        databases2.query( "select * from test" ).transferTo( Outputs.withReceiver( new Receiver<ResultSet, SQLException>()
        {
            @Override
            public void receive( ResultSet item )
                    throws SQLException
            {
                System.out.println( item.getString( "id" ) );
            }

        } ) );

        // Trip the CB
        dataSource2.metaInfo( CircuitBreaker.class ).trip();

        // This should now fail
        try {
            databases2.query( "select * from test" ).transferTo( Outputs.withReceiver( new Receiver<ResultSet, SQLException>()
            {
                @Override
                public void receive( ResultSet item )
                        throws SQLException
                {
                    System.out.println( item.getString( "id" ) );
                }

            } ) );

            Assert.fail();

        } catch ( Throwable e ) {
            // Correct
        }

        // Turn the CB back on
        dataSource2.metaInfo( CircuitBreaker.class ).turnOn();

        // This should now work
        databases2.query( "select * from test" ).transferTo( Outputs.withReceiver( new Receiver<ResultSet, SQLException>()
        {
            @Override
            public void receive( ResultSet item )
                    throws SQLException
            {
View Full Code Here


        // START SNIPPET: io
        // Look up the DataSource
        DataSource ds = module.findService( DataSource.class ).get();

        // Instanciate Databases helper
        Databases database = new Databases( ds );

        // Assert that insertion works
        assertTrue( database.update( "insert into test values ('someid', 'bar')" ) == 1 );
        // END SNIPPET: io

        database.query( "select * from test", new Databases.ResultSetVisitor()
        {
            @Override
            public boolean visit( ResultSet visited )
                throws SQLException
            {
                assertThat( visited.getString( "id" ), equalTo( "someid" ) );
                assertThat( visited.getString( "foo" ), equalTo( "bar" ) );

                return true;
            }
        } );

        Function<ResultSet, SomeValue> toValue = new Function<ResultSet, SomeValue>()
        {
            @Override
            public SomeValue map( ResultSet resultSet )
            {
                ValueBuilder<SomeValue> builder = assembler.module().newValueBuilder( SomeValue.class );
                try
                {
                    builder.prototype().id().set( resultSet.getString( "id" ) );
                    builder.prototype().foo().set( resultSet.getString( "foo" ) );
                }
                catch( SQLException e )
                {
                    throw new IllegalArgumentException( "Could not convert to SomeValue", e );
                }

                return builder.newInstance();
            }
        };

        // START SNIPPET: io
        // Select rows and load them in a List
        List<SomeValue> rows = new ArrayList<SomeValue>();
        database.query( "select * from test" ).transferTo( map( toValue, collection( rows ) ) );

        // Transfer all rows to System.out
        Inputs.iterable( rows ).transferTo( Outputs.systemOut() );
        // END SNIPPET: io
    }
View Full Code Here

TOP

Related Classes of org.qi4j.library.sql.common.Databases$StatementVisitor

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.