22
22
import org .apache .calcite .schema .Schema ;
23
23
import org .apache .calcite .schema .SchemaPlus ;
24
24
import org .apache .calcite .schema .Schemas ;
25
+ import org .apache .calcite .schema .Table ;
25
26
import org .apache .calcite .schema .Wrapper ;
26
- import org .apache .calcite .schema .impl .AbstractSchema ;
27
+ import org .apache .calcite .schema .lookup .IgnoreCaseLookup ;
28
+ import org .apache .calcite .schema .lookup .LikePattern ;
29
+ import org .apache .calcite .schema .lookup .LoadingCacheLookup ;
30
+ import org .apache .calcite .schema .lookup .Lookup ;
27
31
import org .apache .calcite .sql .SqlDialect ;
28
32
import org .apache .calcite .sql .SqlDialectFactory ;
29
33
import org .apache .calcite .sql .SqlDialectFactoryImpl ;
30
34
import org .apache .calcite .util .BuiltInMethod ;
31
35
32
36
import com .google .common .base .Suppliers ;
33
- import com .google .common .collect .ImmutableMap ;
37
+ import com .google .common .collect .ImmutableSet ;
34
38
35
39
import org .checkerframework .checker .nullness .qual .Nullable ;
36
40
37
41
import java .sql .Connection ;
38
42
import java .sql .ResultSet ;
39
43
import java .sql .SQLException ;
40
- import java .util .Map ;
44
+ import java .util .Set ;
41
45
import java .util .function .Supplier ;
42
46
import javax .sql .DataSource ;
43
47
51
55
* an instance of {@link JdbcSchema}.
52
56
*
53
57
* <p>This schema is lazy: it does not compute the list of schema names until
54
- * the first call to {@link #getSubSchemaMap( )}. Then it creates a
55
- * {@link JdbcSchema} for each schema name. Each JdbcSchema will populate its
58
+ * the first call to {@link #subSchemas()} and {@link Lookup#get(String )}. Then it creates a
59
+ * {@link JdbcSchema} for this schema name. Each JdbcSchema will populate its
56
60
* tables on demand.
57
61
*/
58
- public class JdbcCatalogSchema extends AbstractSchema implements Wrapper {
62
+ public class JdbcCatalogSchema extends JdbcBaseSchema implements Wrapper {
59
63
final DataSource dataSource ;
60
64
public final SqlDialect dialect ;
61
65
final JdbcConvention convention ;
62
66
final String catalog ;
67
+ private final Lookup <JdbcSchema > subSchemas ;
63
68
64
- /** Sub-schemas by name, lazily initialized. */
69
+ /** default schema name, lazily initialized. */
65
70
@ SuppressWarnings ({"method.invocation.invalid" , "Convert2MethodRef" })
66
- final Supplier <SubSchemaMap > subSchemaMapSupplier =
67
- Suppliers .memoize (() -> computeSubSchemaMap ());
71
+ private final Supplier <String > defaultSchemaName =
72
+ Suppliers .memoize (() -> computeDefaultSchemaName ());
68
73
69
74
/** Creates a JdbcCatalogSchema. */
70
75
public JdbcCatalogSchema (DataSource dataSource , SqlDialect dialect ,
@@ -73,6 +78,40 @@ public JdbcCatalogSchema(DataSource dataSource, SqlDialect dialect,
73
78
this .dialect = requireNonNull (dialect , "dialect" );
74
79
this .convention = requireNonNull (convention , "convention" );
75
80
this .catalog = catalog ;
81
+ this .subSchemas = new LoadingCacheLookup <>(new IgnoreCaseLookup <JdbcSchema >() {
82
+ @ Override public @ Nullable JdbcSchema get (String name ) {
83
+ try (Connection connection = dataSource .getConnection ();
84
+ ResultSet resultSet =
85
+ connection .getMetaData ().getSchemas (catalog , name )) {
86
+ while (resultSet .next ()) {
87
+ final String schemaName =
88
+ requireNonNull (resultSet .getString (1 ),
89
+ "got null schemaName from the database" );
90
+ return new JdbcSchema (dataSource , dialect , convention , catalog , schemaName );
91
+ }
92
+ } catch (SQLException e ) {
93
+ throw new RuntimeException (e );
94
+ }
95
+ return null ;
96
+ }
97
+
98
+ @ Override public Set <String > getNames (LikePattern pattern ) {
99
+ final ImmutableSet .Builder <String > builder =
100
+ ImmutableSet .builder ();
101
+ try (Connection connection = dataSource .getConnection ();
102
+ ResultSet resultSet =
103
+ connection .getMetaData ().getSchemas (catalog , pattern .pattern )) {
104
+ while (resultSet .next ()) {
105
+ builder .add (
106
+ requireNonNull (resultSet .getString (1 ),
107
+ "got null schemaName from the database" ));
108
+ }
109
+ } catch (SQLException e ) {
110
+ throw new RuntimeException (e );
111
+ }
112
+ return builder .build ();
113
+ }
114
+ });
76
115
}
77
116
78
117
public static JdbcCatalogSchema create (
@@ -103,34 +142,25 @@ public static JdbcCatalogSchema create(
103
142
return new JdbcCatalogSchema (dataSource , dialect , convention , catalog );
104
143
}
105
144
106
- private SubSchemaMap computeSubSchemaMap () {
107
- final ImmutableMap .Builder <String , Schema > builder =
108
- ImmutableMap .builder ();
109
- @ Nullable String defaultSchemaName ;
110
- try (Connection connection = dataSource .getConnection ();
111
- ResultSet resultSet =
112
- connection .getMetaData ().getSchemas (catalog , null )) {
113
- defaultSchemaName = connection .getSchema ();
114
- while (resultSet .next ()) {
115
- final String schemaName =
116
- requireNonNull (resultSet .getString (1 ),
117
- "got null schemaName from the database" );
118
- builder .put (schemaName ,
119
- new JdbcSchema (dataSource , dialect , convention , catalog , schemaName ));
120
- }
145
+ @ Override public Lookup <Table > tables () {
146
+ return Lookup .empty ();
147
+ }
148
+
149
+ @ Override public Lookup <? extends Schema > subSchemas () {
150
+ return subSchemas ;
151
+ }
152
+
153
+ private String computeDefaultSchemaName () {
154
+ try (Connection connection = dataSource .getConnection ()) {
155
+ return connection .getSchema ();
121
156
} catch (SQLException e ) {
122
157
throw new RuntimeException (e );
123
158
}
124
- return new SubSchemaMap (defaultSchemaName , builder .build ());
125
- }
126
-
127
- @ Override protected Map <String , Schema > getSubSchemaMap () {
128
- return subSchemaMapSupplier .get ().map ;
129
159
}
130
160
131
161
/** Returns the name of the default sub-schema. */
132
162
public @ Nullable String getDefaultSubSchemaName () {
133
- return subSchemaMapSupplier .get (). defaultSchemaName ;
163
+ return defaultSchemaName .get ();
134
164
}
135
165
136
166
/** Returns the data source. */
@@ -148,16 +178,4 @@ public DataSource getDataSource() {
148
178
}
149
179
return null ;
150
180
}
151
-
152
- /** Contains sub-schemas by name, and the name of the default schema. */
153
- private static class SubSchemaMap {
154
- final @ Nullable String defaultSchemaName ;
155
- final ImmutableMap <String , Schema > map ;
156
-
157
- private SubSchemaMap (@ Nullable String defaultSchemaName ,
158
- ImmutableMap <String , Schema > map ) {
159
- this .defaultSchemaName = defaultSchemaName ;
160
- this .map = map ;
161
- }
162
- }
163
181
}
0 commit comments