Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mapping NULLs to Maybes? #1

Open
duairc opened this issue Nov 30, 2015 · 1 comment
Open

Mapping NULLs to Maybes? #1

duairc opened this issue Nov 30, 2015 · 1 comment

Comments

@duairc
Copy link

duairc commented Nov 30, 2015

Hi,

I'm new to DSH, I haven't used it before, I'm trying to figure it out for the first time, so forgive me if I'm missing something. Basically, I have a table in a database where the fields are NULLable, and I expect to be able to map those nullable fields to Maybes in Haskell. Here's a simple example:

{-# LANGUAGE OverloadedLists #-}

import           Database.DSH
import           Data.Text (Text)

-- create table names (id serial not null, name varchar(256) null, constraint pk_names primary key(id));
names :: Q [(Integer, Maybe Text)]
names = table "names" ["id", "name"] (defaultHints [Key ["id"]])

This fails with the error:

Main.hs:8:25:
    No instance for (DSH-0.13.0.1:Database.DSH.Frontend.Internals.BasicType
                       (Maybe Text))
      arising from a use of ‘table’
    In the second argument of ‘($)’, namely
      ‘table "names" ["id", "name"] (defaultHints [Key ["id"]])’
    In the expression:
      take (toQ 20)
      $ table "names" ["id", "name"] (defaultHints [Key ["id"]])
    In an equation for ‘names’:
        names
          = take (toQ 20)
            $ table "names" ["id", "name"] (defaultHints [Key ["id"]])

If I replace Maybe Text with Text, then it compiles. But it will probably fail at runtime if it encounters a NULL from the database, right?

Could there or should there be an instance BasicType a => BasicType (Maybe a) somewhere in the guts of DSH to support this? Or alternatively, how can I detect NULL values in the database with DSH?

@ulricha
Copy link
Owner

ulricha commented Dec 1, 2015

NULL is not supported in DSH at the moment. If the database result contains NULL values due to nullable columns in base tables, DSH will fail.

I agree that representing nullable columns as Maybe is the right thing to do. The support for Maybe (and algebraic data types in general) in DSH represents data types by mapping them to nested lists. It should be possible to represent Nothing as NULL instead for a BasicType. This is somewhere on my TODO list, although not at the top.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants