🐘🐬 mod_swift 0.7.0 - SQL databases

A feature of Apache 2 known to few is mod_dbd. Using that you can configure a SQL database connection within the Apache.conf and use that within all your Apache modules/handlers.

Though you can also write your own, Apache 2 comes with drivers for SQLite3, PostgreSQL, MySQL and Oracle. The macOS system Apache and the one than can be tapped in Homebrew includes SQLite3. On Linux you can easily install the ones you want:

sudo apt-get install libaprutil1-dbd-sqlite3 \
                     libaprutil1-dbd-pgsql

Your Swift Apache module doesn’t need to concern itself with the database type or connection, it can just ask Apache for a connection and it’ll happily return one from its pool. The code looks like this:

guard let con = req.dbdAcquire()                 else { return ... }
guard let res = con.select("SELECT * FROM pets") else { return ... }

while let row = res.next() {
  req.puts("<li>\(row[0])</li>")
}

So how does Apache know how to connect and all that? As everything in Apache, the admin can configure that in the apache.conf:

<IfModule dbd_module>
  DBDriver  sqlite3
  DBDParams "/var/data/testdb.sqlite3"
</IfModule>

Or to access your favorite OpenGroupware.org database using PostgreSQL:

<IfModule dbd_module>
  DBDriver pgsql
  DBDParams "host=127.0.0.1 port=5432 dbname=OGo user=OGo password=OGo"

  # Connection Pool Management
  DBDMin      1
  DBDKeep     2
  DBDMax     10
  DBDExptime 60
</IfModule>

You get the idea. We provide a simple wrapper for the select query shown as part of our mods_baredemo. It is still very simplistic though you can access all the mod_dbd API as demoed by that wrapper.

It is funny what stuff Apache can do, right? And all that in a little clumsy but very modular way. With mod_swift we can reduce the clumsyness and potentially come up with something quite neat.


Like it? You are welcome!

There is a Slack team and a Mailing list you can join for discussion.

Have fun! hh

Written on February 6, 2017