[plt-scheme] Re: two ffis, reviews?

From: David Van Horn (dvanhorn at cs.brandeis.edu)
Date: Mon Sep 12 15:56:40 EDT 2005

Hello Pupeno,

I haven't had a chance to look over your libmysqlclient library, but I
can at least share some experiences I've had using the FFI to wrap MySQL
and more recently in using the foreign interface to access the MySQL
client library.

Like you, I started a project that needed access to a database, and I
decided on MySQL, in part because of its simple C interface.  At the
time, there was no foreign interface, so I looked at using either SWIG
or the foreign function interface (FFI) of mzc.  SWIG is nice because
you don't have to think, you just generate the wrapper code and run with

(I've put the SWIG interface file and Makefile needed at the following
location temporarily in case you or anyone else is interested in taking
this approach: http://www.cs.uvm.edu/~dvanhorn/tmp/mysql-swig/ )

Of course, SWIG is a monster of a program with its own set of
dependencies and idiosyncrasies.  I had a hard time getting it to run on
the impoverished web server I needed the Scheme mysql library to work
on.  Eventually I gave up dealing with it and wrote an interface using
the FFI.

The FFI was easy to use and I was able to quickly get what I needed for
the project at hand.  But then I wanted to use the interface on a
different machine with a (slightly) different version of MySQL.  Nothing
compiled.  I had to comment out this and that, make minor tweaks, etc to
get the thing to work properly.  I had to do this several times working
on several machines with various versions of MySQL.  Although the
changes were always minor, I needed to carefully read the mysql.h file
in each instance and make the appropriate changes.  This is boring, and
worse, error prone.  But, I still keep the library available online so
people can use it (mutatis mutandis!).  It's a very nice %70 solution.

When Eli's foreign interface was released, I took the opportunity to
rewrite the library using it instead of the FFI.  However, I never
finished it (no project to motivate it since I had a working MySQL
interface).  I do still plan to finish it and release it via Planet, but
I'm not going to do this until I rewrite Riverbot for v300, which in
turn, I won't do until v300 is released.

The foreign interface won't solve any of the version compatibility
problems endemic of the FFI version, but at least you're no longer
concerned with the minor changes in the header file, but now with binary
compatibility of the library files.  The best solution I could think of
was to implement a library for each binary version of MySQL.  By my
count, they are 6, 10, 12, and 14.  Six is too old to bother with, so
I've been working on implementations for 10, 12, and 14.  I can send you
the code off-list since it's embarrassingly incomplete, but I have most
of 10 and 12 done.  Maybe it will give you some ideas for your version
and answer some of the type questions.

With the foreign implementation it has been much easier to shift between
machines with slight variations of MySQL and to use DrScheme to develop
and test the library interactively.  I ran into exactly the problems you
described though: compile time definitions, such the definition of
my_ulong_ulong, aren't available.  My solution was to mark that part of
the code with a ;; FIXME.  ;)


Posted on the users mailing list.