21
21
package com .apple .foundationdb .relational .api ;
22
22
23
23
import com .apple .foundationdb .annotation .API ;
24
-
25
24
import com .apple .foundationdb .relational .api .exceptions .ErrorCode ;
26
25
import com .apple .foundationdb .relational .api .exceptions .RelationalException ;
27
-
26
+ import com . apple . foundationdb . relational . api . metadata . DataType ;
28
27
import com .google .common .base .Suppliers ;
29
28
30
29
import javax .annotation .Nonnull ;
30
+ import java .sql .DatabaseMetaData ;
31
31
import java .sql .SQLException ;
32
- import java .sql .Types ;
33
32
import java .util .Objects ;
34
33
import java .util .function .Supplier ;
35
34
39
38
@ API (API .Status .EXPERIMENTAL )
40
39
public final class RelationalArrayMetaData implements ArrayMetaData {
41
40
42
- private final FieldDescription element ;
41
+ private final DataType . ArrayType type ;
43
42
44
43
private final Supplier <Integer > hashCodeSupplier ;
45
44
46
- private RelationalArrayMetaData (@ Nonnull FieldDescription element ) {
47
- this .element = element ;
45
+ private RelationalArrayMetaData (@ Nonnull DataType . ArrayType type ) {
46
+ this .type = type ;
48
47
this .hashCodeSupplier = Suppliers .memoize (this ::calculateHashCode );
49
48
}
50
49
51
- public static RelationalArrayMetaData ofPrimitive (int sqlType , int nullable ) {
52
- return new RelationalArrayMetaData (FieldDescription .primitive ("VALUE" , sqlType , nullable ));
53
- }
54
-
55
- public static RelationalArrayMetaData ofStruct (@ Nonnull StructMetaData metaData , int nullable ) {
56
- return new RelationalArrayMetaData (FieldDescription .struct ("VALUE" , nullable , metaData ));
50
+ @ Nonnull
51
+ public static RelationalArrayMetaData of (@ Nonnull DataType .ArrayType type ) {
52
+ return new RelationalArrayMetaData (type );
57
53
}
58
54
59
55
@ Override
60
- public int isElementNullable () throws SQLException {
61
- return element .isNullable ();
56
+ public int isElementNullable () {
57
+ if (type .getElementType ().isNullable ()) {
58
+ return DatabaseMetaData .columnNullable ;
59
+ } else {
60
+ return DatabaseMetaData .columnNoNulls ;
61
+ }
62
62
}
63
63
64
64
@ Override
65
65
public String getElementName () throws SQLException {
66
- return element . getName () ;
66
+ return "VALUE" ;
67
67
}
68
68
69
69
@ Override
70
70
public int getElementType () throws SQLException {
71
- return element . getSqlTypeCode ();
71
+ return type . getElementType (). getJdbcSqlCode ();
72
72
}
73
73
74
74
@ Override
75
- public String getElementTypeName () throws SQLException {
76
- return SqlTypeNamesSupport .getSqlTypeName (element . getSqlTypeCode ());
75
+ public String getElementTypeName () {
76
+ return SqlTypeNamesSupport .getSqlTypeName (type . getElementType (). getJdbcSqlCode ());
77
77
}
78
78
79
79
/**
@@ -86,10 +86,10 @@ public String getElementTypeName() throws SQLException {
86
86
*/
87
87
@ Override
88
88
public StructMetaData getElementStructMetaData () throws SQLException {
89
- if (element . getSqlTypeCode () != Types .STRUCT ) {
89
+ if (type . getElementType (). getCode () != DataType . Code .STRUCT ) {
90
90
throw new RelationalException ("Element is not of STRUCT type" , ErrorCode .CANNOT_CONVERT_TYPE ).toSqlException ();
91
91
}
92
- return element . getFieldMetaData ( );
92
+ return RelationalStructMetaData . of (( DataType . StructType ) type . getElementType () );
93
93
}
94
94
95
95
/**
@@ -102,15 +102,21 @@ public StructMetaData getElementStructMetaData() throws SQLException {
102
102
*/
103
103
@ Override
104
104
public ArrayMetaData getElementArrayMetaData () throws SQLException {
105
- if (element . getSqlTypeCode () != Types .ARRAY ) {
105
+ if (type . getElementType (). getCode () != DataType . Code .ARRAY ) {
106
106
throw new RelationalException ("Element is not of ARRAY type" , ErrorCode .CANNOT_CONVERT_TYPE ).toSqlException ();
107
107
}
108
- return element .getArrayMetaData ();
108
+ return RelationalArrayMetaData .of ((DataType .ArrayType ) type .getElementType ());
109
+ }
110
+
111
+ @ Nonnull
112
+ @ Override
113
+ public DataType .ArrayType getRelationalDataType () throws SQLException {
114
+ return type ;
109
115
}
110
116
111
117
@ Nonnull
112
- public FieldDescription getElementField () {
113
- return element ;
118
+ public DataType getElementDataType () {
119
+ return type . getElementType () ;
114
120
}
115
121
116
122
@ Override
@@ -132,7 +138,7 @@ public boolean equals(Object other) {
132
138
if (otherMetadata == this ) {
133
139
return true ;
134
140
}
135
- return element .equals (otherMetadata .element );
141
+ return type .equals (otherMetadata .type );
136
142
}
137
143
138
144
@ Override
@@ -141,7 +147,7 @@ public int hashCode() {
141
147
}
142
148
143
149
private int calculateHashCode () {
144
- return Objects .hash (element );
150
+ return Objects .hash (type );
145
151
}
146
152
147
153
}
0 commit comments