Skip to content

Commit

Permalink
Support getting server version in JDBC
Browse files Browse the repository at this point in the history
  • Loading branch information
nezihyigitbasi authored and electrum committed Sep 17, 2015
1 parent efb345a commit 8d1589e
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 4 deletions.
1 change: 1 addition & 0 deletions presto-docs/src/main/sphinx/release/release-0.119.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ General Changes

* Add :func:`geometric_mean` function.
* Fix restoring interrupt status in ``StatementClient``.
* Support getting server version in JDBC driver.
* Execute views with the permissions of the view owner.
* Add owner to view metadata.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package com.facebook.presto.jdbc;

import com.facebook.presto.client.ClientSession;
import com.facebook.presto.client.ServerInfo;
import com.facebook.presto.client.StatementClient;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableMap;
Expand Down Expand Up @@ -567,6 +568,11 @@ String getUser()
return user;
}

ServerInfo getServerInfo()
{
return queryExecutor.getServerInfo(createHttpUri(address));
}

StatementClient startQuery(String sql)
{
URI uri = createHttpUri(address);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,12 @@ public String getDatabaseProductName()
public String getDatabaseProductVersion()
throws SQLException
{
// TODO: get version from server
return "UNKNOWN";
try {
return connection.getServerInfo().getNodeVersion().getVersion();
}
catch (RuntimeException e) {
throw new SQLException("Error fetching version from server", e);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@

import com.facebook.presto.client.ClientSession;
import com.facebook.presto.client.QueryResults;
import com.facebook.presto.client.ServerInfo;
import com.facebook.presto.client.StatementClient;
import com.google.common.collect.ImmutableSet;
import com.google.common.net.HostAndPort;
import io.airlift.http.client.HttpClient;
import io.airlift.http.client.HttpClientConfig;
import io.airlift.http.client.Request;
import io.airlift.http.client.jetty.JettyHttpClient;
import io.airlift.http.client.jetty.JettyIoPool;
import io.airlift.http.client.jetty.JettyIoPoolConfig;
Expand All @@ -35,21 +37,27 @@
import java.net.URI;
import java.util.concurrent.TimeUnit;

import static io.airlift.http.client.HttpUriBuilder.uriBuilderFrom;
import static io.airlift.http.client.JsonResponseHandler.createJsonResponseHandler;
import static io.airlift.http.client.Request.Builder.prepareGet;
import static io.airlift.json.JsonCodec.jsonCodec;
import static java.util.Objects.requireNonNull;

class QueryExecutor
implements Closeable
{
private final JsonCodec<QueryResults> queryInfoCodec;
private final JsonCodec<ServerInfo> serverInfoCodec;
private final HttpClient httpClient;

private QueryExecutor(String userAgent, JsonCodec<QueryResults> queryResultsCodec, HostAndPort socksProxy)
private QueryExecutor(String userAgent, JsonCodec<QueryResults> queryResultsCodec, JsonCodec<ServerInfo> serverInfoCodec, HostAndPort socksProxy)
{
requireNonNull(userAgent, "userAgent is null");
requireNonNull(queryResultsCodec, "queryResultsCodec is null");
requireNonNull(serverInfoCodec, "serverInfoCodec is null");

this.queryInfoCodec = queryResultsCodec;
this.serverInfoCodec = serverInfoCodec;
this.httpClient = new JettyHttpClient(
new HttpClientConfig()
.setConnectTimeout(new Duration(10, TimeUnit.SECONDS))
Expand All @@ -69,6 +77,13 @@ public void close()
httpClient.close();
}

public ServerInfo getServerInfo(URI server)
{
URI uri = uriBuilderFrom(server).replacePath("/v1/info").build();
Request request = prepareGet().setUri(uri).build();
return httpClient.execute(request, createJsonResponseHandler(serverInfoCodec));
}

// TODO: replace this with a phantom reference
@SuppressWarnings("FinalizeDeclaration")
@Override
Expand All @@ -79,7 +94,7 @@ protected void finalize()

static QueryExecutor create(String userAgent)
{
return new QueryExecutor(userAgent, jsonCodec(QueryResults.class), getSystemSocksProxy());
return new QueryExecutor(userAgent, jsonCodec(QueryResults.class), jsonCodec(ServerInfo.class), getSystemSocksProxy());
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,15 @@ public void testGetCatalogs()
}
}

@Test
public void testGetDatabaseProductVersion()
throws Exception
{
try (Connection connection = createConnection()) {
assertNotNull(connection.getMetaData().getDatabaseProductVersion());
}
}

@Test
public void testGetSchemas()
throws Exception
Expand Down

0 comments on commit 8d1589e

Please sign in to comment.