项目作者: IndabaConsultores

项目描述 :
Support library for externalizing SQL queries from Java code (LGPL)
高级语言: Java
项目地址: git://github.com/IndabaConsultores/sql-definition-support.git
创建时间: 2017-08-25T07:13:12Z
项目社区:https://github.com/IndabaConsultores/sql-definition-support

开源协议:GNU Lesser General Public License v3.0

下载


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

  1. <dependency>
  2. <groupId>es.indaba</groupId>
  3. <artifactId>sql-definition-support</artifactId>
  4. <version>1.1.1</version>
  5. </dependency>

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

  1. MY_QUERY_1 {
  2. SELECT *
  3. FRON ANY_TABLE
  4. WHERE A=? OR B=?
  5. }
  6. -- Second query
  7. MY_QUERY_2 {
  8. -- This is my favorite query
  9. SELECT *
  10. FRON FAVORITE_TABLE
  11. WHERE C=?
  12. }
  13. ...

YAML syntax is also supported for files with .ysqld extension

com/test/test.ysqld

  1. query1: |
  2. QUERY1_CONTENT
  3. query2: |
  4. QUERY2_CONTENT
  5. query5: |
  6. Select *
  7. from table
  8. where a=1
  9. and b=?

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

  1. ...
  2. QueryDefinitionsStaticHolder.loadQueryDefinitions("com.test");
  3. ...

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

  1. ...
  2. QueryDefinition query = new QueryDefinition("MY_QUERY_2");
  3. -- Get the query as a String
  4. String sqlQuery = query.getQueryAsString();
  5. ...
  6. sqlQuery = QueryDefinitionsStaticHolder.getQueryAsString("query1");
  7. ...

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.