Poor Man's MySQL API for Gambit-C
By Arto on Sun, 2006-06-11 20:00. API | Gambit | hacks | Lisp | MySQL | SchemeLately I’ve been familiarizing myself with Gambit-C. This has mostly been preparatory work in anticipation of the imminent release of Termite, Guillaume Germain distributed computing system inspired by Erlang and implemented using Gambit’s serializable continuations and lightweight processes, scalable up to millions of concurrent threads.
Gambit is definitely one of the more interesting Scheme implementations out there, not to mention among the fastest; its creator Marc Feeley has been described as a performance junkie, and according to the Gambit manual, “with appropriate declarations in the source code the executable programs generated by the compiler run roughly as fast as equivalent C programs.”
However, despite its technical excellence, Gambit doesn’t, as of yet, have much of a standard library.
Yesterday I needed to interface Gambit with MySQL, and couldn’t find any existing binding to let me do that (though an SQLite 2.x interface does exist). As I wasn’t familiar enough with Gambit’s FFI system, I took the über-simple, lazy man’s approach: I created a trivial wrapper API on top of the mysql and mysqladmin command line utilities.
I now present here the result: the Poor Man’s MySQL API for Gambit-C.
Most of the generally useful functions in the MySQL C API are implemented, though not necessarily yet tested. The wrapper functions generally take the same parameters as defined in MySQL’s C API, with the obvious Lispy changes.
If you are using Gambit-C 4.0 on a Unix-like system and have the aforementioned two MySQL utilities in your PATH, you should be able to use this wrapper without much trouble. My own configuration is Gambit-C 4.0 beta 17 and MySQL 4.1 running on a Mac OS X 10.4 system.
Here’s a short usage example:
(define *user* "root")
(define *passwd* "root")
(let* ((conn (mysql-connect user: *user* password: *passwd*
db: "mysql"))
(query "SELECT host, user, password FROM user")
(result (mysql-query conn query)))
(while (mysql-fetch-row result)
(lambda (row)
(pretty-print row)))
(mysql-close conn))
You can download the code here:
http://bendiken.net/files/gambit/gambit-mysql-wrapper.tar.bz2
This wrapper will hopefully serve as a useful interim solution for Gambit hackers till a proper MySQL FFI becomes available. The same approach could also easily be adapted to e.g. get some communication going from Gambit to PostgreSQL — or any other database system providing similar command-line utilities, for that matter.
