Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Started to implement the Driver, Connection, Statement and ResultSet class #45

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,30 @@ tasks.register('integrationTest', Test) {
}
}

tasks.register('jdbcTest', Test) {
description = 'Runs JDBC tests.'
group = 'verification'

testClassesDirs = sourceSets.test.output.classesDirs
classpath = sourceSets.test.runtimeClasspath
shouldRunAfter test

useJUnitPlatform()

environment('TEST_SURREAL_HOST', 'localhost')
environment('TEST_SURREAL_PORT', 8000)
environment('TEST_SURREAL_USERNAME', 'root')
environment('TEST_SURREAL_PASSWORD', 'root')
environment('TEST_SURREAL_NAMESPACE', 'test')
environment('TEST_SURREAL_DATABASE', 'test')
environment('TEST_SURREAL_TOKEN', '')
environment('TEST_SURREAL_SCOPE', 'allusers')

testLogging {
events "passed"
}
}

checkstyle {
toolVersion = "10.9.3"
configFile = resources.text.fromUri("https://raw.githubusercontent.com/checkstyle/checkstyle/checkstyle-${toolVersion}/src/main/resources/google_checks.xml").asFile()
Expand Down
117 changes: 104 additions & 13 deletions src/main/java/com/surrealdb/jdbc/SurrealJDBCConnection.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.surrealdb.jdbc;

import com.surrealdb.connection.SurrealWebSocketConnection;
import com.surrealdb.driver.AsyncSurrealDriver;
import com.surrealdb.driver.SyncSurrealDriver;
import java.sql.Array;
import java.sql.Blob;
import java.sql.CallableStatement;
Expand All @@ -20,9 +23,83 @@
import java.util.concurrent.Executor;

public class SurrealJDBCConnection implements Connection {

private final String host;
private final String dbName;
private final int port;

// Maybe do these non-final to allow switching these while runtime
private final boolean useTls;
private final boolean useAsync;
private final SurrealWebSocketConnection webSocketConnection;
private SyncSurrealDriver syncDriver;
private AsyncSurrealDriver asyncDriver;

public SurrealJDBCConnection(
String host,
int port,
String dbName,
String namespace,
String user,
String password,
boolean useTls,
boolean useAsync) {
this.host = host;
this.port = port;
this.dbName = dbName;
this.useTls = useTls;
this.useAsync = useAsync;

webSocketConnection = new SurrealWebSocketConnection(host, port, useTls);
webSocketConnection.connect(60);

if (useAsync) {
asyncDriver = new AsyncSurrealDriver(webSocketConnection);
asyncDriver.signIn(user, password);
asyncDriver.use(namespace, dbName);
} else {
syncDriver = new SyncSurrealDriver(webSocketConnection);
syncDriver.signIn(user, password);
syncDriver.use(namespace, dbName);
}

System.out.println("Connection established");
}

public SurrealJDBCConnection(
String host,
int port,
String dbName,
String namespace,
String token,
boolean useTls,
boolean useAsync) {
this.host = host;
this.port = port;
this.dbName = dbName;
this.useTls = useTls;
this.useAsync = useAsync;

webSocketConnection = new SurrealWebSocketConnection(host, port, useTls);
webSocketConnection.connect(5);

if (useAsync) {
asyncDriver = new AsyncSurrealDriver(webSocketConnection);
asyncDriver.authenticate(token);
asyncDriver.use(namespace, dbName);
} else {
syncDriver = new SyncSurrealDriver(webSocketConnection);
syncDriver.authenticate(token);
syncDriver.use(namespace, dbName);
}
}

@Override
public Statement createStatement() throws SQLException {
return new SurrealJDBCStatement();
if (useAsync) {
return new SurrealJDBCStatement(asyncDriver);
}
return new SurrealJDBCStatement(syncDriver);
}

@Override
Expand Down Expand Up @@ -62,6 +139,7 @@ public void rollback() throws SQLException {

@Override
public void close() throws SQLException {
webSocketConnection.close();
throw new UnsupportedOperationException("connection close is currently unsupported");
}

Expand Down Expand Up @@ -97,12 +175,14 @@ public String getCatalog() throws SQLException {

@Override
public void setTransactionIsolation(int level) throws SQLException {
throw new UnsupportedOperationException("connection setTransactionIsolation is unimplemented");
throw new UnsupportedOperationException(
"connection setTransactionIsolation is unimplemented");
}

@Override
public int getTransactionIsolation() throws SQLException {
throw new UnsupportedOperationException("connection getTransactionIsolation is unimplemented");
throw new UnsupportedOperationException(
"connection getTransactionIsolation is unimplemented");
}

@Override
Expand All @@ -116,17 +196,20 @@ public void clearWarnings() throws SQLException {
}

@Override
public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException {
public Statement createStatement(int resultSetType, int resultSetConcurrency)
throws SQLException {
throw new UnsupportedOperationException("connection createStatement is unimplemented");
}

@Override
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
public PreparedStatement prepareStatement(
String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
throw new UnsupportedOperationException("connection prepareStatement is unimplemented");
}

@Override
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency)
throws SQLException {
throw new UnsupportedOperationException("connection prepareCall is unimplemneted");
}

Expand All @@ -147,7 +230,7 @@ public void setHoldability(int holdability) throws SQLException {

@Override
public int getHoldability() throws SQLException {
throw new UnsupportedOperationException();
return 10;
}

@Override
Expand All @@ -171,22 +254,29 @@ public void releaseSavepoint(Savepoint savepoint) throws SQLException {
}

@Override
public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
public Statement createStatement(
int resultSetType, int resultSetConcurrency, int resultSetHoldability)
throws SQLException {
throw new UnsupportedOperationException();
}

@Override
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
public PreparedStatement prepareStatement(
String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability)
throws SQLException {
throw new UnsupportedOperationException();
}

@Override
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
public CallableStatement prepareCall(
String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability)
throws SQLException {
throw new UnsupportedOperationException();
}

@Override
public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException {
public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys)
throws SQLException {
throw new UnsupportedOperationException();
}

Expand All @@ -196,7 +286,8 @@ public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throw
}

@Override
public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException {
public PreparedStatement prepareStatement(String sql, String[] columnNames)
throws SQLException {
throw new UnsupportedOperationException();
}

Expand Down Expand Up @@ -262,7 +353,7 @@ public void setSchema(String schema) throws SQLException {

@Override
public String getSchema() throws SQLException {
throw new UnsupportedOperationException();
return host;
}

@Override
Expand Down
49 changes: 46 additions & 3 deletions src/main/java/com/surrealdb/jdbc/SurrealJDBCDriver.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.surrealdb.jdbc;

import java.sql.Connection;
import com.surrealdb.jdbc.exception.SurrealJDBCDriverInitializationException;
import java.net.URI;
import java.sql.Driver;
import java.sql.DriverPropertyInfo;
import java.sql.SQLException;
Expand All @@ -9,9 +10,51 @@
import java.util.logging.Logger;

public class SurrealJDBCDriver implements Driver {
private static final Driver INSTANCE = new SurrealJDBCDriver();
private static boolean registered;

public SurrealJDBCDriver() {
// Default constructor
}

@Override
public Connection connect(String url, Properties info) throws SQLException {
throw new UnsupportedOperationException();
public SurrealJDBCConnection connect(String url, Properties info)
throws SQLException, SurrealJDBCDriverInitializationException {
/*
Note:
I think it would be wise to create a method to parse the information for tls, async, dbName
from the url and not from the Properties object, please give feedback about this. :)

URL suggestion: jdbc:surrealdb://host:port/dbname;namespace;optionalParam2=SomeValue2;
*/
SurrealJDBCConnection connection;
String host, user, password, dbName, namespace;
boolean useTls, useAsync;
int port;

if (!url.contains("jdbc")) {
throw new SurrealJDBCDriverInitializationException("Missing 'jdbc' in URI!");
}

URI uri = URI.create(url.replace("jdbc:", ""));

if (!uri.getScheme().equals("surrealdb")) {
throw new SurrealJDBCDriverInitializationException(
"Failed to validate the JDBC-URL: Missing scheme 'surrealdb'");
}

host = uri.getHost();
port = uri.getPort();
dbName = uri.getPath().split(";")[0];
namespace = uri.getPath().split(";")[1];

user = info.getProperty("user");
password = info.getProperty("password");
useTls = info.getProperty("tls").equals("true");
useAsync = info.getProperty("async").equals("true");

return new SurrealJDBCConnection(
host, port, dbName, namespace, user, password, useTls, useAsync);
}

@Override
Expand Down
Loading