Skip to content

A code generation platform for java with intact templates. A sql generator for prepared statements. A fluent API for easy application.

License

Notifications You must be signed in to change notification settings

jproggy/snippetory

Repository files navigation

A code generation platform for java

Fluent API

Syntaxes.FLUYT.parse("Hello $who").set("who", "world").render(System.out);

Simple Syntax

Locations

$name

Locations can have attributes.

$label(pad="15" pad.fill="."): $name(default="Snippetory")

Regions

$fields{
$name(pad="15"pad.fill="."): $value(number="#.###,##" date="long")    
}$

Regions allow loops or conditions

void renderFields(Map<String, Object> fields, Template fieldsTpl) {
    field.forEach(k, v -> 
        fieldsTpl
            .get("fields")
            .set("name", k)
            .set("value", v)
            .render()
    )
}

Regions have attributes like locations.

$products(delimiter="------------\n"){
...
}$

Comments

/// start with a triple slash

Syntax selector

To keep the template intact there are several syntaxes.

<!-- Syntax:FLUYT_X -->
<nav>
<ul>
  <t:menu>
  <li><a href="$page(enc='url')">$text</a></li>
  </t:menu>
</ul>
</nav>

The tag syntax allowes the IDE to support in closing the tags and keep the overview with code folding...

// Syntax:FLUYT_CC
// $getters{
  public $type get$Name$() {
    return $name$;
  }
// }$

And having valid java code lets the IDE maintain the templates during re-factorings.

SQL module

With the StatementRepository templates produce prepared statements in JDBC

-- $variables{
set :currency = 'EUR';
set :catIds = 17;
-- }$
SELECT products.*
FROM products
LEFT OUTER JOIN prices ON ( 
      products.id = prices.productId 
  AND prices.currency = :currency 
)
WHERE products.id IN (
    SELECT prodCat.product.id
    FROM prodCat
    /*${*/
    WHERE prodCat.categoryId IN (:catIds/*delimiter=', '*/)
    /*}$*/
)

Having prepared variables makes it easy to test the intact statement, a fluent API gives access to the data.

Statement stmt = new SqlContext()
  .uriResolver(UriResolver.resource("org/jproggy/sample"))
  .connections(() -> connection)
  .getRepository("DbAccessRepo.sql")
  .get("statement1");

catIds.forEach(id -> stmt.append("catId", id));
List<Object[]> result = stmt.set("currency", "USD")
  .list(SQL.objects());

So much more

Further information can be found on the homepage or the project blog.