2020
2121package com .apple .foundationdb .relational .jdbc ;
2222
23- import com .apple .foundationdb .relational .api .ArrayMetaData ;
2423import com .apple .foundationdb .relational .api .RelationalArray ;
2524import com .apple .foundationdb .relational .api .RelationalStruct ;
2625import com .apple .foundationdb .relational .api .RelationalStructBuilder ;
2726import com .apple .foundationdb .relational .api .RelationalStructMetaData ;
2827import com .apple .foundationdb .relational .api .StructMetaData ;
2928import com .apple .foundationdb .relational .api .exceptions .ErrorCode ;
30- import com .apple .foundationdb .relational .api .exceptions .RelationalException ;
3129import com .apple .foundationdb .relational .api .metadata .DataType ;
3230import com .apple .foundationdb .relational .jdbc .grpc .v1 .column .Column ;
3331import com .apple .foundationdb .relational .jdbc .grpc .v1 .column .ColumnMetadata ;
34- import com .apple .foundationdb .relational .jdbc .grpc .v1 .column .EnumMetadata ;
3532import com .apple .foundationdb .relational .jdbc .grpc .v1 .column .ListColumn ;
3633import com .apple .foundationdb .relational .jdbc .grpc .v1 .column .ListColumnMetadata ;
3734import com .apple .foundationdb .relational .jdbc .grpc .v1 .column .Struct ;
5451import java .util .UUID ;
5552import java .util .function .Supplier ;
5653
54+ import static com .apple .foundationdb .relational .jdbc .TypeConversion .getStructDataType ;
55+
5756/**
5857 * Facade over grpc protobuf objects that offers a {@link RelationalStruct} view.
5958 * Used by jdbc client but also serializable (protobuf) so can be passed over
@@ -69,7 +68,6 @@ class RelationalStructFacade implements RelationalStruct {
6968 /**
7069 * A StructMetaData facade over {@link #delegateMetadata}.
7170 */
72- private final StructMetaData structMetaData ;
7371 private final Supplier <DataType .StructType > type ;
7472
7573 /**
@@ -87,10 +85,9 @@ class RelationalStructFacade implements RelationalStruct {
8785 private boolean wasNull ;
8886
8987 RelationalStructFacade (ListColumnMetadata delegateMetadata , Struct delegate ) {
90- type = Suppliers .memoize (() -> TypeConversion . getStructDataType (delegateMetadata .getColumnMetadataList (), false ));
88+ type = Suppliers .memoize (() -> getStructDataType (delegateMetadata .getColumnMetadataList (), false ));
9189 this .delegate = delegate ;
9290 this .delegateMetadata = delegateMetadata ;
93- this .structMetaData = new RelationalStructFacadeMetaData (delegateMetadata );
9491 this .wasNull = false ;
9592 }
9693
@@ -115,7 +112,7 @@ ListColumnMetadata getDelegateMetadata() {
115112 */
116113 @ Override
117114 public StructMetaData getMetaData () {
118- return type . get () != null ? RelationalStructMetaData .of (type .get ()) : structMetaData ;
115+ return RelationalStructMetaData .of (type .get ());
119116 }
120117
121118 @ Override
@@ -430,7 +427,7 @@ int getZeroBasedOffsetOrThrow(String fieldName) throws SQLException {
430427 }
431428
432429 @ Override
433- public RelationalStructBuilder addBoolean (String fieldName , boolean b ) throws SQLException {
430+ public RelationalStructBuilder addBoolean (String fieldName , boolean b ) {
434431 // Add the metadata and get offset at where to insert data.
435432 int offset = addMetadata (ColumnMetadata .newBuilder ()
436433 .setName (fieldName ).setJavaSqlTypesCode (Types .BOOLEAN ).setType (Type .BOOLEAN ).build ());
@@ -440,7 +437,7 @@ public RelationalStructBuilder addBoolean(String fieldName, boolean b) throws SQ
440437 }
441438
442439 @ Override
443- public RelationalStructBuilder addLong (String fieldName , long l ) throws SQLException {
440+ public RelationalStructBuilder addLong (String fieldName , long l ) {
444441 int offset = addMetadata (ColumnMetadata .newBuilder ()
445442 .setName (fieldName ).setJavaSqlTypesCode (Types .BIGINT ).setType (Type .LONG ).build ());
446443 this .listColumnBuilder .addColumn (offset , Column .newBuilder ().setLong (l ).build ());
@@ -453,15 +450,15 @@ public RelationalStructBuilder addFloat(String fieldName, float f) throws SQLExc
453450 }
454451
455452 @ Override
456- public RelationalStructBuilder addDouble (String fieldName , double d ) throws SQLException {
453+ public RelationalStructBuilder addDouble (String fieldName , double d ) {
457454 int offset = addMetadata (ColumnMetadata .newBuilder ()
458455 .setName (fieldName ).setJavaSqlTypesCode (Types .DOUBLE ).setType (Type .DOUBLE ).build ());
459456 this .listColumnBuilder .addColumn (offset , Column .newBuilder ().setDouble (d ).build ());
460457 return this ;
461458 }
462459
463460 @ Override
464- public RelationalStructBuilder addBytes (String fieldName , byte [] bytes ) throws SQLException {
461+ public RelationalStructBuilder addBytes (String fieldName , byte [] bytes ) {
465462 int offset = addMetadata (ColumnMetadata .newBuilder ()
466463 .setName (fieldName ).setJavaSqlTypesCode (Types .BINARY ).setType (Type .BYTES ).build ());
467464 this .listColumnBuilder .addColumn (offset , Column .newBuilder ().setBinary (ByteString .copyFrom (bytes )).build ());
@@ -470,7 +467,7 @@ public RelationalStructBuilder addBytes(String fieldName, byte[] bytes) throws S
470467
471468 @ Override
472469 @ SpotBugsSuppressWarnings ("NP" )
473- public RelationalStructBuilder addString (String fieldName , @ Nullable String s ) throws SQLException {
470+ public RelationalStructBuilder addString (String fieldName , @ Nullable String s ) {
474471 int offset = addMetadata (ColumnMetadata .newBuilder ()
475472 .setName (fieldName ).setJavaSqlTypesCode (Types .VARCHAR ).setType (Type .STRING ).build ());
476473 // TODO: setString requires a non-null string, but this method takes a nullable string
@@ -503,7 +500,7 @@ public RelationalStructBuilder addStruct(String fieldName, @Nonnull RelationalSt
503500 // Insert the data portion of RelationalStruct here.
504501 RelationalStructFacade relationalStructFacade = struct .unwrap (RelationalStructFacade .class );
505502 int offset = addMetadata (ColumnMetadata .newBuilder ().setName (fieldName )
506- .setJavaSqlTypesCode (Types .STRUCT ).setType (Type .STRUCT ).setStructMetadata (relationalStructFacade .delegateMetadata ).build ());
503+ .setJavaSqlTypesCode (Types .STRUCT ).setType (Type .STRUCT ).setStructMetadata (relationalStructFacade .getDelegateMetadata () ).build ());
507504 this .listColumnBuilder
508505 .addColumn (offset , Column .newBuilder ().setStruct (relationalStructFacade .delegate ).build ());
509506 return this ;
@@ -540,152 +537,4 @@ public RelationalStruct build() {
540537 return new RelationalStructFacade (columnListMetadataBuilder .build (), struct );
541538 }
542539 }
543-
544- /**
545- * Facade over protobuf column metadata to present a StructMetaData view.
546- */
547- static final class RelationalStructFacadeMetaData implements StructMetaData {
548- private final ListColumnMetadata metadata ;
549- private final Supplier <DataType .StructType > type ;
550-
551- RelationalStructFacadeMetaData (ListColumnMetadata metadata ) {
552- this .metadata = metadata ;
553- this .type = Suppliers .memoize (this ::computeType );
554- }
555-
556- private DataType .StructType computeType () {
557- return getStructDataType (metadata .getColumnMetadataList (), false );
558- }
559-
560- private static DataType .EnumType getEnumDataType (@ Nonnull EnumMetadata enumMetadata , boolean nullable ) {
561- final var enumValues = new ArrayList <DataType .EnumType .EnumValue >();
562- int i = 1 ;
563- for (var value : enumMetadata .getValuesList ()) {
564- enumValues .add (DataType .EnumType .EnumValue .of (value , i ++));
565- }
566- return DataType .EnumType .from (enumMetadata .getName (), enumValues , nullable );
567- }
568-
569- private static DataType .StructType getStructDataType (@ Nonnull List <ColumnMetadata > columnMetadataList , boolean nullable ) {
570- final var fields = new ArrayList <DataType .StructType .Field >();
571- for (int i = 0 ; i < columnMetadataList .size (); i ++) {
572- final var columnMetadata = columnMetadataList .get (i );
573- fields .add (DataType .StructType .Field .from (columnMetadata .getName (), getDataType (columnMetadata .getType (), columnMetadata , columnMetadata .getNullable ()), i ));
574- }
575- return DataType .StructType .from ("ANONYMOUS_STRUCT" , fields , nullable );
576- }
577-
578- static DataType getDataType (@ Nonnull Type type , @ Nonnull ColumnMetadata columnMetadata , boolean nullable ) {
579- switch (type ) {
580- case LONG :
581- return nullable ? DataType .Primitives .NULLABLE_LONG .type () : DataType .Primitives .LONG .type ();
582- case INTEGER :
583- return nullable ? DataType .Primitives .NULLABLE_INTEGER .type () : DataType .Primitives .INTEGER .type ();
584- case DOUBLE :
585- return nullable ? DataType .Primitives .NULLABLE_DOUBLE .type () : DataType .Primitives .DOUBLE .type ();
586- case FLOAT :
587- return nullable ? DataType .Primitives .NULLABLE_FLOAT .type () : DataType .Primitives .FLOAT .type ();
588- case BOOLEAN :
589- return nullable ? DataType .Primitives .NULLABLE_BOOLEAN .type () : DataType .Primitives .BOOLEAN .type ();
590- case BYTES :
591- return nullable ? DataType .Primitives .NULLABLE_BYTES .type () : DataType .Primitives .BYTES .type ();
592- case UUID :
593- return nullable ? DataType .Primitives .NULLABLE_UUID .type () : DataType .Primitives .UUID .type ();
594- case STRING :
595- return nullable ? DataType .Primitives .NULLABLE_STRING .type () : DataType .Primitives .STRING .type ();
596- case VERSION :
597- return nullable ? DataType .Primitives .NULLABLE_VERSION .type () : DataType .Primitives .VERSION .type ();
598- case STRUCT :
599- return getStructDataType (columnMetadata .getStructMetadata ().getColumnMetadataList (), nullable );
600- case ENUM :
601- return getEnumDataType (columnMetadata .getEnumMetadata (), nullable );
602- case ARRAY :
603- final var arrayMetadata = columnMetadata .getArrayMetadata ();
604- return DataType .ArrayType .from (getDataType (arrayMetadata .getType (), arrayMetadata , arrayMetadata .getNullable ()), nullable );
605- default :
606- throw new RelationalException ("Not implemeneted: " + type .name (), ErrorCode .INTERNAL_ERROR ).toUncheckedWrappedException ();
607- }
608- }
609-
610- @ Override
611- public int getColumnCount () throws SQLException {
612- return metadata .getColumnMetadataCount ();
613- }
614-
615- @ Override
616- public int isNullable (int oneBasedColumn ) throws SQLException {
617- throw new SQLException ("Not implemented" , ErrorCode .UNSUPPORTED_OPERATION .getErrorCode ());
618- }
619-
620- @ Override
621- public String getTypeName () throws SQLException {
622- throw new SQLException ("Not implemented" , ErrorCode .UNSUPPORTED_OPERATION .getErrorCode ());
623- }
624-
625- @ Override
626- public String getColumnLabel (int oneBasedColumn ) throws SQLException {
627- throw new SQLException ("Not implemented" , ErrorCode .UNSUPPORTED_OPERATION .getErrorCode ());
628- }
629-
630- @ Override
631- public String getColumnName (int oneBasedColumn ) throws SQLException {
632- return metadata .getColumnMetadata (PositionalIndex .toProtobuf (oneBasedColumn )).getName ();
633- }
634-
635- @ Override
636- public String getSchemaName (int oneBasedColumn ) throws SQLException {
637- throw new SQLException ("Not implemented" , ErrorCode .UNSUPPORTED_OPERATION .getErrorCode ());
638- }
639-
640- @ Override
641- public String getTableName (int oneBasedColumn ) throws SQLException {
642- throw new SQLException ("Not implemented" , ErrorCode .UNSUPPORTED_OPERATION .getErrorCode ());
643- }
644-
645- @ Override
646- public String getCatalogName (int oneBasedColumn ) throws SQLException {
647- throw new SQLException ("Not implemented" , ErrorCode .UNSUPPORTED_OPERATION .getErrorCode ());
648- }
649-
650- @ Override
651- public int getColumnType (int oneBasedColumn ) throws SQLException {
652- return metadata .getColumnMetadata (PositionalIndex .toProtobuf (oneBasedColumn )).getJavaSqlTypesCode ();
653- }
654-
655- @ Override
656- public String getColumnTypeName (int oneBasedColumn ) throws SQLException {
657- throw new SQLException ("Not implemented" , ErrorCode .UNSUPPORTED_OPERATION .getErrorCode ());
658- }
659-
660- @ Override
661- public StructMetaData getStructMetaData (int oneBasedColumn ) throws SQLException {
662- throw new SQLException ("Not implemented" , ErrorCode .UNSUPPORTED_OPERATION .getErrorCode ());
663- }
664-
665- @ Override
666- public ArrayMetaData getArrayMetaData (int oneBasedColumn ) throws SQLException {
667- throw new SQLException ("Not implemented" , ErrorCode .UNSUPPORTED_OPERATION .getErrorCode ());
668- }
669-
670- @ Override
671- public int getLeadingPhantomColumnCount () {
672- return -1000 ;
673- }
674-
675- @ Nonnull
676- @ Override
677- public DataType .StructType getRelationalDataType () {
678- return type .get ();
679- }
680-
681- @ Override
682- public <T > T unwrap (Class <T > iface ) throws SQLException {
683- throw new SQLException ("Not implemented" , ErrorCode .UNSUPPORTED_OPERATION .getErrorCode ());
684- }
685-
686- @ Override
687- public boolean isWrapperFor (Class <?> iface ) throws SQLException {
688- throw new SQLException ("Not implemented" , ErrorCode .UNSUPPORTED_OPERATION .getErrorCode ());
689- }
690- }
691540}
0 commit comments