jOOQ in the wild

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/

2 thoughts on “jOOQ in the wild

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s