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 b4f7bd8
Show file tree
Hide file tree
Showing 23 changed files with 608 additions and 155 deletions.
6 changes: 5 additions & 1 deletion services/base-jdbc/pom.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2020, 2021 Contributors to the Eclipse Foundation
Copyright (c) 2020, 2023 Contributors to the Eclipse Foundation
See the NOTICE file(s) distributed with this work for additional
information regarding copyright ownership.
Expand Down 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,127 @@
/*******************************************************************************
* 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;

/**
* Quarkus AgroalDataSource based JDBC client.
*/
public class JdbcClient implements JdbcOperations {
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<JdbcConnection>> handler) {
handler.handle(JdbcAsyncResult.run(() -> {
return new JdbcConnection(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<JdbcConnection> promise = Promise.promise();
getConnection(promise);
promise.future()
.compose(connection -> {
connection.queryWithParams(sql, jsonArray, handler);
connection.close();
return Future.succeededFuture();
});
}

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

}
Loading

0 comments on commit b4f7bd8

Please sign in to comment.