PostreSQL client in D
I’m sick of MySQL. Like, really.
I have some production code written in D that connects to a MySQL server, and it does a fine job at it. Now I’m using PostgreSQL and I need a new client library for that. It doesn’t exist. Well lets write one, shall we?
There’s already a client library for C in the form of libpq. D has an excellent bridge for C libraries. The idea is that you simply rewrite the header file of the library you want into a format D can understand, and wrap the important functions in D code. You create a bridge, in other words.
Then there’s bcd, and I quote:
BCD stands for Bindings to C(++) in D. Its most notable component is bcd.gen, which generates C or C++ bindings from .h files.
Well great, we need that. To my surprise, it creates a file that isn’t too far of. With a bit of emacs ninjaing, we manage to get a file called ‘libpq_fe.d’ that compiles and everything. We add one or two compatibility wrapper functions for D, and we’re on our merry way.
The finished file is here. There’s some sample usage in the top of the file in a comment block. It’s far from complete, but you can query, select in an assoc manner and stuff like that. Enjoy.