Skip to content

Support library for externalizing SQL queries from Java code (LGPL)

License

LGPL-3.0, GPL-3.0 licenses found

Licenses found

LGPL-3.0
COPYING.LESSER
GPL-3.0
COPYING
Notifications You must be signed in to change notification settings

IndabaConsultores/sql-definition-support

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

62 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Travis-CI Sonarcloud SonarCloud Technical Debt SonarCloud Coverage

SQL Definition Support (LGPL)

Support library for externalizing SQL queries from Java code (LGPL)

A CDI Extension to call stored procedures or database functions declaratively using hibernate.

Feel free to use this library as you wish, make sure to quote the LGPL in all used sources.

Using this project

Include Maven dependency on your pom.xml

<dependency>
	<groupId>es.indaba</groupId>
	<artifactId>sql-definition-support</artifactId>
	<version>1.1.1</version>
</dependency>

Place the queries in one or many text files with .sqld extension in the applications classpath. i.e. com/test/test-queries.sql

MY_QUERY_1 {
 SELECT * 
 FRON ANY_TABLE 
 WHERE A=? OR B=?
}
-- Second query
MY_QUERY_2 {
 -- This is my favorite query
 SELECT * 
 FRON FAVORITE_TABLE 
 WHERE C=?
}
...

YAML syntax is also supported for files with .ysqld extension

com/test/test.ysqld

query1: |
 QUERY1_CONTENT
query2: |
 QUERY2_CONTENT
query5: |
 Select * 
 from table
 where a=1
 and b=?

Load the sqld definitions on your application startup, providing the classpath prefix for restricting the search

...
QueryDefinitionsStaticHolder.loadQueryDefinitions("com.test");
...

Where you need to access to a query instantiate a QueryDefinition class with the query's key

...
QueryDefinition query = new QueryDefinition("MY_QUERY_2");
-- Get the query as a String
String sqlQuery = query.getQueryAsString();
...
sqlQuery = QueryDefinitionsStaticHolder.getQueryAsString("query1");
...

Check tests for detailed use.

References

Contribute

Pull requests are welcomed!!

This is an open debate if you find this support unnecessary or you think there is a better way to manage the SQL queries in code. Please open an issue and we will be please to discuss about it.

Licenses

This work is distributed under LGPL v3.

The text file parser (es.indaba.sqld.parser.TextBlockReader) is inspired in the work done by the WEB4J project that was released under the 3-Clause BSD License. See /LICENSES/BSD-LICENSE.txt.