gpt4 book ai didi

java - jOOQ MySQL 如何确定复杂的 DELETE 是否影响行

转载 作者:行者123 更新时间:2023-11-30 22:06:04 27 4
gpt4 key购买 nike

我正在尝试确定以下方法的结果,理想情况下将受影响的行数作为 Int 返回:

/**
* Delete the Account ONLY if there are no `account_foo` or `account_bar` belonging to it.
*
* @param db context.
* @param accountId specific Account to delete.
*/
private void deleteAccount(DSLContext db, ULong accountId) throws DatabaseException {
db.deleteFrom(ACCOUNT)
.where(ACCOUNT.ID.eq(accountId))
.andNotExists(
db.selectFrom(ACCOUNT_FOO)
.where(ACCOUNT_FOO.ACCOUNT_ID.eq(accountId))
)
.andNotExists(
db.selectFrom(ACCOUNT_BAR)
.where(ACCOUNT_BAR.ACCOUNT_ID.eq(accountId))
)
.execute();
}

当通过直接 MySQL 客户端完全像这样执行时,以下查询“有效”:

mysql> DELETE FROM `account` \
WHERE `account`.`id`=12 \
AND NOT EXISTS (SELECT `id` FROM `account_foo` WHERE `account_foo`.`account_id`=12) \
AND NOT EXISTS (SELECT `id` FROM `account_bar` WHERE `account_bar`.`account_id`=12); \
SELECT ROW_COUNT();
Query OK, 1 row affected (0.01 sec)

+-------------+
| ROW_COUNT() |
+-------------+
| 1 |
+-------------+
1 row in set (0.00 sec)

mysql> DELETE FROM `account` \
WHERE `account`.`id`=14 \
AND NOT EXISTS (SELECT `id` FROM `account_foo` WHERE `account_foo`.`account_id`=14) \
AND NOT EXISTS (SELECT `id` FROM `account_bar` WHERE `account_bar`.`account_id`=14); \
SELECT ROW_COUNT();
Query OK, 0 rows affected (0.00 sec)

+-------------+
| ROW_COUNT() |
+-------------+
| 0 |
+-------------+
1 row in set (0.00 sec)

但是以下实现 jOOQ 的版本失败了:

/**
* Delete the Account ONLY if there are no `library` or `account_user_role` belonging to it.
*
* @param db context.
* @param accountId specific Account to delete.
*/
private Integer deleteAccount(DSLContext db, ULong accountId) throws DatabaseException {
Integer rows;
String accountIdString = accountId.toString();
ResultSet rs = db.resultQuery(
"DELETE FROM `account` "+
"WHERE `account`.`id`=" + accountId.toString() +
"AND NOT EXISTS (SELECT `id` FROM `account_foo` WHERE `account_foo`.`account_id`=" + accountId.toString() +") "+
"AND NOT EXISTS (SELECT `id` FROM `account_bar` WHERE `account_bar`.`account_id`=" + accountId.toString() +"); "+
"SELECT ROW_COUNT();"
).fetchResultSet();

try {
rs.next();
rows = rs.getInt(1);
} catch (SQLException e) {
throw new DatabaseException("SQLException: " + e.getMessage());
}

return rows;
}

这是失败的堆栈跟踪:

org.jooq.exception.DataAccessException: SQL [DELETE FROM `account` WHERE `account`.`id`=14 AND NOT EXISTS (SELECT `id` FROM `account_foo` WHERE `account_foo`.`account_id`=14) AND NOT EXISTS (SELECT `id` FROM `account_bar` WHERE `account_bar`.`account_id`=14); SELECT ROW_COUNT();]; You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT ROW_COUNT()' at line 1
at org.jooq.impl.Tools.translate(Tools.java:1941)
at org.jooq.impl.DefaultExecuteContext.sqlException(DefaultExecuteContext.java:659)
at org.jooq.impl.AbstractQuery.execute(AbstractQuery.java:362)
at org.jooq.impl.AbstractResultQuery.fetchLazy(AbstractResultQuery.java:365)
at org.jooq.impl.AbstractResultQuery.fetchLazy(AbstractResultQuery.java:352)
at org.jooq.impl.AbstractResultQuery.fetchResultSet(AbstractResultQuery.java:318)
at io.outright.xj.hub.controller.account.AccountControllerImpl.deleteAccount(AccountControllerImpl.java:173)
at io.outright.xj.hub.controller.account.AccountControllerImpl.deleteAccount(AccountControllerImpl.java:84)
at io.outright.xj.hub.resource.accounts.AccountRecordResource.deleteAccount(AccountRecordResource.java:109)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory$1.invoke(ResourceMethodInvocationHandlerFactory.java:81)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:164)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:181)
at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$ResponseOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:158)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:101)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:389)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:347)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:102)
at org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:305)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:267)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)
at org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:288)
at org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1110)
at org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpContainer.service(GrizzlyHttpContainer.java:381)
at org.glassfish.grizzly.http.server.HttpHandler$1.run(HttpHandler.java:219)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT ROW_COUNT()' at line 1
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:536)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:513)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:115)
at com.mysql.cj.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:1983)
at com.mysql.cj.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1826)
at com.mysql.cj.jdbc.PreparedStatement.execute(PreparedStatement.java:1153)
at org.jooq.tools.jdbc.DefaultPreparedStatement.execute(DefaultPreparedStatement.java:194)
at org.jooq.impl.AbstractResultQuery.execute(AbstractResultQuery.java:269)
at org.jooq.impl.AbstractQuery.execute(AbstractQuery.java:348)
... 32 more

另请参阅:MySQL doc for ROW_COUNT()

最佳答案

您遇到了两个问题:

1。您的 JDBC 驱动程序必须设置为允许每个 JDBC 语句进行多次查询

您需要将此标志添加到您的 JDBC 连接 URL:allowMultiQueries=true

See for instance this stack overflow question .

2。 jOOQ 只返回批处理的第一个结果

当通过 jOOQ 的纯 SQL API 发送批处理时,您只会从该批处理中获得第一个结果。在这种情况下,这是 INSERT 语句的更新计数,而不是 SELECT 语句的结果(注意 jOOQ 3.9.1 中也有错误 #5818)。

您可能想要做的是使用 ResultQuery.fetchMany() :

db.resultQuery(
"DELETE FROM `account` "+
"WHERE `account`.`id`={0} "+
"AND NOT EXISTS (SELECT `id` FROM `account_foo` WHERE `account_foo`.`account_id`={0}) "+
"AND NOT EXISTS (SELECT `id` FROM `account_bar` WHERE `account_bar`.`account_id`={0}); "+
"SELECT ROW_COUNT();", val(accountId)
).fetchMany();

这将返回更新计数和结果集。

请注意,为了防止 SQL 注入(inject)风险,我还通过绑定(bind)变量将 accountId 的字符串连接替换为您的 SQL 语句。

关于java - jOOQ MySQL 如何确定复杂的 DELETE 是否影响行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41731827/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com