Skip to content

Commit

Permalink
[#3562] Migrate to Quarkus JDBC implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
harism committed Oct 9, 2023
1 parent a3bd800 commit c42376c
Show file tree
Hide file tree
Showing 24 changed files with 752 additions and 127 deletions.
4 changes: 4 additions & 0 deletions services/base-jdbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@
<scope>provided</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-agroal</artifactId>
</dependency>

<!-- testing -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*******************************************************************************
* Copyright (c) 2023 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/

package org.eclipse.hono.service.base.jdbc.client;

import java.util.concurrent.Callable;

import io.vertx.core.AsyncResult;

/**
* JDBC properties for the device store.
* @param <T> Return class type
*/
public class JdbcAsyncResult<T> implements AsyncResult<T> {
private T result;
private Throwable cause;
private boolean succeeded;

@Override
public T result() {
return result;
}

@Override
public Throwable cause() {
return cause;
}

@Override
public boolean succeeded() {
return succeeded;
}

@Override
public boolean failed() {
return !succeeded;
}

/**
* Static method to create AsyncResult instance.
* @param <S> Return class type
* @param func Execution function
* @return AsyncResult instance
*/
public static <S> AsyncResult<S> run(final Callable<S> func) {
final JdbcAsyncResult<S> result = new JdbcAsyncResult<S>();
try {
result.result = func.call();
result.succeeded = true;
} catch (Throwable t) {
result.succeeded = false;
result.cause = t;
}
return result;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
/*******************************************************************************
* Copyright (c) 2023 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/

package org.eclipse.hono.service.base.jdbc.client;

import io.agroal.api.AgroalDataSource;
import io.vertx.codegen.annotations.Nullable;
import io.vertx.core.AsyncResult;
import io.vertx.core.Future;
import io.vertx.core.Handler;
import io.vertx.core.Promise;
import io.vertx.core.json.JsonArray;
import io.vertx.ext.sql.ResultSet;
import io.vertx.ext.sql.SQLRowStream;

/**
* Quarkus AgroalDataSource based JDBC client.
*/
public class JdbcClient implements JdbcSqlOperations {
private final AgroalDataSource dataSource;

/**
* JdbcClient constructor.
* @param dataSource Quarkus Argoal data source
*/
public JdbcClient(final AgroalDataSource dataSource) {
this.dataSource = dataSource;
}

/**
* Open connection asynchronously.
* @param handler Asynchronous listener
*/
public void getConnection(final Handler<AsyncResult<JdbcSqlConnection>> handler) {
handler.handle(JdbcAsyncResult.run(() -> {
return new JdbcSqlConnection(dataSource.getConnection());
}));
}

/**
* Close connection asynchronously.
* @param handler Asynchronous listener
*/
public void close(final Handler<AsyncResult<Void>> handler) {
handler.handle(JdbcAsyncResult.run(() -> {
dataSource.close();
return null;
}));
};

/**
* Close connection synchronously.
*/
public void close() {
dataSource.close();
}

@Override
public void queryWithParams(final String sql, final JsonArray jsonArray, final Handler<AsyncResult<ResultSet>> handler) {
final Promise<JdbcSqlConnection> promise = Promise.promise();
getConnection(promise);
promise.future()
.compose(connection -> {
connection.queryWithParams(sql, jsonArray, handler);
connection.close();
return Future.succeededFuture();
});
}

@Override
public void queryStream(final String sql, final Handler<AsyncResult<SQLRowStream>> handler) {
final Promise<JdbcSqlConnection> promise = Promise.promise();
getConnection(promise);
promise.future()
.compose(connection -> {
connection.queryStream(sql, handler);
connection.close();
return Future.succeededFuture();
});
}

@Override
public void queryStreamWithParams(final String sql, final JsonArray jsonArray, final Handler<AsyncResult<SQLRowStream>> handler) {
final Promise<JdbcSqlConnection> promise = Promise.promise();
getConnection(promise);
promise.future()
.compose(connection -> {
connection.queryStreamWithParams(sql, jsonArray, handler);
connection.close();
return Future.succeededFuture();
});
}

@Override
public void querySingle(final String sql, final Handler<AsyncResult<@Nullable JsonArray>> handler) {
final Promise<JdbcSqlConnection> promise = Promise.promise();
getConnection(promise);
promise.future()
.compose(connection -> {
connection.querySingle(sql, handler);
connection.close();
return Future.succeededFuture();
});
}

@Override
public void querySingleWithParams(final String sql, final JsonArray arguments, final Handler<AsyncResult<@Nullable JsonArray>> handler) {
final Promise<JdbcSqlConnection> promise = Promise.promise();
getConnection(promise);
promise.future()
.compose(connection -> {
connection.querySingleWithParams(sql, arguments, handler);
connection.close();
return Future.succeededFuture();
});
}

@Override
public void updateWithParams(final String sql, final JsonArray jsonArray, final Handler<AsyncResult<Integer>> handler) {
final Promise<JdbcSqlConnection> promise = Promise.promise();
getConnection(promise);
promise.future()
.compose(connection -> {
connection.updateWithParams(sql, jsonArray, handler);
connection.close();
return Future.succeededFuture();
});
}

@Override
public void call(final String sql, final Handler<AsyncResult<Void>> handler) {
final Promise<JdbcSqlConnection> promise = Promise.promise();
getConnection(promise);
promise.future()
.compose(connection -> {
connection.call(sql, handler);
connection.close();
return Future.succeededFuture();
});
}

}
Loading

0 comments on commit c42376c

Please sign in to comment.