|
| 1 | +package com.linkedin.hoptimator.jdbc; |
| 2 | + |
| 3 | +import java.sql.Connection; |
| 4 | +import java.sql.SQLException; |
| 5 | +import java.util.Properties; |
| 6 | +import java.util.function.Supplier; |
| 7 | +import org.apache.calcite.avatica.AvaticaConnection; |
| 8 | +import org.apache.calcite.avatica.ConnectStringParser; |
| 9 | +import org.apache.calcite.jdbc.CalciteFactory; |
| 10 | +import org.apache.calcite.jdbc.CalcitePrepare; |
| 11 | +import org.apache.calcite.jdbc.CalciteSchema; |
| 12 | +import org.apache.calcite.jdbc.Driver; |
| 13 | +import org.checkerframework.checker.nullness.qual.Nullable; |
| 14 | + |
| 15 | +import static java.util.Objects.requireNonNull; |
| 16 | + |
| 17 | + |
| 18 | +// CalciteDriver is an extension of Driver that extends the connect() method to allow for disabling caching. |
| 19 | +// Caching enables snapshots via CachingCalciteSchema which has the side effect of preloading all tables for a driver |
| 20 | +public class CalciteDriver extends Driver { |
| 21 | + |
| 22 | + public CalciteDriver() { |
| 23 | + this(null); |
| 24 | + } |
| 25 | + |
| 26 | + protected CalciteDriver(@Nullable Supplier<CalcitePrepare> prepareFactory) { |
| 27 | + super(prepareFactory); |
| 28 | + } |
| 29 | + |
| 30 | + @Override |
| 31 | + public CalciteDriver withPrepareFactory(Supplier<CalcitePrepare> prepareFactory) { |
| 32 | + requireNonNull(prepareFactory, "prepareFactory"); |
| 33 | + if (this.prepareFactory == prepareFactory) { |
| 34 | + return this; |
| 35 | + } |
| 36 | + return new CalciteDriver(prepareFactory); |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public Connection connect(String url, Properties info) throws SQLException { |
| 41 | + return connect(url, info, false); |
| 42 | + } |
| 43 | + |
| 44 | + public Connection connect(String url, Properties info, boolean cache) throws SQLException { |
| 45 | + if (!this.acceptsURL(url)) { |
| 46 | + return null; |
| 47 | + } else { |
| 48 | + String prefix = this.getConnectStringPrefix(); |
| 49 | + |
| 50 | + assert url.startsWith(prefix); |
| 51 | + |
| 52 | + String urlSuffix = url.substring(prefix.length()); |
| 53 | + Properties info2 = ConnectStringParser.parse(urlSuffix, info); |
| 54 | + CalciteSchema rootSchema = CalciteSchema.createRootSchema(true, cache); |
| 55 | + AvaticaConnection connection = ((CalciteFactory) this.factory) |
| 56 | + .newConnection(this, this.factory, url, info2, rootSchema, null); |
| 57 | + this.handler.onConnectionInit(connection); |
| 58 | + return connection; |
| 59 | + } |
| 60 | + } |
| 61 | +} |
0 commit comments