dystudio/java-sql-generator
Folders and files
| Name | Name | Last commit date | ||
|---|---|---|---|---|
Repository files navigation
The java-sql-generator is a framework to generate syntactically correct SQL statements with Java language. It provides uniform, typesafe, and (reasonably) easy way to create SQL statements using provided API's. It is specifically designed to handle even really complex SQL clauses. In fact, the API's were designed by looking at SQL 99 BNF ( http://savage.net.au/SQL/sql-99.bnf.html ) in one monitor, and creating the corresponding Java API in another monitor. However, there are many things omitted from automated BNF conversion, so that API would remain usable and simple. Please notice that this framework depends on API Typeable POJOs (APT) project ( http://github.com/stazz/api-typeable-pojos ). Currently APT is not in public maven repo, so you will need to download it manually. However, I am working on this issue, and already have ticket for importing APT to Maven repo. Please notice that this framework is still in very early development stage, and thus has no documentation (at all). Also, API changes are possible, and very likely. Currently there is support for data querying (SELECT) and modification (INSERT, UPDATE, DELETE). The support for CREATE clauses is still missing. Please see the tests of the implementation project on how to use the java-sql-generator framework. Typical usecase would be: // Create or acquire vendor SQLVendor vendor = SQLVendorProvider.createVendor( MyVendorClass.class ); SQLVendor vendor = ...; // Get factories QueryFactory q = vendor.getQueryFactory(); BooleanFactory b = vendor.getBooleanFactory(); TableReferenceFactory t = vendor.getTableReferenceFactory(); LiteralFactory l = vendor.getLiteralFactory(); ColumnsFactory c = vendor.getColumnsFactory(); // Use factories and builders to construct SQL statement // SELECT column // FROM table // WHERE column = 'test' QuerySpecificationBuilder builder = q.querySpecificationBuilder( ); builder.setSelect( q.columnsBuilder().addUnnamedColumns( c.colName( "column" ) ) ); builder.getFrom().addTableReferences( t.tableBuilder( t.table( t.tableName( "table" ) ) ) ); builder.getWhere().reset( b.eq( c.colName( "column"), l.s("test") ) ); // Get textual query QueryExpression query = q.createQuery( q.queryBuilder( builder.createExpression() ).createExpression() ); String sqlString = vendor.toString( query );