diff --git a/com.dbeaver.jdbc.driver.libsql/src/main/java/com/dbeaver/jdbc/driver/libsql/LibSqlConnection.java b/com.dbeaver.jdbc.driver.libsql/src/main/java/com/dbeaver/jdbc/driver/libsql/LibSqlConnection.java index ab18e71..7683d8f 100644 --- a/com.dbeaver.jdbc.driver.libsql/src/main/java/com/dbeaver/jdbc/driver/libsql/LibSqlConnection.java +++ b/com.dbeaver.jdbc.driver.libsql/src/main/java/com/dbeaver/jdbc/driver/libsql/LibSqlConnection.java @@ -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; @@ -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 driverProperties; + private final String url; + @NotNull + private final Map driverProperties; + private LibSqlDatabaseMetaData databaseMetaData; public LibSqlConnection( @NotNull LibSqlDriver driver, @@ -50,7 +52,7 @@ 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); } @@ -58,7 +60,7 @@ public LibSqlConnection( getMetaData().getDatabaseProductName(); } - public LSqlClient getClient() { + public LibSqlClient getClient() { return client; } @@ -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; } } diff --git a/com.dbeaver.jdbc.driver.libsql/src/main/java/com/dbeaver/jdbc/driver/libsql/LibSqlDatabaseMetaData.java b/com.dbeaver.jdbc.driver.libsql/src/main/java/com/dbeaver/jdbc/driver/libsql/LibSqlDatabaseMetaData.java index 75caf44..65bacf4 100644 --- a/com.dbeaver.jdbc.driver.libsql/src/main/java/com/dbeaver/jdbc/driver/libsql/LibSqlDatabaseMetaData.java +++ b/com.dbeaver.jdbc.driver.libsql/src/main/java/com/dbeaver/jdbc/driver/libsql/LibSqlDatabaseMetaData.java @@ -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 { - 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) { @@ -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(); } @@ -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, " + @@ -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," + @@ -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"); + } + } + + } diff --git a/com.dbeaver.jdbc.driver.libsql/src/main/java/com/dbeaver/jdbc/driver/libsql/LibSqlDriver.java b/com.dbeaver.jdbc.driver.libsql/src/main/java/com/dbeaver/jdbc/driver/libsql/LibSqlDriver.java index a5528f1..8c67158 100644 --- a/com.dbeaver.jdbc.driver.libsql/src/main/java/com/dbeaver/jdbc/driver/libsql/LibSqlDriver.java +++ b/com.dbeaver.jdbc.driver.libsql/src/main/java/com/dbeaver/jdbc/driver/libsql/LibSqlDriver.java @@ -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; @@ -45,80 +48,7 @@ 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 serverProperties = new LinkedHashMap<>(); - Map driverProperties = new LinkedHashMap<>(); - for (Map.Entry 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 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) { @@ -126,7 +56,7 @@ public boolean acceptsURL(String url) { } @Override - public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException { + public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) { return new DriverPropertyInfo[] { }; @@ -148,7 +78,7 @@ public boolean jdbcCompliant() { } @Override - public Logger getParentLogger() throws SQLFeatureNotSupportedException { + public Logger getParentLogger() { return parentLogger; } diff --git a/com.dbeaver.jdbc.driver.libsql/src/main/java/com/dbeaver/jdbc/driver/libsql/client/LSqlClient.java b/com.dbeaver.jdbc.driver.libsql/src/main/java/com/dbeaver/jdbc/driver/libsql/client/LibSqlClient.java similarity index 89% rename from com.dbeaver.jdbc.driver.libsql/src/main/java/com/dbeaver/jdbc/driver/libsql/client/LSqlClient.java rename to com.dbeaver.jdbc.driver.libsql/src/main/java/com/dbeaver/jdbc/driver/libsql/client/LibSqlClient.java index 0730a40..377df29 100644 --- a/com.dbeaver.jdbc.driver.libsql/src/main/java/com/dbeaver/jdbc/driver/libsql/client/LSqlClient.java +++ b/com.dbeaver.jdbc.driver.libsql/src/main/java/com/dbeaver/jdbc/driver/libsql/client/LibSqlClient.java @@ -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; @@ -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) @@ -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; } @@ -44,16 +46,15 @@ public LSqlClient(URL url, String authToken) { * @return The result set. */ public LibSqlExecutionResult execute(String stmt, Map 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[] parameters) throws SQLException { + public LibSqlExecutionResult[] executeBatch( + @NotNull String[] stmts, + @Nullable Map[] parameters) throws SQLException { try { HttpURLConnection conn = openConnection(); conn.setRequestMethod("POST"); @@ -103,13 +104,17 @@ private void setAuthParameters(HttpURLConnection conn) { } } - private void executeQuery(String[] stmts, Map[] parameters, OutputStream os) throws IOException { + private void executeQuery( + @NotNull String[] queries, + @Nullable Map[] 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();