Skip to content

Commit

Permalink
dbeaver/dbeaver#23361 Statement execution + client
Browse files Browse the repository at this point in the history
  • Loading branch information
serge-rider committed Oct 27, 2024
1 parent e89e698 commit 95dec00
Show file tree
Hide file tree
Showing 10 changed files with 153 additions and 16 deletions.
3 changes: 2 additions & 1 deletion com.dbeaver.jdbc.driver.libsql/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Bundle-RequiredExecutionEnvironment: JavaSE-17
Export-Package: com.dbeaver.jdbc.libsql
Require-Bundle: com.dbeaver.jdbc.api,
com.dbeaver.rpc,
org.jkiss.utils
org.jkiss.utils,
com.google.gson
Bundle-Vendor: DBeaver Corp
Automatic-Module-Name: com.dbeaver.jdbc.driver.libsql
5 changes: 5 additions & 0 deletions com.dbeaver.jdbc.driver.libsql/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
<packaging>eclipse-plugin</packaging>

<dependencies>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.dbeaver.common</groupId>
<artifactId>org.jkiss.utils</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,45 @@
*/
package com.dbeaver.jdbc.driver.libsql;

import com.dbeaver.jdbc.driver.libsql.client.LSqlClient;
import com.dbeaver.jdbc.model.AbstractJdbcConnection;
import org.jkiss.code.NotNull;
import org.jkiss.utils.CommonUtils;

import java.io.IOException;
import java.net.URL;
import java.sql.*;
import java.util.Map;

public class LSqlConnection extends AbstractJdbcConnection {

@NotNull
private final LSqlDriver driver;
private final LSqlClient client;
@NotNull
private String url;
@NotNull
private Map<String, String> driverProperties;
private Map<String, Object> driverProperties;

public LSqlConnection(
@NotNull LSqlDriver driver,
@NotNull String url,
@NotNull Map<String, String> driverProperties
@NotNull Map<String, Object> driverProperties
) throws SQLException {
this.driver = driver;
this.url = url;
this.driverProperties = driverProperties;

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

public LSqlClient getClient() {
return client;
}

@NotNull
Expand All @@ -47,7 +63,7 @@ public String getUrl() {
}

@NotNull
public Map<String, String> getDriverProperties() {
public Map<String, Object> getDriverProperties() {
return driverProperties;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@


import java.sql.*;
import java.util.Enumeration;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Logger;
import java.util.regex.Matcher;
Expand All @@ -34,8 +37,14 @@ public Connection connect(String url, Properties info) throws SQLException {
"Invalid connection URL: " + url +
".\nExpected URL format: " + LSqlConstants.CONNECTION_URL_EXAMPLE);
}
String targetUrl = matcher.group(1);

throw new SQLFeatureNotSupportedException();
Map<String, Object> props = new LinkedHashMap<>();
for (Enumeration<?> pne = info.propertyNames(); pne.hasMoreElements(); ) {
String propName = pne.toString();
props.put(propName, info.get(propName));
}
return new LSqlConnection(this, targetUrl, props);
/*
UPDEndpoint endpoint = new UPDEndpoint();
endpoint.setServerAddress(matcher.group(1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public LSqlExecutionResult getResult() {
private int getColumnIndex(String columnLabel) throws LSqlException {
if (nameMap == null) {
nameMap = new HashMap<>();
List<String> columns = result.getColumnNames();
List<String> columns = result.getColumns();
for (int i = 0; i < columns.size(); i++) {
nameMap.put(columns.get(i).toUpperCase(Locale.ENGLISH), i + 1);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public LSqlResultSetMetaData(@NotNull LSqlResultSet resultSet) throws SQLExcepti

@Override
public int getColumnCount() throws SQLException {
return resultSet.getResult().getColumnNames().size();
return resultSet.getResult().getColumns().size();
}

@Override
Expand Down Expand Up @@ -74,12 +74,12 @@ public int getColumnDisplaySize(int column) throws SQLException {

@Override
public String getColumnLabel(int column) throws SQLException {
return resultSet.getResult().getColumnNames().get(column);
return resultSet.getResult().getColumns().get(column - 1);
}

@Override
public String getColumnName(int column) throws SQLException {
return resultSet.getResult().getColumnNames().get(column);
return resultSet.getResult().getColumns().get(column - 1);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ public LSqlStatement(@NotNull LSqlConnection connection) throws SQLException {

@Override
public ResultSet executeQuery(String sql) throws SQLException {
throw new SQLFeatureNotSupportedException();
LSqlExecutionResult result = connection.getClient().execute(sql);
return new LSqlResultSet(result);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package com.dbeaver.jdbc.driver.libsql.client;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.Strictness;
import com.google.gson.ToNumberPolicy;
import com.google.gson.stream.JsonWriter;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.sql.SQLException;

/**
* The entry point to LibSQL client API.
*/
public class LSqlClient {
public static final String DEFAULT_ISO_TIMESTAMP_FORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'";

private URL url;
private String authToken;
private final Gson gson = new GsonBuilder()
.setStrictness(Strictness.LENIENT)
.setDateFormat(DEFAULT_ISO_TIMESTAMP_FORMAT)
.setObjectToNumberStrategy(ToNumberPolicy.LONG_OR_DOUBLE)
.create();

public LSqlClient(URL url, String authToken) {
this.url = url;
this.authToken = authToken;
}

/**
* Execute a single SQL statement.
*
* @return The result set.
*/
public LSqlExecutionResult execute(String stmt) throws SQLException {
return batch(new String[]{stmt})[0];
}

/**
* Execute a batch of SQL statements.
*
* @param stmts The SQL statements.
* @return The result sets.
*/
public LSqlExecutionResult[] batch(String[] stmts) throws SQLException {
try {
HttpURLConnection conn = openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);

try (OutputStream os = conn.getOutputStream()) {
query(stmts, os);
}
try (InputStreamReader in = new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8)) {
Response[] response = gson.fromJson(in, Response[].class);
LSqlExecutionResult[] resultSets = new LSqlExecutionResult[response.length];
for (int i = 0; i < response.length; i++) {
resultSets[i] = response[i].results;
}
return resultSets;
}
} catch (Exception e) {
throw new SQLException(e);
}

}

private HttpURLConnection openConnection() throws IOException {
HttpURLConnection conn = (HttpURLConnection) this.url.openConnection();
if (authToken != null) {
conn.setRequestProperty("Authorization", "Bearer " + authToken);
}
return conn;
}

private void query(String[] stmts, OutputStream os) throws IOException {
JsonWriter jsonWriter = new JsonWriter(new OutputStreamWriter(os, StandardCharsets.UTF_8));
jsonWriter.beginObject();
jsonWriter.name("statements");
jsonWriter.beginArray();
for (String stmt : stmts) {
jsonWriter.value(stmt);
}
jsonWriter.endArray();
jsonWriter.endObject();
jsonWriter.flush();
}

public static class Response {
public LSqlExecutionResult results;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@

public class LSqlExecutionResult {

private List<String> columnNames;
private List<String> columns;
private List<Object[]> rows;
private long updateCount;

public List<String> getColumnNames() {
return columnNames;
public List<String> getColumns() {
return columns;
}

public void setColumnNames(List<String> columnNames) {
this.columnNames = columnNames;
public void setColumns(List<String> columns) {
this.columns = columns;
}

public List<Object[]> getRows() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ public static void main(String[] args) throws Exception {
try {
LSqlDriver driver = new LSqlDriver();
Properties props = new Properties();
try (Connection connection = driver.connect("jdbc:dbeaver.libsql:http://localhost:9999", props)) {
try (Connection connection = driver.connect("jdbc:dbeaver:libsql:" + args[0], props)) {
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());
Expand Down

0 comments on commit 95dec00

Please sign in to comment.