Skip to content

Commit

Permalink
dbeaver/dbeaver#23361 Metadata, server version read
Browse files Browse the repository at this point in the history
  • Loading branch information
serge-rider committed Oct 28, 2024
1 parent 5e81979 commit 92a220d
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@

import java.io.IOException;
import java.net.URL;
import java.sql.*;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Map;

public class LSqlConnection extends AbstractJdbcConnection {
Expand Down Expand Up @@ -98,8 +101,7 @@ public boolean isClosed() {

@Override
public DatabaseMetaData getMetaData() throws SQLException {
//return new LSqlDatabaseMetaData(this, metaData);
throw new SQLFeatureNotSupportedException();
return new LSqlDatabaseMetaData(this);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,56 @@
*/
package com.dbeaver.jdbc.driver.libsql;

import com.dbeaver.jdbc.driver.libsql.client.LSqlExecutionResult;
import com.dbeaver.jdbc.model.AbstractJdbcDatabaseMetaData;
import org.jkiss.code.NotNull;
import org.jkiss.utils.IOUtils;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class LSqlDatabaseMetaData extends AbstractJdbcDatabaseMetaData<LSqlConnection> {

private final LSqlExecutionResult metaData;
private static Pattern VERSION_PATTERN = Pattern.compile("(\\w+)\\s+([0-9.]+)\\s+(.+)");
private String serverVersion;

public LSqlDatabaseMetaData(@NotNull LSqlConnection connection, @NotNull LSqlExecutionResult metaData) {
public LSqlDatabaseMetaData(@NotNull LSqlConnection connection) {
super(connection, connection.getUrl());
this.metaData = metaData;
}

private void readServerVersion() throws SQLException {
if (serverVersion != null) {
return;
}
try {
HttpURLConnection con = connection.getClient().openConnection("version");
try (InputStream is = con.getInputStream()) {
serverVersion = IOUtils.readLine(is);
}
} catch (IOException e) {
throw new SQLException(e);
}
}

@Override
public String getDatabaseProductName() throws SQLException {
throw new SQLFeatureNotSupportedException();
readServerVersion();
return serverVersion;
}

@Override
public String getDatabaseProductVersion() throws SQLException {
throw new SQLFeatureNotSupportedException();
readServerVersion();
Matcher matcher = VERSION_PATTERN.matcher(serverVersion);
if (matcher.matches()) {
return matcher.group(2);
}
return serverVersion;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,27 @@ public LSqlExecutionResult[] batch(String[] stmts) throws SQLException {

}

private HttpURLConnection openConnection() throws IOException {
public HttpURLConnection openConnection() throws IOException {
HttpURLConnection conn = (HttpURLConnection) this.url.openConnection();
setAuthParameters(conn);
return conn;
}

public HttpURLConnection openConnection(String endpoint) throws IOException {
String baseURL = url.toString();
if (!baseURL.endsWith("/")) {
baseURL += "/";
}
baseURL += endpoint;
HttpURLConnection conn = (HttpURLConnection) new URL(baseURL).openConnection();
setAuthParameters(conn);
return conn;
}

private void setAuthParameters(HttpURLConnection conn) {
if (authToken != null) {
conn.setRequestProperty("Authorization", "Bearer " + authToken);
}
return conn;
}

private void query(String[] stmts, OutputStream os) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,25 @@ public static void main(String[] args) throws Exception {
LSqlDriver driver = new LSqlDriver();
Properties props = new Properties();
try (Connection connection = driver.connect("jdbc:dbeaver:libsql:" + args[0], props)) {
DatabaseMetaData metaData = connection.getMetaData();
System.out.println("Driver: " + metaData.getDriverName());
System.out.println("Database: " + metaData.getDatabaseProductName() + " " + metaData.getDatabaseProductVersion());

System.out.println("Query:");
try (Statement dbStat = connection.createStatement()) {
try (ResultSet dbResult = dbStat.executeQuery("select * from testme")) {
printResultSet(dbResult);
}
}
DatabaseMetaData metaData = connection.getMetaData();
System.out.println("Driver: " + metaData.getDriverName());
System.out.println("Database: " + metaData.getDatabaseProductName() + " " + metaData.getDatabaseProductVersion());

System.out.println("Tables:");
try (ResultSet tables = metaData.getTables(null, null, null, null)) {
while (tables.next()) {
String tableName = tables.getString("TABLE_NAME");
System.out.println("\t- " + tableName);
}
}

}
} finally {
System.out.println("Finished (" + (System.currentTimeMillis() - startTime) + "ms)");
Expand Down

0 comments on commit 92a220d

Please sign in to comment.