diff --git a/manifold-deps-parent/manifold-sql-rt/src/main/java/manifold/sql/rt/api/Runner.java b/manifold-deps-parent/manifold-sql-rt/src/main/java/manifold/sql/rt/api/Runner.java index 4a9233f6c..08b9d873d 100644 --- a/manifold-deps-parent/manifold-sql-rt/src/main/java/manifold/sql/rt/api/Runner.java +++ b/manifold-deps-parent/manifold-sql-rt/src/main/java/manifold/sql/rt/api/Runner.java @@ -19,6 +19,7 @@ import manifold.util.ManExceptionUtil; import java.sql.*; +import java.util.Iterator; public class Runner { @@ -32,7 +33,7 @@ public Runner( QueryContext ctx, String sqlQuery ) } @SuppressWarnings( "unused" ) - public Result run() + public Result fetch() { ConnectionProvider cp = Dependencies.instance().getConnectionProvider(); try( Connection c = cp.getConnection( _ctx.getConfigName(), _ctx.getQueryClass() ) ) @@ -52,6 +53,38 @@ public Result run() } } + @SuppressWarnings( "unused" ) + public T fetchOne() + { + ConnectionProvider cp = Dependencies.instance().getConnectionProvider(); + try( Connection c = cp.getConnection( _ctx.getConfigName(), _ctx.getQueryClass() ) ) + { + try( PreparedStatement ps = c.prepareStatement( _sqlQuery ) ) + { + setParameters( ps ); + try( ResultSet resultSet = ps.executeQuery() ) + { + Result rs = new Result<>( _ctx.getTxScope(), resultSet, _ctx.getRowMaker() ); + Iterator iterator = rs.iterator(); + if( !iterator.hasNext() ) + { + return null; + } + T one = iterator.next(); + if( iterator.hasNext() ) + { + throw new SQLException( "Results contain more than one row." ); + } + return one; + } + } + } + catch( SQLException e ) + { + throw ManExceptionUtil.unchecked( e ); + } + } + private void setParameters( PreparedStatement ps ) throws SQLException { int i = 0; diff --git a/manifold-deps-parent/manifold-sql-rt/src/main/java/manifold/sql/rt/impl/BasicCrudProvider.java b/manifold-deps-parent/manifold-sql-rt/src/main/java/manifold/sql/rt/impl/BasicCrudProvider.java index 7a6a78420..e88cb6288 100644 --- a/manifold-deps-parent/manifold-sql-rt/src/main/java/manifold/sql/rt/impl/BasicCrudProvider.java +++ b/manifold-deps-parent/manifold-sql-rt/src/main/java/manifold/sql/rt/impl/BasicCrudProvider.java @@ -141,7 +141,7 @@ public T read( QueryContext ctx ) T result = iterator.next(); if( iterator.hasNext() ) { - throw new SQLException( "Expecting one result row" ); + throw new SQLException( "Results contain more than one row." ); } return result; } diff --git a/manifold-deps-parent/manifold-sql-test/src/test/java/manifold/sql/schema/crud/CrudTest.java b/manifold-deps-parent/manifold-sql-test/src/test/java/manifold/sql/schema/crud/CrudTest.java index 3ec42e782..a85a7e210 100644 --- a/manifold-deps-parent/manifold-sql-test/src/test/java/manifold/sql/schema/crud/CrudTest.java +++ b/manifold-deps-parent/manifold-sql-test/src/test/java/manifold/sql/schema/crud/CrudTest.java @@ -42,7 +42,7 @@ public void testCreate() throws SQLException assertTrue( hi.getCountryId() > 0 ); auto row = "[.sql:H2Sakila/] SELECT country_id FROM Country where country = 'mycountry'" - .run( txScope ).iterator().next(); + .fetchOne( txScope ); assertEquals( row.getCountryId(), hi.getCountryId() ); } diff --git a/manifold-deps-parent/manifold-sql-test/src/test/java/manifold/sql/schema/crud/DefaultScopeCrudTest.java b/manifold-deps-parent/manifold-sql-test/src/test/java/manifold/sql/schema/crud/DefaultScopeCrudTest.java index d32f59ca3..461950930 100644 --- a/manifold-deps-parent/manifold-sql-test/src/test/java/manifold/sql/schema/crud/DefaultScopeCrudTest.java +++ b/manifold-deps-parent/manifold-sql-test/src/test/java/manifold/sql/schema/crud/DefaultScopeCrudTest.java @@ -37,7 +37,7 @@ public void testCreate() throws SQLException assertTrue( hi.getCountryId() > 0 ); auto row = "[.sql:H2Sakila/] SELECT country_id FROM Country where country = 'mycountry'" - .run().iterator().next(); + .fetchOne(); assertEquals( row.getCountryId(), hi.getCountryId() ); } diff --git a/manifold-deps-parent/manifold-sql-test/src/test/java/manifold/sql/schema/simple/ScratchTest.java b/manifold-deps-parent/manifold-sql-test/src/test/java/manifold/sql/schema/simple/ScratchTest.java index 5c4bbc769..2dafb5c09 100644 --- a/manifold-deps-parent/manifold-sql-test/src/test/java/manifold/sql/schema/simple/ScratchTest.java +++ b/manifold-deps-parent/manifold-sql-test/src/test/java/manifold/sql/schema/simple/ScratchTest.java @@ -31,7 +31,7 @@ public class ScratchTest extends H2SalesTest public void testSimple() { StringBuilder sb = new StringBuilder(); - for( PurchaseOrder po : Foo.run() ) + for( PurchaseOrder po : Foo.fetch() ) { // just make sure the results can be navigated assertNotNull( po.getId() + " " + po.getCustomerRef().getId() + " " + po.getOrderDate() ); @@ -47,7 +47,7 @@ public void testCommentQueryWithParameters() Select * From purchase_order Where customer_id = :c_id */ StringBuilder actual = new StringBuilder(); - for( PurchaseOrder po : MyQuery.run( 2L ) ) + for( PurchaseOrder po : MyQuery.fetch( 2L ) ) { actual.append( po.getId() ).append( "," ).append( po.getCustomerRef().getId() ).append( "," ).append( po.getOrderDate() ).append( "\n" ); } @@ -67,7 +67,7 @@ public void testStringQueryWithParameters() StringBuilder actual = new StringBuilder(); actual = new StringBuilder(); - for( PurchaseOrder po : query.run( 2L ) ) + for( PurchaseOrder po : query.fetch( 2L ) ) { actual.append( po.getId() ).append( "," ).append( po.getCustomerRef().getId() ).append( "," ).append( po.getOrderDate() ).append( "\n" ); } @@ -83,7 +83,7 @@ public void testStringJoinQueryWithParameters() "3,2,2023-09-08,Cheryl Dunno\n"; StringBuilder actual = new StringBuilder(); - for( auto row : query.run( 2L ) ) + for( auto row : query.fetch( 2L ) ) { auto flatRow = row.flatRow(); actual.append( flatRow.getId() ).append( "," ) @@ -94,7 +94,7 @@ public void testStringJoinQueryWithParameters() assertEquals( expected, actual.toString() ); actual = new StringBuilder(); - for( auto row : query.run( 2L ) ) + for( auto row : query.fetch( 2L ) ) { actual.append( row.getPurchaseOrder().getId() ).append( "," ) .append( row.getPurchaseOrder().getCustomerRef().getId() ).append( "," ) diff --git a/manifold-deps-parent/manifold-sql-test/src/test/java/manifold/sql/schema/simple/ScratchTest_Sakila.java b/manifold-deps-parent/manifold-sql-test/src/test/java/manifold/sql/schema/simple/ScratchTest_Sakila.java index 73c75c904..f854e3725 100644 --- a/manifold-deps-parent/manifold-sql-test/src/test/java/manifold/sql/schema/simple/ScratchTest_Sakila.java +++ b/manifold-deps-parent/manifold-sql-test/src/test/java/manifold/sql/schema/simple/ScratchTest_Sakila.java @@ -27,7 +27,7 @@ public class ScratchTest_Sakila extends H2SakilaTest public void testSomeInterestingQueries() { Stores s = "[Stores.sql:H2Sakila/] Select * From store"; - for( Store r : s.run() ) + for( Store r : s.fetch() ) { System.out.println( r.display() ); System.out.println( r.getAddressRef().display() ); @@ -42,7 +42,7 @@ JOIN film_actor AS fa USING (actor_id) ORDER BY films DESC LIMIT 1; */ - for (ActorWithMostFilms.Row row : ActorWithMostFilms.run()) { + for (ActorWithMostFilms.Row row : ActorWithMostFilms.fetch()) { System.out.println(row.display()); } @@ -55,18 +55,17 @@ GROUP BY CAST(payment_date AS DATE) ) p ORDER BY payment_date; */ - for (CumulativeRevenueAllStores.Row row : CumulativeRevenueAllStores.run()) { + for (CumulativeRevenueAllStores.Row row : CumulativeRevenueAllStores.fetch()) { System.out.println(row.getPaymentDate()); System.out.println(row.getSumAmount_Over_OrderByPaymentDate()); System.out.println(row.display()); } - auto one = "[.sql:H2Sakila/] select 1 from dual"; - auto run = one.run(); - run.forEach( r -> System.out.println(r.display())); + auto one = "[.sql:H2Sakila/] select 1 from dual".fetchOne(); + System.out.println(one.display()); - for( Staff staff: "[.sql:H2Sakila/] select * from staff".run() ) + for( Staff staff: "[.sql:H2Sakila/] select * from staff".fetch() ) { System.out.println( staff.display() ); } diff --git a/manifold-deps-parent/manifold-sql/src/main/java/manifold/sql/query/type/SqlParentType.java b/manifold-deps-parent/manifold-sql/src/main/java/manifold/sql/query/type/SqlParentType.java index 4268e0873..64c50442a 100644 --- a/manifold-deps-parent/manifold-sql/src/main/java/manifold/sql/query/type/SqlParentType.java +++ b/manifold-deps-parent/manifold-sql/src/main/java/manifold/sql/query/type/SqlParentType.java @@ -82,13 +82,13 @@ void render( StringBuilder sb, JavaFileManager.Location location, IModule module addActualNameAnnotation( srcClass, name, false ); addImports( srcClass ); addFlatRowType( srcClass ); - addRunMethods( srcClass ); + addFetchMethods( srcClass ); addFragmentValueMethod( srcClass ); srcClass.render( sb, 0 ); } - private void addRunMethods( SrcLinkedClass srcClass ) + private void addFetchMethods( SrcLinkedClass srcClass ) { Pair> selectedTable = getQuery().findSelectedTable(); String rowType; @@ -104,7 +104,7 @@ private void addRunMethods( SrcLinkedClass srcClass ) addRowType( srcClass ); } - addRunMethods( srcClass, rowType ); + addFetchMethods( srcClass, rowType ); } private QueryTable getQuery() @@ -153,18 +153,24 @@ private String getQueryName() return (name == null || name.isEmpty()) ? ANONYMOUS_TYPE + _anonCount++ : name; } - private void addRunMethods( SrcLinkedClass srcClass, @SuppressWarnings( "unused" ) String rowType ) + private void addFetchMethods( SrcLinkedClass srcClass, @SuppressWarnings( "unused" ) String rowType ) + { + addFetchMethods( srcClass, rowType, "fetch", "Iterable<$rowType>" ); + addFetchMethods( srcClass, rowType, "fetchOne", rowType ); + } + private void addFetchMethods( SrcLinkedClass srcClass, @SuppressWarnings( "unused" ) String rowType, + String methodName, @SuppressWarnings( "unused" ) String returnType ) { //noinspection unused String configName = _model.getScope().getDbconfig().getName(); SrcMethod method = new SrcMethod( srcClass ) - .name( "run" ) + .name( methodName ) .modifiers( isFragment() ? Flags.DEFAULT : Modifier.STATIC ) - .returns( new SrcType( "Iterable<$rowType>" ) ); + .returns( new SrcType( returnType ) ); addRequiredParameters( method ); StringBuilder sb = new StringBuilder(); - sb.append( "return run(DefaultTxScopeProvider.instance().defaultScope($configName.class)" ); + sb.append( "return $methodName(DefaultTxScopeProvider.instance().defaultScope($configName.class)" ); sb.append( method.getParameters().isEmpty() ? "" : ", " ); method.forwardParameters( sb ); sb.append( ");" ); @@ -172,10 +178,10 @@ private void addRunMethods( SrcLinkedClass srcClass, @SuppressWarnings( "unused" srcClass.addMethod( method ); method = new SrcMethod( srcClass ) - .name( "run" ) + .name( methodName ) .modifiers( isFragment() ? Flags.DEFAULT : Modifier.STATIC ) .addParam( "txScope", TxScope.class ) - .returns( new SrcType( "Iterable<$rowType>" ) ); + .returns( new SrcType( returnType ) ); addRequiredParameters( method ); sb = new StringBuilder(); sb.append( "DataBindings paramBindings = new DataBindings(new ConcurrentHashMap<>());\n" ); @@ -203,7 +209,7 @@ private void addRunMethods( SrcLinkedClass srcClass, @SuppressWarnings( "unused" " return new Runner<$rowType>(new QueryContext<>(txScope, $rowType.class, null, ${getJdbcParamTypes()}, paramBindings, \"$configName\",\n" + " rowBindings -> new $rowType() {public TxBindings getBindings() { return rowBindings; }}),\n" + " \"$query\"\n" + - " ).run();" ); + " ).$methodName();" ); method.body( sb.toString() ); srcClass.addMethod( method ); }