Skip to content

Commit

Permalink
manifold-sql changes
Browse files Browse the repository at this point in the history
- rename run() query exec methods to fetch() to better align with the fetchXxx() fk methods
- add fetchOne() methods for convenience
  • Loading branch information
rsmckinney committed Aug 15, 2023
1 parent aa01545 commit cdf3326
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import manifold.util.ManExceptionUtil;

import java.sql.*;
import java.util.Iterator;

public class Runner<T extends ResultRow>
{
Expand All @@ -32,7 +33,7 @@ public Runner( QueryContext<T> ctx, String sqlQuery )
}

@SuppressWarnings( "unused" )
public Result<T> run()
public Result<T> fetch()
{
ConnectionProvider cp = Dependencies.instance().getConnectionProvider();
try( Connection c = cp.getConnection( _ctx.getConfigName(), _ctx.getQueryClass() ) )
Expand All @@ -52,6 +53,38 @@ public Result<T> 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<T> rs = new Result<>( _ctx.getTxScope(), resultSet, _ctx.getRowMaker() );
Iterator<T> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public <T extends TableRow> T read( QueryContext<T> 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() );
Expand All @@ -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" );
}
Expand All @@ -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" );
}
Expand All @@ -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( "," )
Expand All @@ -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( "," )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() );
Expand All @@ -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());
}

Expand All @@ -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() );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<SchemaTable, List<QueryColumn>> selectedTable = getQuery().findSelectedTable();
String rowType;
Expand All @@ -104,7 +104,7 @@ private void addRunMethods( SrcLinkedClass srcClass )
addRowType( srcClass );
}

addRunMethods( srcClass, rowType );
addFetchMethods( srcClass, rowType );
}

private QueryTable getQuery()
Expand Down Expand Up @@ -153,29 +153,35 @@ 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( ");" );
method.body( sb.toString() );
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" );
Expand Down Expand Up @@ -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 );
}
Expand Down

0 comments on commit cdf3326

Please sign in to comment.