Skip to content

Commit

Permalink
Remove CreateTable class and other small fixes
Browse files Browse the repository at this point in the history
CreateTable class is unnecessary since `createTable` will only be used
to generate a query for users to manually modify anyway.

Move examples to examples.hs for now -- help keep imports/extensions
clean

Add PGDATABASE=postgres env variable to setLocalDB

Remove unused imports and extensions
  • Loading branch information
alevy committed May 6, 2013
1 parent f85066a commit e594075
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 97 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@
/dist
/doc
/cabal-dev
/cabal.sandbox.config
/.cabal-sandbox
1 change: 1 addition & 0 deletions Database/PostgreSQL/Devel.hs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ setLocalDB dir0 = do
dir1 <- canonicalizePath dir0
setEnv "PGHOST" dir1 True
setEnv "PGDATA" dir1 True
setEnv "PGDATABASE" "postgres" True
let dir = showCommandForUser dir1 []
msh <- getEnv "SHELL"
return $ case msh of Just sh | isSuffixOf "csh" sh ->
Expand Down
100 changes: 3 additions & 97 deletions Database/PostgreSQL/ORM/CreateTable.hs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
{-# LANGUAGE CPP, FlexibleInstances, FlexibleContexts, TypeOperators #-}
{-# LANGUAGE DeriveGeneric, OverloadedStrings, MultiParamTypeClasses #-}
{-# LANGUAGE FlexibleContexts, TypeOperators, OverloadedStrings #-}

module Database.PostgreSQL.ORM.CreateTable where

import qualified Data.ByteString as S
import qualified Data.ByteString.Char8 as S8
import Data.Int
import Data.Monoid
import Database.PostgreSQL.Simple
import Database.PostgreSQL.Simple.Types
Expand All @@ -16,11 +14,6 @@ import Database.PostgreSQL.ORM.Model
import Database.PostgreSQL.ORM.Relationships
import Database.PostgreSQL.ORM.SqlType

-- import Database.PostgreSQL.ORM.Keywords
import Control.Exception
import Data.Functor
import Data.Maybe

class GDefTypes f where
gDefTypes :: f p -> [S.ByteString]
instance (SqlType c) => GDefTypes (K1 i c) where
Expand Down Expand Up @@ -48,13 +41,8 @@ createTableWithTypes except a = Query $ S.concat [
++ " has incorrect number of columns"


class (Model a, Generic a, GDefTypes (Rep a)) => CreateTable a where
createTableTypes :: ModelInfo a -> [(S.ByteString, S.ByteString)]
createTableTypes _ = []

createTable :: (CreateTable a) => a -> Query
createTable a = createTableWithTypes
(createTableTypes $ modelInfo `gAsTypeOf` a) a
createTable :: (Model a, Generic a, GDefTypes (Rep a)) => a -> Query
createTable a = createTableWithTypes [] a

createJoinTable :: (Joinable a b) => (a, b) -> Query
createJoinTable ab
Expand All @@ -70,85 +58,3 @@ createJoinTable ab
(refa, refb) = (const (undefined, undefined)
:: (a, b) -> (DBRef a, DBRef b)) ab


data Foo = Foo {
foo_key :: !DBKey
, foo_name :: String
-- , parent :: !(Maybe (DBRef Bar))
} deriving (Show, Generic)

instance Model Foo
instance CreateTable Foo

mkFoo :: String -> Foo
mkFoo = Foo NullKey

data Bar = Bar {
bar_key :: !DBKey
, bar_none :: !(Maybe Int32)
, bar_name :: !String
, bar_parent :: !(Maybe (DBRef Bar))
} deriving (Show, Generic)

instance Model Bar -- where modelInfo = underscoreModelInfo "bar"
instance CreateTable Bar where
createTableTypes _ = [("bar_string", "varchar(16)")]

mkBar :: String -> Bar
mkBar msg = Bar NullKey (Just n) msg Nothing
where n = foldl (+) 0 $ map (toEnum . fromEnum) msg

instance HasMany Bar Bar
instance HasParent Bar Bar

data Joiner = Joiner {
jkey :: !DBKey
, jcomment :: !String
, jfoo :: (DBRef Foo)
, jbar :: !(Maybe (DBRef Bar))
} deriving (Show, Generic)
instance Model Joiner
instance CreateTable Joiner

joiner :: Joiner
joiner = Joiner (DBKey 5) "join comment" (DBRef 1) Nothing

instance Joinable Foo Bar where
-- joinTable = (joinThroughModel joiner) { jtAllowModification = True }
joinTable = joinDefault
instance Joinable Bar Foo where
joinTable = joinReverse

bar :: Bar
bar = Bar NullKey (Just 44) "hi" Nothing

mkc :: IO Connection
mkc = connectPostgreSQL ""

bar' :: Bar
bar' = Bar NullKey (Just 75) "bye" Nothing

data X = X deriving (Generic)
instance RowAlias X

selfjoin :: IO [Bar :. As X Bar]
selfjoin = bracket mkc close $ \c ->
findWhere "bar.bar_key = x.bar_parent" c () :: IO [Bar :. As X Bar]

selfjoin' :: IO [(Bar,Bar)]
selfjoin' = bracket mkc close $ \c ->
map (\(b1 :. b2) -> (b1, fromAs X b2)) <$>
findWhere "bar.bar_key = X.bar_parent" c ()

getOne :: (Model a) => DBKeyType -> IO a
getOne k = bracket mkc close $ \c ->
let r = fromJust <$> findRow c (DBRef k `gAsTypeOf1` r)
in r

x :: Maybe Int32
x = Just 5

y :: Maybe Float
y = Just 6.0


82 changes: 82 additions & 0 deletions examples.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
{-# LANGUAGE TypeOperators, MultiParamTypeClasses, DeriveGeneric, OverloadedStrings #-}
import Control.Exception
import Database.PostgreSQL.ORM.CreateTable
import Database.PostgreSQL.ORM.Model
import Database.PostgreSQL.ORM.Relationships
import Database.PostgreSQL.Simple
import GHC.Generics
import Data.AsTypeOf
import Data.Int
import Data.Maybe

import Control.Applicative

data Foo = Foo {
foo_key :: !DBKey
, foo_name :: String
-- , parent :: !(Maybe (DBRef Bar))
} deriving (Show, Generic)

instance Model Foo

mkFoo :: String -> Foo
mkFoo = Foo NullKey

data Bar = Bar {
barId :: !DBKey
, barNone :: !(Maybe Int32)
, barName :: !String
, barParent :: !(Maybe (DBRef Bar))
} deriving (Show, Generic)

instance Model Bar where modelInfo = underscoreModelInfo "bar"

mkBar :: String -> Bar
mkBar msg = Bar NullKey (Just n) msg Nothing
where n = foldl (+) 0 $ map (toEnum . fromEnum) msg

instance HasMany Bar Bar
instance HasParent Bar Bar

data Joiner = Joiner {
jkey :: !DBKey
, jcomment :: !String
, jfoo :: (DBRef Foo)
, jbar :: !(Maybe (DBRef Bar))
} deriving (Show, Generic)
instance Model Joiner

joiner :: Joiner
joiner = Joiner (DBKey 5) "join comment" (DBRef 1) Nothing

instance Joinable Foo Bar where
-- joinTable = (joinThroughModel joiner) { jtAllowModification = True }
joinTable = joinDefault
instance Joinable Bar Foo where
joinTable = joinReverse

bar = Bar NullKey (Just 44) "hi" Nothing

mkc :: IO Connection
mkc = connectPostgreSQL ""

bar' :: Bar
bar' = Bar NullKey (Just 75) "bye" Nothing

data X = X deriving (Generic)
instance RowAlias X

selfjoin :: IO [Bar :. As X Bar]
selfjoin = bracket mkc close $ \c ->
findWhere "bar.id = x.parent" c () :: IO [Bar :. As X Bar]

selfjoin' :: IO [(Bar,Bar)]
selfjoin' = bracket mkc close $ \c ->
map (\(b1 :. b2) -> (b1, fromAs X b2)) <$>
findWhere "bar.bar_key = X.bar_parent" c ()

getOne :: (Model a) => DBKeyType -> IO a
getOne k = bracket mkc close $ \c ->
let r = fromJust <$> findRow c (DBRef k `gAsTypeOf1` r)
in r

0 comments on commit e594075

Please sign in to comment.