I’m pleased to announce the release of a Haskell library for connecting to SQL Server databases via ODBC.
The library is very simple, but what it does support should be high quality:
Haskell has lacked a stable, “drop in”, working package capable of connecting to SQL Server for years.
Existing implementations are pretty lacking, either they segfault, or they have weird bugs, and their code is difficult to correct.
This has been a bit of a sore point for people who just want to easily connect to an SQL Server database in Haskell. Many big businesses rely soley on many SQL Server databases, and this limitation excluded Haskell.
I needed an actual usable library for a client project. I have some good experience with FFI (foreign function interface) writing in Haskell, and knew that I could write something much more stable, provided I keep things simple.
ODBC is a C API that is split into a manager and a driver.
On Windows, there is an ODBC manager that comes with the OS. On Linux and OS X, the unixODBC package provides the same functionality.
Separately, for each database type, you have driver packages. When you provide a connection string, like this:
ODBC_TEST_CONNECTION_STRING='DRIVER={ODBC Driver 13 for SQL Server};SERVER=127.0.0.1;Uid=SA;Pwd=Passw0rd;Encrypt=no'
The DRIVER
tells the ODBC API which library to use.
In this case, it’s the recent SQL Server driver provided by
Microsoft. Then, ODBC functions like SQLDriverConnectW
will call that library.
I’ve put special effort into stability of the library. Talking to C can be perilous; we have to take special care in dealing with it.
Here are a number of steps taken to prevent common issues:
Connection
objects are blocked
by wrapping the Connection
in an MVar
.
This means only one thread can perform an operation on a connection
at once.Maybe
: once freed, the
Just
becomes a Nothing
. Future attempts
to close just throw an exception to say you tried to double
close.ForeignPtr
, whose finalizer (run when garbage
collected) closes and frees the C object.There are several aspects to the testing on this project. The first is:
There is limited performance work done on this project, with its main focus on correctness. But there is a benchmark suite for space usage. This means we have a baseline for future improvements. Time usage tests can come later.
It’s available on Hackage and on Stackage under the name odbc
. The README explains how to install the
Microsoft ODBC Driver and how to specify the right connection
string.
The Travis CI and AppVeyor files should also be useful to see how a from-nothing setup is able to run a SQL Server and connect to it from Haskell.
We invite you to contribute any changes on GitHub and share any experiences you have using the library, especially if you are a heavy user of SQL Server!
Subscribe to our blog via email
Email subscriptions come from our Atom feed and are handled by Blogtrottr. You will only receive notifications of blog posts, and can unsubscribe any time.