4

Is there any way to test that SQL scripts contain standard SQL with java/junit tests?

Currently we have sql scripts for creating a database etc. in a Postgres db, but when using hsqldb everything fails. That's why I wonder if any java tools exist for testing if sql statements are standard sql.

Or would it just be wise to create different sets of scripts per database vendor? If so, is there a way to test if a given script works with postgres/hsqldb?

2
  • See stackoverflow.com/questions/141499/… Commented Aug 25, 2010 at 8:27
  • Another good reason to not use a different DBMS in production and for testing. And even if you could test for a SQL query being "standard" SQL that doesn't mean it runs on all DBMS equally well. No DBMS implements the full SQL standard, some ignore rules defined by the standard. Commented Jan 6, 2016 at 19:28

6 Answers 6

1

The H2 database supports different modes, which may help you with postgres testing, I've found that our sql often contains functions which are not supported but H2, but you can create your own "stored procedures" which actually invoke a static Java method to work around this. If you want to support different database vendors you should go down the vendor specific script route, unless you are doing really basic queries.

If you have the available resources I would recommend setting up a fully fledged UAT environment which you can use to test against a live postgres database, as even seemingly minor db configuration differences can impact query plans in unexpected ways.

Sign up to request clarification or add additional context in comments.

Comments

0

I've usually made a very simple java-wrapper that tests this code by using a localhost-connection with some standard user/pass settings.

Remember to use a temporary database or a known test-database so your tests doesn't destroy anything important.

Reason for above is that I have had the need for specific databases (non standard features etc).

If you only want to test standard sql-stuff for junit tests (like syntax, selects etc), I would consider using a embedded sql database in java (ususally memory only). That way it is easy to test lots of stuff without the need to install a db and also without the risk of destoring other installations.

Comments

0

It sounds like you're looking for an SQL syntax parser and validator. The only Java SQL parser with which I'm familiar is Zql, but I've never actually used it.

A similar question was asked early last year, and the best answer there turned out to be writing your own parser with ANTLR.

Comments

0

The best tool for checking SQL statements for conformance to Standard SQL is HSQLDB 2.0. This is especially true with data definition statements. HSQLDB has been written to the Standard, as opposed to adopting bits of the Standard over several versions.

PostgresSQL has been slowly moving towards standard SQL. But it still has some "legacy" data types. HSQLDB allows you to define types with the CREATE TYPE statement for compatibility with other dialects. It also allows you to define functions in SQL or Java for the same purpose.

The best policy is to use standard SQL as much as possible, with user-defined types and functions to support an alternative database's syntax.

Comments

0

ZQL works fine with Junit..

ZqlParser parser = new ZqlParser(any input stream);
try{
 parser = parser.readStatement();
}catch(ParseException e){
// if sql is not valid it caught here
}

Comments

0

Different vendors expose different additional features in their SQL implementation.

You may decide the set of databases to test. Then use http://dbunit.sourceforge.net to simplify the testing job.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.