[Haskell-beginners] SQLite3 Row <-> Data models -- Is this a typical Haskell pattern ?
Stef T
stef at ummon.com
Sun Mar 20 02:14:51 CET 2011
On Mar 19, 2011, at 5:43 PM, Antoine Latter wrote:
> On Sat, Mar 19, 2011 at 7:21 PM, Stef T <stef at ummon.com> wrote:
>> Hey Everyone,
>> Greetings, I am a total newb to haskell, so, I have a strange question (perhaps). Consider I have a data model which looks like this;
>>
>> data Brand = Brand { id :: Int,
>> name :: String,
>> created_at :: String,
>> updated_at :: String
>> }
>>
>> Now, I have a sqlite3 function such as
>>
>> checkout :: Int -> IO (Either String [[Row Value]])
>> checkout a = do
>> handle <- openConnection "/Users/stef/haskell/db/development.sqlite3"
>> execStatement handle $ "SELECT * from brands where id = " ++ show a
>>
>> From this, I get a Row/Tuple. So far, so good, but, the question _I_ have is, how do you go about mapping a returned row to the data model ? Is this even a design pattern that is used in FP languages ? I admit, I have spent numerous years in the MVC world, so, perhaps this is simply "not done". It would seem to be a much nicer thing to then do ;
>>
>
> What problem are you running into trying to do this?
>
> You would need to write functions to convert each column into the
> format you want it for the Brand type, and then pass output of the
> functions Brand data constructor. Depending on the format of the data
> and the library you're using these functions might turn out to be
> pretty simple.
>
I guess I am having a fundamental disconnect in 'how' to do it nicely (or DRY-ly).
the format of the row is ;
Right [[[("id",Int 4239),("name",Text "Zoppini"),("created_at",Text "2011-02-02 20:51:44.706633+0000"),("updated_at",Text "2011-02-02 20:51:44.706633+0000")]]]
I had attempted to do something along the lines of ;
getBrand :: Int -> Brand
getBrand a = do
b <- checkout a
Brand { id = b id, name = b name }
but, that explodes (not least of which is the id being a reserved keyword from prelude)
> It's considered good in Haskell to get data out of "weak" types into
> specific types when you want them, as in from a String or from a 'Row
> Value' - it makes manipulations of the values easier to read and you
> get the compiler's help figuring things out.
>
makes sense. It also helps -me- (and possibly future programmers/maintainers) to understand what the heck is going on.
> Which SQL library are you using?
I am using the haskell sqlite (version 0.5.2)
Regards
S.
>
>> name myBrandModel
>>
>> Thanks for reading this far, feel free to complain about my design :D
>> Regards
>> S.
>> _______________________________________________
>> Beginners mailing list
>> Beginners at haskell.org
>> http://www.haskell.org/mailman/listinfo/beginners
>>
More information about the Beginners
mailing list