The first open source projects start to appear on the web with public dependencies on jOOQ. One of them is a small backup tool called blizzys-backup by Maik Schreiber. It uses jOOQ to handle a small H2 database with 3-4 relations describing scheduled backups and backup-file meta data. Interestingly, Maik seems to prefer lazy fetching his data using an
org.jooq.Cursor, keeping the underlying
java.sql.ResultSet open during iterations. An example code snippet from the
BackupRun class can be seen here:
// below code (c) by Maik Schreiber (slightly modified)
Cursor<Record> cursor = null;
try {
cursor = database.factory()
.select(Backups.ID)
.from(Backups.BACKUPS)
.where(Backups.ID.notEqual(Integer.valueOf(backupId)))
.orderBy(Backups.RUN_TIME.desc())
.fetchLazy();
while (cursor.hasNext()) {
int backupId = cursor.fetchOne().getValue(Backups.ID).intValue();
// [...]
}
// Note: The above loop could be re-written to this
// as Cursor<R extends Record> extends Iterable<R>:
for (Record record : cursor) {
int backupId = record.getValue(...);
}
} finally {
database.closeQuietly(cursor);
}
Another example:
// below code (c) by Maik Schreiber (slightly modified)
Cursor<Record> cursor = null;
try {
cursor = database.factory()
.select(Files.ID,
Files.BACKUP_PATH)
.from(Files.FILES)
.leftOuterJoin(Entries.ENTRIES)
.on(Entries.FILE_ID.equal(Files.ID))
.where(Entries.FILE_ID.isNull())
.fetchLazy();
while (cursor.hasNext()) {
Record record = cursor.fetchOne();
FileEntry file = new FileEntry(
record.getValue(Files.ID).intValue(),
record.getValue(Files.BACKUP_PATH));
// [...]
}
} finally {
database.closeQuietly(cursor);
}
blizzys-backup auto-installs its schema at the first run. This can be seen in the
Database class. It shows that jOOQ’s planned support for DDL statements is going to add even more value to jOOQ, as this is a real need for many open-source tools. Some sample statement (today, an example of going back to string concatenation…):
factory.query("CREATE TABLE IF NOT EXISTS backups (" +
"id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, " +
"run_time DATETIME NOT NULL, " +
"num_entries INT NULL" +
")").execute();
See more of blizzys-backup here:
https://github.com/blizzy78/blizzys-backup/Like this:
Like Loading...
Published by lukaseder
I made jOOQ
View all posts by lukaseder
Congrats Lukas ! Champagne is on me ;-)
Swell, John! The flight to Japan, too? :-)