Skip to content

Commit

Permalink
dbeaver/dbeaver#23361 Code style, dead code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
serge-rider committed Oct 30, 2024
1 parent 84d8b03 commit acd1760
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
package com.dbeaver.jdbc.driver.libsql;

import com.dbeaver.jdbc.driver.libsql.client.LSqlClient;
import com.dbeaver.jdbc.driver.libsql.client.LibSqlClient;
import com.dbeaver.jdbc.model.AbstractJdbcConnection;
import org.jkiss.code.NotNull;
import org.jkiss.utils.CommonUtils;
Expand All @@ -33,11 +33,13 @@ public class LibSqlConnection extends AbstractJdbcConnection {

@NotNull
private final LibSqlDriver driver;
private final LSqlClient client;
@NotNull
private String url;
private final LibSqlClient client;
@NotNull
private Map<String, Object> driverProperties;
private final String url;
@NotNull
private final Map<String, Object> driverProperties;
private LibSqlDatabaseMetaData databaseMetaData;

public LibSqlConnection(
@NotNull LibSqlDriver driver,
Expand All @@ -50,15 +52,15 @@ public LibSqlConnection(

try {
String token = CommonUtils.toString(driverProperties.get("password"));
this.client = new LSqlClient(new URL(url), token);
this.client = new LibSqlClient(new URL(url), token);
} catch (IOException e) {
throw new SQLException(e);
}
// Verify connection
getMetaData().getDatabaseProductName();
}

public LSqlClient getClient() {
public LibSqlClient getClient() {
return client;
}

Expand Down Expand Up @@ -103,7 +105,10 @@ public boolean isClosed() {

@Override
public DatabaseMetaData getMetaData() throws SQLException {
return new LibSqlDatabaseMetaData(this);
if (databaseMetaData == null) {
databaseMetaData = new LibSqlDatabaseMetaData(this);
}
return databaseMetaData;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,23 @@

import com.dbeaver.jdbc.model.AbstractJdbcDatabaseMetaData;
import org.jkiss.code.NotNull;
import org.jkiss.utils.CommonUtils;
import org.jkiss.utils.IOUtils;

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

public class LibSqlDatabaseMetaData extends AbstractJdbcDatabaseMetaData<LibSqlConnection> {

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

private String serverVersion;

public LibSqlDatabaseMetaData(@NotNull LibSqlConnection connection) {
Expand Down Expand Up @@ -67,12 +72,12 @@ public String getDatabaseProductVersion() throws SQLException {
}

@Override
public String getDriverName() throws SQLException {
public String getDriverName() {
return connection.getDriver().getDriverName();
}

@Override
public String getDriverVersion() throws SQLException {
public String getDriverVersion() {
return connection.getDriver().getFullVersion();
}

Expand All @@ -86,23 +91,9 @@ public int getDriverMinorVersion() {
return connection.getDriver().getMinorVersion();
}

@Override
public ResultSet getCatalogs() throws SQLException {
throw new SQLFeatureNotSupportedException();
}

@Override
public ResultSet getSchemas() throws SQLException {
throw new SQLFeatureNotSupportedException();
}

@Override
public ResultSet getSchemas(String catalog, String schemaPattern) throws SQLException {
throw new SQLFeatureNotSupportedException();
}

@Override
public ResultSet getTables(String catalog, String schemaPattern, String tableNamePattern, String[] types) throws SQLException {
verifySchemaParameters(catalog, schemaPattern);
try (PreparedStatement dbStat = connection.prepareStatement(
"SELECT NULL as TABLE_CAT, NULL AS TABLE_SCHEM," +
"name AS TABLE_NAME,type as TABLE_TYPE, " +
Expand All @@ -115,6 +106,7 @@ public ResultSet getTables(String catalog, String schemaPattern, String tableNam

@Override
public ResultSet getColumns(String catalog, String schemaPattern, String tableNamePattern, String columnNamePattern) throws SQLException {
verifySchemaParameters(catalog, schemaPattern);
String tableName = tableNamePattern;
try (PreparedStatement dbStat = connection.prepareStatement(
"SELECT NULL as TABLE_CAT, NULL AS TABLE_SCHEM,'" + tableName + "' AS TABLE_NAME," +
Expand All @@ -125,4 +117,15 @@ public ResultSet getColumns(String catalog, String schemaPattern, String tableNa
return dbStat.executeQuery();
}
}

private static void verifySchemaParameters(String catalog, String schemaPattern) throws SQLException {
if (!CommonUtils.isEmpty(catalog)) {
throw new SQLException("Catalogs are not supported");
}
if (!CommonUtils.isEmpty(schemaPattern)) {
throw new SQLException("Schemas are not supported");
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
package com.dbeaver.jdbc.driver.libsql;


import java.sql.*;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverPropertyInfo;
import java.sql.SQLException;
import java.util.Enumeration;
import java.util.LinkedHashMap;
import java.util.Map;
Expand Down Expand Up @@ -45,88 +48,15 @@ public Connection connect(String url, Properties info) throws SQLException {
props.put(propName, info.get(propName));
}
return new LibSqlConnection(this, targetUrl, props);
/*
UPDEndpoint endpoint = new UPDEndpoint();
endpoint.setServerAddress(matcher.group(1));
endpoint.setServerPort(CommonUtils.toInt(matcher.group(2), endpoint.getServerPort()));
endpoint.setProjectId(matcher.group(3));
String dsRef = matcher.group(4);
if (!dsRef.contains("=")) {
endpoint.setDataSourceId(dsRef);
} else {
}
Map<String, String> serverProperties = new LinkedHashMap<>();
Map<String, String> driverProperties = new LinkedHashMap<>();
for (Map.Entry<Object, Object> pe : info.entrySet()) {
String propName = pe.getKey().toString();
if (propName.startsWith(UPDConstants.DRIVER_PROPERTY_DBEAVER)) {
serverProperties.put(
propName.substring(UPDConstants.DRIVER_PROPERTY_DBEAVER.length()),
(String)pe.getValue());
} else {
driverProperties.put(propName, (String)pe.getValue());
}
}
UPDProtocol protocol = openServerConnection(endpoint, serverProperties);
try {
return new LSqlConnection(this, protocol, endpoint, serverProperties, driverProperties);
} catch (Throwable e) {
// Terminate remote client
protocol.close();
throw e;
}
*/
}

/*
private UPDProtocol openServerConnection(
@NotNull UPDEndpoint endpoint,
@NotNull Map<String, String> serverProperties
) throws SQLException {
StringBuilder url = new StringBuilder();
String schema = endpoint.getServerPort() == 0 ||
endpoint.getServerPort() == UPDConstants.DEFAULT_SERVER_PORT ? "https" : "http";
url.append(schema).append("://");
url.append(endpoint.getServerAddress());
if (endpoint.getServerPort() > 0) {
url.append(":").append(endpoint.getServerPort());
}
url.append("/api/upd/?");
if (endpoint.getProjectId() != null) {
url.append(UPDConstants.SERVER_URL_PARAM_PROJECT).append("=").append(endpoint.getProjectId());
} else {
throw new LSqlException("Datasource ID is missing");
}
if (endpoint.getDataSourceId() != null) {
url.append(UPDConstants.SERVER_URL_PARAM_DATASOURCE_ID).append("=").append(endpoint.getDataSourceId());
} else {
throw new LSqlException("Datasource ID is missing");
}
URI serverURI;
try {
serverURI = new URI(url.toString());
} catch (URISyntaxException e) {
throw new LSqlException("Invalid server URI [" + url + "]", e);
}
return JsonRpcClient
.builder(serverURI, UPDProtocol.class)
.setUserAgent(getDriverName() + " " + getFullVersion())
.create();
}
*/

@Override
public boolean acceptsURL(String url) {
return LibSqlConstants.CONNECTION_URL_PATTERN.matcher(url).matches();
}

@Override
public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException {
public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) {
return new DriverPropertyInfo[] {

};
Expand All @@ -148,7 +78,7 @@ public boolean jdbcCompliant() {
}

@Override
public Logger getParentLogger() throws SQLFeatureNotSupportedException {
public Logger getParentLogger() {
return parentLogger;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import com.google.gson.Strictness;
import com.google.gson.ToNumberPolicy;
import com.google.gson.stream.JsonWriter;
import org.jkiss.code.NotNull;
import org.jkiss.code.Nullable;
import org.jkiss.utils.CommonUtils;

import java.io.IOException;
Expand All @@ -22,7 +24,7 @@
/**
* The entry point to LibSQL client API.
*/
public class LSqlClient {
public class LibSqlClient {

private static final Gson gson = new GsonBuilder()
.setStrictness(Strictness.LENIENT)
Expand All @@ -33,7 +35,7 @@ public class LSqlClient {
private final URL url;
private final String authToken;

public LSqlClient(URL url, String authToken) {
public LibSqlClient(URL url, String authToken) {
this.url = url;
this.authToken = authToken;
}
Expand All @@ -44,16 +46,15 @@ public LSqlClient(URL url, String authToken) {
* @return The result set.
*/
public LibSqlExecutionResult execute(String stmt, Map<Object, Object> parameters) throws SQLException {
return batch(new String[]{stmt}, new Map[]{ parameters })[0];
return executeBatch(new String[]{stmt}, new Map[]{ parameters })[0];
}

/**
* Execute a batch of SQL statements.
*
* @param stmts The SQL statements.
* @return The result sets.
*/
public LibSqlExecutionResult[] batch(String[] stmts, Map<Object, Object>[] parameters) throws SQLException {
public LibSqlExecutionResult[] executeBatch(
@NotNull String[] stmts,
@Nullable Map<Object, Object>[] parameters) throws SQLException {
try {
HttpURLConnection conn = openConnection();
conn.setRequestMethod("POST");
Expand Down Expand Up @@ -103,13 +104,17 @@ private void setAuthParameters(HttpURLConnection conn) {
}
}

private void executeQuery(String[] stmts, Map<Object, Object>[] parameters, OutputStream os) throws IOException {
private void executeQuery(
@NotNull String[] queries,
@Nullable Map<Object, Object>[] parameters,
@NotNull OutputStream os
) throws IOException {
JsonWriter jsonWriter = new JsonWriter(new OutputStreamWriter(os, StandardCharsets.UTF_8));
jsonWriter.beginObject();
jsonWriter.name("statements");
jsonWriter.beginArray();
for (int i = 0; i < stmts.length; i++) {
String stmt = stmts[i];
for (int i = 0; i < queries.length; i++) {
String stmt = queries[i];
if (i < parameters.length && !CommonUtils.isEmpty(parameters[i])) {
// Query with parameters
jsonWriter.beginObject();
Expand Down

0 comments on commit acd1760

Please sign in to comment.