From: Frederik E. <fre...@a5...> - 2005-11-09 15:25:36
|
Hi, I just added a new SqlBind instance to HSQL.hsc instance SqlBind a => SqlBind (Maybe a) which returns NULL values as Nothing. I needed this because I'm using an algorithm that uses NULL values and I didn't want to have to call getFieldValueMB all the time. Actually I'm using my own wrapper module which uses heterogenous lists so handling NULL values through the type system is just much more convenient for me: http://ofb.net/~frederik/futility/src/HSQLExtras.hs Anyway, it was a bit of work to add this. I had to add a new method to the SqlBind class fromSqlCStringLen :: SqlType -> CString -> Int -> IO (Maybe a) fromSqlCStringLen sqlType cstr cstrLen = if cstr == nullPtr then return Nothing else fromNonNullSqlCStringLen sqlType cstr cstrLen to be used instead of fromNonNullSqlCStringLen :: SqlType -> CString -> Int -> IO (Maybe a) This is because apparently NULL values originally get into the system as nullPtr's, so SqlBind needs to be able to know about those nullPtr's in order to process NULL values. Now getFieldValueMB passes fromSqlCStringLen rather than fromNonNullSqlCStringLen to the driver. The drivers have to be updated to make use of this feature - currently they intercept NULL values and return Nothing, but they have to be changed so that they pass these along to value extractor (which will return Just Nothing in the case of my new instance). If they aren't changed, the old behavior will just be preserved so it's *not* necessary to change all of them at once. I updated MySQL (tested) and PostgreSQL (not tested), so you can see what has to be done. Now I can write something like this (it uses another wrapper, http://ofb.net/~frederik/futility/src/DBTool.hs) query0 (sql "insert into $bar values (2,null)") Just ((a::Maybe Int) :. (b::Maybe Int) :. Nil) <- query1 (sql "select * from $bar limit 1") print (a,b) ==> (Just 2,Nothing) I hope this patch is suitable. If it isn't accepted then I'll have to create my own fork of the library, because I depend on this feature in other stuff I'm distributing. Cheers, Frederik |