Jooq count method getting wrapped in backticks - java

I recently updated a project's jooq version from 3.13.5 to 3.14.15. I've rerun jooq-codegen. I'm using MySQL 5.7.
When running one of my tests - it performs a DAOImpl.exists. That call generates the following exception:
org.jooq.exception.DataAccessException: SQL [select `count`(*) from `users` where `users`.`id` = ?]; 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 '*) from `users` where `users`.`id` = 1094' at line 1
at org.jooq_3.14.15.MYSQL.debug(Unknown Source)
at org.jooq.impl.Tools.translate(Tools.java:2903)
at org.jooq.impl.DefaultExecuteContext.sqlException(DefaultExecuteContext.java:757)
at org.jooq.impl.AbstractQuery.execute(AbstractQuery.java:389)
at org.jooq.impl.AbstractResultQuery.fetchLazy(AbstractResultQuery.java:453)
at org.jooq.impl.AbstractResultQuery.fetchLazy(AbstractResultQuery.java:422)
at org.jooq.impl.AbstractResultQuery.fetchLazyNonAutoClosing(AbstractResultQuery.java:436)
at org.jooq.impl.AbstractResultQuery.fetchOne(AbstractResultQuery.java:613)
at org.jooq.impl.AbstractResultQuery.fetchOne(AbstractResultQuery.java:565)
at org.jooq.impl.SelectImpl.fetchOne(SelectImpl.java:3034)
at org.jooq.impl.DAOImpl.existsById(DAOImpl.java:300)
at org.jooq.impl.DAOImpl.exists(DAOImpl.java:288)
The problem is that count has backticks. It should be select count(*) from `users` where `users`.`id` = ?
Has anyone run into this before and know how to fix it? TIA

You probably specified RenderQuotedNames.ALWAYS and ran into this problem? https://github.com/jOOQ/jOOQ/issues/9931
The setting ALWAYS is misleading. It really means literally always, though people probably read it as ALWAYSISH_I_E_ONLY_WHEN_IT_MAKES_SENSE. Probably a naming design error, but what you want is EXPLICIT_DEFAULT_QUOTED, see the documentation:
https://www.jooq.org/doc/latest/manual/sql-building/dsl-context/custom-settings/settings-name-style/
RenderQuotedNames
ALWAYS: This will quote all identifiers.
EXPLICIT_DEFAULT_QUOTED: This will quote all identifiers, which are not explicitly unquoted using DSL.unquotedName().
EXPLICIT_DEFAULT_UNQUOTED: This will not quote any identifiers, unless they are explicitly quoted using DSL.quotedName().
NEVER: This will not quote any identifiers.
COUNT in DSL.count()is an explicitly unquoted name, and ALWAYS will override that.

Related

MariaDB JDBC chokes on column name containing curly brackets

I have a very simple SQL query I need to run
SELECT `id`, `{Document id}` FROM `test`.`test` LIMIT 10;
where {Document id} is a column name. Whenever I run it through MariaDB JDBC, it fails with error unknown escape sequence. From my understanding {CALL ...} is used to call stored procedure with a JDBC CallableStatement.
How do I escape it? I want JDBC to treat it as literal string without special meaning. \ didn't work for me.
As mentioned in deleted answer by #a_horse_with_no_name, there is setEscapeProcessing. But it's not supported by a lot of connectors (example MariaDB).
I have confirmed this issue using mariadb-java-client-2.2.5. It is not an issue with mysql-connector-java-5.1.44 so you might want to report this issue to MariaDB.
Following the suggestion by #JoopEggen in the comments above, I got it to work with MariaDB JDBC by adding sessionVariables=sql_mode=ANSI_QUOTES to the connection string and using
ResultSet rs = st.executeQuery("SELECT \"{Document id}\" FROM test.test");

rewriteBatchedStatements introduces Syntax Error

I'm optimizing some SQL queries. And I've written them to use batches. Integration tests run fine, and everything is happy. But then I learn that MySQL doesn't give a performance increase unless rewriteBatchedStatements is enabled.
So I modified my connection string to include rewriteBatchedStatement, but I start getting a MySQLSytaxErrorException. When I remove rewriteBatchedStatement from the connection String, the exception goes away.
SQL: INSERT INTO rawdata.scales (ID, NAME) VALUES (?, ?)
Exception:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 ';INSERT INTO rawdata.scales` at line 1
Curiously, this only happens on the first SQL statement that runs in the tests I've setup. All the others run fine.
Why would enabling this feature add a semi-colon to the front of my SQL?
There is a known issue in Connector/J 5.1.40 up to and including at least 5.1.43 which causes MySQLSytaxErrorException to be thrown on inserts when rewriteBatchedStatement is enabled.
See https://bugs.mysql.com/bug.php?id=84813
I have been able to reproduce this in statements with a trailing semicolon, e.g.
INSERT INTO rawdata.scales (ID, NAME) VALUES (?, ?);
Removing the trailing semicolon fixes the issue in my case.

Column (XIT115) not found in any table in the query (or SLV is undefined)

I am getting below error when running this informix update statement using Eclipse. The same query runs absolutely fine when it is run using DBeaver tool.
Update logistics_user_security:informix.user set password_expiry_ts = Today-1 where user_id = "XIT115"
Error observer in eclipse in below :
Column (XIT115) not found in any table in the query (or SLV is undefined).];
Your client configuration in Eclipse is probably setting the DELIMIDENT environment variable. That means that the strings inside double quotation marks ( " ) are interpreted as SQL identifiers. Use single quotation marks ( ' ) for literal strings or change the DELIMIDENT environment variable.
Link to the official documentation: The DELIMIDENT Environment Variable

MySQL syntax mistake

When I try to execute:
SELECT matricula FROM lss_alumnos WHERE lss_alumnos.division = 'PREP';
I get this message:
SQLException: 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 '.division = 'PREP'' at line 1
And I am also using MySQL Workbench, and there I got the right result.
Try removing the qualified name lss_alumnos before the column name, also, remove the leading ;.
SELECT `matricula` FROM `lss_alumnos` WHERE `division` = 'PREP'
(It's also a good practice to surround the names with the ` symbol).
I believe the comments are closer to the truth than the only other answer. To be sure, this query will work:
SELECT la.`matricula` FROM `lss_alumnos` la WHERE la.`division`='PREP'
Edit: I checked. None of these words are a MySQL reserved word http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html. It must be the semicolon

A query works in SQL*Plus but fails in JDBC with an ORA-00911 exception

I'm trying to track the amount of redo being generated during a database session with the following query:
SELECT a.name, b.VALUE
FROM v$statname a, v$mystat b
WHERE a.statistic# = b.statistic# AND a.name = 'redo size';
This query works directly in SQL*Plus and Toad, but I get an ORA-00911 exception using JDBC, and I've narrowed it down to the "statistic#" column name.
How do I get around this?
The column name statistic# is not the problem.
My bet is that you also send the terminating ; from inside your Java program.
But you may not include the the ; when executing a SQL statement through JDBC (at least not in Oracle and some other DBMS).
Remove the ; from your SQL String and it should be fine.
put it in double quotes - that should let you call a field anything in Oracle
Switch on JDBC logging, check your driver documentation for how to do this. In the JDBC log you see the actual statement prepared and executed in the DB. This eliminates one possible cause for the error.

Categories

Resources