🇺🇸 (bumbag 🇬🇧)
Keep your SQL queries right on your waist!
Inlined SQL is a PITA.
Why make your life miserable with inlined SQL and the ugly mess of string concatenation?
Instead organize your queries your own way and grab 'em out of your fannypack when you need to use them!
Once you have fannypack in your classpath via your favorite method of choice, do the following:
- Get that inlined SQL out of your code and into some SQL files.
- Before each query in your file, add a comment that looks like this:
-- name: QueryNameHere
So for a super simplistic inventory query your file will look like this:
-- name: DemoInventoryQuery SELECT id, name, price FROM inventory;
FannyPack supports multiple queries per file, just label them with a comment like the one shown above.
Additionally, you can have comments, multi-line queries and placeholders just as you'd expect.
-- name: LongFakeUserQuery SELECT id, username, email_address, password_hash, ip_address, first_name, last_name, address, company FROM users u INNER JOIN orders o ON u.id = orders.purchaser -- Just a comment pointing out the placeholders on the line below. WHERE o.orderedOn > ? AND o.orderedOn < ? AND o.itemCount = 4 ORDER BY o.OrderedOn DESC, u.last_name ASC; -- name: CustomerCount SELECT COUNT(*) FROM customer;
- Make a fannypack instance by passing in the SQL files you've created.
var fp = FannyPack.fill("src/test/resources/queries.sql"); // FannyPack::fill takes a var-args String parameter. // Feel free to enumerate every SQL file you want to include in your pack
- Grab the query out of your pack and use it!
var q = fp.get("LongFakeUserQuery");
- If you give two (or more) queries the same name, the last one parsed wins.
- This is just a dumb helper library. It doesn't do anything to help with SQL injection. Be sure to use the proper parameter binding you'd otherwise be using.
Localization (or an excuse to add an Easter Egg)
BumBag in the above examples. You're welcome.