|
1 | 1 | package tech.ydb.jdbc.context; |
2 | 2 |
|
| 3 | +import java.nio.charset.StandardCharsets; |
3 | 4 | import java.sql.SQLDataException; |
4 | 5 | import java.sql.SQLException; |
5 | 6 | import java.time.Duration; |
|
10 | 11 | import java.util.List; |
11 | 12 | import java.util.Map; |
12 | 13 | import java.util.concurrent.Executors; |
| 14 | +import java.util.concurrent.TimeUnit; |
13 | 15 | import java.util.concurrent.atomic.AtomicInteger; |
14 | 16 | import java.util.logging.Level; |
15 | 17 | import java.util.logging.Logger; |
16 | 18 |
|
| 19 | +import com.google.common.base.Supplier; |
| 20 | +import com.google.common.base.Suppliers; |
17 | 21 | import com.google.common.cache.Cache; |
18 | 22 | import com.google.common.cache.CacheBuilder; |
19 | 23 |
|
|
50 | 54 | import tech.ydb.table.description.TableDescription; |
51 | 55 | import tech.ydb.table.impl.PooledTableClient; |
52 | 56 | import tech.ydb.table.query.DataQuery; |
| 57 | +import tech.ydb.table.query.DataQueryResult; |
53 | 58 | import tech.ydb.table.query.ExplainDataQueryResult; |
| 59 | +import tech.ydb.table.result.ResultSetReader; |
54 | 60 | import tech.ydb.table.rpc.grpc.GrpcTableRpc; |
55 | 61 | import tech.ydb.table.settings.DescribeTableSettings; |
56 | 62 | import tech.ydb.table.settings.ExplainDataQuerySettings; |
57 | 63 | import tech.ydb.table.settings.PrepareDataQuerySettings; |
58 | 64 | import tech.ydb.table.settings.RequestSettings; |
| 65 | +import tech.ydb.table.transaction.TxControl; |
59 | 66 | import tech.ydb.table.values.Type; |
60 | 67 |
|
61 | 68 | /** |
@@ -87,6 +94,7 @@ public class YdbContext implements AutoCloseable { |
87 | 94 | private final Cache<String, QueryStat> statsCache; |
88 | 95 | private final Cache<String, Map<String, Type>> queryParamsCache; |
89 | 96 | private final Cache<String, TableDescription> tableDescribeCache; |
| 97 | + private final Supplier<String> version = Suppliers.memoizeWithExpiration(this::readVersion, 1, TimeUnit.HOURS); |
90 | 98 |
|
91 | 99 | private final boolean autoResizeSessionPool; |
92 | 100 | private final AtomicInteger connectionsCount = new AtomicInteger(); |
@@ -144,15 +152,14 @@ public YdbTypes getTypes() { |
144 | 152 | return types; |
145 | 153 | } |
146 | 154 |
|
147 | | - /** |
148 | | - * Grpc Transport for other API YDB server clients |
149 | | - * |
150 | | - * @return grpcTransport for YDB |
151 | | - */ |
152 | 155 | public GrpcTransport getGrpcTransport() { |
153 | 156 | return grpcTransport; |
154 | 157 | } |
155 | 158 |
|
| 159 | + public String getDatabaseVersion() { |
| 160 | + return version.get(); |
| 161 | + } |
| 162 | + |
156 | 163 | public YdbTracer getTracer() { |
157 | 164 | return config.isTxTracedEnabled() ? YdbTracer.current() : YdbTracerNone.DISABLED; |
158 | 165 | } |
@@ -338,6 +345,21 @@ public static YdbContext createContext(YdbConfig config) throws SQLException { |
338 | 345 | } |
339 | 346 | } |
340 | 347 |
|
| 348 | + private String readVersion() { |
| 349 | + Result<DataQueryResult> res = retryCtx.supplyResult( |
| 350 | + s -> s.executeDataQuery("SELECT version();", TxControl.snapshotRo()) |
| 351 | + ).join(); |
| 352 | + |
| 353 | + if (res.isSuccess()) { |
| 354 | + ResultSetReader rs = res.getValue().getResultSet(0); |
| 355 | + if (rs.next()) { |
| 356 | + return rs.getColumn(0).getBytesAsString(StandardCharsets.UTF_8); |
| 357 | + } |
| 358 | + } |
| 359 | + return "unknown"; |
| 360 | + } |
| 361 | + |
| 362 | + |
341 | 363 | public <T extends RequestSettings<?>> T withDefaultTimeout(T settings) { |
342 | 364 | Duration operation = operationOptions.getDeadlineTimeout(); |
343 | 365 | if (!operation.isZero() && !operation.isNegative()) { |
|
0 commit comments