From f80a58754ef749862f108d4aa4f0992cc0f51c3b Mon Sep 17 00:00:00 2001 From: Martin Vahlensieck Date: Wed, 31 Jul 2024 12:30:49 +0200 Subject: [PATCH] Reformat code & optimize imports --- .../java/org/polypheny/jdbc/RpcService.java | 4 +- .../jdbc/multimodel/GraphResult.java | 32 ++++-- .../multimodel/RelationalColumnMetadata.java | 3 + .../jdbc/multimodel/RelationalMetadata.java | 3 +- .../org/polypheny/jdbc/types/PolyEdge.java | 2 + .../jdbc/types/PolyGraphElement.java | 1 + .../org/polypheny/jdbc/types/TypedValue.java | 43 -------- .../org/polypheny/jdbc/utils/ProtoUtils.java | 10 +- .../org/polypheny/jdbc/ConnectionTest.java | 2 +- src/test/java/org/polypheny/jdbc/Demo.java | 39 +++---- .../java/org/polypheny/jdbc/QueryTest.java | 12 +- .../polypheny/jdbc/types/TypedValueTest.java | 103 ++++++++++++++++++ 12 files changed, 167 insertions(+), 87 deletions(-) diff --git a/src/main/java/org/polypheny/jdbc/RpcService.java b/src/main/java/org/polypheny/jdbc/RpcService.java index 1df50ba..89b69c5 100644 --- a/src/main/java/org/polypheny/jdbc/RpcService.java +++ b/src/main/java/org/polypheny/jdbc/RpcService.java @@ -419,6 +419,7 @@ PreparedStatementSignature prepareIndexedStatement( PrepareStatementRequest msg, return completeSynchronously( req, timeout ).getPreparedStatementSignature(); } + public PreparedStatementSignature prepareNamedStatement( PrepareStatementRequest msg, int timeout ) throws PrismInterfaceServiceException { Request.Builder req = newMessage(); req.setPrepareIndexedStatementRequest( msg ); @@ -426,13 +427,13 @@ public PreparedStatementSignature prepareNamedStatement( PrepareStatementRequest } - StatementResult executeIndexedStatement( ExecuteIndexedStatementRequest msg, int timeout ) throws PrismInterfaceServiceException { Request.Builder req = newMessage(); req.setExecuteIndexedStatementRequest( msg ); return completeSynchronously( req, timeout ).getStatementResult(); } + public StatementResult executeNamedStatement( ExecuteNamedStatementRequest msg, int timeout ) throws PrismInterfaceServiceException { Request.Builder req = newMessage(); req.setExecuteNamedStatementRequest( msg ); @@ -466,4 +467,5 @@ CloseResultResponse closeResult( CloseResultRequest msg, int timeout ) throws Pr req.setCloseResultRequest( msg ); return completeSynchronously( req, timeout ).getCloseResultResponse(); } + } diff --git a/src/main/java/org/polypheny/jdbc/multimodel/GraphResult.java b/src/main/java/org/polypheny/jdbc/multimodel/GraphResult.java index ce7398f..f9848d9 100644 --- a/src/main/java/org/polypheny/jdbc/multimodel/GraphResult.java +++ b/src/main/java/org/polypheny/jdbc/multimodel/GraphResult.java @@ -17,7 +17,6 @@ package org.polypheny.jdbc.multimodel; import java.util.ArrayList; - import java.util.Iterator; import java.util.List; import java.util.NoSuchElementException; @@ -41,29 +40,30 @@ public class GraphResult extends Result implements Iterable { private final List elements; - public GraphResult( Frame frame, PolyStatement polyStatement ) { super( ResultType.GRAPH ); - this.polyStatement= polyStatement; + this.polyStatement = polyStatement; this.isFullyFetched = frame.getIsLast(); this.elements = new ArrayList<>(); - addGraphElements(frame.getGraphFrame()); + addGraphElements( frame.getGraphFrame() ); } + private void addGraphElements( GraphFrame graphFrame ) { - if (graphFrame.getNodesCount() > 0) { - graphFrame.getNodesList().forEach( n -> elements.add( new PolyNode(n) ) ); + if ( graphFrame.getNodesCount() > 0 ) { + graphFrame.getNodesList().forEach( n -> elements.add( new PolyNode( n ) ) ); return; } - if (graphFrame.getEdgesCount() > 0) { - graphFrame.getEdgesList().forEach( n -> elements.add( new PolyEdge(n) ) ); + if ( graphFrame.getEdgesCount() > 0 ) { + graphFrame.getEdgesList().forEach( n -> elements.add( new PolyEdge( n ) ) ); return; } - if (graphFrame.getPathsCount() > 0) { - graphFrame.getPathsList().forEach( n -> elements.add( new PolyPath(n) ) ); + if ( graphFrame.getPathsCount() > 0 ) { + graphFrame.getPathsList().forEach( n -> elements.add( new PolyPath( n ) ) ); } } + private void fetchMore() throws PrismInterfaceServiceException { int id = polyStatement.getStatementId(); int timeout = getPolyphenyConnection().getTimeout(); @@ -78,6 +78,7 @@ private void fetchMore() throws PrismInterfaceServiceException { addGraphElements( frame.getGraphFrame() ); } + private PolyConnection getPolyphenyConnection() { return polyStatement.getConnection(); } @@ -87,12 +88,18 @@ private PrismInterfaceClient getPrismInterfaceClient() { return getPolyphenyConnection().getPrismInterfaceClient(); } + @Override - public Iterator iterator() {return new GraphElementIterator();} + public Iterator iterator() { + return new GraphElementIterator(); + } + class GraphElementIterator implements Iterator { + int index = -1; + @Override public boolean hasNext() { if ( index + 1 >= elements.size() ) { @@ -108,6 +115,7 @@ public boolean hasNext() { return index + 1 < elements.size(); } + @Override public PolyGraphElement next() { if ( !hasNext() ) { @@ -115,8 +123,8 @@ public PolyGraphElement next() { } return elements.get( ++index ); } - } + } } diff --git a/src/main/java/org/polypheny/jdbc/multimodel/RelationalColumnMetadata.java b/src/main/java/org/polypheny/jdbc/multimodel/RelationalColumnMetadata.java index fa92070..f0004ef 100644 --- a/src/main/java/org/polypheny/jdbc/multimodel/RelationalColumnMetadata.java +++ b/src/main/java/org/polypheny/jdbc/multimodel/RelationalColumnMetadata.java @@ -21,6 +21,7 @@ @Getter public class RelationalColumnMetadata { + private final int columnIndex; private final boolean isNullable; private final int length; @@ -31,6 +32,7 @@ public class RelationalColumnMetadata { private final String protocolTypeName; private final int scale; + public RelationalColumnMetadata( ColumnMeta columnMeta ) { this.columnIndex = columnMeta.getColumnIndex(); this.isNullable = columnMeta.getIsNullable(); @@ -41,4 +43,5 @@ public RelationalColumnMetadata( ColumnMeta columnMeta ) { this.protocolTypeName = columnMeta.getTypeMeta().getProtoValueType().name(); this.scale = columnMeta.getScale(); } + } diff --git a/src/main/java/org/polypheny/jdbc/multimodel/RelationalMetadata.java b/src/main/java/org/polypheny/jdbc/multimodel/RelationalMetadata.java index f75e24e..f4fea83 100644 --- a/src/main/java/org/polypheny/jdbc/multimodel/RelationalMetadata.java +++ b/src/main/java/org/polypheny/jdbc/multimodel/RelationalMetadata.java @@ -19,12 +19,12 @@ import java.util.List; import java.util.Map; import java.util.stream.Collectors; -import lombok.Getter; import org.polypheny.jdbc.PrismInterfaceErrors; import org.polypheny.jdbc.PrismInterfaceServiceException; import org.polypheny.prism.ColumnMeta; public class RelationalMetadata { + private List columnMetas; private Map columnIndexes; @@ -35,6 +35,7 @@ public RelationalMetadata( List columnMetadata ) { } + public RelationalColumnMetadata getColumnMeta( int columnIndex ) throws PrismInterfaceServiceException { try { return columnMetas.get( columnIndex ); diff --git a/src/main/java/org/polypheny/jdbc/types/PolyEdge.java b/src/main/java/org/polypheny/jdbc/types/PolyEdge.java index 66a1801..92ac758 100644 --- a/src/main/java/org/polypheny/jdbc/types/PolyEdge.java +++ b/src/main/java/org/polypheny/jdbc/types/PolyEdge.java @@ -27,6 +27,7 @@ public class PolyEdge extends PolyGraphElement { private final String target; private final EdgeDirection direction; + public PolyEdge( ProtoEdge protoEdge ) { super(); this.id = protoEdge.getId(); @@ -43,6 +44,7 @@ public PolyEdge( ProtoEdge protoEdge ) { this.direction = EdgeDirection.valueOf( protoEdge.getDirection().name() ); } + enum EdgeDirection { LEFT_TO_RIGHT, RIGHT_TO_LEFT, diff --git a/src/main/java/org/polypheny/jdbc/types/PolyGraphElement.java b/src/main/java/org/polypheny/jdbc/types/PolyGraphElement.java index dced7e0..1c9df69 100644 --- a/src/main/java/org/polypheny/jdbc/types/PolyGraphElement.java +++ b/src/main/java/org/polypheny/jdbc/types/PolyGraphElement.java @@ -30,6 +30,7 @@ public class PolyGraphElement extends HashMap { protected String name; protected List labels; + public T unwrap( Class aClass ) throws PrismInterfaceServiceException { if ( aClass.isInstance( this ) ) { return aClass.cast( this ); diff --git a/src/main/java/org/polypheny/jdbc/types/TypedValue.java b/src/main/java/org/polypheny/jdbc/types/TypedValue.java index 9875650..47d02c7 100644 --- a/src/main/java/org/polypheny/jdbc/types/TypedValue.java +++ b/src/main/java/org/polypheny/jdbc/types/TypedValue.java @@ -1428,47 +1428,4 @@ private static PolyInterval getInterval( ProtoInterval interval ) { return new PolyInterval( interval.getMonths(), interval.getMilliseconds() ); } - - @Override - public String toString() { - if ( isSerialized ) { - deserialize(); - } - switch ( valueCase ) { - case BOOLEAN: - return "" + booleanValue; - case INTEGER: - return "" + integerValue; - case LONG: - return "" + bigintValue; - case BIG_DECIMAL: - return "" + bigDecimalValue; - case FLOAT: - return "" + floatValue; - case DOUBLE: - return "" + doubleValue; - case DATE: - return "" + dateValue; - case TIME: - return "" + timeValue; - case TIMESTAMP: - return "" + timestampValue; - case INTERVAL: - return "" + otherValue; - case STRING: - return varcharValue; - case BINARY: - return "BINARY"; - case NULL: - return "NULL"; - case LIST: - return "LIST"; - case FILE: - return "FILE"; - case DOCUMENT: - return "DOCUMENT"; - } - return ""; - } - } diff --git a/src/main/java/org/polypheny/jdbc/utils/ProtoUtils.java b/src/main/java/org/polypheny/jdbc/utils/ProtoUtils.java index 5aeb9ed..ef1b5a0 100644 --- a/src/main/java/org/polypheny/jdbc/utils/ProtoUtils.java +++ b/src/main/java/org/polypheny/jdbc/utils/ProtoUtils.java @@ -48,17 +48,17 @@ public static List serializeParameterList( List values ) } - public static Map serializeParameterMap(Map values) { - return values.entrySet().stream().collect(Collectors.toMap( + public static Map serializeParameterMap( Map values ) { + return values.entrySet().stream().collect( Collectors.toMap( Entry::getKey, value -> { try { return value.getValue().serialize(); - } catch (Exception e) { - throw new RuntimeException("Serialization failed", e); + } catch ( Exception e ) { + throw new RuntimeException( "Serialization failed", e ); } } - )); + ) ); } } diff --git a/src/test/java/org/polypheny/jdbc/ConnectionTest.java b/src/test/java/org/polypheny/jdbc/ConnectionTest.java index 4961c1a..2e9e74a 100644 --- a/src/test/java/org/polypheny/jdbc/ConnectionTest.java +++ b/src/test/java/org/polypheny/jdbc/ConnectionTest.java @@ -194,7 +194,7 @@ void testMetaDataGetProceduresNotStrict() throws SQLException { @Test @Disabled - // This test fails due to syntax definitions missing for some of the functions on the server side (operator registry). + // This test fails due to syntax definitions missing for some of the functions on the server side (operator registry). void testMetaDataGetFunctionsNotStrict() throws SQLException { try ( Connection con = DriverManager.getConnection( "jdbc:polypheny://127.0.0.1:20590?strict=false", "pa", "" ) ) { DatabaseMetaData meta = con.getMetaData(); diff --git a/src/test/java/org/polypheny/jdbc/Demo.java b/src/test/java/org/polypheny/jdbc/Demo.java index 4dfd3be..e796f68 100644 --- a/src/test/java/org/polypheny/jdbc/Demo.java +++ b/src/test/java/org/polypheny/jdbc/Demo.java @@ -40,46 +40,47 @@ public class Demo { private static String namespace = "public"; private static Connection con; - public static void main(String[] args) { + + public static void main( String[] args ) { try { - con = DriverManager.getConnection("jdbc:polypheny://127.0.0.1:20590?strict=false", "pa", ""); - if (!con.isWrapperFor(PolyConnection.class)) { - System.out.println("Driver must support unwrapping to PolyConnection"); + con = DriverManager.getConnection( "jdbc:polypheny://127.0.0.1:20590?strict=false", "pa", "" ); + if ( !con.isWrapperFor( PolyConnection.class ) ) { + System.out.println( "Driver must support unwrapping to PolyConnection" ); return; } - Scanner scanner = new Scanner(System.in); + Scanner scanner = new Scanner( System.in ); - while (true) { + while ( true ) { printPrompt(); String input = scanner.nextLine(); - if (input.startsWith("lng ")) { - language = input.substring(4).trim(); - System.out.println("Language set to " + language); - } else if (input.startsWith("ns ")) { - namespace = input.substring(3).trim(); - System.out.println("Namespace set to " + namespace); - } else if (input.equals("exit")) { + if ( input.startsWith( "lng " ) ) { + language = input.substring( 4 ).trim(); + System.out.println( "Language set to " + language ); + } else if ( input.startsWith( "ns " ) ) { + namespace = input.substring( 3 ).trim(); + System.out.println( "Namespace set to " + namespace ); + } else if ( input.equals( "exit" ) ) { break; } else { - if (language == null || namespace == null) { - System.out.println("Please set both language and namespace before executing a statement."); + if ( language == null || namespace == null ) { + System.out.println( "Please set both language and namespace before executing a statement." ); continue; } String statement = input.trim(); - executeStatement(namespace, language, statement); + executeStatement( namespace, language, statement ); } } scanner.close(); - } catch (Exception e) { + } catch ( Exception e ) { e.printStackTrace(); } finally { try { - if (con != null && !con.isClosed()) { + if ( con != null && !con.isClosed() ) { con.close(); } - } catch (Exception e) { + } catch ( Exception e ) { e.printStackTrace(); } } diff --git a/src/test/java/org/polypheny/jdbc/QueryTest.java b/src/test/java/org/polypheny/jdbc/QueryTest.java index 1d334bd..09a6e00 100644 --- a/src/test/java/org/polypheny/jdbc/QueryTest.java +++ b/src/test/java/org/polypheny/jdbc/QueryTest.java @@ -35,7 +35,6 @@ import org.polypheny.jdbc.multimodel.Result.ResultType; import org.polypheny.jdbc.types.PolyDocument; import org.polypheny.jdbc.types.PolyGraphElement; -import org.polypheny.jdbc.types.PolyNode; public class QueryTest { @@ -117,6 +116,7 @@ public void mqlDataRetrievalTest() { } } + @Test public void simpleCypherNodesTest() { try ( Connection connection = TestHelper.getConnection() ) { @@ -128,8 +128,8 @@ public void simpleCypherNodesTest() { assertEquals( ResultType.GRAPH, result.getResultType() ); GraphResult graphResult = result.unwrap( GraphResult.class ); for ( PolyGraphElement element : graphResult ) { - element.get("name"); - element.get("year_joined"); + element.get( "name" ); + element.get( "year_joined" ); element.getLabels(); element.getId(); } @@ -138,6 +138,7 @@ public void simpleCypherNodesTest() { } } + @Test public void simpleCypherPropertyTest() { try ( Connection connection = TestHelper.getConnection() ) { @@ -151,11 +152,12 @@ public void simpleCypherPropertyTest() { assertEquals( ResultType.RELATIONAL, result.getResultType() ); RelationalResult relationalResult = result.unwrap( RelationalResult.class ); for ( PolyRow row : relationalResult ) { - row.get(0); - row.get("c.name"); + row.get( 0 ); + row.get( "c.name" ); } } catch ( SQLException e ) { throw new RuntimeException( e ); } } + } diff --git a/src/test/java/org/polypheny/jdbc/types/TypedValueTest.java b/src/test/java/org/polypheny/jdbc/types/TypedValueTest.java index f37e9bd..4921a85 100644 --- a/src/test/java/org/polypheny/jdbc/types/TypedValueTest.java +++ b/src/test/java/org/polypheny/jdbc/types/TypedValueTest.java @@ -953,5 +953,108 @@ void getLengthTest() throws SQLException { TypedValue typedValue2 = new TypedValue( protoValue ); assertEquals( value.length(), typedValue2.getLength() ); } + + + @Test + void testFromBoolean() throws SQLException { + TypedValue value = TypedValue.fromBoolean( true ); + assertEquals( "1", value.asString() ); + } + + + @Test + void testFromByte() throws SQLException { + byte byteValue = 10; + TypedValue value = TypedValue.fromByte( byteValue ); + assertEquals( "10", value.asString() ); + } + + + @Test + void testFromShort() throws SQLException { + short shortValue = 20; + TypedValue value = TypedValue.fromShort( shortValue ); + assertEquals( "20", value.asString() ); + } + + + @Test + void testFromInteger() throws SQLException { + int intValue = 30; + TypedValue value = TypedValue.fromInteger( intValue ); + assertEquals( "30", value.asString() ); + } + + + @Test + void testFromLong() throws SQLException { + long longValue = 40L; + TypedValue value = TypedValue.fromLong( longValue ); + assertEquals( "40", value.asString() ); + } + + + @Test + void testFromFloat() throws SQLException { + float floatValue = 50.5f; + TypedValue value = TypedValue.fromFloat( floatValue ); + assertEquals( "50.5", value.asString() ); + } + + + @Test + void testFromDouble() throws SQLException { + double doubleValue = 60.6; + TypedValue value = TypedValue.fromDouble( doubleValue ); + assertEquals( "60.6", value.asString() ); + } + + + @Test + void testFromBigDecimal() throws SQLException { + BigDecimal bigDecimalValue = new BigDecimal( "70.7" ); + TypedValue value = TypedValue.fromBigDecimal( bigDecimalValue ); + assertEquals( "70.7", value.asString() ); + } + + + @Test + void testFromString() throws SQLException { + String stringValue = "test"; + TypedValue value = TypedValue.fromString( stringValue ); + assertEquals( "test", value.asString() ); + } + + + @Test + void testFromDate() throws SQLException { + Date dateValue = new Date( 2024, 7, 28 ); + TypedValue value = TypedValue.fromDate( dateValue ); + assertEquals( dateValue.toString(), value.asString() ); + } + + + @Test + void testFromTime() throws SQLException { + Time timeValue = new Time( System.currentTimeMillis() ); + TypedValue value = TypedValue.fromTime( timeValue ); + assertEquals( timeValue.toString(), value.asString() ); + } + + + @Test + void testFromTimestamp() throws SQLException { + Timestamp timestampValue = new Timestamp( System.currentTimeMillis() ); + TypedValue value = TypedValue.fromTimestamp( timestampValue ); + assertEquals( timestampValue.toString(), value.asString() ); + } + + + @Test + void testFromInterval() throws SQLException { + PolyInterval interval = new PolyInterval( 1, 0 ); + TypedValue value = TypedValue.fromInterval( interval ); + assertEquals( interval.toString(), value.asString() ); + } }