The ElSql library provides this functionality.
ElSql consists of a small jar file (six public classes) that allows an external SQL file (elsql) to be loaded. The file uses a simple format to optionally provide slightly more behaviour than simply loading a file:
-- an example comment
@NAME(SelectBlogs)
@PAGING(:paging_offset,:paging_fetch)
SELECT @INCLUDE(CommonFields)
FROM blogs
WHERE id = :id
@AND(:date)
date > :date
@AND(:active)
active = :active
ORDER BY title, author
@NAME(CommonFields)
title, author, content
// Java code:
bundle.getSql("SelectBlogs", searchArgs);
The file is broken up into @NAME
blocks which can be referred to from code. Each block is defined by significant whitespace indentation. @PAGING
will insert the necessary code for paging such as FETCH/OFFSET. @AND
will only be output if the specified variable exists (helping build dynamic searches). The DSL also handles LIKE
vs =
for wildcards in searches. The goal of the optional DSL tags is to provide the common basics that often hit when trying to build dynamic SQL in a database-neutral way.
More info on the blog or user guide.