-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#3562] Migrate to Quarkus JDBC implementation
- Loading branch information
Showing
24 changed files
with
752 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
...es/base-jdbc/src/main/java/org/eclipse/hono/service/base/jdbc/client/JdbcAsyncResult.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
152 changes: 152 additions & 0 deletions
152
services/base-jdbc/src/main/java/org/eclipse/hono/service/base/jdbc/client/JdbcClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
} | ||
|
||
} |
Oops, something went wrong.