Java common JDBC SQL Query strategy for Unit Test using HSQLDB and runtime using MySQL

孤人 提交于 2020-07-23 06:51:07

问题


I am developing Java Vert.x 3 application. I use HSQLDB for testing with in-memory DB and MySQL 8.0.20 for runtime. When the vertx verticle is deployed, it initializes the db and tables. Since this is a common code and there are differing SQL syntax between HSQLDB and MySQL and more ridiculously, the HSQLDB capitalizes all the property names and I have to double-quote the properties to use lower-case. I wonder how to achieve this. Here are my questions:

(1) HSQLDB uses "IDENTITY" keyword for creating the in-memory database table. This results in runtime error in MySQL DB as "IDENTITY" is not valid keyword. This poses a challenge that I am facing now.

(2) If it is not possible to have a common SQL syntax which satisfies both MySQL and HSQLDB, what's the best approach to split this common execution path based on the java application runtime profile since this DB initialization is done in the start function of the verticle which is the core of the application?

Any advice and insight is appreciated.


回答1:


You can create the HSQLDB database with MySQL compatibility mode (append ;sql.syntax_mys=true to the JDBC URL. In this mode, you can do the following:

Use MySQL syntax for CREATE TABLE. HSQLDB understands MySQL's AUTO_INCREMENT keyword as an alias for IDENTITY. It also understands all other MySQL-specific syntax.

The capitalization of column names is really not a problem. With the column names capitalized, you can use any case without quoting in your SELECT statements. This means you can run the same query that you use for MySQL on HSQLDB.

See http://hsqldb.org/doc/2.0/guide/compatibility-chapt.html#coc_compatibility_mysql




回答2:


Solution: Ditch HSQLDB and use H2 with database_to_upper=false option.



来源:https://stackoverflow.com/questions/62711738/java-common-jdbc-sql-query-strategy-for-unit-test-using-hsqldb-and-runtime-using

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!