m4dc4p / haskelldb

A library for building re-usable and composable SQL queries.
BSD 3-Clause "New" or "Revised" License
101 stars 17 forks source link

dbdirect now creates a type for result rows #3

Closed ghost closed 11 years ago

ghost commented 11 years ago

Hi,

what do you think of this? I needed a type signature for the result of the rows (not only insert/update, buts select). a bit quick and dirty. i'm using a flag (True/False) to determine if it's creating a result type or not.

it compiles for me and i could create my own files directly from database without problem. any suggestions are welcomed.

ghost commented 11 years ago

Hi,

I just realized this isn't too useful w/o a ProjectRec like class (ProjectRecResult), but for results. Anyway, this was only to be able to have short signatures (haskelldb won't select * (wildcard), but instead specify each column). I don't have the needed knowledge to implement this class, but i accept tutorials.

m4dc4p commented 11 years ago

Can you give me an example of the output of this change? Also, can you explain further how this isn't useful with out a ProjectRect-like class -maybe with another example?

ghost commented 11 years ago
{-# LANGUAGE EmptyDataDecls, TypeSynonymInstances #-}
{-# OPTIONS_GHC -fcontext-stack47 #-}
---------------------------------------------------------------------------
-- Generated by DB/Direct
---------------------------------------------------------------------------
module Database.Definition.PTP where

import Database.HaskellDB.DBLayout

---------------------------------------------------------------------------
-- Table type
---------------------------------------------------------------------------

type PTP =
    (RecCons Ptp_id (Expr (Maybe Int))
     (RecCons Ptp_tpr_id (Expr Int)
      (RecCons Ptp_pla_id (Expr Int)
       (RecCons Ptp_active (Expr Bool)
        (RecCons Ptp_updated_by (Expr Int)
         (RecCons Ptp_updated (Expr (Maybe CalendarTime))
          (RecCons Ptp_created (Expr (Maybe CalendarTime)) RecNil)))))))

type PTP_result =
    (RecCons Ptp_id (Maybe Int)
     (RecCons Ptp_tpr_id Int
      (RecCons Ptp_pla_id Int
       (RecCons Ptp_active Bool
        (RecCons Ptp_updated_by Int
         (RecCons Ptp_updated (Maybe CalendarTime)
          (RecCons Ptp_created (Maybe CalendarTime) RecNil)))))))

---------------------------------------------------------------------------
-- Table
---------------------------------------------------------------------------
pTP :: Table PTP
pTP = baseTable "PTP" $
      hdbMakeEntry Ptp_id #
      hdbMakeEntry Ptp_tpr_id #
      hdbMakeEntry Ptp_pla_id #
      hdbMakeEntry Ptp_active #
      hdbMakeEntry Ptp_updated_by #
      hdbMakeEntry Ptp_updated #
      hdbMakeEntry Ptp_created

This allows me for example to create a function like this

import qualified Database.Definition.PTP as PTP

get :: Database -> Int -> IO (Maybe (Record PTP.PTP_result))
get db ptp_id = fmap listToMaybe $ query db $ do
    t <- table PTP.pTP
    restrict $ (t!PTP.ptp_id) .==. (constant (Just ptp_id))
    return t

It is useful only if you don't make any projections on the view. A projection would return a diferent type (subset).